From 49e9eec26ca1b35bd906702d387fec39bdfae610 Mon Sep 17 00:00:00 2001 From: Marc Herbert Date: Thu, 17 Feb 2022 06:26:49 +0000 Subject: [PATCH 1/2] xtensa-build-zephyr.py: switch to RawTextHelpFormatter and """ We need control to format add_argument(help=...) strings manually too. Zero functional change. Signed-off-by: Marc Herbert --- scripts/xtensa-build-zephyr.py | 50 ++++++++++++++++++---------------- 1 file changed, 27 insertions(+), 23 deletions(-) diff --git a/scripts/xtensa-build-zephyr.py b/scripts/xtensa-build-zephyr.py index d19069d71fe8..a76758130bac 100755 --- a/scripts/xtensa-build-zephyr.py +++ b/scripts/xtensa-build-zephyr.py @@ -93,7 +93,7 @@ def __call__(self, parser, namespace, values, option_string=None): def parse_args(): global args global west_top - parser = argparse.ArgumentParser(formatter_class=argparse.RawDescriptionHelpFormatter, + parser = argparse.ArgumentParser(formatter_class=argparse.RawTextHelpFormatter, epilog=("This script supports XtensaTools but only when installed in a specific\n" + "directory structure, example:\n" + "myXtensa/\n" + @@ -130,31 +130,35 @@ def parse_args(): help="URL to clone Zephyr from") mode_group = parser.add_mutually_exclusive_group() mode_group.add_argument("-p", "--west_path", required=False, type=pathlib.Path, - help="Points to existing Zephyr project directory. Incompatible with -c." - " Uses by default path used by -c mode: SOF_TOP/zephyrproject. " - " If zephyr-project/modules/audio/sof is missing then a" - " symbolic link pointing to SOF_TOP directory will automatically be" - " created and west will recognize it as its new sof module." - " If zephyr-project/modules/audio/sof already exists and is a" - " different copy than where this script is run from, then the" - " behavior is undefined." - " This -p option is always required_if the real (not symbolic)" - " sof/ and zephyr-project/ directories are not nested in one" - " another.") + help="""Points to existing Zephyr project directory. Incompatible with -c. +Uses by default path used by -c mode: SOF_TOP/zephyrproject. +If zephyr-project/modules/audio/sof is missing then a +symbolic link pointing to SOF_TOP directory will automatically be +created and west will recognize it as its new sof module. +If zephyr-project/modules/audio/sof already exists and is a +different copy than where this script is run from, then the +behavior is undefined. +This -p option is always required_if the real (not symbolic) +sof/ and zephyr-project/ directories are not nested in one +another.""", + ) mode_group.add_argument("-c", "--clone_mode", required=False, action="store_true", - help="Using west, downloads inside this sof clone a new Zephyr" - " project with the required git repos. Creates a" - " sof/zephyrproject/modules/audio/sof symbolic link pointing" - " back at this sof clone." - " Incompatible with -p. To stop after downloading Zephyr, do not" - " pass any platform or cmake argument.") + help="""Using west, downloads inside this sof clone a new Zephyr +project with the required git repos. Creates a +sof/zephyrproject/modules/audio/sof symbolic link pointing +back at this sof clone. +Incompatible with -p. To stop after downloading Zephyr, do not +pass any platform or cmake argument.""", + ) parser.add_argument('-v', '--verbose', default=0, action='count', - help="Verbosity level. Repetition of the flag increases verbosity." - " verbosity lvl 1: shows underlying build system commands," - " verbosity lvl 2: lvl 1 + prints commands invoked by this script.") + help="""Verbosity level. Repetition of the flag increases verbosity. +verbosity lvl 1: shows underlying build system commands, +verbosity lvl 2: lvl 1 + prints commands invoked by this script.""", + ) parser.add_argument("-C", "--cmake-args", required=False, - help="Cmake arguments passed to cmake configure step without" - " '-D' prefix. Eg. CMAKE_GENERATOR='Ninja'. Preserves internal quotes.") + help="""Cmake arguments passed to cmake configure step without +'-D' prefix. Eg. CMAKE_GENERATOR='Ninja'. Preserves internal quotes.""", + ) args = parser.parse_args() From d227a96609c7d41d9e29ab29774a23205b7d8d13 Mon Sep 17 00:00:00 2001 From: Marc Herbert Date: Thu, 17 Feb 2022 07:23:05 +0000 Subject: [PATCH 2/2] xtensa-build-zephyr.py: fix -C option so it can support whitespace Make it possible to invoke -C multiple times which is required to support whitespace as in: -C=--warn-uninitialized -C '-DEXTRA_FLAGS=-Werror -g0' Signed-off-by: Marc Herbert --- scripts/xtensa-build-zephyr.py | 41 +++++++++++++++++++++------------- 1 file changed, 26 insertions(+), 15 deletions(-) diff --git a/scripts/xtensa-build-zephyr.py b/scripts/xtensa-build-zephyr.py index a76758130bac..8fc3a29f206c 100755 --- a/scripts/xtensa-build-zephyr.py +++ b/scripts/xtensa-build-zephyr.py @@ -155,9 +155,15 @@ def parse_args(): verbosity lvl 1: shows underlying build system commands, verbosity lvl 2: lvl 1 + prints commands invoked by this script.""", ) - parser.add_argument("-C", "--cmake-args", required=False, - help="""Cmake arguments passed to cmake configure step without -'-D' prefix. Eg. CMAKE_GENERATOR='Ninja'. Preserves internal quotes.""", + # Cannot use a standard -- delimiter because argparse deletes it. + parser.add_argument("-C", "--cmake-args", action='append', default=[], + help="""Cmake arguments passed as is to cmake configure step. +Can be passed multiple times; whitespace is preserved Example: + + -C=--warn-uninitialized -C '-DEXTRA_FLAGS=-Werror -g0' + +Note '-C --warn-uninitialized' is not supported by argparse, an equal +sign must be used (https://bugs.python.org/issue9334)""", ) args = parser.parse_args() @@ -180,17 +186,6 @@ def parse_args(): elif not args.clone_mode: # if neather -p nor -c provided, use -p args.west_path = west_top - if args.verbose >= 1: - if not args.cmake_args: - args.cmake_args = "CMAKE_VERBOSE_MAKEFILE=ON" - else: - args.cmake_args = "CMAKE_VERBOSE_MAKEFILE=ON " + args.cmake_args - - # split arguments by whitespaces and append -D prefix to match expected CMake format - if args.cmake_args: - arg_list = shlex.split(args.cmake_args) - arg_list = ["-D" + arg for arg in arg_list] - args.cmake_args = " ".join(arg_list) def execute_command(command_args, stdin=None, input=None, stdout=None, stderr=None, capture_output=False, shell=False, cwd=None, timeout=None, check=False, encoding=None, @@ -333,6 +328,17 @@ def build_platforms(): print(f"XTENSA_SYSTEM={XTENSA_SYSTEM}") platform_build_dir_name = f"build-{platform}" + + # https://docs.zephyrproject.org/latest/guides/west/build-flash-debug.html#one-time-cmake-arguments + # https://github.com/zephyrproject-rtos/zephyr/pull/40431#issuecomment-975992951 + abs_build_dir = pathlib.Path(west_top, platform_build_dir_name) + if (pathlib.Path(abs_build_dir, "build.ninja").is_file() + or pathlib.Path(abs_build_dir, "Makefile").is_file()): + if args.cmake_args: + print(args.cmake_args) + raise RuntimeError("Some CMake arguments are ignored in incremental builds, " + + f"you must delete {abs_build_dir} first") + PLAT_CONFIG = platform_dict["PLAT_CONFIG"] build_cmd = ["west"] if args.verbose > 0: @@ -340,8 +346,13 @@ def build_platforms(): build_cmd += ["build", "--build-dir", platform_build_dir_name] source_dir = pathlib.Path(west_top, "zephyr", "samples", "subsys", "audio", "sof") build_cmd += ["--board", PLAT_CONFIG, str(source_dir)] + + build_cmd.append('--') if args.cmake_args: - build_cmd += ["--", args.cmake_args] + build_cmd += args.cmake_args + if args.verbose >= 1: + build_cmd.append("-DCMAKE_VERBOSE_MAKEFILE=ON") + # Build execute_command(build_cmd, check=True, cwd=west_top) smex_executable = pathlib.Path(west_top, platform_build_dir_name, "zephyr", "smex_ep",