forked from Point72/csp
-
Notifications
You must be signed in to change notification settings - Fork 1
/
CMakeLists.txt
370 lines (314 loc) · 13.8 KB
/
CMakeLists.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
###################################################################################################################################################
# Project Configuration #
#########################
cmake_minimum_required(VERSION 3.20.0)
project(csp VERSION "0.0.4")
set(CMAKE_CXX_STANDARD 20)
###################################################################################################################################################
# CMake Dependencies #
######################
include(CheckCCompilerFlag)
include(CheckLinkerFlag)
###################################################################################################################################################
# CMake Policies #
##################
# option() should use new make behavior wrt variable clobbering
cmake_policy (SET CMP0077 NEW)
# Allow dep roots from env vars
cmake_policy (SET CMP0074 NEW)
# Set CMP0094 to NEW - find the first version that matches constraints,
# instead of the latest version installed
cmake_policy(SET CMP0094 NEW)
###################################################################################################################################################
# Environment #
###############
if(${CMAKE_SYSTEM_NAME} MATCHES "Windows")
set(WIN32 ON)
set(MACOS OFF)
set(LINUX OFF)
elseif(${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
set(WIN32 OFF)
set(MACOS ON)
set(LINUX OFF)
else()
set(WIN32 OFF)
set(MACOS OFF)
set(LINUX ON)
endif()
###################################################################################################################################################
# Paths #
#########
# Custom CMake modules
if(NOT DEFINED CSP_CMAKE_MODULE_PATH)
set(CSP_CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cpp/cmake/modules")
endif()
list(PREPEND CMAKE_MODULE_PATH "${CSP_CMAKE_MODULE_PATH}")
###################################################################################################################################################
# Build Configuration #
#######################
find_package(Color)
# Build options
option(CMAKE_BUILD_TYPE "Release/Debug build" RELEASE)
option(CSP_BUILD_NO_CXX_ABI "Do not use CXX11 ABI" OFF)
option(CSP_BUILD_TESTS "Build tests" OFF)
option(CSP_BUILD_COVERAGE "Hook into gcov for coverage testing" OFF)
option(CSP_BUILD_GPROF "Hook into gprof for profiling" OFF)
option(CSP_MANYLINUX "Build for python's manylinux setup" OFF)
option(CSP_USE_VCPKG "Build with vcpkg dependencies" ON)
option(CSP_USE_CCACHE "Build with ccache caching" OFF)
option(CSP_USE_LD_CLASSIC_MAC "On macOS, link with ld_classic" OFF)
# Extension options
option(CSP_BUILD_KAFKA_ADAPTER "Build kafka adapter" ON)
option(CSP_BUILD_PARQUET_ADAPTER "Build parquet adapter" ON)
option(CSP_BUILD_WS_CLIENT_ADAPTER "Build ws client adapter" ON)
# Normalize build type for downstream comparisons
string(TOLOWER "${CMAKE_BUILD_TYPE}" CMAKE_BUILD_TYPE_LOWER)
# Python library config
set(BUILD_SHARED_LIBS TRUE)
set(CMAKE_MACOSX_RPATH TRUE)
set(CMAKE_SKIP_RPATH FALSE)
set(CMAKE_SKIP_BUILD_RPATH FALSE)
set(CMAKE_BUILD_WITH_INSTALL_RPATH TRUE)
set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)
set(CMAKE_INSTALL_NAME_DIR "@rpath")
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
# Normalize flags
string(REGEX REPLACE "[ ]*-O[^ ]+[ ]*" " " CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
string(REGEX REPLACE "[ ]*-Wl,-O2 -Wl,[^ ]+[ ]*" " " CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS}")
string(REGEX REPLACE "[ ]*-Wl,-O2 -Wl,[^ ]+[ ]*" " " CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS}")
# CCache setup
if(CSP_USE_CCACHE)
set(CMAKE_C_COMPILE_LAUNCHER ccache)
set(CMAKE_CXX_COMPILER_LAUNCHER ccache)
endif()
if(NOT DEFINED CSP_PYTHON_VERSION)
set(CSP_PYTHON_VERSION 3.12)
endif()
# Path to python folder for autogen
if(NOT DEFINED CSP_PYTHON_FOLDER)
set(CSP_PYTHON_FOLDER ${CMAKE_SOURCE_DIR}/csp)
get_filename_component(CSP_PYTHON_FOLDER ${CSP_PYTHON_FOLDER} REALPATH BASE_DIR ${CMAKE_BINARY_DIR})
endif()
if(MACOS)
# fix for threads on osx
# assume built-in pthreads on MacOS
set(CMAKE_THREAD_LIBS_INIT "-lpthread")
set(CMAKE_HAVE_THREADS_LIBRARY 1)
set(CMAKE_USE_WIN32_THREADS_INIT 0)
set(CMAKE_USE_PTHREADS_INIT 1)
set(THREADS_PREFER_PTHREAD_FLAG ON)
# don't link against build python
# https://blog.tim-smith.us/2015/09/python-extension-modules-os-x/
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -undefined dynamic_lookup")
endif()
###################################################################################################################################################
# Version Information #
#######################
# Set version from cmake and extract latest hash if available
set(CSP_VERSION_MAJOR ${PROJECT_VERSION_MAJOR})
set(CSP_VERSION_MINOR ${PROJECT_VERSION_MINOR})
set(CSP_VERSION_PATCH ${PROJECT_VERSION_PATCH})
if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/.git")
# Get latest commit
execute_process(COMMAND git rev-parse HEAD
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
OUTPUT_VARIABLE CSP_VERSION_COMMIT_SHA)
# strip newline
string(REGEX REPLACE "\n$" "" CSP_VERSION_COMMIT_SHA "${CSP_VERSION_COMMIT_SHA}")
else()
set(CSP_VERSION_COMMIT_SHA "release")
endif()
###################################################################################################################################################
# RPath #
#########
if(MACOS)
set(CMAKE_INSTALL_RPATH "@loader_path/")
elseif(LINUX)
set(CMAKE_INSTALL_RPATH "\$ORIGIN")
endif()
###################################################################################################################################################
# Flags #
# Optimization Flags
if(WIN32)
if(CMAKE_BUILD_TYPE_LOWER STREQUAL debug)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /DEBUG /Z7 /Zi")
add_definitions(-DCSP_DEBUG)
else()
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /O2")
add_definitions(-DNDEBUG)
endif()
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /EHsc /MP /bigobj")
foreach(warning 4244 4251 4267 4275 4290 4786 4305 4996)
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /wd${warning}")
endforeach(warning)
add_compile_definitions(WIN32 _WIN32)
else()
if(CSP_BUILD_NO_CXX_ABI)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -D_GLIBCXX_USE_CXX11_ABI=0")
endif()
if (COVERAGE)
# TODO windows
add_compile_options(--coverage)
set(CMAKE_CXX_FLAGS "-O0 ${CMAKE_CXX_FLAGS}")
link_libraries(gcov)
endif ()
if (GPROF_BUILD)
set(CMAKE_CXX_FLAGS "-pg ${CMAKE_CXX_FLAGS}")
set(CMAKE_EXE_LINKER_FLAGS "-pg ${CMAKE_EXE_LINKER_FLAGS}")
set(CMAKE_SHARED_LINKER_FLAGS "-pg ${CMAKE_SHARED_LINKER_FLAGS}")
endif ()
if(CMAKE_BUILD_TYPE_LOWER STREQUAL debug)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} \
-O0 \
-g3 \
")
add_definitions(-DCSP_DEBUG)
else()
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} \
-O3 \
-g0 \
-Wall \
-Wno-deprecated-declarations \
-Wno-deprecated \
")
add_definitions(-DNDEBUG)
if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} \
-Wno-maybe-uninitialized \
")
endif()
endif()
endif()
###################################################################################################################################################
# Messages #
############
message("\n${Green}Building CSP version v${CSP_VERSION_MAJOR}.${CSP_VERSION_MINOR}.${CSP_VERSION_PATCH} [${CSP_VERSION_COMMIT_SHA}]")
message("\n${Green}Building C++ binding${ColorReset}")
message("\n${Green}Building Python ${Red}${CSP_PYTHON_VERSION}${Green} binding${ColorReset}")
if(CMAKE_BUILD_TYPE_LOWER STREQUAL debug)
message("\n${Red}Building DEBUG${ColorReset}")
else()
message("\n${Green}Building RELEASE${ColorReset}")
endif()
message("\n${Green}CMake Search Path: ${CMAKE_MODULE_PATH}${ColorReset}")
###################################################################################################################################################
# Helpers #
###########
# FIXME: consolidate this function with Findcsp_autogen.cmake
function(csp_autogen MODULE_NAME DEST_FILENAME HEADER_NAME_OUTVAR SOURCE_NAME_OUTVAR)
string( REPLACE "." "\/" MODULE_FILENAME ${MODULE_NAME} )
string( JOIN "." MODULE_FILENAME ${MODULE_FILENAME} "py" )
add_custom_target( mkdir_autogen_${MODULE_NAME}
ALL COMMAND ${CMAKE_COMMAND} -E make_directory
"${CMAKE_CURRENT_BINARY_DIR}/csp_autogen" )
# VARARGS done by position
if(ARGV4)
set(CSP_AUTOGEN_EXTRA_ARGS "${ARGV4}")
else()
set(CSP_AUTOGEN_EXTRA_ARGS "")
endif()
cmake_path(SET CSP_AUTOGEN_MODULE_PATH NORMALIZE "${CMAKE_SOURCE_DIR}/csp/build/csp_autogen.py")
cmake_path(SET CSP_AUTOGEN_DESTINATION_FOLDER NORMALIZE "${CMAKE_CURRENT_BINARY_DIR}/csp_autogen")
cmake_path(SET CSP_AUTOTGEN_CPP_OUT NORMALIZE "${CMAKE_CURRENT_BINARY_DIR}/csp_autogen/${DEST_FILENAME}.cpp")
cmake_path(SET CSP_AUTOTGEN_H_OUT NORMALIZE "${CMAKE_CURRENT_BINARY_DIR}/csp_autogen/${DEST_FILENAME}.h")
if(${CMAKE_SYSTEM_NAME} MATCHES "Windows")
set(CSP_AUTOGEN_PYTHONPATH ${PROJECT_BINARY_DIR}/lib/${CMAKE_BUILD_TYPE};${CMAKE_SOURCE_DIR};%PYTHONPATH% )
else()
set(CSP_AUTOGEN_PYTHONPATH ${PROJECT_BINARY_DIR}/lib:${CMAKE_SOURCE_DIR}:$$PYTHONPATH )
endif()
add_custom_command(OUTPUT "${CSP_AUTOTGEN_CPP_OUT}" "${CSP_AUTOTGEN_H_OUT}"
COMMAND ${CMAKE_COMMAND} -E env "PYTHONPATH=${CSP_AUTOGEN_PYTHONPATH}" ${Python_EXECUTABLE} ${CSP_AUTOGEN_MODULE_PATH} -m ${MODULE_NAME} -d ${CSP_AUTOGEN_DESTINATION_FOLDER} -o ${DEST_FILENAME} ${CSP_AUTOGEN_EXTRA_ARGS}
COMMENT "generating csp c++ types from module ${MODULE_NAME}"
DEPENDS mkdir_autogen_${MODULE_NAME}
${CMAKE_SOURCE_DIR}/csp/build/csp_autogen.py
${CMAKE_SOURCE_DIR}/${MODULE_FILENAME}
csptypesimpl
)
set(${SOURCE_NAME_OUTVAR} "${CMAKE_CURRENT_BINARY_DIR}/csp_autogen/${DEST_FILENAME}.cpp" PARENT_SCOPE )
set(${HEADER_NAME_OUTVAR} "${CMAKE_CURRENT_BINARY_DIR}/csp_autogen/${DEST_FILENAME}.h" PARENT_SCOPE )
endfunction()
###################################################################################################################################################
# Dependencies #
################
find_package(DepsBase REQUIRED)
# Adapter dependencies
if(CSP_BUILD_KAFKA_ADAPTER)
find_package(DepsKafkaAdapter REQUIRED)
endif()
if(CSP_BUILD_PARQUET_ADAPTER)
find_package(DepsParquetAdapter REQUIRED)
endif()
# PYTHON
if(CSP_MANYLINUX)
# Manylinux docker images have no shared libraries
# The instead use a statically built python.
# Cmake's default FindPython can't find the python headers
# without also finding (or failing to find) the python libraries
# so we use a custom FindPythonHeaders that is the same as the
# default, but ignores when the python libraries can't be found.
message("${Red}Manylinux build has no python shared libraries${ColorReset}")
find_package(Python ${CSP_PYTHON_VERSION} EXACT REQUIRED COMPONENTS Interpreter)
find_package(PythonHeaders ${CSP_PYTHON_VERSION} EXACT REQUIRED)
# Run with exact version so its cached for pybind
find_package(PythonInterp ${CSP_PYTHON_VERSION} EXACT REQUIRED)
else()
message("${Cyan}Use python shared libraries${ColorReset}")
find_package(Python ${CSP_PYTHON_VERSION} EXACT REQUIRED COMPONENTS Interpreter Development)
# Run with exact version so its cached for pybind
find_package(PythonInterp ${CSP_PYTHON_VERSION} EXACT REQUIRED)
find_package(PythonLibs ${CSP_PYTHON_VERSION} EXACT REQUIRED)
link_directories(${Python_LIBRARY_DIRS})
endif()
message("${Cyan}Using Python ${Python_VERSION}\nPython_INCLUDE_DIRS: ${Python_INCLUDE_DIRS}\nPython_LIBRARIES: ${Python_LIBRARIES}\nPython_EXECUTABLE: ${Python_EXECUTABLE} ${ColorReset}")
include_directories(${Python_INCLUDE_DIRS})
# NUMPY
find_package(Numpy)
if(NOT NUMPY_FOUND)
message(FATAL_ERROR "${Red}Numpy could not be located${ColorReset}")
else()
message("${Cyan}Numpy found: ${NUMPY_INCLUDE_DIR}${ColorReset}")
include_directories(${NUMPY_INCLUDE_DIR})
endif()
#####################
###################################################################################################################################################
# Asset names #
################
# prefix is _ by default
set(CMAKE_SHARED_LIBRARY_PREFIX _)
if(NOT WIN32)
# shared suffix is .so for both linux and mac
set(CMAKE_SHARED_LIBRARY_SUFFIX .so)
# static suffix is _static.a # TODO decide if we want this
set(CMAKE_STATIC_LIBRARY_SUFFIX _static.a)
else()
# shared suffix is .pyd for windows
set(CMAKE_SHARED_LIBRARY_SUFFIX .pyd)
endif()
###################################################################################################################################################
# Build assets #
################
include_directories("${CMAKE_SOURCE_DIR}/cpp")
# for autogen
include_directories("${CMAKE_BINARY_DIR}/cpp")
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
if(WIN32)
# On windows, force dlls into lib folder
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
set(CSP_RUNTIME_INSTALL_SUBDIR lib/)
else()
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
set(CSP_RUNTIME_INSTALL_SUBDIR bin/)
endif()
add_subdirectory(cpp/csp/adapters)
add_subdirectory(cpp/csp/core)
add_subdirectory(cpp/csp/cppnodes)
add_subdirectory(cpp/csp/engine)
add_subdirectory(cpp/csp/python)
add_subdirectory(cpp/csp/python/adapters)
###################################################################################################################################################
# Tests #
#########
if(CSP_BUILD_TESTS)
add_subdirectory(cpp/tests)
endif()