CMakeLists.txt 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. cmake_minimum_required( VERSION 3.13.0 )
  2. project( "CoreJSON unit test"
  3. VERSION 1.0.0
  4. LANGUAGES C )
  5. # Allow the project to be organized into folders.
  6. set_property( GLOBAL PROPERTY USE_FOLDERS ON )
  7. # Use C90.
  8. set( CMAKE_C_STANDARD 90 )
  9. set( CMAKE_C_STANDARD_REQUIRED ON )
  10. # Do not allow in-source build.
  11. if( ${PROJECT_SOURCE_DIR} STREQUAL ${PROJECT_BINARY_DIR} )
  12. message( FATAL_ERROR "In-source build is not allowed. Please build in a separate directory, such as ${PROJECT_SOURCE_DIR}/build." )
  13. endif()
  14. # Set global path variables.
  15. get_filename_component(__MODULE_ROOT_DIR "${CMAKE_CURRENT_LIST_DIR}/.." ABSOLUTE)
  16. set( MODULE_ROOT_DIR ${__MODULE_ROOT_DIR} CACHE INTERNAL "coreJSON source root." )
  17. set( UNIT_TEST_DIR ${MODULE_ROOT_DIR}/test/unit-test CACHE INTERNAL "coreJSON unit test directory." )
  18. set( UNITY_DIR ${UNIT_TEST_DIR}/Unity CACHE INTERNAL "Unity library source directory." )
  19. # If no configuration is defined, turn everything on.
  20. if( NOT DEFINED COV_ANALYSIS AND NOT DEFINED UNITTEST )
  21. set( COV_ANALYSIS TRUE )
  22. set( UNITTEST TRUE )
  23. endif()
  24. # Configure options to always show in CMake GUI.
  25. option( BUILD_CLONE_SUBMODULES
  26. "Set this to ON to automatically clone any required Git submodules. When OFF, submodules must be manually cloned."
  27. OFF )
  28. # Set output directories.
  29. set( CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin )
  30. set( CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib )
  31. set( CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib )
  32. # ================================ Coverity Analysis Configuration =================================
  33. if( COV_ANALYSIS )
  34. # Include filepaths for source and include.
  35. include( ${MODULE_ROOT_DIR}/jsonFilePaths.cmake )
  36. # Target for Coverity analysis that builds the library.
  37. add_library( coverity_analysis
  38. ${JSON_SOURCES} )
  39. # JSON public include path.
  40. target_include_directories( coverity_analysis PUBLIC ${JSON_INCLUDE_PUBLIC_DIRS} )
  41. # When building the coverity analysis target we disable debug
  42. target_compile_options(coverity_analysis PUBLIC -DNDEBUG )
  43. endif()
  44. # ==================================== Test Configuration ========================================
  45. if( UNITTEST )
  46. # Include Unity build configuration.
  47. include( unit-test/unity_build.cmake )
  48. # Check if the Unity source directory exists, and if not present, clone the submodule
  49. # if BUILD_CLONE_SUBMODULES configuration is enabled.
  50. if( NOT EXISTS ${UNITY_DIR}/src )
  51. # Attempt to clone Unity.
  52. if( ${BUILD_CLONE_SUBMODULES} )
  53. clone_unity()
  54. else()
  55. message( FATAL_ERROR "The required submodule Unity does not exist. Either clone it manually, or set BUILD_CLONE_SUBMODULES to 1 to automatically clone it during build." )
  56. endif()
  57. endif()
  58. # Add unit test and coverage configuration.
  59. # Use CTest utility for managing test runs. This has to be added BEFORE
  60. # defining test targets with add_test()
  61. enable_testing()
  62. # Add build targets for Unity and Unit, required for unit testing.
  63. add_unity_targets()
  64. # Add function to enable Unity based tests and coverage.
  65. include( ${MODULE_ROOT_DIR}/tools/unity/create_test.cmake )
  66. # Include build configuration for unit tests.
  67. add_subdirectory( unit-test )
  68. # ==================================== Coverage Analysis configuration ============================
  69. # Add a target for running coverage on tests.
  70. add_custom_target( coverage
  71. COMMAND ${CMAKE_COMMAND} -DUNITY_DIR=${UNITY_DIR}
  72. -P ${MODULE_ROOT_DIR}/tools/unity/coverage.cmake
  73. DEPENDS unity core_json_utest
  74. WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
  75. )
  76. endif()