Skip to content

Commit

Permalink
Add header example
Browse files Browse the repository at this point in the history
  • Loading branch information
gen740 committed Mar 7, 2023
1 parent b625c88 commit 8bdba9c
Show file tree
Hide file tree
Showing 10 changed files with 188 additions and 1 deletion.
2 changes: 1 addition & 1 deletion cppygen/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def run():
exit(1)
for i in config_sources:
sources.extend([j for j in cwd.glob(i)])
elif mode == "source":
elif mode == "header":
if configs.get("sources"):
logger.error("Do not set sources")
exit(1)
Expand Down
1 change: 1 addition & 0 deletions example/header_mode/.clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
style: Google
58 changes: 58 additions & 0 deletions example/header_mode/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
cmake_minimum_required(VERSION 3.20)

project(pyshell LANGUAGES CXX)

set(CMAKE_EXPORT_COMPILE_COMMANDS on)

add_compile_options(-fPIC -std=c++20 -Wall -Wextra)

file(GLOB MAIN_SOURCES src/*.cpp)
file(GLOB SHELL_SOURCES shell/*.cpp shell/*.hpp)

find_package(Python COMPONENTS Interpreter Development)
find_package(pybind11 CONFIG)

# Auto Generation
set(cppygen_generated_hpp ${CMAKE_CURRENT_BINARY_DIR}/cppygen_generated.hpp)
set(cppygen_generated_cpp ${CMAKE_CURRENT_BINARY_DIR}/cppygen_generated.cpp)
set(cppygen_config_file ${CMAKE_CURRENT_LIST_DIR}/cppygenconfig.toml)

find_program(_CPPYGEN_GENERATOR cppygen)
message(${cppygen_config_file})

pybind11_add_module(pyshell MODULE ${MAIN_SOURCES} ${SHELL_SOURCES}
${cppygen_generated_cpp})

target_link_libraries(
pyshell
PUBLIC #
pybind11::module #
pybind11::lto #
pybind11::windows_extras #
${MAIN_LINK_LIBRARIES})
target_include_directories(pyshell PRIVATE ${CMAKE_CURRENT_LIST_DIR}/shell
${CMAKE_CURRENT_BINARY_DIR})

# pybind11_extension(pyshell) pybind11_strip(pyshell)

set_target_properties(
pyshell
PROPERTIES #
INTERPROCEDURAL_OPTIMIZATION ON
CXX_VISIBILITY_PRESET "hidden"
VISIBILITY_INLINES_HIDDEN ON)

add_custom_command(
OUTPUT ${cppygen_generated_hpp} ${cppygen_generated_cpp}
COMMAND
${_CPPYGEN_GENERATOR} ARGS #
--config_file ${cppygen_config_file} #
--cwd ${CMAKE_CURRENT_LIST_DIR} #
--include_directories $<TARGET_PROPERTY:pyshell,INCLUDE_DIRECTORIES> #
--verbose
DEPENDS ${SHELL_SOURCES} ${cppygen_config_file}
COMMENT
"Generating CPPyGen Code To ${cppygen_generated_hpp} and ${cppygen_generated_cpp}"
VERBATIM)

# vim:sw=2
14 changes: 14 additions & 0 deletions example/header_mode/cppygenconfig.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
mode = "header"

search_namespace = "Shell" # default: cppygen

# class の定義が書かれた Shell namespace を持つ header の一覧: 重複は解決しない
headers = ["shell/*.hpp"]

# 出力ディレクトリ
output_dir = "build"

# ここでは shell 以下のヘッダーを source で include しているためそのことを宣言してあげる必要がある。
include_directories = ["shell"]

flags = ["-Wall"]
45 changes: 45 additions & 0 deletions example/header_mode/shell/hoge.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#include "hoge.hpp"
#include <iostream>
#include <string>

namespace Shell {

void start(std::string arg = "", std::string tty = "none") {
std::cout << "This is dummy start function with arg = " << arg << std::endl;
std::cout << "tty = " << tty << std::endl;
}

// このようなユーザー定義のクラスを用いている場合は
// PyGen はデフォルトでは Header
// の場所を探しきれず、ただしく、これがユーザー定義
// のクラスを返す変数であることを認識できないので、 flag を渡して
// Header の位置を教えてあげる必要がある。
Foo make_foo() { return Foo(1, 2); }

void hoge() { std::cout << "Hello" << std::endl; }

void hoge(char) { std::cout << "Hello" << std::endl; }

void fuga() { std::cout << "Hello Fuga" << std::endl; }

int add(int a, int b) { return a + b; }

int sub(int a, int b) { return a - b; }

namespace hogehoge {

/// Piyopiyo!!!
namespace piyo {

/// Comment!!!
int add_piyo(int x, int y) { return x + y; }

int add_(int x, int y) { return x + y; }

} // namespace piyo

} // namespace hogehoge

Shell::VectorD return_vector(Shell::VectorD a) { return a; }

} // namespace Shell
29 changes: 29 additions & 0 deletions example/header_mode/shell/hoge.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#pragma once
#include <vector>

namespace Shell {

void hoge();

void hoge(char);

void fuga();

int add(int a, int b);

int sub(int a, int b);

class Foo {
public:
Foo() = default; // pybind11 から見えるのは この default コンストラクターのみ
Foo(int a, int b) : a(a), b(b) {}

void bar() {}
void bar(int) {}
int a = 32;
int b = 42;
};

typedef std::vector<double> VectorD;

} // namespace Shell
14 changes: 14 additions & 0 deletions example/header_mode/shell/piyo.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#include "piyo.hpp"
#include "hoge.hpp"
#include <vector>

namespace Shell::piyo {

Piyoyo make_piyoyo() {
Piyoyo tmp;
tmp.setValue(-5);
return tmp;
}
std::vector<double> fugafuga() { return {3, 4, 5, 6}; }

} // namespace Shell::piyo
14 changes: 14 additions & 0 deletions example/header_mode/shell/piyo.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#pragma once

#include <vector>

namespace Shell {
struct Piyoyo {
int value;
int getValue() { return value; }
void setValue(int val) { value = val; }
};

std::vector<double> fugafuga();

} // namespace Shell
8 changes: 8 additions & 0 deletions example/header_mode/src/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#include <pybind11/cast.h>
#include <pybind11/pybind11.h>
#include <pybind11/pytypes.h>
#include <pybind11/stl.h>

#include "cppygen_generated.hpp"

PYBIND11_MODULE(pyshell, m) { CPPyGen::CPPyGenExport(m); }
4 changes: 4 additions & 0 deletions example/header_mode/stubgen.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/bash

# このコマンドで stub を作る
cd ./build && stubgen -p pyshell -o ../python/stubs

0 comments on commit 8bdba9c

Please sign in to comment.