From a0befae2d4662a1596c611d5acb749a300fd218c Mon Sep 17 00:00:00 2001 From: Robert Walton Date: Thu, 10 Jun 2021 21:43:58 +0100 Subject: [PATCH] CMake: Only build unit tests if Mbed OS is the current project Typically when adding a unit test directory to a CMake project a check will be used to ensure the subdirectory is added only if the following are true: * The BUILD_TESTING option is set to ON. * The current CMake project is the top-level project. The reason being, if a downstream project includes our project they generally don't want to build our unit tests. In mbed-os, we do correctly specify the above condition before adding the central UNITTEST subdirectory, which fetches googletest and adds the "stub" libraries the unit tests depend on. However, we only check if CMAKE_CROSSCOMPILING is OFF (or undefined) before actually adding the unit tests. This mismatched logic would lead to unexpected build failures in various scenarios. One likely case could be: a downstream project including mbed-os happens to set CMAKE_CROSSCOMPILING to OFF/undefined for any reason (possibly to build its own unit tests). mbed-os would go ahead and attempt to build its tests without fetching googletest or adding the required stub targets. To fix the issue replace the check for CMAKE_CROSSCOMPILING in the unit tests with the same BUILD_TESTING idiom we use for adding the central UNITTESTS subdirectory. --- connectivity/cellular/CMakeLists.txt | 2 +- connectivity/lorawan/CMakeLists.txt | 2 +- connectivity/netsocket/CMakeLists.txt | 2 +- drivers/CMakeLists.txt | 2 +- events/CMakeLists.txt | 3 ++- platform/CMakeLists.txt | 2 +- storage/blockdevice/CMakeLists.txt | 2 +- storage/kvstore/filesystemstore/CMakeLists.txt | 2 +- storage/kvstore/tdbstore/CMakeLists.txt | 2 +- 9 files changed, 10 insertions(+), 9 deletions(-) diff --git a/connectivity/cellular/CMakeLists.txt b/connectivity/cellular/CMakeLists.txt index 8455c59b4c0..d7ce11f90d4 100644 --- a/connectivity/cellular/CMakeLists.txt +++ b/connectivity/cellular/CMakeLists.txt @@ -1,7 +1,7 @@ # Copyright (c) 2020 ARM Limited. All rights reserved. # SPDX-License-Identifier: Apache-2.0 -if(NOT ${CMAKE_CROSSCOMPILING}) +if(CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME AND BUILD_TESTING) add_subdirectory(tests/UNITTESTS) endif() diff --git a/connectivity/lorawan/CMakeLists.txt b/connectivity/lorawan/CMakeLists.txt index 552a72a6fee..941b01e04e5 100644 --- a/connectivity/lorawan/CMakeLists.txt +++ b/connectivity/lorawan/CMakeLists.txt @@ -1,7 +1,7 @@ # Copyright (c) 2020 ARM Limited. All rights reserved. # SPDX-License-Identifier: Apache-2.0 -if(NOT ${CMAKE_CROSSCOMPILING}) +if(CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME AND BUILD_TESTING) add_subdirectory(tests/UNITTESTS) endif() diff --git a/connectivity/netsocket/CMakeLists.txt b/connectivity/netsocket/CMakeLists.txt index 0ce8a2f466e..62bea8cc636 100644 --- a/connectivity/netsocket/CMakeLists.txt +++ b/connectivity/netsocket/CMakeLists.txt @@ -1,7 +1,7 @@ # Copyright (c) 2020 ARM Limited. All rights reserved. # SPDX-License-Identifier: Apache-2.0 -if(NOT ${CMAKE_CROSSCOMPILING}) +if(CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME AND BUILD_TESTING) add_subdirectory(tests/UNITTESTS) endif() diff --git a/drivers/CMakeLists.txt b/drivers/CMakeLists.txt index 4d26ac6dec1..9079d3d75ac 100644 --- a/drivers/CMakeLists.txt +++ b/drivers/CMakeLists.txt @@ -1,7 +1,7 @@ # Copyright (c) 2020 ARM Limited. All rights reserved. # SPDX-License-Identifier: Apache-2.0 -if(NOT ${CMAKE_CROSSCOMPILING}) +if(CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME AND BUILD_TESTING) add_subdirectory(tests/UNITTESTS) endif() diff --git a/events/CMakeLists.txt b/events/CMakeLists.txt index 1cda3156d39..da4d6935112 100644 --- a/events/CMakeLists.txt +++ b/events/CMakeLists.txt @@ -1,9 +1,10 @@ # Copyright (c) 2020 ARM Limited. All rights reserved. # SPDX-License-Identifier: Apache-2.0 -if(NOT ${CMAKE_CROSSCOMPILING}) +if(CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME AND BUILD_TESTING) add_subdirectory(tests/UNITTESTS) else() + add_library(mbed-events INTERFACE) target_include_directories(mbed-events diff --git a/platform/CMakeLists.txt b/platform/CMakeLists.txt index b79a4a28cbe..df83719fc69 100644 --- a/platform/CMakeLists.txt +++ b/platform/CMakeLists.txt @@ -1,7 +1,7 @@ # Copyright (c) 2020 ARM Limited. All rights reserved. # SPDX-License-Identifier: Apache-2.0 -if(NOT ${CMAKE_CROSSCOMPILING}) +if(CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME AND BUILD_TESTING) add_subdirectory(tests/UNITTESTS) endif() diff --git a/storage/blockdevice/CMakeLists.txt b/storage/blockdevice/CMakeLists.txt index cbbb76f0aae..9f847d43604 100644 --- a/storage/blockdevice/CMakeLists.txt +++ b/storage/blockdevice/CMakeLists.txt @@ -1,7 +1,7 @@ # Copyright (c) 2020 ARM Limited. All rights reserved. # SPDX-License-Identifier: Apache-2.0 -if(NOT ${CMAKE_CROSSCOMPILING}) +if(CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME AND BUILD_TESTING) add_subdirectory(tests/UNITTESTS) endif() diff --git a/storage/kvstore/filesystemstore/CMakeLists.txt b/storage/kvstore/filesystemstore/CMakeLists.txt index aeb8aef1912..7289fe8b8df 100644 --- a/storage/kvstore/filesystemstore/CMakeLists.txt +++ b/storage/kvstore/filesystemstore/CMakeLists.txt @@ -1,7 +1,7 @@ # Copyright (c) 2020 ARM Limited. All rights reserved. # SPDX-License-Identifier: Apache-2.0 -if(NOT ${CMAKE_CROSSCOMPILING}) +if(CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME AND BUILD_TESTING) add_subdirectory(tests/UNITTESTS) endif() diff --git a/storage/kvstore/tdbstore/CMakeLists.txt b/storage/kvstore/tdbstore/CMakeLists.txt index a1cb048ad93..d4f8d92ed59 100644 --- a/storage/kvstore/tdbstore/CMakeLists.txt +++ b/storage/kvstore/tdbstore/CMakeLists.txt @@ -1,7 +1,7 @@ # Copyright (c) 2020 ARM Limited. All rights reserved. # SPDX-License-Identifier: Apache-2.0 -if(NOT ${CMAKE_CROSSCOMPILING}) +if(CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME AND BUILD_TESTING) add_subdirectory(tests/UNITTESTS) endif()