diff --git a/README.md b/README.md index f184aed..9eb6695 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# PYGEN +# CPPYGEN Automatic code generation for pybind11. @@ -9,7 +9,7 @@ write a python module using c++. ## Installation ``` -pip install pybind11-pygen +pip install cppygen ``` ## Env diff --git a/pygen/__init__.py b/cppygen/__init__.py similarity index 100% rename from pygen/__init__.py rename to cppygen/__init__.py diff --git a/pygen/__main__.py b/cppygen/__main__.py similarity index 66% rename from pygen/__main__.py rename to cppygen/__main__.py index f616cea..c5a02e0 100644 --- a/pygen/__main__.py +++ b/cppygen/__main__.py @@ -3,7 +3,7 @@ import toml -from pygen.pygen_parser import Parser +from cppygen.cppygen_parser import Parser def run(): @@ -32,7 +32,7 @@ def run(): output_dir = cwd.joinpath(configs["output_dir"]) - pygen = Parser(namespace=configs["search_namespace"]) + cppygen = Parser(namespace=configs["search_namespace"]) flags = configs["flags"] for i in configs["include_directories"]: @@ -40,19 +40,19 @@ def run(): print(f"-I{str(cwd.joinpath(i).absolute())}") for i in sources: - pygen.parse_from_file(i, lang="cpp", flags=configs["flags"]) + cppygen.parse_from_file(i, lang="cpp", flags=configs["flags"]) for i in headers: - pygen.parse_from_file(i, lang="hpp", flags=configs["flags"]) + cppygen.parse_from_file(i, lang="hpp", flags=configs["flags"]) for i in configs["include_headers"]: - pygen.add_hpp_includes(i) + cppygen.add_hpp_includes(i) - with open(str(output_dir) + "/pygen_generated.hpp", "w") as f: - f.write(pygen.hpp_generate()) + with open(str(output_dir) + "/cppygen_generated.hpp", "w") as f: + f.write(cppygen.hpp_generate()) - with open(str(output_dir) + "/pygen_generated.cpp", "w") as f: - f.write(pygen.cpp_generate()) + with open(str(output_dir) + "/cppygen_generated.cpp", "w") as f: + f.write(cppygen.cpp_generate()) if __name__ == "__main__": diff --git a/pygen/component.py b/cppygen/component.py similarity index 100% rename from pygen/component.py rename to cppygen/component.py diff --git a/pygen/pygen_parser.py b/cppygen/cppygen_parser.py similarity index 91% rename from pygen/pygen_parser.py rename to cppygen/cppygen_parser.py index 46799cc..d5521cf 100644 --- a/pygen/pygen_parser.py +++ b/cppygen/cppygen_parser.py @@ -12,13 +12,13 @@ class Parser: """ - PyGen はヘッダーを解析し、pybind11 用の関数を自動で作り出すコードジェネレー + CPPyGen はヘッダーを解析し、pybind11 用の関数を自動で作り出すコードジェネレー タです。 """ def __init__( self, - namespace: str = "pygen", + namespace: str = "cppygen", *, library_path: str | None = None, library_file: str | None = None, @@ -38,16 +38,16 @@ def __init__( Config.set_library_file(library_file) return if (library_file is None and library_path is None) and ( - pygen_libclang_path := os.environ.get("PYGEN_LIBCLANG_PATH", None) + cppygen_libclang_path := os.environ.get("CPPYGEN_LIBCLANG_PATH", None) ) is not None: - Config.set_library_file(pygen_libclang_path) + Config.set_library_file(cppygen_libclang_path) return def _get_tu(self, source: str, filename: str, flags=[]) -> TranslationUnit: if flags == None: flags = [] - if (pygen_flags := os.environ.get("PYGEN_COMPILE_FLAGS", None)) is not None: - flags.extend(pygen_flags.split(" ")) + if (cppygen_flags := os.environ.get("CPPYGEN_COMPILE_FLAGS", None)) is not None: + flags.extend(cppygen_flags.split(" ")) args = list(flags) name = "t.c" name = filename @@ -211,8 +211,8 @@ def generate(self) -> str: + "/* Custom Header Include End */\n\n" f"{self.to_decl_string()}\n" "\n" - "namespace PyGen {\n\n" - f"static inline void PyGenExport(pybind11::module_ {self._namespace})\n" + "namespace CPPyGen {\n\n" + f"static inline void CPPyGenExport(pybind11::module_ {self._namespace})\n" "{\n\n" f"{self.to_submod_string()}\n\n" f"{self.to_export_string()}\n\n" @@ -225,7 +225,7 @@ def cpp_generate(self) -> str: hpp_generate と対になる。 Export の関数の実装部分を自動生成するコード """ return ( - '#include "pygen_generated.hpp"\n\n' + '#include "cppygen_generated.hpp"\n\n' "#include \n" "#include \n" "#include \n" @@ -233,8 +233,8 @@ def cpp_generate(self) -> str: "\n" f"{self.to_decl_string()}\n" "\n" - "namespace PyGen {\n\n" - f"void PyGenExport(pybind11::module_ {self._namespace})\n" + "namespace CPPyGen {\n\n" + f"void CPPyGenExport(pybind11::module_ {self._namespace})\n" "{\n\n" f"{self.to_submod_string()}\n\n" f"{self.to_export_string()}\n\n" @@ -255,7 +255,7 @@ def hpp_generate(self) -> str: "/* Custom Header Include Start */\n" + "\n".join([f'#include "{i}"' for i in self._hpp_includes]) + "/* Custom Header Include End */\n\n" - "namespace PyGen {\n\n" - f"extern void PyGenExport(pybind11::module_ {self._namespace});\n\n" + "namespace CPPyGen {\n\n" + f"extern void CPPyGenExport(pybind11::module_ {self._namespace});\n\n" "}" ) diff --git a/pygen/logging.py b/cppygen/logging.py similarity index 100% rename from pygen/logging.py rename to cppygen/logging.py diff --git a/example/CMakeLists.txt b/example/CMakeLists.txt index c3c2268..472ff13 100644 --- a/example/CMakeLists.txt +++ b/example/CMakeLists.txt @@ -11,29 +11,29 @@ file(GLOB SHELL_SOURCES shell/*.cpp) file(GLOB SHELL_HEADERS shell/*.hpp) # class の定義は header で行い、 Header をパーサーに渡す # 自動生成 -set(pygen_generate_dir ${CMAKE_CURRENT_BINARY_DIR}) -set(pygen_generated_hpp ${CMAKE_CURRENT_BINARY_DIR}/pygen_generated.hpp) -set(pygen_generated_cpp ${CMAKE_CURRENT_BINARY_DIR}/pygen_generated.cpp) +set(cppygen_generate_dir ${CMAKE_CURRENT_BINARY_DIR}) +set(cppygen_generated_hpp ${CMAKE_CURRENT_BINARY_DIR}/cppygen_generated.hpp) +set(cppygen_generated_cpp ${CMAKE_CURRENT_BINARY_DIR}/cppygen_generated.cpp) -find_program(_PYGEN_GENERATOR pygen) +find_program(_CPPYGEN_GENERATOR cppygen) find_package(Python COMPONENTS Interpreter Development) find_package(pybind11 CONFIG) add_custom_command( - OUTPUT ${pygen_generated_hpp} ${pygen_generated_cpp} + OUTPUT ${cppygen_generated_hpp} ${cppygen_generated_cpp} COMMAND - # pygen の設定ファイルと current working directory を設定してあげる。 - ${_PYGEN_GENERATOR} ARGS # - --config_file ${CMAKE_CURRENT_LIST_DIR}/pygenconfig.toml --cwd + # cppygen の設定ファイルと current working directory を設定してあげる。 + ${_CPPYGEN_GENERATOR} ARGS # + --config_file ${CMAKE_CURRENT_LIST_DIR}/cppygenconfig.toml --cwd ${CMAKE_CURRENT_LIST_DIR} DEPENDS ${SHELL_SOURCES} - COMMENT "Generating PyGen Code To ${pygen_generated_hpp}" + COMMENT "Generating CPPyGen Code To ${cppygen_generated_hpp}" VERBATIM) # add_library(pyshell MODULE ${MAIN_SOURCES} ${SHELL_SOURCES} -# ${pygen_generated_cpp}) +# ${cppygen_generated_cpp}) pybind11_add_module(pyshell MODULE ${MAIN_SOURCES} ${SHELL_SOURCES} - ${pygen_generated_cpp}) + ${cppygen_generated_cpp}) target_link_libraries( pyshell diff --git a/example/pygenconfig.toml b/example/cppygenconfig.toml similarity index 100% rename from example/pygenconfig.toml rename to example/cppygenconfig.toml diff --git a/pyproject.toml b/pyproject.toml index e0adbd5..85814a3 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -9,14 +9,14 @@ pythonVersion = "3.10" pythonPlatform = "Darwin" [tool.poetry] -name = "pygen" +name = "cppygen" version = "0.1.0" description = "A simple c++ code generator for pybind11" readme = "README.md" authors = ["Gen740 "] [tool.poetry.scripts] -pygen = 'pygen.__main__:run' +cppygen = 'cppygen.__main__:run' [tool.setuptools.packages.find] where = ["."] diff --git a/tests/expect_out/header b/tests/expect_out/header index 3031738..148b7a8 100644 --- a/tests/expect_out/header +++ b/tests/expect_out/header @@ -1,4 +1,4 @@ -#include "pygen_generated.hpp" +#include "cppygen_generated.hpp" #include #include @@ -15,9 +15,9 @@ namespace Shell { auto i(int ); } -namespace PyGen { +namespace CPPyGen { -void PyGenExport(pybind11::module_ Shell) +void CPPyGenExport(pybind11::module_ Shell) { /* Submodule Declarations Start */ @@ -52,8 +52,8 @@ void PyGenExport(pybind11::module_ Shell) /* Custom Header Include Start */ /* Custom Header Include End */ -namespace PyGen { +namespace CPPyGen { -extern void PyGenExport(pybind11::module_ Shell); +extern void CPPyGenExport(pybind11::module_ Shell); } diff --git a/tests/sources/test.cpp b/tests/sources/test.cpp index 820da45..0eaf5e0 100644 --- a/tests/sources/test.cpp +++ b/tests/sources/test.cpp @@ -5,7 +5,7 @@ //! INCLUDE THIS HPP #include "test.hpp" -namespace pygen { +namespace cppygen { void f() {} diff --git a/tests/sources/test.hpp b/tests/sources/test.hpp index da114b9..3749c85 100644 --- a/tests/sources/test.hpp +++ b/tests/sources/test.hpp @@ -1,6 +1,6 @@ #pragma once -namespace pygen { +namespace cppygen { struct Hoge { public: @@ -11,4 +11,4 @@ struct Hoge { int b = 3; }; -} // namespace pygen +} // namespace cppygen diff --git a/tests/test_components.py b/tests/test_components.py index dc577ab..835bab5 100644 --- a/tests/test_components.py +++ b/tests/test_components.py @@ -1,4 +1,4 @@ -from pygen.component import Function, StructOrClass, Submodule +from cppygen.component import Function, StructOrClass, Submodule def test_function(): diff --git a/tests/test_pygen.py b/tests/test_cppygen.py similarity index 78% rename from tests/test_pygen.py rename to tests/test_cppygen.py index e3fa342..d534329 100644 --- a/tests/test_pygen.py +++ b/tests/test_cppygen.py @@ -2,15 +2,15 @@ import pytest -from pygen.pygen_parser import Parser +from cppygen.cppygen_parser import Parser -def test_pygen_valueerror(): +def test_cppygen_valueerror(): with pytest.raises(ValueError): Parser(library_file="/usr/lib", library_path="/usr/lib") -def test_pygen(): +def test_cppygen(): p = Parser() p.parse_from_file( @@ -26,4 +26,4 @@ def test_pygen(): print(p.hpp_generate()) -test_pygen() +test_cppygen() diff --git a/tests/test_flags.py b/tests/test_flags.py deleted file mode 100644 index 7408da5..0000000 --- a/tests/test_flags.py +++ /dev/null @@ -1,16 +0,0 @@ -from pygen.pygen_parser import Parser - - -def main(): - with open("./sources/hoge.cpp", "r") as f: - data = f.read() - p = Parser() - p.parse(data, flags=["-Isources"]) - # print(p.to_decl_string()) - # print(p.to_submod_string()) - # print(p.to_export_string()) - print(p.generate()) - - -if __name__ == "__main__": - main()