diff --git a/doc/guides/zephyr_cmake_package.rst b/doc/guides/zephyr_cmake_package.rst index ff8e749f846..c650e7fc471 100644 --- a/doc/guides/zephyr_cmake_package.rst +++ b/doc/guides/zephyr_cmake_package.rst @@ -58,11 +58,16 @@ Zephyr CMake package is exported to the CMake user package registry using the fo .. code-block:: bash - cmake -S /share/zephyr-package/cmake -B /share/zephyr-package/cmake - cmake --build /share/zephyr-package/cmake --target pristine + cmake -P /share/zephyr-package/cmake/zephyr_export.cmake + +This will export the current Zephyr to the CMake user package registry. + +To also export the Zephyr Unittest CMake package, run the following command in addition: + +.. code-block:: bash + + cmake -P /share/zephyrunittest-package/cmake/zephyr_export.cmake -This will export the current Zephyr to the CMake user package registry and remove the temporary -files generated by CMake during export. .. _zephyr_cmake_package_zephyr_base: diff --git a/scripts/west_commands/export.py b/scripts/west_commands/export.py index f7f8dec5844..b650ed2b55c 100644 --- a/scripts/west_commands/export.py +++ b/scripts/west_commands/export.py @@ -5,7 +5,6 @@ import argparse from pathlib import Path from shutil import rmtree -from subprocess import CalledProcessError from west.commands import WestCommand from west import log @@ -45,35 +44,20 @@ class ZephyrExport(WestCommand): # The 'share' subdirectory of the top level zephyr repository. share = Path(__file__).parents[2] / 'share' - run_cmake_and_clean_up(share / 'zephyr-package' / 'cmake') - run_cmake_and_clean_up(share / 'zephyrunittest-package' / 'cmake') + run_cmake_export(share / 'zephyr-package' / 'cmake') + run_cmake_export(share / 'zephyrunittest-package' / 'cmake') -def run_cmake_and_clean_up(path): - # Run a package installation script, cleaning up afterwards. +def run_cmake_export(path): + # Run a package installation script. # # Filtering out lines that start with -- ignores the normal # CMake status messages and instead only prints the important # information. - try: - lines = run_cmake(['-S', str(path), '-B', str(path)], - capture_output=True) - finally: - msg = [line for line in lines if not line.startswith('-- ')] - log.inf('\n'.join(msg)) - clean_up(path) - -def clean_up(path): - try: - run_cmake(['-P', str(path / 'pristine.cmake')], - capture_output=True) - except CalledProcessError: - # Do our best to clean up even though CMake failed. - log.wrn(f'Failed to make {path} pristine; ' - 'removing known generated files...') - for subpath in ['CMakeCache.txt', 'CMakeFiles', 'build.ninja', - 'cmake_install.cmake', 'rules.ninja']: - remove_if_exists(Path(path) / subpath) + lines = run_cmake(['-P', str(path / 'zephyr_export.cmake')], + capture_output=True) + msg = [line for line in lines if not line.startswith('-- ')] + log.inf('\n'.join(msg)) def remove_if_exists(pathobj): if pathobj.is_file(): diff --git a/share/zephyr-package/cmake/CMakeLists.txt b/share/zephyr-package/cmake/CMakeLists.txt deleted file mode 100644 index 28028f503f6..00000000000 --- a/share/zephyr-package/cmake/CMakeLists.txt +++ /dev/null @@ -1,30 +0,0 @@ -# SPDX-License-Identifier: Apache-2.0 - -# Purpose of this CMake file is to install a ZephyrConfig package reference in: -# Unix/Linux/MacOS: ~/.cmake/packages/Zephyr -# Windows : HKEY_CURRENT_USER -# -# Having ZephyrConfig package allows for find_package(Zephyr) 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(ZephyrPackageConfig NONE) - -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 (${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\n") -else() - message("~/.cmake/packages/Zephyr\n") -endif() - -add_custom_target(pristine - COMMAND ${CMAKE_COMMAND} -P ${CMAKE_CURRENT_LIST_DIR}/pristine.cmake -) - -export(PACKAGE Zephyr) diff --git a/share/zephyr-package/cmake/pristine.cmake b/share/zephyr-package/cmake/pristine.cmake deleted file mode 100644 index d5bd9895d31..00000000000 --- a/share/zephyr-package/cmake/pristine.cmake +++ /dev/null @@ -1,22 +0,0 @@ -# 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}/zephyr_package_search.cmake" - "${CMAKE_CURRENT_LIST_DIR}/ZephyrConfigVersion.cmake" - "${CMAKE_CURRENT_LIST_DIR}/ZephyrConfig.cmake" -) - -# Delete everything else, as those files are created by CMake. -file(REMOVE_RECURSE ${GENERATED_FILES}) diff --git a/share/zephyr-package/cmake/zephyr_export.cmake b/share/zephyr-package/cmake/zephyr_export.cmake new file mode 100644 index 00000000000..ed652540ae3 --- /dev/null +++ b/share/zephyr-package/cmake/zephyr_export.cmake @@ -0,0 +1,37 @@ +# SPDX-License-Identifier: Apache-2.0 + +# Purpose of this CMake file is to install a ZephyrConfig package reference in: +# Unix/Linux/MacOS: ~/.cmake/packages/Zephyr +# Windows : HKEY_CURRENT_USER +# +# Having ZephyrConfig package allows for find_package(Zephyr) to work when ZEPHYR_BASE is not defined. +# +# Create the reference by running `cmake -P zephyr_export.cmake` in this directory. + +set(MD5_INFILE "current_path.txt") + +# We write CMAKE_CURRENT_LIST_DIR into MD5_INFILE, as the content of that file will be used for MD5 calculation. +# This means we effectively get the MD5 of CMAKE_CURRENT_LIST_DIR which must be used for CMake user package registry. +file(WRITE ${CMAKE_CURRENT_LIST_DIR}/${MD5_INFILE} ${CMAKE_CURRENT_LIST_DIR}) +execute_process(COMMAND ${CMAKE_COMMAND} -E md5sum ${CMAKE_CURRENT_LIST_DIR}/${MD5_INFILE} + OUTPUT_VARIABLE MD5_SUM +) +string(SUBSTRING ${MD5_SUM} 0 32 MD5_SUM) +if(WIN32) + execute_process(COMMAND ${CMAKE_COMMAND} + -E write_regv + "HKEY_CURRENT_USER\\Software\\Kitware\\CMake\\Packages\\Zephyr\;${MD5_SUM}" "${CMAKE_CURRENT_LIST_DIR}" +) +else() + file(WRITE $ENV{HOME}/.cmake/packages/Zephyr/${MD5_SUM} ${CMAKE_CURRENT_LIST_DIR}) +endif() + +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\n") +else() + message("~/.cmake/packages/Zephyr\n") +endif() + +file(REMOVE ${CMAKE_CURRENT_LIST_DIR}/ZephyrPackagePath.txt) diff --git a/share/zephyrunittest-package/cmake/CMakeLists.txt b/share/zephyrunittest-package/cmake/CMakeLists.txt deleted file mode 100644 index 287f369868d..00000000000 --- a/share/zephyrunittest-package/cmake/CMakeLists.txt +++ /dev/null @@ -1,30 +0,0 @@ -# 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/pristine.cmake b/share/zephyrunittest-package/cmake/pristine.cmake deleted file mode 100644 index 6e0dfee6157..00000000000 --- a/share/zephyrunittest-package/cmake/pristine.cmake +++ /dev/null @@ -1,21 +0,0 @@ -# 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}) diff --git a/share/zephyrunittest-package/cmake/zephyr_export.cmake b/share/zephyrunittest-package/cmake/zephyr_export.cmake new file mode 100644 index 00000000000..a0a65b2e090 --- /dev/null +++ b/share/zephyrunittest-package/cmake/zephyr_export.cmake @@ -0,0 +1,37 @@ +# 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 -P zephyr_export.cmake` in this directory. + +set(MD5_INFILE "current_path.txt") + +# We write CMAKE_CURRENT_LIST_DIR into MD5_INFILE, as the content of that file will be used for MD5 calculation. +# This means we effectively get the MD5 of CMAKE_CURRENT_LIST_DIR which must be used for CMake user package registry. +file(WRITE ${CMAKE_CURRENT_LIST_DIR}/${MD5_INFILE} ${CMAKE_CURRENT_LIST_DIR}) +execute_process(COMMAND ${CMAKE_COMMAND} -E md5sum ${CMAKE_CURRENT_LIST_DIR}/${MD5_INFILE} + OUTPUT_VARIABLE MD5_SUM +) +string(SUBSTRING ${MD5_SUM} 0 32 MD5_SUM) +if(WIN32) + execute_process(COMMAND ${CMAKE_COMMAND} + -E write_regv + "HKEY_CURRENT_USER\\Software\\Kitware\\CMake\\Packages\\ZephyrUnittest\;${MD5_SUM}" "${CMAKE_CURRENT_LIST_DIR}" +) +else() + file(WRITE $ENV{HOME}/.cmake/packages/ZephyrUnittest/${MD5_SUM} ${CMAKE_CURRENT_LIST_DIR}) +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() + +file(REMOVE ${CMAKE_CURRENT_LIST_DIR}/${MD5_INFILE})