Repository navigation
Expand file tree
/
Copy pathCMakeLists.txt
More file actions
301 lines (262 loc) · 10.1 KB
/
Copy pathCMakeLists.txt
File metadata and controls
301 lines (262 loc) · 10.1 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
cmake_minimum_required(VERSION 3.22)
project(jsonata
VERSION 0.1.1
DESCRIPTION "JSONata for C++"
LANGUAGES CXX
)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
include(GNUInstallDirs)
include(FetchContent)
# Find nlohmann_json or fetch if not found
find_package(nlohmann_json CONFIG QUIET)
if(NOT nlohmann_json_FOUND)
FetchContent_Declare(
nlohmann_json
GIT_REPOSITORY https://github.com/nlohmann/json.git
GIT_TAG v3.12.0
)
FetchContent_MakeAvailable(nlohmann_json)
# Mark that we used FetchContent
set(JSONATA_USED_FETCHCONTENT_JSON TRUE)
endif()
# Collect source files
file(GLOB_RECURSE HEADERS "include/*.h")
file(GLOB_RECURSE SOURCES "src/*.cpp")
# Library type option
option(JSONATA_BUILD_SHARED "Build jsonata as a shared library" OFF)
set(JSONATA_LIB_TYPE STATIC)
if(JSONATA_BUILD_SHARED)
set(JSONATA_LIB_TYPE SHARED)
endif()
# Create the library
add_library(jsonata ${JSONATA_LIB_TYPE} ${SOURCES} ${HEADERS})
set_target_properties(jsonata PROPERTIES
VERSION ${PROJECT_VERSION}
SOVERSION ${PROJECT_VERSION_MAJOR}
POSITION_INDEPENDENT_CODE ON
)
# Provide a canonical alias for consumers who add_subdirectory()
add_library(jsonata::jsonata ALIAS jsonata)
# Compiler features
target_compile_features(jsonata PUBLIC cxx_std_17)
# Compiler options
target_compile_options(jsonata
PRIVATE
# Common warnings
$<$<OR:$<CXX_COMPILER_ID:Clang>,$<CXX_COMPILER_ID:AppleClang>,$<CXX_COMPILER_ID:GNU>>:-Wall -Wno-unused-variable>
# Clang-only suppression
$<$<OR:$<CXX_COMPILER_ID:Clang>,$<CXX_COMPILER_ID:AppleClang>>:-Wno-unused-lambda-capture>
# MSVC: ensure source files are treated as UTF-8 to avoid codepage warnings
$<$<CXX_COMPILER_ID:MSVC>:/utf-8>
)
# Set include directories for the library
target_include_directories(jsonata PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/third_party/utfcpp>
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
)
# nlohmann_json is used from public headers, so propagate it PUBLIC.
# Ensure the exported target only records the imported name in the install
# interface to avoid referencing non-exported build targets.
if(TARGET nlohmann_json::nlohmann_json)
target_link_libraries(jsonata PUBLIC
$<BUILD_INTERFACE:nlohmann_json::nlohmann_json>
$<INSTALL_INTERFACE:nlohmann_json::nlohmann_json>
)
elseif(TARGET nlohmann_json)
target_link_libraries(jsonata PUBLIC
$<BUILD_INTERFACE:nlohmann_json>
$<INSTALL_INTERFACE:nlohmann_json::nlohmann_json>
)
else()
# Not present in the build; rely on the installed config to find it.
target_link_libraries(jsonata PUBLIC
$<INSTALL_INTERFACE:nlohmann_json::nlohmann_json>
)
endif()
# Optionally build tests
option(JSONATA_BUILD_TESTS "Build JSONata C++ tests" OFF)
if(JSONATA_BUILD_TESTS)
# Download GoogleTest if not already present
# See https://google.github.io/googletest/quickstart-cmake.html
FetchContent_Declare(
googletest
URL https://github.com/google/googletest/archive/refs/tags/v1.17.0.zip
DOWNLOAD_EXTRACT_TIMESTAMP true
)
FetchContent_MakeAvailable(googletest)
# Use CMake Threads package for cross-platform thread linkage
find_package(Threads REQUIRED)
# Collect all Google Test format test files
set(TEST_SOURCES
test/ArrayTest.cpp
test/CustomFunctionTest.cpp
test/DateTimeTest.cpp
test/NullSafetyTest.cpp
test/NumberTestSimplified.cpp
test/ParseIntegerTest.cpp
test/SerializationTest.cpp
test/SignatureTest.cpp
test/StringTest.cpp
test/ThreadTest.cpp
test/TypesTest.cpp
)
# Create main test executable
add_executable(
jsonata_tests
${TEST_SOURCES}
)
target_link_libraries(
jsonata_tests
jsonata
gtest_main
gtest
Threads::Threads
)
# Create test generator executable
add_executable(generate test/Generate.cpp)
target_link_libraries(generate jsonata)
# Custom command to generate test files
add_custom_command(
OUTPUT ${CMAKE_CURRENT_SOURCE_DIR}/test/gen/generated.stamp
COMMAND $<TARGET_FILE:generate>
COMMAND ${CMAKE_COMMAND} -E touch ${CMAKE_CURRENT_SOURCE_DIR}/test/gen/generated.stamp
DEPENDS generate
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
COMMENT "Generating JSONata test files from test suite"
)
# Custom target that depends on the generated files
add_custom_target(generate_tests
DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/test/gen/generated.stamp
)
# Make sure generated tests are created before building main tests
add_dependencies(jsonata_tests generate_tests)
# Convenience target to just run the generator
add_custom_target(run_generator
COMMAND $<TARGET_FILE:generate>
DEPENDS generate
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
COMMENT "Running JSONata test generator"
)
# Option to build generated tests as separate executables
option(BUILD_GENERATED_TESTS "Build generated JSONata test suites" ON)
if(BUILD_GENERATED_TESTS)
# Find all generated test files
file(GLOB GENERATED_TEST_FILES "${CMAKE_CURRENT_SOURCE_DIR}/test/gen/*Test.cpp")
if(GENERATED_TEST_FILES)
# Create executable for generated tests
add_executable(jsonata_generated_tests ${GENERATED_TEST_FILES} test/JsonataTest.cpp)
target_include_directories(jsonata_generated_tests PRIVATE test)
target_link_libraries(jsonata_generated_tests
jsonata
gtest_main
gtest
Threads::Threads
)
# Windows-only: increase stack size to 8MB for generated tests
if(WIN32)
if(MSVC)
target_link_options(jsonata_generated_tests PRIVATE /STACK:8388608)
else()
target_link_options(jsonata_generated_tests PRIVATE -Wl,--stack,8388608)
endif()
endif()
add_dependencies(jsonata_generated_tests generate_tests)
# Add to testing (only if Google Test is available)
if(TARGET gtest)
include(GoogleTest)
gtest_discover_tests(jsonata_generated_tests
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
)
endif()
endif()
endif()
# Optional: demonstrates that jsonata-cpp's pluggable regex engine
# (IRegex, see include/jsonata/IRegex.h) can be backed by Google's RE2
# instead of std::regex. Off by default. RE2 must already be available
# via find_package (e.g. installed through vcpkg/conan/brew) -- it is
# not fetched automatically like nlohmann_json, since current RE2
# releases also require Abseil, making a FetchContent fallback a much
# heavier addition than an optional test warrants.
option(JSONATA_BUILD_RE2_TEST "Build the optional RE2 regex engine test (requires re2 to be installed)" OFF)
if(JSONATA_BUILD_RE2_TEST)
find_package(re2 CONFIG REQUIRED)
add_executable(jsonata_re2_test test/RE2Engine.cpp test/RE2EngineTest.cpp)
target_link_libraries(jsonata_re2_test
jsonata
re2::re2
gtest_main
gtest
Threads::Threads
)
# re2::re2 pulls in Abseil, and Abseil's ABI (e.g. absl::string_view's
# layout) depends on which copy of its headers get resolved first.
# Other PUBLIC dependencies (e.g. a system-wide nlohmann_json found
# via find_package) can inject unrelated include directories that
# happen to also contain a differently-configured Abseil, silently
# shadowing the one re2 was actually built against and causing link
# errors. Force re2/Abseil's own include dirs to be searched first.
target_include_directories(jsonata_re2_test BEFORE PRIVATE
$<TARGET_PROPERTY:re2::re2,INTERFACE_INCLUDE_DIRECTORIES>
)
if(TARGET gtest)
include(GoogleTest)
gtest_discover_tests(jsonata_re2_test
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
)
endif()
endif()
endif()
# Installation rules
if(JSONATA_USED_FETCHCONTENT_JSON)
install(TARGETS jsonata nlohmann_json
EXPORT jsonataTargets
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
INCLUDES DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
)
else()
install(TARGETS jsonata
EXPORT jsonataTargets
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
INCLUDES DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
)
endif()
install(DIRECTORY include/ DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
install(DIRECTORY third_party/utfcpp/ DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
# Export targets for the install-tree
install(EXPORT jsonataTargets
FILE jsonataTargets.cmake
NAMESPACE jsonata::
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/jsonata
)
# Generate and install package config + version files
include(CMakePackageConfigHelpers)
write_basic_package_version_file(
"${CMAKE_CURRENT_BINARY_DIR}/jsonataConfigVersion.cmake"
VERSION ${PROJECT_VERSION}
COMPATIBILITY SameMajorVersion
)
configure_package_config_file(
cmake/jsonataConfig.cmake.in
"${CMAKE_CURRENT_BINARY_DIR}/jsonataConfig.cmake"
INSTALL_DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/jsonata
)
install(FILES
"${CMAKE_CURRENT_BINARY_DIR}/jsonataConfig.cmake"
"${CMAKE_CURRENT_BINARY_DIR}/jsonataConfigVersion.cmake"
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/jsonata
)
# Enable testing with Google Test
if(JSONATA_BUILD_TESTS)
enable_testing()
include(GoogleTest)
gtest_discover_tests(jsonata_tests
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
)
endif()