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",
65 # Convert the default value of an option to the string used in
68 if opt
["name"] == "libdir":
69 return 'system default'
71 if isinstance(value
, list):
72 return ",".join(value
)
73 if isinstance(value
, bool):
74 return "enabled" if value
else "disabled"
78 def wrap(left
, text
, indent
):
80 if len(left
) >= indent
:
84 left
= (left
+ spaces
)[0:indent
]
85 yield from textwrap
.wrap(
86 text
, width
=LINE_WIDTH
, initial_indent
=left
, subsequent_indent
=spaces
90 def sh_print(line
=""):
91 print(' printf "%s\\n"', shlex
.quote(line
))
94 def help_line(left
, opt
, indent
, long):
95 right
= f
'{opt["description"]}'
98 if value
!= "auto" and value
!= "":
99 right
+= f
" [{value}]"
100 if "choices" in opt
and long:
101 choices
= "/".join(sorted(opt
["choices"]))
102 right
+= f
" (choices: {choices})"
103 for x
in wrap(" " + left
, right
, indent
):
107 # Return whether the option (a dictionary) can be used with
108 # arguments. Booleans can never be used with arguments;
109 # combos allow an argument only if they accept other values
110 # than "auto", "enabled", and "disabled".
112 if opt
["type"] == "boolean":
114 if opt
["type"] != "combo":
116 return not (set(opt
["choices"]) <= {"auto", "disabled", "enabled"})
119 # Return whether the option (a dictionary) can be used without
120 # arguments. Booleans can only be used without arguments;
121 # combos require an argument if they accept neither "enabled"
123 def require_arg(opt
):
124 if opt
["type"] == "boolean":
126 if opt
["type"] != "combo":
128 return not ({"enabled", "disabled"}.intersection(opt
["choices"]))
131 def filter_options(json
):
132 if ":" in json
["name"]:
134 if json
["section"] == "user":
135 return json
["name"] not in SKIP_OPTIONS
137 return json
["name"] in BUILTIN_OPTIONS
140 def load_options(json
):
141 json
= [x
for x
in json
if filter_options(x
)]
142 return sorted(json
, key
=lambda x
: x
["name"])
147 if name
in OPTION_NAMES
:
148 return OPTION_NAMES
[name
]
149 return name
.replace("_", "-")
152 def cli_help_key(opt
):
153 key
= cli_option(opt
)
156 if opt
["type"] == "boolean" and opt
["value"]:
157 return f
"disable-{key}"
158 return f
"enable-{key}"
161 def cli_metavar(opt
):
162 if opt
["type"] == "string":
164 if opt
["type"] == "array":
165 return "CHOICES" if "choices" in opt
else "VALUES"
169 def print_help(options
):
170 print("meson_options_help() {")
171 for opt
in sorted(options
, key
=cli_help_key
):
172 key
= cli_help_key(opt
)
173 # The first section includes options that have an arguments,
174 # and booleans (i.e., only one of enable/disable makes sense)
176 metavar
= cli_metavar(opt
)
177 left
= f
"--{key}={metavar}"
178 help_line(left
, opt
, 27, True)
179 elif opt
["type"] == "boolean":
181 help_line(left
, opt
, 27, False)
183 if opt
["type"] == "combo" and "enabled" in opt
["choices"]:
184 left
= f
"--{key}[=CHOICE]"
186 left
= f
"--{key}=CHOICE"
187 help_line(left
, opt
, 27, True)
190 sh_print("Optional features, enabled with --enable-FEATURE and")
191 sh_print("disabled with --disable-FEATURE, default is enabled if available")
192 sh_print("(unless built with --without-default-features):")
195 key
= opt
["name"].replace("_", "-")
196 if opt
["type"] != "boolean" and not allow_arg(opt
):
197 help_line(key
, opt
, 18, False)
201 def print_parse(options
):
202 print("_meson_option_parse() {")
205 key
= cli_option(opt
)
208 if opt
["type"] == "array" and not "choices" in opt
:
209 print(f
' --{key}=*) quote_sh "-D{name}=$(meson_option_build_array $2)" ;;')
211 print(f
' --{key}=*) quote_sh "-D{name}=$2" ;;')
212 elif opt
["type"] == "boolean":
213 print(f
' --enable-{key}) printf "%s" -D{name}=true ;;')
214 print(f
' --disable-{key}) printf "%s" -D{name}=false ;;')
216 if opt
["type"] == "combo" and "enabled" in opt
["choices"]:
217 print(f
' --enable-{key}) printf "%s" -D{name}=enabled ;;')
218 if opt
["type"] == "combo" and "disabled" in opt
["choices"]:
219 print(f
' --disable-{key}) printf "%s" -D{name}=disabled ;;')
221 print(f
' --enable-{key}=*) quote_sh "-D{name}=$2" ;;')
222 print(" *) return 1 ;;")
227 options
= load_options(json
.load(sys
.stdin
))
228 print("# This file is generated by meson-buildoptions.py, do not edit!")