Skip to content

Commit a0befae

Browse files
committed
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.
1 parent d4f0181 commit a0befae

File tree

9 files changed

+10
-9
lines changed

9 files changed

+10
-9
lines changed

connectivity/cellular/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Copyright (c) 2020 ARM Limited. All rights reserved.
22
# SPDX-License-Identifier: Apache-2.0
33

4-
if(NOT ${CMAKE_CROSSCOMPILING})
4+
if(CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME AND BUILD_TESTING)
55
add_subdirectory(tests/UNITTESTS)
66
endif()
77

connectivity/lorawan/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Copyright (c) 2020 ARM Limited. All rights reserved.
22
# SPDX-License-Identifier: Apache-2.0
33

4-
if(NOT ${CMAKE_CROSSCOMPILING})
4+
if(CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME AND BUILD_TESTING)
55
add_subdirectory(tests/UNITTESTS)
66
endif()
77

connectivity/netsocket/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Copyright (c) 2020 ARM Limited. All rights reserved.
22
# SPDX-License-Identifier: Apache-2.0
33

4-
if(NOT ${CMAKE_CROSSCOMPILING})
4+
if(CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME AND BUILD_TESTING)
55
add_subdirectory(tests/UNITTESTS)
66
endif()
77

drivers/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Copyright (c) 2020 ARM Limited. All rights reserved.
22
# SPDX-License-Identifier: Apache-2.0
33

4-
if(NOT ${CMAKE_CROSSCOMPILING})
4+
if(CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME AND BUILD_TESTING)
55
add_subdirectory(tests/UNITTESTS)
66
endif()
77

events/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
# Copyright (c) 2020 ARM Limited. All rights reserved.
22
# SPDX-License-Identifier: Apache-2.0
33

4-
if(NOT ${CMAKE_CROSSCOMPILING})
4+
if(CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME AND BUILD_TESTING)
55
add_subdirectory(tests/UNITTESTS)
66
else()
7+
78
add_library(mbed-events INTERFACE)
89

910
target_include_directories(mbed-events

platform/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Copyright (c) 2020 ARM Limited. All rights reserved.
22
# SPDX-License-Identifier: Apache-2.0
33

4-
if(NOT ${CMAKE_CROSSCOMPILING})
4+
if(CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME AND BUILD_TESTING)
55
add_subdirectory(tests/UNITTESTS)
66
endif()
77

storage/blockdevice/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Copyright (c) 2020 ARM Limited. All rights reserved.
22
# SPDX-License-Identifier: Apache-2.0
33

4-
if(NOT ${CMAKE_CROSSCOMPILING})
4+
if(CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME AND BUILD_TESTING)
55
add_subdirectory(tests/UNITTESTS)
66
endif()
77

storage/kvstore/filesystemstore/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Copyright (c) 2020 ARM Limited. All rights reserved.
22
# SPDX-License-Identifier: Apache-2.0
33

4-
if(NOT ${CMAKE_CROSSCOMPILING})
4+
if(CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME AND BUILD_TESTING)
55
add_subdirectory(tests/UNITTESTS)
66
endif()
77

storage/kvstore/tdbstore/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Copyright (c) 2020 ARM Limited. All rights reserved.
22
# SPDX-License-Identifier: Apache-2.0
33

4-
if(NOT ${CMAKE_CROSSCOMPILING})
4+
if(CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME AND BUILD_TESTING)
55
add_subdirectory(tests/UNITTESTS)
66
endif()
77

0 commit comments

Comments
 (0)