Library for MQTT

Committer:
pavleradojkovic
Date:
Mon Jun 20 16:24:43 2022 +0000
Revision:
0:ba7e439238ab
Inital commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
pavleradojkovic 0:ba7e439238ab 1 cmake_minimum_required(VERSION 3.0.2)
pavleradojkovic 0:ba7e439238ab 2
pavleradojkovic 0:ba7e439238ab 3 set(PROJECT_NAME unittests)
pavleradojkovic 0:ba7e439238ab 4 set(LIB_NAME MbedOS)
pavleradojkovic 0:ba7e439238ab 5
pavleradojkovic 0:ba7e439238ab 6 project(${PROJECT_NAME} CXX C)
pavleradojkovic 0:ba7e439238ab 7
pavleradojkovic 0:ba7e439238ab 8 # Setup c++ standard
pavleradojkovic 0:ba7e439238ab 9 macro(use_cxx14)
pavleradojkovic 0:ba7e439238ab 10 if (CMAKE_VERSION VERSION_LESS 3.1)
pavleradojkovic 0:ba7e439238ab 11 if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
pavleradojkovic 0:ba7e439238ab 12 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=gnu++14")
pavleradojkovic 0:ba7e439238ab 13 endif()
pavleradojkovic 0:ba7e439238ab 14 else()
pavleradojkovic 0:ba7e439238ab 15 set(CMAKE_CXX_STANDARD 14)
pavleradojkovic 0:ba7e439238ab 16 set(CMAKE_CXX_STANDARD_REQUIRED ON)
pavleradojkovic 0:ba7e439238ab 17 endif()
pavleradojkovic 0:ba7e439238ab 18 endmacro()
pavleradojkovic 0:ba7e439238ab 19
pavleradojkovic 0:ba7e439238ab 20 use_cxx14()
pavleradojkovic 0:ba7e439238ab 21
pavleradojkovic 0:ba7e439238ab 22 ####################
pavleradojkovic 0:ba7e439238ab 23 # GTEST
pavleradojkovic 0:ba7e439238ab 24 ####################
pavleradojkovic 0:ba7e439238ab 25
pavleradojkovic 0:ba7e439238ab 26 # Download and unpack googletest at configure time
pavleradojkovic 0:ba7e439238ab 27 configure_file(googletest-CMakeLists.txt.in googletest-download/CMakeLists.txt)
pavleradojkovic 0:ba7e439238ab 28 execute_process(COMMAND ${CMAKE_COMMAND} -G "${CMAKE_GENERATOR}" .
pavleradojkovic 0:ba7e439238ab 29 RESULT_VARIABLE result
pavleradojkovic 0:ba7e439238ab 30 WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/googletest-download)
pavleradojkovic 0:ba7e439238ab 31 if (result)
pavleradojkovic 0:ba7e439238ab 32 message(FATAL_ERROR "CMake failed for google test: ${result}")
pavleradojkovic 0:ba7e439238ab 33 endif()
pavleradojkovic 0:ba7e439238ab 34 execute_process(COMMAND ${CMAKE_COMMAND} --build .
pavleradojkovic 0:ba7e439238ab 35 RESULT_VARIABLE result
pavleradojkovic 0:ba7e439238ab 36 WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/googletest-download)
pavleradojkovic 0:ba7e439238ab 37 if (result)
pavleradojkovic 0:ba7e439238ab 38 message(FATAL_ERROR "Build failed for google test: ${result}")
pavleradojkovic 0:ba7e439238ab 39 endif()
pavleradojkovic 0:ba7e439238ab 40
pavleradojkovic 0:ba7e439238ab 41 # Prevent overriding the parent project's compiler/linker
pavleradojkovic 0:ba7e439238ab 42 # settings on Windows
pavleradojkovic 0:ba7e439238ab 43 set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
pavleradojkovic 0:ba7e439238ab 44
pavleradojkovic 0:ba7e439238ab 45 # Add googletest directly to our build. This defines
pavleradojkovic 0:ba7e439238ab 46 # the gtest and gtest_main targets.
pavleradojkovic 0:ba7e439238ab 47 add_subdirectory(${CMAKE_BINARY_DIR}/googletest-src
pavleradojkovic 0:ba7e439238ab 48 ${CMAKE_BINARY_DIR}/googletest-build
pavleradojkovic 0:ba7e439238ab 49 EXCLUDE_FROM_ALL)
pavleradojkovic 0:ba7e439238ab 50
pavleradojkovic 0:ba7e439238ab 51 # The gtest/gtest_main/gmock/gmock_main targets carry header search path
pavleradojkovic 0:ba7e439238ab 52 # dependencies automatically when using CMake 2.8.11 or
pavleradojkovic 0:ba7e439238ab 53 # later.
pavleradojkovic 0:ba7e439238ab 54 target_include_directories(gmock_main SYSTEM BEFORE INTERFACE
pavleradojkovic 0:ba7e439238ab 55 "$<BUILD_INTERFACE:${gtest_SOURCE_DIR}/include>"
pavleradojkovic 0:ba7e439238ab 56 "$<BUILD_INTERFACE:${gmock_SOURCE_DIR}/include>")
pavleradojkovic 0:ba7e439238ab 57
pavleradojkovic 0:ba7e439238ab 58 ####################
pavleradojkovic 0:ba7e439238ab 59 # TESTING
pavleradojkovic 0:ba7e439238ab 60 ####################
pavleradojkovic 0:ba7e439238ab 61
pavleradojkovic 0:ba7e439238ab 62 include(CTest)
pavleradojkovic 0:ba7e439238ab 63
pavleradojkovic 0:ba7e439238ab 64 set_property(DIRECTORY APPEND PROPERTY ADDITIONAL_MAKE_CLEAN_FILES
pavleradojkovic 0:ba7e439238ab 65 "${CMAKE_BINARY_DIR}/Testing"
pavleradojkovic 0:ba7e439238ab 66 )
pavleradojkovic 0:ba7e439238ab 67
pavleradojkovic 0:ba7e439238ab 68 ####################
pavleradojkovic 0:ba7e439238ab 69 # CODE COVERAGE SETUP
pavleradojkovic 0:ba7e439238ab 70 ####################
pavleradojkovic 0:ba7e439238ab 71
pavleradojkovic 0:ba7e439238ab 72 if (COVERAGE)
pavleradojkovic 0:ba7e439238ab 73
pavleradojkovic 0:ba7e439238ab 74 if (NOT CMAKE_BUILD_TYPE STREQUAL "Debug")
pavleradojkovic 0:ba7e439238ab 75 message(WARNING "Non-debug build may result misleading code coverage results.")
pavleradojkovic 0:ba7e439238ab 76 endif()
pavleradojkovic 0:ba7e439238ab 77
pavleradojkovic 0:ba7e439238ab 78 # Append coverage compiler flags
pavleradojkovic 0:ba7e439238ab 79 set(COVERAGE_COMPILER_FLAGS "-g -O0 --coverage -fprofile-arcs -ftest-coverage")
pavleradojkovic 0:ba7e439238ab 80 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${COVERAGE_COMPILER_FLAGS}")
pavleradojkovic 0:ba7e439238ab 81 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${COVERAGE_COMPILER_FLAGS}")
pavleradojkovic 0:ba7e439238ab 82
pavleradojkovic 0:ba7e439238ab 83 endif(COVERAGE)
pavleradojkovic 0:ba7e439238ab 84
pavleradojkovic 0:ba7e439238ab 85 ####################
pavleradojkovic 0:ba7e439238ab 86 # UNIT TESTS
pavleradojkovic 0:ba7e439238ab 87 ####################
pavleradojkovic 0:ba7e439238ab 88
pavleradojkovic 0:ba7e439238ab 89 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DUNITTEST")
pavleradojkovic 0:ba7e439238ab 90 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DUNITTEST")
pavleradojkovic 0:ba7e439238ab 91
pavleradojkovic 0:ba7e439238ab 92 # Set include dirs.
pavleradojkovic 0:ba7e439238ab 93 set(unittest-includes-base
pavleradojkovic 0:ba7e439238ab 94 "${PROJECT_SOURCE_DIR}/../paho_mqtt_embedded_c/MQTTClient/src"
pavleradojkovic 0:ba7e439238ab 95 "${PROJECT_SOURCE_DIR}/../paho_mqtt_embedded_c/MQTTPacket/src"
pavleradojkovic 0:ba7e439238ab 96 "${PROJECT_SOURCE_DIR}/stubs"
pavleradojkovic 0:ba7e439238ab 97 )
pavleradojkovic 0:ba7e439238ab 98
pavleradojkovic 0:ba7e439238ab 99 # Create a list for test suites.
pavleradojkovic 0:ba7e439238ab 100 set(TEST_SUITES)
pavleradojkovic 0:ba7e439238ab 101
pavleradojkovic 0:ba7e439238ab 102 # Get all matched tests.
pavleradojkovic 0:ba7e439238ab 103 file(GLOB_RECURSE unittest-file-list
pavleradojkovic 0:ba7e439238ab 104 "unittest.cmake"
pavleradojkovic 0:ba7e439238ab 105 )
pavleradojkovic 0:ba7e439238ab 106
pavleradojkovic 0:ba7e439238ab 107 if ("${unittest-file-list}" STREQUAL "")
pavleradojkovic 0:ba7e439238ab 108 message(FATAL_ERROR "No tests found. Exiting...")
pavleradojkovic 0:ba7e439238ab 109 endif()
pavleradojkovic 0:ba7e439238ab 110
pavleradojkovic 0:ba7e439238ab 111 # Create unit test targets
pavleradojkovic 0:ba7e439238ab 112 foreach(testfile ${unittest-file-list})
pavleradojkovic 0:ba7e439238ab 113 ####################
pavleradojkovic 0:ba7e439238ab 114 # DEFINE TARGETS
pavleradojkovic 0:ba7e439238ab 115 ####################
pavleradojkovic 0:ba7e439238ab 116
pavleradojkovic 0:ba7e439238ab 117 # Init file lists.
pavleradojkovic 0:ba7e439238ab 118 set(unittest-includes ${unittest-includes-base})
pavleradojkovic 0:ba7e439238ab 119 set(unittest-sources)
pavleradojkovic 0:ba7e439238ab 120 set(unittest-test-sources)
pavleradojkovic 0:ba7e439238ab 121
pavleradojkovic 0:ba7e439238ab 122 # Get source files
pavleradojkovic 0:ba7e439238ab 123 include("${testfile}")
pavleradojkovic 0:ba7e439238ab 124
pavleradojkovic 0:ba7e439238ab 125 get_filename_component(TEST_SUITE_DIR ${testfile} DIRECTORY)
pavleradojkovic 0:ba7e439238ab 126
pavleradojkovic 0:ba7e439238ab 127 file(RELATIVE_PATH
pavleradojkovic 0:ba7e439238ab 128 TEST_SUITE_NAME # output
pavleradojkovic 0:ba7e439238ab 129 ${PROJECT_SOURCE_DIR} # root
pavleradojkovic 0:ba7e439238ab 130 ${TEST_SUITE_DIR} #abs dirpath
pavleradojkovic 0:ba7e439238ab 131 )
pavleradojkovic 0:ba7e439238ab 132
pavleradojkovic 0:ba7e439238ab 133 string(REGEX REPLACE "/|\\\\" "-" TEST_SUITE_NAME ${TEST_SUITE_NAME})
pavleradojkovic 0:ba7e439238ab 134
pavleradojkovic 0:ba7e439238ab 135 set(TEST_SUITES ${TEST_SUITES} ${TEST_SUITE_NAME})
pavleradojkovic 0:ba7e439238ab 136
pavleradojkovic 0:ba7e439238ab 137 set(LIBS_TO_BE_LINKED gmock_main)
pavleradojkovic 0:ba7e439238ab 138
pavleradojkovic 0:ba7e439238ab 139 # Build directories list
pavleradojkovic 0:ba7e439238ab 140 set(BUILD_DIRECTORIES)
pavleradojkovic 0:ba7e439238ab 141
pavleradojkovic 0:ba7e439238ab 142 if (unittest-sources)
pavleradojkovic 0:ba7e439238ab 143 # Create the testable static library.
pavleradojkovic 0:ba7e439238ab 144 add_library("${TEST_SUITE_NAME}.${LIB_NAME}" STATIC ${unittest-sources})
pavleradojkovic 0:ba7e439238ab 145 target_include_directories("${TEST_SUITE_NAME}.${LIB_NAME}" PRIVATE
pavleradojkovic 0:ba7e439238ab 146 ${unittest-includes})
pavleradojkovic 0:ba7e439238ab 147 set(LIBS_TO_BE_LINKED ${LIBS_TO_BE_LINKED} "${TEST_SUITE_NAME}.${LIB_NAME}")
pavleradojkovic 0:ba7e439238ab 148
pavleradojkovic 0:ba7e439238ab 149 # Append lib build directory to list
pavleradojkovic 0:ba7e439238ab 150 list(APPEND BUILD_DIRECTORIES "./CMakeFiles/${TEST_SUITE_NAME}.${LIB_NAME}.dir")
pavleradojkovic 0:ba7e439238ab 151 endif(unittest-sources)
pavleradojkovic 0:ba7e439238ab 152
pavleradojkovic 0:ba7e439238ab 153 if (unittest-test-sources)
pavleradojkovic 0:ba7e439238ab 154 # Create the executable.
pavleradojkovic 0:ba7e439238ab 155 add_executable(${TEST_SUITE_NAME} ${unittest-test-sources})
pavleradojkovic 0:ba7e439238ab 156 target_include_directories(${TEST_SUITE_NAME} PRIVATE
pavleradojkovic 0:ba7e439238ab 157 ${unittest-includes})
pavleradojkovic 0:ba7e439238ab 158
pavleradojkovic 0:ba7e439238ab 159 # Link the executable with the libraries.
pavleradojkovic 0:ba7e439238ab 160 target_link_libraries(${TEST_SUITE_NAME} ${LIBS_TO_BE_LINKED})
pavleradojkovic 0:ba7e439238ab 161
pavleradojkovic 0:ba7e439238ab 162 add_test(NAME "${TEST_SUITE_NAME}" COMMAND ${TEST_SUITE_NAME})
pavleradojkovic 0:ba7e439238ab 163
pavleradojkovic 0:ba7e439238ab 164 # Append test build directory to list
pavleradojkovic 0:ba7e439238ab 165 list(APPEND BUILD_DIRECTORIES "./CMakeFiles/${TEST_SUITE_NAME}.dir")
pavleradojkovic 0:ba7e439238ab 166 else()
pavleradojkovic 0:ba7e439238ab 167 message(WARNING "No test source files found for ${TEST_SUITE_NAME}.\n")
pavleradojkovic 0:ba7e439238ab 168 endif(unittest-test-sources)
pavleradojkovic 0:ba7e439238ab 169
pavleradojkovic 0:ba7e439238ab 170 set_target_properties("${TEST_SUITE_NAME}.${LIB_NAME}" PROPERTIES LINKER_LANGUAGE CXX)
pavleradojkovic 0:ba7e439238ab 171 endforeach(testfile)
pavleradojkovic 0:ba7e439238ab 172