run_doxygen.py 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. # Extension to generate Doxygen XML include files, with IDF config & soc macros included
  2. from __future__ import print_function
  3. from __future__ import unicode_literals
  4. from io import open
  5. import os
  6. import os.path
  7. import re
  8. import subprocess
  9. from .util import copy_if_modified
  10. ALL_KINDS = [
  11. ("function", "Functions"),
  12. ("union", "Unions"),
  13. ("struct", "Structures"),
  14. ("define", "Macros"),
  15. ("typedef", "Type Definitions"),
  16. ("enum", "Enumerations")
  17. ]
  18. """list of items that will be generated for a single API file
  19. """
  20. def setup(app):
  21. # The idf_build_system extension will emit this event once it has generated documentation macro definitions
  22. app.connect('idf-defines-generated', generate_doxygen)
  23. return {'parallel_read_safe': True, 'parallel_write_safe': True, 'version': '0.2'}
  24. def generate_doxygen(app, defines):
  25. build_dir = os.path.dirname(app.doctreedir.rstrip(os.sep))
  26. # Call Doxygen to get XML files from the header files
  27. print("Calling Doxygen to generate latest XML files")
  28. doxy_env = os.environ
  29. doxy_env.update({
  30. "ENV_DOXYGEN_DEFINES": " ".join('{}={}'.format(key, value) for key, value in defines.items()),
  31. "IDF_PATH": app.config.idf_path,
  32. "IDF_TARGET": app.config.idf_target,
  33. })
  34. doxyfile = os.path.join(app.config.docs_root, "Doxyfile")
  35. print("Running doxygen with doxyfile {}".format(doxyfile))
  36. # It's possible to have doxygen log warnings to a file using WARN_LOGFILE directive,
  37. # but in some cases it will still log an error to stderr and return success!
  38. #
  39. # So take all of stderr and redirect it to a logfile (will contain warnings and errors)
  40. logfile = os.path.join(build_dir, "doxygen-warning-log.txt")
  41. with open(logfile, "w") as f:
  42. # note: run Doxygen in the build directory, so the xml & xml_in files end up in there
  43. subprocess.check_call(["doxygen", doxyfile], env=doxy_env, cwd=build_dir, stderr=f)
  44. # Doxygen has generated XML files in 'xml' directory.
  45. # Copy them to 'xml_in', only touching the files which have changed.
  46. copy_if_modified(os.path.join(build_dir, 'xml/'), os.path.join(build_dir, 'xml_in/'))
  47. # Generate 'api_name.inc' files from the Doxygen XML files
  48. convert_api_xml_to_inc(app, doxyfile)
  49. def convert_api_xml_to_inc(app, doxyfile):
  50. """ Generate header_file.inc files
  51. with API reference made of doxygen directives
  52. for each header file
  53. specified in the 'INPUT' statement of the Doxyfile.
  54. """
  55. build_dir = app.config.build_dir
  56. xml_directory_path = "{}/xml".format(build_dir)
  57. inc_directory_path = "{}/inc".format(build_dir)
  58. if not os.path.isdir(xml_directory_path):
  59. raise RuntimeError("Directory {} does not exist!".format(xml_directory_path))
  60. if not os.path.exists(inc_directory_path):
  61. os.makedirs(inc_directory_path)
  62. header_paths = get_doxyfile_input_paths(app, doxyfile)
  63. print("Generating 'api_name.inc' files with Doxygen directives")
  64. for header_file_path in header_paths:
  65. api_name = get_api_name(header_file_path)
  66. inc_file_path = inc_directory_path + "/" + api_name + ".inc"
  67. rst_output = generate_directives(header_file_path, xml_directory_path)
  68. previous_rst_output = ''
  69. if os.path.isfile(inc_file_path):
  70. with open(inc_file_path, "r", encoding='utf-8') as inc_file_old:
  71. previous_rst_output = inc_file_old.read()
  72. if previous_rst_output != rst_output:
  73. with open(inc_file_path, "w", encoding='utf-8') as inc_file:
  74. inc_file.write(rst_output)
  75. def get_doxyfile_input_paths(app, doxyfile_path):
  76. """Get contents of Doxyfile's INPUT statement.
  77. Returns:
  78. Contents of Doxyfile's INPUT.
  79. """
  80. if not os.path.isfile(doxyfile_path):
  81. raise RuntimeError("Doxyfile '{}' does not exist!".format(doxyfile_path))
  82. print("Getting Doxyfile's INPUT")
  83. with open(doxyfile_path, "r", encoding='utf-8') as input_file:
  84. line = input_file.readline()
  85. # read contents of Doxyfile until 'INPUT' statement
  86. while line:
  87. if line.find("INPUT") == 0:
  88. break
  89. line = input_file.readline()
  90. doxyfile_INPUT = []
  91. line = input_file.readline()
  92. # skip input_file contents until end of 'INPUT' statement
  93. while line:
  94. if line.isspace():
  95. # we have reached the end of 'INPUT' statement
  96. break
  97. # process only lines that are not comments
  98. if line.find("#") == -1:
  99. # extract header file path inside components folder
  100. m = re.search("components/(.*\.h)", line) # noqa: W605 - regular expression
  101. header_file_path = m.group(1)
  102. # Replace env variable used for multi target header
  103. header_file_path = header_file_path.replace("$(IDF_TARGET)", app.config.idf_target)
  104. doxyfile_INPUT.append(header_file_path)
  105. # proceed reading next line
  106. line = input_file.readline()
  107. return doxyfile_INPUT
  108. def get_api_name(header_file_path):
  109. """Get name of API from header file path.
  110. Args:
  111. header_file_path: path to the header file.
  112. Returns:
  113. The name of API.
  114. """
  115. api_name = ""
  116. regex = r".*/(.*)\.h"
  117. m = re.search(regex, header_file_path)
  118. if m:
  119. api_name = m.group(1)
  120. return api_name
  121. def generate_directives(header_file_path, xml_directory_path):
  122. """Generate API reference with Doxygen directives for a header file.
  123. Args:
  124. header_file_path: a path to the header file with API.
  125. Returns:
  126. Doxygen directives for the header file.
  127. """
  128. api_name = get_api_name(header_file_path)
  129. # in XLT file name each "_" in the api name is expanded by Doxygen to "__"
  130. xlt_api_name = api_name.replace("_", "__")
  131. xml_file_path = "%s/%s_8h.xml" % (xml_directory_path, xlt_api_name)
  132. rst_output = ""
  133. rst_output = ".. File automatically generated by 'gen-dxd.py'\n"
  134. rst_output += "\n"
  135. rst_output += get_rst_header("Header File")
  136. rst_output += "* :component_file:`" + header_file_path + "`\n"
  137. rst_output += "\n"
  138. try:
  139. import xml.etree.cElementTree as ET
  140. except ImportError:
  141. import xml.etree.ElementTree as ET
  142. tree = ET.ElementTree(file=xml_file_path)
  143. for kind, label in ALL_KINDS:
  144. rst_output += get_directives(tree, kind)
  145. return rst_output
  146. def get_rst_header(header_name):
  147. """Get rst formatted code with a header.
  148. Args:
  149. header_name: name of header.
  150. Returns:
  151. Formatted rst code with the header.
  152. """
  153. rst_output = ""
  154. rst_output += header_name + "\n"
  155. rst_output += "^" * len(header_name) + "\n"
  156. rst_output += "\n"
  157. return rst_output
  158. def select_unions(innerclass_list):
  159. """Select unions from innerclass list.
  160. Args:
  161. innerclass_list: raw list with unions and structures
  162. extracted from Dogygen's xml file.
  163. Returns:
  164. Doxygen directives with unions selected from the list.
  165. """
  166. rst_output = ""
  167. for line in innerclass_list.splitlines():
  168. # union is denoted by "union" at the beginning of line
  169. if line.find("union") == 0:
  170. union_id, union_name = re.split(r"\t+", line)
  171. rst_output += ".. doxygenunion:: "
  172. rst_output += union_name
  173. rst_output += "\n"
  174. return rst_output
  175. def select_structs(innerclass_list):
  176. """Select structures from innerclass list.
  177. Args:
  178. innerclass_list: raw list with unions and structures
  179. extracted from Dogygen's xml file.
  180. Returns:
  181. Doxygen directives with structures selected from the list.
  182. Note: some structures are excluded as described on code below.
  183. """
  184. rst_output = ""
  185. for line in innerclass_list.splitlines():
  186. # structure is denoted by "struct" at the beginning of line
  187. if line.find("struct") == 0:
  188. # skip structures that are part of union
  189. # they are documented by 'doxygenunion' directive
  190. if line.find("::") > 0:
  191. continue
  192. struct_id, struct_name = re.split(r"\t+", line)
  193. rst_output += ".. doxygenstruct:: "
  194. rst_output += struct_name
  195. rst_output += "\n"
  196. rst_output += " :members:\n"
  197. rst_output += "\n"
  198. return rst_output
  199. def get_directives(tree, kind):
  200. """Get directives for specific 'kind'.
  201. Args:
  202. tree: the ElementTree 'tree' of XML by Doxygen
  203. kind: name of API "kind" to be generated
  204. Returns:
  205. Doxygen directives for selected 'kind'.
  206. Note: the header with "kind" name is included.
  207. """
  208. rst_output = ""
  209. if kind in ["union", "struct"]:
  210. innerclass_list = ""
  211. for elem in tree.iterfind('compounddef/innerclass'):
  212. innerclass_list += elem.attrib["refid"] + "\t" + elem.text + "\n"
  213. if kind == "union":
  214. rst_output += select_unions(innerclass_list)
  215. else:
  216. rst_output += select_structs(innerclass_list)
  217. else:
  218. for elem in tree.iterfind(
  219. 'compounddef/sectiondef/memberdef[@kind="%s"]' % kind):
  220. name = elem.find('name')
  221. rst_output += ".. doxygen%s:: " % kind
  222. rst_output += name.text + "\n"
  223. if rst_output:
  224. all_kinds_dict = dict(ALL_KINDS)
  225. rst_output = get_rst_header(all_kinds_dict[kind]) + rst_output + "\n"
  226. return rst_output