]> git.proxmox.com Git - mirror_qemu.git/blame - scripts/meson-buildoptions.py
meson, configure: move bdrv whitelists to meson
[mirror_qemu.git] / scripts / meson-buildoptions.py
CommitLineData
61d63097
PB
1#! /usr/bin/env python3
2
3# Generate configure command line options handling code, based on Meson's
4# user build options introspection data
5#
6# Copyright (C) 2021 Red Hat, Inc.
7#
8# Author: Paolo Bonzini <pbonzini@redhat.com>
9#
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)
13# any later version.
14#
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.
19#
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/>.
22
23import json
24import textwrap
25import shlex
26import sys
27
3b4da132 28SKIP_OPTIONS = {
3b4da132
PB
29 "default_devices",
30 "docdir",
31 "fuzzing_engine",
32 "qemu_firmwarepath",
33 "qemu_suffix",
35acbb30 34 "smbd",
3b4da132
PB
35}
36
119fc611
PB
37OPTION_NAMES = {
38 "malloc": "enable-malloc",
39 "trace_backends": "enable-trace-backends",
4fda6011 40 "trace_file": "with-trace-file",
119fc611
PB
41}
42
a70248db
PB
43BUILTIN_OPTIONS = {
44 "strip",
45}
46
3b4da132
PB
47LINE_WIDTH = 76
48
49
50# Convert the default value of an option to the string used in
51# the help message
52def value_to_help(value):
53 if isinstance(value, list):
54 return ",".join(value)
55 if isinstance(value, bool):
56 return "enabled" if value else "disabled"
57 return str(value)
58
59
60def wrap(left, text, indent):
61 spaces = " " * indent
62 if len(left) >= indent:
63 yield left
64 left = spaces
65 else:
66 left = (left + spaces)[0:indent]
67 yield from textwrap.wrap(
68 text, width=LINE_WIDTH, initial_indent=left, subsequent_indent=spaces
69 )
70
71
61d63097
PB
72def sh_print(line=""):
73 print(' printf "%s\\n"', shlex.quote(line))
74
75
3b4da132
PB
76def help_line(left, opt, indent, long):
77 right = f'{opt["description"]}'
78 if long:
79 value = value_to_help(opt["value"])
119fc611 80 if value != "auto" and value != "":
3b4da132
PB
81 right += f" [{value}]"
82 if "choices" in opt and long:
83 choices = "/".join(sorted(opt["choices"]))
84 right += f" (choices: {choices})"
85 for x in wrap(" " + left, right, indent):
86 sh_print(x)
87
88
89# Return whether the option (a dictionary) can be used with
90# arguments. Booleans can never be used with arguments;
91# combos allow an argument only if they accept other values
92# than "auto", "enabled", and "disabled".
93def allow_arg(opt):
94 if opt["type"] == "boolean":
95 return False
96 if opt["type"] != "combo":
97 return True
98 return not (set(opt["choices"]) <= {"auto", "disabled", "enabled"})
99
100
119fc611
PB
101# Return whether the option (a dictionary) can be used without
102# arguments. Booleans can only be used without arguments;
103# combos require an argument if they accept neither "enabled"
104# nor "disabled"
105def require_arg(opt):
106 if opt["type"] == "boolean":
107 return False
108 if opt["type"] != "combo":
109 return True
110 return not ({"enabled", "disabled"}.intersection(opt["choices"]))
111
112
a70248db
PB
113def filter_options(json):
114 if ":" in json["name"]:
115 return False
116 if json["section"] == "user":
117 return json["name"] not in SKIP_OPTIONS
118 else:
119 return json["name"] in BUILTIN_OPTIONS
120
121
61d63097 122def load_options(json):
a70248db 123 json = [x for x in json if filter_options(x)]
61d63097
PB
124 return sorted(json, key=lambda x: x["name"])
125
126
119fc611
PB
127def cli_option(opt):
128 name = opt["name"]
129 if name in OPTION_NAMES:
130 return OPTION_NAMES[name]
131 return name.replace("_", "-")
132
133
134def cli_help_key(opt):
135 key = cli_option(opt)
136 if require_arg(opt):
137 return key
138 if opt["type"] == "boolean" and opt["value"]:
139 return f"disable-{key}"
140 return f"enable-{key}"
141
142
143def cli_metavar(opt):
144 if opt["type"] == "string":
145 return "VALUE"
146 if opt["type"] == "array":
147 return "CHOICES"
148 return "CHOICE"
149
150
61d63097
PB
151def print_help(options):
152 print("meson_options_help() {")
119fc611
PB
153 for opt in sorted(options, key=cli_help_key):
154 key = cli_help_key(opt)
3b4da132
PB
155 # The first section includes options that have an arguments,
156 # and booleans (i.e., only one of enable/disable makes sense)
119fc611
PB
157 if require_arg(opt):
158 metavar = cli_metavar(opt)
159 left = f"--{key}={metavar}"
160 help_line(left, opt, 27, True)
161 elif opt["type"] == "boolean":
162 left = f"--{key}"
3b4da132
PB
163 help_line(left, opt, 27, False)
164 elif allow_arg(opt):
165 if opt["type"] == "combo" and "enabled" in opt["choices"]:
119fc611 166 left = f"--{key}[=CHOICE]"
3b4da132 167 else:
119fc611 168 left = f"--{key}=CHOICE"
3b4da132
PB
169 help_line(left, opt, 27, True)
170
61d63097
PB
171 sh_print()
172 sh_print("Optional features, enabled with --enable-FEATURE and")
173 sh_print("disabled with --disable-FEATURE, default is enabled if available")
174 sh_print("(unless built with --without-default-features):")
175 sh_print()
3b4da132
PB
176 for opt in options:
177 key = opt["name"].replace("_", "-")
178 if opt["type"] != "boolean" and not allow_arg(opt):
179 help_line(key, opt, 18, False)
61d63097
PB
180 print("}")
181
182
183def print_parse(options):
184 print("_meson_option_parse() {")
185 print(" case $1 in")
3b4da132 186 for opt in options:
119fc611 187 key = cli_option(opt)
3b4da132 188 name = opt["name"]
119fc611
PB
189 if require_arg(opt):
190 print(f' --{key}=*) quote_sh "-D{name}=$2" ;;')
191 elif opt["type"] == "boolean":
3b4da132
PB
192 print(f' --enable-{key}) printf "%s" -D{name}=true ;;')
193 print(f' --disable-{key}) printf "%s" -D{name}=false ;;')
194 else:
195 if opt["type"] == "combo" and "enabled" in opt["choices"]:
196 print(f' --enable-{key}) printf "%s" -D{name}=enabled ;;')
197 if opt["type"] == "combo" and "disabled" in opt["choices"]:
198 print(f' --disable-{key}) printf "%s" -D{name}=disabled ;;')
199 if allow_arg(opt):
200 print(f' --enable-{key}=*) quote_sh "-D{name}=$2" ;;')
61d63097
PB
201 print(" *) return 1 ;;")
202 print(" esac")
203 print("}")
204
205
206options = load_options(json.load(sys.stdin))
207print("# This file is generated by meson-buildoptions.py, do not edit!")
208print_help(options)
209print_parse(options)