gen-dxd.py 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. # gen-dxd.py - Generate Doxygen Directives
  2. #
  3. # This code is in the Public Domain (or CC0 licensed, at your option.)
  4. # Unless required by applicable law or agreed to in writing, this
  5. # software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
  6. # CONDITIONS OF ANY KIND, either express or implied.
  7. #
  8. import sys
  9. import os
  10. import re
  11. # Determime build directory
  12. builddir = '_build'
  13. if 'BUILDDIR' in os.environ:
  14. builddir = os.environ['BUILDDIR']
  15. # Script configuration
  16. header_file_path_prefix = "../../components/"
  17. """string: path prefix for header files.
  18. """
  19. doxyfile_path = "../Doxyfile"
  20. """string: path to a file containing header files to processs.
  21. """
  22. xml_directory_path = "xml"
  23. """string: path to directory with XML files by Doxygen.
  24. """
  25. inc_directory_path = os.path.join(builddir, 'inc')
  26. """string: path prefix for header files.
  27. """
  28. all_kinds = [
  29. ("function", "Functions"),
  30. ("union", "Unions"),
  31. ("struct", "Structures"),
  32. ("define", "Macros"),
  33. ("typedef", "Type Definitions"),
  34. ("enum", "Enumerations")
  35. ]
  36. """list of items that will be generated for a single API file
  37. """
  38. def get_doxyfile_input():
  39. """Get contents of Doxyfile's INPUT statement.
  40. Returns:
  41. Contents of Doxyfile's INPUT.
  42. """
  43. if not os.path.isfile(doxyfile_path):
  44. print "Doxyfile '%s' does not exist!" % doxyfile_path
  45. sys.exit()
  46. print "Getting Doxyfile's INPUT"
  47. input_file = open(doxyfile_path, "r")
  48. line = input_file.readline()
  49. # read contents of Doxyfile until 'INPUT' statement
  50. while line:
  51. if line.find("INPUT") == 0:
  52. break
  53. line = input_file.readline()
  54. doxyfile_INPUT = ""
  55. line = input_file.readline()
  56. # skip input_file contents until end of 'INPUT' statement
  57. while line:
  58. if line.isspace():
  59. # we have reached the end of 'INPUT' statement
  60. break
  61. # process only lines that are not comments
  62. if line.find("#") == -1:
  63. # extract header file path inside components folder
  64. m = re.search(header_file_path_prefix + "(.*\.h)", line)
  65. header_file_path = m.group(1)
  66. doxyfile_INPUT += header_file_path + "\n"
  67. # proceed reading next line
  68. line = input_file.readline()
  69. input_file.close()
  70. return doxyfile_INPUT
  71. def get_api_name(header_file_path):
  72. """Get name of API from header file path.
  73. Args:
  74. header_file_path: path to the header file.
  75. Returns:
  76. The name of API.
  77. """
  78. api_name = ""
  79. regex = r".*/(.*)\.h"
  80. m = re.search(regex, header_file_path)
  81. if m:
  82. api_name = m.group(1)
  83. return api_name
  84. def get_rst_header(header_name):
  85. """Get rst formatted code with a header.
  86. Args:
  87. header_name: name of header.
  88. Returns:
  89. Formatted rst code with the header.
  90. """
  91. rst_output = ""
  92. rst_output += header_name + "\n"
  93. rst_output += "^" * len(header_name) + "\n"
  94. rst_output += "\n"
  95. return rst_output
  96. def select_unions(innerclass_list):
  97. """Select unions from innerclass list.
  98. Args:
  99. innerclass_list: raw list with unions and structures
  100. extracted from Dogygen's xml file.
  101. Returns:
  102. Doxygen directives with unions selected from the list.
  103. """
  104. rst_output = ""
  105. for line in innerclass_list.splitlines():
  106. # union is denoted by "union" at the beginning of line
  107. if line.find("union") == 0:
  108. union_id, union_name = re.split(r"\t+", line)
  109. rst_output += ".. doxygenunion:: "
  110. rst_output += union_name
  111. rst_output += "\n"
  112. return rst_output
  113. def select_structs(innerclass_list):
  114. """Select structures from innerclass list.
  115. Args:
  116. innerclass_list: raw list with unions and structures
  117. extracted from Dogygen's xml file.
  118. Returns:
  119. Doxygen directives with structures selected from the list.
  120. Note: some structures are excluded as described on code below.
  121. """
  122. rst_output = ""
  123. for line in innerclass_list.splitlines():
  124. # structure is denoted by "struct" at the beginning of line
  125. if line.find("struct") == 0:
  126. # skip structures that are part of union
  127. # they are documented by 'doxygenunion' directive
  128. if line.find("::") > 0:
  129. continue
  130. struct_id, struct_name = re.split(r"\t+", line)
  131. rst_output += ".. doxygenstruct:: "
  132. rst_output += struct_name
  133. rst_output += "\n"
  134. rst_output += " :members:\n"
  135. rst_output += "\n"
  136. return rst_output
  137. def get_directives(tree, kind):
  138. """Get directives for specific 'kind'.
  139. Args:
  140. tree: the ElementTree 'tree' of XML by Doxygen
  141. kind: name of API "kind" to be generated
  142. Returns:
  143. Doxygen directives for selected 'kind'.
  144. Note: the header with "kind" name is included.
  145. """
  146. rst_output = ""
  147. if kind in ["union", "struct"]:
  148. innerclass_list = ""
  149. for elem in tree.iterfind('compounddef/innerclass'):
  150. innerclass_list += elem.attrib["refid"] + "\t" + elem.text + "\n"
  151. if kind == "union":
  152. rst_output += select_unions(innerclass_list)
  153. else:
  154. rst_output += select_structs(innerclass_list)
  155. else:
  156. for elem in tree.iterfind(
  157. 'compounddef/sectiondef/memberdef[@kind="%s"]' % kind):
  158. name = elem.find('name')
  159. rst_output += ".. doxygen%s:: " % kind
  160. rst_output += name.text + "\n"
  161. if rst_output:
  162. all_kinds_dict = dict(all_kinds)
  163. rst_output = get_rst_header(all_kinds_dict[kind]) + rst_output + "\n"
  164. return rst_output
  165. def generate_directives(header_file_path):
  166. """Generate API reference with Doxygen directives for a header file.
  167. Args:
  168. header_file_path: a path to the header file with API.
  169. Returns:
  170. Doxygen directives for the header file.
  171. """
  172. api_name = get_api_name(header_file_path)
  173. # in XLT file name each "_" in the api name is expanded by Doxygen to "__"
  174. xlt_api_name = api_name.replace("_", "__")
  175. xml_file_path = "%s/%s_8h.xml" % (xml_directory_path, xlt_api_name)
  176. rst_output = ""
  177. rst_output = ".. File automatically generated by 'gen-dxd.py'\n"
  178. rst_output += "\n"
  179. rst_output += get_rst_header("Header File")
  180. rst_output += "* :component_file:`" + header_file_path + "`\n"
  181. rst_output += "\n"
  182. try:
  183. import xml.etree.cElementTree as ET
  184. except ImportError:
  185. import xml.etree.ElementTree as ET
  186. tree = ET.ElementTree(file=xml_file_path)
  187. for i in range(len(all_kinds)):
  188. kind = all_kinds[i][0]
  189. rst_output += get_directives(tree, kind)
  190. return rst_output
  191. def generate_api_inc_files():
  192. """Generate header_file.inc files
  193. with API reference made of doxygen directives
  194. for each header file
  195. specified in the 'INPUT' statement of Doxyfile.
  196. """
  197. if not os.path.isdir(xml_directory_path):
  198. print "Directory %s does not exist!" % xml_directory_path
  199. sys.exit()
  200. if not os.path.exists(inc_directory_path):
  201. os.makedirs(inc_directory_path)
  202. list_to_generate = get_doxyfile_input()
  203. print "Generating 'api_name.inc' files with Doxygen directives"
  204. for header_file_path in list_to_generate.splitlines():
  205. api_name = get_api_name(header_file_path)
  206. inc_file_path = inc_directory_path + "/" + api_name + ".inc"
  207. rst_output = generate_directives(header_file_path)
  208. previous_rst_output = ''
  209. if os.path.isfile(inc_file_path):
  210. with open(inc_file_path, "r") as inc_file_old:
  211. previous_rst_output = inc_file_old.read()
  212. if previous_rst_output != rst_output:
  213. with open(inc_file_path, "w") as inc_file:
  214. inc_file.write(rst_output)
  215. if __name__ == "__main__":
  216. """The main script that generates
  217. Doxygen directives.
  218. """
  219. # Process command line arguments, if any
  220. if len(sys.argv) > 1:
  221. if not os.path.isdir(xml_directory_path):
  222. print "Directory %s does not exist!" % xml_directory_path
  223. sys.exit()
  224. header_file_path = sys.argv[1]
  225. api_name = get_api_name(header_file_path)
  226. if api_name:
  227. rst_output = generate_directives(header_file_path)
  228. print "Doxygen directives for '%s'" % header_file_path
  229. print
  230. print rst_output
  231. else:
  232. print "Options to execute 'gen-dxd.py' application:"
  233. print "1: $ python gen-dxd.py"
  234. print " Generate API 'header_file.inc' files for headers defined in '%s'" % doxyfile_path
  235. print "2: $ python gen-dxd.py header_file_path"
  236. print " Print out Doxygen directives for a single header file"
  237. print " example: $ python gen-dxd.py mdns/include/mdns.h"
  238. print " NOTE: Run Doxygen first to get XML files for the header file"
  239. sys.exit()
  240. # No command line arguments given
  241. generate_api_inc_files()