|
1 | 1 | cmake_minimum_required(VERSION 3.20.0) |
2 | | -project(csp-example-counter-adapter VERSION "0.0.1") |
| 2 | +project(csp-example-c-api-adapter VERSION "0.0.1") |
3 | 3 | set(CMAKE_CXX_STANDARD 17) |
4 | 4 |
|
5 | | -include(CheckCCompilerFlag) |
| 5 | +# Find Python |
| 6 | +find_package(Python REQUIRED COMPONENTS Interpreter Development.Module) |
6 | 7 |
|
7 | | -if(${CMAKE_SYSTEM_NAME} MATCHES "Darwin") |
8 | | - set(MACOS ON) |
9 | | - set(LINUX OFF) |
10 | | -else() |
11 | | - set(MACOS OFF) |
12 | | - set(LINUX ON) |
13 | | -endif() |
| 8 | +# Find CSP include and lib paths |
| 9 | +execute_process( |
| 10 | + COMMAND "${Python_EXECUTABLE}" -c "import csp; print(csp.get_include_path(), end='')" |
| 11 | + OUTPUT_VARIABLE CSP_INCLUDE_DIR) |
| 12 | +execute_process( |
| 13 | + COMMAND "${Python_EXECUTABLE}" -c "import csp; print(csp.get_lib_path(), end='')" |
| 14 | + OUTPUT_VARIABLE CSP_LIB_DIR) |
14 | 15 |
|
15 | | -list(PREPEND CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/../../../cpp/cmake/modules") |
16 | | -set(ENV{PYTHONPATH} "${CMAKE_SOURCE_DIR}/../../../:$ENV{PYTHONPATH}") |
| 16 | +find_library(CSP_LIBRARY NAMES _cspimpl.so PATHS "${CSP_LIB_DIR}" NO_DEFAULT_PATH) |
17 | 17 |
|
18 | | -set(CMAKE_MACOSX_RPATH TRUE) |
19 | | -set(CMAKE_SKIP_RPATH FALSE) |
20 | | -set(CMAKE_SKIP_BUILD_RPATH FALSE) |
21 | | -set(CMAKE_BUILD_WITH_INSTALL_RPATH TRUE) |
22 | | -set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE) |
23 | | -set(CMAKE_INSTALL_NAME_DIR "@rpath") |
| 18 | +# Position independent code for shared libraries |
24 | 19 | set(CMAKE_POSITION_INDEPENDENT_CODE ON) |
25 | 20 |
|
26 | | -if(NOT DEFINED PYTHON_VERSION) |
27 | | - set(PYTHON_VERSION 3.11) |
28 | | -endif() |
29 | | - |
30 | | -find_package(Color) |
31 | | - |
32 | | -if(MACOS) |
33 | | - # fix for threads on osx |
34 | | - set(CMAKE_THREAD_LIBS_INIT "-lpthread") |
35 | | - set(CMAKE_HAVE_THREADS_LIBRARY 1) |
36 | | - set(CMAKE_USE_WIN32_THREADS_INIT 0) |
37 | | - set(CMAKE_USE_PTHREADS_INIT 1) |
38 | | - set(THREADS_PREFER_PTHREAD_FLAG ON) |
39 | | - |
40 | | - set(CMAKE_OSX_DEPLOYMENT_TARGET "10.13" CACHE STRING "Minimum OS X deployment version") |
41 | | - |
42 | | - # don't link against build python |
43 | | - set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -undefined dynamic_lookup") |
44 | | - set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,-ld_classic") |
45 | | - set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -Wl,-ld_classic") |
46 | | - |
47 | | - # Support cross build |
48 | | - check_c_compiler_flag("-arch x86_64" x86_64Supported) |
49 | | - check_c_compiler_flag("-arch arm64" arm64Supported) |
50 | | - |
51 | | - if(x86_64Supported AND arm64Supported) |
52 | | - set(CMAKE_OSX_ARCHITECTURES "x86_64;arm64" CACHE STRING "Build universal architecture for OSX" FORCE) |
53 | | - elseif(x86_64Supported) |
54 | | - set(CMAKE_REQUIRED_LINK_OPTIONS "-arch;x86_64") |
55 | | - set(CMAKE_OSX_ARCHITECTURES "x86_64" CACHE STRING "Build universal architecture for OSX" FORCE) |
56 | | - elseif(arm64Supported) |
57 | | - set(CMAKE_REQUIRED_LINK_OPTIONS "-arch;arm64") |
58 | | - set(CMAKE_OSX_ARCHITECTURES "arm64" CACHE STRING "Build universal architecture for OSX" FORCE) |
59 | | - endif() |
60 | | - |
61 | | - set(CMAKE_INSTALL_RPATH "@loader_path/../csp/lib") |
62 | | - |
63 | | - message("${Cyan}Use python shared libraries${ColorReset}") |
64 | | - find_package(Python ${PYTHON_VERSION} EXACT REQUIRED COMPONENTS Interpreter Development) |
65 | | - find_package(PythonInterp ${PYTHON_VERSION} EXACT REQUIRED) |
66 | | - find_package(PythonLibs ${PYTHON_VERSION} EXACT REQUIRED) |
67 | | - |
68 | | - link_directories(${Python_LIBRARY_DIRS}) |
69 | | -elseif(LINUX) |
70 | | - set(CMAKE_INSTALL_RPATH "\$ORIGIN/../csp/lib") |
71 | | - |
72 | | - message("${Red}Manylinux build has no python shared libraries${ColorReset}") |
73 | | - find_package(Python ${PYTHON_VERSION} EXACT REQUIRED COMPONENTS Interpreter) |
74 | | - find_package(PythonHeaders ${PYTHON_VERSION} EXACT REQUIRED) |
75 | | - find_package(PythonInterp ${PYTHON_VERSION} EXACT REQUIRED) |
76 | | -endif() |
77 | | - |
78 | | -message("${Cyan}Using Python ${Python_VERSION}\nPython_INCLUDE_DIRS: ${Python_INCLUDE_DIRS}\nPython_LIBRARIES: ${Python_LIBRARIES}\nPython_EXECUTABLE: ${Python_EXECUTABLE} ${ColorReset}") |
79 | | -include_directories(${Python_INCLUDE_DIRS}) |
80 | | - |
81 | | -# prefix is _ by default |
| 21 | +# Shared library naming conventions |
82 | 22 | set(CMAKE_SHARED_LIBRARY_PREFIX _) |
83 | | - |
84 | | -# shared suffix is .so for both linux and mac |
85 | 23 | set(CMAKE_SHARED_LIBRARY_SUFFIX .so) |
86 | 24 |
|
87 | | -find_package(CSP REQUIRED) |
88 | | -message("${Cyan}Found CSP:\n\tincludes in: ${CSP_INCLUDE_DIR}\n\tlibraries in: ${CSP_LIBS_DIR}${ColorReset}") |
89 | 25 | include_directories(${CSP_INCLUDE_DIR}) |
90 | | - |
91 | | - |
| 26 | +include_directories(${Python_INCLUDE_DIRS}) |
92 | 27 |
|
93 | 28 | # Example C adapters demonstrating the C ABI interface |
94 | | - |
95 | | -set(C_EXAMPLE_HEADER_FILES |
96 | | - cpp/ExamplePushInputAdapter.h |
97 | | - cpp/ExampleOutputAdapter.h |
98 | | - cpp/ExampleManagedAdapter.h |
99 | | -) |
100 | | - |
101 | | -set(C_EXAMPLE_SOURCE_FILES |
102 | | - cpp/ExamplePushInputAdapter.c |
103 | | - cpp/ExampleOutputAdapter.c |
104 | | - cpp/ExampleManagedAdapter.c |
105 | | -) |
106 | | - |
107 | | -add_library(csp_c_example_adapter STATIC ${C_EXAMPLE_SOURCE_FILES} ${C_EXAMPLE_HEADER_FILES}) |
108 | | - |
109 | | -# Include the engine c headers |
110 | | -target_include_directories(csp_c_example_adapter PUBLIC |
111 | | - ${CMAKE_SOURCE_DIR}/cpp |
112 | | -) |
113 | | - |
114 | | -# Set C standard |
115 | | -set_target_properties(csp_c_example_adapter PROPERTIES |
116 | | - PUBLIC_HEADER "${C_EXAMPLE_HEADER_FILES}" |
117 | | - C_STANDARD 11 |
118 | | - C_STANDARD_REQUIRED ON |
| 29 | +add_library(csp_c_example_adapter STATIC |
| 30 | + cpp/ExamplePushInputAdapter.c |
| 31 | + cpp/ExampleOutputAdapter.c |
| 32 | + cpp/ExampleManagedAdapter.c |
119 | 33 | ) |
| 34 | +target_include_directories(csp_c_example_adapter PUBLIC ${CMAKE_SOURCE_DIR}/cpp) |
| 35 | +set_target_properties(csp_c_example_adapter PROPERTIES C_STANDARD 11 C_STANDARD_REQUIRED ON) |
120 | 36 |
|
121 | | -# On non-Windows, we need pthread for the example threaded adapters |
122 | 37 | if(NOT WIN32) |
123 | 38 | target_link_libraries(csp_c_example_adapter pthread) |
124 | 39 | endif() |
125 | 40 |
|
126 | | -install(TARGETS csp_c_example_adapter |
127 | | - LIBRARY DESTINATION exampleadapter/lib |
128 | | -) |
| 41 | +install(TARGETS csp_c_example_adapter LIBRARY DESTINATION exampleadapter/lib) |
129 | 42 |
|
130 | 43 | # Python bindings for example C adapters |
131 | | - |
132 | | -# This creates a Python extension module |
133 | 44 | Python_add_library(_exampleadapterimpl MODULE cpp/exampleadapterimpl.c) |
| 45 | +target_link_libraries(_exampleadapterimpl PRIVATE csp_c_example_adapter ${CSP_LIBRARY}) |
| 46 | +target_include_directories(_exampleadapterimpl PRIVATE ${CMAKE_SOURCE_DIR}/cpp ${Python_INCLUDE_DIRS}) |
| 47 | +set_target_properties(_exampleadapterimpl PROPERTIES PREFIX "" C_STANDARD 11 C_STANDARD_REQUIRED ON) |
134 | 48 |
|
135 | | -# Link with the example adapter library |
136 | | -target_link_libraries(_exampleadapterimpl PRIVATE |
137 | | - csp_c_example_adapter |
138 | | -) |
139 | | - |
140 | | -# Include directories |
141 | | -target_include_directories(_exampleadapterimpl PRIVATE |
142 | | - ${CMAKE_SOURCE_DIR}/cpp |
143 | | - ${Python_INCLUDE_DIRS} |
144 | | -) |
145 | | - |
146 | | -# Set output name without lib prefix |
147 | | -set_target_properties(_exampleadapterimpl PROPERTIES |
148 | | - PREFIX "" |
149 | | - C_STANDARD 11 |
150 | | - C_STANDARD_REQUIRED ON |
151 | | -) |
152 | | - |
153 | | -# Install to the Python package location |
154 | | -install(TARGETS _exampleadapterimpl |
155 | | - LIBRARY DESTINATION exampleadapter |
156 | | -) |
| 49 | +install(TARGETS _exampleadapterimpl LIBRARY DESTINATION exampleadapter) |
0 commit comments