2
0

meson-buildoptions.py 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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. "coroutine_backend": "with-coroutine",
  35. "debug": "debug-info",
  36. "malloc": "enable-malloc",
  37. "pkgversion": "with-pkgversion",
  38. "qemu_firmwarepath": "firmwarepath",
  39. "trace_backends": "enable-trace-backends",
  40. "trace_file": "with-trace-file",
  41. }
  42. # Options that configure autodetects, even though meson defines them as boolean
  43. AUTO_OPTIONS = {
  44. "plugins",
  45. }
  46. BUILTIN_OPTIONS = {
  47. "b_coverage",
  48. "b_lto",
  49. "datadir",
  50. "debug",
  51. "includedir",
  52. "libdir",
  53. "libexecdir",
  54. "localedir",
  55. "localstatedir",
  56. "mandir",
  57. "strip",
  58. "sysconfdir",
  59. }
  60. LINE_WIDTH = 76
  61. # Convert the default value of an option to the string used in
  62. # the help message
  63. def get_help(opt):
  64. if opt["name"] == "libdir":
  65. return 'system default'
  66. value = opt["value"]
  67. if isinstance(value, list):
  68. return ",".join(value)
  69. if isinstance(value, bool):
  70. return "enabled" if value else "disabled"
  71. return str(value)
  72. def 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. def sh_print(line=""):
  83. print(' printf "%s\\n"', shlex.quote(line))
  84. def help_line(left, opt, indent, long):
  85. right = f'{opt["description"]}'
  86. if long:
  87. value = get_help(opt)
  88. if value != "auto" and value != "":
  89. right += f" [{value}]"
  90. if "choices" in opt and long:
  91. choices = "/".join(sorted(opt["choices"]))
  92. right += f" (choices: {choices})"
  93. for x in wrap(" " + left, right, indent):
  94. sh_print(x)
  95. # Return whether the option (a dictionary) can be used with
  96. # arguments. Booleans can never be used with arguments;
  97. # combos allow an argument only if they accept other values
  98. # than "auto", "enabled", and "disabled".
  99. def allow_arg(opt):
  100. if opt["type"] == "boolean":
  101. return False
  102. if opt["type"] != "combo":
  103. return True
  104. return not (set(opt["choices"]) <= {"auto", "disabled", "enabled"})
  105. # Return whether the option (a dictionary) can be used without
  106. # arguments. Booleans can only be used without arguments;
  107. # combos require an argument if they accept neither "enabled"
  108. # nor "disabled"
  109. def require_arg(opt):
  110. if opt["type"] == "boolean":
  111. return False
  112. if opt["type"] != "combo":
  113. return True
  114. return not ({"enabled", "disabled"}.intersection(opt["choices"]))
  115. def filter_options(json):
  116. if ":" in json["name"]:
  117. return False
  118. if json["section"] == "user":
  119. return json["name"] not in SKIP_OPTIONS
  120. else:
  121. return json["name"] in BUILTIN_OPTIONS
  122. def load_options(json):
  123. json = [x for x in json if filter_options(x)]
  124. return sorted(json, key=lambda x: x["name"])
  125. def cli_option(opt):
  126. name = opt["name"]
  127. if name in OPTION_NAMES:
  128. return OPTION_NAMES[name]
  129. return name.replace("_", "-")
  130. def cli_help_key(opt):
  131. key = cli_option(opt)
  132. if require_arg(opt):
  133. return key
  134. if opt["type"] == "boolean" and opt["value"]:
  135. return f"disable-{key}"
  136. return f"enable-{key}"
  137. def cli_metavar(opt):
  138. if opt["type"] == "string":
  139. return "VALUE"
  140. if opt["type"] == "array":
  141. return "CHOICES" if "choices" in opt else "VALUES"
  142. return "CHOICE"
  143. def print_help(options):
  144. print("meson_options_help() {")
  145. feature_opts = []
  146. for opt in sorted(options, key=cli_help_key):
  147. key = cli_help_key(opt)
  148. # The first section includes options that have an arguments,
  149. # and booleans (i.e., only one of enable/disable makes sense)
  150. if require_arg(opt):
  151. metavar = cli_metavar(opt)
  152. left = f"--{key}={metavar}"
  153. help_line(left, opt, 27, True)
  154. elif opt["type"] == "boolean" and opt["name"] not in AUTO_OPTIONS:
  155. left = f"--{key}"
  156. help_line(left, opt, 27, False)
  157. elif allow_arg(opt):
  158. if opt["type"] == "combo" and "enabled" in opt["choices"]:
  159. left = f"--{key}[=CHOICE]"
  160. else:
  161. left = f"--{key}=CHOICE"
  162. help_line(left, opt, 27, True)
  163. else:
  164. feature_opts.append(opt)
  165. sh_print()
  166. sh_print("Optional features, enabled with --enable-FEATURE and")
  167. sh_print("disabled with --disable-FEATURE, default is enabled if available")
  168. sh_print("(unless built with --without-default-features):")
  169. sh_print()
  170. for opt in sorted(feature_opts, key=cli_option):
  171. key = cli_option(opt)
  172. help_line(key, opt, 18, False)
  173. print("}")
  174. def print_parse(options):
  175. print("_meson_option_parse() {")
  176. print(" case $1 in")
  177. for opt in options:
  178. key = cli_option(opt)
  179. name = opt["name"]
  180. if require_arg(opt):
  181. if opt["type"] == "array" and not "choices" in opt:
  182. print(f' --{key}=*) quote_sh "-D{name}=$(meson_option_build_array $2)" ;;')
  183. else:
  184. print(f' --{key}=*) quote_sh "-D{name}=$2" ;;')
  185. elif opt["type"] == "boolean":
  186. print(f' --enable-{key}) printf "%s" -D{name}=true ;;')
  187. print(f' --disable-{key}) printf "%s" -D{name}=false ;;')
  188. else:
  189. if opt["type"] == "combo" and "enabled" in opt["choices"]:
  190. print(f' --enable-{key}) printf "%s" -D{name}=enabled ;;')
  191. if opt["type"] == "combo" and "disabled" in opt["choices"]:
  192. print(f' --disable-{key}) printf "%s" -D{name}=disabled ;;')
  193. if allow_arg(opt):
  194. print(f' --enable-{key}=*) quote_sh "-D{name}=$2" ;;')
  195. print(" *) return 1 ;;")
  196. print(" esac")
  197. print("}")
  198. options = load_options(json.load(sys.stdin))
  199. print("# This file is generated by meson-buildoptions.py, do not edit!")
  200. print_help(options)
  201. print_parse(options)