Skip to content

Commit 522e489

Browse files
committed
WIP: pybind test
1 parent 90226ca commit 522e489

File tree

4 files changed

+109
-2
lines changed

4 files changed

+109
-2
lines changed

CMakeLists.txt

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
cmake_minimum_required(VERSION 3.10)
2-
if (POLICY CMP0091)
2+
# Allow the user to specify the MSVC runtime
3+
if (POLICY CMP0091) # >= 3.15
34
cmake_policy(SET CMP0091 NEW)
45
endif()
56
project(myframe
6-
VERSION 0.9.9
7+
VERSION 1.0.0
78
LANGUAGES CXX
89
)
910

@@ -14,6 +15,7 @@ option(MYFRAME_GENERATE_TEST "Generate test executable program" ON)
1415
option(MYFRAME_INSTALL_TEMPLATE "Install template project" ON)
1516
option(MYFRAME_INSTALL_LAUNCHER "Install launcher program" ON)
1617
option(MYFRAME_ENABLE_ASAN "Enable ASAN" OFF)
18+
option(MYFRAME_ENABLE_PYBIND "Enable Python bindings" OFF)
1719

1820
### cmake module
1921
set(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake")
@@ -50,11 +52,16 @@ set(MYFRAME_LIB_DIR "lib")
5052
set(MYFRAME_LOG_DIR "log")
5153
set(MYFRAME_SERVICE_DIR "service")
5254
set(MYFRAME_CONF_DIR "conf")
55+
set(MYFRAME_PYTHON_DIR "lib/python")
5356

5457
### deps libs
5558
find_package(Threads REQUIRED)
5659
find_package(jsoncpp REQUIRED)
5760
find_package(glog REQUIRED)
61+
if (MYFRAME_ENABLE_PYBIND)
62+
find_package(SWIG REQUIRED)
63+
find_package(Python COMPONENTS Development REQUIRED)
64+
endif()
5865

5966
get_target_property(glog_lib_type glog::glog TYPE)
6067
get_target_property(jsoncpp_lib_type JsonCpp::JsonCpp TYPE)
@@ -70,6 +77,9 @@ endif()
7077
if (MYFRAME_GENERATE_TEST)
7178
add_subdirectory(test)
7279
endif()
80+
if (MYFRAME_ENABLE_PYBIND)
81+
add_subdirectory(python)
82+
endif()
7383

7484
### install file/dir
7585
install(FILES

python/CMakeLists.txt

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# UseSWIG generates standard target names.
2+
if (POLICY CMP0078) # >= 3.13
3+
cmake_policy(SET CMP0078 NEW)
4+
endif()
5+
# Changed in version 3.14: If policy CMP0086 is set to NEW, -module <module_name> is passed to SWIG compiler.
6+
if (POLICY CMP0086) # >= 3.14
7+
cmake_policy(SET CMP0086 NEW)
8+
endif()
9+
project(pymyframe)
10+
11+
include(${SWIG_USE_FILE})
12+
13+
set(dot_i_srcs
14+
swig/test.i
15+
)
16+
set_property(SOURCE ${dot_i_srcs} PROPERTY CPLUSPLUS ON)
17+
swig_add_library(${PROJECT_NAME}
18+
TYPE MODULE
19+
LANGUAGE python
20+
SOURCES ${dot_i_srcs}
21+
)
22+
set_property(TARGET ${PROJECT_NAME} PROPERTY SWIG_INCLUDE_DIRECTORIES
23+
${CMAKE_CURRENT_SOURCE_DIR}
24+
)
25+
target_include_directories(${PROJECT_NAME}
26+
PRIVATE
27+
${CMAKE_CURRENT_SOURCE_DIR}
28+
${Python_INCLUDE_DIRS}
29+
)
30+
target_link_libraries(${PROJECT_NAME}
31+
PRIVATE
32+
${Python_LIBRARIES}
33+
)
34+
35+
install(TARGETS
36+
${PROJECT_NAME}
37+
LIBRARY DESTINATION ${MYFRAME_PYTHON_DIR}
38+
ARCHIVE DESTINATION ${MYFRAME_PYTHON_DIR}
39+
RUNTIME DESTINATION ${MYFRAME_PYTHON_DIR}
40+
)
41+
file(TOUCH "${CMAKE_CURRENT_BINARY_DIR}/__init__.py")
42+
install(FILES
43+
"${CMAKE_CURRENT_BINARY_DIR}/myframe.py"
44+
"${CMAKE_CURRENT_BINARY_DIR}/__init__.py"
45+
PERMISSIONS
46+
OWNER_READ OWNER_WRITE
47+
GROUP_READ
48+
WORLD_READ
49+
DESTINATION ${MYFRAME_PYTHON_DIR}
50+
)

python/py_test.h

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/****************************************************************************
2+
Copyright (c) 2019, 李柯鹏
3+
All rights reserved.
4+
5+
Author: 李柯鹏 <likepeng0418@163.com>
6+
****************************************************************************/
7+
#pragma once
8+
#include <iostream>
9+
10+
namespace myframe {
11+
12+
class PyTestObj {
13+
public:
14+
PyTestObj() {
15+
std::cout << "PyTestObj constructor" << std::endl;
16+
}
17+
~PyTestObj() {
18+
std::cout << "PyTestObj destructor" << std::endl;
19+
}
20+
21+
int GetValue() const {
22+
return 42;
23+
}
24+
};
25+
26+
class PyTest {
27+
public:
28+
PyTest() {
29+
std::cout << "PyTest constructor" << std::endl;
30+
}
31+
~PyTest() {
32+
std::cout << "PyTest destructor" << std::endl;
33+
}
34+
35+
PyTestObj* CreateTestObj() {
36+
return new PyTestObj();
37+
}
38+
};
39+
40+
} // namespace myframe

python/swig/test.i

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
%module myframe
2+
3+
%{
4+
#include "py_test.h"
5+
%}
6+
7+
%include "py_test.h"

0 commit comments

Comments
 (0)