Commit 63cf79bd authored by Torsten Rasmussen's avatar Torsten Rasmussen Committed by Benjamin Cabé
Browse files

scripts: support number of jobs and verbosity in west build.

Calling `west build --build-opt="-v" --build-opt="-j=<n>"`
passes the build options to `cmake --build ... -- <build-opt>` which
again is passed to the native build tool, such as ninja or make.

However, when ExternalProjects are used in CMake, such as in TF-M or
sysbuild builds then those extra build options are only passed to the
first image build and not those build as external projects.

CMake supports environment variables for those flags, so translate
verbosity and number of jobs to those environment variables and thereby
support those flags for all images when specified by the user.

Signed-off-by: default avatarTorsten Rasmussen <Torsten.Rasmussen@nordicsemi.no>
parent dbd35026
Loading
Loading
Loading
Loading
+26 −3
Changes for scripts/west_commands/zcmake.py: 26 added lines, 3 removed lines.
Original line number Diff line number Diff line
@@ -10,6 +10,7 @@ See build.py for the build command itself.
'''

from collections import OrderedDict
import argparse
import os.path
import re
import subprocess
@@ -26,7 +27,7 @@ DEFAULT_CMAKE_GENERATOR = 'Ninja'
'''Name of the default CMake generator.'''


def run_cmake(args, cwd=None, capture_output=False, dry_run=False):
def run_cmake(args, cwd=None, capture_output=False, dry_run=False, env=None):
    '''Run cmake to (re)generate a build system, a script, etc.

    :param args: arguments to pass to CMake
@@ -36,6 +37,7 @@ def run_cmake(args, cwd=None, capture_output=False, dry_run=False):
                           dry_run is also True)
    :param dry_run: don't actually execute the command, just print what
                    would have been run
    :param env: used adjusted environment when running CMake

    If capture_output is set to True, returns the output of the command instead
    of displaying it on stdout/stderr..'''
@@ -60,7 +62,7 @@ def run_cmake(args, cwd=None, capture_output=False, dry_run=False):
        return None

    log.dbg('Running CMake:', quote_sh_list(cmd), level=log.VERBOSE_NORMAL)
    p = subprocess.Popen(cmd, **kwargs)
    p = subprocess.Popen(cmd, env=env, **kwargs)
    out, _ = p.communicate()
    if p.returncode == 0:
        if out:
@@ -82,8 +84,29 @@ def run_build(build_directory, **kwargs):

    Any additional keyword arguments are passed as-is to run_cmake().
    '''
    cmake_env = None
    extra_args = kwargs.pop('extra_args', [])
    return run_cmake(['--build', build_directory] + extra_args, **kwargs)

    try:
        index = extra_args.index('--') + 1
        build_opt_parser = argparse.ArgumentParser(allow_abbrev=False)
        build_opt_parser.add_argument('-j', '--jobs')
        build_opt_parser.add_argument('-v', '--verbose', action='store_true')
        build_opts, native_args = build_opt_parser.parse_known_args(extra_args[index:])
        extra_args = extra_args[:index] + native_args

        if build_opts:
            cmake_env = os.environ.copy()
            if build_opts.jobs:
                cmake_env["CMAKE_BUILD_PARALLEL_LEVEL"] = build_opts.jobs

            if build_opts.verbose:
                cmake_env["VERBOSE"] = "1"

    except ValueError:
        pass # Ignore, no presence of '--' so nothing to do.

    return run_cmake(['--build', build_directory] + extra_args, env=cmake_env, **kwargs)


def make_c_identifier(string):