-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCMakeLists.txt
More file actions
343 lines (310 loc) · 14.8 KB
/
Copy pathCMakeLists.txt
File metadata and controls
343 lines (310 loc) · 14.8 KB
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
cmake_minimum_required(VERSION 3.16)
project(CALF VERSION 0.3.1 LANGUAGES CXX)
# -------------------------------------------------------------------------
# Options & Defaults
# -------------------------------------------------------------------------
option(CALF_LOG "Enable CALF logging globally by default" ON)
option(CALF_TESTS "Enable CALF unit tests" OFF)
option(CALF_PYTHON_TESTS "Enable CALF Python binding tests" ${CALF_TESTS})
option(CALF_BUILD_PYTHON_BINDINGS "Build Python bindings for CALF" OFF)
option(CALF_PROTOBUF "Build the Google Protobuf C++ logging component. Fallback to JSON logging when turned OFF" ON)
option(CALF_PROTOBUF_FETCH "Fetch Protobuf when an installation is unavailable" ON)
option(CALF_PROTOBUF_FORCE_FETCH "Fetch Protobuf even when it is installed" OFF)
option(CALF_PROTOBUF_REGENERATE "Add the calf_regenerate_protobuf target" OFF)
set(CALF_DEFAULT_COMPONENT_NAME "calf" CACHE STRING "Fallback component name")
set(CALF_DEFAULT_LOG_DIR_NAME "./calf_logs" CACHE STRING "Fallback default log dir name")
add_library(calf_headers INTERFACE)
add_library(calf::stl ALIAS calf_headers)
add_library(calf::syscall ALIAS calf_headers)
target_include_directories(calf_headers INTERFACE "${CMAKE_CURRENT_SOURCE_DIR}")
if(CALF_LOG)
target_compile_definitions(calf_headers INTERFACE CALF_LOG)
endif()
# ---------------------------------------------------------------------------
# Protobuf component
# ---------------------------------------------------------------------------
if(CALF_PROTOBUF)
if(NOT CALF_PROTOBUF_FORCE_FETCH)
find_package(Protobuf CONFIG QUIET)
if(NOT Protobuf_FOUND)
find_package(Protobuf QUIET)
endif()
if(Protobuf_FOUND AND NOT TARGET protobuf::protoc)
set(Protobuf_FOUND FALSE)
endif()
endif()
if(CALF_PROTOBUF_FORCE_FETCH OR NOT Protobuf_FOUND)
if(NOT CALF_PROTOBUF_FETCH AND NOT CALF_PROTOBUF_FORCE_FETCH)
message(FATAL_ERROR "Protobuf with protoc is not installed and CALF_PROTOBUF_FETCH=OFF")
endif()
include(FetchContent)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
if(DEFINED BUILD_TESTING)
set(_CALF_SAVED_BUILD_TESTING "${BUILD_TESTING}")
set(_CALF_BUILD_TESTING_WAS_DEFINED ON)
endif()
set(BUILD_TESTING OFF)
set(protobuf_BUILD_TESTS OFF CACHE BOOL "" FORCE)
set(protobuf_BUILD_SHARED_LIBS OFF CACHE BOOL "" FORCE)
set(protobuf_BUILD_EXAMPLES OFF CACHE BOOL "" FORCE)
set(protobuf_BUILD_CONFORMANCE OFF CACHE BOOL "" FORCE)
set(protobuf_BUILD_PROTOC_BINARIES ON CACHE BOOL "" FORCE)
set(protobuf_BUILD_LIBPROTOC ON CACHE BOOL "" FORCE)
set(protobuf_BUILD_LIBUPB ON CACHE BOOL "" FORCE)
set(protobuf_INSTALL OFF CACHE BOOL "" FORCE)
set(protobuf_WITH_ZLIB OFF CACHE BOOL "" FORCE)
set(utf8_range_ENABLE_INSTALL OFF CACHE BOOL "" FORCE)
set(ABSL_PROPAGATE_CXX_STD ON CACHE BOOL "" FORCE)
set(ABSL_BUILD_TESTING OFF CACHE BOOL "" FORCE)
set(ABSL_BUILD_TEST_HELPERS OFF CACHE BOOL "" FORCE)
set(ABSL_IDE_FOLDER "ThirdParty/Abseil" CACHE STRING "" FORCE)
if(CMAKE_VERSION VERSION_GREATER_EQUAL 3.25)
set(_CALF_PROTOBUF_SYSTEM SYSTEM)
endif()
FetchContent_Declare(
protobuf
GIT_REPOSITORY https://github.com/protocolbuffers/protobuf.git
# ponytail: keep one known-good download fallback; installed versions are unrestricted.
GIT_TAG v31.1
GIT_SHALLOW TRUE
${_CALF_PROTOBUF_SYSTEM}
)
unset(_CALF_PROTOBUF_SYSTEM)
# Parent projects may enable -Werror globally. Do not apply that policy to
# third-party sources, which use supported compiler extensions such as __int128.
get_directory_property(_CALF_SAVED_COMPILE_OPTIONS COMPILE_OPTIONS)
if(CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang")
add_compile_options(-Wno-error)
endif()
FetchContent_MakeAvailable(protobuf)
set_property(DIRECTORY PROPERTY COMPILE_OPTIONS "${_CALF_SAVED_COMPILE_OPTIONS}")
unset(_CALF_SAVED_COMPILE_OPTIONS)
if(_CALF_BUILD_TESTING_WAS_DEFINED)
set(BUILD_TESTING "${_CALF_SAVED_BUILD_TESTING}")
else()
unset(BUILD_TESTING)
endif()
unset(_CALF_SAVED_BUILD_TESTING)
unset(_CALF_BUILD_TESTING_WAS_DEFINED)
else()
message(STATUS "CALF using installed Protobuf ${Protobuf_VERSION}: ${Protobuf_LIBRARIES}")
endif()
if(NOT TARGET protobuf::protoc)
message(FATAL_ERROR "The selected Protobuf installation does not provide protoc")
endif()
set(_CALF_PROTOBUF_GENERATED_DIR "${CMAKE_CURRENT_BINARY_DIR}/calf/protobuf")
add_custom_command(
OUTPUT "${_CALF_PROTOBUF_GENERATED_DIR}/calf_trace.pb.cc"
"${_CALF_PROTOBUF_GENERATED_DIR}/calf_trace.pb.h"
COMMAND ${CMAKE_COMMAND} -E make_directory "${_CALF_PROTOBUF_GENERATED_DIR}"
COMMAND protobuf::protoc
--cpp_out=${CMAKE_CURRENT_BINARY_DIR}
--proto_path=${CMAKE_CURRENT_SOURCE_DIR}
${CMAKE_CURRENT_SOURCE_DIR}/calf/protobuf/calf_trace.proto
DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/calf/protobuf/calf_trace.proto protobuf::protoc
VERBATIM
)
add_library(calf_protobuf_schema STATIC
"${_CALF_PROTOBUF_GENERATED_DIR}/calf_trace.pb.cc"
"${_CALF_PROTOBUF_GENERATED_DIR}/calf_trace.pb.h"
)
add_library(calf_protobuf INTERFACE)
add_library(calf::protobuf ALIAS calf_protobuf)
set_target_properties(calf_protobuf_schema PROPERTIES POSITION_INDEPENDENT_CODE ON)
target_compile_features(calf_protobuf_schema PUBLIC cxx_std_17)
if(CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang")
target_compile_options(calf_protobuf_schema PRIVATE -Wno-error)
endif()
target_include_directories(calf_protobuf_schema
PUBLIC "${CMAKE_CURRENT_BINARY_DIR}" "${CMAKE_CURRENT_SOURCE_DIR}")
target_link_libraries(calf_protobuf_schema PUBLIC protobuf::libprotobuf)
target_compile_definitions(calf_protobuf INTERFACE CALF_LOG_FORMAT_PROTOBUF)
target_link_libraries(calf_protobuf INTERFACE calf_headers calf_protobuf_schema)
foreach(_CALF_PROTOBUF_TARGET libprotobuf libprotobuf-lite)
if(TARGET ${_CALF_PROTOBUF_TARGET})
set_property(TARGET ${_CALF_PROTOBUF_TARGET} PROPERTY FOLDER "ThirdParty/Protobuf")
endif()
endforeach()
if(CALF_PROTOBUF_REGENERATE)
add_custom_target(calf_regenerate_protobuf
COMMAND protobuf::protoc
--cpp_out=${CMAKE_CURRENT_SOURCE_DIR}
--python_out=${CMAKE_CURRENT_SOURCE_DIR}/bindings
--proto_path=${CMAKE_CURRENT_SOURCE_DIR}
${CMAKE_CURRENT_SOURCE_DIR}/calf/protobuf/calf_trace.proto
DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/calf/protobuf/calf_trace.proto protobuf::protoc
COMMENT "Regenerating CALF protobuf C++ and Python sources"
VERBATIM
)
endif()
endif()
# ---------------------------------------------------------------------------
# Helper functions (Now modifying targets directly)
# ---------------------------------------------------------------------------
function(calf_set_component target)
if (${ARGC} LESS 1 OR ${ARGC} GREATER 2)
message(FATAL_ERROR "calf_set_component invoked with incorrect number of arguments")
endif ()
set(val "${CALF_DEFAULT_COMPONENT_NAME}")
if(${ARGC} EQUAL 2)
set(val "${ARGV1}")
endif()
# Apply directly to the target scope
target_compile_definitions(${target} PRIVATE _CALF_COMPONENT_NAME="${val}")
endfunction()
function(calf_set_default_log_dir target)
if (${ARGC} LESS 1 OR ${ARGC} GREATER 2)
message(FATAL_ERROR "calf_set_default_log_dir invoked with incorrect number of arguments")
endif ()
set(val "${CALF_DEFAULT_LOG_DIR_NAME}")
if(${ARGC} EQUAL 2)
set(val "${ARGV1}")
endif()
# Apply directly to the target scope
target_compile_definitions(${target} PRIVATE _CALF_DEFAULT_LOG_DIR_NAME="${val}")
endfunction()
function(calf_enable_log target ENABLE)
if (${ARGC} GREATER 3)
message(FATAL_ERROR "calf_enable_log accepts target, ENABLE, and optional JSON or PROTOBUF format")
endif()
if (ENABLE)
target_compile_definitions(${target} PRIVATE CALF_LOG)
if(${ARGC} EQUAL 3)
string(TOUPPER "${ARGV2}" format)
elseif(CALF_PROTOBUF)
set(format "PROTOBUF")
else()
set(format "JSON")
endif()
if(format STREQUAL "PROTOBUF")
if(NOT TARGET calf::protobuf)
message(FATAL_ERROR "calf_enable_log(${target}) requested PROTOBUF but CALF_PROTOBUF is OFF")
endif()
target_link_libraries(${target} PRIVATE calf::protobuf)
elseif(format STREQUAL "JSON")
target_link_libraries(${target} PRIVATE calf_headers)
else()
message(FATAL_ERROR "calf_enable_log format must be JSON or PROTOBUF, got: ${ARGV2}")
endif()
endif ()
target_include_directories(${target} PRIVATE "${CMAKE_CURRENT_FUNCTION_LIST_DIR}")
message(STATUS "calf_enable_log(${target}) -> ${format} include dir: ${CMAKE_CURRENT_FUNCTION_LIST_DIR}")
endfunction()
function(calf_enable_warnings_as_errors target)
target_compile_options(${target} PRIVATE -Werror)
endfunction()
# ---------------------------------------------------------------------------
# Tests
# ---------------------------------------------------------------------------
if(CALF_TESTS)
include(FetchContent)
FetchContent_Declare(
googletest
GIT_REPOSITORY https://github.com/google/googletest.git
GIT_TAG v1.17.0
GIT_SHALLOW TRUE
)
FetchContent_Declare(
nlohmann_json
GIT_REPOSITORY https://github.com/nlohmann/json.git
GIT_TAG v3.12.0
GIT_SHALLOW TRUE
)
FetchContent_MakeAvailable(googletest)
FetchContent_MakeAvailable(nlohmann_json)
enable_testing()
include(GoogleTest)
add_executable(calf_core_tests tests/core_tests.cpp)
calf_enable_log(calf_core_tests ON JSON)
calf_enable_warnings_as_errors(calf_core_tests)
target_compile_features(calf_core_tests PRIVATE cxx_std_17)
target_link_libraries(calf_core_tests PRIVATE GTest::gtest_main nlohmann_json::nlohmann_json)
gtest_discover_tests(calf_core_tests)
add_executable(calf_stl_tests tests/stl_tests.cpp)
calf_enable_log(calf_stl_tests ON JSON)
calf_enable_warnings_as_errors(calf_stl_tests)
target_compile_features(calf_stl_tests PRIVATE cxx_std_17)
target_link_libraries(calf_stl_tests PRIVATE GTest::gtest_main nlohmann_json::nlohmann_json)
gtest_discover_tests(calf_stl_tests)
if(CALF_PROTOBUF)
add_executable(calf_protobuf_tests tests/protobuf_tests.cpp)
calf_enable_log(calf_protobuf_tests ON)
calf_enable_warnings_as_errors(calf_protobuf_tests)
target_compile_features(calf_protobuf_tests PRIVATE cxx_std_17)
target_link_libraries(calf_protobuf_tests PRIVATE GTest::gtest_main)
gtest_discover_tests(calf_protobuf_tests)
if(CMAKE_SYSTEM_NAME STREQUAL "Linux")
add_executable(calf_syscall_protobuf_tests tests/syscall_protobuf_tests.cpp)
calf_enable_log(calf_syscall_protobuf_tests ON)
calf_enable_warnings_as_errors(calf_syscall_protobuf_tests)
target_compile_definitions(calf_syscall_protobuf_tests PRIVATE __CALF_POSIX)
target_compile_features(calf_syscall_protobuf_tests PRIVATE cxx_std_17)
target_link_libraries(calf_syscall_protobuf_tests PRIVATE GTest::gtest_main)
gtest_discover_tests(calf_syscall_protobuf_tests)
endif()
endif()
if(CMAKE_SYSTEM_NAME STREQUAL "Linux")
add_executable(calf_syscall_tests tests/syscall_tests.cpp)
calf_enable_log(calf_syscall_tests ON JSON)
calf_enable_warnings_as_errors(calf_syscall_tests)
target_compile_definitions(calf_syscall_tests PRIVATE __CALF_POSIX)
target_compile_features(calf_syscall_tests PRIVATE cxx_std_17)
target_link_libraries(calf_syscall_tests PRIVATE GTest::gtest_main
nlohmann_json::nlohmann_json)
gtest_discover_tests(calf_syscall_tests)
endif()
add_executable(calf_disabled_tests tests/disabled_tests.cpp)
calf_enable_log(calf_disabled_tests OFF)
calf_enable_warnings_as_errors(calf_disabled_tests)
target_compile_features(calf_disabled_tests PRIVATE cxx_std_17)
target_link_libraries(calf_disabled_tests PRIVATE GTest::gtest_main)
gtest_discover_tests(calf_disabled_tests)
endif ()
# ---------------------------------------------------------------------------
# Python bindings
# ---------------------------------------------------------------------------
if(CALF_BUILD_PYTHON_BINDINGS OR CALF_PYTHON_TESTS)
include(FetchContent)
find_package(Python 3.10 COMPONENTS Interpreter Development.Module REQUIRED)
FetchContent_Declare(
pybind11
GIT_REPOSITORY https://github.com/pybind/pybind11.git
GIT_TAG v3.0.1
)
FetchContent_MakeAvailable(pybind11)
pybind11_add_module(_py_calf bindings/python_bindings.cpp)
target_include_directories(_py_calf PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}")
target_compile_features(_py_calf PRIVATE cxx_std_17)
calf_set_component(_py_calf)
calf_set_default_log_dir(_py_calf)
calf_enable_warnings_as_errors(_py_calf)
if(CALF_BUILD_PYTHON_BINDINGS)
install(TARGETS _py_calf LIBRARY DESTINATION calf)
endif()
if(CALF_PYTHON_TESTS)
if(NOT CALF_TESTS)
enable_testing()
endif()
if(WIN32)
set(CALF_TEST_PYTHON ${CMAKE_CURRENT_BINARY_DIR}/calf-python-test-env/Scripts/python.exe)
else()
set(CALF_TEST_PYTHON ${CMAKE_CURRENT_BINARY_DIR}/calf-python-test-env/bin/python)
endif()
add_custom_target(
calf_python_tests
COMMAND ${Python_EXECUTABLE} -m venv
${CMAKE_CURRENT_BINARY_DIR}/calf-python-test-env
COMMAND ${CALF_TEST_PYTHON} -m pip install "${CMAKE_CURRENT_SOURCE_DIR}[test]"
COMMAND ${CALF_TEST_PYTHON} -m pytest -v
${CMAKE_CURRENT_SOURCE_DIR}/tests/python_bindings_tests.py
USES_TERMINAL
)
add_test(
NAME calf_python_bindings_tests
COMMAND ${CMAKE_COMMAND} --build ${CMAKE_CURRENT_BINARY_DIR}
--target calf_python_tests
)
endif()
endif ()