xref: /aosp_15_r20/external/pigweed/pw_build/py/pw_build/project_builder_argparse.py (revision 61c4878ac05f98d0ceed94b57d316916de578985)
1# Copyright 2022 The Pigweed Authors
2#
3# Licensed under the Apache License, Version 2.0 (the "License"); you may not
4# use this file except in compliance with the License. You may obtain a copy of
5# the License at
6#
7#     https://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12# License for the specific language governing permissions and limitations under
13# the License.
14"""Pigweed Project Builder Common argparse."""
15
16import argparse
17from pathlib import Path
18
19
20def add_project_builder_arguments(
21    parser: argparse.ArgumentParser,
22) -> argparse.ArgumentParser:
23    """Add ProjectBuilder.main specific arguments."""
24    build_dir_group = parser.add_argument_group(
25        title='Build Directory and Command Options'
26    )
27
28    build_dir_group.add_argument(
29        '-C',
30        '--build-directory',
31        dest='build_directories',
32        nargs='+',
33        action='append',
34        default=[],
35        metavar=('directory', 'target'),
36        help=(
37            "Specify a build directory and optionally targets to "
38            "build. `pw watch -C out target1 target2` is equivalent to 'ninja "
39            "-C out taret1 target2'. The 'out' directory will be used if no "
40            "others are provided."
41        ),
42    )
43
44    build_dir_group.add_argument(
45        'default_build_targets',
46        nargs='*',
47        metavar='target',
48        default=[],
49        help=(
50            "Default build targets. For example if the build directory is "
51            "'out' then, 'ninja -C out taret1 target2' will be run. To "
52            "specify one or more directories, use the "
53            "``-C / --build-directory`` option."
54        ),
55    )
56
57    build_dir_group.add_argument(
58        '--build-system-command',
59        nargs=2,
60        action='append',
61        default=[],
62        dest='build_system_commands',
63        metavar=('directory', 'command'),
64        help='Build system command for . Default: ninja',
65    )
66
67    build_dir_group.add_argument(
68        '--run-command',
69        action='append',
70        default=[],
71        help=(
72            'Additional commands to run. These are run before any -C '
73            'arguments and may be repeated. For example: '
74            "--run-command 'bazel build //pw_cli/...' "
75            "--run-command 'bazel test //pw_cli/...' "
76            "-C out python.lint python.test"
77        ),
78    )
79    build_dir_group.add_argument(
80        '-S',
81        '--source-path',
82        type=Path,
83        help=(
84            'Path to the root of the source files. Defaults to the'
85            'current working directory. If running under bazel this'
86            'will be set to the $BUILD_WORKSPACE_DIRECTORY'
87            'environment variable.'
88        ),
89    )
90    build_dir_group.add_argument(
91        '--default-build-system',
92        choices=['ninja', 'bazel'],
93        help=(
94            'Build system to use when no build directories or build targets '
95            'are specified on the command line.'
96        ),
97    )
98
99    build_options_group = parser.add_argument_group(
100        title='Build Execution Options'
101    )
102    build_options_group.add_argument(
103        '-j',
104        '--jobs',
105        type=int,
106        help=(
107            'Specify the number of cores to use for each build system. '
108            'This is passed to ninja, bazel and make as "-j"'
109        ),
110    )
111    build_options_group.add_argument(
112        '-k',
113        '--keep-going',
114        action='store_true',
115        help=(
116            'Keep building past the first failure. This is equivalent to '
117            'running "ninja -k 0" or "bazel build -k".'
118        ),
119    )
120    build_options_group.add_argument(
121        '--parallel',
122        action='store_true',
123        help='Run all builds in parallel.',
124    )
125    build_options_group.add_argument(
126        '--parallel-workers',
127        default=0,
128        type=int,
129        help=(
130            'How many builds may run at the same time when --parallel is '
131            'enabled. Default: 0 meaning run all in parallel.'
132        ),
133    )
134
135    logfile_group = parser.add_argument_group(title='Log File Options')
136    logfile_group.add_argument(
137        '--logfile',
138        type=Path,
139        help='Global build output log file.',
140    )
141
142    logfile_group.add_argument(
143        '--separate-logfiles',
144        action='store_true',
145        help='Create separate log files per build directory.',
146    )
147
148    logfile_group.add_argument(
149        '--debug-logging',
150        action='store_true',
151        help='Enable Python build execution tool debug logging.',
152    )
153
154    output_group = parser.add_argument_group(title='Display Output Options')
155
156    output_group.add_argument(
157        '--banners',
158        action=argparse.BooleanOptionalAction,
159        default=True,
160        help='Show pass/fail banners.',
161    )
162
163    output_group.add_argument(
164        '--colors',
165        action=argparse.BooleanOptionalAction,
166        default=True,
167        help='Force color output from ninja.',
168    )
169
170    return parser
171