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.

181 lines
6.7KB

  1. #!/usr/bin/env python
  2. import os
  3. import shutil
  4. import re
  5. import argparse
  6. def get_curly_brace_scope_end(string, start_pos):
  7. """Given a string and a starting position of an opening brace, find the
  8. position of the closing brace.
  9. """
  10. start_pos += 1
  11. string_end = len(string)
  12. bracket_counter = 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 remove_juce_namespaces(source):
  23. """Return a string of source code with any juce namespaces removed.
  24. """
  25. namespace_regex = re.compile(r"\s+namespace\s+juce\s*{")
  26. match = namespace_regex.search(source)
  27. while (match is not None):
  28. source = source[:match.start()] + source[match.end():]
  29. end = get_curly_brace_scope_end(source, match.start() - 1)
  30. if end != -1:
  31. source = source[:end] + source[end + 1:]
  32. match = namespace_regex.search(source)
  33. continue
  34. else:
  35. raise ValueError("failed to find the end of the "
  36. + match.group(1) + " namespace")
  37. return source
  38. def add_doxygen_group(path, group_name):
  39. """Add a Doxygen group to the file at 'path'.
  40. The addition of juce namespacing code to all of the source files breaks
  41. backwards compatibility by changing the doc URLs, so we need to remove
  42. the namespaces.
  43. """
  44. filename = os.path.basename(path)
  45. if re.match(r"^juce_.*\.(h|dox)", filename):
  46. with open(path, "r") as f:
  47. content = f.read()
  48. with open(path, "w") as f:
  49. f.write("\r\n/** @weakgroup " + group_name + "\r\n * @{\r\n */\r\n")
  50. f.write(remove_juce_namespaces(content))
  51. f.write("\r\n/** @}*/\r\n")
  52. ###############################################################################
  53. if __name__ == "__main__":
  54. parser = argparse.ArgumentParser()
  55. parser.add_argument("source_dir",
  56. help="the directory to search for source files")
  57. parser.add_argument("dest_dir",
  58. help="the directory in which to place processed files")
  59. parser.add_argument("--subdirs",
  60. help="if specified, only include these comma separated"
  61. "subdirectories")
  62. args = parser.parse_args()
  63. try:
  64. shutil.rmtree(args.dest_dir)
  65. except OSError:
  66. pass
  67. except FileNotFoundError:
  68. pass
  69. # Get the list of JUCE modules to include.
  70. if args.subdirs:
  71. juce_modules = args.subdirs.split(",")
  72. else:
  73. juce_modules = []
  74. for item in sorted(os.listdir(args.source_dir)):
  75. if os.path.isdir(os.path.join(args.source_dir, item)):
  76. juce_modules.append(item)
  77. # Copy the JUCE modules to the temporary directory, and process the source
  78. # files.
  79. module_definitions = []
  80. for module_name in juce_modules:
  81. # Copy the required modules.
  82. original_module_dir = os.path.join(args.source_dir, module_name)
  83. module_path = os.path.join(args.dest_dir, module_name)
  84. shutil.copytree(original_module_dir, module_path)
  85. # Parse the module header to get module information.
  86. module_header = os.path.join(module_path, module_name + ".h")
  87. with open(module_header, "r") as f:
  88. content = f.read()
  89. block_info_result = re.match(r".*BEGIN_JUCE_MODULE_DECLARATION"
  90. "(.*)"
  91. "END_JUCE_MODULE_DECLARATION.*",
  92. content,
  93. re.DOTALL)
  94. detail_lines = []
  95. for line in block_info_result.group(1).split("\n"):
  96. stripped_line = line.strip()
  97. if stripped_line:
  98. result = re.match(r"^.*?description:\s*(.*)$", stripped_line)
  99. if result:
  100. short_description = result.group(1)
  101. else:
  102. detail_lines.append(stripped_line)
  103. # The module header causes problems for Doxygen, so delete it.
  104. os.remove(module_header)
  105. # Create a Doxygen group definition for the module.
  106. module_definiton = []
  107. module_definiton.append("/** @defgroup {n} {n}".format(n=module_name))
  108. module_definiton.append(" {d}".format(d=short_description))
  109. module_definiton.append("")
  110. for line in detail_lines:
  111. module_definiton.append(" - {l}".format(l=line))
  112. module_definiton.append("")
  113. module_definiton.append(" @{")
  114. module_definiton.append("*/")
  115. # Create a list of the directories in the module that we can use as
  116. # subgroups and create the Doxygen group hierarchy string.
  117. dir_contents = sorted(os.listdir(module_path))
  118. # Ignore "native" folders as these are excluded by doxygen.
  119. try:
  120. dir_contents.remove("native")
  121. except ValueError:
  122. pass
  123. subdirs = []
  124. for item in dir_contents:
  125. if (os.path.isdir(os.path.join(module_path, item))):
  126. subdirs.append(item)
  127. module_groups = {}
  128. for subdir in subdirs:
  129. subgroup_name = "{n}-{s}".format(n=module_name, s=subdir)
  130. module_groups[subgroup_name] = os.path.join(module_path, subdir)
  131. module_definiton.append("")
  132. module_definiton.append(
  133. "/** @defgroup {tag} {n} */".format(tag=subgroup_name, n=subdir)
  134. )
  135. module_definiton.append("")
  136. module_definiton.append("/** @} */")
  137. module_definitions.append("\r\n".join(module_definiton))
  138. # Put the top level files into the main group.
  139. for filename in (set(dir_contents) - set(subdirs)):
  140. add_doxygen_group(os.path.join(module_path, filename), module_name)
  141. # Put subdirectory files into their respective groups.
  142. for group_name in module_groups:
  143. for dirpath, dirnames, filenames in os.walk(module_groups[group_name]):
  144. for filename in filenames:
  145. filepath = os.path.join(dirpath, filename)
  146. add_doxygen_group(filepath, group_name)
  147. # Create an extra header file containing the module hierarchy.
  148. with open(os.path.join(args.dest_dir, "juce_modules.dox"), "w") as f:
  149. f.write("\r\n\r\n".join(module_definitions))
  150. # Copy markdown docs
  151. for name in ["JUCE Module Format.md", "CMake API.md"]:
  152. shutil.copyfile(os.path.join(args.source_dir, "..", "docs", name),
  153. os.path.join(args.dest_dir, name))