Error when calling the west build command from a python script, works from cmd line

Hi,

When running the python script in attachments

import argparse
import os
import subprocess
import sys

def install_toolchain_and_sdk(version, install_dir):
    print(f"Installing SDK version {version} using sdk-manager...")
    result = subprocess.run([
        "nrfutil", "sdk-manager", "install", version, "--install-dir", install_dir
    ], capture_output=True, text=True)
    if result.returncode != 0:
        print("Error installing SDK:", result.stderr)
        sys.exit(1)
    print("SDK installed successfully.")

def launch_build(sdk_version, project_path, build_dir, board, config, install_dir, custom_board_dir):
    print(f"Setting up environment for SDK version {sdk_version}...")
    nrf_command = [
        "nrfutil", "sdk-manager", "toolchain", "launch",
        "--ncs-version", sdk_version,
        "--chdir", project_path,
        "--install-dir", install_dir,
        "--"
    ]
    west_command = [
        "west", "build",
        "--sysbuild",
        "--pristine",
        "-b", board,
        "-d", build_dir,
        project_path,
    ]
    cmake_args = []
    if config:
        cmake_args.append(f"-DCONF_FILE={config}")
    if custom_board_dir:
        cmake_args.append(f"-DBOARD_ROOT={custom_board_dir}")
    if cmake_args:
        west_command += ["--"] + cmake_args

    env = os.environ.copy()
    env["ZEPHYR_BASE"] = install_dir + '\\' + sdk_version + '\\' + "zephyr"
    print(env["ZEPHYR_BASE"])

    result = subprocess.run(nrf_command + west_command, text=True, env=env)

    print(result.stdout)

    if result.returncode != 0:
        print("Build failed:", result.stderr)
        sys.exit(1)
    print("Build completed successfully.")

def main():
    parser = argparse.ArgumentParser(description="Build a Nordic NRF Connect application with sysbuild.")
    parser.add_argument("--sdk-version", required=True, help="The version of the SDK/toolchain to install.")
    parser.add_argument("--project-path", required=True, help="Path to the NRF Connect project.")
    parser.add_argument("--build-dir", required=True, help="Destination directory for the build output.")
    parser.add_argument("--board", default="nrf5340dk_nrf5340_cpuapp", help="Target board for the build (default: nrf5340dk_nrf5340_cpuapp).")
    parser.add_argument("--config", help="Optional configuration file for the build.")
    parser.add_argument("--install-dir", required=True, help="Directory to install the SDK and toolchain.")
    parser.add_argument("--custom-board-dir", help="Optional directory for custom board definitions.")
    args = parser.parse_args()

    install_toolchain_and_sdk(args.sdk_version, args.install_dir)
    launch_build(args.sdk_version, args.project_path, args.build_dir, args.board, args.config, args.install_dir, args.custom_board_dir)

if __name__ == "__main__":
    main()

By invoking it  like this : 

python.exe ./nrf_build.py --sdk-version v3.0.2 --project-path <path_of_the_project> --build-dir <build_dest> --install-dir D:\ncs --custom-board-dir <path_of_the_project> --board rf2g4/nrf52833

I get the error:

...

-- Configuring done
-- Generating done
CMake Error:
  Running

   'D:/ncs/toolchains/0b393f9e1b/opt/bin/ninja.exe' '-C' 'D:/builds/opt.rf.2g4/build/opt.rf.2g4' '-t' 'recompact'

  failed with:

   ninja: error: build.ninja:9629: lexing error

  



CMake Generate step failed.  Build files cannot be regenerated correctly.
CMake Error at cmake/modules/sysbuild_extensions.cmake:514 (message):
  CMake configure failed for Zephyr project: opt.rf.2g4

  Location: <project_path>
Call Stack (most recent call first):
  cmake/modules/sysbuild_images.cmake:43 (ExternalZephyrProject_Cmake)
  cmake/modules/sysbuild_default.cmake:21 (include)
  D:/ncs/v3.0.2/zephyr/share/zephyr-package/cmake/ZephyrConfig.cmake:75 (include)
  D:/ncs/v3.0.2/zephyr/share/zephyr-package/cmake/ZephyrConfig.cmake:92 (include_boilerplate)
  D:/ncs/v3.0.2/zephyr/share/sysbuild-package/cmake/SysbuildConfig.cmake:8 (include)
  template/CMakeLists.txt:10 (find_package)


-- Configuring incomplete, errors occurred!

In fact, the generated build.ninja file contains some strange char (seems to be the \x1B escape char) at the start of indicated line, in # Custom command for zephyr\cmake\reports\CMakeFiles\ram_report and # Custom command for zephyr\cmake\reports\CMakeFiles\rom_report

When I do it from cmd line:

1. cd D:\ncs\v3.0.2\zephyr then run zephyr-env.cmd to set the ZEPHYR_BASE env var

2. nrfutil sdk-manager toolchain launch --ncs-version v3.0.2 --chdir <project-path> --install-dir D:\ncs -- west build --sysbuild --pristine -b rf2g4/nrf52833 -d <build_dest> <project-path> -- -DBOARD_ROOT=<path_of_the_project>

It works completely. Anyone having trouble to launch the build from a python script ? (tested with python 3.11, 3.12, 3.13 with same failure, also trying with older SDK gives the same results : v2.9.1)

We need to launch the build from our internal build tool written in python, I don't figure out why I get different results from calling the exact same cmd from the CLI vs in python

Thanks

Parents Reply Children
No Data
Related