core_builders.py 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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", "Gold donors", "Silver donors", "Bronze donors"]
  69. sections_id = [
  70. "DONORS_SPONSOR_PLAT",
  71. "DONORS_SPONSOR_GOLD",
  72. "DONORS_SPONSOR_MINI",
  73. "DONORS_GOLD",
  74. "DONORS_SILVER",
  75. "DONORS_BRONZE",
  76. ]
  77. src = source[0]
  78. dst = target[0]
  79. f = open_utf8(src, "r")
  80. g = open_utf8(dst, "w")
  81. g.write("/* THIS FILE IS GENERATED DO NOT EDIT */\n")
  82. g.write("#ifndef _EDITOR_DONORS_H\n")
  83. g.write("#define _EDITOR_DONORS_H\n")
  84. reading = False
  85. def close_section():
  86. g.write("\t0\n")
  87. g.write("};\n")
  88. for line in f:
  89. if reading >= 0:
  90. if line.startswith(" "):
  91. g.write('\t"' + escape_string(line.strip()) + '",\n')
  92. continue
  93. if line.startswith("## "):
  94. if reading:
  95. close_section()
  96. reading = False
  97. for section, section_id in zip(sections, sections_id):
  98. if line.strip().endswith(section):
  99. current_section = escape_string(section_id)
  100. reading = True
  101. g.write("const char *const " + current_section + "[] = {\n")
  102. break
  103. if reading:
  104. close_section()
  105. g.write("#endif\n")
  106. g.close()
  107. f.close()
  108. def make_license_header(target, source, env):
  109. src_copyright = source[0]
  110. src_license = source[1]
  111. dst = target[0]
  112. class LicenseReader:
  113. def __init__(self, license_file):
  114. self._license_file = license_file
  115. self.line_num = 0
  116. self.current = self.next_line()
  117. def next_line(self):
  118. line = self._license_file.readline()
  119. self.line_num += 1
  120. while line.startswith("#"):
  121. line = self._license_file.readline()
  122. self.line_num += 1
  123. self.current = line
  124. return line
  125. def next_tag(self):
  126. if not ":" in self.current:
  127. return ("", [])
  128. tag, line = self.current.split(":", 1)
  129. lines = [line.strip()]
  130. while self.next_line() and self.current.startswith(" "):
  131. lines.append(self.current.strip())
  132. return (tag, lines)
  133. from collections import OrderedDict
  134. projects = OrderedDict()
  135. license_list = []
  136. with open_utf8(src_copyright, "r") as copyright_file:
  137. reader = LicenseReader(copyright_file)
  138. part = {}
  139. while reader.current:
  140. tag, content = reader.next_tag()
  141. if tag in ("Files", "Copyright", "License"):
  142. part[tag] = content[:]
  143. elif tag == "Comment":
  144. # attach part to named project
  145. projects[content[0]] = projects.get(content[0], []) + [part]
  146. if not tag or not reader.current:
  147. # end of a paragraph start a new part
  148. if "License" in part and not "Files" in part:
  149. # no Files tag in this one, so assume standalone license
  150. license_list.append(part["License"])
  151. part = {}
  152. reader.next_line()
  153. data_list = []
  154. for project in itervalues(projects):
  155. for part in project:
  156. part["file_index"] = len(data_list)
  157. data_list += part["Files"]
  158. part["copyright_index"] = len(data_list)
  159. data_list += part["Copyright"]
  160. with open_utf8(dst, "w") as f:
  161. f.write("/* THIS FILE IS GENERATED DO NOT EDIT */\n")
  162. f.write("#ifndef _EDITOR_LICENSE_H\n")
  163. f.write("#define _EDITOR_LICENSE_H\n")
  164. f.write("const char *const GODOT_LICENSE_TEXT =")
  165. with open_utf8(src_license, "r") as license_file:
  166. for line in license_file:
  167. escaped_string = escape_string(line.strip())
  168. f.write('\n\t\t"' + escaped_string + '\\n"')
  169. f.write(";\n\n")
  170. f.write(
  171. "struct ComponentCopyrightPart {\n"
  172. "\tconst char *license;\n"
  173. "\tconst char *const *files;\n"
  174. "\tconst char *const *copyright_statements;\n"
  175. "\tint file_count;\n"
  176. "\tint copyright_count;\n"
  177. "};\n\n"
  178. )
  179. f.write(
  180. "struct ComponentCopyright {\n"
  181. "\tconst char *name;\n"
  182. "\tconst ComponentCopyrightPart *parts;\n"
  183. "\tint part_count;\n"
  184. "};\n\n"
  185. )
  186. f.write("const char *const COPYRIGHT_INFO_DATA[] = {\n")
  187. for line in data_list:
  188. f.write('\t"' + escape_string(line) + '",\n')
  189. f.write("};\n\n")
  190. f.write("const ComponentCopyrightPart COPYRIGHT_PROJECT_PARTS[] = {\n")
  191. part_index = 0
  192. part_indexes = {}
  193. for project_name, project in iteritems(projects):
  194. part_indexes[project_name] = part_index
  195. for part in project:
  196. f.write(
  197. '\t{ "'
  198. + escape_string(part["License"][0])
  199. + '", '
  200. + "&COPYRIGHT_INFO_DATA["
  201. + str(part["file_index"])
  202. + "], "
  203. + "&COPYRIGHT_INFO_DATA["
  204. + str(part["copyright_index"])
  205. + "], "
  206. + str(len(part["Files"]))
  207. + ", "
  208. + str(len(part["Copyright"]))
  209. + " },\n"
  210. )
  211. part_index += 1
  212. f.write("};\n\n")
  213. f.write("const int COPYRIGHT_INFO_COUNT = " + str(len(projects)) + ";\n")
  214. f.write("const ComponentCopyright COPYRIGHT_INFO[] = {\n")
  215. for project_name, project in iteritems(projects):
  216. f.write(
  217. '\t{ "'
  218. + escape_string(project_name)
  219. + '", '
  220. + "&COPYRIGHT_PROJECT_PARTS["
  221. + str(part_indexes[project_name])
  222. + "], "
  223. + str(len(project))
  224. + " },\n"
  225. )
  226. f.write("};\n\n")
  227. f.write("const int LICENSE_COUNT = " + str(len(license_list)) + ";\n")
  228. f.write("const char *const LICENSE_NAMES[] = {\n")
  229. for l in license_list:
  230. f.write('\t"' + escape_string(l[0]) + '",\n')
  231. f.write("};\n\n")
  232. f.write("const char *const LICENSE_BODIES[] = {\n\n")
  233. for l in license_list:
  234. for line in l[1:]:
  235. if line == ".":
  236. f.write('\t"\\n"\n')
  237. else:
  238. f.write('\t"' + escape_string(line) + '\\n"\n')
  239. f.write('\t"",\n\n')
  240. f.write("};\n\n")
  241. f.write("#endif\n")
  242. if __name__ == "__main__":
  243. subprocess_main(globals())