Skip to content

Commit

Permalink
change project name pygen to cppygen
Browse files Browse the repository at this point in the history
  • Loading branch information
gen740 committed Feb 3, 2023
1 parent 5fd620e commit 4efcc89
Show file tree
Hide file tree
Showing 15 changed files with 50 additions and 66 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# PYGEN
# CPPYGEN

Automatic code generation for pybind11.

Expand All @@ -9,7 +9,7 @@ write a python module using c++.

## Installation
```
pip install pybind11-pygen
pip install cppygen
```

## Env
Expand Down
File renamed without changes.
18 changes: 9 additions & 9 deletions pygen/__main__.py → cppygen/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

import toml

from pygen.pygen_parser import Parser
from cppygen.cppygen_parser import Parser


def run():
Expand Down Expand Up @@ -32,27 +32,27 @@ 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"]:
flags.append(f"-I{str(cwd.joinpath(i).absolute())}")
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__":
Expand Down
File renamed without changes.
26 changes: 13 additions & 13 deletions pygen/pygen_parser.py → cppygen/cppygen_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
Expand Down Expand Up @@ -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"
Expand All @@ -225,16 +225,16 @@ def cpp_generate(self) -> str:
hpp_generate と対になる。 Export の関数の実装部分を自動生成するコード
"""
return (
'#include "pygen_generated.hpp"\n\n'
'#include "cppygen_generated.hpp"\n\n'
"#include <pybind11/cast.h>\n"
"#include <pybind11/pybind11.h>\n"
"#include <pybind11/pytypes.h>\n"
"#include <pybind11/stl.h>\n\n"
"\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"
Expand All @@ -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"
"}"
)
File renamed without changes.
22 changes: 11 additions & 11 deletions example/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
File renamed without changes.
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 <[email protected]>"]

[tool.poetry.scripts]
pygen = 'pygen.__main__:run'
cppygen = 'cppygen.__main__:run'

[tool.setuptools.packages.find]
where = ["."]
Expand Down
10 changes: 5 additions & 5 deletions tests/expect_out/header
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "pygen_generated.hpp"
#include "cppygen_generated.hpp"

#include <pybind11/cast.h>
#include <pybind11/pybind11.h>
Expand All @@ -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 */
Expand Down Expand Up @@ -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);

}
2 changes: 1 addition & 1 deletion tests/sources/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
//! INCLUDE THIS HPP
#include "test.hpp"

namespace pygen {
namespace cppygen {

void f() {}

Expand Down
4 changes: 2 additions & 2 deletions tests/sources/test.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#pragma once

namespace pygen {
namespace cppygen {

struct Hoge {
public:
Expand All @@ -11,4 +11,4 @@ struct Hoge {
int b = 3;
};

} // namespace pygen
} // namespace cppygen
2 changes: 1 addition & 1 deletion tests/test_components.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from pygen.component import Function, StructOrClass, Submodule
from cppygen.component import Function, StructOrClass, Submodule


def test_function():
Expand Down
8 changes: 4 additions & 4 deletions tests/test_pygen.py → tests/test_cppygen.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -26,4 +26,4 @@ def test_pygen():
print(p.hpp_generate())


test_pygen()
test_cppygen()
16 changes: 0 additions & 16 deletions tests/test_flags.py

This file was deleted.

0 comments on commit 4efcc89

Please sign in to comment.