Merge changes Ia3ad8ebf,I94422993 into main

* changes:
  Add option to print command output and fix env on dumpvars commands
  Make build script executable via python
This commit is contained in:
Luca Farsi 2024-01-18 19:56:57 +00:00 committed by Gerrit Code Review
commit 9c7dbf06a4
2 changed files with 29 additions and 13 deletions

View file

@ -1,3 +1,4 @@
#!prebuilts/build-tools/linux-x86/bin/py3-cmd
# Copyright 2024, The Android Open Source Project
#
# Licensed under the Apache License, Version 2.0 (the "License");

View file

@ -69,7 +69,7 @@ def build_everything(args: argparse.Namespace):
build_command = base_build_command(args)
build_command.append('general-tests')
run_command(build_command)
run_command(build_command, print_output=True)
def build_affected_modules(args: argparse.Namespace):
@ -81,9 +81,9 @@ def build_affected_modules(args: argparse.Namespace):
build_command = base_build_command(args)
build_command.extend(modules_to_build)
run_command(build_command)
run_command(build_command, print_output=True)
zip_build_outputs(modules_to_build, args.dist_dir)
zip_build_outputs(modules_to_build, args.dist_dir, args.target_release)
def base_build_command(args: argparse.Namespace) -> list:
@ -102,12 +102,17 @@ def base_build_command(args: argparse.Namespace) -> list:
return build_command
def run_command(args: list[str]) -> str:
def run_command(
args: list[str],
env: Dict[Text, Text] = os.environ,
print_output: bool = False,
) -> str:
result = subprocess.run(
args=args,
text=True,
capture_output=True,
check=False,
env=env,
)
# If the process failed, print its stdout and propagate the exception.
if not result.returncode == 0:
@ -116,6 +121,10 @@ def run_command(args: list[str]) -> str:
print('stderr: ' + result.stderr)
result.check_returncode()
if print_output:
print(result.stdout)
return result.stdout
@ -201,17 +210,19 @@ def matches_file_patterns(
return False
def zip_build_outputs(modules_to_build: Set[Text], dist_dir: Text):
def zip_build_outputs(
modules_to_build: Set[Text], dist_dir: Text, target_release: Text
):
src_top = os.environ.get('TOP', os.getcwd())
# Call dumpvars to get the necessary things.
# TODO(lucafarsi): Don't call soong_ui 4 times for this, --dumpvars-mode can
# do it but it requires parsing.
host_out_testcases = get_soong_var('HOST_OUT_TESTCASES')
target_out_testcases = get_soong_var('TARGET_OUT_TESTCASES')
product_out = get_soong_var('PRODUCT_OUT')
soong_host_out = get_soong_var('SOONG_HOST_OUT')
host_out = get_soong_var('HOST_OUT')
host_out_testcases = get_soong_var('HOST_OUT_TESTCASES', target_release)
target_out_testcases = get_soong_var('TARGET_OUT_TESTCASES', target_release)
product_out = get_soong_var('PRODUCT_OUT', target_release)
soong_host_out = get_soong_var('SOONG_HOST_OUT', target_release)
host_out = get_soong_var('HOST_OUT', target_release)
# Call the class to package the outputs.
# TODO(lucafarsi): Move this code into a replaceable class.
@ -267,12 +278,16 @@ def zip_build_outputs(modules_to_build: Set[Text], dist_dir: Text):
zip_command.append('-o')
zip_command.append(os.path.join(dist_dir, 'general-tests.zip'))
run_command(zip_command)
run_command(zip_command, print_output=True)
def get_soong_var(var: str) -> str:
def get_soong_var(var: str, target_release: str) -> str:
new_env = os.environ.copy()
new_env['TARGET_RELEASE'] = target_release
value = run_command(
['./build/soong/soong_ui.bash', '--dumpvar-mode', '--abs', var]
['./build/soong/soong_ui.bash', '--dumpvar-mode', '--abs', var],
env=new_env,
).strip()
if not value:
raise RuntimeError('Necessary soong variable ' + var + ' not found.')