meson-buildoptions.py 6.6 KB

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