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 <Torsten.Rasmussen@nordicsemi.no>
This commit is contained in:
Torsten Rasmussen 2020-03-05 13:42:42 +01:00 committed by Carles Cufí
commit e85cfe4554
5 changed files with 92 additions and 0 deletions

View file

@ -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

View file

@ -770,6 +770,7 @@ __comp_west()
debug
debugserver
attach
zephyr-export
)
local cmds=(${builtin_cmds[*]} ${zephyr_ext_cmds[*]})

View file

@ -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))

View file

@ -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)

View file

@ -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})