meson-buildoptions.py 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. #! /usr/bin/env python3
  2. # Generate configure command line options handling code, based on Meson's
  3. # user build options introspection data
  4. #
  5. # Copyright (C) 2021 Red Hat, Inc.
  6. #
  7. # Author: Paolo Bonzini <pbonzini@redhat.com>
  8. #
  9. # This program is free software; you can redistribute it and/or modify
  10. # it under the terms of the GNU General Public License as published by
  11. # the Free Software Foundation; either version 2, or (at your option)
  12. # any later version.
  13. #
  14. # This program is distributed in the hope that it will be useful,
  15. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. # GNU General Public License for more details.
  18. #
  19. # You should have received a copy of the GNU General Public License
  20. # along with this program. If not, see <https://www.gnu.org/licenses/>.
  21. import json
  22. import textwrap
  23. import shlex
  24. import sys
  25. SKIP_OPTIONS = {
  26. "default_devices",
  27. "docdir",
  28. "fuzzing_engine",
  29. "qemu_firmwarepath",
  30. "qemu_suffix",
  31. "smbd",
  32. }
  33. OPTION_NAMES = {
  34. "malloc": "enable-malloc",
  35. "trace_backends": "enable-trace-backends",
  36. "trace_file": "with-trace-file",
  37. }
  38. BUILTIN_OPTIONS = {
  39. "strip",
  40. }
  41. LINE_WIDTH = 76
  42. # Convert the default value of an option to the string used in
  43. # the help message
  44. def value_to_help(value):
  45. if isinstance(value, list):
  46. return ",".join(value)
  47. if isinstance(value, bool):
  48. return "enabled" if value else "disabled"
  49. return str(value)
  50. def wrap(left, text, indent):
  51. spaces = " " * indent
  52. if len(left) >= indent:
  53. yield left
  54. left = spaces
  55. else:
  56. left = (left + spaces)[0:indent]
  57. yield from textwrap.wrap(
  58. text, width=LINE_WIDTH, initial_indent=left, subsequent_indent=spaces
  59. )
  60. def sh_print(line=""):
  61. print(' printf "%s\\n"', shlex.quote(line))
  62. def help_line(left, opt, indent, long):
  63. right = f'{opt["description"]}'
  64. if long:
  65. value = value_to_help(opt["value"])
  66. if value != "auto" and value != "":
  67. right += f" [{value}]"
  68. if "choices" in opt and long:
  69. choices = "/".join(sorted(opt["choices"]))
  70. right += f" (choices: {choices})"
  71. for x in wrap(" " + left, right, indent):
  72. sh_print(x)
  73. # Return whether the option (a dictionary) can be used with
  74. # arguments. Booleans can never be used with arguments;
  75. # combos allow an argument only if they accept other values
  76. # than "auto", "enabled", and "disabled".
  77. def allow_arg(opt):
  78. if opt["type"] == "boolean":
  79. return False
  80. if opt["type"] != "combo":
  81. return True
  82. return not (set(opt["choices"]) <= {"auto", "disabled", "enabled"})
  83. # Return whether the option (a dictionary) can be used without
  84. # arguments. Booleans can only be used without arguments;
  85. # combos require an argument if they accept neither "enabled"
  86. # nor "disabled"
  87. def require_arg(opt):
  88. if opt["type"] == "boolean":
  89. return False
  90. if opt["type"] != "combo":
  91. return True
  92. return not ({"enabled", "disabled"}.intersection(opt["choices"]))
  93. def filter_options(json):
  94. if ":" in json["name"]:
  95. return False
  96. if json["section"] == "user":
  97. return json["name"] not in SKIP_OPTIONS
  98. else:
  99. return json["name"] in BUILTIN_OPTIONS
  100. def load_options(json):
  101. json = [x for x in json if filter_options(x)]
  102. return sorted(json, key=lambda x: x["name"])
  103. def cli_option(opt):
  104. name = opt["name"]
  105. if name in OPTION_NAMES:
  106. return OPTION_NAMES[name]
  107. return name.replace("_", "-")
  108. def cli_help_key(opt):
  109. key = cli_option(opt)
  110. if require_arg(opt):
  111. return key
  112. if opt["type"] == "boolean" and opt["value"]:
  113. return f"disable-{key}"
  114. return f"enable-{key}"
  115. def cli_metavar(opt):
  116. if opt["type"] == "string":
  117. return "VALUE"
  118. if opt["type"] == "array":
  119. return "CHOICES"
  120. return "CHOICE"
  121. def print_help(options):
  122. print("meson_options_help() {")
  123. for opt in sorted(options, key=cli_help_key):
  124. key = cli_help_key(opt)
  125. # The first section includes options that have an arguments,
  126. # and booleans (i.e., only one of enable/disable makes sense)
  127. if require_arg(opt):
  128. metavar = cli_metavar(opt)
  129. left = f"--{key}={metavar}"
  130. help_line(left, opt, 27, True)
  131. elif opt["type"] == "boolean":
  132. left = f"--{key}"
  133. help_line(left, opt, 27, False)
  134. elif allow_arg(opt):
  135. if opt["type"] == "combo" and "enabled" in opt["choices"]:
  136. left = f"--{key}[=CHOICE]"
  137. else:
  138. left = f"--{key}=CHOICE"
  139. help_line(left, opt, 27, True)
  140. sh_print()
  141. sh_print("Optional features, enabled with --enable-FEATURE and")
  142. sh_print("disabled with --disable-FEATURE, default is enabled if available")
  143. sh_print("(unless built with --without-default-features):")
  144. sh_print()
  145. for opt in options:
  146. key = opt["name"].replace("_", "-")
  147. if opt["type"] != "boolean" and not allow_arg(opt):
  148. help_line(key, opt, 18, False)
  149. print("}")
  150. def print_parse(options):
  151. print("_meson_option_parse() {")
  152. print(" case $1 in")
  153. for opt in options:
  154. key = cli_option(opt)
  155. name = opt["name"]
  156. if require_arg(opt):
  157. print(f' --{key}=*) quote_sh "-D{name}=$2" ;;')
  158. elif opt["type"] == "boolean":
  159. print(f' --enable-{key}) printf "%s" -D{name}=true ;;')
  160. print(f' --disable-{key}) printf "%s" -D{name}=false ;;')
  161. else:
  162. if opt["type"] == "combo" and "enabled" in opt["choices"]:
  163. print(f' --enable-{key}) printf "%s" -D{name}=enabled ;;')
  164. if opt["type"] == "combo" and "disabled" in opt["choices"]:
  165. print(f' --disable-{key}) printf "%s" -D{name}=disabled ;;')
  166. if allow_arg(opt):
  167. print(f' --enable-{key}=*) quote_sh "-D{name}=$2" ;;')
  168. print(" *) return 1 ;;")
  169. print(" esac")
  170. print("}")
  171. options = load_options(json.load(sys.stdin))
  172. print("# This file is generated by meson-buildoptions.py, do not edit!")
  173. print_help(options)
  174. print_parse(options)