CMakeLists.txt 4.0 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 5
include(GNUInstallDirs)

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

8 9
set(PROJECT_VERSION_MAJOR 1)
set(PROJECT_VERSION_MINOR 0)
M
Max Bruckner 已提交
10
set(PROJECT_VERSION_PATCH 2)
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
option(ENABLE_CUSTOM_COMPILER_FLAGS "Enables custom compiler flags for Clang and GCC" ON)
16 17
if (ENABLE_CUSTOM_COMPILER_FLAGS)
    if(("${CMAKE_C_COMPILER_ID}" MATCHES "GNU") OR ("${CMAKE_C_COMPILER_ID}" MATCHES "Clang"))
18
        set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=c89 -pedantic -Wall -Wextra -Werror -Wstrict-prototypes -Wwrite-strings -Wshadow -Winit-self -Wcast-align -Wformat=2 -Wmissing-prototypes -Wstrict-overflow=2")
19
    endif()
20
endif()
M
Max Bruckner 已提交
21

22
#variables for pkg-config
M
Max Bruckner 已提交
23
set(prefix "${CMAKE_INSTALL_PREFIX}")
M
Max Bruckner 已提交
24
set(libdir "${CMAKE_INSTALL_LIBDIR}")
25
set(version "${PROJECT_VERSION}")
M
Max Bruckner 已提交
26
set(includedir "${CMAKE_INSTALL_INCLUDEDIR}")
27

28
option(BUILD_SHARED_LIBS "Build shared libraries" ON)
29
option(ENABLE_TARGET_EXPORT "Enable exporting of CMake targets. Disable when it causes problems!" ON)
30

M
Max Bruckner 已提交
31
#cJSON
32
set(CJSON_LIB cjson)
M
Max Bruckner 已提交
33

A
Anton Sergeev 已提交
34 35 36
file(GLOB HEADERS cJSON.h)
set(SOURCES cJSON.c)

M
Max Bruckner 已提交
37
add_library("${CJSON_LIB}" "${HEADERS}" "${SOURCES}")
38
if (NOT WIN32)
M
Max Bruckner 已提交
39
    target_link_libraries("${CJSON_LIB}" m)
40
endif()
A
Anton Sergeev 已提交
41

42 43
configure_file("${CMAKE_CURRENT_SOURCE_DIR}/libcjson.pc.in"
    "${CMAKE_CURRENT_BINARY_DIR}/libcjson.pc" @ONLY)
44

45 46
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 已提交
47
install(TARGETS "${CJSON_LIB}" DESTINATION "${CMAKE_INSTALL_LIBDIR}" EXPORT "${CJSON_LIB}")
48
if(ENABLE_TARGET_EXPORT)
M
Max Bruckner 已提交
49 50
    # export library information for CMake projects
    install(EXPORT "${CJSON_LIB}" DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/cJSON")
51
endif()
52

M
Max Bruckner 已提交
53
set_target_properties("${CJSON_LIB}"
M
Max Bruckner 已提交
54
    PROPERTIES
M
Max Bruckner 已提交
55
        SOVERSION "${CJSON_VERSION_SO}"
56
        VERSION "${PROJECT_VERSION}")
M
Max Bruckner 已提交
57

M
Max Bruckner 已提交
58
#cJSON_Utils
59 60 61
option(ENABLE_CJSON_UTILS "Enable building the cJSON_Utils library." OFF)
if(ENABLE_CJSON_UTILS)
    set(CJSON_UTILS_LIB cjson_utils)
62

63 64
    file(GLOB HEADERS_UTILS cJSON_Utils.h)
    set(SOURCES_UTILS cJSON_Utils.c)
65

M
Max Bruckner 已提交
66 67
    add_library("${CJSON_UTILS_LIB}" "${HEADERS_UTILS}" "${SOURCES_UTILS}")
    target_link_libraries("${CJSON_UTILS_LIB}" "${CJSON_LIB}")
68

69 70
    configure_file("${CMAKE_CURRENT_SOURCE_DIR}/libcjson_utils.pc.in"
        "${CMAKE_CURRENT_BINARY_DIR}/libcjson_utils.pc" @ONLY)
M
Max Bruckner 已提交
71

M
Max Bruckner 已提交
72
    install(TARGETS "${CJSON_UTILS_LIB}" DESTINATION "${CMAKE_INSTALL_LIBDIR}" EXPORT "${CJSON_UTILS_LIB}")
73 74
    install(FILES cJSON_Utils.h DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/cjson")
    install (FILES "${CMAKE_CURRENT_BINARY_DIR}/libcjson_utils.pc" DESTINATION "${CMAKE_INSTALL_LIBDIR}/pkgconfig")
75 76 77 78
    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 已提交
79

M
Max Bruckner 已提交
80
    set_target_properties("${CJSON_UTILS_LIB}"
81
        PROPERTIES
M
Max Bruckner 已提交
82
            SOVERSION "${CJSON_UTILS_VERSION_SO}"
83
            VERSION "${PROJECT_VERSION}")
84
endif()
M
Max Bruckner 已提交
85

86 87
# create the other package config files
configure_file(
M
Max Bruckner 已提交
88 89
    cJSONConfig.cmake.in
    ${PROJECT_BINARY_DIR}/cJSONConfig.cmake @ONLY)
90
configure_file(
M
Max Bruckner 已提交
91 92
    cJSONConfigVersion.cmake.in
    ${PROJECT_BINARY_DIR}/cJSONConfigVersion.cmake @ONLY)
93 94 95

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

M
Max Bruckner 已提交
99
option(ENABLE_CJSON_TEST "Enable building cJSON test" ON)
A
Anton Sergeev 已提交
100
if(ENABLE_CJSON_TEST)
M
Max Bruckner 已提交
101
    set(TEST_CJSON cJSON_test)
M
Max Bruckner 已提交
102 103
    add_executable("${TEST_CJSON}" test.c)
    target_link_libraries("${TEST_CJSON}" "${CJSON_LIB}")
104

105 106
    if(ENABLE_CJSON_UTILS)
        set(TEST_CJSON_UTILS cJSON_test_utils)
M
Max Bruckner 已提交
107 108
        add_executable("${TEST_CJSON_UTILS}" test_utils.c)
        target_link_libraries("${TEST_CJSON_UTILS}" "${CJSON_UTILS_LIB}")
109
    endif()
A
Anton Sergeev 已提交
110
endif()