CMakeLists.txt 6.2 KB
Newer Older
M
Max Bruckner 已提交
1
set(CMAKE_LEGACY_CYGWIN_WIN32 0)
2
cmake_minimum_required(VERSION 2.8.5)
A
Anton Sergeev 已提交
3

4 5
include(GNUInstallDirs)

M
Max Bruckner 已提交
6 7
project(cJSON C)

8
set(PROJECT_VERSION_MAJOR 1)
M
Max Bruckner 已提交
9
set(PROJECT_VERSION_MINOR 5)
M
Max Bruckner 已提交
10
set(PROJECT_VERSION_PATCH 3)
M
Max Bruckner 已提交
11
set(CJSON_VERSION_SO 1)
12
set(CJSON_UTILS_VERSION_SO 1)
13
set(PROJECT_VERSION "${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}.${PROJECT_VERSION_PATCH}")
A
Anton Sergeev 已提交
14

15 16 17 18

set(custom_compiler_flags)

include(CheckCCompilerFlag)
19
option(ENABLE_CUSTOM_COMPILER_FLAGS "Enables custom compiler flags for Clang and GCC" ON)
20
if (ENABLE_CUSTOM_COMPILER_FLAGS)
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
    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
39
        -Wc++-compat
40
        -fstack-protector-strong
M
Max Bruckner 已提交
41
        -Wcomma
42
        -Wdouble-promotion
M
Max Bruckner 已提交
43
        -Wparentheses
44
        )
45
endif()
M
Max Bruckner 已提交
46

M
Max Bruckner 已提交
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
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()

62 63 64 65 66 67 68 69 70 71
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()

72 73
# apply custom compiler flags
foreach(compiler_flag ${custom_compiler_flags})
74 75 76 77 78
    #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})
79
        list(APPEND supported_compiler_flags)
80 81 82 83
        set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${compiler_flag}")
    endif()
endforeach()

84 85
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${supported_compiler_flags}")

86
#variables for pkg-config
M
Max Bruckner 已提交
87
set(prefix "${CMAKE_INSTALL_PREFIX}")
M
Max Bruckner 已提交
88
set(libdir "${CMAKE_INSTALL_LIBDIR}")
89
set(version "${PROJECT_VERSION}")
M
Max Bruckner 已提交
90
set(includedir "${CMAKE_INSTALL_INCLUDEDIR}")
91

92
option(BUILD_SHARED_LIBS "Build shared libraries" ON)
93
option(ENABLE_TARGET_EXPORT "Enable exporting of CMake targets. Disable when it causes problems!" ON)
94

M
Max Bruckner 已提交
95
#cJSON
96
set(CJSON_LIB cjson)
M
Max Bruckner 已提交
97

A
Anton Sergeev 已提交
98 99 100
file(GLOB HEADERS cJSON.h)
set(SOURCES cJSON.c)

M
Max Bruckner 已提交
101
add_library("${CJSON_LIB}" "${HEADERS}" "${SOURCES}")
102
if (NOT WIN32)
M
Max Bruckner 已提交
103
    target_link_libraries("${CJSON_LIB}" m)
104
endif()
A
Anton Sergeev 已提交
105

106
configure_file("${CMAKE_CURRENT_SOURCE_DIR}/library_config/libcjson.pc.in"
107
    "${CMAKE_CURRENT_BINARY_DIR}/libcjson.pc" @ONLY)
108

