meson-buildoptions.py 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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. "fuzzing_engine",
  28. "qemu_suffix",
  29. "smbd",
  30. }
  31. OPTION_NAMES = {
  32. "b_coverage": "gcov",
  33. "b_lto": "lto",
  34. "malloc": "enable-malloc",
  35. "pkgversion": "with-pkgversion",
  36. "qemu_firmwarepath": "firmwarepath",
  37. "trace_backends": "enable-trace-backends",
  38. "trace_file": "with-trace-file",
  39. }
  40. BUILTIN_OPTIONS = {
  41. "b_coverage",
  42. "b_lto",
  43. "datadir",
  44. "includedir",
  45. "libdir",
  46. "libexecdir",
  47. "localedir",
  48. "localstatedir",
  49. "mandir",
  50. "strip",
  51. "sysconfdir",
  52. }
  53. LINE_WIDTH = 76
  54. # Convert the default value of an option to the string used in
  55. # the help message
  56. def value_to_help(value):
  57. if isinstance(value, list):
  58. return ",".join(value)
  59. if isinstance(value, bool):
  60. return "enabled" if value else "disabled"
  61. return str(value)
  62. def wrap(left, text, indent):
  63. spaces = " " * indent
  64. if len(left) >= indent:
  65. yield left
  66. left = spaces
  67. else:
  68. left = (left + spaces)[0:indent]
  69. yield from textwrap.wrap(
  70. text, width=LINE_WIDTH, initial_indent=left, subsequent_indent=spaces
  71. )
  72. def sh_print(line=""):
  73. print(' printf "%s\\n"', shlex.quote(line))
  74. def help_line(left, opt, indent, long):
  75. right = f'{opt["description"]}'
  76. if long:
  77. value = value_to_help(opt["value"])
  78. if value != "auto" and value != "":
  79. right += f" [{value}]"
  80. if "choices" in opt and long:
  81. choices = "/".join(sorted(opt["choices"]))
  82. right += f" (choices: {choices})"
  83. for x in wrap(" " + left, right, indent):
  84. sh_print(x)
  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".
  89. def 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. # Return whether the option (a dictionary) can be used without
  96. # arguments. Booleans can only be used without arguments;
  97. # combos require an argument if they accept neither "enabled"
  98. # nor "disabled"
  99. def require_arg(opt):
  100. if opt["type"] == "boolean":
  101. return False
  102. if opt["type"] != "combo":
  103. return True
  104. return not ({"enabled", "disabled"}.intersection(opt["choices"]))
  105. def filter_options(json):
  106. if ":" in json["name"]:
  107. return False
  108. if json["section"] == "user":
  109. return json["name"] not in SKIP_OPTIONS
  110. else:
  111. return json["name"] in BUILTIN_OPTIONS
  112. def load_options(json):
  113. json = [x for x in json if filter_options(x)]
  114. return sorted(json, key=lambda x: x["name"])
  115. def cli_option(opt):
  116. name = opt["name"]
  117. if name in OPTION_NAMES:
  118. return OPTION_NAMES[name]
  119. return name.replace("_", "-")
  120. def cli_help_key(opt):
  121. key = cli_option(opt)
  122. if require_arg(opt):
  123. return key
  124. if opt["type"] == "boolean" and opt["value"]:
  125. return f"disable-{key}"
  126. return f"enable-{key}"
  127. def cli_metavar(opt):
  128. if opt["type"] == "string":
  129. return "VALUE"
  130. if opt["type"] == "array":
  131. return "CHOICES"
  132. return "CHOICE"
  133. def print_help(options):
  134. print("meson_options_help() {")
  135. for opt in sorted(options, key=cli_help_key):
  136. key = cli_help_key(opt)
  137. # The first section includes options that have an arguments,
  138. # and booleans (i.e., only one of enable/disable makes sense)
  139. if require_arg(opt):
  140. metavar = cli_metavar(opt)
  141. left = f"--{key}={metavar}"
  142. help_line(left, opt, 27, True)
  143. elif opt["type"] == "boolean":
  144. left = f"--{key}"
  145. help_line(left, opt, 27, False)
  146. elif allow_arg(opt):
  147. if opt["type"] == "combo" and "enabled" in opt["choices"]:
  148. left = f"--{key}[=CHOICE]"
  149. else:
  150. left = f"--{key}=CHOICE"
  151. help_line(left, opt, 27, True)
  152. sh_print()
  153. sh_print("Optional features, enabled with --enable-FEATURE and")
  154. sh_print("disabled with --disable-FEATURE, default is enabled if available")
  155. sh_print("(unless built with --without-default-features):")
  156. sh_print()
  157. for opt in options:
  158. key = opt["name"].replace("_", "-")
  159. if opt["type"] != "boolean" and not allow_arg(opt):
  160. help_line(key, opt, 18, False)
  161. print("}")
  162. def print_parse(options):
  163. print("_meson_option_parse() {")
  164. print(" case $1 in")
  165. for opt in options:
  166. key = cli_option(opt)
  167. name = opt["name"]
  168. if require_arg(opt):
  169. print(f' --{key}=*) quote_sh "-D{name}=$2" ;;')
  170. elif opt["type"] == "boolean":
  171. print(f' --enable-{key}) printf "%s" -D{name}=true ;;')
  172. print(f' --disable-{key}) printf "%s" -D{name}=false ;;')
  173. else:
  174. if opt["type"] == "combo" and "enabled" in opt["choices"]:
  175. print(f' --enable-{key}) printf "%s" -D{name}=enabled ;;')
  176. if opt["type"] == "combo" and "disabled" in opt["choices"]:
  177. print(f' --disable-{key}) printf "%s" -D{name}=disabled ;;')
  178. if allow_arg(opt):
  179. print(f' --enable-{key}=*) quote_sh "-D{name}=$2" ;;')
  180. print(" *) return 1 ;;")
  181. print(" esac")
  182. print("}")
  183. options = load_options(json.load(sys.stdin))
  184. print("# This file is generated by meson-buildoptions.py, do not edit!")
  185. print_help(options)
  186. print_parse(options)