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 "coroutine_backend": "with-coroutine",
39 "debug": "debug-info",
40 "malloc": "enable-malloc",
41 "pkgversion": "with-pkgversion",
42 "qemu_firmwarepath": "firmwarepath",
43 "trace_backends": "enable-trace-backends",
44 "trace_file": "with-trace-file",
47 # Options that configure autodetects, even though meson defines them as boolean
70 # Convert the default value of an option to the string used in
73 if opt
["name"] == "libdir":
74 return 'system default'
76 if isinstance(value
, list):
77 return ",".join(value
)
78 if isinstance(value
, bool):
79 return "enabled" if value
else "disabled"
83 def wrap(left
, text
, indent
):
85 if len(left
) >= indent
:
89 left
= (left
+ spaces
)[0:indent
]
90 yield from textwrap
.wrap(
91 text
, width
=LINE_WIDTH
, initial_indent
=left
, subsequent_indent
=spaces
95 def sh_print(line
=""):
96 print(' printf "%s\\n"', shlex
.quote(line
))
99 def help_line(left
, opt
, indent
, long):
100 right
= f
'{opt["description"]}'
102 value
= get_help(opt
)
103 if value
!= "auto" and value
!= "":
104 right
+= f
" [{value}]"
105 if "choices" in opt
and long:
106 choices
= "/".join(sorted(opt
["choices"]))
107 right
+= f
" (choices: {choices})"
108 for x
in wrap(" " + left
, right
, indent
):
112 # Return whether the option (a dictionary) can be used with
113 # arguments. Booleans can never be used with arguments;
114 # combos allow an argument only if they accept other values
115 # than "auto", "enabled", and "disabled".
117 if opt
["type"] == "boolean":
119 if opt
["type"] != "combo":
121 return not (set(opt
["choices"]) <= {"auto", "disabled", "enabled"})
124 # Return whether the option (a dictionary) can be used without
125 # arguments. Booleans can only be used without arguments;
126 # combos require an argument if they accept neither "enabled"
128 def require_arg(opt
):
129 if opt
["type"] == "boolean":
131 if opt
["type"] != "combo":
133 return not ({"enabled", "disabled"}.intersection(opt
["choices"]))
136 def filter_options(json
):
137 if ":" in json
["name"]:
139 if json
["section"] == "user":
140 return json
["name"] not in SKIP_OPTIONS
142 return json
["name"] in BUILTIN_OPTIONS
145 def load_options(json
):
146 json
= [x
for x
in json
if filter_options(x
)]
147 return sorted(json
, key
=lambda x
: x
["name"])
152 if name
in OPTION_NAMES
:
153 return OPTION_NAMES
[name
]
154 return name
.replace("_", "-")
157 def cli_help_key(opt
):
158 key
= cli_option(opt
)
161 if opt
["type"] == "boolean" and opt
["value"]:
162 return f
"disable-{key}"
163 return f
"enable-{key}"
166 def cli_metavar(opt
):
167 if opt
["type"] == "string":
169 if opt
["type"] == "array":
170 return "CHOICES" if "choices" in opt
else "VALUES"
174 def print_help(options
):
175 print("meson_options_help() {")
177 for opt
in sorted(options
, key
=cli_help_key
):
178 key
= cli_help_key(opt
)
179 # The first section includes options that have an arguments,
180 # and booleans (i.e., only one of enable/disable makes sense)
182 metavar
= cli_metavar(opt
)
183 left
= f
"--{key}={metavar}"
184 help_line(left
, opt
, 27, True)
185 elif opt
["type"] == "boolean" and opt
["name"] not in AUTO_OPTIONS
:
187 help_line(left
, opt
, 27, False)
189 if opt
["type"] == "combo" and "enabled" in opt
["choices"]:
190 left
= f
"--{key}[=CHOICE]"
192 left
= f
"--{key}=CHOICE"
193 help_line(left
, opt
, 27, True)
195 feature_opts
.append(opt
)
198 sh_print("Optional features, enabled with --enable-FEATURE and")
199 sh_print("disabled with --disable-FEATURE, default is enabled if available")
200 sh_print("(unless built with --without-default-features):")
202 for opt
in sorted(feature_opts
, key
=cli_option
):
203 key
= cli_option(opt
)
204 help_line(key
, opt
, 18, False)
208 def print_parse(options
):
209 print("_meson_option_parse() {")
212 key
= cli_option(opt
)
215 if opt
["type"] == "array" and not "choices" in opt
:
216 print(f
' --{key}=*) quote_sh "-D{name}=$(meson_option_build_array $2)" ;;')
218 print(f
' --{key}=*) quote_sh "-D{name}=$2" ;;')
219 elif opt
["type"] == "boolean":
220 print(f
' --enable-{key}) printf "%s" -D{name}=true ;;')
221 print(f
' --disable-{key}) printf "%s" -D{name}=false ;;')
223 if opt
["type"] == "combo" and "enabled" in opt
["choices"]:
224 print(f
' --enable-{key}) printf "%s" -D{name}=enabled ;;')
225 if opt
["type"] == "combo" and "disabled" in opt
["choices"]:
226 print(f
' --disable-{key}) printf "%s" -D{name}=disabled ;;')
228 print(f
' --enable-{key}=*) quote_sh "-D{name}=$2" ;;')
229 print(" *) return 1 ;;")
234 options
= load_options(json
.load(sys
.stdin
))
235 print("# This file is generated by meson-buildoptions.py, do not edit!")