xref: /qemu/scripts/meson-buildoptions.py (revision b83a80e8)
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
28SKIP_OPTIONS = {
29    "audio_drv_list",
30    "default_devices",
31    "docdir",
32    "fuzzing_engine",
33    "qemu_firmwarepath",
34    "qemu_suffix",
35    "sphinx_build",
36    "trace_file",
37}
38
39BUILTIN_OPTIONS = {
40    "strip",
41}
42
43LINE_WIDTH = 76
44
45
46# Convert the default value of an option to the string used in
47# the help message
48def 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"
53    return str(value)
54
55
56def wrap(left, text, indent):
57    spaces = " " * indent
58    if len(left) >= indent:
59        yield left
60        left = spaces
61    else:
62        left = (left + spaces)[0:indent]
63    yield from textwrap.wrap(
64        text, width=LINE_WIDTH, initial_indent=left, subsequent_indent=spaces
65    )
66
67
68def sh_print(line=""):
69    print('  printf "%s\\n"', shlex.quote(line))
70
71
72def help_line(left, opt, indent, long):
73    right = f'{opt["description"]}'
74    if long:
75        value = value_to_help(opt["value"])
76        if value != "auto":
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):
82        sh_print(x)
83
84
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".
89def allow_arg(opt):
90    if opt["type"] == "boolean":
91        return False
92    if opt["type"] != "combo":
93        return True
94    return not (set(opt["choices"]) <= {"auto", "disabled", "enabled"})
95
96
97def filter_options(json):
98    if ":" in json["name"]:
99        return False
100    if json["section"] == "user":
101        return json["name"] not in SKIP_OPTIONS
102    else:
103        return json["name"] in BUILTIN_OPTIONS
104
105
106def load_options(json):
107    json = [x for x in json if filter_options(x)]
108    return sorted(json, key=lambda x: x["name"])
109
110
111def print_help(options):
112    print("meson_options_help() {")
113    for opt in options:
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)
120        elif allow_arg(opt):
121            if opt["type"] == "combo" and "enabled" in opt["choices"]:
122                left = f"--enable-{key}[=CHOICE]"
123            else:
124                left = f"--enable-{key}=CHOICE"
125            help_line(left, opt, 27, True)
126
127    sh_print()
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):")
131    sh_print()
132    for opt in options:
133        key = opt["name"].replace("_", "-")
134        if opt["type"] != "boolean" and not allow_arg(opt):
135            help_line(key, opt, 18, False)
136    print("}")
137
138
139def print_parse(options):
140    print("_meson_option_parse() {")
141    print("  case $1 in")
142    for opt in options:
143        key = opt["name"].replace("_", "-")
144        name = opt["name"]
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 ;;')
148        else:
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 ;;')
153            if allow_arg(opt):
154                print(f'    --enable-{key}=*) quote_sh "-D{name}=$2" ;;')
155    print("    *) return 1 ;;")
156    print("  esac")
157    print("}")
158
159
160options = load_options(json.load(sys.stdin))
161print("# This file is generated by meson-buildoptions.py, do not edit!")
162print_help(options)
163print_parse(options)
164