xref: /aosp_15_r20/tools/netsim/scripts/build_tools.py (revision cf78ab8cffb8fc9207af348f23af247fb04370a6)
1#!/usr/bin/env python3
2#
3# Copyright 2023 - The Android Open Source Project
4#
5# Licensed under the Apache License, Version 2.0 (the',  help="License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9#     http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an',  help="AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16from __future__ import absolute_import, division, print_function
17
18import argparse
19import logging
20import os
21from pathlib import Path
22
23from environment import get_default_environment
24from server_config import ServerConfig
25from tasks import (
26    TASK_LIST,
27    get_tasks,
28    log_enabled_tasks,
29)
30from utils import (
31    AOSP_ROOT,
32    config_logging,
33    create_emulator_artifact_path,
34    default_target,
35    fetch_build_chaining_artifacts,
36    is_presubmit,
37    log_system_info,
38)
39
40
41def main():
42  config_logging()
43  log_system_info()
44  create_emulator_artifact_path()
45
46  parser = argparse.ArgumentParser(
47      description=(
48          "Configures the android netsim cmake project so it can be build"
49      )
50  )
51  parser.add_argument(
52      "--out_dir",
53      type=str,
54      default="tools/netsim/objs/",
55      help="The output directory",
56  )
57  parser.add_argument(
58      "--dist_dir", type=str, default="dist/", help="The destination directory"
59  )
60  parser.add_argument(
61      "--build-id",
62      type=str,
63      default="",
64      dest="build_id",
65      help="The netsim build number",
66  )
67  parser.add_argument(
68      "--target",
69      type=str,
70      default=default_target(),
71      help="The build target, defaults to current os",
72  )
73  parser.add_argument(
74      "--enable_system_rust",
75      action="store_true",
76      help="Build the netsim with the System Rust on the host machine",
77  )
78  parser.add_argument(
79      "--with_debug", action="store_true", help="Build debug instead of release"
80  )
81  parser.add_argument(
82      "--buildbot", action="store_true", help="Invoked by Android buildbots"
83  )
84  parser.add_argument(
85      "--task",
86      nargs="+",
87      type=str.lower,
88      choices=[choice.lower() for choice in TASK_LIST],
89      help=(
90          "Tasks to perform (Configure, Compile, CompileInstall,"
91          " InstallEmulator, RunPyTest, LocalRunAll)"
92      ),
93  )
94  parser.add_argument(
95      "--config",
96      default="release",
97      choices=["debug", "release"],
98      help="Whether we are building a release or debug configuration.",
99  )
100  parser.add_argument(
101      "--emulator_target",
102      type=str,
103      default="emulator-linux_x64",
104      help=(
105          "The emulator build target to install for local case, defaults to"
106          " emulator-linux_x64"
107      ),
108  )
109  parser.add_argument(
110      "--local_emulator_dir",
111      type=str,
112      default="",
113      help=(
114          "For providing an emulator build artifact in a directory."
115          " This will install the emulator from local_emulator_dir instead of"
116          " fetching the artifacts"
117      ),
118  )
119  parser.add_argument(
120      "--pytest_input_dir",
121      type=str,
122      default="",
123      help=(
124          "For providing netsim & emulator binaries and libraries for pytest."
125          " This will allow pytest to be run on directory path specified on"
126          " pytest_input_dir"
127      ),
128  )
129
130  args = parser.parse_args()
131
132  presubmit = is_presubmit(args.build_id)
133
134  # The environment of build
135  env = get_default_environment(AOSP_ROOT)
136  if args.buildbot:
137    cfg = ServerConfig(presubmit, args)
138    env = cfg.get_env()
139
140  # Set Environment Variables
141  os.environ["GIT_DISCOVERY_ACROSS_FILESYSTEM"] = "1"
142  if not args.buildbot:
143    # Able to config C++ file in vscode.
144    os.environ["CMAKE_EXPORT_COMPILE_COMMANDS"] = "1"
145
146  # Provide absolute path for args.out_dir
147  if not os.path.isabs(args.out_dir):
148    args.out_dir = os.path.join(AOSP_ROOT, args.out_dir)
149
150  # Build preparation work for buildbot
151  if args.buildbot:
152    # Fetch Emulator Artifacts
153    fetch_build_chaining_artifacts(args.out_dir, presubmit)
154    # Set the out_dir to "out/objs"
155    args.out_dir = Path(args.out_dir) / "objs"
156
157  # Obtain tasks
158  tasks = get_tasks(args, env)
159
160  # Log enabled tasks
161  log_enabled_tasks(tasks)
162
163  # Turn on sccache?
164  # if args.buildbot and cfg.sccache:
165  #    launcher.append(f"-DOPTION_CCACHE=${cfg.sccache}")
166
167  # Configure
168  tasks.get("Configure").run()
169
170  # Build
171  tasks.get("Compile").run()
172
173  # Install
174  tasks.get("CompileInstall").run()
175
176  # Run Tests
177  tasks.get("RunTest").run()
178
179  # Zip results..
180  tasks.get("ZipArtifact").run()
181
182  # Install Emulator artifacts and Run PyTests
183  tasks.get("InstallEmulator").run()
184  tasks.get("RunPyTest").run()
185
186
187if __name__ == "__main__":
188  main()
189