CMakeLists.txt 4.6 KB

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