CMakeLists.txt 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. cmake_minimum_required ( VERSION 3.13.0 )
  2. project ( "CoreMQTT 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 if not specified.
  8. if( NOT DEFINED CMAKE_C_STANDARD )
  9. set( CMAKE_C_STANDARD 90 )
  10. endif()
  11. if( NOT DEFINED CMAKE_C_STANDARD_REQUIRED )
  12. set( CMAKE_C_STANDARD_REQUIRED ON )
  13. endif()
  14. # Do not allow in-source build.
  15. if( ${PROJECT_SOURCE_DIR} STREQUAL ${PROJECT_BINARY_DIR} )
  16. message( FATAL_ERROR "In-source build is not allowed. Please build in a separate directory, such as ${PROJECT_SOURCE_DIR}/build." )
  17. endif()
  18. # Set global path variables.
  19. get_filename_component(__MODULE_ROOT_DIR "${CMAKE_CURRENT_LIST_DIR}/.." ABSOLUTE)
  20. set(MODULE_ROOT_DIR ${__MODULE_ROOT_DIR} CACHE INTERNAL "coreMQTT repository root.")
  21. # Configure options to always show in CMake GUI.
  22. option( BUILD_CLONE_SUBMODULES
  23. "Set this to ON to automatically clone any required Git submodules. When OFF, submodules must be manually cloned."
  24. OFF )
  25. # Set output directories.
  26. set( CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin )
  27. set( CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib )
  28. set( CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib )
  29. # ===================================== Coverity Analysis Configuration =================================================
  30. # Include filepaths for source and include.
  31. include( ${MODULE_ROOT_DIR}/mqttFilePaths.cmake )
  32. # Target for Coverity analysis that builds the library.
  33. add_library( coverity_analysis
  34. ${MQTT_SOURCES}
  35. ${MQTT_SERIALIZER_SOURCES} )
  36. # Build MQTT library target without custom config dependency.
  37. target_compile_definitions( coverity_analysis PUBLIC MQTT_DO_NOT_USE_CUSTOM_CONFIG=1 )
  38. target_compile_definitions( coverity_analysis PUBLIC NDEBUG=1 )
  39. # MQTT public include path.
  40. target_include_directories( coverity_analysis PUBLIC ${MQTT_INCLUDE_PUBLIC_DIRS} )
  41. # ==================================== Test Configuration ========================================
  42. # Define a CMock resource path.
  43. set( CMOCK_DIR ${MODULE_ROOT_DIR}/test/unit-test/CMock CACHE INTERNAL "CMock library source directory." )
  44. # Include CMock build configuration.
  45. include( unit-test/cmock_build.cmake )
  46. # Check if the CMock source directory exists, and if not present, clone the submodule
  47. # if BUILD_CLONE_SUBMODULES configuration is enabled.
  48. if( NOT EXISTS ${CMOCK_DIR}/src )
  49. # Attempt to clone CMock.
  50. if( ${BUILD_CLONE_SUBMODULES} )
  51. clone_cmock()
  52. else()
  53. message( FATAL_ERROR "The required submodule CMock does not exist. Either clone it manually, or set BUILD_CLONE_SUBMODULES to 1 to automatically clone it during build." )
  54. endif()
  55. endif()
  56. # Add unit test and coverage configuration.
  57. # Use CTest utility for managing test runs. This has to be added BEFORE
  58. # defining test targets with add_test()
  59. enable_testing()
  60. # Add build targets for CMock and Unit, required for unit testing.
  61. add_cmock_targets()
  62. # Add function to enable CMock based tests and coverage.
  63. include( ${MODULE_ROOT_DIR}/tools/cmock/create_test.cmake )
  64. # Include build configuration for unit tests.
  65. add_subdirectory( unit-test )
  66. # ==================================== Coverage Analysis configuration ========================================
  67. # Add a target for running coverage on tests.
  68. add_custom_target( coverage
  69. COMMAND ${CMAKE_COMMAND} -DCMOCK_DIR=${CMOCK_DIR}
  70. -P ${MODULE_ROOT_DIR}/tools/cmock/coverage.cmake
  71. DEPENDS cmock unity core_mqtt_utest core_mqtt_serializer_utest core_mqtt_state_utest
  72. WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
  73. )