|
| 1 | +"""Functions used to generate source files during build time |
| 2 | +
|
| 3 | +All such functions are invoked in a subprocess on Windows to prevent build flakiness. |
| 4 | +
|
| 5 | +""" |
| 6 | +from platform_methods import subprocess_main |
| 7 | +from compat import iteritems, itervalues, open_utf8, escape_string |
| 8 | + |
| 9 | + |
| 10 | +def make_authors_header(target, source, env): |
| 11 | + sections = ["Project Founders", "Lead Developer", "Project Manager", "Developers"] |
| 12 | + sections_id = ["AUTHORS_FOUNDERS", "AUTHORS_LEAD_DEVELOPERS", "AUTHORS_PROJECT_MANAGERS", "AUTHORS_DEVELOPERS"] |
| 13 | + |
| 14 | + src = source[0] |
| 15 | + dst = target[0] |
| 16 | + f = open_utf8(src, "r") |
| 17 | + g = open_utf8(dst, "w") |
| 18 | + |
| 19 | + g.write("/* THIS FILE IS GENERATED DO NOT EDIT */\n") |
| 20 | + g.write("#ifndef _EDITOR_AUTHORS_H\n") |
| 21 | + g.write("#define _EDITOR_AUTHORS_H\n") |
| 22 | + |
| 23 | + reading = False |
| 24 | + |
| 25 | + def close_section(): |
| 26 | + g.write("\t0\n") |
| 27 | + g.write("};\n") |
| 28 | + |
| 29 | + for line in f: |
| 30 | + if reading: |
| 31 | + if line.startswith(" "): |
| 32 | + g.write("\t\"" + escape_string(line.strip()) + "\",\n") |
| 33 | + continue |
| 34 | + if line.startswith("## "): |
| 35 | + if reading: |
| 36 | + close_section() |
| 37 | + reading = False |
| 38 | + for section, section_id in zip(sections, sections_id): |
| 39 | + if line.strip().endswith(section): |
| 40 | + current_section = escape_string(section_id) |
| 41 | + reading = True |
| 42 | + g.write("const char *const " + current_section + "[] = {\n") |
| 43 | + break |
| 44 | + |
| 45 | + if reading: |
| 46 | + close_section() |
| 47 | + |
| 48 | + g.write("#endif\n") |
| 49 | + |
| 50 | + g.close() |
| 51 | + f.close() |
| 52 | + |
| 53 | + |
| 54 | +def make_donors_header(target, source, env): |
| 55 | + sections = ["Platinum sponsors", "Gold sponsors", "Mini sponsors", |
| 56 | + "Gold donors", "Silver donors", "Bronze donors"] |
| 57 | + sections_id = ["DONORS_SPONSOR_PLAT", "DONORS_SPONSOR_GOLD", "DONORS_SPONSOR_MINI", |
| 58 | + "DONORS_GOLD", "DONORS_SILVER", "DONORS_BRONZE"] |
| 59 | + |
| 60 | + src = source[0] |
| 61 | + dst = target[0] |
| 62 | + f = open_utf8(src, "r") |
| 63 | + g = open_utf8(dst, "w") |
| 64 | + |
| 65 | + g.write("/* THIS FILE IS GENERATED DO NOT EDIT */\n") |
| 66 | + g.write("#ifndef _EDITOR_DONORS_H\n") |
| 67 | + g.write("#define _EDITOR_DONORS_H\n") |
| 68 | + |
| 69 | + reading = False |
| 70 | + |
| 71 | + def close_section(): |
| 72 | + g.write("\t0\n") |
| 73 | + g.write("};\n") |
| 74 | + |
| 75 | + for line in f: |
| 76 | + if reading >= 0: |
| 77 | + if line.startswith(" "): |
| 78 | + g.write("\t\"" + escape_string(line.strip()) + "\",\n") |
| 79 | + continue |
| 80 | + if line.startswith("## "): |
| 81 | + if reading: |
| 82 | + close_section() |
| 83 | + reading = False |
| 84 | + for section, section_id in zip(sections, sections_id): |
| 85 | + if line.strip().endswith(section): |
| 86 | + current_section = escape_string(section_id) |
| 87 | + reading = True |
| 88 | + g.write("const char *const " + current_section + "[] = {\n") |
| 89 | + break |
| 90 | + |
| 91 | + if reading: |
| 92 | + close_section() |
| 93 | + |
| 94 | + g.write("#endif\n") |
| 95 | + |
| 96 | + g.close() |
| 97 | + f.close() |
| 98 | + |
| 99 | + |
| 100 | +def make_license_header(target, source, env): |
| 101 | + src_copyright = source[0] |
| 102 | + src_license = source[1] |
| 103 | + dst = target[0] |
| 104 | + |
| 105 | + class LicenseReader: |
| 106 | + def __init__(self, license_file): |
| 107 | + self._license_file = license_file |
| 108 | + self.line_num = 0 |
| 109 | + self.current = self.next_line() |
| 110 | + |
| 111 | + def next_line(self): |
| 112 | + line = self._license_file.readline() |
| 113 | + self.line_num += 1 |
| 114 | + while line.startswith("#"): |
| 115 | + line = self._license_file.readline() |
| 116 | + self.line_num += 1 |
| 117 | + self.current = line |
| 118 | + return line |
| 119 | + |
| 120 | + def next_tag(self): |
| 121 | + if not ':' in self.current: |
| 122 | + return ('', []) |
| 123 | + tag, line = self.current.split(":", 1) |
| 124 | + lines = [line.strip()] |
| 125 | + while self.next_line() and self.current.startswith(" "): |
| 126 | + lines.append(self.current.strip()) |
| 127 | + return (tag, lines) |
| 128 | + |
| 129 | + from collections import OrderedDict |
| 130 | + projects = OrderedDict() |
| 131 | + license_list = [] |
| 132 | + |
| 133 | + with open_utf8(src_copyright, "r") as copyright_file: |
| 134 | + reader = LicenseReader(copyright_file) |
| 135 | + part = {} |
| 136 | + while reader.current: |
| 137 | + tag, content = reader.next_tag() |
| 138 | + if tag in ("Files", "Copyright", "License"): |
| 139 | + part[tag] = content[:] |
| 140 | + elif tag == "Comment": |
| 141 | + # attach part to named project |
| 142 | + projects[content[0]] = projects.get(content[0], []) + [part] |
| 143 | + |
| 144 | + if not tag or not reader.current: |
| 145 | + # end of a paragraph start a new part |
| 146 | + if "License" in part and not "Files" in part: |
| 147 | + # no Files tag in this one, so assume standalone license |
| 148 | + license_list.append(part["License"]) |
| 149 | + part = {} |
| 150 | + reader.next_line() |
| 151 | + |
| 152 | + data_list = [] |
| 153 | + for project in itervalues(projects): |
| 154 | + for part in project: |
| 155 | + part["file_index"] = len(data_list) |
| 156 | + data_list += part["Files"] |
| 157 | + part["copyright_index"] = len(data_list) |
| 158 | + data_list += part["Copyright"] |
| 159 | + |
| 160 | + with open_utf8(dst, "w") as f: |
| 161 | + |
| 162 | + f.write("/* THIS FILE IS GENERATED DO NOT EDIT */\n") |
| 163 | + f.write("#ifndef _EDITOR_LICENSE_H\n") |
| 164 | + f.write("#define _EDITOR_LICENSE_H\n") |
| 165 | + f.write("const char *const GODOT_LICENSE_TEXT =") |
| 166 | + |
| 167 | + with open_utf8(src_license, "r") as license_file: |
| 168 | + for line in license_file: |
| 169 | + escaped_string = escape_string(line.strip()) |
| 170 | + f.write("\n\t\t\"" + escaped_string + "\\n\"") |
| 171 | + f.write(";\n\n") |
| 172 | + |
| 173 | + f.write("struct ComponentCopyrightPart {\n" |
| 174 | + "\tconst char *license;\n" |
| 175 | + "\tconst char *const *files;\n" |
| 176 | + "\tconst char *const *copyright_statements;\n" |
| 177 | + "\tint file_count;\n" |
| 178 | + "\tint copyright_count;\n" |
| 179 | + "};\n\n") |
| 180 | + |
| 181 | + f.write("struct ComponentCopyright {\n" |
| 182 | + "\tconst char *name;\n" |
| 183 | + "\tconst ComponentCopyrightPart *parts;\n" |
| 184 | + "\tint part_count;\n" |
| 185 | + "};\n\n") |
| 186 | + |
| 187 | + f.write("const char *const COPYRIGHT_INFO_DATA[] = {\n") |
| 188 | + for line in data_list: |
| 189 | + f.write("\t\"" + escape_string(line) + "\",\n") |
| 190 | + f.write("};\n\n") |
| 191 | + |
| 192 | + f.write("const ComponentCopyrightPart COPYRIGHT_PROJECT_PARTS[] = {\n") |
| 193 | + part_index = 0 |
| 194 | + part_indexes = {} |
| 195 | + for project_name, project in iteritems(projects): |
| 196 | + part_indexes[project_name] = part_index |
| 197 | + for part in project: |
| 198 | + f.write("\t{ \"" + escape_string(part["License"][0]) + "\", " |
| 199 | + + "©RIGHT_INFO_DATA[" + str(part["file_index"]) + "], " |
| 200 | + + "©RIGHT_INFO_DATA[" + str(part["copyright_index"]) + "], " |
| 201 | + + str(len(part["Files"])) + ", " |
| 202 | + + str(len(part["Copyright"])) + " },\n") |
| 203 | + part_index += 1 |
| 204 | + f.write("};\n\n") |
| 205 | + |
| 206 | + f.write("const int COPYRIGHT_INFO_COUNT = " + str(len(projects)) + ";\n") |
| 207 | + |
| 208 | + f.write("const ComponentCopyright COPYRIGHT_INFO[] = {\n") |
| 209 | + for project_name, project in iteritems(projects): |
| 210 | + f.write("\t{ \"" + escape_string(project_name) + "\", " |
| 211 | + + "©RIGHT_PROJECT_PARTS[" + str(part_indexes[project_name]) + "], " |
| 212 | + + str(len(project)) + " },\n") |
| 213 | + f.write("};\n\n") |
| 214 | + |
| 215 | + f.write("const int LICENSE_COUNT = " + str(len(license_list)) + ";\n") |
| 216 | + |
| 217 | + f.write("const char *const LICENSE_NAMES[] = {\n") |
| 218 | + for l in license_list: |
| 219 | + f.write("\t\"" + escape_string(l[0]) + "\",\n") |
| 220 | + f.write("};\n\n") |
| 221 | + |
| 222 | + f.write("const char *const LICENSE_BODIES[] = {\n\n") |
| 223 | + for l in license_list: |
| 224 | + for line in l[1:]: |
| 225 | + if line == ".": |
| 226 | + f.write("\t\"\\n\"\n") |
| 227 | + else: |
| 228 | + f.write("\t\"" + escape_string(line) + "\\n\"\n") |
| 229 | + f.write("\t\"\",\n\n") |
| 230 | + f.write("};\n\n") |
| 231 | + |
| 232 | + f.write("#endif\n") |
| 233 | + |
| 234 | + |
| 235 | +if __name__ == '__main__': |
| 236 | + subprocess_main(globals()) |
0 commit comments