1 #! /usr/bin/env python3
3 # Generate configure command line options handling code, based on Meson's
4 # user build options introspection data
6 # Copyright (C) 2021 Red Hat, Inc.
8 # Author: Paolo Bonzini <pbonzini@redhat.com>
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)
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.
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/>.
28 # Options with nonstandard names (e.g. --with/--without) or OS-dependent
29 # defaults. Try not to add any.
35 # Options whose name doesn't match the option for backwards compatibility
36 # reasons, because Meson gives them a funny name, or both
40 "coroutine_backend": "with-coroutine",
41 "debug": "debug-info",
42 "malloc": "enable-malloc",
43 "pkgversion": "with-pkgversion",
44 "qemu_firmwarepath": "firmwarepath",
45 "qemu_suffix": "with-suffix",
46 "trace_backends": "enable-trace-backends",
47 "trace_file": "with-trace-file",
50 # Options that configure autodetects, even though meson defines them as boolean
56 # 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.
81 # Convert the default value of an option to the string used in
84 if opt["name"] == "libdir":
85 return 'system default'
87 if isinstance(value, list):
88 return ",".join(value)
89 if isinstance(value, bool):
90 return "enabled" if value else "disabled"
94 def wrap(left, text, indent):
96 if len(left) >= indent:
100 left = (left + spaces)[0:indent]
101 yield from textwrap.wrap(
102 text, width=LINE_WIDTH, initial_indent=left, subsequent_indent=spaces
106 def sh_print(line=""):
107 print(' printf "%s\\n"', shlex.quote(line))
110 def help_line(left, opt, indent, long):
111 right = f'{opt["description"]}'
113 value = get_help(opt)
114 if value != "auto" and value != "":
115 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):
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".
128 if opt["type"] == "boolean":
130 if opt["type"] != "combo":
132 return not (set(opt["choices"]) <= {"auto", "disabled", "enabled"})
135 # 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"
139 def require_arg(opt):
140 if opt["type"] == "boolean":
142 if opt["type"] != "combo":
144 return not ({"enabled", "disabled"}.intersection(opt["choices"]))
147 def filter_options(json):
148 if ":" in json["name"]:
150 if json["section"] == "user":
151 return json["name"] not in SKIP_OPTIONS
153 return json["name"] in BUILTIN_OPTIONS
156 def load_options(json):
157 json = [x for x in json if filter_options(x)]
158 return sorted(json, key=lambda x: x["name"])
163 if name in OPTION_NAMES:
164 return OPTION_NAMES[name]
165 return name.replace("_", "-")
168 def cli_help_key(opt):
169 key = cli_option(opt)
172 if opt["type"] == "boolean" and opt["value"]:
173 return f"disable-{key}"
174 return f"enable-{key}"
177 def cli_metavar(opt):
178 if opt["type"] == "string":
180 if opt["type"] == "array":
181 return "CHOICES" if "choices" in opt else "VALUES"
185 def print_help(options):
186 print("meson_options_help() {")
188 for opt in sorted(options, key=cli_help_key):
189 key = cli_help_key(opt)
190 # The first section includes options that have an arguments,
191 # and booleans (i.e., only one of enable/disable makes sense)
193 metavar = cli_metavar(opt)
194 left = f"--{key}={metavar}"
195 help_line(left, opt, 27, True)
196 elif opt["type"] == "boolean" and opt["name"] not in AUTO_OPTIONS:
198 help_line(left, opt, 27, False)
200 if opt["type"] == "combo" and "enabled" in opt["choices"]:
201 left = f"--{key}[=CHOICE]"
203 left = f"--{key}=CHOICE"
204 help_line(left, opt, 27, True)
206 feature_opts.append(opt)
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):")
213 for opt in sorted(feature_opts, key=cli_option):
214 key = cli_option(opt)
215 help_line(key, opt, 18, False)
219 def print_parse(options):
220 print("_meson_option_parse() {")
223 key = cli_option(opt)
226 if opt["type"] == "array" and not "choices" in opt:
227 print(f' --{key}=*) quote_sh "-D{name}=$(meson_option_build_array $2)" ;;')
229 print(f' --{key}=*) quote_sh "-D{name}=$2" ;;')
230 elif opt["type"] == "boolean":
231 print(f' --enable-{key}) printf "%s" -D{name}=true ;;')
232 print(f' --disable-{key}) printf "%s" -D{name}=false ;;')
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 ;;')
239 print(f' --enable-{key}=*) quote_sh "-D{name}=$2" ;;')
240 print(" *) return 1 ;;")
245 options = load_options(json.load(sys.stdin))
246 print("# This file is generated by meson-buildoptions.py, do not edit!")