editor_builders.py 5.2 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. from compat import encode_utf8, byte_to_str, open_utf8
  12. def make_doc_header(target, source, env):
  13. dst = target[0]
  14. g = open_utf8(dst, "w")
  15. buf = ""
  16. docbegin = ""
  17. docend = ""
  18. for src in source:
  19. if not src.endswith(".xml"):
  20. continue
  21. with open_utf8(src, "r") as f:
  22. content = f.read()
  23. buf += content
  24. buf = encode_utf8(docbegin + buf + docend)
  25. decomp_size = len(buf)
  26. import zlib
  27. buf = zlib.compress(buf)
  28. g.write("/* THIS FILE IS GENERATED DO NOT EDIT */\n")
  29. g.write("#ifndef _DOC_DATA_RAW_H\n")
  30. g.write("#define _DOC_DATA_RAW_H\n")
  31. g.write("static const int _doc_data_compressed_size = " + str(len(buf)) + ";\n")
  32. g.write("static const int _doc_data_uncompressed_size = " + str(decomp_size) + ";\n")
  33. g.write("static const unsigned char _doc_data_compressed[] = {\n")
  34. for i in range(len(buf)):
  35. g.write("\t" + byte_to_str(buf[i]) + ",\n")
  36. g.write("};\n")
  37. g.write("#endif")
  38. g.close()
  39. def make_fonts_header(target, source, env):
  40. dst = target[0]
  41. g = open_utf8(dst, "w")
  42. g.write("/* THIS FILE IS GENERATED DO NOT EDIT */\n")
  43. g.write("#ifndef _EDITOR_FONTS_H\n")
  44. g.write("#define _EDITOR_FONTS_H\n")
  45. # saving uncompressed, since freetype will reference from memory pointer
  46. xl_names = []
  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" + byte_to_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_utf8(dst, "w")
  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. buf = zlib.compress(buf)
  100. name = os.path.splitext(os.path.basename(sorted_paths[i]))[0]
  101. g.write("static const unsigned char _{}_translation_{}_compressed[] = {{\n".format(category, name))
  102. for j in range(len(buf)):
  103. g.write("\t" + byte_to_str(buf[j]) + ",\n")
  104. g.write("};\n")
  105. xl_names.append([name, len(buf), str(decomp_size)])
  106. g.write("struct {}TranslationList {{\n".format(category.capitalize()))
  107. g.write("\tconst char* lang;\n")
  108. g.write("\tint comp_size;\n")
  109. g.write("\tint uncomp_size;\n")
  110. g.write("\tconst unsigned char* data;\n")
  111. g.write("};\n\n")
  112. g.write("static {}TranslationList _{}_translations[] = {{\n".format(category.capitalize(), category))
  113. for x in xl_names:
  114. g.write(
  115. '\t{{ "{}", {}, {}, _{}_translation_{}_compressed }},\n'.format(x[0], str(x[1]), str(x[2]), category, x[0])
  116. )
  117. g.write("\t{NULL, 0, 0, NULL}\n")
  118. g.write("};\n")
  119. g.write("#endif")
  120. g.close()
  121. def make_editor_translations_header(target, source, env):
  122. make_translations_header(target, source, env, "editor")
  123. def make_doc_translations_header(target, source, env):
  124. make_translations_header(target, source, env, "doc")
  125. if __name__ == "__main__":
  126. subprocess_main(globals())