• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python3
2# -*- coding: utf-8 -*-
3# Copyright (c) 2025 Huawei Device Co., Ltd.
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8#     http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15
16
17"""
18A script to run binary commands in GN build actions.
19This script takes the binary name as the first argument,
20followed by any arguments to pass to the binary.
21If the first argument is in the format 'path-env=xxxx',
22it will add xxxx to the beginning of the PATH environment variable.
23"""
24
25import os
26import sys
27import subprocess
28import platform
29
30def main():
31    if len(sys.argv) < 2:
32        print("Error: No binary specified")
33        print("Usage: run_binary.py <binary_name> [args...]")
34        print("       run_binary.py path-env=<path> <binary_name> [args...]")
35        return 1
36
37    # Check if the first argument is a path-env parameter
38    args_start_index = 1
39    if sys.argv[1].startswith("path-env="):
40        # Extract the path from the parameter
41        path_to_add = sys.argv[1].split("=", 1)[1]
42        # Add the path to the beginning of the PATH environment variable
43        current_path = os.environ.get("PATH", "")
44        os.environ["PATH"] = path_to_add + os.pathsep + current_path
45        print(f"Added '{path_to_add}' to the beginning of PATH")
46
47        args_start_index = 2
48
49        if len(sys.argv) < 3:
50            print("Error: No binary specified after path-env parameter")
51            print("Usage: run_binary.py path-env=<path> <binary_name> [args...]")
52            return 1
53
54    # The binary name is now at args_start_index
55    binary = sys.argv[args_start_index]
56
57    # All remaining arguments are passed to the binary
58    args = sys.argv[args_start_index + 1:]
59
60    # Construct the full command
61    cmd = [binary] + args
62    print("start run cmd:")
63    print(" ".join(cmd))
64
65    try:
66        ret = subprocess.run(cmd, capture_output=True, text=True, check=True)
67        print(f"run cmd: {ret.stdout}")
68        return 0
69    except subprocess.CalledProcessError as e:
70        print(f"Error executing command: {e}", file=sys.stderr)
71        print(f"error message: {e.stderr}")
72        print(f"output message: {e.output}")
73        return 1
74
75if __name__ == "__main__":
76    sys.exit(main())