• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1set(CMAKE_LEGACY_CYGWIN_WIN32 0)
2cmake_minimum_required(VERSION 3.0)
3
4project(cJSON
5    VERSION 1.7.16
6    LANGUAGES C)
7
8cmake_policy(SET CMP0054 NEW)  # set CMP0054 policy
9
10include(GNUInstallDirs)
11
12set(CJSON_VERSION_SO 1)
13set(CJSON_UTILS_VERSION_SO 1)
14
15set(custom_compiler_flags)
16
17include(CheckCCompilerFlag)
18option(ENABLE_CUSTOM_COMPILER_FLAGS "Enables custom compiler flags" ON)
19if (ENABLE_CUSTOM_COMPILER_FLAGS)
20    if (("${CMAKE_C_COMPILER_ID}" STREQUAL "Clang") OR ("${CMAKE_C_COMPILER_ID}" STREQUAL "GNU"))
21        list(APPEND custom_compiler_flags
22            -std=c89
23            -pedantic
24            -Wall
25            -Wextra
26            -Werror
27            -Wstrict-prototypes
28            -Wwrite-strings
29            -Wshadow
30            -Winit-self
31            -Wcast-align
32            -Wformat=2
33            -Wmissing-prototypes
34            -Wstrict-overflow=2
35            -Wcast-qual
36            -Wundef
37            -Wswitch-default
38            -Wconversion
39            -Wc++-compat
40            -fstack-protector-strong
41            -Wcomma
42            -Wdouble-promotion
43            -Wparentheses
44            -Wformat-overflow
45            -Wunused-macros
46            -Wmissing-variable-declarations
47            -Wused-but-marked-unused
48            -Wswitch-enum
49        )
50    elseif("${CMAKE_C_COMPILER_ID}" STREQUAL "MSVC")
51        # Disable warning c4001 - nonstandard extension 'single line comment' was used
52        # Define _CRT_SECURE_NO_WARNINGS to disable deprecation warnings for "insecure" C library functions
53        list(APPEND custom_compiler_flags
54            /GS
55            /Za
56            /sdl
57            /W4
58            /wd4001
59            /D_CRT_SECURE_NO_WARNINGS
60        )
61    endif()
62endif()
63
64option(ENABLE_SANITIZERS "Enables AddressSanitizer and UndefinedBehaviorSanitizer." OFF)
65if (ENABLE_SANITIZERS)
66    list(APPEND custom_compiler_flags
67        -fno-omit-frame-pointer
68        -fsanitize=address
69        -fsanitize=undefined
70        -fsanitize=float-cast-overflow
71        -fsanitize-address-use-after-scope
72        -fsanitize=integer
73        -01
74        -fno-sanitize-recover
75        )
76endif()
77
78option(ENABLE_SAFE_STACK "Enables the SafeStack instrumentation pass by the Code Pointer Integrity Project" OFF)
79if (ENABLE_SAFE_STACK)
80    if (ENABLE_SANITIZERS)
81        message(FATAL_ERROR "ENABLE_SAFE_STACK cannot be used in combination with ENABLE_SANITIZERS")
82    endif()
83    list(APPEND custom_compiler_flags
84        -fsanitize=safe-stack
85        )
86endif()
87
88option(ENABLE_PUBLIC_SYMBOLS "Export library symbols." On)
89if (ENABLE_PUBLIC_SYMBOLS)
90    list(APPEND custom_compiler_flags -fvisibility=hidden)
91    add_definitions(-DCJSON_EXPORT_SYMBOLS -DCJSON_API_VISIBILITY)
92endif()
93option(ENABLE_HIDDEN_SYMBOLS "Hide library symbols." Off)
94if (ENABLE_HIDDEN_SYMBOLS)
95    add_definitions(-DCJSON_HIDE_SYMBOLS -UCJSON_API_VISIBILITY)
96endif()
97
98# apply custom compiler flags
99foreach(compiler_flag ${custom_compiler_flags})
100    #remove problematic characters
101    string(REGEX REPLACE "[^a-zA-Z0-9]" "" current_variable ${compiler_flag})
102
103    CHECK_C_COMPILER_FLAG(${compiler_flag} "FLAG_SUPPORTED_${current_variable}")
104    if (FLAG_SUPPORTED_${current_variable})
105        list(APPEND supported_compiler_flags)
106        set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${compiler_flag}")
107    endif()
108endforeach()
109
110set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${supported_compiler_flags}")
111
112option(BUILD_SHARED_LIBS "Build shared libraries" ON)
113option(ENABLE_TARGET_EXPORT "Enable exporting of CMake targets. Disable when it causes problems!" ON)
114
115#cJSON
116set(CJSON_LIB cjson)
117
118file(GLOB HEADERS cJSON.h)
119set(SOURCES cJSON.c)
120
121option(BUILD_SHARED_AND_STATIC_LIBS "Build both shared and static libraries" Off)
122option(CJSON_OVERRIDE_BUILD_SHARED_LIBS "Override BUILD_SHARED_LIBS with CJSON_BUILD_SHARED_LIBS" OFF)
123option(CJSON_BUILD_SHARED_LIBS "Overrides BUILD_SHARED_LIBS if CJSON_OVERRIDE_BUILD_SHARED_LIBS is enabled" ON)
124option(ENABLE_CJSON_VERSION_SO "Enables cJSON so version" ON)
125
126if ((CJSON_OVERRIDE_BUILD_SHARED_LIBS AND CJSON_BUILD_SHARED_LIBS) OR ((NOT CJSON_OVERRIDE_BUILD_SHARED_LIBS) AND BUILD_SHARED_LIBS))
127    set(CJSON_LIBRARY_TYPE SHARED)
128else()
129    set(CJSON_LIBRARY_TYPE STATIC)
130endif()
131
132
133if (NOT BUILD_SHARED_AND_STATIC_LIBS)
134    add_library("${CJSON_LIB}" "${CJSON_LIBRARY_TYPE}" "${HEADERS}" "${SOURCES}")
135else()
136    # See https://cmake.org/Wiki/CMake_FAQ#How_do_I_make_my_shared_and_static_libraries_have_the_same_root_name.2C_but_different_suffixes.3F
137    add_library("${CJSON_LIB}" SHARED "${HEADERS}" "${SOURCES}")
138    add_library("${CJSON_LIB}-static" STATIC "${HEADERS}" "${SOURCES}")
139    set_target_properties("${CJSON_LIB}-static" PROPERTIES OUTPUT_NAME "${CJSON_LIB}")
140    set_target_properties("${CJSON_LIB}-static" PROPERTIES PREFIX "lib")
141endif()
142if (NOT WIN32)
143    target_link_libraries("${CJSON_LIB}" m)
144endif()
145
146configure_file("${CMAKE_CURRENT_SOURCE_DIR}/library_config/libcjson.pc.in"
147    "${CMAKE_CURRENT_BINARY_DIR}/libcjson.pc" @ONLY)
148
149install(FILES cJSON.h DESTINATION "${CMAKE_INSTALL_FULL_INCLUDEDIR}/cjson")
150install (FILES "${CMAKE_CURRENT_BINARY_DIR}/libcjson.pc" DESTINATION "${CMAKE_INSTALL_FULL_LIBDIR}/pkgconfig")
151install(TARGETS "${CJSON_LIB}"
152    EXPORT "${CJSON_LIB}"
153    ARCHIVE DESTINATION "${CMAKE_INSTALL_FULL_LIBDIR}"
154    LIBRARY DESTINATION "${CMAKE_INSTALL_FULL_LIBDIR}"
155    RUNTIME DESTINATION "${CMAKE_INSTALL_FULL_BINDIR}"
156    INCLUDES DESTINATION "${CMAKE_INSTALL_FULL_INCLUDEDIR}"
157)
158if (BUILD_SHARED_AND_STATIC_LIBS)
159    install(TARGETS "${CJSON_LIB}-static"
160    EXPORT "${CJSON_LIB}"
161    ARCHIVE DESTINATION "${CMAKE_INSTALL_FULL_LIBDIR}"
162    INCLUDES DESTINATION "${CMAKE_INSTALL_FULL_INCLUDEDIR}"
163)
164endif()
165if(ENABLE_TARGET_EXPORT)
166    # export library information for CMake projects
167    install(EXPORT "${CJSON_LIB}" DESTINATION "${CMAKE_INSTALL_FULL_LIBDIR}/cmake/cJSON")
168endif()
169
170if(ENABLE_CJSON_VERSION_SO)
171    set_target_properties("${CJSON_LIB}"
172        PROPERTIES
173            SOVERSION "${CJSON_VERSION_SO}"
174            VERSION "${PROJECT_VERSION}")
175endif()
176
177#cJSON_Utils
178option(ENABLE_CJSON_UTILS "Enable building the cJSON_Utils library." OFF)
179if(ENABLE_CJSON_UTILS)
180    set(CJSON_UTILS_LIB cjson_utils)
181
182    file(GLOB HEADERS_UTILS cJSON_Utils.h)
183    set(SOURCES_UTILS cJSON_Utils.c)
184
185    if (NOT BUILD_SHARED_AND_STATIC_LIBS)
186        add_library("${CJSON_UTILS_LIB}" "${CJSON_LIBRARY_TYPE}" "${HEADERS_UTILS}" "${SOURCES_UTILS}")
187        target_link_libraries("${CJSON_UTILS_LIB}" "${CJSON_LIB}")
188    else()
189        add_library("${CJSON_UTILS_LIB}" SHARED "${HEADERS_UTILS}" "${SOURCES_UTILS}")
190        target_link_libraries("${CJSON_UTILS_LIB}" "${CJSON_LIB}")
191        add_library("${CJSON_UTILS_LIB}-static" STATIC "${HEADERS_UTILS}" "${SOURCES_UTILS}")
192        target_link_libraries("${CJSON_UTILS_LIB}-static" "${CJSON_LIB}-static")
193        set_target_properties("${CJSON_UTILS_LIB}-static" PROPERTIES OUTPUT_NAME "${CJSON_UTILS_LIB}")
194        set_target_properties("${CJSON_UTILS_LIB}-static" PROPERTIES PREFIX "lib")
195    endif()
196
197    configure_file("${CMAKE_CURRENT_SOURCE_DIR}/library_config/libcjson_utils.pc.in"
198        "${CMAKE_CURRENT_BINARY_DIR}/libcjson_utils.pc" @ONLY)
199
200    install(TARGETS "${CJSON_UTILS_LIB}"
201        EXPORT "${CJSON_UTILS_LIB}"
202        ARCHIVE DESTINATION "${CMAKE_INSTALL_FULL_LIBDIR}"
203        LIBRARY DESTINATION "${CMAKE_INSTALL_FULL_LIBDIR}"
204        RUNTIME DESTINATION "${CMAKE_INSTALL_FULL_BINDIR}"
205        INCLUDES DESTINATION "${CMAKE_INSTALL_FULL_INCLUDEDIR}"
206    )
207    if (BUILD_SHARED_AND_STATIC_LIBS)
208        install(TARGETS "${CJSON_UTILS_LIB}-static"
209        EXPORT "${CJSON_UTILS_LIB}"
210        ARCHIVE DESTINATION "${CMAKE_INSTALL_FULL_LIBDIR}"
211        INCLUDES DESTINATION "${CMAKE_INSTALL_FULL_INCLUDEDIR}"
212        )
213    endif()
214    install(FILES cJSON_Utils.h DESTINATION "${CMAKE_INSTALL_FULL_INCLUDEDIR}/cjson")
215    install (FILES "${CMAKE_CURRENT_BINARY_DIR}/libcjson_utils.pc" DESTINATION "${CMAKE_INSTALL_FULL_LIBDIR}/pkgconfig")
216    if(ENABLE_TARGET_EXPORT)
217      # export library information for CMake projects
218      install(EXPORT "${CJSON_UTILS_LIB}" DESTINATION "${CMAKE_INSTALL_FULL_LIBDIR}/cmake/cJSON")
219    endif()
220
221    if(ENABLE_CJSON_VERSION_SO)
222        set_target_properties("${CJSON_UTILS_LIB}"
223            PROPERTIES
224                SOVERSION "${CJSON_UTILS_VERSION_SO}"
225                VERSION "${PROJECT_VERSION}")
226    endif()
227endif()
228
229# create the other package config files
230configure_file(
231    "${CMAKE_CURRENT_SOURCE_DIR}/library_config/cJSONConfig.cmake.in"
232    ${PROJECT_BINARY_DIR}/cJSONConfig.cmake @ONLY)
233configure_file(
234    "${CMAKE_CURRENT_SOURCE_DIR}/library_config/cJSONConfigVersion.cmake.in"
235    ${PROJECT_BINARY_DIR}/cJSONConfigVersion.cmake @ONLY)
236
237if(ENABLE_TARGET_EXPORT)
238    # Install package config files
239    install(FILES ${PROJECT_BINARY_DIR}/cJSONConfig.cmake
240        ${PROJECT_BINARY_DIR}/cJSONConfigVersion.cmake
241        DESTINATION "${CMAKE_INSTALL_FULL_LIBDIR}/cmake/cJSON")
242endif()
243
244option(ENABLE_CJSON_TEST "Enable building cJSON test" ON)
245if(ENABLE_CJSON_TEST)
246    enable_testing()
247
248    set(TEST_CJSON cJSON_test)
249    add_executable("${TEST_CJSON}" test.c)
250    target_link_libraries("${TEST_CJSON}" "${CJSON_LIB}")
251
252    add_test(NAME ${TEST_CJSON} COMMAND "${CMAKE_CURRENT_BINARY_DIR}/${TEST_CJSON}")
253
254    # Disable -fsanitize=float-divide-by-zero for cJSON_test
255    if (FLAG_SUPPORTED_fsanitizefloatdividebyzero)
256        if ("${CMAKE_VERSION}" VERSION_LESS "2.8.12")
257            set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fno-sanitize=float-divide-by-zero")
258        else()
259            target_compile_options(${TEST_CJSON} PRIVATE "-fno-sanitize=float-divide-by-zero")
260        endif()
261    endif()
262
263    #"check" target that automatically builds everything and runs the tests
264    add_custom_target(check
265        COMMAND ${CMAKE_CTEST_COMMAND} --output-on-failure
266        DEPENDS ${TEST_CJSON})
267endif()
268
269#Create the uninstall target
270option(ENABLE_CJSON_UNINSTALL "Enable creating uninstall target" ON)
271if(ENABLE_CJSON_UNINSTALL)
272  add_custom_target(uninstall "${CMAKE_COMMAND}" -P
273    "${PROJECT_SOURCE_DIR}/library_config/uninstall.cmake")
274endif()
275
276# Enable the use of locales
277option(ENABLE_LOCALES "Enable the use of locales" ON)
278if(ENABLE_LOCALES)
279	add_definitions(-DENABLE_LOCALES)
280endif()
281
282add_subdirectory(tests)
283add_subdirectory(fuzzing)
284