Repository navigation
Expand file tree
/
Copy pathCMakeLists.txt
More file actions
305 lines (263 loc) · 8.93 KB
/
Copy pathCMakeLists.txt
File metadata and controls
305 lines (263 loc) · 8.93 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
cmake_minimum_required(VERSION 3.30)
# Export compile commands to root directory
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
include(cmake/CPM.cmake)
CPMAddPackage(
NAME cmake_git_versioning
GITHUB_REPOSITORY Katze719/cmake-git-versioning
GIT_TAG v1.1.0
)
# Include cmake-git-versioning module
include(${cmake_git_versioning_SOURCE_DIR}/cmake/cmake-git-versioning.cmake)
get_git_version_info()
project(
cpp-bindings-windows
VERSION "${GIT_VERSION_MAJOR}.${GIT_VERSION_MINOR}.${GIT_VERSION_PATCH}"
DESCRIPTION "Windows implementation of cpp-core serial communication bindings"
LANGUAGES CXX
)
# Check after project() so cross-compilation toolchains can initialize WIN32.
if(NOT WIN32)
message(FATAL_ERROR "cpp-bindings-windows can only be built for Windows.")
endif()
file(WRITE "${CMAKE_BINARY_DIR}/env.bat" "set PACKAGE_VERSION=${GIT_DESCRIBE_NO_V}\n")
# Set C++ standard
set(CMAKE_CXX_STANDARD 26)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
# Enable C++26 module support
set(CMAKE_CXX_MODULE_STD 26)
set(CMAKE_CXX_MODULE_EXTENSIONS OFF)
option(
CPP_BINDINGS_WINDOWS_ENABLE_FFI_JSON_EXPORT
"Enable ASTrein JSON export for the cpp-core FFI headers"
OFF
)
option(
CPP_BINDINGS_WINDOWS_STATIC_MSVC_RUNTIME
"Statically link the MSVC runtime into the shared library"
OFF
)
set(
CPP_BINDINGS_WINDOWS_ASTREIN_EXECUTABLE
""
CACHE FILEPATH
"Path to the ASTrein executable used for FFI JSON export"
)
set(
CPP_BINDINGS_WINDOWS_FFI_JSON_OUTPUT
"${CMAKE_BINARY_DIR}/cpp_bindings_windows.ffi.json"
CACHE FILEPATH
"Output path for the generated cpp-core FFI API metadata"
)
CPMAddPackage(
NAME cpp_core
GITHUB_REPOSITORY Serial-IO/cpp-core
GIT_TAG v3.0.0
OPTIONS
"CMAKE_EXPORT_COMPILE_COMMANDS OFF"
)
# The Windows bindings consume cpp-core's C API and non-reflection helpers.
# Released clang-cl 22 does not expose -freflection, so do not propagate that
# experimental option into these targets.
if(
CMAKE_CXX_COMPILER_ID STREQUAL "Clang"
AND CMAKE_CXX_COMPILER_FRONTEND_VARIANT STREQUAL "MSVC"
)
set_property(TARGET cpp_core PROPERTY INTERFACE_COMPILE_OPTIONS "")
endif()
if(CPP_BINDINGS_WINDOWS_ENABLE_FFI_JSON_EXPORT)
if(CPP_BINDINGS_WINDOWS_ASTREIN_EXECUTABLE)
set(_cpp_bindings_windows_astrein "${CPP_BINDINGS_WINDOWS_ASTREIN_EXECUTABLE}")
else()
find_program(_cpp_bindings_windows_astrein NAMES astrein astrein.exe)
endif()
if(NOT _cpp_bindings_windows_astrein)
message(
FATAL_ERROR
"CPP_BINDINGS_WINDOWS_ENABLE_FFI_JSON_EXPORT=ON requires ASTrein. "
"Install astrein or set CPP_BINDINGS_WINDOWS_ASTREIN_EXECUTABLE."
)
endif()
file(
GLOB_RECURSE _cpp_bindings_windows_ffi_headers
CONFIGURE_DEPENDS
"${cpp_core_SOURCE_DIR}/include/*.h"
"${cpp_core_SOURCE_DIR}/include/*.hpp"
)
set(_cpp_bindings_windows_ffi_wrapper "${CMAKE_BINARY_DIR}/ffi.cpp")
get_filename_component(
_cpp_bindings_windows_ffi_output_dir
"${CPP_BINDINGS_WINDOWS_FFI_JSON_OUTPUT}"
DIRECTORY
)
file(
GENERATE
OUTPUT "${_cpp_bindings_windows_ffi_wrapper}"
CONTENT "#include <cpp_core/serial.h>\n"
)
add_library(
cpp_bindings_windows_ffi_ast_context
OBJECT
EXCLUDE_FROM_ALL
"${_cpp_bindings_windows_ffi_wrapper}"
)
target_include_directories(
cpp_bindings_windows_ffi_ast_context
PRIVATE
"${cpp_core_SOURCE_DIR}/include"
)
target_compile_definitions(
cpp_bindings_windows_ffi_ast_context
PRIVATE
cpp_bindings_windows_EXPORTS
)
target_compile_features(cpp_bindings_windows_ffi_ast_context PRIVATE cxx_std_26)
add_custom_command(
OUTPUT "${CPP_BINDINGS_WINDOWS_FFI_JSON_OUTPUT}"
COMMAND
${CMAKE_COMMAND} -E make_directory
"${_cpp_bindings_windows_ffi_output_dir}"
COMMAND
"${_cpp_bindings_windows_astrein}"
--ffi
--compile-commands "${CMAKE_BINARY_DIR}/compile_commands.json"
--require-c-linkage
--require-default-visibility
--public-header "cpp_core/serial.h"
--api-root "${cpp_core_SOURCE_DIR}/include"
--output "${CPP_BINDINGS_WINDOWS_FFI_JSON_OUTPUT}"
"${_cpp_bindings_windows_ffi_wrapper}"
DEPENDS
"${_cpp_bindings_windows_astrein}"
"${CMAKE_BINARY_DIR}/compile_commands.json"
"${_cpp_bindings_windows_ffi_wrapper}"
${_cpp_bindings_windows_ffi_headers}
COMMENT "Exporting cpp-core FFI API metadata with ASTrein"
VERBATIM
)
add_custom_target(
cpp_bindings_windows_ffi_json
DEPENDS "${CPP_BINDINGS_WINDOWS_FFI_JSON_OUTPUT}"
)
endif()
# Generate version information
generate_git_version(
OUTPUT_DIR ${CMAKE_BINARY_DIR}/generated
OUTPUT_FILE version.hpp
)
CPMAddPackage(
NAME GTest
GITHUB_REPOSITORY google/googletest
GIT_TAG v1.14.0
OPTIONS
"INSTALL_GTEST OFF"
"gtest_force_shared_crt ON"
"BUILD_GMOCK OFF"
)
# GoogleTest 1.14 enables /WX internally and triggers this Clang 22 warning in
# its char8_t printer. Keep dependency warnings from breaking our test build.
if(
CMAKE_CXX_COMPILER_ID STREQUAL "Clang"
AND CMAKE_CXX_COMPILER_FRONTEND_VARIANT STREQUAL "MSVC"
)
target_compile_options(gtest PRIVATE "/clang:-Wno-character-conversion")
target_compile_options(gtest_main PRIVATE "/clang:-Wno-character-conversion")
endif()
include(CTest)
enable_testing()
add_compile_definitions(_WIN32_WINNT=_WIN32_WINNT_WIN10 WINVER=_WIN32_WINNT)
# Library sources: src/*.cpp only, exclude *.test.cpp and test_helpers/
file(GLOB_RECURSE LIB_SOURCES CONFIGURE_DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/src/*.cpp")
list(FILTER LIB_SOURCES EXCLUDE REGEX ".*\\.test\\.cpp$")
list(FILTER LIB_SOURCES EXCLUDE REGEX ".*/test_helpers/.*")
add_library(cpp_bindings_windows SHARED ${LIB_SOURCES})
set_target_properties(
cpp_bindings_windows
PROPERTIES
VERSION "${GIT_VERSION_MAJOR}.${GIT_VERSION_MINOR}.${GIT_VERSION_PATCH}"
SOVERSION "${GIT_VERSION_MAJOR}"
OUTPUT_NAME cpp_bindings_windows
PREFIX ""
)
target_include_directories(
cpp_bindings_windows
PRIVATE
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/src>
$<BUILD_INTERFACE:${CMAKE_BINARY_DIR}/generated>
$<BUILD_INTERFACE:${cpp_core_SOURCE_DIR}/include>
)
target_link_libraries(
cpp_bindings_windows
PRIVATE
cpp_core::cpp_core
setupapi
cfgmgr32
advapi32
user32
uuid
)
target_compile_features(cpp_bindings_windows PRIVATE cxx_std_26)
if(MSVC AND CPP_BINDINGS_WINDOWS_STATIC_MSVC_RUNTIME)
set_property(
TARGET cpp_bindings_windows
PROPERTY MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>"
)
endif()
# Test sources: src/*.test.cpp, tests/*.test.cpp, src/test_helpers/*.cpp (helpers excluded from lib)
file(GLOB SRC_UNIT_TESTS CONFIGURE_DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/src/*.test.cpp")
file(GLOB TESTS_INTEGRATION CONFIGURE_DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/tests/*.test.cpp")
file(GLOB TEST_HELPER_SOURCES CONFIGURE_DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/src/test_helpers/*.cpp")
set(TEST_SOURCES ${SRC_UNIT_TESTS} ${TESTS_INTEGRATION} ${TEST_HELPER_SOURCES})
if(TEST_SOURCES)
add_executable(cpp_bindings_windows_tests ${TEST_SOURCES})
target_include_directories(
cpp_bindings_windows_tests
PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/src
${CMAKE_BINARY_DIR}/generated
${cpp_core_SOURCE_DIR}/include
)
target_link_libraries(
cpp_bindings_windows_tests
PRIVATE
cpp_bindings_windows
GTest::gtest
GTest::gtest_main
)
target_compile_features(cpp_bindings_windows_tests PRIVATE cxx_std_26)
target_compile_definitions(cpp_bindings_windows_tests PRIVATE
CPP_BINDINGS_WINDOWS_TEST_VERSION="${GIT_DESCRIBE}"
)
include(GoogleTest)
if(CMAKE_CROSSCOMPILING)
gtest_add_tests(TARGET cpp_bindings_windows_tests)
else()
gtest_discover_tests(cpp_bindings_windows_tests)
endif()
endif()
include(GNUInstallDirs)
install(
TARGETS cpp_bindings_windows
EXPORT cpp_bindings_windowsTargets
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
)
# Copy generated version header
install(
FILES ${CMAKE_BINARY_DIR}/generated/version.hpp
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
OPTIONAL
)
if(CMAKE_EXPORT_COMPILE_COMMANDS AND EXISTS "${CMAKE_BINARY_DIR}/compile_commands.json")
add_custom_target(
copy-compile-commands
ALL
${CMAKE_COMMAND} -E copy_if_different
${CMAKE_BINARY_DIR}/compile_commands.json
${CMAKE_SOURCE_DIR}/compile_commands.json
DEPENDS ${CMAKE_BINARY_DIR}/compile_commands.json
COMMENT "Copying compile_commands.json to project root"
)
endif()