Paolo Bonzini | 61d6309 | 2021-10-07 15:08:28 +0200 | [diff] [blame] | 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 | |
| 23 | import json |
| 24 | import textwrap |
| 25 | import shlex |
| 26 | import sys |
| 27 | |
Paolo Bonzini | 79fccf7 | 2023-09-28 11:20:01 +0200 | [diff] [blame] | 28 | # Options with nonstandard names (e.g. --with/--without) or OS-dependent |
| 29 | # defaults. Try not to add any. |
Paolo Bonzini | 3b4da13 | 2021-10-07 15:08:29 +0200 | [diff] [blame] | 30 | SKIP_OPTIONS = { |
Paolo Bonzini | 3b4da13 | 2021-10-07 15:08:29 +0200 | [diff] [blame] | 31 | "default_devices", |
Paolo Bonzini | 3b4da13 | 2021-10-07 15:08:29 +0200 | [diff] [blame] | 32 | "fuzzing_engine", |
Paolo Bonzini | 3b4da13 | 2021-10-07 15:08:29 +0200 | [diff] [blame] | 33 | } |
| 34 | |
Paolo Bonzini | 79fccf7 | 2023-09-28 11:20:01 +0200 | [diff] [blame] | 35 | # Options whose name doesn't match the option for backwards compatibility |
| 36 | # reasons, because Meson gives them a funny name, or both |
Paolo Bonzini | 119fc61 | 2022-04-20 17:33:48 +0200 | [diff] [blame] | 37 | OPTION_NAMES = { |
Paolo Bonzini | c54b59e | 2022-04-20 17:33:58 +0200 | [diff] [blame] | 38 | "b_coverage": "gcov", |
| 39 | "b_lto": "lto", |
Paolo Bonzini | 6739825 | 2022-10-12 13:19:35 +0200 | [diff] [blame] | 40 | "coroutine_backend": "with-coroutine", |
Paolo Bonzini | c0e705c | 2023-05-11 09:38:53 +0200 | [diff] [blame] | 41 | "debug": "debug-info", |
Paolo Bonzini | 119fc61 | 2022-04-20 17:33:48 +0200 | [diff] [blame] | 42 | "malloc": "enable-malloc", |
Paolo Bonzini | b0b4323 | 2022-04-20 17:33:54 +0200 | [diff] [blame] | 43 | "pkgversion": "with-pkgversion", |
Paolo Bonzini | c09c1ce | 2022-04-20 17:33:57 +0200 | [diff] [blame] | 44 | "qemu_firmwarepath": "firmwarepath", |
Paolo Bonzini | c36dd41 | 2023-10-16 08:18:08 +0200 | [diff] [blame] | 45 | "qemu_suffix": "with-suffix", |
Paolo Bonzini | 119fc61 | 2022-04-20 17:33:48 +0200 | [diff] [blame] | 46 | "trace_backends": "enable-trace-backends", |
Paolo Bonzini | 4fda601 | 2022-04-20 17:33:51 +0200 | [diff] [blame] | 47 | "trace_file": "with-trace-file", |
Paolo Bonzini | 119fc61 | 2022-04-20 17:33:48 +0200 | [diff] [blame] | 48 | } |
| 49 | |
Paolo Bonzini | 39fb3cf | 2023-09-18 11:06:48 +0200 | [diff] [blame] | 50 | # Options that configure autodetects, even though meson defines them as boolean |
| 51 | AUTO_OPTIONS = { |
| 52 | "plugins", |
Paolo Bonzini | 090a188 | 2023-10-16 08:20:13 +0200 | [diff] [blame] | 53 | "werror", |
Paolo Bonzini | 39fb3cf | 2023-09-18 11:06:48 +0200 | [diff] [blame] | 54 | } |
| 55 | |
Paolo Bonzini | 79fccf7 | 2023-09-28 11:20:01 +0200 | [diff] [blame] | 56 | # Builtin options that should be definable via configure. Some of the others |
| 57 | # we really do not want (e.g. c_args is defined via the native file, not |
| 58 | # via -D, because it's a mix of CFLAGS and --extra-cflags); for specific |
| 59 | # cases "../configure -D" can be used as an escape hatch. |
Paolo Bonzini | a70248d | 2021-11-09 10:36:39 +0100 | [diff] [blame] | 60 | BUILTIN_OPTIONS = { |
Paolo Bonzini | c54b59e | 2022-04-20 17:33:58 +0200 | [diff] [blame] | 61 | "b_coverage", |
| 62 | "b_lto", |
Paolo Bonzini | c36dd41 | 2023-10-16 08:18:08 +0200 | [diff] [blame] | 63 | "bindir", |
Paolo Bonzini | c09c1ce | 2022-04-20 17:33:57 +0200 | [diff] [blame] | 64 | "datadir", |
Paolo Bonzini | c0e705c | 2023-05-11 09:38:53 +0200 | [diff] [blame] | 65 | "debug", |
Paolo Bonzini | c09c1ce | 2022-04-20 17:33:57 +0200 | [diff] [blame] | 66 | "includedir", |
| 67 | "libdir", |
| 68 | "libexecdir", |
| 69 | "localedir", |
| 70 | "localstatedir", |
| 71 | "mandir", |
Paolo Bonzini | c36dd41 | 2023-10-16 08:18:08 +0200 | [diff] [blame] | 72 | "prefix", |
Paolo Bonzini | a70248d | 2021-11-09 10:36:39 +0100 | [diff] [blame] | 73 | "strip", |
Paolo Bonzini | c09c1ce | 2022-04-20 17:33:57 +0200 | [diff] [blame] | 74 | "sysconfdir", |
Paolo Bonzini | 090a188 | 2023-10-16 08:20:13 +0200 | [diff] [blame] | 75 | "werror", |
Paolo Bonzini | a70248d | 2021-11-09 10:36:39 +0100 | [diff] [blame] | 76 | } |
| 77 | |
Paolo Bonzini | 3b4da13 | 2021-10-07 15:08:29 +0200 | [diff] [blame] | 78 | LINE_WIDTH = 76 |
| 79 | |
| 80 | |
| 81 | # Convert the default value of an option to the string used in |
| 82 | # the help message |
Paolo Bonzini | 808d15b | 2023-02-06 13:32:32 +0100 | [diff] [blame] | 83 | def get_help(opt): |
| 84 | if opt["name"] == "libdir": |
| 85 | return 'system default' |
| 86 | value = opt["value"] |
Paolo Bonzini | 3b4da13 | 2021-10-07 15:08:29 +0200 | [diff] [blame] | 87 | if isinstance(value, list): |
| 88 | return ",".join(value) |
| 89 | if isinstance(value, bool): |
| 90 | return "enabled" if value else "disabled" |
| 91 | return str(value) |
| 92 | |
| 93 | |
| 94 | def wrap(left, text, indent): |
| 95 | spaces = " " * indent |
| 96 | if len(left) >= indent: |
| 97 | yield left |
| 98 | left = spaces |
| 99 | else: |
| 100 | left = (left + spaces)[0:indent] |
| 101 | yield from textwrap.wrap( |
| 102 | text, width=LINE_WIDTH, initial_indent=left, subsequent_indent=spaces |
| 103 | ) |
| 104 | |
| 105 | |
Paolo Bonzini | 61d6309 | 2021-10-07 15:08:28 +0200 | [diff] [blame] | 106 | def sh_print(line=""): |
| 107 | print(' printf "%s\\n"', shlex.quote(line)) |
| 108 | |
| 109 | |
Paolo Bonzini | 3b4da13 | 2021-10-07 15:08:29 +0200 | [diff] [blame] | 110 | def help_line(left, opt, indent, long): |
| 111 | right = f'{opt["description"]}' |
| 112 | if long: |
Paolo Bonzini | 808d15b | 2023-02-06 13:32:32 +0100 | [diff] [blame] | 113 | value = get_help(opt) |
Paolo Bonzini | 119fc61 | 2022-04-20 17:33:48 +0200 | [diff] [blame] | 114 | if value != "auto" and value != "": |
Paolo Bonzini | 3b4da13 | 2021-10-07 15:08:29 +0200 | [diff] [blame] | 115 | right += f" [{value}]" |
| 116 | if "choices" in opt and long: |
| 117 | choices = "/".join(sorted(opt["choices"])) |
| 118 | right += f" (choices: {choices})" |
| 119 | for x in wrap(" " + left, right, indent): |
| 120 | sh_print(x) |
| 121 | |
| 122 | |
| 123 | # Return whether the option (a dictionary) can be used with |
| 124 | # arguments. Booleans can never be used with arguments; |
| 125 | # combos allow an argument only if they accept other values |
| 126 | # than "auto", "enabled", and "disabled". |
| 127 | def allow_arg(opt): |
| 128 | if opt["type"] == "boolean": |
| 129 | return False |
| 130 | if opt["type"] != "combo": |
| 131 | return True |
| 132 | return not (set(opt["choices"]) <= {"auto", "disabled", "enabled"}) |
| 133 | |
| 134 | |
Paolo Bonzini | 119fc61 | 2022-04-20 17:33:48 +0200 | [diff] [blame] | 135 | # Return whether the option (a dictionary) can be used without |
| 136 | # arguments. Booleans can only be used without arguments; |
| 137 | # combos require an argument if they accept neither "enabled" |
| 138 | # nor "disabled" |
| 139 | def require_arg(opt): |
| 140 | if opt["type"] == "boolean": |
| 141 | return False |
| 142 | if opt["type"] != "combo": |
| 143 | return True |
| 144 | return not ({"enabled", "disabled"}.intersection(opt["choices"])) |
| 145 | |
| 146 | |
Paolo Bonzini | a70248d | 2021-11-09 10:36:39 +0100 | [diff] [blame] | 147 | def filter_options(json): |
| 148 | if ":" in json["name"]: |
| 149 | return False |
| 150 | if json["section"] == "user": |
| 151 | return json["name"] not in SKIP_OPTIONS |
| 152 | else: |
| 153 | return json["name"] in BUILTIN_OPTIONS |
| 154 | |
| 155 | |
Paolo Bonzini | 61d6309 | 2021-10-07 15:08:28 +0200 | [diff] [blame] | 156 | def load_options(json): |
Paolo Bonzini | a70248d | 2021-11-09 10:36:39 +0100 | [diff] [blame] | 157 | json = [x for x in json if filter_options(x)] |
Paolo Bonzini | 61d6309 | 2021-10-07 15:08:28 +0200 | [diff] [blame] | 158 | return sorted(json, key=lambda x: x["name"]) |
| 159 | |
| 160 | |
Paolo Bonzini | 119fc61 | 2022-04-20 17:33:48 +0200 | [diff] [blame] | 161 | def cli_option(opt): |
| 162 | name = opt["name"] |
| 163 | if name in OPTION_NAMES: |
| 164 | return OPTION_NAMES[name] |
| 165 | return name.replace("_", "-") |
| 166 | |
| 167 | |
| 168 | def cli_help_key(opt): |
| 169 | key = cli_option(opt) |
| 170 | if require_arg(opt): |
| 171 | return key |
| 172 | if opt["type"] == "boolean" and opt["value"]: |
| 173 | return f"disable-{key}" |
| 174 | return f"enable-{key}" |
| 175 | |
| 176 | |
| 177 | def cli_metavar(opt): |
| 178 | if opt["type"] == "string": |
| 179 | return "VALUE" |
| 180 | if opt["type"] == "array": |
Akihiko Odaki | 8154f5e | 2022-06-25 00:40:42 +0900 | [diff] [blame] | 181 | return "CHOICES" if "choices" in opt else "VALUES" |
Paolo Bonzini | 119fc61 | 2022-04-20 17:33:48 +0200 | [diff] [blame] | 182 | return "CHOICE" |
| 183 | |
| 184 | |
Paolo Bonzini | 61d6309 | 2021-10-07 15:08:28 +0200 | [diff] [blame] | 185 | def print_help(options): |
| 186 | print("meson_options_help() {") |
Paolo Bonzini | 39fb3cf | 2023-09-18 11:06:48 +0200 | [diff] [blame] | 187 | feature_opts = [] |
Paolo Bonzini | 119fc61 | 2022-04-20 17:33:48 +0200 | [diff] [blame] | 188 | for opt in sorted(options, key=cli_help_key): |
| 189 | key = cli_help_key(opt) |
Paolo Bonzini | 3b4da13 | 2021-10-07 15:08:29 +0200 | [diff] [blame] | 190 | # The first section includes options that have an arguments, |
| 191 | # and booleans (i.e., only one of enable/disable makes sense) |
Paolo Bonzini | 119fc61 | 2022-04-20 17:33:48 +0200 | [diff] [blame] | 192 | if require_arg(opt): |
| 193 | metavar = cli_metavar(opt) |
| 194 | left = f"--{key}={metavar}" |
| 195 | help_line(left, opt, 27, True) |
Paolo Bonzini | 39fb3cf | 2023-09-18 11:06:48 +0200 | [diff] [blame] | 196 | elif opt["type"] == "boolean" and opt["name"] not in AUTO_OPTIONS: |
Paolo Bonzini | 119fc61 | 2022-04-20 17:33:48 +0200 | [diff] [blame] | 197 | left = f"--{key}" |
Paolo Bonzini | 3b4da13 | 2021-10-07 15:08:29 +0200 | [diff] [blame] | 198 | help_line(left, opt, 27, False) |
| 199 | elif allow_arg(opt): |
| 200 | if opt["type"] == "combo" and "enabled" in opt["choices"]: |
Paolo Bonzini | 119fc61 | 2022-04-20 17:33:48 +0200 | [diff] [blame] | 201 | left = f"--{key}[=CHOICE]" |
Paolo Bonzini | 3b4da13 | 2021-10-07 15:08:29 +0200 | [diff] [blame] | 202 | else: |
Paolo Bonzini | 119fc61 | 2022-04-20 17:33:48 +0200 | [diff] [blame] | 203 | left = f"--{key}=CHOICE" |
Paolo Bonzini | 3b4da13 | 2021-10-07 15:08:29 +0200 | [diff] [blame] | 204 | help_line(left, opt, 27, True) |
Paolo Bonzini | 39fb3cf | 2023-09-18 11:06:48 +0200 | [diff] [blame] | 205 | else: |
| 206 | feature_opts.append(opt) |
Paolo Bonzini | 3b4da13 | 2021-10-07 15:08:29 +0200 | [diff] [blame] | 207 | |
Paolo Bonzini | 61d6309 | 2021-10-07 15:08:28 +0200 | [diff] [blame] | 208 | sh_print() |
| 209 | sh_print("Optional features, enabled with --enable-FEATURE and") |
| 210 | sh_print("disabled with --disable-FEATURE, default is enabled if available") |
| 211 | sh_print("(unless built with --without-default-features):") |
| 212 | sh_print() |
Paolo Bonzini | 39fb3cf | 2023-09-18 11:06:48 +0200 | [diff] [blame] | 213 | for opt in sorted(feature_opts, key=cli_option): |
| 214 | key = cli_option(opt) |
| 215 | help_line(key, opt, 18, False) |
Paolo Bonzini | 61d6309 | 2021-10-07 15:08:28 +0200 | [diff] [blame] | 216 | print("}") |
| 217 | |
| 218 | |
| 219 | def print_parse(options): |
| 220 | print("_meson_option_parse() {") |
| 221 | print(" case $1 in") |
Paolo Bonzini | 3b4da13 | 2021-10-07 15:08:29 +0200 | [diff] [blame] | 222 | for opt in options: |
Paolo Bonzini | 119fc61 | 2022-04-20 17:33:48 +0200 | [diff] [blame] | 223 | key = cli_option(opt) |
Paolo Bonzini | 3b4da13 | 2021-10-07 15:08:29 +0200 | [diff] [blame] | 224 | name = opt["name"] |
Paolo Bonzini | 119fc61 | 2022-04-20 17:33:48 +0200 | [diff] [blame] | 225 | if require_arg(opt): |
Akihiko Odaki | 8154f5e | 2022-06-25 00:40:42 +0900 | [diff] [blame] | 226 | if opt["type"] == "array" and not "choices" in opt: |
| 227 | print(f' --{key}=*) quote_sh "-D{name}=$(meson_option_build_array $2)" ;;') |
| 228 | else: |
| 229 | print(f' --{key}=*) quote_sh "-D{name}=$2" ;;') |
Paolo Bonzini | 119fc61 | 2022-04-20 17:33:48 +0200 | [diff] [blame] | 230 | elif opt["type"] == "boolean": |
Paolo Bonzini | 3b4da13 | 2021-10-07 15:08:29 +0200 | [diff] [blame] | 231 | print(f' --enable-{key}) printf "%s" -D{name}=true ;;') |
| 232 | print(f' --disable-{key}) printf "%s" -D{name}=false ;;') |
| 233 | else: |
| 234 | if opt["type"] == "combo" and "enabled" in opt["choices"]: |
| 235 | print(f' --enable-{key}) printf "%s" -D{name}=enabled ;;') |
| 236 | if opt["type"] == "combo" and "disabled" in opt["choices"]: |
| 237 | print(f' --disable-{key}) printf "%s" -D{name}=disabled ;;') |
| 238 | if allow_arg(opt): |
| 239 | print(f' --enable-{key}=*) quote_sh "-D{name}=$2" ;;') |
Paolo Bonzini | 61d6309 | 2021-10-07 15:08:28 +0200 | [diff] [blame] | 240 | print(" *) return 1 ;;") |
| 241 | print(" esac") |
| 242 | print("}") |
| 243 | |
Nabih Estefan | cff666a | 2025-02-27 18:04:54 +0000 | [diff] [blame] | 244 | json_data = sys.stdin.read() |
| 245 | try: |
| 246 | options = load_options(json.loads(json_data)) |
| 247 | except: |
| 248 | print("Failure in scripts/meson-buildoptions.py parsing stdin as json", |
| 249 | file=sys.stderr) |
| 250 | print(json_data, file=sys.stderr) |
| 251 | sys.exit(1) |
Paolo Bonzini | 61d6309 | 2021-10-07 15:08:28 +0200 | [diff] [blame] | 252 | print("# This file is generated by meson-buildoptions.py, do not edit!") |
| 253 | print_help(options) |
| 254 | print_parse(options) |