CMake: Update mbed os, unittest CMake and add CMAKE_CROSSCOMPILING guard

- Add a new MbedOS project in mbed os root CMake which can be used along with
  BUILD_TESTING conditional check for enabling the unittest build
- Update UNITTEST CMake for setting the CMake configuration like c, cxx flags etc.,
- Add if CMAKE_CROSSCOMPILING conditional check wherever target configuration check
  and toolchain configuration to avoid such configuration gets included for unittest build.
pull/14426/head
Rajkumar Kanagaraj 2021-04-07 07:56:38 -07:00
parent 4ade0bdbea
commit a88f43f367
6 changed files with 193 additions and 345 deletions

2
.gitignore vendored
View File

@ -102,3 +102,5 @@ DELIVERY/
CMakeCache.txt
cmake_install.cmake
CMakeFiles/
cmake_build/
Testing/

View File

@ -5,8 +5,10 @@
cmake_minimum_required(VERSION 3.19.0 FATAL_ERROR)
if(${CMAKE_CROSSCOMPILING})
include(${MBED_CONFIG_PATH}/mbed_config.cmake)
include(mbed_set_linker_script)
endif()
project(mbed-os)
@ -15,6 +17,18 @@ list(APPEND CMAKE_MODULE_PATH
"${mbed-os_SOURCE_DIR}/platform/FEATURE_EXPERIMENTAL_API/FEATURE_PSA/TARGET_TFM/TARGET_TFM_LATEST/scripts;${mbed-os_SOURCE_DIR}/targets/TARGET_Cypress/scripts;${mbed-os_SOURCE_DIR}/targets/TARGET_NXP/scripts"
)
option(BUILD_TESTING "Run unit tests only." OFF)
if(CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME AND BUILD_TESTING)
include(CTest)
add_subdirectory(UNITTESTS)
# Add MBED_TEST_MODE for backward compatibility with Greentea tests written for use with Mbed CLI 1
target_compile_definitions(${PROJECT_NAME}
PUBLIC
MBED_TEST_MODE
)
endif()
add_library(mbed-core INTERFACE)
add_library(mbed-os INTERFACE)
@ -33,6 +47,7 @@ target_link_libraries(mbed-baremetal
)
# Validate selected C library type
# The C library type selected has to match the library that the target can support
if(${CMAKE_CROSSCOMPILING})
if(${MBED_C_LIB} STREQUAL "small")
if(NOT "small" IN_LIST MBED_TARGET_SUPPORTED_C_LIBS)
if("std" IN_LIST MBED_TARGET_SUPPORTED_C_LIBS)
@ -61,6 +76,7 @@ endif()
mbed_set_cpu_core_definitions(mbed-core)
if(${MBED_TOOLCHAIN_FILE_USED})
message(STATUS ${MBED_TOOLCHAIN})
mbed_set_profile_options(mbed-core ${MBED_TOOLCHAIN})
mbed_set_c_lib(mbed-core ${MBED_C_LIB})
mbed_set_printf_lib(mbed-core ${MBED_PRINTF_LIB})
@ -79,14 +95,6 @@ target_compile_definitions(mbed-core
${MBED_CONFIG_DEFINITIONS}
)
# Add MBED_TEST_MODE for backward compatibility with Greentea tests written for use with Mbed CLI 1
if(CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME AND BUILD_TESTING)
target_compile_definitions(${PROJECT_NAME}
PUBLIC
MBED_TEST_MODE
)
endif()
# We need to generate a "response file" to pass to the C preprocessor when we preprocess the linker
# script, because of path le ngth limitations on Windows. We set the response file and bind the path
# to a global property here. The MBED_TARGET being built queries this global property when it sets
@ -116,6 +124,7 @@ elseif(${MBED_TOOLCHAIN} STREQUAL "ARM")
TOOLCHAIN_ARM
)
endif()
endif()
# Include mbed.h and config from generate folder
target_include_directories(mbed-core
@ -135,24 +144,35 @@ add_subdirectory(drivers)
add_subdirectory(hal)
add_subdirectory(platform)
add_subdirectory(rtos)
add_subdirectory(targets)
if(${CMAKE_CROSSCOMPILING})
add_subdirectory(targets)
# The directories below contain optional target libraries
add_subdirectory(events EXCLUDE_FROM_ALL)
add_subdirectory(connectivity EXCLUDE_FROM_ALL)
add_subdirectory(storage EXCLUDE_FROM_ALL)
add_subdirectory(events EXCLUDE_FROM_ALL)
else()
add_subdirectory(connectivity)
add_subdirectory(storage)
add_subdirectory(events)
endif()
# The directories below contain optional target libraries
add_subdirectory(drivers/device_key EXCLUDE_FROM_ALL)
add_subdirectory(drivers/usb EXCLUDE_FROM_ALL)
add_subdirectory(features EXCLUDE_FROM_ALL)
add_subdirectory(cmsis/CMSIS_5/CMSIS/RTOS2 EXCLUDE_FROM_ALL)
add_subdirectory(cmsis/device/rtos EXCLUDE_FROM_ALL)
if(${CMAKE_CROSSCOMPILING})
# Ensure the words that make up the Mbed target name are separated with a hyphen, lowercase, and with the `mbed-` prefix.
string(TOLOWER ${MBED_TARGET} MBED_TARGET_CONVERTED)
string(REPLACE "_" "-" MBED_TARGET_CONVERTED ${MBED_TARGET_CONVERTED})
string(PREPEND MBED_TARGET_CONVERTED "mbed-")
target_link_libraries(mbed-core INTERFACE ${MBED_TARGET_CONVERTED})
endif()
#
# Converts output file of `target` to binary file and to Intel HEX file.

View File

@ -1,73 +1,37 @@
cmake_minimum_required(VERSION 3.0.2)
set(PROJECT_NAME unittests)
set(LIB_NAME MbedOS)
project(${PROJECT_NAME})
# Copyright (c) 2021 ARM Limited. All rights reserved.
# SPDX-License-Identifier: Apache-2.0
# Setup c++ standard
macro(use_cxx14)
if (CMAKE_VERSION VERSION_LESS 3.1)
if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=gnu++14")
endif()
else()
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
endif()
endmacro()
use_cxx14()
set(CMAKE_CXX_STANDARD 14 CACHE STRING "")
set(CMAKE_CXX_STANDARD_REQUIRED ON CACHE BOOL "")
add_definitions(-DUNITTEST)
if (MINGW)
# enable PRIx formatting globally
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -D__STDC_FORMAT_MACROS")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -D__STDC_FORMAT_MACROS")
add_definitions(-D__STDC_FORMAT_MACROS)
endif (MINGW)
####################
# GTEST
####################
# Download and unpack googletest at configure time
configure_file(googletest-CMakeLists.txt.in googletest-download/CMakeLists.txt)
execute_process(COMMAND ${CMAKE_COMMAND} -G "${CMAKE_GENERATOR}" .
RESULT_VARIABLE result
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/googletest-download)
if (result)
message(FATAL_ERROR "CMake failed for google test: ${result}")
endif()
execute_process(COMMAND ${CMAKE_COMMAND} --build .
RESULT_VARIABLE result
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/googletest-download)
if (result)
message(FATAL_ERROR "Build failed for google test: ${result}")
endif()
include(FetchContent)
# Download and unpack googletest
FetchContent_Declare(googletest
GIT_REPOSITORY https://github.com/google/googletest.git
GIT_TAG release-1.10.0
)
FetchContent_MakeAvailable(googletest)
# Prevent overriding the parent project's compiler/linker
# settings on Windows
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
# Add googletest directly to our build. This defines
# the gtest and gtest_main targets.
add_subdirectory(${CMAKE_BINARY_DIR}/googletest-src
${CMAKE_BINARY_DIR}/googletest-build
EXCLUDE_FROM_ALL)
# The gtest/gtest_main/gmock/gmock_main targets carry header search path
# dependencies automatically when using CMake 2.8.11 or
# later.
target_include_directories(gmock_main SYSTEM BEFORE INTERFACE
"$<BUILD_INTERFACE:${gtest_SOURCE_DIR}/include>"
"$<BUILD_INTERFACE:${gmock_SOURCE_DIR}/include>")
####################
# TESTING
####################
include(CTest)
set_property(DIRECTORY APPEND PROPERTY ADDITIONAL_MAKE_CLEAN_FILES
"${CMAKE_BINARY_DIR}/Testing"
)
@ -84,8 +48,8 @@ if (COVERAGE)
# Append coverage compiler flags
set(COVERAGE_COMPILER_FLAGS "-g -O0 --coverage")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${COVERAGE_COMPILER_FLAGS}")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${COVERAGE_COMPILER_FLAGS}")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${COVERAGE_COMPILER_FLAGS}" CACHE STRING "" FORCE)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${COVERAGE_COMPILER_FLAGS}" CACHE STRING "" FORCE)
endif(COVERAGE)
@ -93,148 +57,4 @@ if (VALGRIND)
find_program(MEMORYCHECK_COMMAND valgrind)
endif(VALGRIND)
####################
# UNIT TESTS
####################
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DUNITTEST")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DUNITTEST")
# Set include dirs.
set(unittest-includes-base
"${PROJECT_SOURCE_DIR}/target_h"
"${PROJECT_SOURCE_DIR}/../events/tests/UNITTESTS/target_h"
"${PROJECT_SOURCE_DIR}/../events/tests/UNITTESTS/target_h/equeue"
"${PROJECT_SOURCE_DIR}/target_h/platform"
"${PROJECT_SOURCE_DIR}/target_h/platform/cxxsupport"
"${PROJECT_SOURCE_DIR}/target_h/drivers"
"${PROJECT_SOURCE_DIR}/target_h/rtos/include"
"${PROJECT_SOURCE_DIR}/stubs"
"${PROJECT_SOURCE_DIR}/.."
"${PROJECT_SOURCE_DIR}/../features"
"${PROJECT_SOURCE_DIR}/../platform/include"
"${PROJECT_SOURCE_DIR}/../platform/include/platform"
"${PROJECT_SOURCE_DIR}/../platform/mbed-trace/include"
"${PROJECT_SOURCE_DIR}/../storage/filesystem/littlefs/include"
"${PROJECT_SOURCE_DIR}/../storage/filesystem/fat/include"
"${PROJECT_SOURCE_DIR}/../storage/blockdevice/include"
"${PROJECT_SOURCE_DIR}/../storage/filesystem/include"
"${PROJECT_SOURCE_DIR}/../storage/kvstore/include"
"${PROJECT_SOURCE_DIR}/../storage/kvstore/kv_config"
"${PROJECT_SOURCE_DIR}/../storage/kvstore/kv_config/include"
"${PROJECT_SOURCE_DIR}/../storage/kvstore/tdbstore/include"
"${PROJECT_SOURCE_DIR}/../storage/kvstore/filesystemstore/include"
"${PROJECT_SOURCE_DIR}/../storage/kvstore/kvstore_global_api/include"
"${PROJECT_SOURCE_DIR}/../drivers"
"${PROJECT_SOURCE_DIR}/../drivers/include"
"${PROJECT_SOURCE_DIR}/../drivers/include/drivers"
"${PROJECT_SOURCE_DIR}/../drivers/include/drivers/internal"
"${PROJECT_SOURCE_DIR}/../hal"
"${PROJECT_SOURCE_DIR}/../hal/include"
"${PROJECT_SOURCE_DIR}/../events/include"
"${PROJECT_SOURCE_DIR}/../events/include/events/internal"
"${PROJECT_SOURCE_DIR}/../events/source"
"${PROJECT_SOURCE_DIR}/../rtos/include"
"${PROJECT_SOURCE_DIR}/../features/frameworks"
"${PROJECT_SOURCE_DIR}/../connectivity/libraries/nanostack-libservice"
"${PROJECT_SOURCE_DIR}/../connectivity/libraries/nanostack-libservice/mbed-client-libservice"
"${PROJECT_SOURCE_DIR}/../connectivity/netsocket/include"
"${PROJECT_SOURCE_DIR}/../features/filesystem/fat"
"${PROJECT_SOURCE_DIR}/../features/filesystem/fat/ChaN"
"${PROJECT_SOURCE_DIR}/../features/filesystem/bd"
"${PROJECT_SOURCE_DIR}/../features/filesystem/"
"${PROJECT_SOURCE_DIR}/../features/filesystem/littlefs"
"${PROJECT_SOURCE_DIR}/../features/filesystem/littlefs/littlefs"
"${PROJECT_SOURCE_DIR}/../connectivity/cellular/include/cellular/framework/API"
"${PROJECT_SOURCE_DIR}/../connectivity/cellular/include/cellular/framework/AT"
"${PROJECT_SOURCE_DIR}/../connectivity/cellular/include/cellular/framework/device"
"${PROJECT_SOURCE_DIR}/../connectivity/cellular/include/cellular/framework"
"${PROJECT_SOURCE_DIR}/../connectivity/cellular/include/cellular/framework/common"
"${PROJECT_SOURCE_DIR}/../connectivity"
"${PROJECT_SOURCE_DIR}/../connectivity/lorawan/include/lorawan"
"${PROJECT_SOURCE_DIR}/../connectivity/lorawan/lorastack"
"${PROJECT_SOURCE_DIR}/../connectivity/lorawan/lorastack/mac"
"${PROJECT_SOURCE_DIR}/../connectivity/lorawan/lorastack/phy"
"${PROJECT_SOURCE_DIR}/../connectivity/lorawan"
"${PROJECT_SOURCE_DIR}/../connectivity/mbedtls"
"${PROJECT_SOURCE_DIR}/../connectivity/mbedtls/include"
)
# Create a list for test suites.
set(TEST_SUITES)
# Get all matched tests.
file(GLOB_RECURSE unittest-file-list
"../unittest.cmake" # matches any ../**/unittest.cmake
)
if ("${unittest-file-list}" STREQUAL "")
message(FATAL_ERROR "No tests found. Exiting...")
endif()
# Create unit test targets
foreach(testfile ${unittest-file-list})
####################
# DEFINE TARGETS
####################
# Init file lists.
set(unittest-includes ${unittest-includes-base})
set(unittest-sources)
set(unittest-test-sources)
set(unittest-test-flags)
# Get source files
include("${testfile}")
get_filename_component(TEST_SUITE_DIR ${testfile} DIRECTORY)
file(RELATIVE_PATH
TEST_SUITE_NAME # output
"${PROJECT_SOURCE_DIR}/.." # root
${TEST_SUITE_DIR} #abs dirpath
)
string(REGEX REPLACE "/|\\\\" "-" TEST_SUITE_NAME ${TEST_SUITE_NAME})
set(TEST_SUITES ${TEST_SUITES} ${TEST_SUITE_NAME})
set(LIBS_TO_BE_LINKED gmock_main)
# Build directories list
set(BUILD_DIRECTORIES)
if (unittest-sources)
# Create the testable static library.
add_library("${TEST_SUITE_NAME}.${LIB_NAME}" STATIC ${unittest-sources})
target_include_directories("${TEST_SUITE_NAME}.${LIB_NAME}" PRIVATE
${unittest-includes})
target_compile_options("${TEST_SUITE_NAME}.${LIB_NAME}" PRIVATE
${unittest-test-flags})
set(LIBS_TO_BE_LINKED ${LIBS_TO_BE_LINKED} "${TEST_SUITE_NAME}.${LIB_NAME}")
# Append lib build directory to list
list(APPEND BUILD_DIRECTORIES "./CMakeFiles/${TEST_SUITE_NAME}.${LIB_NAME}.dir")
endif(unittest-sources)
if (unittest-test-sources)
# Create the executable.
add_executable(${TEST_SUITE_NAME} ${unittest-test-sources})
target_include_directories(${TEST_SUITE_NAME} PRIVATE
${unittest-includes})
target_compile_options(${TEST_SUITE_NAME} PRIVATE
${unittest-test-flags})
# Link the executable with the libraries.
target_link_libraries(${TEST_SUITE_NAME} ${LIBS_TO_BE_LINKED})
add_test(NAME "${TEST_SUITE_NAME}" COMMAND ${TEST_SUITE_NAME})
# Append test build directory to list
list(APPEND BUILD_DIRECTORIES "./CMakeFiles/${TEST_SUITE_NAME}.dir")
else()
message(WARNING "No test source files found for ${TEST_SUITE_NAME}.\n")
endif(unittest-test-sources)
endforeach(testfile)
add_subdirectory(stubs)

