From e819fa46cd67a8e7edcdc63648122174643a7d3b Mon Sep 17 00:00:00 2001 From: Torsten Rasmussen Date: Tue, 10 Mar 2020 14:52:35 +0100 Subject: [PATCH] scripts: west-commands: Updates needed when removing ZEPHYR_BASE env MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This commit includes the following fixes in order to remove environment setting of ZEPHYR_BASE is west extension commands. - Build command west build --pristine will now use the ZEPHYR_BASE variable found in CMakeCache.txt in the build folder. This ensures that the pristine command is executed from the same Zephyr that was used for compilation. - Board command The west boards command no longer sets Zephyr base before invoking cmake -P cmake/boards.cmake Instead boards.cmake uses find_package(Zephyr) to ensure consistent behavior with Zephyr samples, so that the detection of Zephyr base is uniform across CMake commands. It also changes BOARD_ROOT_SPACE_SEPARATED to BOARD_ROOT in order to be consistent with existing user documentation. Signed-off-by: Torsten Rasmussen Signed-off-by: Martí Bolívar --- cmake/boards.cmake | 15 +++++++-------- scripts/west_commands/boards.py | 11 +++-------- scripts/west_commands/build.py | 9 ++------- scripts/west_commands/sign.py | 4 ++-- scripts/west_commands/zephyr_ext_common.py | 17 ++++++++--------- 5 files changed, 22 insertions(+), 34 deletions(-) diff --git a/cmake/boards.cmake b/cmake/boards.cmake index e93f4a1e662..53e873bbc25 100644 --- a/cmake/boards.cmake +++ b/cmake/boards.cmake @@ -95,18 +95,17 @@ if(CMAKE_SCRIPT_MODE_FILE AND NOT CMAKE_PARENT_LIST_FILE) # some other script # The options available are: -# BOARD_ROOT_SPACE_SEPARATED: Space-separated board roots +# BOARD_ROOT: Semi-colon separated board roots # FILE_OUT: Set to a file path to save the boards to a file. If not defined the # the contents will be printed to stdout -if(NOT DEFINED ZEPHYR_BASE) - message(FATAL_ERROR "ZEPHYR_BASE not set") -endif() +cmake_minimum_required(VERSION 3.13.1) -if (NOT BOARD_ROOT_SPACE_SEPARATED) - message(FATAL_ERROR "BOARD_ROOT_SPACE_SEPARATED not defined") -endif() +set(NO_BOILERPLATE TRUE) +find_package(Zephyr HINTS $ENV{ZEPHYR_BASE}) -string(REPLACE " " ";" BOARD_ROOT "${BOARD_ROOT_SPACE_SEPARATED}") +# Appending Zephyr base to list of board roots, as this is also done in boilerplate.cmake. +# But as this file was executed in script mode, it must also be done here, to give same output. +list(APPEND BOARD_ROOT ${ZEPHYR_BASE}) if (NOT FILE_OUT) set(FILE_OUT FALSE) diff --git a/scripts/west_commands/boards.py b/scripts/west_commands/boards.py index 4743a44febe..330ec3d390f 100644 --- a/scripts/west_commands/boards.py +++ b/scripts/west_commands/boards.py @@ -4,13 +4,14 @@ import argparse import collections -import os import re import textwrap from west import log from west.commands import WestCommand + from zcmake import run_cmake +from zephyr_ext_common import ZEPHYR_CMAKE class Boards(WestCommand): @@ -54,13 +55,7 @@ class Boards(WestCommand): return parser def do_run(self, args, unknown_args): - zb = os.environ.get('ZEPHYR_BASE') - if not zb: - log.die('Internal error: ZEPHYR_BASE not set in the environment, ' - 'and should have been by the main script') - - cmake_args = ['-DBOARD_ROOT_SPACE_SEPARATED={}'.format(zb), - '-P', '{}/cmake/boards.cmake'.format(zb)] + cmake_args = ['-P', f'{ZEPHYR_CMAKE}/boards.cmake'] lines = run_cmake(cmake_args, capture_output=True) arch_re = re.compile(r'\s*([\w-]+)\:') board_re = re.compile(r'\s*([\w-]+)\s*') diff --git a/scripts/west_commands/build.py b/scripts/west_commands/build.py index 0b385df5433..d3cf87bb1dc 100644 --- a/scripts/west_commands/build.py +++ b/scripts/west_commands/build.py @@ -400,17 +400,12 @@ class Build(Forceable): def _run_pristine(self): _banner('making build dir {} pristine'.format(self.build_dir)) - - zb = os.environ.get('ZEPHYR_BASE') - if not zb: - log.die('Internal error: ZEPHYR_BASE not set in the environment, ' - 'and should have been by the main script') - if not is_zephyr_build(self.build_dir): log.die('Refusing to run pristine on a folder that is not a ' 'Zephyr build system') - cmake_args = ['-P', '{}/cmake/pristine.cmake'.format(zb)] + cache = CMakeCache.from_build_dir(self.build_dir) + cmake_args = ['-P', cache['ZEPHYR_BASE'] + '/cmake/pristine.cmake'] run_cmake(cmake_args, cwd=self.build_dir, dry_run=self.args.dry_run) def _run_build(self, target): diff --git a/scripts/west_commands/sign.py b/scripts/west_commands/sign.py index 0aad0b2e42b..26a597113d8 100644 --- a/scripts/west_commands/sign.py +++ b/scripts/west_commands/sign.py @@ -18,9 +18,9 @@ from build_helpers import find_build_dir, is_zephyr_build, \ from runners.core import BuildConfiguration from zcmake import CMakeCache from zephyr_ext_common import Forceable, cached_runner_config, \ - zephyr_scripts_path + ZEPHYR_SCRIPTS -sys.path.append(os.path.join(zephyr_scripts_path(), 'dts')) +sys.path.append(str(ZEPHYR_SCRIPTS / 'dts')) import edtlib diff --git a/scripts/west_commands/zephyr_ext_common.py b/scripts/west_commands/zephyr_ext_common.py index fe81f0eb132..233044cc046 100644 --- a/scripts/west_commands/zephyr_ext_common.py +++ b/scripts/west_commands/zephyr_ext_common.py @@ -16,6 +16,14 @@ from west.commands import WestCommand from runners.core import RunnerConfig +# This relies on this file being zephyr/scripts/foo/bar.py. +# If you move this file, you'll break it, so be careful. +THIS_ZEPHYR = Path(__file__).parent.parent.parent +ZEPHYR_BASE = Path(os.environ.get('ZEPHYR_BASE', THIS_ZEPHYR)) + +# FIXME we need a nicer way to handle imports from scripts and cmake than this. +ZEPHYR_SCRIPTS = ZEPHYR_BASE / 'scripts' +ZEPHYR_CMAKE = ZEPHYR_BASE / 'cmake' class Forceable(WestCommand): '''WestCommand subclass for commands with a --force option.''' @@ -55,12 +63,3 @@ def cached_runner_config(build_dir, cache): elf_file, hex_file, bin_file, gdb=gdb, openocd=openocd, openocd_search=openocd_search) - -# FIXME we should think of a nicer way to manage sys.path -# for shared Zephyr code. -def zephyr_scripts_path(): - # This relies on this file being zephyr/scripts/foo/bar.py. - zephyr_base = Path(os.environ.get('ZEPHYR_BASE', - Path(__file__).parent.parent.parent)) - - return str(zephyr_base / 'scripts')