2016-11-11 13:52:10 +07:00
|
|
|
set(CMAKE_LEGACY_CYGWIN_WIN32 0)
|
2017-03-16 01:09:26 +01:00
|
|
|
cmake_minimum_required(VERSION 2.8.5)
|
2015-07-15 14:01:46 +06:00
|
|
|
|
2016-11-05 21:18:03 +07:00
|
|
|
project(cJSON C)
|
|
|
|
|
2017-11-04 17:39:54 +08:00
|
|
|
include(GNUInstallDirs)
|
|
|
|
|
2016-11-06 14:58:22 +07:00
|
|
|
set(PROJECT_VERSION_MAJOR 1)
|
2017-12-31 02:03:40 +01:00
|
|
|
set(PROJECT_VERSION_MINOR 7)
|
2019-04-14 23:58:02 +02:00
|
|
|
set(PROJECT_VERSION_PATCH 11)
|
2016-11-04 20:31:56 +07:00
|
|
|
set(CJSON_VERSION_SO 1)
|
2016-11-05 01:30:31 +07:00
|
|
|
set(CJSON_UTILS_VERSION_SO 1)
|
2016-11-06 14:58:22 +07:00
|
|
|
set(PROJECT_VERSION "${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}.${PROJECT_VERSION_PATCH}")
|
2015-07-15 14:01:46 +06:00
|
|
|
|
2017-02-07 14:51:29 +01:00
|
|
|
|
|
|
|
set(custom_compiler_flags)
|
|
|
|
|
|
|
|
include(CheckCCompilerFlag)
|
2017-06-17 14:42:52 +02:00
|
|
|
option(ENABLE_CUSTOM_COMPILER_FLAGS "Enables custom compiler flags" ON)
|
2016-11-15 20:21:57 +07:00
|
|
|
if (ENABLE_CUSTOM_COMPILER_FLAGS)
|
2017-06-17 14:42:52 +02:00
|
|
|
if (("${CMAKE_C_COMPILER_ID}" STREQUAL "Clang") OR ("${CMAKE_C_COMPILER_ID}" STREQUAL "GNU"))
|
|
|
|
list(APPEND custom_compiler_flags
|
|
|
|
-std=c89
|
|
|
|
-pedantic
|
|
|
|
-Wall
|
|
|
|
-Wextra
|
|
|
|
-Werror
|
|
|
|
-Wstrict-prototypes
|
|
|
|
-Wwrite-strings
|
|
|
|
-Wshadow
|
|
|
|
-Winit-self
|
|
|
|
-Wcast-align
|
|
|
|
-Wformat=2
|
|
|
|
-Wmissing-prototypes
|
|
|
|
-Wstrict-overflow=2
|
|
|
|
-Wcast-qual
|
|
|
|
-Wundef
|
|
|
|
-Wswitch-default
|
|
|
|
-Wconversion
|
|
|
|
-Wc++-compat
|
|
|
|
-fstack-protector-strong
|
|
|
|
-Wcomma
|
|
|
|
-Wdouble-promotion
|
|
|
|
-Wparentheses
|
|
|
|
-Wformat-overflow
|
|
|
|
-Wunused-macros
|
|
|
|
-Wmissing-variable-declarations
|
|
|
|
-Wused-but-marked-unused
|
|
|
|
-Wswitch-enum
|
|
|
|
)
|
|
|
|
elseif("${CMAKE_C_COMPILER_ID}" STREQUAL "MSVC")
|
2018-09-12 15:32:30 -05:00
|
|
|
# Disable warning c4001 - nonstandard extension 'single line comment' was used
|
|
|
|
# Define _CRT_SECURE_NO_WARNINGS to disable deprecation warnings for "insecure" C library functions
|
2017-06-17 14:42:52 +02:00
|
|
|
list(APPEND custom_compiler_flags
|
|
|
|
/GS
|
|
|
|
/Za
|
|
|
|
/sdl
|
|
|
|
/W4
|
2018-09-04 14:25:15 -05:00
|
|
|
/wd4001
|
|
|
|
/D_CRT_SECURE_NO_WARNINGS
|
2017-02-07 14:51:29 +01:00
|
|
|
)
|
2017-06-17 14:42:52 +02:00
|
|
|
endif()
|
2016-11-05 21:24:24 +07:00
|
|
|
endif()
|
2016-11-05 00:51:31 +07:00
|
|
|
|
2017-02-07 15:07:54 +01:00
|
|
|
option(ENABLE_SANITIZERS "Enables AddressSanitizer and UndefinedBehaviorSanitizer." OFF)
|
|
|
|
if (ENABLE_SANITIZERS)
|
|
|
|
list(APPEND custom_compiler_flags
|
|
|
|
-fno-omit-frame-pointer
|
|
|
|
-fsanitize=address
|
|
|
|
-fsanitize=undefined
|
|
|
|
-fsanitize=float-divide-by-zero
|
|
|
|
-fsanitize=float-cast-overflow
|
|
|
|
-fsanitize-address-use-after-scope
|
|
|
|
-fsanitize=integer
|
|
|
|
-01
|
|
|
|
-fno-sanitize-recover
|
|
|
|
)
|
|
|
|
endif()
|
|
|
|
|
2017-07-05 10:31:00 +02:00
|
|
|
option(ENABLE_SAFE_STACK "Enables the SafeStack instrumentation pass by the Code Pointer Integrity Project" OFF)
|
|
|
|
if (ENABLE_SAFE_STACK)
|
|
|
|
if (ENABLE_SANITIZERS)
|
|
|
|
message(FATAL_ERROR "ENABLE_SAFE_STACK cannot be used in combination with ENABLE_SANITIZERS")
|
|
|
|
endif()
|
|
|
|
list(APPEND custom_compiler_flags
|
|
|
|
-fsanitize=safe-stack
|
|
|
|
)
|
|
|
|
endif()
|
|
|
|
|
2017-03-01 23:16:47 +01:00
|
|
|
option(ENABLE_PUBLIC_SYMBOLS "Export library symbols." On)
|
|
|
|
if (ENABLE_PUBLIC_SYMBOLS)
|
|
|
|
list(APPEND custom_compiler_flags -fvisibility=hidden)
|
|
|
|
add_definitions(-DCJSON_EXPORT_SYMBOLS -DCJSON_API_VISIBILITY)
|
|
|
|
endif()
|
|
|
|
option(ENABLE_HIDDEN_SYMBOLS "Hide library symbols." Off)
|
|
|
|
if (ENABLE_HIDDEN_SYMBOLS)
|
|
|
|
add_definitions(-DCJSON_HIDE_SYMBOLS -UCJSON_API_VISIBILITY)
|
|
|
|
endif()
|
|
|
|
|
2017-02-07 14:51:29 +01:00
|
|
|
# apply custom compiler flags
|
|
|
|
foreach(compiler_flag ${custom_compiler_flags})
|
2017-02-07 20:20:56 +01:00
|
|
|
#remove problematic characters
|
|
|
|
string(REGEX REPLACE "[^a-zA-Z0-9]" "" current_variable ${compiler_flag})
|
|
|
|
|
|
|
|
CHECK_C_COMPILER_FLAG(${compiler_flag} "FLAG_SUPPORTED_${current_variable}")
|
|
|
|
if (FLAG_SUPPORTED_${current_variable})
|
2017-03-16 00:22:53 +01:00
|
|
|
list(APPEND supported_compiler_flags)
|
2017-02-07 14:51:29 +01:00
|
|
|
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${compiler_flag}")
|
|
|
|
endif()
|
|
|
|
endforeach()
|
|
|
|
|
2017-03-16 00:22:53 +01:00
|
|
|
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${supported_compiler_flags}")
|
|
|
|
|
2016-11-05 16:41:24 +07:00
|
|
|
option(BUILD_SHARED_LIBS "Build shared libraries" ON)
|
2016-11-07 00:13:38 +07:00
|
|
|
option(ENABLE_TARGET_EXPORT "Enable exporting of CMake targets. Disable when it causes problems!" ON)
|
2016-11-05 16:41:24 +07:00
|
|
|
|
2016-11-05 00:38:30 +07:00
|
|
|
#cJSON
|
2016-11-05 00:46:41 +07:00
|
|
|
set(CJSON_LIB cjson)
|
2016-11-05 00:38:30 +07:00
|
|
|
|
2015-07-15 14:01:46 +06:00
|
|
|
file(GLOB HEADERS cJSON.h)
|
|
|
|
set(SOURCES cJSON.c)
|
|
|
|
|
2017-06-07 00:15:23 +02:00
|
|
|
option(BUILD_SHARED_AND_STATIC_LIBS "Build both shared and static libraries" Off)
|
2017-11-28 17:16:11 +01:00
|
|
|
option(CJSON_OVERRIDE_BUILD_SHARED_LIBS "Override BUILD_SHARED_LIBS with CJSON_BUILD_SHARED_LIBS" OFF)
|
|
|
|
option(CJSON_BUILD_SHARED_LIBS "Overrides BUILD_SHARED_LIBS if CJSON_OVERRIDE_BUILD_SHARED_LIBS is enabled" ON)
|
|
|
|
|
|
|
|
if ((CJSON_OVERRIDE_BUILD_SHARED_LIBS AND CJSON_BUILD_SHARED_LIBS) OR ((NOT CJSON_OVERRIDE_BUILD_SHARED_LIBS) AND BUILD_SHARED_LIBS))
|
|
|
|
set(CJSON_LIBRARY_TYPE SHARED)
|
|
|
|
else()
|
|
|
|
set(CJSON_LIBRARY_TYPE STATIC)
|
|
|
|
endif()
|
|
|
|
|
2017-06-07 00:15:23 +02:00
|
|
|
|
|
|
|
if (NOT BUILD_SHARED_AND_STATIC_LIBS)
|
2017-11-28 17:16:11 +01:00
|
|
|
add_library("${CJSON_LIB}" "${CJSON_LIBRARY_TYPE}" "${HEADERS}" "${SOURCES}")
|
2017-06-07 00:15:23 +02:00
|
|
|
else()
|
|
|
|
# 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
|
|
|
|
add_library("${CJSON_LIB}" SHARED "${HEADERS}" "${SOURCES}")
|
|
|
|
add_library("${CJSON_LIB}-static" STATIC "${HEADERS}" "${SOURCES}")
|
|
|
|
set_target_properties("${CJSON_LIB}-static" PROPERTIES OUTPUT_NAME "${CJSON_LIB}")
|
|
|
|
set_target_properties("${CJSON_LIB}-static" PROPERTIES PREFIX "lib")
|
|
|
|
endif()
|
2015-08-19 23:03:08 -04:00
|
|
|
if (NOT WIN32)
|
2016-11-05 22:52:40 +07:00
|
|
|
target_link_libraries("${CJSON_LIB}" m)
|
2015-08-19 23:03:08 -04:00
|
|
|
endif()
|
2015-07-15 14:01:46 +06:00
|
|
|
|
2017-04-12 21:49:36 +02:00
|
|
|
configure_file("${CMAKE_CURRENT_SOURCE_DIR}/library_config/libcjson.pc.in"
|
2016-11-05 22:42:35 +07:00
|
|
|
"${CMAKE_CURRENT_BINARY_DIR}/libcjson.pc" @ONLY)
|
2016-10-17 22:12:12 -02:00
|
|
|
|
2018-02-06 11:24:03 +01:00
|
|
|
install(FILES cJSON.h DESTINATION "${CMAKE_INSTALL_FULL_INCLUDEDIR}/cjson")
|
|
|
|
install (FILES "${CMAKE_CURRENT_BINARY_DIR}/libcjson.pc" DESTINATION "${CMAKE_INSTALL_FULL_LIBDIR}/pkgconfig")
|
|
|
|
install(TARGETS "${CJSON_LIB}" DESTINATION "${CMAKE_INSTALL_FULL_LIBDIR}" EXPORT "${CJSON_LIB}")
|
2017-06-07 00:15:23 +02:00
|
|
|
if (BUILD_SHARED_AND_STATIC_LIBS)
|
2018-02-06 11:24:03 +01:00
|
|
|
install(TARGETS "${CJSON_LIB}-static" DESTINATION "${CMAKE_INSTALL_FULL_LIBDIR}")
|
2017-06-07 00:15:23 +02:00
|
|
|
endif()
|
2016-11-07 00:13:38 +07:00
|
|
|
if(ENABLE_TARGET_EXPORT)
|
2016-11-15 20:21:30 +07:00
|
|
|
# export library information for CMake projects
|
2018-02-06 11:24:03 +01:00
|
|
|
install(EXPORT "${CJSON_LIB}" DESTINATION "${CMAKE_INSTALL_FULL_LIBDIR}/cmake/cJSON")
|
2016-11-07 00:13:38 +07:00
|
|
|
endif()
|
2015-07-28 03:33:17 +06:00
|
|
|
|
2016-11-05 22:52:40 +07:00
|
|
|
set_target_properties("${CJSON_LIB}"
|
2016-11-05 00:50:51 +07:00
|
|
|
PROPERTIES
|
2016-11-05 22:52:40 +07:00
|
|
|
SOVERSION "${CJSON_VERSION_SO}"
|
2016-11-06 14:58:22 +07:00
|
|
|
VERSION "${PROJECT_VERSION}")
|
2016-11-05 00:50:51 +07:00
|
|
|
|
2016-11-05 00:38:30 +07:00
|
|
|
#cJSON_Utils
|
2016-11-05 22:20:55 +07:00
|
|
|
option(ENABLE_CJSON_UTILS "Enable building the cJSON_Utils library." OFF)
|
|
|
|
if(ENABLE_CJSON_UTILS)
|
|
|
|
set(CJSON_UTILS_LIB cjson_utils)
|
2015-07-28 03:33:17 +06:00
|
|
|
|
2016-11-05 22:20:55 +07:00
|
|
|
file(GLOB HEADERS_UTILS cJSON_Utils.h)
|
|
|
|
set(SOURCES_UTILS cJSON_Utils.c)
|
2015-07-28 03:33:17 +06:00
|
|
|
|
2017-06-07 00:15:23 +02:00
|
|
|
if (NOT BUILD_SHARED_AND_STATIC_LIBS)
|
2017-11-28 17:16:11 +01:00
|
|
|
add_library("${CJSON_UTILS_LIB}" "${CJSON_LIBRARY_TYPE}" "${HEADERS_UTILS}" "${SOURCES_UTILS}")
|
2017-06-07 00:15:23 +02:00
|
|
|
target_link_libraries("${CJSON_UTILS_LIB}" "${CJSON_LIB}")
|
|
|
|
else()
|
|
|
|
add_library("${CJSON_UTILS_LIB}" SHARED "${HEADERS_UTILS}" "${SOURCES_UTILS}")
|
|
|
|
target_link_libraries("${CJSON_UTILS_LIB}" "${CJSON_LIB}")
|
|
|
|
add_library("${CJSON_UTILS_LIB}-static" STATIC "${HEADERS_UTILS}" "${SOURCES_UTILS}")
|
|
|
|
target_link_libraries("${CJSON_UTILS_LIB}-static" "${CJSON_LIB}-static")
|
|
|
|
set_target_properties("${CJSON_UTILS_LIB}-static" PROPERTIES OUTPUT_NAME "${CJSON_UTILS_LIB}")
|
|
|
|
set_target_properties("${CJSON_UTILS_LIB}-static" PROPERTIES PREFIX "lib")
|
|
|
|
endif()
|
2015-07-28 03:33:17 +06:00
|
|
|
|
2017-04-12 21:49:36 +02:00
|
|
|
configure_file("${CMAKE_CURRENT_SOURCE_DIR}/library_config/libcjson_utils.pc.in"
|
2016-11-05 22:42:35 +07:00
|
|
|
"${CMAKE_CURRENT_BINARY_DIR}/libcjson_utils.pc" @ONLY)
|
2016-11-05 15:57:14 +07:00
|
|
|
|
2018-02-06 11:24:03 +01:00
|
|
|
install(TARGETS "${CJSON_UTILS_LIB}" DESTINATION "${CMAKE_INSTALL_FULL_LIBDIR}" EXPORT "${CJSON_UTILS_LIB}")
|
2017-06-07 00:15:23 +02:00
|
|
|
if (BUILD_SHARED_AND_STATIC_LIBS)
|
2018-02-06 11:24:03 +01:00
|
|
|
install(TARGETS "${CJSON_UTILS_LIB}-static" DESTINATION "${CMAKE_INSTALL_FULL_LIBDIR}")
|
2017-06-07 00:15:23 +02:00
|
|
|
endif()
|
2018-02-06 11:24:03 +01:00
|
|
|
install(FILES cJSON_Utils.h DESTINATION "${CMAKE_INSTALL_FULL_INCLUDEDIR}/cjson")
|
|
|
|
install (FILES "${CMAKE_CURRENT_BINARY_DIR}/libcjson_utils.pc" DESTINATION "${CMAKE_INSTALL_FULL_LIBDIR}/pkgconfig")
|
2016-11-07 00:13:38 +07:00
|
|
|
if(ENABLE_TARGET_EXPORT)
|
|
|
|
# export library information for CMake projects
|
2018-02-06 11:24:03 +01:00
|
|
|
install(EXPORT "${CJSON_UTILS_LIB}" DESTINATION "${CMAKE_INSTALL_FULL_LIBDIR}/cmake/cJSON")
|
2016-11-07 00:13:38 +07:00
|
|
|
endif()
|
2015-07-15 14:01:46 +06:00
|
|
|
|
2016-11-05 22:52:40 +07:00
|
|
|
set_target_properties("${CJSON_UTILS_LIB}"
|
2016-11-05 22:20:55 +07:00
|
|
|
PROPERTIES
|
2016-11-05 22:52:40 +07:00
|
|
|
SOVERSION "${CJSON_UTILS_VERSION_SO}"
|
2016-11-06 14:58:22 +07:00
|
|
|
VERSION "${PROJECT_VERSION}")
|
2016-11-05 22:20:55 +07:00
|
|
|
endif()
|
2016-11-05 00:50:51 +07:00
|
|
|
|
2016-11-04 19:59:04 +01:00
|
|
|
# create the other package config files
|
|
|
|
configure_file(
|
2017-04-12 21:49:36 +02:00
|
|
|
"${CMAKE_CURRENT_SOURCE_DIR}/library_config/cJSONConfig.cmake.in"
|
2016-11-15 20:21:30 +07:00
|
|
|
${PROJECT_BINARY_DIR}/cJSONConfig.cmake @ONLY)
|
2016-11-04 19:59:04 +01:00
|
|
|
configure_file(
|
2017-04-12 21:49:36 +02:00
|
|
|
"${CMAKE_CURRENT_SOURCE_DIR}/library_config/cJSONConfigVersion.cmake.in"
|
2016-11-15 20:21:30 +07:00
|
|
|
${PROJECT_BINARY_DIR}/cJSONConfigVersion.cmake @ONLY)
|
2016-11-04 19:59:04 +01:00
|
|
|
|
|
|
|
# Install package config files
|
|
|
|
install(FILES ${PROJECT_BINARY_DIR}/cJSONConfig.cmake
|
2016-11-15 20:21:30 +07:00
|
|
|
${PROJECT_BINARY_DIR}/cJSONConfigVersion.cmake
|
2018-02-06 11:24:03 +01:00
|
|
|
DESTINATION "${CMAKE_INSTALL_FULL_LIBDIR}/cmake/cJSON")
|
2016-11-04 19:59:04 +01:00
|
|
|
|
2016-11-14 19:36:04 +07:00
|
|
|
option(ENABLE_CJSON_TEST "Enable building cJSON test" ON)
|
2015-07-15 14:01:46 +06:00
|
|
|
if(ENABLE_CJSON_TEST)
|
2017-01-16 14:52:01 +01:00
|
|
|
enable_testing()
|
|
|
|
|
2016-11-05 00:40:23 +07:00
|
|
|
set(TEST_CJSON cJSON_test)
|
2016-11-05 22:52:40 +07:00
|
|
|
add_executable("${TEST_CJSON}" test.c)
|
|
|
|
target_link_libraries("${TEST_CJSON}" "${CJSON_LIB}")
|
2015-07-28 03:33:17 +06:00
|
|
|
|
2017-03-15 00:23:48 +01:00
|
|
|
add_test(NAME ${TEST_CJSON} COMMAND "${CMAKE_CURRENT_BINARY_DIR}/${TEST_CJSON}")
|
2017-03-16 00:22:53 +01:00
|
|
|
|
2017-03-15 00:23:48 +01:00
|
|
|
# Disable -fsanitize=float-divide-by-zero for cJSON_test
|
2017-03-16 00:22:53 +01:00
|
|
|
if (FLAG_SUPPORTED_fsanitizefloatdividebyzero)
|
2017-03-16 01:09:26 +01:00
|
|
|
if ("${CMAKE_VERSION}" VERSION_LESS "2.8.12")
|
|
|
|
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fno-sanitize=float-divide-by-zero")
|
|
|
|
else()
|
|
|
|
target_compile_options(${TEST_CJSON} PRIVATE "-fno-sanitize=float-divide-by-zero")
|
|
|
|
endif()
|
2017-03-15 00:23:48 +01:00
|
|
|
endif()
|
|
|
|
|
|
|
|
#"check" target that automatically builds everything and runs the tests
|
|
|
|
add_custom_target(check
|
|
|
|
COMMAND ${CMAKE_CTEST_COMMAND} --output-on-failure
|
2017-04-28 13:58:51 +02:00
|
|
|
DEPENDS ${TEST_CJSON})
|
2015-07-15 14:01:46 +06:00
|
|
|
endif()
|
2017-03-16 00:22:53 +01:00
|
|
|
|
2017-10-08 21:55:58 +03:00
|
|
|
# Enable the use of locales
|
|
|
|
option(ENABLE_LOCALES "Enable the use of locales" ON)
|
|
|
|
if(ENABLE_LOCALES)
|
|
|
|
add_definitions(-DENABLE_LOCALES)
|
2017-11-04 20:23:37 +08:00
|
|
|
endif()
|
2017-10-08 21:55:58 +03:00
|
|
|
|
2017-03-16 00:22:53 +01:00
|
|
|
add_subdirectory(tests)
|
|
|
|
add_subdirectory(fuzzing)
|