From 3074a7a4984b8d480030f25c9f77246b8f9ed5bd Mon Sep 17 00:00:00 2001 From: Torsten Rasmussen Date: Fri, 27 Mar 2020 21:31:36 +0100 Subject: [PATCH] cmake: Relocating Zephyr Unittest CMake package. Fixes: #23872 Relocating Zephyr Unittest CMake package to ensure that HINTS ${ZEPHYR_BASE} in find_package(ZephyrUnittest HINTS ${ZEPHYR_BASE}) works correctly when the package has not been exported to CMake user package registry. This ensure that the new package functionality is fully backwards compatible on systems where the package is not exported and ZEPHYR_BASE is set. Signed-off-by: Torsten Rasmussen --- CODEOWNERS | 1 + scripts/west_commands/export.py | 20 ++++++++++++- share/zephyr-package/cmake/CMakeLists.txt | 9 ++---- .../cmake/ZephyrUnittestConfig.cmake | 4 --- .../cmake/ZephyrUnittestConfigVersion.cmake | 3 -- share/zephyr-package/cmake/pristine.cmake | 2 -- .../cmake/CMakeLists.txt | 30 +++++++++++++++++++ .../cmake/ZephyrUnittestConfig.cmake | 4 +++ .../cmake/ZephyrUnittestConfigVersion.cmake | 3 ++ .../cmake/pristine.cmake | 21 +++++++++++++ 10 files changed, 81 insertions(+), 16 deletions(-) delete mode 100644 share/zephyr-package/cmake/ZephyrUnittestConfig.cmake delete mode 100644 share/zephyr-package/cmake/ZephyrUnittestConfigVersion.cmake create mode 100644 share/zephyrunittest-package/cmake/CMakeLists.txt create mode 100644 share/zephyrunittest-package/cmake/ZephyrUnittestConfig.cmake create mode 100644 share/zephyrunittest-package/cmake/ZephyrUnittestConfigVersion.cmake create mode 100644 share/zephyrunittest-package/cmake/pristine.cmake diff --git a/CODEOWNERS b/CODEOWNERS index 68861317abe..92db3a68b58 100644 --- a/CODEOWNERS +++ b/CODEOWNERS @@ -408,6 +408,7 @@ /scripts/zephyr_module.py @tejlmand /scripts/valgrind.supp @aescolar @daor-oti /share/zephyr-package/ @tejlmand +/share/zephyrunittest-package/ @tejlmand /subsys/bluetooth/ @joerchan @jhedberg @Vudentz /subsys/bluetooth/controller/ @carlescufi @cvinayak @thoh-ot /subsys/bluetooth/mesh/ @jhedberg @trond-snekvik @joerchan @Vudentz diff --git a/scripts/west_commands/export.py b/scripts/west_commands/export.py index fa753d836bf..f4f8367b72b 100644 --- a/scripts/west_commands/export.py +++ b/scripts/west_commands/export.py @@ -47,7 +47,7 @@ class ZephyrExport(WestCommand): lines = run_cmake(cmake_args, capture_output=True) # Let's clean up, as Zephyr has now been exported, and we no longer - # need the generated files. + # need the generated files. cmake_args = ['--build', f'{zephyr_config_package_path}', '--target', 'pristine'] run_cmake(cmake_args, capture_output=True) @@ -56,3 +56,21 @@ class ZephyrExport(WestCommand): # the important information. msg = [line for line in lines if not line.startswith('-- ')] print('\n'.join(msg)) + + zephyr_unittest_config_package_path = PurePath(__file__).parents[2] \ + / 'share' / 'zephyrunittest-package' / 'cmake' + + cmake_args = ['-S', f'{zephyr_unittest_config_package_path}', + '-B', f'{zephyr_unittest_config_package_path}'] + lines = run_cmake(cmake_args, capture_output=True) + + # Let's clean up, as Zephyr has now been exported, and we no longer + # need the generated files. + cmake_args = ['--build', f'{zephyr_unittest_config_package_path}', + '--target', 'pristine'] + run_cmake(cmake_args, capture_output=True) + + # Let's ignore the normal CMake printing and instead only print + # the important information. + msg = [line for line in lines if not line.startswith('-- ')] + print('\n'.join(msg)) diff --git a/share/zephyr-package/cmake/CMakeLists.txt b/share/zephyr-package/cmake/CMakeLists.txt index 002eb9b67c3..28028f503f6 100644 --- a/share/zephyr-package/cmake/CMakeLists.txt +++ b/share/zephyr-package/cmake/CMakeLists.txt @@ -15,14 +15,12 @@ if(NOT (CMAKE_BINARY_DIR STREQUAL CMAKE_CURRENT_LIST_DIR)) message(WARNING "\$\{CMAKE_BINARY_DIR\} is different from \$\{CMAKE_CURRENT_LIST_DIR\}, Zephyr config package may not work as expected.") endif() -message("Zephyr and ZephyrUnitTest (${CMAKE_CURRENT_LIST_DIR})") +message("Zephyr (${CMAKE_CURRENT_LIST_DIR})") message("has been added to the user package registry in:") if(WIN32) - message("HKEY_CURRENT_USER\\Software\\Kitware\\CMake\\Packages\\Zephyr") - message("HKEY_CURRENT_USER\\Software\\Kitware\\CMake\\Packages\\ZephyrUnitTest") + message("HKEY_CURRENT_USER\\Software\\Kitware\\CMake\\Packages\\Zephyr\n") else() - message("~/.cmake/packages/Zephyr") - message("~/.cmake/packages/ZephyrUnitTest") + message("~/.cmake/packages/Zephyr\n") endif() add_custom_target(pristine @@ -30,4 +28,3 @@ add_custom_target(pristine ) export(PACKAGE Zephyr) -export(PACKAGE ZephyrUnittest) diff --git a/share/zephyr-package/cmake/ZephyrUnittestConfig.cmake b/share/zephyr-package/cmake/ZephyrUnittestConfig.cmake deleted file mode 100644 index f157761d16b..00000000000 --- a/share/zephyr-package/cmake/ZephyrUnittestConfig.cmake +++ /dev/null @@ -1,4 +0,0 @@ -# SPDX-License-Identifier: Apache-2.0 - -set(ZEPHYR_UNITTEST TRUE) -include(${CMAKE_CURRENT_LIST_DIR}/ZephyrConfig.cmake) diff --git a/share/zephyr-package/cmake/ZephyrUnittestConfigVersion.cmake b/share/zephyr-package/cmake/ZephyrUnittestConfigVersion.cmake deleted file mode 100644 index f246147d9d2..00000000000 --- a/share/zephyr-package/cmake/ZephyrUnittestConfigVersion.cmake +++ /dev/null @@ -1,3 +0,0 @@ -# SPDX-License-Identifier: Apache-2.0 - -include(${CMAKE_CURRENT_LIST_DIR}/ZephyrConfigVersion.cmake) diff --git a/share/zephyr-package/cmake/pristine.cmake b/share/zephyr-package/cmake/pristine.cmake index 30187c02a88..d5bd9895d31 100644 --- a/share/zephyr-package/cmake/pristine.cmake +++ b/share/zephyr-package/cmake/pristine.cmake @@ -15,9 +15,7 @@ list(REMOVE_ITEM GENERATED_FILES "${CMAKE_CURRENT_LIST_DIR}/pristine.cmake" "${CMAKE_CURRENT_LIST_DIR}/zephyr_package_search.cmake" "${CMAKE_CURRENT_LIST_DIR}/ZephyrConfigVersion.cmake" - "${CMAKE_CURRENT_LIST_DIR}/ZephyrUnittestConfigVersion.cmake" "${CMAKE_CURRENT_LIST_DIR}/ZephyrConfig.cmake" - "${CMAKE_CURRENT_LIST_DIR}/ZephyrUnittestConfig.cmake" ) # Delete everything else, as those files are created by CMake. diff --git a/share/zephyrunittest-package/cmake/CMakeLists.txt b/share/zephyrunittest-package/cmake/CMakeLists.txt new file mode 100644 index 00000000000..287f369868d --- /dev/null +++ b/share/zephyrunittest-package/cmake/CMakeLists.txt @@ -0,0 +1,30 @@ +# SPDX-License-Identifier: Apache-2.0 + +# Purpose of this CMake file is to install a ZephyrUnittestConfig package reference in: +# Unix/Linux/MacOS: ~/.cmake/packages/ZephyrUnittest +# Windows : HKEY_CURRENT_USER +# +# Having ZephyrUnittestConfig package allows for find_package(ZephyrUnittest) to work when ZEPHYR_BASE is not defined. +# +# Create the reference by running `cmake .` in this directory. + +cmake_minimum_required(VERSION 3.13.1) +project(ZephyrUnittestPackageConfig NONE) + +if(NOT (CMAKE_BINARY_DIR STREQUAL CMAKE_CURRENT_LIST_DIR)) + message(WARNING "\$\{CMAKE_BINARY_DIR\} is different from \$\{CMAKE_CURRENT_LIST_DIR\}, ZephyrUnittest config package may not work as expected.") +endif() + +message("ZephyrUnittest (${CMAKE_CURRENT_LIST_DIR})") +message("has been added to the user package registry in:") +if(WIN32) + message("HKEY_CURRENT_USER\\Software\\Kitware\\CMake\\Packages\\ZephyrUnittest\n") +else() + message("~/.cmake/packages/ZephyrUnittest\n") +endif() + +add_custom_target(pristine + COMMAND ${CMAKE_COMMAND} -P ${CMAKE_CURRENT_LIST_DIR}/pristine.cmake +) + +export(PACKAGE ZephyrUnittest) diff --git a/share/zephyrunittest-package/cmake/ZephyrUnittestConfig.cmake b/share/zephyrunittest-package/cmake/ZephyrUnittestConfig.cmake new file mode 100644 index 00000000000..c70d7555748 --- /dev/null +++ b/share/zephyrunittest-package/cmake/ZephyrUnittestConfig.cmake @@ -0,0 +1,4 @@ +# SPDX-License-Identifier: Apache-2.0 + +set(ZEPHYR_UNITTEST TRUE) +include(${CMAKE_CURRENT_LIST_DIR}/../../zephyr-package/cmake/ZephyrConfig.cmake) diff --git a/share/zephyrunittest-package/cmake/ZephyrUnittestConfigVersion.cmake b/share/zephyrunittest-package/cmake/ZephyrUnittestConfigVersion.cmake new file mode 100644 index 00000000000..a90abdcc6ab --- /dev/null +++ b/share/zephyrunittest-package/cmake/ZephyrUnittestConfigVersion.cmake @@ -0,0 +1,3 @@ +# SPDX-License-Identifier: Apache-2.0 + +include(${CMAKE_CURRENT_LIST_DIR}/../../zephyr-package/cmake/ZephyrConfigVersion.cmake) diff --git a/share/zephyrunittest-package/cmake/pristine.cmake b/share/zephyrunittest-package/cmake/pristine.cmake new file mode 100644 index 00000000000..6e0dfee6157 --- /dev/null +++ b/share/zephyrunittest-package/cmake/pristine.cmake @@ -0,0 +1,21 @@ +# SPDX-License-Identifier: Apache-2.0 + +# Purpose of this CMake file is to clean all CMake files generated by CMake when +# exporting Zephry to CMake user package registry. + +# Get a list of all files. +file(GLOB_RECURSE GENERATED_FILES + LIST_DIRECTORIES true + ${CMAKE_CURRENT_LIST_DIR}/* +) + +# Remove the files that is used be Zephyr from the list. +list(REMOVE_ITEM GENERATED_FILES + "${CMAKE_CURRENT_LIST_DIR}/CMakeLists.txt" + "${CMAKE_CURRENT_LIST_DIR}/pristine.cmake" + "${CMAKE_CURRENT_LIST_DIR}/ZephyrUnittestConfigVersion.cmake" + "${CMAKE_CURRENT_LIST_DIR}/ZephyrUnittestConfig.cmake" +) + +# Delete everything else, as those files are created by CMake. +file(REMOVE_RECURSE ${GENERATED_FILES})