109 110
install(FILES cJSON.h DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/cjson")
install (FILES "${CMAKE_CURRENT_BINARY_DIR}/libcjson.pc" DESTINATION "${CMAKE_INSTALL_LIBDIR}/pkgconfig")
M
Max Bruckner 已提交
111
install(TARGETS "${CJSON_LIB}" DESTINATION "${CMAKE_INSTALL_LIBDIR}" EXPORT "${CJSON_LIB}")
112
if(ENABLE_TARGET_EXPORT)
M
Max Bruckner 已提交
113 114
    # export library information for CMake projects
    install(EXPORT "${CJSON_LIB}" DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/cJSON")
115
endif()
116

M
Max Bruckner 已提交
117
set_target_properties("${CJSON_LIB}"
M
Max Bruckner 已提交
118
    PROPERTIES
M
Max Bruckner 已提交
119
        SOVERSION "${CJSON_VERSION_SO}"
120
        VERSION "${PROJECT_VERSION}")
M
Max Bruckner 已提交
121

M
Max Bruckner 已提交
122
#cJSON_Utils
123 124 125
option(ENABLE_CJSON_UTILS "Enable building the cJSON_Utils library." OFF)
if(ENABLE_CJSON_UTILS)
    set(CJSON_UTILS_LIB cjson_utils)
126

127 128
    file(GLOB HEADERS_UTILS cJSON_Utils.h)
    set(SOURCES_UTILS cJSON_Utils.c)
129

M
Max Bruckner 已提交
130 131
    add_library("${CJSON_UTILS_LIB}" "${HEADERS_UTILS}" "${SOURCES_UTILS}")
    target_link_libraries("${CJSON_UTILS_LIB}" "${CJSON_LIB}")
132

133
    configure_file("${CMAKE_CURRENT_SOURCE_DIR}/library_config/libcjson_utils.pc.in"
134
        "${CMAKE_CURRENT_BINARY_DIR}/libcjson_utils.pc" @ONLY)
M
Max Bruckner 已提交
135

M
Max Bruckner 已提交
136
    install(TARGETS "${CJSON_UTILS_LIB}" DESTINATION "${CMAKE_INSTALL_LIBDIR}" EXPORT "${CJSON_UTILS_LIB}")
137 138
    install(FILES cJSON_Utils.h DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/cjson")
    install (FILES "${CMAKE_CURRENT_BINARY_DIR}/libcjson_utils.pc" DESTINATION "${CMAKE_INSTALL_LIBDIR}/pkgconfig")
139 140 141 142
    if(ENABLE_TARGET_EXPORT)
      # export library information for CMake projects
      install(EXPORT "${CJSON_UTILS_LIB}" DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/cJSON")
    endif()
A
Anton Sergeev 已提交
143

M
Max Bruckner 已提交
144
    set_target_properties("${CJSON_UTILS_LIB}"
145
        PROPERTIES
M
Max Bruckner 已提交
146
            SOVERSION "${CJSON_UTILS_VERSION_SO}"
147
            VERSION "${PROJECT_VERSION}")
148
endif()
M
Max Bruckner 已提交
149

150 151
# create the other package config files
configure_file(
152
    "${CMAKE_CURRENT_SOURCE_DIR}/library_config/cJSONConfig.cmake.in"
M
Max Bruckner 已提交
153
    ${PROJECT_BINARY_DIR}/cJSONConfig.cmake @ONLY)
154
configure_file(
155
    "${CMAKE_CURRENT_SOURCE_DIR}/library_config/cJSONConfigVersion.cmake.in"
M
Max Bruckner 已提交
156
    ${PROJECT_BINARY_DIR}/cJSONConfigVersion.cmake @ONLY)
157 158 159

# Install package config files
install(FILES ${PROJECT_BINARY_DIR}/cJSONConfig.cmake
M
Max Bruckner 已提交
160 161
    ${PROJECT_BINARY_DIR}/cJSONConfigVersion.cmake
    DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/cJSON")
162

M
Max Bruckner 已提交
163
option(ENABLE_CJSON_TEST "Enable building cJSON test" ON)
A
Anton Sergeev 已提交
164
if(ENABLE_CJSON_TEST)
165 166
    enable_testing()

M
Max Bruckner 已提交
167
    set(TEST_CJSON cJSON_test)
M
Max Bruckner 已提交
168 169
    add_executable("${TEST_CJSON}" test.c)
    target_link_libraries("${TEST_CJSON}" "${CJSON_LIB}")
170

171
    add_test(NAME ${TEST_CJSON} COMMAND "${CMAKE_CURRENT_BINARY_DIR}/${TEST_CJSON}")
172

173
    # Disable -fsanitize=float-divide-by-zero for cJSON_test
174
    if (FLAG_SUPPORTED_fsanitizefloatdividebyzero)
175 176 177 178 179
        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()
180 181 182 183 184
    endif()

    #"check" target that automatically builds everything and runs the tests
    add_custom_target(check
        COMMAND ${CMAKE_CTEST_COMMAND} --output-on-failure
185
        DEPENDS ${TEST_CJSON})
A
Anton Sergeev 已提交
186
endif()
187 188 189

add_subdirectory(tests)
add_subdirectory(fuzzing)