interplane.Get Omega
Read in your language

cJSON

[Ported from github.com/DaveGamble/cJSON] Fix: heap-use-after-free in merge_patch when patch is subtree of target (#1065)

Brought by drakestapleton

branch master Branches Tags History

cJSON / CMakeLists.txt

279 lines · 10205 bytes Raw Blame History
1set(CMAKE_LEGACY_CYGWIN_WIN32 0)
2cmake_minimum_required(VERSION 3.5)
3
4project(cJSON
5 VERSION 1.7.19
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 -fno-sanitize-recover
74 )
75endif()
76
77option(ENABLE_SAFE_STACK "Enables the SafeStack instrumentation pass by the Code Pointer Integrity Project" OFF)
78if (ENABLE_SAFE_STACK)
79 if (ENABLE_SANITIZERS)
80 message(FATAL_ERROR "ENABLE_SAFE_STACK cannot be used in combination with ENABLE_SANITIZERS")
81 endif()
82 list(APPEND custom_compiler_flags
83 -fsanitize=safe-stack
84 )
85endif()
86
87option(ENABLE_PUBLIC_SYMBOLS "Export library symbols." On)
88if (ENABLE_PUBLIC_SYMBOLS)
89 list(APPEND custom_compiler_flags -fvisibility=hidden)
90 add_definitions(-DCJSON_EXPORT_SYMBOLS -DCJSON_API_VISIBILITY)
91endif()
92option(ENABLE_HIDDEN_SYMBOLS "Hide library symbols." Off)
93if (ENABLE_HIDDEN_SYMBOLS)
94 add_definitions(-DCJSON_HIDE_SYMBOLS -UCJSON_API_VISIBILITY)
95endif()
96
97# apply custom compiler flags
98foreach(compiler_flag ${custom_compiler_flags})
99 #remove problematic characters
100 string(REGEX REPLACE "[^a-zA-Z0-9]" "" current_variable ${compiler_flag})
101
102 CHECK_C_COMPILER_FLAG(${compiler_flag} "FLAG_SUPPORTED_${current_variable}")
103 if (FLAG_SUPPORTED_${current_variable})
104 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${compiler_flag}")
105 endif()
106endforeach()
107
108option(BUILD_SHARED_LIBS "Build shared libraries" ON)
109option(ENABLE_TARGET_EXPORT "Enable exporting of CMake targets. Disable when it causes problems!" ON)
110
111#cJSON
112set(CJSON_LIB cjson)
113
114file(GLOB HEADERS cJSON.h)
115set(SOURCES cJSON.c)
116
117option(BUILD_SHARED_AND_STATIC_LIBS "Build both shared and static libraries" Off)
118option(CJSON_OVERRIDE_BUILD_SHARED_LIBS "Override BUILD_SHARED_LIBS with CJSON_BUILD_SHARED_LIBS" OFF)
119option(CJSON_BUILD_SHARED_LIBS "Overrides BUILD_SHARED_LIBS if CJSON_OVERRIDE_BUILD_SHARED_LIBS is enabled" ON)
120option(ENABLE_CJSON_VERSION_SO "Enables cJSON so version" ON)
121
122if ((CJSON_OVERRIDE_BUILD_SHARED_LIBS AND CJSON_BUILD_SHARED_LIBS) OR ((NOT CJSON_OVERRIDE_BUILD_SHARED_LIBS) AND BUILD_SHARED_LIBS))
123 set(CJSON_LIBRARY_TYPE SHARED)
124else()
125 set(CJSON_LIBRARY_TYPE STATIC)
126endif()
127
128
129if (NOT BUILD_SHARED_AND_STATIC_LIBS)
130 add_library("${CJSON_LIB}" "${CJSON_LIBRARY_TYPE}" "${HEADERS}" "${SOURCES}")
131else()
132 # 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
133 add_library("${CJSON_LIB}" SHARED "${HEADERS}" "${SOURCES}")
134 add_library("${CJSON_LIB}-static" STATIC "${HEADERS}" "${SOURCES}")
135 set_target_properties("${CJSON_LIB}-static" PROPERTIES OUTPUT_NAME "${CJSON_LIB}")
136 set_target_properties("${CJSON_LIB}-static" PROPERTIES PREFIX "lib")
137endif()
138if (NOT WIN32)
139 target_link_libraries("${CJSON_LIB}" m)
140endif()
141
142configure_file("${CMAKE_CURRENT_SOURCE_DIR}/library_config/libcjson.pc.in"
143 "${CMAKE_CURRENT_BINARY_DIR}/libcjson.pc" @ONLY)
144
145install(FILES cJSON.h DESTINATION "${CMAKE_INSTALL_FULL_INCLUDEDIR}/cjson")
146install (FILES "${CMAKE_CURRENT_BINARY_DIR}/libcjson.pc" DESTINATION "${CMAKE_INSTALL_FULL_LIBDIR}/pkgconfig")
147install(TARGETS "${CJSON_LIB}"
148 EXPORT "${CJSON_LIB}"
149 ARCHIVE DESTINATION "${CMAKE_INSTALL_FULL_LIBDIR}"
150 LIBRARY DESTINATION "${CMAKE_INSTALL_FULL_LIBDIR}"
151 RUNTIME DESTINATION "${CMAKE_INSTALL_FULL_BINDIR}"
152 INCLUDES DESTINATION "${CMAKE_INSTALL_FULL_INCLUDEDIR}"
153)
154if (BUILD_SHARED_AND_STATIC_LIBS)
155 install(TARGETS "${CJSON_LIB}-static"
156 EXPORT "${CJSON_LIB}"
157 ARCHIVE DESTINATION "${CMAKE_INSTALL_FULL_LIBDIR}"
158 INCLUDES DESTINATION "${CMAKE_INSTALL_FULL_INCLUDEDIR}"
159)
160endif()
161if(ENABLE_TARGET_EXPORT)
162 # export library information for CMake projects
163 install(EXPORT "${CJSON_LIB}" DESTINATION "${CMAKE_INSTALL_FULL_LIBDIR}/cmake/cJSON")
164endif()
165
166if(ENABLE_CJSON_VERSION_SO)
167 set_target_properties("${CJSON_LIB}"
168 PROPERTIES
169 SOVERSION "${CJSON_VERSION_SO}"
170 VERSION "${PROJECT_VERSION}")
171endif()
172
173#cJSON_Utils
174option(ENABLE_CJSON_UTILS "Enable building the cJSON_Utils library." OFF)
175if(ENABLE_CJSON_UTILS)
176 set(CJSON_UTILS_LIB cjson_utils)
177
178 file(GLOB HEADERS_UTILS cJSON_Utils.h)
179 set(SOURCES_UTILS cJSON_Utils.c)
180
181 if (NOT BUILD_SHARED_AND_STATIC_LIBS)
182 add_library("${CJSON_UTILS_LIB}" "${CJSON_LIBRARY_TYPE}" "${HEADERS_UTILS}" "${SOURCES_UTILS}")
183 target_link_libraries("${CJSON_UTILS_LIB}" "${CJSON_LIB}")
184 else()
185 add_library("${CJSON_UTILS_LIB}" SHARED "${HEADERS_UTILS}" "${SOURCES_UTILS}")
186 target_link_libraries("${CJSON_UTILS_LIB}" "${CJSON_LIB}")
187 add_library("${CJSON_UTILS_LIB}-static" STATIC "${HEADERS_UTILS}" "${SOURCES_UTILS}")
188 target_link_libraries("${CJSON_UTILS_LIB}-static" "${CJSON_LIB}-static")
189 set_target_properties("${CJSON_UTILS_LIB}-static" PROPERTIES OUTPUT_NAME "${CJSON_UTILS_LIB}")
190 set_target_properties("${CJSON_UTILS_LIB}-static" PROPERTIES PREFIX "lib")
191 endif()
192
193 configure_file("${CMAKE_CURRENT_SOURCE_DIR}/library_config/libcjson_utils.pc.in"
194 "${CMAKE_CURRENT_BINARY_DIR}/libcjson_utils.pc" @ONLY)
195
196 install(TARGETS "${CJSON_UTILS_LIB}"
197 EXPORT "${CJSON_UTILS_LIB}"
198 ARCHIVE DESTINATION "${CMAKE_INSTALL_FULL_LIBDIR}"
199 LIBRARY DESTINATION "${CMAKE_INSTALL_FULL_LIBDIR}"
200 RUNTIME DESTINATION "${CMAKE_INSTALL_FULL_BINDIR}"
201 INCLUDES DESTINATION "${CMAKE_INSTALL_FULL_INCLUDEDIR}"
202 )
203 if (BUILD_SHARED_AND_STATIC_LIBS)
204 install(TARGETS "${CJSON_UTILS_LIB}-static"
205 EXPORT "${CJSON_UTILS_LIB}"
206 ARCHIVE DESTINATION "${CMAKE_INSTALL_FULL_LIBDIR}"
207 INCLUDES DESTINATION "${CMAKE_INSTALL_FULL_INCLUDEDIR}"
208 )
209 endif()
210 install(FILES cJSON_Utils.h DESTINATION "${CMAKE_INSTALL_FULL_INCLUDEDIR}/cjson")
211 install (FILES "${CMAKE_CURRENT_BINARY_DIR}/libcjson_utils.pc" DESTINATION "${CMAKE_INSTALL_FULL_LIBDIR}/pkgconfig")
212 if(ENABLE_TARGET_EXPORT)
213 # export library information for CMake projects
214 install(EXPORT "${CJSON_UTILS_LIB}" DESTINATION "${CMAKE_INSTALL_FULL_LIBDIR}/cmake/cJSON")
215 endif()
216
217 if(ENABLE_CJSON_VERSION_SO)
218 set_target_properties("${CJSON_UTILS_LIB}"
219 PROPERTIES
220 SOVERSION "${CJSON_UTILS_VERSION_SO}"
221 VERSION "${PROJECT_VERSION}")
222 endif()
223endif()
224
225# create the other package config files
226configure_file(
227 "${CMAKE_CURRENT_SOURCE_DIR}/library_config/cJSONConfig.cmake.in"
228 ${PROJECT_BINARY_DIR}/cJSONConfig.cmake @ONLY)
229configure_file(
230 "${CMAKE_CURRENT_SOURCE_DIR}/library_config/cJSONConfigVersion.cmake.in"
231 ${PROJECT_BINARY_DIR}/cJSONConfigVersion.cmake @ONLY)
232
233if(ENABLE_TARGET_EXPORT)
234 # Install package config files
235 install(FILES ${PROJECT_BINARY_DIR}/cJSONConfig.cmake
236 ${PROJECT_BINARY_DIR}/cJSONConfigVersion.cmake
237 DESTINATION "${CMAKE_INSTALL_FULL_LIBDIR}/cmake/cJSON")
238endif()
239
240option(ENABLE_CJSON_TEST "Enable building cJSON test" ON)
241if(ENABLE_CJSON_TEST)
242 enable_testing()
243
244 set(TEST_CJSON cJSON_test)
245 add_executable("${TEST_CJSON}" test.c)
246 target_link_libraries("${TEST_CJSON}" "${CJSON_LIB}")
247
248 add_test(NAME ${TEST_CJSON} COMMAND "${CMAKE_CURRENT_BINARY_DIR}/${TEST_CJSON}")
249
250 # Disable -fsanitize=float-divide-by-zero for cJSON_test
251 if (FLAG_SUPPORTED_fsanitizefloatdividebyzero)
252 if ("${CMAKE_VERSION}" VERSION_LESS "2.8.12")
253 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fno-sanitize=float-divide-by-zero")
254 else()
255 target_compile_options(${TEST_CJSON} PRIVATE "-fno-sanitize=float-divide-by-zero")
256 endif()
257 endif()
258
259 #"check" target that automatically builds everything and runs the tests
260 add_custom_target(check
261 COMMAND ${CMAKE_CTEST_COMMAND} --output-on-failure
262 DEPENDS ${TEST_CJSON})
263endif()
264
265#Create the uninstall target
266option(ENABLE_CJSON_UNINSTALL "Enable creating uninstall target" ON)
267if(ENABLE_CJSON_UNINSTALL)
268 add_custom_target(uninstall "${CMAKE_COMMAND}" -P
269 "${PROJECT_SOURCE_DIR}/library_config/uninstall.cmake")
270endif()
271
272# Enable the use of locales
273option(ENABLE_LOCALES "Enable the use of locales" ON)
274if(ENABLE_LOCALES)
275 add_definitions(-DENABLE_LOCALES)
276endif()
277
278add_subdirectory(tests)
279add_subdirectory(fuzzing)