editor_builders.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. import os
  5. import os.path
  6. import shutil
  7. import subprocess
  8. import tempfile
  9. import uuid
  10. import zlib
  11. from platform_methods import subprocess_main
  12. def make_doc_header(target, source, env):
  13. dst = target[0]
  14. g = open(dst, "w", encoding="utf-8")
  15. buf = ""
  16. docbegin = ""
  17. docend = ""
  18. for src in source:
  19. if not src.endswith(".xml"):
  20. continue
  21. with open(src, "r", encoding="utf-8") as f:
  22. content = f.read()
  23. buf += content
  24. buf = (docbegin + buf + docend).encode("utf-8")
  25. decomp_size = len(buf)
  26. # Use maximum zlib compression level to further reduce file size
  27. # (at the cost of initial build times).
  28. buf = zlib.compress(buf, zlib.Z_BEST_COMPRESSION)
  29. g.write("/* THIS FILE IS GENERATED DO NOT EDIT */\n")
  30. g.write("#ifndef _DOC_DATA_RAW_H\n")
  31. g.write("#define _DOC_DATA_RAW_H\n")
  32. g.write('static const char *_doc_data_hash = "' + str(hash(buf)) + '";\n')
  33. g.write("static const int _doc_data_compressed_size = " + str(len(buf)) + ";\n")
  34. g.write("static const int _doc_data_uncompressed_size = " + str(decomp_size) + ";\n")
  35. g.write("static const unsigned char _doc_data_compressed[] = {\n")
  36. for i in range(len(buf)):
  37. g.write("\t" + str(buf[i]) + ",\n")
  38. g.write("};\n")
  39. g.write("#endif")
  40. g.close()
  41. def make_fonts_header(target, source, env):
  42. dst = target[0]
  43. g = open(dst, "w", encoding="utf-8")
  44. g.write("/* THIS FILE IS GENERATED DO NOT EDIT */\n")
  45. g.write("#ifndef _EDITOR_FONTS_H\n")
  46. g.write("#define _EDITOR_FONTS_H\n")
  47. # Saving uncompressed, since FreeType will reference from memory pointer.
  48. for i in range(len(source)):
  49. with open(source[i], "rb") as f:
  50. buf = f.read()
  51. name = os.path.splitext(os.path.basename(source[i]))[0]
  52. g.write("static const int _font_" + name + "_size = " + str(len(buf)) + ";\n")
  53. g.write("static const unsigned char _font_" + name + "[] = {\n")
  54. for j in range(len(buf)):
  55. g.write("\t" + str(buf[j]) + ",\n")
  56. g.write("};\n")
  57. g.write("#endif")
  58. g.close()
  59. def make_translations_header(target, source, env, category):
  60. dst = target[0]
  61. g = open(dst, "w", encoding="utf-8")
  62. g.write("/* THIS FILE IS GENERATED DO NOT EDIT */\n")
  63. g.write("#ifndef _{}_TRANSLATIONS_H\n".format(category.upper()))
  64. g.write("#define _{}_TRANSLATIONS_H\n".format(category.upper()))
  65. sorted_paths = sorted(source, key=lambda path: os.path.splitext(os.path.basename(path))[0])
  66. msgfmt_available = shutil.which("msgfmt") is not None
  67. if not msgfmt_available:
  68. print("WARNING: msgfmt is not found, using .po files instead of .mo")
  69. xl_names = []
  70. for i in range(len(sorted_paths)):
  71. if msgfmt_available:
  72. mo_path = os.path.join(tempfile.gettempdir(), uuid.uuid4().hex + ".mo")
  73. cmd = "msgfmt " + sorted_paths[i] + " --no-hash -o " + mo_path
  74. try:
  75. subprocess.Popen(cmd, shell=True, stderr=subprocess.PIPE).communicate()
  76. with open(mo_path, "rb") as f:
  77. buf = f.read()
  78. except OSError as e:
  79. print(
  80. "WARNING: msgfmt execution failed, using .po file instead of .mo: path=%r; [%s] %s"
  81. % (sorted_paths[i], e.__class__.__name__, e)
  82. )
  83. with open(sorted_paths[i], "rb") as f:
  84. buf = f.read()
  85. finally:
  86. try:
  87. os.remove(mo_path)
  88. except OSError as e:
  89. # Do not fail the entire build if it cannot delete a temporary file
  90. print(
  91. "WARNING: Could not delete temporary .mo file: path=%r; [%s] %s"
  92. % (mo_path, e.__class__.__name__, e)
  93. )
  94. else:
  95. with open(sorted_paths[i], "rb") as f:
  96. buf = f.read()
  97. decomp_size = len(buf)
  98. # Use maximum zlib compression level to further reduce file size
  99. # (at the cost of initial build times).
  100. buf = zlib.compress(buf, zlib.Z_BEST_COMPRESSION)
  101. name = os.path.splitext(os.path.basename(sorted_paths[i]))[0]
  102. g.write("static const unsigned char _{}_translation_{}_compressed[] = {{\n".format(category, name))
  103. for j in range(len(buf)):
  104. g.write("\t" + str(buf[j]) + ",\n")
  105. g.write("};\n")
  106. xl_names.append([name, len(buf), str(decomp_size)])
  107. g.write("struct {}TranslationList {{\n".format(category.capitalize()))
  108. g.write("\tconst char* lang;\n")
  109. g.write("\tint comp_size;\n")
  110. g.write("\tint uncomp_size;\n")
  111. g.write("\tconst unsigned char* data;\n")
  112. g.write("};\n\n")
  113. g.write("static {}TranslationList _{}_translations[] = {{\n".format(category.capitalize(), category))
  114. for x in xl_names:
  115. g.write(
  116. '\t{{ "{}", {}, {}, _{}_translation_{}_compressed }},\n'.format(x[0], str(x[1]), str(x[2]), category, x[0])
  117. )
  118. g.write("\t{nullptr, 0, 0, nullptr}\n")
  119. g.write("};\n")
  120. g.write("#endif")
  121. g.close()
  122. def make_editor_translations_header(target, source, env):
  123. make_translations_header(target, source, env, "editor")
  124. def make_property_translations_header(target, source, env):
  125. make_translations_header(target, source, env, "property")
  126. def make_doc_translations_header(target, source, env):
  127. make_translations_header(target, source, env, "doc")
  128. if __name__ == "__main__":
  129. subprocess_main(globals())