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