aboutsummaryrefslogtreecommitdiff
path: root/pw_build/py/pw_build/project_builder_argparse.py
blob: 18581eb6686cf206f4adb4af4e43485bd43141ad (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# Copyright 2022 The Pigweed Authors
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may not
# use this file except in compliance with the License. You may obtain a copy of
# the License at
#
#     https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations under
# the License.
"""Pigweed Project Builder Common argparse."""

import argparse
from pathlib import Path


def add_project_builder_arguments(
    parser: argparse.ArgumentParser,
) -> argparse.ArgumentParser:
    """Add ProjectBuilder.main specific arguments."""
    build_dir_group = parser.add_argument_group(
        title='Build Directory and Command Options'
    )
    build_dir_group.add_argument(
        '-C',
        '--build-directory',
        dest='build_directories',
        nargs='+',
        action='append',
        default=[],
        metavar=('directory', 'target'),
        help=(
            "Specify a build directory and optionally targets to "
            "build. `pw watch -C out target1 target2` is equivalent to 'ninja "
            "-C out taret1 target2'. The 'out' directory will be used if no "
            "others are provided."
        ),
    )

    build_dir_group.add_argument(
        'default_build_targets',
        nargs='*',
        metavar='target',
        default=[],
        help=(
            "Default build targets. For example if the build directory is "
            "'out' then, 'ninja -C out taret1 target2' will be run. To "
            "specify one or more directories, use the "
            "``-C / --build-directory`` option."
        ),
    )

    build_dir_group.add_argument(
        '--build-system-command',
        nargs=2,
        action='append',
        default=[],
        dest='build_system_commands',
        metavar=('directory', 'command'),
        help='Build system command for . Default: ninja',
    )

    build_dir_group.add_argument(
        '--run-command',
        action='append',
        default=[],
        help=(
            'Additional commands to run. These are run before any -C '
            'arguments and may be repeated. For example: '
            "--run-command 'bazel build //pw_cli/...'"
            "--run-command 'bazel test //pw_cli/...'"
            "-C out python.lint python.test"
        ),
    )

    build_options_group = parser.add_argument_group(
        title='Build Execution Options'
    )
    build_options_group.add_argument(
        '-j',
        '--jobs',
        type=int,
        help=(
            'Specify the number of cores to use for each build system.'
            'This is passed to ninja, bazel and make as "-j"'
        ),
    )
    build_options_group.add_argument(
        '-k',
        '--keep-going',
        action='store_true',
        help=(
            'Keep building past the first failure. This is equivalent to '
            'running "ninja -k 0" or "bazel build -k".'
        ),
    )
    build_options_group.add_argument(
        '--parallel',
        action='store_true',
        help='Run all builds in parallel.',
    )
    build_options_group.add_argument(
        '--parallel-workers',
        default=0,
        type=int,
        help=(
            'How many builds may run at the same time when --parallel is '
            'enabled. Default: 0 meaning run all in parallel.'
        ),
    )

    logfile_group = parser.add_argument_group(title='Log File Options')
    logfile_group.add_argument(
        '--logfile',
        type=Path,
        help='Global build output log file.',
    )

    logfile_group.add_argument(
        '--separate-logfiles',
        action='store_true',
        help='Create separate log files per build directory.',
    )

    logfile_group.add_argument(
        '--debug-logging',
        action='store_true',
        help='Enable Python build execution tool debug logging.',
    )

    output_group = parser.add_argument_group(title='Display Output Options')

    # TODO(b/248257406) Use argparse.BooleanOptionalAction when Python 3.8 is
    # no longer supported.
    output_group.add_argument(
        '--banners',
        action='store_true',
        default=True,
        help='Show pass/fail banners.',
    )
    output_group.add_argument(
        '--no-banners',
        action='store_false',
        dest='banners',
        help='Hide pass/fail banners.',
    )

    # TODO(b/248257406) Use argparse.BooleanOptionalAction when Python 3.8 is
    # no longer supported.
    output_group.add_argument(
        '--colors',
        action='store_true',
        default=True,
        help='Force color output from ninja.',
    )
    output_group.add_argument(
        '--no-colors',
        action='store_false',
        dest='colors',
        help="Don't force ninja to use color output.",
    )

    return parser