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/>.
38 "malloc": "enable-malloc",
39 "pkgversion": "with-pkgversion",
40 "qemu_firmwarepath": "firmwarepath",
41 "trace_backends": "enable-trace-backends",
42 "trace_file": "with-trace-file",
62 # Convert the default value of an option to the string used in
64 def value_to_help(value):
65 if isinstance(value, list):
66 return ",".join(value)
67 if isinstance(value, bool):
68 return "enabled" if value else "disabled"
72 def wrap(left, text, indent):
74 if len(left) >= indent:
78 left = (left + spaces)[0:indent]
79 yield from textwrap.wrap(
80 text, width=LINE_WIDTH, initial_indent=left, subsequent_indent=spaces
84 def sh_print(line=""):
85 print(' printf "%s\\n"', shlex.quote(line))
88 def help_line(left, opt, indent, long):
89 right = f'{opt["description"]}'
91 value = value_to_help(opt["value"])
92 if value != "auto" and value != "":
93 right += f" [{value}]"
94 if "choices" in opt and long:
95 choices = "/".join(sorted(opt["choices"]))
96 right += f" (choices: {choices})"
97 for x in wrap(" " + left, right, indent):
101 # Return whether the option (a dictionary) can be used with
102 # arguments. Booleans can never be used with arguments;
103 # combos allow an argument only if they accept other values
104 # than "auto", "enabled", and "disabled".
106 if opt["type"] == "boolean":
108 if opt["type"] != "combo":
110 return not (set(opt["choices"]) <= {"auto", "disabled", "enabled"})
113 # Return whether the option (a dictionary) can be used without
114 # arguments. Booleans can only be used without arguments;
115 # combos require an argument if they accept neither "enabled"
117 def require_arg(opt):
118 if opt["type"] == "boolean":
120 if opt["type"] != "combo":
122 return not ({"enabled", "disabled"}.intersection(opt["choices"]))
125 def filter_options(json):
126 if ":" in json["name"]:
128 if json["section"] == "user":
129 return json["name"] not in SKIP_OPTIONS
131 return json["name"] in BUILTIN_OPTIONS
134 def load_options(json):
135 json = [x for x in json if filter_options(x)]
136 return sorted(json, key=lambda x: x["name"])
141 if name in OPTION_NAMES:
142 return OPTION_NAMES[name]
143 return name.replace("_", "-")
146 def cli_help_key(opt):
147 key = cli_option(opt)
150 if opt["type"] == "boolean" and opt["value"]:
151 return f"disable-{key}"
152 return f"enable-{key}"
155 def cli_metavar(opt):
156 if opt["type"] == "string":
158 if opt["type"] == "array":
163 def print_help(options):
164 print("meson_options_help() {")
165 for opt in sorted(options, key=cli_help_key):
166 key = cli_help_key(opt)
167 # The first section includes options that have an arguments,
168 # and booleans (i.e., only one of enable/disable makes sense)
170 metavar = cli_metavar(opt)
171 left = f"--{key}={metavar}"
172 help_line(left, opt, 27, True)
173 elif opt["type"] == "boolean":
175 help_line(left, opt, 27, False)
177 if opt["type"] == "combo" and "enabled" in opt["choices"]:
178 left = f"--{key}[=CHOICE]"
180 left = f"--{key}=CHOICE"
181 help_line(left, opt, 27, True)
184 sh_print("Optional features, enabled with --enable-FEATURE and")
185 sh_print("disabled with --disable-FEATURE, default is enabled if available")
186 sh_print("(unless built with --without-default-features):")
189 key = opt["name"].replace("_", "-")
190 if opt["type"] != "boolean" and not allow_arg(opt):
191 help_line(key, opt, 18, False)
195 def print_parse(options):
196 print("_meson_option_parse() {")
199 key = cli_option(opt)
202 print(f' --{key}=*) quote_sh "-D{name}=$2" ;;')
203 elif opt["type"] == "boolean":
204 print(f' --enable-{key}) printf "%s" -D{name}=true ;;')
205 print(f' --disable-{key}) printf "%s" -D{name}=false ;;')
207 if opt["type"] == "combo" and "enabled" in opt["choices"]:
208 print(f' --enable-{key}) printf "%s" -D{name}=enabled ;;')
209 if opt["type"] == "combo" and "disabled" in opt["choices"]:
210 print(f' --disable-{key}) printf "%s" -D{name}=disabled ;;')
212 print(f' --enable-{key}=*) quote_sh "-D{name}=$2" ;;')
213 print(" *) return 1 ;;")
218 options = load_options(json.load(sys.stdin))
219 print("# This file is generated by meson-buildoptions.py, do not edit!")