core_builders.py 9.1 KB

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