2
0

meson-buildoptions.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. "audio_drv_list",
  27. "default_devices",
  28. "docdir",
  29. "fuzzing_engine",
  30. "iasl",
  31. "qemu_firmwarepath",
  32. "qemu_suffix",
  33. "smbd",
  34. "sphinx_build",
  35. "trace_file",
  36. }
  37. BUILTIN_OPTIONS = {
  38. "strip",
  39. }
  40. LINE_WIDTH = 76
  41. # Convert the default value of an option to the string used in
  42. # the help message
  43. def value_to_help(value):
  44. if isinstance(value, list):
  45. return ",".join(value)
  46. if isinstance(value, bool):
  47. return "enabled" if value else "disabled"
  48. return str(value)
  49. def wrap(left, text, indent):
  50. spaces = " " * indent
  51. if len(left) >= indent:
  52. yield left
  53. left = spaces
  54. else:
  55. left = (left + spaces)[0:indent]
  56. yield from textwrap.wrap(
  57. text, width=LINE_WIDTH, initial_indent=left, subsequent_indent=spaces
  58. )
  59. def sh_print(line=""):
  60. print(' printf "%s\\n"', shlex.quote(line))
  61. def help_line(left, opt, indent, long):
  62. right = f'{opt["description"]}'
  63. if long:
  64. value = value_to_help(opt["value"])
  65. if value != "auto":
  66. right += f" [{value}]"
  67. if "choices" in opt and long:
  68. choices = "/".join(sorted(opt["choices"]))
  69. right += f" (choices: {choices})"
  70. for x in wrap(" " + left, right, indent):
  71. sh_print(x)
  72. # Return whether the option (a dictionary) can be used with
  73. # arguments. Booleans can never be used with arguments;
  74. # combos allow an argument only if they accept other values
  75. # than "auto", "enabled", and "disabled".
  76. def allow_arg(opt):
  77. if opt["type"] == "boolean":
  78. return False
  79. if opt["type"] != "combo":
  80. return True
  81. return not (set(opt["choices"]) <= {"auto", "disabled", "enabled"})
  82. def filter_options(json):
  83. if ":" in json["name"]:
  84. return False
  85. if json["section"] == "user":
  86. return json["name"] not in SKIP_OPTIONS
  87. else:
  88. return json["name"] in BUILTIN_OPTIONS
  89. def load_options(json):
  90. json = [x for x in json if filter_options(x)]
  91. return sorted(json, key=lambda x: x["name"])
  92. def print_help(options):
  93. print("meson_options_help() {")
  94. for opt in options:
  95. key = opt["name"].replace("_", "-")
  96. # The first section includes options that have an arguments,
  97. # and booleans (i.e., only one of enable/disable makes sense)
  98. if opt["type"] == "boolean":
  99. left = f"--disable-{key}" if opt["value"] else f"--enable-{key}"
  100. help_line(left, opt, 27, False)
  101. elif allow_arg(opt):
  102. if opt["type"] == "combo" and "enabled" in opt["choices"]:
  103. left = f"--enable-{key}[=CHOICE]"
  104. else:
  105. left = f"--enable-{key}=CHOICE"
  106. help_line(left, opt, 27, True)
  107. sh_print()
  108. sh_print("Optional features, enabled with --enable-FEATURE and")
  109. sh_print("disabled with --disable-FEATURE, default is enabled if available")
  110. sh_print("(unless built with --without-default-features):")
  111. sh_print()
  112. for opt in options:
  113. key = opt["name"].replace("_", "-")
  114. if opt["type"] != "boolean" and not allow_arg(opt):
  115. help_line(key, opt, 18, False)
  116. print("}")
  117. def print_parse(options):
  118. print("_meson_option_parse() {")
  119. print(" case $1 in")
  120. for opt in options:
  121. key = opt["name"].replace("_", "-")
  122. name = opt["name"]
  123. if opt["type"] == "boolean":
  124. print(f' --enable-{key}) printf "%s" -D{name}=true ;;')
  125. print(f' --disable-{key}) printf "%s" -D{name}=false ;;')
  126. else:
  127. if opt["type"] == "combo" and "enabled" in opt["choices"]:
  128. print(f' --enable-{key}) printf "%s" -D{name}=enabled ;;')
  129. if opt["type"] == "combo" and "disabled" in opt["choices"]:
  130. print(f' --disable-{key}) printf "%s" -D{name}=disabled ;;')
  131. if allow_arg(opt):
  132. print(f' --enable-{key}=*) quote_sh "-D{name}=$2" ;;')
  133. print(" *) return 1 ;;")
  134. print(" esac")
  135. print("}")
  136. options = load_options(json.load(sys.stdin))
  137. print("# This file is generated by meson-buildoptions.py, do not edit!")
  138. print_help(options)
  139. print_parse(options)