2
0

meson-buildoptions.py 4.8 KB

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