CMakeLists.txt 8.1 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 6)
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" 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 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
    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")
        list(APPEND custom_compiler_flags
            /GS
            /Za
            /sdl
            /W4
57
        )
58
    endif()
59
endif()
M
Max Bruckner 已提交
60

M
Max Bruckner 已提交
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
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()

76 77 78 79 80 81 82 83 84 85
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()

86 87
# apply custom compiler flags
foreach(compiler_flag ${custom_compiler_flags})
88 89 90 91 92
    #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})
93
        list(APPEND supported_compiler_flags)
94 95 96 97
        set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${compiler_flag}")
    endif()
endforeach()

98 99
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${supported_compiler_flags}")

100
#variables for pkg-config
M
Max Bruckner 已提交
101
set(prefix "${CMAKE_INSTALL_PREFIX}")
M
Max Bruckner 已提交
102
set(libdir "${CMAKE_INSTALL_LIBDIR}")
103
set(version "${PROJECT_VERSION}")
M
Max Bruckner 已提交
104
set(includedir "${CMAKE_INSTALL_INCLUDEDIR}")
105

106
option(BUILD_SHARED_LIBS "Build shared libraries" ON)
107
option(ENABLE_TARGET_EXPORT "Enable exporting of CMake targets. Disable when it causes problems!" ON)
108

M
Max Bruckner 已提交
109
#cJSON
110
set(CJSON_LIB cjson)
M
Max Bruckner 已提交
111

A
Anton Sergeev 已提交
112 113 114
file(GLOB HEADERS cJSON.h)
set(SOURCES cJSON.c)

115 116 117 118 119 120 121 122 123 124 125
option(BUILD_SHARED_AND_STATIC_LIBS "Build both shared and static libraries" Off)

if (NOT BUILD_SHARED_AND_STATIC_LIBS)
    add_library("${CJSON_LIB}" "${HEADERS}" "${SOURCES}")
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()
126
if (NOT WIN32)
M
Max Bruckner 已提交
127
    target_link_libraries("${CJSON_LIB}" m)
128
endif()
A
Anton Sergeev 已提交
129

130
configure_file("${CMAKE_CURRENT_SOURCE_DIR}/library_config/libcjson.pc.in"
131
    "${CMAKE_CURRENT_BINARY_DIR}/libcjson.pc" @ONLY)
132

133 134
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 已提交
135
install(TARGETS "${CJSON_LIB}" DESTINATION "${CMAKE_INSTALL_LIBDIR}" EXPORT "${CJSON_LIB}")
136 137 138
if (BUILD_SHARED_AND_STATIC_LIBS)
    install(TARGETS "${CJSON_LIB}-static" DESTINATION "${CMAKE_INSTALL_LIBDIR}")
endif()
139
if(ENABLE_TARGET_EXPORT)
M
Max Bruckner 已提交
140 141
    # export library information for CMake projects
    install(EXPORT "${CJSON_LIB}" DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/cJSON")
142
endif()
143

M
Max Bruckner 已提交
144
set_target_properties("${CJSON_LIB}"
M
Max Bruckner 已提交
145
    PROPERTIES
M
Max Bruckner 已提交
146
        SOVERSION "${CJSON_VERSION_SO}"
147
        VERSION "${PROJECT_VERSION}")
M
Max Bruckner 已提交
148

M
Max Bruckner 已提交
149
#cJSON_Utils
150 151 152
option(ENABLE_CJSON_UTILS "Enable building the cJSON_Utils library." OFF)
if(ENABLE_CJSON_UTILS)
    set(CJSON_UTILS_LIB cjson_utils)
153

154 155
    file(GLOB HEADERS_UTILS cJSON_Utils.h)
    set(SOURCES_UTILS cJSON_Utils.c)
156

157 158 159 160 161 162 163 164 165 166 167
    if (NOT BUILD_SHARED_AND_STATIC_LIBS)
        add_library("${CJSON_UTILS_LIB}" "${HEADERS_UTILS}" "${SOURCES_UTILS}")
        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()
168

169
    configure_file("${CMAKE_CURRENT_SOURCE_DIR}/library_config/libcjson_utils.pc.in"
170
        "${CMAKE_CURRENT_BINARY_DIR}/libcjson_utils.pc" @ONLY)
M
Max Bruckner 已提交
171

M
Max Bruckner 已提交
172
    install(TARGETS "${CJSON_UTILS_LIB}" DESTINATION "${CMAKE_INSTALL_LIBDIR}" EXPORT "${CJSON_UTILS_LIB}")
173 174 175
    if (BUILD_SHARED_AND_STATIC_LIBS)
        install(TARGETS "${CJSON_UTILS_LIB}-static" DESTINATION "${CMAKE_INSTALL_LIBDIR}")
    endif()
176 177
    install(FILES cJSON_Utils.h DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/cjson")
    install (FILES "${CMAKE_CURRENT_BINARY_DIR}/libcjson_utils.pc" DESTINATION "${CMAKE_INSTALL_LIBDIR}/pkgconfig")
178 179 180 181
    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 已提交
182

M
Max Bruckner 已提交
183
    set_target_properties("${CJSON_UTILS_LIB}"
184
        PROPERTIES
M
Max Bruckner 已提交
185
            SOVERSION "${CJSON_UTILS_VERSION_SO}"
186
            VERSION "${PROJECT_VERSION}")
187
endif()
M
Max Bruckner 已提交
188

189 190
# create the other package config files
configure_file(
191
    "${CMAKE_CURRENT_SOURCE_DIR}/library_config/cJSONConfig.cmake.in"
M
Max Bruckner 已提交
192
    ${PROJECT_BINARY_DIR}/cJSONConfig.cmake @ONLY)
193
configure_file(
194
    "${CMAKE_CURRENT_SOURCE_DIR}/library_config/cJSONConfigVersion.cmake.in"
M
Max Bruckner 已提交
195
    ${PROJECT_BINARY_DIR}/cJSONConfigVersion.cmake @ONLY)
196 197 198

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

M
Max Bruckner 已提交
202
option(ENABLE_CJSON_TEST "Enable building cJSON test" ON)
A
Anton Sergeev 已提交
203
if(ENABLE_CJSON_TEST)
204 205
    enable_testing()

M
Max Bruckner 已提交
206
    set(TEST_CJSON cJSON_test)
M
Max Bruckner 已提交
207 208
    add_executable("${TEST_CJSON}" test.c)
    target_link_libraries("${TEST_CJSON}" "${CJSON_LIB}")
209

210
    add_test(NAME ${TEST_CJSON} COMMAND "${CMAKE_CURRENT_BINARY_DIR}/${TEST_CJSON}")
211

212
    # Disable -fsanitize=float-divide-by-zero for cJSON_test
213
    if (FLAG_SUPPORTED_fsanitizefloatdividebyzero)
214 215 216 217 218
        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()
219 220 221 222 223
    endif()

    #"check" target that automatically builds everything and runs the tests
    add_custom_target(check
        COMMAND ${CMAKE_CTEST_COMMAND} --output-on-failure
224
        DEPENDS ${TEST_CJSON})
A
Anton Sergeev 已提交
225
endif()
226 227 228

add_subdirectory(tests)
add_subdirectory(fuzzing)