meson-buildoptions.py 6.1 KB

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