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
65 if opt
["name"] == "libdir":
66 return 'system default'
68 if isinstance(value
, list):
69 return ",".join(value
)
70 if isinstance(value
, bool):
71 return "enabled" if value
else "disabled"
75 def wrap(left
, text
, indent
):
77 if len(left
) >= indent
:
81 left
= (left
+ spaces
)[0:indent
]
82 yield from textwrap
.wrap(
83 text
, width
=LINE_WIDTH
, initial_indent
=left
, subsequent_indent
=spaces
87 def sh_print(line
=""):
88 print(' printf "%s\\n"', shlex
.quote(line
))
91 def help_line(left
, opt
, indent
, long):
92 right
= f
'{opt["description"]}'
95 if value
!= "auto" and value
!= "":
96 right
+= f
" [{value}]"
97 if "choices" in opt
and long:
98 choices
= "/".join(sorted(opt
["choices"]))
99 right
+= f
" (choices: {choices})"
100 for x
in wrap(" " + left
, right
, indent
):
104 # Return whether the option (a dictionary) can be used with
105 # arguments. Booleans can never be used with arguments;
106 # combos allow an argument only if they accept other values
107 # than "auto", "enabled", and "disabled".
109 if opt
["type"] == "boolean":
111 if opt
["type"] != "combo":
113 return not (set(opt
["choices"]) <= {"auto", "disabled", "enabled"})
116 # Return whether the option (a dictionary) can be used without
117 # arguments. Booleans can only be used without arguments;
118 # combos require an argument if they accept neither "enabled"
120 def require_arg(opt
):
121 if opt
["type"] == "boolean":
123 if opt
["type"] != "combo":
125 return not ({"enabled", "disabled"}.intersection(opt
["choices"]))
128 def filter_options(json
):
129 if ":" in json
["name"]:
131 if json
["section"] == "user":
132 return json
["name"] not in SKIP_OPTIONS
134 return json
["name"] in BUILTIN_OPTIONS
137 def load_options(json
):
138 json
= [x
for x
in json
if filter_options(x
)]
139 return sorted(json
, key
=lambda x
: x
["name"])
144 if name
in OPTION_NAMES
:
145 return OPTION_NAMES
[name
]
146 return name
.replace("_", "-")
149 def cli_help_key(opt
):
150 key
= cli_option(opt
)
153 if opt
["type"] == "boolean" and opt
["value"]:
154 return f
"disable-{key}"
155 return f
"enable-{key}"
158 def cli_metavar(opt
):
159 if opt
["type"] == "string":
161 if opt
["type"] == "array":
162 return "CHOICES" if "choices" in opt
else "VALUES"
166 def print_help(options
):
167 print("meson_options_help() {")
168 for opt
in sorted(options
, key
=cli_help_key
):
169 key
= cli_help_key(opt
)
170 # The first section includes options that have an arguments,
171 # and booleans (i.e., only one of enable/disable makes sense)
173 metavar
= cli_metavar(opt
)
174 left
= f
"--{key}={metavar}"
175 help_line(left
, opt
, 27, True)
176 elif opt
["type"] == "boolean":
178 help_line(left
, opt
, 27, False)
180 if opt
["type"] == "combo" and "enabled" in opt
["choices"]:
181 left
= f
"--{key}[=CHOICE]"
183 left
= f
"--{key}=CHOICE"
184 help_line(left
, opt
, 27, True)
187 sh_print("Optional features, enabled with --enable-FEATURE and")
188 sh_print("disabled with --disable-FEATURE, default is enabled if available")
189 sh_print("(unless built with --without-default-features):")
192 key
= opt
["name"].replace("_", "-")
193 if opt
["type"] != "boolean" and not allow_arg(opt
):
194 help_line(key
, opt
, 18, False)
198 def print_parse(options
):
199 print("_meson_option_parse() {")
202 key
= cli_option(opt
)
205 if opt
["type"] == "array" and not "choices" in opt
:
206 print(f
' --{key}=*) quote_sh "-D{name}=$(meson_option_build_array $2)" ;;')
208 print(f
' --{key}=*) quote_sh "-D{name}=$2" ;;')
209 elif opt
["type"] == "boolean":
210 print(f
' --enable-{key}) printf "%s" -D{name}=true ;;')
211 print(f
' --disable-{key}) printf "%s" -D{name}=false ;;')
213 if opt
["type"] == "combo" and "enabled" in opt
["choices"]:
214 print(f
' --enable-{key}) printf "%s" -D{name}=enabled ;;')
215 if opt
["type"] == "combo" and "disabled" in opt
["choices"]:
216 print(f
' --disable-{key}) printf "%s" -D{name}=disabled ;;')
218 print(f
' --enable-{key}=*) quote_sh "-D{name}=$2" ;;')
219 print(" *) return 1 ;;")
224 options
= load_options(json
.load(sys
.stdin
))
225 print("# This file is generated by meson-buildoptions.py, do not edit!")