blob: a3e22471b2fdc2d89bf99fe5f755b65213ff7b28 [file] [log] [blame]
Paolo Bonzini61d63092021-10-07 15:08:28 +02001#! /usr/bin/env python3
2
3# Generate configure command line options handling code, based on Meson's
4# user build options introspection data
5#
6# Copyright (C) 2021 Red Hat, Inc.
7#
8# Author: Paolo Bonzini <pbonzini@redhat.com>
9#
10# This program is free software; you can redistribute it and/or modify
11# it under the terms of the GNU General Public License as published by
12# the Free Software Foundation; either version 2, or (at your option)
13# any later version.
14#
15# This program is distributed in the hope that it will be useful,
16# but WITHOUT ANY WARRANTY; without even the implied warranty of
17# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18# GNU General Public License for more details.
19#
20# You should have received a copy of the GNU General Public License
21# along with this program. If not, see <https://www.gnu.org/licenses/>.
22
23import json
24import textwrap
25import shlex
26import sys
27
Paolo Bonzini79fccf72023-09-28 11:20:01 +020028# Options with nonstandard names (e.g. --with/--without) or OS-dependent
29# defaults. Try not to add any.
Paolo Bonzini3b4da132021-10-07 15:08:29 +020030SKIP_OPTIONS = {
Paolo Bonzini3b4da132021-10-07 15:08:29 +020031 "default_devices",
Paolo Bonzini3b4da132021-10-07 15:08:29 +020032 "fuzzing_engine",
Paolo Bonzini3b4da132021-10-07 15:08:29 +020033}
34
Paolo Bonzini79fccf72023-09-28 11:20:01 +020035# Options whose name doesn't match the option for backwards compatibility
36# reasons, because Meson gives them a funny name, or both
Paolo Bonzini119fc612022-04-20 17:33:48 +020037OPTION_NAMES = {
Paolo Bonzinic54b59e2022-04-20 17:33:58 +020038 "b_coverage": "gcov",
39 "b_lto": "lto",
Paolo Bonzini67398252022-10-12 13:19:35 +020040 "coroutine_backend": "with-coroutine",
Paolo Bonzinic0e705c2023-05-11 09:38:53 +020041 "debug": "debug-info",
Paolo Bonzini119fc612022-04-20 17:33:48 +020042 "malloc": "enable-malloc",
Paolo Bonzinib0b43232022-04-20 17:33:54 +020043 "pkgversion": "with-pkgversion",
Paolo Bonzinic09c1ce2022-04-20 17:33:57 +020044 "qemu_firmwarepath": "firmwarepath",
Paolo Bonzinic36dd412023-10-16 08:18:08 +020045 "qemu_suffix": "with-suffix",
Paolo Bonzini119fc612022-04-20 17:33:48 +020046 "trace_backends": "enable-trace-backends",
Paolo Bonzini4fda6012022-04-20 17:33:51 +020047 "trace_file": "with-trace-file",
Paolo Bonzini119fc612022-04-20 17:33:48 +020048}
49
Paolo Bonzini39fb3cf2023-09-18 11:06:48 +020050# Options that configure autodetects, even though meson defines them as boolean
51AUTO_OPTIONS = {
52 "plugins",
Paolo Bonzini090a1882023-10-16 08:20:13 +020053 "werror",
Paolo Bonzini39fb3cf2023-09-18 11:06:48 +020054}
55
Paolo Bonzini79fccf72023-09-28 11:20:01 +020056# Builtin options that should be definable via configure. Some of the others
57# we really do not want (e.g. c_args is defined via the native file, not
58# via -D, because it's a mix of CFLAGS and --extra-cflags); for specific
59# cases "../configure -D" can be used as an escape hatch.
Paolo Bonzinia70248d2021-11-09 10:36:39 +010060BUILTIN_OPTIONS = {
Paolo Bonzinic54b59e2022-04-20 17:33:58 +020061 "b_coverage",
62 "b_lto",
Paolo Bonzinic36dd412023-10-16 08:18:08 +020063 "bindir",
Paolo Bonzinic09c1ce2022-04-20 17:33:57 +020064 "datadir",
Paolo Bonzinic0e705c2023-05-11 09:38:53 +020065 "debug",
Paolo Bonzinic09c1ce2022-04-20 17:33:57 +020066 "includedir",
67 "libdir",
68 "libexecdir",
69 "localedir",
70 "localstatedir",
71 "mandir",
Paolo Bonzinic36dd412023-10-16 08:18:08 +020072 "prefix",
Paolo Bonzinia70248d2021-11-09 10:36:39 +010073 "strip",
Paolo Bonzinic09c1ce2022-04-20 17:33:57 +020074 "sysconfdir",
Paolo Bonzini090a1882023-10-16 08:20:13 +020075 "werror",
Paolo Bonzinia70248d2021-11-09 10:36:39 +010076}
77
Paolo Bonzini3b4da132021-10-07 15:08:29 +020078LINE_WIDTH = 76
79
80
81# Convert the default value of an option to the string used in
82# the help message
Paolo Bonzini808d15b2023-02-06 13:32:32 +010083def get_help(opt):
84 if opt["name"] == "libdir":
85 return 'system default'
86 value = opt["value"]
Paolo Bonzini3b4da132021-10-07 15:08:29 +020087 if isinstance(value, list):
88 return ",".join(value)
89 if isinstance(value, bool):
90 return "enabled" if value else "disabled"
91 return str(value)
92
93
94def wrap(left, text, indent):
95 spaces = " " * indent
96 if len(left) >= indent:
97 yield left
98 left = spaces
99 else:
100 left = (left + spaces)[0:indent]
101 yield from textwrap.wrap(
102 text, width=LINE_WIDTH, initial_indent=left, subsequent_indent=spaces
103 )
104
105
Paolo Bonzini61d63092021-10-07 15:08:28 +0200106def sh_print(line=""):
107 print(' printf "%s\\n"', shlex.quote(line))
108
109
Paolo Bonzini3b4da132021-10-07 15:08:29 +0200110def help_line(left, opt, indent, long):
111 right = f'{opt["description"]}'
112 if long:
Paolo Bonzini808d15b2023-02-06 13:32:32 +0100113 value = get_help(opt)
Paolo Bonzini119fc612022-04-20 17:33:48 +0200114 if value != "auto" and value != "":
Paolo Bonzini3b4da132021-10-07 15:08:29 +0200115 right += f" [{value}]"
116 if "choices" in opt and long:
117 choices = "/".join(sorted(opt["choices"]))
118 right += f" (choices: {choices})"
119 for x in wrap(" " + left, right, indent):
120 sh_print(x)
121
122
123# Return whether the option (a dictionary) can be used with
124# arguments. Booleans can never be used with arguments;
125# combos allow an argument only if they accept other values
126# than "auto", "enabled", and "disabled".
127def allow_arg(opt):
128 if opt["type"] == "boolean":
129 return False
130 if opt["type"] != "combo":
131 return True
132 return not (set(opt["choices"]) <= {"auto", "disabled", "enabled"})
133
134
Paolo Bonzini119fc612022-04-20 17:33:48 +0200135# Return whether the option (a dictionary) can be used without
136# arguments. Booleans can only be used without arguments;
137# combos require an argument if they accept neither "enabled"
138# nor "disabled"
139def require_arg(opt):
140 if opt["type"] == "boolean":
141 return False
142 if opt["type"] != "combo":
143 return True
144 return not ({"enabled", "disabled"}.intersection(opt["choices"]))
145
146
Paolo Bonzinia70248d2021-11-09 10:36:39 +0100147def filter_options(json):
148 if ":" in json["name"]:
149 return False
150 if json["section"] == "user":
151 return json["name"] not in SKIP_OPTIONS
152 else:
153 return json["name"] in BUILTIN_OPTIONS
154
155
Paolo Bonzini61d63092021-10-07 15:08:28 +0200156def load_options(json):
Paolo Bonzinia70248d2021-11-09 10:36:39 +0100157 json = [x for x in json if filter_options(x)]
Paolo Bonzini61d63092021-10-07 15:08:28 +0200158 return sorted(json, key=lambda x: x["name"])
159
160
Paolo Bonzini119fc612022-04-20 17:33:48 +0200161def cli_option(opt):
162 name = opt["name"]
163 if name in OPTION_NAMES:
164 return OPTION_NAMES[name]
165 return name.replace("_", "-")
166
167
168def cli_help_key(opt):
169 key = cli_option(opt)
170 if require_arg(opt):
171 return key
172 if opt["type"] == "boolean" and opt["value"]:
173 return f"disable-{key}"
174 return f"enable-{key}"
175
176
177def cli_metavar(opt):
178 if opt["type"] == "string":
179 return "VALUE"
180 if opt["type"] == "array":
Akihiko Odaki8154f5e2022-06-25 00:40:42 +0900181 return "CHOICES" if "choices" in opt else "VALUES"
Paolo Bonzini119fc612022-04-20 17:33:48 +0200182 return "CHOICE"
183
184
Paolo Bonzini61d63092021-10-07 15:08:28 +0200185def print_help(options):
186 print("meson_options_help() {")
Paolo Bonzini39fb3cf2023-09-18 11:06:48 +0200187 feature_opts = []
Paolo Bonzini119fc612022-04-20 17:33:48 +0200188 for opt in sorted(options, key=cli_help_key):
189 key = cli_help_key(opt)
Paolo Bonzini3b4da132021-10-07 15:08:29 +0200190 # The first section includes options that have an arguments,
191 # and booleans (i.e., only one of enable/disable makes sense)
Paolo Bonzini119fc612022-04-20 17:33:48 +0200192 if require_arg(opt):
193 metavar = cli_metavar(opt)
194 left = f"--{key}={metavar}"
195 help_line(left, opt, 27, True)
Paolo Bonzini39fb3cf2023-09-18 11:06:48 +0200196 elif opt["type"] == "boolean" and opt["name"] not in AUTO_OPTIONS:
Paolo Bonzini119fc612022-04-20 17:33:48 +0200197 left = f"--{key}"
Paolo Bonzini3b4da132021-10-07 15:08:29 +0200198 help_line(left, opt, 27, False)
199 elif allow_arg(opt):
200 if opt["type"] == "combo" and "enabled" in opt["choices"]:
Paolo Bonzini119fc612022-04-20 17:33:48 +0200201 left = f"--{key}[=CHOICE]"
Paolo Bonzini3b4da132021-10-07 15:08:29 +0200202 else:
Paolo Bonzini119fc612022-04-20 17:33:48 +0200203 left = f"--{key}=CHOICE"
Paolo Bonzini3b4da132021-10-07 15:08:29 +0200204 help_line(left, opt, 27, True)
Paolo Bonzini39fb3cf2023-09-18 11:06:48 +0200205 else:
206 feature_opts.append(opt)
Paolo Bonzini3b4da132021-10-07 15:08:29 +0200207
Paolo Bonzini61d63092021-10-07 15:08:28 +0200208 sh_print()
209 sh_print("Optional features, enabled with --enable-FEATURE and")
210 sh_print("disabled with --disable-FEATURE, default is enabled if available")
211 sh_print("(unless built with --without-default-features):")
212 sh_print()
Paolo Bonzini39fb3cf2023-09-18 11:06:48 +0200213 for opt in sorted(feature_opts, key=cli_option):
214 key = cli_option(opt)
215 help_line(key, opt, 18, False)
Paolo Bonzini61d63092021-10-07 15:08:28 +0200216 print("}")
217
218
219def print_parse(options):
220 print("_meson_option_parse() {")
221 print(" case $1 in")
Paolo Bonzini3b4da132021-10-07 15:08:29 +0200222 for opt in options:
Paolo Bonzini119fc612022-04-20 17:33:48 +0200223 key = cli_option(opt)
Paolo Bonzini3b4da132021-10-07 15:08:29 +0200224 name = opt["name"]
Paolo Bonzini119fc612022-04-20 17:33:48 +0200225 if require_arg(opt):
Akihiko Odaki8154f5e2022-06-25 00:40:42 +0900226 if opt["type"] == "array" and not "choices" in opt:
227 print(f' --{key}=*) quote_sh "-D{name}=$(meson_option_build_array $2)" ;;')
228 else:
229 print(f' --{key}=*) quote_sh "-D{name}=$2" ;;')
Paolo Bonzini119fc612022-04-20 17:33:48 +0200230 elif opt["type"] == "boolean":
Paolo Bonzini3b4da132021-10-07 15:08:29 +0200231 print(f' --enable-{key}) printf "%s" -D{name}=true ;;')
232 print(f' --disable-{key}) printf "%s" -D{name}=false ;;')
233 else:
234 if opt["type"] == "combo" and "enabled" in opt["choices"]:
235 print(f' --enable-{key}) printf "%s" -D{name}=enabled ;;')
236 if opt["type"] == "combo" and "disabled" in opt["choices"]:
237 print(f' --disable-{key}) printf "%s" -D{name}=disabled ;;')
238 if allow_arg(opt):
239 print(f' --enable-{key}=*) quote_sh "-D{name}=$2" ;;')
Paolo Bonzini61d63092021-10-07 15:08:28 +0200240 print(" *) return 1 ;;")
241 print(" esac")
242 print("}")
243
Nabih Estefancff666a2025-02-27 18:04:54 +0000244json_data = sys.stdin.read()
245try:
246 options = load_options(json.loads(json_data))
247except:
248 print("Failure in scripts/meson-buildoptions.py parsing stdin as json",
249 file=sys.stderr)
250 print(json_data, file=sys.stderr)
251 sys.exit(1)
Paolo Bonzini61d63092021-10-07 15:08:28 +0200252print("# This file is generated by meson-buildoptions.py, do not edit!")
253print_help(options)
254print_parse(options)