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