CMakeLists.txt 3.7 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 10
set(PROJECT_VERSION_MAJOR 1)
set(PROJECT_VERSION_MINOR 0)
set(PROJECT_VERSION_PATCH 0)
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
if(("${CMAKE_C_COMPILER_ID}" MATCHES "GNU") OR ("${CMAKE_C_COMPILER_ID}" MATCHES "Clang"))
16
    set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=c89 -pedantic -Wall -Wextra -Werror -Wstrict-prototypes -Wwrite-strings")
17
endif()
M
Max Bruckner 已提交
18

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

25
option(BUILD_SHARED_LIBS "Build shared libraries" ON)
26
option(ENABLE_TARGET_EXPORT "Enable exporting of CMake targets. Disable when it causes problems!" ON)
27

M
Max Bruckner 已提交
28
#cJSON
29
set(CJSON_LIB cjson)
M
Max Bruckner 已提交
30

A
Anton Sergeev 已提交
31 32 33
file(GLOB HEADERS cJSON.h)
set(SOURCES cJSON.c)

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

39 40
configure_file("${CMAKE_CURRENT_SOURCE_DIR}/libcjson.pc.in"
    "${CMAKE_CURRENT_BINARY_DIR}/libcjson.pc" @ONLY)
41

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

M
Max Bruckner 已提交
50
set_target_properties("${CJSON_LIB}"
M
Max Bruckner 已提交
51
    PROPERTIES
M
Max Bruckner 已提交
52
        SOVERSION "${CJSON_VERSION_SO}"
53
        VERSION "${PROJECT_VERSION}")
M
Max Bruckner 已提交
54

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

60 61
    file(GLOB HEADERS_UTILS cJSON_Utils.h)
    set(SOURCES_UTILS cJSON_Utils.c)
62

M
Max Bruckner 已提交
63 64
    add_library("${CJSON_UTILS_LIB}" "${HEADERS_UTILS}" "${SOURCES_UTILS}")
    target_link_libraries("${CJSON_UTILS_LIB}" "${CJSON_LIB}")
65

66 67
    configure_file("${CMAKE_CURRENT_SOURCE_DIR}/libcjson_utils.pc.in"
        "${CMAKE_CURRENT_BINARY_DIR}/libcjson_utils.pc" @ONLY)
M
Max Bruckner 已提交
68

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

M
Max Bruckner 已提交
77
    set_target_properties("${CJSON_UTILS_LIB}"
78
        PROPERTIES
M
Max Bruckner 已提交
79
            SOVERSION "${CJSON_UTILS_VERSION_SO}"
80
            VERSION "${PROJECT_VERSION}")
81
endif()
M
Max Bruckner 已提交
82

83 84 85 86 87 88 89 90 91 92 93
# create the other package config files
configure_file(
  cJSONConfig.cmake.in
  ${PROJECT_BINARY_DIR}/cJSONConfig.cmake @ONLY)
configure_file(
  cJSONConfigVersion.cmake.in
  ${PROJECT_BINARY_DIR}/cJSONConfigVersion.cmake @ONLY)

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

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

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