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