From e85cfe45548cc55c9df5b1285fa34bc680fba7b3 Mon Sep 17 00:00:00 2001 From: Torsten Rasmussen Date: Thu, 5 Mar 2020 13:42:42 +0100 Subject: [PATCH] cmake: West extension command for Zephyr config package registration This command make it possible to use west for first time registration of a Zephyr config package in CMake. To register Zephyr as a CMake config package, invoke: west zephyr-export Signed-off-by: Torsten Rasmussen --- scripts/west-commands.yml | 5 ++ .../completion/west-completion.bash | 1 + scripts/west_commands/export.py | 58 +++++++++++++++++++ share/zephyr-package/cmake/CMakeLists.txt | 4 ++ share/zephyr-package/cmake/pristine.cmake | 24 ++++++++ 5 files changed, 92 insertions(+) create mode 100644 scripts/west_commands/export.py create mode 100644 share/zephyr-package/cmake/pristine.cmake diff --git a/scripts/west-commands.yml b/scripts/west-commands.yml index 81ff6306a05..eb1b6c9b13a 100644 --- a/scripts/west-commands.yml +++ b/scripts/west-commands.yml @@ -36,3 +36,8 @@ west-commands: - name: attach class: Attach help: interactively debug a board + - file: scripts/west_commands/export.py + commands: + - name: zephyr-export + class: ZephyrExport + help: export Zephyr installation as a CMake config package diff --git a/scripts/west_commands/completion/west-completion.bash b/scripts/west_commands/completion/west-completion.bash index b434396b4e5..30cd8e3ea22 100644 --- a/scripts/west_commands/completion/west-completion.bash +++ b/scripts/west_commands/completion/west-completion.bash @@ -770,6 +770,7 @@ __comp_west() debug debugserver attach + zephyr-export ) local cmds=(${builtin_cmds[*]} ${zephyr_ext_cmds[*]}) diff --git a/scripts/west_commands/export.py b/scripts/west_commands/export.py new file mode 100644 index 00000000000..fa753d836bf --- /dev/null +++ b/scripts/west_commands/export.py @@ -0,0 +1,58 @@ +# Copyright (c) 2020 Nordic Semiconductor ASA +# +# SPDX-License-Identifier: Apache-2.0 + +import argparse + +from west.commands import WestCommand + +from pathlib import PurePath +from zcmake import run_cmake + +EXPORT_DESCRIPTION = '''\ +This command registers the current Zephyr installation as a CMake +config package in the CMake user package registry. + +In Windows, the CMake user package registry is found in: +HKEY_CURRENT_USER\\Software\\Kitware\\CMake\\Packages\\ + +In Linux and MacOS, the CMake user package registry is found in: +~/.cmake/packages/''' + + +class ZephyrExport(WestCommand): + + def __init__(self): + super().__init__( + 'zephyr-export', + # Keep this in sync with the string in west-commands.yml. + 'export Zephyr installation as a CMake config package', + EXPORT_DESCRIPTION, + accepts_unknown_args=False) + + def do_add_parser(self, parser_adder): + parser = parser_adder.add_parser( + self.name, + help=self.help, + formatter_class=argparse.RawDescriptionHelpFormatter, + description=self.description) + return parser + + def do_run(self, args, unknown_args): + zephyr_config_package_path = PurePath(__file__).parents[2] \ + / 'share' / 'zephyr-package' / 'cmake' + + cmake_args = ['-S', f'{zephyr_config_package_path}', + '-B', f'{zephyr_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_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 31587d37ca4..002eb9b67c3 100644 --- a/share/zephyr-package/cmake/CMakeLists.txt +++ b/share/zephyr-package/cmake/CMakeLists.txt @@ -25,5 +25,9 @@ else() message("~/.cmake/packages/ZephyrUnitTest") endif() +add_custom_target(pristine + COMMAND ${CMAKE_COMMAND} -P ${CMAKE_CURRENT_LIST_DIR}/pristine.cmake +) + export(PACKAGE Zephyr) export(PACKAGE ZephyrUnittest) diff --git a/share/zephyr-package/cmake/pristine.cmake b/share/zephyr-package/cmake/pristine.cmake new file mode 100644 index 00000000000..30187c02a88 --- /dev/null +++ b/share/zephyr-package/cmake/pristine.cmake @@ -0,0 +1,24 @@ +# 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}/ZephyrUnittestConfigVersion.cmake" + "${CMAKE_CURRENT_LIST_DIR}/ZephyrConfig.cmake" + "${CMAKE_CURRENT_LIST_DIR}/ZephyrUnittestConfig.cmake" +) + +# Delete everything else, as those files are created by CMake. +file(REMOVE_RECURSE ${GENERATED_FILES})