View File

@ -1,6 +1,7 @@
# Copyright (c) 2020-2021 ARM Limited. All rights reserved.
# SPDX-License-Identifier: Apache-2.0
if(${CMAKE_CROSSCOMPILING})
function(_mbed_get_cortex_m_exception_handlers toolchain_dir)
foreach(key ${MBED_TARGET_LABELS})
if(${key} STREQUAL CORTEX_A)
@ -43,6 +44,7 @@ if(${MBED_TOOLCHAIN} STREQUAL "GCC_ARM")
elseif(${MBED_TOOLCHAIN} STREQUAL "ARM")
_mbed_get_cortex_m_exception_handlers(TOOLCHAIN_ARM)
endif()
endif()
target_include_directories(mbed-rtos
INTERFACE

View File

@ -1,6 +1,7 @@
# Copyright (c) 2020-2021 ARM Limited. All rights reserved.
# SPDX-License-Identifier: Apache-2.0
if(${CMAKE_CROSSCOMPILING})
if(${MBED_TOOLCHAIN} STREQUAL "GCC_ARM")
target_sources(mbed-rtos
INTERFACE
@ -12,6 +13,7 @@ elseif(${MBED_TOOLCHAIN} STREQUAL "ARM")
TOOLCHAIN_ARM_STD/mbed_boot_arm_std.c
)
endif()
endif()
target_include_directories(mbed-rtos
INTERFACE

View File

@ -3,6 +3,7 @@
add_library(mbed-wiced INTERFACE)
if(${CMAKE_CROSSCOMPILING})
if(${MBED_TOOLCHAIN} STREQUAL "ARM")
set(LIB_WICED_DRIVERS TOOLCHAIN_ARMC6/TARGET_WIO_EMW3166/libwiced_drivers.ar)
elseif(${MBED_TOOLCHAIN} STREQUAL "GCC_ARM")
@ -10,6 +11,7 @@ elseif(${MBED_TOOLCHAIN} STREQUAL "GCC_ARM")
endif()
target_link_libraries(mbed-wiced INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}/${LIB_WICED_DRIVERS})
endif()
target_include_directories(mbed-wiced
INTERFACE