CMakeLists.txt 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. set(CMAKE_LEGACY_CYGWIN_WIN32 0)
  2. cmake_minimum_required(VERSION 2.8)
  3. include(GNUInstallDirs)
  4. project(cJSON C)
  5. set(PROJECT_VERSION_MAJOR 1)
  6. set(PROJECT_VERSION_MINOR 2)
  7. set(PROJECT_VERSION_PATCH 1)
  8. set(CJSON_VERSION_SO 1)
  9. set(CJSON_UTILS_VERSION_SO 1)
  10. set(PROJECT_VERSION "${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}.${PROJECT_VERSION_PATCH}")
  11. set(custom_compiler_flags)
  12. include(CheckCCompilerFlag)
  13. option(ENABLE_CUSTOM_COMPILER_FLAGS "Enables custom compiler flags for Clang and GCC" ON)
  14. if (ENABLE_CUSTOM_COMPILER_FLAGS)
  15. list(APPEND custom_compiler_flags
  16. -std=c89
  17. -pedantic
  18. -Wall
  19. -Wextra
  20. -Werror
  21. -Wstrict-prototypes
  22. -Wwrite-strings
  23. -Wshadow
  24. -Winit-self
  25. -Wcast-align
  26. -Wformat=2
  27. -Wmissing-prototypes
  28. -Wstrict-overflow=2
  29. -Wcast-qual
  30. -Wundef
  31. -Wswitch-default
  32. -Wconversion
  33. -fstack-protector-strong
  34. )
  35. endif()
  36. # "I am starting to hate CMake" - FSMaxB
  37. # In older versions of CMake, testing for -Wc++-compat
  38. # fails because it cannot compile a regular expression
  39. # This seems to be a bug in the CheckCSourceCompiles module
  40. if (NOT (CMAKE_MAJOR_VERSION EQUAL 2))
  41. list(APPEND custom_compiler_flags "-Wc++-compat")
  42. endif()
  43. # apply custom compiler flags
  44. foreach(compiler_flag ${custom_compiler_flags})
  45. CHECK_C_COMPILER_FLAG(${compiler_flag} "FLAG_SUPPORTED-${compiler_flag}")
  46. if (FLAG_SUPPORTED${compiler_flag})
  47. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${compiler_flag}")
  48. endif()
  49. endforeach()
  50. #variables for pkg-config
  51. set(prefix "${CMAKE_INSTALL_PREFIX}")
  52. set(libdir "${CMAKE_INSTALL_LIBDIR}")
  53. set(version "${PROJECT_VERSION}")
  54. set(includedir "${CMAKE_INSTALL_INCLUDEDIR}")
  55. option(BUILD_SHARED_LIBS "Build shared libraries" ON)
  56. option(ENABLE_TARGET_EXPORT "Enable exporting of CMake targets. Disable when it causes problems!" ON)
  57. #cJSON
  58. set(CJSON_LIB cjson)
  59. file(GLOB HEADERS cJSON.h)
  60. set(SOURCES cJSON.c)
  61. add_library("${CJSON_LIB}" "${HEADERS}" "${SOURCES}")
  62. if (NOT WIN32)
  63. target_link_libraries("${CJSON_LIB}" m)
  64. endif()
  65. configure_file("${CMAKE_CURRENT_SOURCE_DIR}/libcjson.pc.in"
  66. "${CMAKE_CURRENT_BINARY_DIR}/libcjson.pc" @ONLY)
  67. install(FILES cJSON.h DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/cjson")
  68. install (FILES "${CMAKE_CURRENT_BINARY_DIR}/libcjson.pc" DESTINATION "${CMAKE_INSTALL_LIBDIR}/pkgconfig")
  69. install(TARGETS "${CJSON_LIB}" DESTINATION "${CMAKE_INSTALL_LIBDIR}" EXPORT "${CJSON_LIB}")
  70. if(ENABLE_TARGET_EXPORT)
  71. # export library information for CMake projects
  72. install(EXPORT "${CJSON_LIB}" DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/cJSON")
  73. endif()
  74. set_target_properties("${CJSON_LIB}"
  75. PROPERTIES
  76. SOVERSION "${CJSON_VERSION_SO}"
  77. VERSION "${PROJECT_VERSION}")
  78. #cJSON_Utils
  79. option(ENABLE_CJSON_UTILS "Enable building the cJSON_Utils library." OFF)
  80. if(ENABLE_CJSON_UTILS)
  81. set(CJSON_UTILS_LIB cjson_utils)
  82. file(GLOB HEADERS_UTILS cJSON_Utils.h)
  83. set(SOURCES_UTILS cJSON_Utils.c)
  84. add_library("${CJSON_UTILS_LIB}" "${HEADERS_UTILS}" "${SOURCES_UTILS}")
  85. target_link_libraries("${CJSON_UTILS_LIB}" "${CJSON_LIB}")
  86. configure_file("${CMAKE_CURRENT_SOURCE_DIR}/libcjson_utils.pc.in"
  87. "${CMAKE_CURRENT_BINARY_DIR}/libcjson_utils.pc" @ONLY)
  88. install(TARGETS "${CJSON_UTILS_LIB}" DESTINATION "${CMAKE_INSTALL_LIBDIR}" EXPORT "${CJSON_UTILS_LIB}")
  89. install(FILES cJSON_Utils.h DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/cjson")
  90. install (FILES "${CMAKE_CURRENT_BINARY_DIR}/libcjson_utils.pc" DESTINATION "${CMAKE_INSTALL_LIBDIR}/pkgconfig")
  91. if(ENABLE_TARGET_EXPORT)
  92. # export library information for CMake projects
  93. install(EXPORT "${CJSON_UTILS_LIB}" DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/cJSON")
  94. endif()
  95. set_target_properties("${CJSON_UTILS_LIB}"
  96. PROPERTIES
  97. SOVERSION "${CJSON_UTILS_VERSION_SO}"
  98. VERSION "${PROJECT_VERSION}")
  99. endif()
  100. # create the other package config files
  101. configure_file(
  102. cJSONConfig.cmake.in
  103. ${PROJECT_BINARY_DIR}/cJSONConfig.cmake @ONLY)
  104. configure_file(
  105. cJSONConfigVersion.cmake.in
  106. ${PROJECT_BINARY_DIR}/cJSONConfigVersion.cmake @ONLY)
  107. # Install package config files
  108. install(FILES ${PROJECT_BINARY_DIR}/cJSONConfig.cmake
  109. ${PROJECT_BINARY_DIR}/cJSONConfigVersion.cmake
  110. DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/cJSON")
  111. option(ENABLE_CJSON_TEST "Enable building cJSON test" ON)
  112. if(ENABLE_CJSON_TEST)
  113. set(TEST_CJSON cJSON_test)
  114. add_executable("${TEST_CJSON}" test.c)
  115. target_link_libraries("${TEST_CJSON}" "${CJSON_LIB}")
  116. if(ENABLE_CJSON_UTILS)
  117. set(TEST_CJSON_UTILS cJSON_test_utils)
  118. add_executable("${TEST_CJSON_UTILS}" test_utils.c)
  119. target_link_libraries("${TEST_CJSON_UTILS}" "${CJSON_UTILS_LIB}")
  120. endif()
  121. endif()