The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

165 lines
6.0KB

  1. import os
  2. import shutil
  3. import re
  4. def get_curly_brace_scope_end(string, start_pos):
  5. """Given a string and the position of an opening curly brace, find the
  6. position of the closing brace.
  7. """
  8. if string[start_pos] != "{":
  9. raise ValueError("string must have \"{\" at start pos")
  10. string_end = len(string)
  11. bracket_counter = 1
  12. start_pos += 1
  13. while start_pos < string_end:
  14. if string[start_pos] == "{":
  15. bracket_counter += 1
  16. elif string[start_pos] == "}":
  17. bracket_counter -= 1
  18. if bracket_counter == 0:
  19. return start_pos
  20. start_pos += 1
  21. return -1
  22. def add_doxygen_group(path, group_name):
  23. """Add a Doxygen group to the file at 'path'.
  24. Namespaces cause all kinds of problems, and we need to ensure that if
  25. the classes in a source file are contained within a namespace then we
  26. also put the @weakgroup inside.
  27. """
  28. filename = os.path.basename(path)
  29. if filename.startswith("juce_") and filename.endswith(".h"):
  30. group_definition_start = ("\n/** @weakgroup "
  31. + group_name
  32. + "\n * @{\n */\n")
  33. group_definition_end = "\n/** @}*/\n"
  34. with open(path, "r") as f:
  35. content = f.read()
  36. # Put the group definitions inside all namespaces.
  37. namespace_regex = re.compile(r"\s+namespace\s+\S+\s+{")
  38. match = namespace_regex.search(content)
  39. while (match is not None):
  40. namespace_end = get_curly_brace_scope_end(content, match.end() - 1)
  41. if namespace_end == -1:
  42. raise ValueError("error finding end of namespace "
  43. + match.group()
  44. + " in "
  45. + path)
  46. content = (content[:match.end()]
  47. + group_definition_start
  48. + content[match.end():namespace_end]
  49. + group_definition_end
  50. + content[namespace_end:])
  51. search_start = (namespace_end
  52. + len(group_definition_start)
  53. + len(group_definition_end))
  54. match = namespace_regex.search(content, search_start)
  55. with open(path, "w") as f:
  56. f.write(group_definition_start)
  57. f.write(content)
  58. f.write(group_definition_end)
  59. ###############################################################################
  60. # Get the list of JUCE modules to include.
  61. juce_modules = []
  62. with open("../juce_modules.txt", "r") as f:
  63. for line in f:
  64. juce_modules.append(line.strip())
  65. # A temporary directory to hold our preprocessed source files.
  66. build_directory = "build"
  67. # Make sure we have a clean temporary directory.
  68. try:
  69. shutil.rmtree(build_directory)
  70. except OSError as e:
  71. if e.errno != 2:
  72. # An errno of 2 indicates that the directory does not exist, which is
  73. # fine!
  74. raise e
  75. # Copy the JUCE modules to the temporary directory, and process the source
  76. # files.
  77. module_definitions = []
  78. for module_name in juce_modules:
  79. # Copy the required modules.
  80. original_module_dir = os.path.join("..", "..", "..", "modules", module_name)
  81. module_path = os.path.join(build_directory, module_name)
  82. shutil.copytree(original_module_dir, module_path)
  83. # Parse the module header to get module information.
  84. module_header = os.path.join(module_path, module_name + ".h")
  85. with open (module_header, "r") as f:
  86. content = f.read()
  87. block_info_result = re.match(r".*BEGIN_JUCE_MODULE_DECLARATION"
  88. "(.*)"
  89. "END_JUCE_MODULE_DECLARATION.*",
  90. content,
  91. re.DOTALL)
  92. detail_lines = []
  93. for line in block_info_result.group(1).split("\n"):
  94. stripped_line = line.strip()
  95. if stripped_line:
  96. result = re.match(r"^.*?description:\s*(.*)$", stripped_line)
  97. if result:
  98. short_description = result.group(1)
  99. else:
  100. detail_lines.append(stripped_line)
  101. # The module header causes problems for Doxygen, so delete it.
  102. os.remove(module_header)
  103. # Create a Doxygen group definition for the module.
  104. module_definiton = []
  105. module_definiton.append("/** @defgroup {n} {n}".format(n=module_name))
  106. module_definiton.append(" {d}".format(d=short_description))
  107. module_definiton.append("")
  108. for line in detail_lines:
  109. module_definiton.append(" - {l}".format(l=line))
  110. module_definiton.append("")
  111. module_definiton.append(" @{")
  112. module_definiton.append("*/")
  113. # Create a list of internal directories we can use as subgroups and create
  114. # the Doxygen group hierarchy string.
  115. dir_contents = os.listdir(module_path)
  116. subdirs = [x for x in dir_contents
  117. if os.path.isdir(os.path.join(module_path, x))]
  118. module_groups = {}
  119. for subdir in subdirs:
  120. subgroup_name = "{n}-{s}".format(n=module_name, s=subdir)
  121. module_groups[subgroup_name] = os.path.join(module_path, subdir)
  122. module_definiton.append("")
  123. module_definiton.append(
  124. "/** @defgroup {tag} {n} */".format(tag=subgroup_name, n=subdir)
  125. )
  126. module_definiton.append("")
  127. module_definiton.append("/** @} */")
  128. module_definitions.append("\n".join(module_definiton))
  129. # Put the top level files into the main group.
  130. for filename in (set(dir_contents) - set(subdirs)):
  131. add_doxygen_group(os.path.join(module_path, filename), module_name)
  132. # Put subdirectory files into their respective groups.
  133. for group_name in module_groups:
  134. for dirpath, dirnames, filenames in os.walk(module_groups[group_name]):
  135. for filename in filenames:
  136. add_doxygen_group(os.path.join(dirpath, filename), group_name)
  137. # Create an extra header file containing the module hierarchy.
  138. with open(os.path.join(build_directory, "juce_modules.dox"), "w") as f:
  139. f.write("\n\n".join(module_definitions))