• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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    build_dir_group.add_argument(
28        '-C',
29        '--build-directory',
30        dest='build_directories',
31        nargs='+',
32        action='append',
33        default=[],
34        metavar=('directory', 'target'),
35        help=(
36            "Specify a build directory and optionally targets to "
37            "build. `pw watch -C out target1 target2` is equivalent to 'ninja "
38            "-C out taret1 target2'. The 'out' directory will be used if no "
39            "others are provided."
40        ),
41    )
42
43    build_dir_group.add_argument(
44        'default_build_targets',
45        nargs='*',
46        metavar='target',
47        default=[],
48        help=(
49            "Default build targets. For example if the build directory is "
50            "'out' then, 'ninja -C out taret1 target2' will be run. To "
51            "specify one or more directories, use the "
52            "``-C / --build-directory`` option."
53        ),
54    )
55
56    build_dir_group.add_argument(
57        '--build-system-command',
58        nargs=2,
59        action='append',
60        default=[],
61        dest='build_system_commands',
62        metavar=('directory', 'command'),
63        help='Build system command for . Default: ninja',
64    )
65
66    build_dir_group.add_argument(
67        '--run-command',
68        action='append',
69        default=[],
70        help=(
71            'Additional commands to run. These are run before any -C '
72            'arguments and may be repeated. For example: '
73            "--run-command 'bazel build //pw_cli/...'"
74            "--run-command 'bazel test //pw_cli/...'"
75            "-C out python.lint python.test"
76        ),
77    )
78
79    build_options_group = parser.add_argument_group(
80        title='Build Execution Options'
81    )
82    build_options_group.add_argument(
83        '-j',
84        '--jobs',
85        type=int,
86        help=(
87            'Specify the number of cores to use for each build system.'
88            'This is passed to ninja, bazel and make as "-j"'
89        ),
90    )
91    build_options_group.add_argument(
92        '-k',
93        '--keep-going',
94        action='store_true',
95        help=(
96            'Keep building past the first failure. This is equivalent to '
97            'running "ninja -k 0" or "bazel build -k".'
98        ),
99    )
100    build_options_group.add_argument(
101        '--parallel',
102        action='store_true',
103        help='Run all builds in parallel.',
104    )
105    build_options_group.add_argument(
106        '--parallel-workers',
107        default=0,
108        type=int,
109        help=(
110            'How many builds may run at the same time when --parallel is '
111            'enabled. Default: 0 meaning run all in parallel.'
112        ),
113    )
114
115    logfile_group = parser.add_argument_group(title='Log File Options')
116    logfile_group.add_argument(
117        '--logfile',
118        type=Path,
119        help='Global build output log file.',
120    )
121
122    logfile_group.add_argument(
123        '--separate-logfiles',
124        action='store_true',
125        help='Create separate log files per build directory.',
126    )
127
128    logfile_group.add_argument(
129        '--debug-logging',
130        action='store_true',
131        help='Enable Python build execution tool debug logging.',
132    )
133
134    output_group = parser.add_argument_group(title='Display Output Options')
135
136    # TODO(b/248257406) Use argparse.BooleanOptionalAction when Python 3.8 is
137    # no longer supported.
138    output_group.add_argument(
139        '--banners',
140        action='store_true',
141        default=True,
142        help='Show pass/fail banners.',
143    )
144    output_group.add_argument(
145        '--no-banners',
146        action='store_false',
147        dest='banners',
148        help='Hide pass/fail banners.',
149    )
150
151    # TODO(b/248257406) Use argparse.BooleanOptionalAction when Python 3.8 is
152    # no longer supported.
153    output_group.add_argument(
154        '--colors',
155        action='store_true',
156        default=True,
157        help='Force color output from ninja.',
158    )
159    output_group.add_argument(
160        '--no-colors',
161        action='store_false',
162        dest='colors',
163        help="Don't force ninja to use color output.",
164    )
165
166    return parser
167