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/>.
46 # Convert the default value of an option to the string used in
48 def value_to_help(value
):
49 if isinstance(value
, list):
50 return ",".join(value
)
51 if isinstance(value
, bool):
52 return "enabled" if value
else "disabled"
56 def wrap(left
, text
, indent
):
58 if len(left
) >= indent
:
62 left
= (left
+ spaces
)[0:indent
]
63 yield from textwrap
.wrap(
64 text
, width
=LINE_WIDTH
, initial_indent
=left
, subsequent_indent
=spaces
68 def sh_print(line
=""):
69 print(' printf "%s\\n"', shlex
.quote(line
))
72 def help_line(left
, opt
, indent
, long):
73 right
= f
'{opt["description"]}'
75 value
= value_to_help(opt
["value"])
77 right
+= f
" [{value}]"
78 if "choices" in opt
and long:
79 choices
= "/".join(sorted(opt
["choices"]))
80 right
+= f
" (choices: {choices})"
81 for x
in wrap(" " + left
, right
, indent
):
85 # Return whether the option (a dictionary) can be used with
86 # arguments. Booleans can never be used with arguments;
87 # combos allow an argument only if they accept other values
88 # than "auto", "enabled", and "disabled".
90 if opt
["type"] == "boolean":
92 if opt
["type"] != "combo":
94 return not (set(opt
["choices"]) <= {"auto", "disabled", "enabled"})
97 def filter_options(json
):
98 if ":" in json
["name"]:
100 if json
["section"] == "user":
101 return json
["name"] not in SKIP_OPTIONS
103 return json
["name"] in BUILTIN_OPTIONS
106 def load_options(json
):
107 json
= [x
for x
in json
if filter_options(x
)]
108 return sorted(json
, key
=lambda x
: x
["name"])
111 def print_help(options
):
112 print("meson_options_help() {")
114 key
= opt
["name"].replace("_", "-")
115 # The first section includes options that have an arguments,
116 # and booleans (i.e., only one of enable/disable makes sense)
117 if opt
["type"] == "boolean":
118 left
= f
"--disable-{key}" if opt
["value"] else f
"--enable-{key}"
119 help_line(left
, opt
, 27, False)
121 if opt
["type"] == "combo" and "enabled" in opt
["choices"]:
122 left
= f
"--enable-{key}[=CHOICE]"
124 left
= f
"--enable-{key}=CHOICE"
125 help_line(left
, opt
, 27, True)
128 sh_print("Optional features, enabled with --enable-FEATURE and")
129 sh_print("disabled with --disable-FEATURE, default is enabled if available")
130 sh_print("(unless built with --without-default-features):")
133 key
= opt
["name"].replace("_", "-")
134 if opt
["type"] != "boolean" and not allow_arg(opt
):
135 help_line(key
, opt
, 18, False)
139 def print_parse(options
):
140 print("_meson_option_parse() {")
143 key
= opt
["name"].replace("_", "-")
145 if opt
["type"] == "boolean":
146 print(f
' --enable-{key}) printf "%s" -D{name}=true ;;')
147 print(f
' --disable-{key}) printf "%s" -D{name}=false ;;')
149 if opt
["type"] == "combo" and "enabled" in opt
["choices"]:
150 print(f
' --enable-{key}) printf "%s" -D{name}=enabled ;;')
151 if opt
["type"] == "combo" and "disabled" in opt
["choices"]:
152 print(f
' --disable-{key}) printf "%s" -D{name}=disabled ;;')
154 print(f
' --enable-{key}=*) quote_sh "-D{name}=$2" ;;')
155 print(" *) return 1 ;;")
160 options
= load_options(json
.load(sys
.stdin
))
161 print("# This file is generated by meson-buildoptions.py, do not edit!")