meson-buildoptions.py 6.7 KB

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