|
| 1 | +#file modified from esp-idf extension |
| 2 | +import os |
| 3 | +import os.path |
| 4 | +import re |
| 5 | +import subprocess |
| 6 | +#from util import copy_if_modified |
| 7 | + |
| 8 | +ALL_KINDS = [ |
| 9 | + ("function", "Functions"), |
| 10 | + ("union", "Unions"), |
| 11 | + ("struct", "Structures"), |
| 12 | + ("define", "Macros"), |
| 13 | + ("typedef", "Type Definitions"), |
| 14 | + ("enum", "Enumerations") |
| 15 | +] |
| 16 | +"""list of items that will be generated for a single API file |
| 17 | +""" |
| 18 | + |
| 19 | +def setup(app): |
| 20 | + app.connect('builder-inited', run_doxygen) |
| 21 | + return {'parallel_read_safe': True, 'parallel_write_safe': True, 'version': '1.0'} |
| 22 | + |
| 23 | +def run_doxygen(app): |
| 24 | + print("Running doxygen with Doxyfile") |
| 25 | + # note: run Doxygen in the build directory, so the xml & xml_in files end up in there |
| 26 | + subprocess.check_call(["doxygen", "Doxyfile"]) |
| 27 | + # Generate 'api_name.inc' files from the Doxygen XML files |
| 28 | + convert_api_xml_to_inc(app, "Doxyfile") |
| 29 | + |
| 30 | +def convert_api_xml_to_inc(app, doxyfile): |
| 31 | + """ Generate header_file.inc files |
| 32 | + with API reference made of doxygen directives |
| 33 | + for each header file |
| 34 | + specified in the 'INPUT' statement of the Doxyfile. |
| 35 | + """ |
| 36 | + |
| 37 | + xml_directory_path = "xml" |
| 38 | + inc_directory_path = "inc" |
| 39 | + |
| 40 | + if not os.path.isdir(xml_directory_path): |
| 41 | + raise RuntimeError("Directory {} does not exist!".format(xml_directory_path)) |
| 42 | + |
| 43 | + if not os.path.exists(inc_directory_path): |
| 44 | + os.makedirs(inc_directory_path) |
| 45 | + |
| 46 | + header_paths = get_doxyfile_input_paths(app, doxyfile) |
| 47 | + print("Generating 'api_name.inc' files with Doxygen directives") |
| 48 | + for header_file_path in header_paths: |
| 49 | + api_name = get_api_name(header_file_path) |
| 50 | + inc_file_path = inc_directory_path + "/" + api_name + ".inc" |
| 51 | + rst_output = generate_directives(header_file_path, xml_directory_path) |
| 52 | + |
| 53 | + previous_rst_output = '' |
| 54 | + if os.path.isfile(inc_file_path): |
| 55 | + with open(inc_file_path, "r", encoding='utf-8') as inc_file_old: |
| 56 | + previous_rst_output = inc_file_old.read() |
| 57 | + |
| 58 | + if previous_rst_output != rst_output: |
| 59 | + with open(inc_file_path, "w", encoding='utf-8') as inc_file: |
| 60 | + inc_file.write(rst_output) |
| 61 | + |
| 62 | + |
| 63 | +def get_doxyfile_input_paths(app, doxyfile_path): |
| 64 | + """Get contents of Doxyfile's INPUT statement. |
| 65 | +
|
| 66 | + Returns: |
| 67 | + Contents of Doxyfile's INPUT. |
| 68 | +
|
| 69 | + """ |
| 70 | + if not os.path.isfile(doxyfile_path): |
| 71 | + raise RuntimeError("Doxyfile '{}' does not exist!".format(doxyfile_path)) |
| 72 | + |
| 73 | + print("Getting Doxyfile's INPUT") |
| 74 | + |
| 75 | + with open(doxyfile_path, "r", encoding='utf-8') as input_file: |
| 76 | + line = input_file.readline() |
| 77 | + # read contents of Doxyfile until 'INPUT' statement |
| 78 | + while line: |
| 79 | + if line.find("INPUT") == 0: |
| 80 | + break |
| 81 | + line = input_file.readline() |
| 82 | + |
| 83 | + doxyfile_INPUT = [] |
| 84 | + line = input_file.readline() |
| 85 | + # skip input_file contents until end of 'INPUT' statement |
| 86 | + while line: |
| 87 | + if line.isspace(): |
| 88 | + # we have reached the end of 'INPUT' statement |
| 89 | + break |
| 90 | + # process only lines that are not comments |
| 91 | + if line.find("#") == -1: |
| 92 | + # extract header file path inside components folder |
| 93 | + m = re.search("include/(.*\.h)", line) # noqa: W605 - regular expression |
| 94 | + header_file_path = m.group(0) |
| 95 | + |
| 96 | + doxyfile_INPUT.append(header_file_path) |
| 97 | + |
| 98 | + # proceed reading next line |
| 99 | + line = input_file.readline() |
| 100 | + |
| 101 | + return doxyfile_INPUT |
| 102 | + |
| 103 | + |
| 104 | +def get_api_name(header_file_path): |
| 105 | + """Get name of API from header file path. |
| 106 | +
|
| 107 | + Args: |
| 108 | + header_file_path: path to the header file. |
| 109 | +
|
| 110 | + Returns: |
| 111 | + The name of API. |
| 112 | +
|
| 113 | + """ |
| 114 | + api_name = "" |
| 115 | + regex = r".*/(.*)\.h" |
| 116 | + m = re.search(regex, header_file_path) |
| 117 | + if m: |
| 118 | + api_name = m.group(1) |
| 119 | + |
| 120 | + return api_name |
| 121 | + |
| 122 | + |
| 123 | +def generate_directives(header_file_path, xml_directory_path): |
| 124 | + """Generate API reference with Doxygen directives for a header file. |
| 125 | +
|
| 126 | + Args: |
| 127 | + header_file_path: a path to the header file with API. |
| 128 | +
|
| 129 | + Returns: |
| 130 | + Doxygen directives for the header file. |
| 131 | +
|
| 132 | + """ |
| 133 | + |
| 134 | + api_name = get_api_name(header_file_path) |
| 135 | + |
| 136 | + # in XLT file name each "_" in the api name is expanded by Doxygen to "__" |
| 137 | + xlt_api_name = api_name.replace("_", "__") |
| 138 | + xml_file_path = "%s/%s_8h.xml" % (xml_directory_path, xlt_api_name) |
| 139 | + |
| 140 | + rst_output = "" |
| 141 | + rst_output = ".. File automatically generated by 'gen-dxd.py'\n" |
| 142 | + rst_output += "\n" |
| 143 | + rst_output += get_rst_header("Header File") |
| 144 | + rst_output += "* :header_src_file:`" + header_file_path + "`\n" |
| 145 | + rst_output += "\n" |
| 146 | + |
| 147 | + try: |
| 148 | + import xml.etree.cElementTree as ET |
| 149 | + except ImportError: |
| 150 | + import xml.etree.ElementTree as ET |
| 151 | + |
| 152 | + tree = ET.ElementTree(file=xml_file_path) |
| 153 | + for kind, label in ALL_KINDS: |
| 154 | + rst_output += get_directives(tree, kind) |
| 155 | + |
| 156 | + return rst_output |
| 157 | + |
| 158 | + |
| 159 | +def get_rst_header(header_name): |
| 160 | + """Get rst formatted code with a header. |
| 161 | +
|
| 162 | + Args: |
| 163 | + header_name: name of header. |
| 164 | +
|
| 165 | + Returns: |
| 166 | + Formatted rst code with the header. |
| 167 | +
|
| 168 | + """ |
| 169 | + |
| 170 | + rst_output = "" |
| 171 | + rst_output += header_name + "\n" |
| 172 | + rst_output += "^" * len(header_name) + "\n" |
| 173 | + rst_output += "\n" |
| 174 | + |
| 175 | + return rst_output |
| 176 | + |
| 177 | + |
| 178 | +def select_unions(innerclass_list): |
| 179 | + """Select unions from innerclass list. |
| 180 | +
|
| 181 | + Args: |
| 182 | + innerclass_list: raw list with unions and structures |
| 183 | + extracted from Dogygen's xml file. |
| 184 | +
|
| 185 | + Returns: |
| 186 | + Doxygen directives with unions selected from the list. |
| 187 | +
|
| 188 | + """ |
| 189 | + |
| 190 | + rst_output = "" |
| 191 | + for line in innerclass_list.splitlines(): |
| 192 | + # union is denoted by "union" at the beginning of line |
| 193 | + if line.find("union") == 0: |
| 194 | + union_id, union_name = re.split(r"\t+", line) |
| 195 | + rst_output += ".. doxygenunion:: " |
| 196 | + rst_output += union_name |
| 197 | + rst_output += "\n" |
| 198 | + |
| 199 | + return rst_output |
| 200 | + |
| 201 | + |
| 202 | +def select_structs(innerclass_list): |
| 203 | + """Select structures from innerclass list. |
| 204 | +
|
| 205 | + Args: |
| 206 | + innerclass_list: raw list with unions and structures |
| 207 | + extracted from Dogygen's xml file. |
| 208 | +
|
| 209 | + Returns: |
| 210 | + Doxygen directives with structures selected from the list. |
| 211 | + Note: some structures are excluded as described on code below. |
| 212 | +
|
| 213 | + """ |
| 214 | + |
| 215 | + rst_output = "" |
| 216 | + for line in innerclass_list.splitlines(): |
| 217 | + # structure is denoted by "struct" at the beginning of line |
| 218 | + if line.find("struct") == 0: |
| 219 | + # skip structures that are part of union |
| 220 | + # they are documented by 'doxygenunion' directive |
| 221 | + if line.find("::") > 0: |
| 222 | + continue |
| 223 | + struct_id, struct_name = re.split(r"\t+", line) |
| 224 | + rst_output += ".. doxygenstruct:: " |
| 225 | + rst_output += struct_name |
| 226 | + rst_output += "\n" |
| 227 | + rst_output += " :members:\n" |
| 228 | + rst_output += "\n" |
| 229 | + |
| 230 | + return rst_output |
| 231 | + |
| 232 | + |
| 233 | +def get_directives(tree, kind): |
| 234 | + """Get directives for specific 'kind'. |
| 235 | +
|
| 236 | + Args: |
| 237 | + tree: the ElementTree 'tree' of XML by Doxygen |
| 238 | + kind: name of API "kind" to be generated |
| 239 | +
|
| 240 | + Returns: |
| 241 | + Doxygen directives for selected 'kind'. |
| 242 | + Note: the header with "kind" name is included. |
| 243 | +
|
| 244 | + """ |
| 245 | + |
| 246 | + rst_output = "" |
| 247 | + if kind in ["union", "struct"]: |
| 248 | + innerclass_list = "" |
| 249 | + for elem in tree.iterfind('compounddef/innerclass'): |
| 250 | + innerclass_list += elem.attrib["refid"] + "\t" + elem.text + "\n" |
| 251 | + if kind == "union": |
| 252 | + rst_output += select_unions(innerclass_list) |
| 253 | + else: |
| 254 | + rst_output += select_structs(innerclass_list) |
| 255 | + else: |
| 256 | + for elem in tree.iterfind( |
| 257 | + 'compounddef/sectiondef/memberdef[@kind="%s"]' % kind): |
| 258 | + name = elem.find('name') |
| 259 | + rst_output += ".. doxygen%s:: " % kind |
| 260 | + rst_output += name.text + "\n" |
| 261 | + if rst_output: |
| 262 | + all_kinds_dict = dict(ALL_KINDS) |
| 263 | + rst_output = get_rst_header(all_kinds_dict[kind]) + rst_output + "\n" |
| 264 | + |
| 265 | + return rst_output |
0 commit comments