blob: 3e2b4785388ffbb815882beb1173173d4449f79f [file] [log] [blame]
Paolo Bonzini61d63092021-10-07 15:08:28 +02001#! /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
Paolo Bonzini3b4da132021-10-07 15:08:29 +020028SKIP_OPTIONS = {
Paolo Bonzini3b4da132021-10-07 15:08:29 +020029 "default_devices",
Paolo Bonzini3b4da132021-10-07 15:08:29 +020030 "fuzzing_engine",
Paolo Bonzini3b4da132021-10-07 15:08:29 +020031 "qemu_suffix",
Paolo Bonzini35acbb32021-10-13 13:43:36 +020032 "smbd",
Paolo Bonzini3b4da132021-10-07 15:08:29 +020033}
34
Paolo Bonzini119fc612022-04-20 17:33:48 +020035OPTION_NAMES = {
Paolo Bonzinic54b59e2022-04-20 17:33:58 +020036 "b_coverage": "gcov",
37 "b_lto": "lto",
Paolo Bonzini119fc612022-04-20 17:33:48 +020038 "malloc": "enable-malloc",
Paolo Bonzinib0b43232022-04-20 17:33:54 +020039 "pkgversion": "with-pkgversion",
Paolo Bonzinic09c1ce2022-04-20 17:33:57 +020040 "qemu_firmwarepath": "firmwarepath",
Paolo Bonzini119fc612022-04-20 17:33:48 +020041 "trace_backends": "enable-trace-backends",
Paolo Bonzini4fda6012022-04-20 17:33:51 +020042 "trace_file": "with-trace-file",
Paolo Bonzini119fc612022-04-20 17:33:48 +020043}
44
Paolo Bonzinia70248d2021-11-09 10:36:39 +010045BUILTIN_OPTIONS = {
Paolo Bonzinic54b59e2022-04-20 17:33:58 +020046 "b_coverage",
47 "b_lto",
Paolo Bonzinic09c1ce2022-04-20 17:33:57 +020048 "datadir",
49 "includedir",
50 "libdir",
51 "libexecdir",
52 "localedir",
53 "localstatedir",
54 "mandir",
Paolo Bonzinia70248d2021-11-09 10:36:39 +010055 "strip",
Paolo Bonzinic09c1ce2022-04-20 17:33:57 +020056 "sysconfdir",
Paolo Bonzinia70248d2021-11-09 10:36:39 +010057}
58
Paolo Bonzini3b4da132021-10-07 15:08:29 +020059LINE_WIDTH = 76
60
61
62# Convert the default value of an option to the string used in
63# the help message
64def value_to_help(value):
65 if isinstance(value, list):
66 return ",".join(value)
67 if isinstance(value, bool):
68 return "enabled" if value else "disabled"
69 return str(value)
70
71
72def wrap(left, text, indent):
73 spaces = " " * indent
74 if len(left) >= indent:
75 yield left
76 left = spaces
77 else:
78 left = (left + spaces)[0:indent]
79 yield from textwrap.wrap(
80 text, width=LINE_WIDTH, initial_indent=left, subsequent_indent=spaces
81 )
82
83
Paolo Bonzini61d63092021-10-07 15:08:28 +020084def sh_print(line=""):
85 print(' printf "%s\\n"', shlex.quote(line))
86
87
Paolo Bonzini3b4da132021-10-07 15:08:29 +020088def help_line(left, opt, indent, long):
89 right = f'{opt["description"]}'
90 if long:
91 value = value_to_help(opt["value"])
Paolo Bonzini119fc612022-04-20 17:33:48 +020092 if value != "auto" and value != "":
Paolo Bonzini3b4da132021-10-07 15:08:29 +020093 right += f" [{value}]"
94 if "choices" in opt and long:
95 choices = "/".join(sorted(opt["choices"]))
96 right += f" (choices: {choices})"
97 for x in wrap(" " + left, right, indent):
98 sh_print(x)
99
100
101# Return whether the option (a dictionary) can be used with
102# arguments. Booleans can never be used with arguments;
103# combos allow an argument only if they accept other values
104# than "auto", "enabled", and "disabled".
105def allow_arg(opt):
106 if opt["type"] == "boolean":
107 return False
108 if opt["type"] != "combo":
109 return True
110 return not (set(opt["choices"]) <= {"auto", "disabled", "enabled"})
111
112
Paolo Bonzini119fc612022-04-20 17:33:48 +0200113# Return whether the option (a dictionary) can be used without
114# arguments. Booleans can only be used without arguments;
115# combos require an argument if they accept neither "enabled"
116# nor "disabled"
117def require_arg(opt):
118 if opt["type"] == "boolean":
119 return False
120 if opt["type"] != "combo":
121 return True
122 return not ({"enabled", "disabled"}.intersection(opt["choices"]))
123
124
Paolo Bonzinia70248d2021-11-09 10:36:39 +0100125def filter_options(json):
126 if ":" in json["name"]:
127 return False
128 if json["section"] == "user":
129 return json["name"] not in SKIP_OPTIONS
130 else:
131 return json["name"] in BUILTIN_OPTIONS
132
133
Paolo Bonzini61d63092021-10-07 15:08:28 +0200134def load_options(json):
Paolo Bonzinia70248d2021-11-09 10:36:39 +0100135 json = [x for x in json if filter_options(x)]
Paolo Bonzini61d63092021-10-07 15:08:28 +0200136 return sorted(json, key=lambda x: x["name"])
137
138
Paolo Bonzini119fc612022-04-20 17:33:48 +0200139def cli_option(opt):
140 name = opt["name"]
141 if name in OPTION_NAMES:
142 return OPTION_NAMES[name]
143 return name.replace("_", "-")
144
145
146def cli_help_key(opt):
147 key = cli_option(opt)
148 if require_arg(opt):
149 return key
150 if opt["type"] == "boolean" and opt["value"]:
151 return f"disable-{key}"
152 return f"enable-{key}"
153
154
155def cli_metavar(opt):
156 if opt["type"] == "string":
157 return "VALUE"
158 if opt["type"] == "array":
Akihiko Odaki8154f5e2022-06-25 00:40:42 +0900159 return "CHOICES" if "choices" in opt else "VALUES"
Paolo Bonzini119fc612022-04-20 17:33:48 +0200160 return "CHOICE"
161
162
Paolo Bonzini61d63092021-10-07 15:08:28 +0200163def print_help(options):
164 print("meson_options_help() {")
Paolo Bonzini119fc612022-04-20 17:33:48 +0200165 for opt in sorted(options, key=cli_help_key):
166 key = cli_help_key(opt)
Paolo Bonzini3b4da132021-10-07 15:08:29 +0200167 # The first section includes options that have an arguments,
168 # and booleans (i.e., only one of enable/disable makes sense)
Paolo Bonzini119fc612022-04-20 17:33:48 +0200169 if require_arg(opt):
170 metavar = cli_metavar(opt)
171 left = f"--{key}={metavar}"
172 help_line(left, opt, 27, True)
173 elif opt["type"] == "boolean":
174 left = f"--{key}"
Paolo Bonzini3b4da132021-10-07 15:08:29 +0200175 help_line(left, opt, 27, False)
176 elif allow_arg(opt):
177 if opt["type"] == "combo" and "enabled" in opt["choices"]:
Paolo Bonzini119fc612022-04-20 17:33:48 +0200178 left = f"--{key}[=CHOICE]"
Paolo Bonzini3b4da132021-10-07 15:08:29 +0200179 else:
Paolo Bonzini119fc612022-04-20 17:33:48 +0200180 left = f"--{key}=CHOICE"
Paolo Bonzini3b4da132021-10-07 15:08:29 +0200181 help_line(left, opt, 27, True)
182
Paolo Bonzini61d63092021-10-07 15:08:28 +0200183 sh_print()
184 sh_print("Optional features, enabled with --enable-FEATURE and")
185 sh_print("disabled with --disable-FEATURE, default is enabled if available")
186 sh_print("(unless built with --without-default-features):")
187 sh_print()
Paolo Bonzini3b4da132021-10-07 15:08:29 +0200188 for opt in options:
189 key = opt["name"].replace("_", "-")
190 if opt["type"] != "boolean" and not allow_arg(opt):
191 help_line(key, opt, 18, False)
Paolo Bonzini61d63092021-10-07 15:08:28 +0200192 print("}")
193
194
195def print_parse(options):
196 print("_meson_option_parse() {")
197 print(" case $1 in")
Paolo Bonzini3b4da132021-10-07 15:08:29 +0200198 for opt in options:
Paolo Bonzini119fc612022-04-20 17:33:48 +0200199 key = cli_option(opt)
Paolo Bonzini3b4da132021-10-07 15:08:29 +0200200 name = opt["name"]
Paolo Bonzini119fc612022-04-20 17:33:48 +0200201 if require_arg(opt):
Akihiko Odaki8154f5e2022-06-25 00:40:42 +0900202 if opt["type"] == "array" and not "choices" in opt:
203 print(f' --{key}=*) quote_sh "-D{name}=$(meson_option_build_array $2)" ;;')
204 else:
205 print(f' --{key}=*) quote_sh "-D{name}=$2" ;;')
Paolo Bonzini119fc612022-04-20 17:33:48 +0200206 elif opt["type"] == "boolean":
Paolo Bonzini3b4da132021-10-07 15:08:29 +0200207 print(f' --enable-{key}) printf "%s" -D{name}=true ;;')
208 print(f' --disable-{key}) printf "%s" -D{name}=false ;;')
209 else:
210 if opt["type"] == "combo" and "enabled" in opt["choices"]:
211 print(f' --enable-{key}) printf "%s" -D{name}=enabled ;;')
212 if opt["type"] == "combo" and "disabled" in opt["choices"]:
213 print(f' --disable-{key}) printf "%s" -D{name}=disabled ;;')
214 if allow_arg(opt):
215 print(f' --enable-{key}=*) quote_sh "-D{name}=$2" ;;')
Paolo Bonzini61d63092021-10-07 15:08:28 +0200216 print(" *) return 1 ;;")
217 print(" esac")
218 print("}")
219
220
221options = load_options(json.load(sys.stdin))
Paolo Bonzini61d63092021-10-07 15:08:28 +0200222print("# This file is generated by meson-buildoptions.py, do not edit!")
223print_help(options)
224print_parse(options)