Skip to content

Commit 718e59c

Browse files
committed
Style enhancements, example cmake simplifications
Signed-off-by: Tim Paine <3105306+timkpaine@users.noreply.github.com>
1 parent a385bd6 commit 718e59c

12 files changed

Lines changed: 58 additions & 224 deletions

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ target/
9999
lib/
100100
lib64/
101101
csp/bin/
102+
csp/cmake/
102103
csp/include/
103104
csp/lib/
104105
*.so

CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,11 @@ if(NOT DEFINED CSP_CMAKE_MODULE_PATH)
5252
endif()
5353
list(PREPEND CMAKE_MODULE_PATH "${CSP_CMAKE_MODULE_PATH}")
5454

55+
# Add meta-target for cmake modules and install them with the package
56+
add_library(csp_cmake_modules INTERFACE)
57+
target_include_directories(csp_cmake_modules INTERFACE "${CMAKE_SOURCE_DIR}/cpp/cmake/modules")
58+
install(DIRECTORY "${CMAKE_SOURCE_DIR}/cpp/cmake/modules" DESTINATION "cmake/modules")
59+
5560
###################################################################################################################################################
5661
# Build Configuration #
5762
#######################

cpp/cmake/modules/FindPythonHeaders.cmake

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,10 @@ if(IS_ABSOLUTE "${Python_EXECUTABLE}")
6666
endif()
6767
endif()
6868

69+
70+
cmake_policy (SET CMP0173 OLD)
71+
cmake_policy (SET CMP0148 OLD)
72+
6973
include(CMakeFindFrameworks)
7074
# Search for the python framework on Apple.
7175
CMAKE_FIND_FRAMEWORKS(Python)

examples/05_cpp/3_cpp_adapter/CMakeLists.txt

Lines changed: 13 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -2,91 +2,28 @@ cmake_minimum_required(VERSION 3.20.0)
22
project(csp-example-counter-adapter VERSION "0.0.1")
33
set(CMAKE_CXX_STANDARD 17)
44

5-
include(CheckCCompilerFlag)
5+
# Find Python
6+
find_package(Python REQUIRED COMPONENTS Interpreter Development.Module)
67

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)
1415

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)
1717

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
2419
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
2520

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
8222
set(CMAKE_SHARED_LIBRARY_PREFIX _)
83-
84-
# shared suffix is .so for both linux and mac
8523
set(CMAKE_SHARED_LIBRARY_SUFFIX .so)
8624

87-
find_package(CSP REQUIRED)
88-
message("${Cyan}Found CSP:\n\tincludes in: ${CSP_INCLUDE_DIR}\n\tlibraries in: ${CSP_LIBS_DIR}${ColorReset}")
8925
include_directories(${CSP_INCLUDE_DIR})
26+
include_directories(${Python_INCLUDE_DIRS})
9027

9128
# The Counter Adapter library
9229
add_library(counteradapter SHARED
Lines changed: 25 additions & 132 deletions
Original file line numberDiff line numberDiff line change
@@ -1,156 +1,49 @@
11
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")
33
set(CMAKE_CXX_STANDARD 17)
44

5-
include(CheckCCompilerFlag)
5+
# Find Python
6+
find_package(Python REQUIRED COMPONENTS Interpreter Development.Module)
67

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)
1415

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)
1717

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
2419
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
2520

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
8222
set(CMAKE_SHARED_LIBRARY_PREFIX _)
83-
84-
# shared suffix is .so for both linux and mac
8523
set(CMAKE_SHARED_LIBRARY_SUFFIX .so)
8624

87-
find_package(CSP REQUIRED)
88-
message("${Cyan}Found CSP:\n\tincludes in: ${CSP_INCLUDE_DIR}\n\tlibraries in: ${CSP_LIBS_DIR}${ColorReset}")
8925
include_directories(${CSP_INCLUDE_DIR})
90-
91-
26+
include_directories(${Python_INCLUDE_DIRS})
9227

9328
# 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
11933
)
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)
12036

121-
# On non-Windows, we need pthread for the example threaded adapters
12237
if(NOT WIN32)
12338
target_link_libraries(csp_c_example_adapter pthread)
12439
endif()
12540

126-
install(TARGETS csp_c_example_adapter
127-
LIBRARY DESTINATION exampleadapter/lib
128-
)
41+
install(TARGETS csp_c_example_adapter LIBRARY DESTINATION exampleadapter/lib)
12942

13043
# Python bindings for example C adapters
131-
132-
# This creates a Python extension module
13344
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)
13448

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)

examples/05_cpp/4_c_api_adapter/cpp/ExampleManagedAdapter.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@
55
* multiple input/output adapters, similar to KafkaAdapterManager.
66
*/
77

8-
#include "ExampleManagedAdapter.h"
98
#include <stdio.h>
109
#include <stdlib.h>
1110
#include <string.h>
1211

12+
#include "ExampleManagedAdapter.h"
13+
1314
/* ============================================================================
1415
* Adapter Manager Callbacks
1516
* ============================================================================ */

examples/05_cpp/4_c_api_adapter/cpp/ExampleManagedAdapter.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,3 @@ CCspOutputAdapterVTable example_managed_output_adapter_create( ManagedAdapterSta
7878
#endif
7979

8080
#endif /* _IN_CSP_ADAPTERS_C_EXAMPLE_MANAGED_ADAPTER_H */
81-
82-
#ifdef __cplusplus
83-
}
84-
#endif
85-
86-
#endif /* _IN_CSP_ADAPTERS_C_EXAMPLE_MANAGED_ADAPTER_H */

examples/05_cpp/4_c_api_adapter/cpp/ExampleOutputAdapter.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
*
44
* This demonstrates how to implement an output adapter using the C ABI interface.
55
*/
6-
#include "ExampleOutputAdapter.h"
76
#include <csp/engine/c/OutputAdapter.h>
87
#include <csp/engine/c/CspError.h>
98
#include <csp/engine/c/CspStruct.h>
@@ -12,6 +11,8 @@
1211
#include <string.h>
1312
#include <unistd.h>
1413

14+
#include "ExampleOutputAdapter.h"
15+
1516
/* Adapter state structure */
1617
typedef struct {
1718
char * prefix; /* Prefix to print before each value */

examples/05_cpp/4_c_api_adapter/cpp/ExampleOutputAdapter.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,5 +36,3 @@ CCspOutputAdapterVTable example_output_adapter_create_fd( int fd, const char * p
3636

3737
#endif /* _IN_CSP_ADAPTERS_C_EXAMPLE_OUTPUT_ADAPTER_H */
3838

39-
#endif /* _IN_CSP_ADAPTERS_C_EXAMPLE_OUTPUT_ADAPTER_H */
40-

examples/05_cpp/4_c_api_adapter/cpp/ExamplePushInputAdapter.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@
44
* This demonstrates how to implement a push input adapter using the C ABI interface.
55
* Note: This is a simplified example. A real adapter would use proper threading.
66
*/
7-
#include "ExamplePushInputAdapter.h"
87
#include <csp/engine/c/InputAdapter.h>
98
#include <csp/engine/c/CspError.h>
109
#include <stdio.h>
1110
#include <stdlib.h>
1211
#include <string.h>
1312

13+
#include "ExamplePushInputAdapter.h"
14+
1415
#ifdef _WIN32
1516
#include <windows.h>
1617
#else

0 commit comments

Comments
 (0)