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

4
subdirs(tests fuzzing)
M
Max Bruckner 已提交
5

6 7
include(GNUInstallDirs)

M
Max Bruckner 已提交
8 9
project(cJSON C)

10
set(PROJECT_VERSION_MAJOR 1)
M
Max Bruckner 已提交
11
set(PROJECT_VERSION_MINOR 4)
M
Max Bruckner 已提交
12
set(PROJECT_VERSION_PATCH 0)
M
Max Bruckner 已提交
13
set(CJSON_VERSION_SO 1)
14
set(CJSON_UTILS_VERSION_SO 1)
15
set(PROJECT_VERSION "${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}.${PROJECT_VERSION_PATCH}")
A
Anton Sergeev 已提交
16

17 18 19 20

set(custom_compiler_flags)

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

170 171
    if(ENABLE_CJSON_UTILS)
        set(TEST_CJSON_UTILS cJSON_test_utils)
M
Max Bruckner 已提交
172 173
        add_executable("${TEST_CJSON_UTILS}" test_utils.c)
        target_link_libraries("${TEST_CJSON_UTILS}" "${CJSON_UTILS_LIB}")
174
    endif()
A
Anton Sergeev 已提交
175
endif()