run_doxygen.py 9.7 KB

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