1# SPDX-License-Identifier: GPL-2.0+
2# Copyright (c) 2014 Google, Inc
3#
4
5"""Handles parsing of buildman arguments
6
7This creates the argument parser and uses it to parse the arguments passed in
8"""
9
10import argparse
11import os
12import pathlib
13
14BUILDMAN_DIR = pathlib.Path(__file__).parent
15HAS_TESTS = os.path.exists(BUILDMAN_DIR / "test.py")
16
17def add_upto_m(parser):
18    """Add arguments up to 'M'
19
20    Args:
21        parser (ArgumentParser): Parse to add to
22
23    This is split out to avoid having too many statements in one function
24    """
25    parser.add_argument('-a', '--adjust-cfg', type=str, action='append',
26          help='Adjust the Kconfig settings in .config before building')
27    parser.add_argument('-A', '--print-prefix', action='store_true',
28          help='Print the tool-chain prefix for a board (CROSS_COMPILE=)')
29    parser.add_argument('-b', '--branch', type=str,
30          help='Branch name to build, or range of commits to build')
31    parser.add_argument('-B', '--bloat', dest='show_bloat',
32          action='store_true', default=False,
33          help='Show changes in function code size for each board')
34    parser.add_argument('--boards', type=str, action='append',
35          help='List of board names to build separated by comma')
36    parser.add_argument('-c', '--count', dest='count', type=int,
37          default=-1, help='Run build on the top n commits')
38    parser.add_argument('-C', '--force-reconfig', dest='force_reconfig',
39          action='store_true', default=False,
40          help='Reconfigure for every commit (disable incremental build)')
41    parser.add_argument('--config-only', action='store_true',
42                        default=False,
43                        help="Don't build, just configure each commit")
44    parser.add_argument('-d', '--detail', dest='show_detail',
45          action='store_true', default=False,
46          help='Show detailed size delta for each board in the -S summary')
47    parser.add_argument('-D', '--debug', action='store_true',
48        help='Enabling debugging (provides a full traceback on error)')
49    parser.add_argument('-e', '--show_errors', action='store_true',
50          default=False, help='Show errors and warnings')
51    parser.add_argument('-E', '--warnings-as-errors', action='store_true',
52          default=False, help='Treat all compiler warnings as errors')
53    parser.add_argument('-f', '--force-build', dest='force_build',
54          action='store_true', default=False,
55          help='Force build of boards even if already built')
56    parser.add_argument('-F', '--force-build-failures', dest='force_build_failures',
57          action='store_true', default=False,
58          help='Force build of previously-failed build')
59    parser.add_argument('--fetch-arch', type=str,
60          help="Fetch a toolchain for architecture FETCH_ARCH ('list' to list)."
61              ' You can also fetch several toolchains separate by comma, or'
62              " 'all' to download all")
63    parser.add_argument(
64          '--full-check', action='store_true',
65          help='Check maintainer entries and TARGET configs')
66    parser.add_argument('-g', '--git', type=str,
67          help='Git repo containing branch to build', default='.')
68    parser.add_argument('-G', '--config-file', type=str,
69          help='Path to buildman config file', default='')
70    parser.add_argument('-H', '--full-help', action='store_true', dest='full_help',
71          default=False, help='Display the README file')
72    parser.add_argument('-i', '--in-tree', dest='in_tree',
73          action='store_true', default=False,
74          help='Build in the source tree instead of a separate directory')
75    parser.add_argument('-I', '--ide', action='store_true', default=False,
76          help='Create build output that can be parsed by an IDE')
77    parser.add_argument('-j', '--jobs', dest='jobs', type=int,
78          default=None, help='Number of jobs to run at once (passed to make)')
79    parser.add_argument('-k', '--keep-outputs', action='store_true',
80          default=False, help='Keep all build output files (e.g. binaries)')
81    parser.add_argument('-K', '--show-config', action='store_true',
82          default=False,
83          help='Show configuration changes in summary (both board config files and Kconfig)')
84    parser.add_argument('--preserve-config-y', action='store_true',
85          default=False, help="Don't convert y to 1 in configs")
86    parser.add_argument('-l', '--list-error-boards', action='store_true',
87          default=False, help='Show a list of boards next to each error/warning')
88    parser.add_argument('-L', '--no-lto', action='store_true',
89          default=False, help='Disable Link-time Optimisation (LTO) for builds')
90    parser.add_argument('--list-tool-chains', action='store_true', default=False,
91          help='List available tool chains (use -v to see probing detail)')
92    parser.add_argument('-m', '--mrproper', action='store_true',
93          default=False, help="Run 'make mrproper before reconfiguring")
94    parser.add_argument(
95          '-M', '--allow-missing', action='store_true', default=False,
96          help='Tell binman to allow missing blobs and generate fake ones as needed')
97    parser.add_argument(
98          '--maintainer-check', action='store_true',
99          help='Check that maintainer entries exist for each board')
100    parser.add_argument(
101          '--no-allow-missing', action='store_true', default=False,
102          help='Disable telling binman to allow missing blobs')
103    parser.add_argument('-n', '--dry-run', action='store_true', dest='dry_run',
104          default=False, help="Do a dry run (describe actions, but do nothing)")
105    parser.add_argument('-N', '--no-subdirs', action='store_true', dest='no_subdirs',
106          default=False,
107          help="Don't create subdirectories when building current source for a single board")
108
109
110def add_after_m(parser):
111    """Add arguments after 'M'
112
113    Args:
114        parser (ArgumentParser): Parse to add to
115
116    This is split out to avoid having too many statements in one function
117    """
118    parser.add_argument('-o', '--output-dir', type=str, dest='output_dir',
119          help='Directory where all builds happen and buildman has its workspace (default is ../)')
120    parser.add_argument('-O', '--override-toolchain', type=str,
121          help="Override host toochain to use for sandbox (e.g. 'clang-7')")
122    parser.add_argument('-Q', '--quick', action='store_true',
123          default=False, help='Do a rough build, with limited warning resolution')
124    parser.add_argument('-p', '--full-path', action='store_true',
125          default=False, help="Use full toolchain path in CROSS_COMPILE")
126    parser.add_argument('-P', '--per-board-out-dir', action='store_true',
127          default=False, help="Use an O= (output) directory per board rather than per thread")
128    parser.add_argument('--print-arch', action='store_true',
129          default=False, help="Print the architecture for a board (ARCH=)")
130    parser.add_argument('-r', '--reproducible-builds', action='store_true',
131          help='Set SOURCE_DATE_EPOCH=0 to suuport a reproducible build')
132    parser.add_argument('-R', '--regen-board-list', type=str,
133          help='Force regeneration of the list of boards, like the old boards.cfg file')
134    parser.add_argument('-s', '--summary', action='store_true',
135          default=False, help='Show a build summary')
136    parser.add_argument('-S', '--show-sizes', action='store_true',
137          default=False, help='Show image size variation in summary')
138    parser.add_argument('--step', type=int,
139          default=1, help='Only build every n commits (0=just first and last)')
140    if HAS_TESTS:
141        parser.add_argument('--skip-net-tests', action='store_true', default=False,
142                          help='Skip tests which need the network')
143        parser.add_argument('-t', '--test', action='store_true', dest='test',
144                          default=False, help='run tests')
145        parser.add_argument('--coverage', action='store_true',
146                            help='Calculated test coverage')
147    parser.add_argument('-T', '--threads', type=int,
148          default=None,
149          help='Number of builder threads to use (0=single-thread)')
150    parser.add_argument('-u', '--show_unknown', action='store_true',
151          default=False, help='Show boards with unknown build result')
152    parser.add_argument('-U', '--show-environment', action='store_true',
153          default=False, help='Show environment changes in summary')
154    parser.add_argument('-v', '--verbose', action='store_true',
155          default=False, help='Show build results while the build progresses')
156    parser.add_argument('-V', '--verbose-build', action='store_true',
157          default=False, help='Run make with V=1, logging all output')
158    parser.add_argument('-w', '--work-in-output', action='store_true',
159          default=False, help='Use the output directory as the work directory')
160    parser.add_argument('-W', '--ignore-warnings', action='store_true',
161          default=False, help='Return success even if there are warnings')
162    parser.add_argument('-x', '--exclude', dest='exclude',
163          type=str, action='append',
164          help='Specify a list of boards to exclude, separated by comma')
165    parser.add_argument('-y', '--filter-dtb-warnings', action='store_true',
166          default=False,
167          help='Filter out device-tree-compiler warnings from output')
168    parser.add_argument('-Y', '--filter-migration-warnings', action='store_true',
169          default=False,
170          help='Filter out migration warnings from output')
171
172
173def parse_args():
174    """Parse command line arguments from sys.argv[]
175
176    Returns:
177        tuple containing:
178            options: command line options
179            args: command lin arguments
180    """
181    epilog = """ [list of target/arch/cpu/board/vendor/soc to build]
182
183    Build U-Boot for all commits in a branch. Use -n to do a dry run"""
184
185    parser = argparse.ArgumentParser(epilog=epilog)
186    add_upto_m(parser)
187    add_after_m(parser)
188    parser.add_argument('terms', type=str, nargs='*',
189                        help='Board / SoC names to build')
190
191    return parser.parse_args()
192