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 9)
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
        -Wformat-overflow
45
        )
46
endif()
M
Max Bruckner 已提交
47

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

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

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

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

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

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

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

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

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

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

110 111
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 已提交
112
install(TARGETS "${CJSON_LIB}" DESTINATION "${CMAKE_INSTALL_LIBDIR}" EXPORT "${CJSON_LIB}")
113
if(ENABLE_TARGET_EXPORT)
M
Max Bruckner 已提交
114 115
    # export library information for CMake projects
    install(EXPORT "${CJSON_LIB}" DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/cJSON")
116
endif()
117

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

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

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

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

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

M
Max Bruckner 已提交
137
    install(TARGETS "${CJSON_UTILS_LIB}" DESTINATION "${CMAKE_INSTALL_LIBDIR}" EXPORT "${CJSON_UTILS_LIB}")
138 139
    install(FILES cJSON_Utils.h DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/cjson")
    install (FILES "${CMAKE_CURRENT_BINARY_DIR}/libcjson_utils.pc" DESTINATION "${CMAKE_INSTALL_LIBDIR}/pkgconfig")
140 141 142 143
    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 已提交
144

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

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

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

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

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

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

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

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

add_subdirectory(tests)
add_subdirectory(fuzzing)