Debian Codegraph @

CMakeLists.txt

raw ◀ ▶
Source reconstructed at patch state 1 of 2 — not the installed (fully patched) source.
2 Debian patches touch this file
1 cmake_minimum_required(VERSION 3.15...4.1)
2
3 #### Check important configuration pre-requisites.
4 #
5
6 # Discourage in-source builds; they overwrite the hand-written Makefile, and generally cause confusion.
7 # Use `cmake . -B<dir>` or the CMake GUI to do an out-of-source build.
8 if (${CMAKE_SOURCE_DIR} STREQUAL ${CMAKE_BINARY_DIR})
9 message(FATAL_ERROR "Out-of-source builds are recommended for this project.")
10 endif()
11
12 #### Extract version from include/libjsonnet.h so it can be put in the project version.
13 #
14
15 # Extract release version number from include/libjsonnet.h.
16 # We do this in various other (non-CMake-build) places too, e.g., the Python wheel build.
17
18 set(JSONNET_HEADER_VERSION_REGEX
19 [=[^[ \t]*#[ \t]*define[ \t]+LIB_JSONNET_VERSION[ \t]+\"v([0-9.]+(-?[a-z][a-z0-9]*)?)\"[ \t]*$]=])
20 file(
21 STRINGS ${CMAKE_CURRENT_SOURCE_DIR}/include/libjsonnet.h JSONNET_HEADER_VERSION
22 LIMIT_COUNT 1
23 REGEX ${JSONNET_HEADER_VERSION_REGEX})
24 string(REGEX REPLACE ${JSONNET_HEADER_VERSION_REGEX} "\\1" JSONNET_HEADER_VERSION "${JSONNET_HEADER_VERSION}")
25 message(VERBOSE "Extracted version from include/libjsonnet.h: ${JSONNET_HEADER_VERSION}")
26
27 #### Set CMake project.
28 #
29
30 project(
31 jsonnet
32 HOMEPAGE_URL "https://jsonnet.org/"
33 LANGUAGES C CXX)
34
35 #### Unconditional CMake module includes.
36 #
37
38 include(GNUInstallDirs)
39
40 #### Set up cacheable/user-configurable state.
41 #
42
43 # User-configurable options.
44 option(BUILD_JSONNET "Build jsonnet command-line tool." ON)
45 option(BUILD_JSONNETFMT "Build jsonnetfmt command-line tool." ON)
46 option(BUILD_TESTS "Build and run jsonnet tests." ON)
47 option(BUILD_STATIC_LIBS "Build a static libjsonnet." ON)
48 option(BUILD_SHARED_LIBS "Build shared libjsonnet." ON)
49 option(BUILD_SHARED_BINARIES "Link binaries to the shared libjsonnet instead of the static one." OFF)
50 option(BUILD_MAN_PAGES "Build manpages." ON)
51 option(USE_SYSTEM_GTEST "Use system-provided gtest library" OFF)
52 option(USE_SYSTEM_JSON "Use the system-provided json library" OFF)
53 option(USE_SYSTEM_RAPIDYAML "Use the system-provided rapidyaml library" OFF)
54
55 #### Compute derived values from user-configurable state.
56 #
57
58 # Helper variable set to ON if there is any reason we need to build the jsonnet command binaries.
59 if(BUILD_TESTS OR BUILD_JSONNET OR BUILD_JSONNETFMT)
60 set(BUILD_SOME_BINARIES ON)
61 else()
62 set(BUILD_SOME_BINARIES OFF)
63 endif()
64
65 #### System package searches.
66 #
67
68 if(USE_SYSTEM_JSON)
69 find_package(nlohmann_json 3.6.1 REQUIRED)
70 endif()
71
72 if(USE_SYSTEM_RAPIDYAML)
73 find_package(ryml REQUIRED)
74 endif()
75
76 #### Utility function to configure a target with our preferred C and C++ compilation flags.
77 #
78
79 function(configure_target_cc_props LIB_TARGET_NAME)
80 set_property(TARGET ${LIB_TARGET_NAME} PROPERTY CXX_STANDARD 17)
81 set_property(TARGET ${LIB_TARGET_NAME} PROPERTY CXX_EXTENSIONS OFF)
82 set_property(TARGET ${LIB_TARGET_NAME} PROPERTY CXX_STANDARD_REQUIRED ON)
83 set_property(TARGET ${LIB_TARGET_NAME} PROPERTY C_STANDARD 99)
84 set_property(TARGET ${LIB_TARGET_NAME} PROPERTY C_EXTENSIONS OFF)
85 set_property(TARGET ${LIB_TARGET_NAME} PROPERTY C_STANDARD_REQUIRED ON)
86
87 if (NOT MSVC)
88 target_compile_options(${LIB_TARGET_NAME}
89 PRIVATE
90 -Wall -Wextra -Wimplicit-fallthrough
91 -pedantic
92 $<$<COMPILE_LANGUAGE:CXX>:-Woverloaded-virtual>
93 )
94 endif()
95 endfunction()
96
97 #### Sub-directory CMakeLists includes.
98 #
99
100 add_subdirectory(stdlib)
101
102 #### Utility function to set up all the properties for an OBJECT library for the jsonnet core.
103 #
104 # This is done as a function because there separate object libraries for shared vs. static.
105 # Shared has slightly different compilation flags (it needs -fPIC).
106 #
107
108 function(configure_jsonnet_obj_target LIB_TARGET_NAME)
109 configure_target_cc_props(${LIB_TARGET_NAME})
110 target_sources(${LIB_TARGET_NAME}
111 PRIVATE
112 third_party/md5/md5.h
113 third_party/md5/md5.cpp
114
115 core/static_error.h
116 core/desugarer.h
117 core/unicode.h
118 core/vm.h
119 core/static_analysis.h
120 core/path_utils.h
121 core/state.h
122 core/pass.h
123 core/ast.h
124 core/json.h
125 core/string_utils.h
126 core/formatter.h
127 core/parser.h
128 core/lexer.h
129
130 core/desugarer.cpp
131 core/formatter.cpp
132 core/lexer.cpp
133 core/libjsonnet.cpp
134 core/parser.cpp
135 core/pass.cpp
136 core/path_utils.cpp
137 core/static_analysis.cpp
138 core/string_utils.cpp
139 core/vm.cpp
140
141 PUBLIC
142 include/libjsonnet.h
143 include/libjsonnet_fmt.h
144 )
145
146 target_link_libraries(${LIB_TARGET_NAME} PRIVATE stdlib_h)
147
148 if(USE_SYSTEM_JSON)
149 target_link_libraries(${LIB_TARGET_NAME} PRIVATE nlohmann_json::nlohmann_json)
150 else()
151 target_sources(${LIB_TARGET_NAME}
152 PRIVATE
153 third_party/json/nlohmann/json.hpp
154 )
155 target_include_directories(${LIB_TARGET_NAME}
156 SYSTEM PRIVATE third_party/json)
157 endif()
158
159 if(USE_SYSTEM_RAPIDYAML)
160 target_link_libraries(${LIB_TARGET_NAME} PRIVATE ryml::ryml)
161 target_compile_definitions(${LIB_TARGET_NAME} PRIVATE USE_SYSTEM_RAPIDYAML)
162 else()
163 target_sources(${LIB_TARGET_NAME}
164 PRIVATE
165 third_party/rapidyaml/rapidyaml-0.10.0.hpp
166 third_party/rapidyaml/rapidyaml.cpp
167 )
168 endif()
169
170 target_include_directories(${LIB_TARGET_NAME}
171 PUBLIC
172 $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
173 $<INSTALL_INTERFACE:include>
174 PRIVATE
175 third_party/md5
176 $<$<NOT:$<BOOL:${USE_SYSTEM_RAPIDYAML}>>:${CMAKE_CURRENT_SOURCE_DIR}/third_party/rapidyaml>
177 )
178 endfunction()
179
180 #### Utility function to set up library target properties for the C lib.
181 #
182
183 function(configure_jsonnet_lib_target LIB_TARGET_NAME OBJ_TARGET_NAME)
184 configure_target_cc_props(${LIB_TARGET_NAME})
185 target_link_libraries(${LIB_TARGET_NAME} PUBLIC ${OBJ_TARGET_NAME})
186 set(JSONNET_PUBLIC_HEADERS include/libjsonnet.h include/libjsonnet_fmt.h)
187 set_target_properties(${LIB_TARGET_NAME} PROPERTIES
188 OUTPUT_NAME jsonnet
189 PUBLIC_HEADER "${JSONNET_PUBLIC_HEADERS}")
190 endfunction()
191
192 #### Utility function to set up library target properties for the C++ lib.
193 #
194
195 function(configure_jsonnet_cpp_lib_target LIB_TARGET_NAME OBJ_TARGET_NAME)
196 configure_target_cc_props(${LIB_TARGET_NAME})
197 target_sources(${LIB_TARGET_NAME}
198 PRIVATE
199 cpp/libjsonnet++.cpp
200 include/libjsonnet++.h
201 )
202 target_link_libraries(${LIB_TARGET_NAME} PUBLIC ${OBJ_TARGET_NAME})
203 set(JSONNET_PUBLIC_HEADERS include/libjsonnet.h include/libjsonnet++.h)
204 set_target_properties(${LIB_TARGET_NAME} PROPERTIES
205 OUTPUT_NAME jsonnet++
206 PUBLIC_HEADER "${JSONNET_PUBLIC_HEADERS}")
207 endfunction()
208
209 #### Static library target setup.
210 #
211
212 if(BUILD_STATIC_LIBS OR (BUILD_SOME_BINARIES AND NOT BUILD_SHARED_BINARIES))
213 add_library(jsonnet_obj_static OBJECT)
214 configure_jsonnet_obj_target(jsonnet_obj_static)
215
216 add_library(jsonnet_static STATIC)
217 configure_jsonnet_lib_target(jsonnet_static jsonnet_obj_static)
218
219 add_library(jsonnet_cpp_static STATIC)
220 configure_jsonnet_cpp_lib_target(jsonnet_cpp_static jsonnet_obj_static)
221
222 if(BUILD_STATIC_LIBS)
223 install(TARGETS jsonnet_static jsonnet_cpp_static)
224 endif()
225 endif()
226
227 #### Shared library target setup.
228 #
229
230 if(BUILD_SHARED_LIBS OR (BUILD_SOME_BINARIES AND BUILD_SHARED_BINARIES))
231 add_library(jsonnet_obj_shared OBJECT)
232 set_property(TARGET jsonnet_obj_shared PROPERTY POSITION_INDEPENDENT_CODE ON)
233 configure_jsonnet_obj_target(jsonnet_obj_shared)
234
235 add_library(jsonnet_shared SHARED)
236 configure_jsonnet_lib_target(jsonnet_shared jsonnet_obj_shared)
237 set_target_properties(jsonnet_shared PROPERTIES
238 VERSION "${JSONNET_HEADER_VERSION}"
239 SOVERSION "0")
240
241 add_library(jsonnet_cpp_shared SHARED)
242 configure_jsonnet_cpp_lib_target(jsonnet_cpp_shared jsonnet_obj_shared)
243 set_target_properties(jsonnet_cpp_shared PROPERTIES
244 VERSION "${JSONNET_HEADER_VERSION}"
245 SOVERSION "0")
246
247 if(BUILD_SHARED_LIBS)
248 install(TARGETS jsonnet_shared jsonnet_cpp_shared)
249 endif()
250 endif()
251
252 #### Alias the library to use for binaries.
253 #
254
255 if(BUILD_SHARED_BINARIES)
256 add_library(jsonnet_lib_for_binaries ALIAS jsonnet_shared)
257 add_library(jsonnet_cpp_lib_for_binaries ALIAS jsonnet_cpp_shared)
258 else()
259 add_library(jsonnet_lib_for_binaries ALIAS jsonnet_static)
260 add_library(jsonnet_cpp_lib_for_binaries ALIAS jsonnet_cpp_static)
261 endif()
262
263 #### An object library for the utils.cpp code which is shared by both CLI binaries.
264 #
265
266 if(BUILD_SOME_BINARIES)
267 add_library(jsonnet_cmd_utils OBJECT
268 cmd/utils.cpp
269 cmd/utils.h
270 )
271 endif()
272
273 #### CLI (jsonnet and jsonnetfmt) binary targets.
274 #
275
276 if(BUILD_JSONNET OR BUILD_TESTS)
277 add_executable(jsonnet
278 cmd/jsonnet.cpp
279 $<TARGET_OBJECTS:jsonnet_cmd_utils>
280 )
281 target_link_libraries(jsonnet PRIVATE jsonnet_lib_for_binaries)
282 if(BUILD_JSONNET)
283 install(TARGETS jsonnet)
284 endif()
285 endif()
286
287 if(BUILD_JSONNETFMT OR BUILD_TESTS)
288 add_executable(jsonnetfmt
289 cmd/jsonnetfmt.cpp
290 $<TARGET_OBJECTS:jsonnet_cmd_utils>
291 )
292 target_link_libraries(jsonnetfmt PRIVATE jsonnet_lib_for_binaries)
293 if(BUILD_JSONNETFMT)
294 install(TARGETS jsonnetfmt)
295 endif()
296 endif()
297
298 #### GoogleTest based tests.
299 #
300
301 if(BUILD_TESTS)
302 enable_testing()
303 include(GoogleTest)
304
305 if(USE_SYSTEM_GTEST)
306 find_package(GTest REQUIRED)
307 else()
308 include(FetchContent)
309 FetchContent_Declare(googletest
310 GIT_REPOSITORY https://github.com/google/googletest.git
311 GIT_TAG v1.17.0
312 )
313 # For Windows: Prevent overriding the parent project's compiler/linker settings.
314 set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
315 # Prevent including GoogleTest in the generated install rules.
316 set(INSTALL_GTEST OFF CACHE BOOL "" FORCE)
317 FetchContent_MakeAvailable(googletest)
318 endif()
319
320 set(JSONNET_TEST_TARGETS)
321
322 function(jsonnet_add_gtest TESTNAME JSONNET_LIB_TARGET)
323 set(JSONNET_TEST_TARGETS ${JSONNET_TEST_TARGETS} ${TESTNAME} PARENT_SCOPE)
324 add_executable(${TESTNAME} ${ARGN})
325 target_link_libraries(${TESTNAME} ${JSONNET_LIB_TARGET} gtest gmock gtest_main)
326 gtest_discover_tests(${TESTNAME}
327 WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
328 PROPERTIES
329 VS_DEBUGGER_WORKING_DIRECTORY "${PROJECT_SOURCE_DIR}"
330 ENVIRONMENT "JSONNET_SOURCE_BASE=${PROJECT_SOURCE_DIR}"
331 )
332 set_target_properties(${TESTNAME} PROPERTIES FOLDER tests)
333 endfunction()
334
335 function(jsonnet_add_raw_test TESTNAME JSONNET_LIB_TARGET)
336 set(JSONNET_TEST_TARGETS ${JSONNET_TEST_TARGETS} ${TESTNAME} PARENT_SCOPE)
337 add_executable(${TESTNAME} ${ARGN})
338 target_link_libraries(${TESTNAME} ${JSONNET_LIB_TARGET})
339 set_target_properties(${TESTNAME} PROPERTIES FOLDER tests)
340 add_test(NAME ${TESTNAME} COMMAND ${TESTNAME})
341 endfunction()
342
343 jsonnet_add_gtest(test_core_unicode jsonnet_lib_for_binaries core/unicode_test.cpp)
344 jsonnet_add_gtest(test_core_lexer jsonnet_lib_for_binaries core/lexer_test.cpp)
345 jsonnet_add_gtest(test_core_parser jsonnet_lib_for_binaries core/parser_test.cpp)
346 jsonnet_add_gtest(test_core_libjsonnet jsonnet_lib_for_binaries core/libjsonnet_test.cpp)
347 jsonnet_add_gtest(test_cpp_libjsonnet jsonnet_cpp_lib_for_binaries cpp/libjsonnet++_test.cpp)
348 jsonnet_add_gtest(test_cpp_libjsonnet_locale jsonnet_cpp_lib_for_binaries cpp/libjsonnet_locale_test.cpp)
349
350 jsonnet_add_raw_test(test_core_libjsonnet_native_callbacks jsonnet_lib_for_binaries core/libjsonnet_native_callbacks_test.c)
351
352 # core/libjsonnet_file_test needs an input file
353 add_executable(test_libjsonnet_file core/libjsonnet_file_test.c)
354 target_link_libraries(test_libjsonnet_file jsonnet_lib_for_binaries)
355 set_target_properties(test_libjsonnet_file PROPERTIES FOLDER tests)
356 add_test(
357 NAME test_libjsonnet_file
358 COMMAND test_libjsonnet_file "test_suite/object.jsonnet"
359 WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
360 )
361
362 add_subdirectory(test_suite)
363
364 # `run_tests` target builds and runs all tests
365 add_custom_target(run_tests COMMAND ${CMAKE_CTEST_COMMAND}
366 DEPENDS
367 jsonnet jsonnetfmt test_libjsonnet_file ${JSONNET_TEST_TARGETS}
368 )
369 endif() # if(BUILD_TESTS)
370
371 #### Man pages
372 #
373
374 if(BUILD_MAN_PAGES AND (BUILD_JSONNET OR BUILD_JSONNETFMT))
375 find_program(HELP2MAN_BINARY NAMES help2man)
376 if (HELP2MAN_BINARY)
377 message(STATUS "help2man found, man pages will be generated.")
378 file(MAKE_DIRECTORY "${PROJECT_BINARY_DIR}/man/man1")
379 if(BUILD_JSONNET)
380 add_custom_command(
381 OUTPUT "${PROJECT_BINARY_DIR}/man/man1/jsonnet.1"
382 COMMAND "${HELP2MAN_BINARY}"
383 ARGS --section=1 --no-info --name="Jsonnet data templating language interpreter" --output="${PROJECT_BINARY_DIR}/man/man1/jsonnet.1" "$<TARGET_FILE:jsonnet>"
384 DEPENDS jsonnet
385 )
386 endif()
387 if(BUILD_JSONNETFMT)
388 add_custom_command(
389 OUTPUT "${PROJECT_BINARY_DIR}/man/man1/jsonnetfmt.1"
390 COMMAND "${HELP2MAN_BINARY}"
391 ARGS --section=1 --no-info --name="Jsonnet data templating language auto-formatter" --output="${PROJECT_BINARY_DIR}/man/man1/jsonnetfmt.1" "$<TARGET_FILE:jsonnetfmt>"
392 DEPENDS jsonnetfmt
393 )
394 endif()
395
396 add_custom_target(jsonnet-man ALL DEPENDS "${PROJECT_BINARY_DIR}/man/man1/jsonnet.1")
397 add_custom_target(jsonnetfmt-man ALL DEPENDS "${PROJECT_BINARY_DIR}/man/man1/jsonnetfmt.1")
398
399 install(FILES ${PROJECT_BINARY_DIR}/man/man1/jsonnet.1 DESTINATION "${CMAKE_INSTALL_MANDIR}/man1")
400 install(FILES ${PROJECT_BINARY_DIR}/man/man1/jsonnetfmt.1 DESTINATION "${CMAKE_INSTALL_MANDIR}/man1")
401 else()
402 message(STATUS "help2man not found, man pages will not be generated.")
403 endif()
404 endif() # if(BUILD_MAN_PAGES)
405
406 #### CMake debugging outputs
407 #
408
409 # Note cmake_language GET_MESSAGE_LOG_LEVEL exists only in 3.25 and above.
410 if(CMAKE_VERSION VERSION_GREATER "3.25")
411 include(CMakePrintHelpers)
412 cmake_language(GET_MESSAGE_LOG_LEVEL CURRENT_CMAKE_LOG_LEVEL)
413 if(CURRENT_CMAKE_LOG_LEVEL MATCHES "TRACE")
414 # Print specific properties for specific targets
415 cmake_print_properties(
416 TARGETS
417 to_c_array
418 stdlib_h
419 jsonnet_obj_static
420 jsonnet_obj_shared
421 jsonnet_static
422 jsonnet_shared
423 jsonnet_cpp_static
424 jsonnet_cpp_shared
425 jsonnet_cmd_utils
426 jsonnet
427 jsonnetfmt
428 jsonnet-man
429 jsonnetfmt-man
430 PROPERTIES
431 TYPE
432 C_STANDARD
433 CXX_STANDARD
434 POSITION_INDEPENDENT_CODE
435 PUBLIC_LINK_DEPENDS
436 INTERFACE_LINK_DEPENDS
437 PUBLIC_LINK_LIBRARIES
438 INTERFACE_LINK_LIBRARIES
439 PUBLIC_INCLUDE_DIRECTORIES
440 INTERFACE_INCLUDE_DIRECTORIES
441 OUTPUT_NAME
442 PUBLIC_HEADER
443 RUNTIME_OUTPUT_DIRECTORY
444 )
445 endif()
446 endif()