main_builders.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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 byte_to_str
  6. from collections import OrderedDict
  7. def make_splash(target, source, env):
  8. src = source[0]
  9. dst = target[0]
  10. with open(src, "rb") as f:
  11. buf = f.read()
  12. with open(dst, "w") as g:
  13. g.write("/* THIS FILE IS GENERATED DO NOT EDIT */\n")
  14. g.write("#ifndef BOOT_SPLASH_H\n")
  15. g.write("#define BOOT_SPLASH_H\n")
  16. g.write('static const Color boot_splash_bg_color = Color::html("#232323");\n')
  17. g.write("static const unsigned char boot_splash_png[] = {\n")
  18. for i in range(len(buf)):
  19. g.write(byte_to_str(buf[i]) + ",\n")
  20. g.write("};\n")
  21. g.write("#endif")
  22. def make_splash_editor(target, source, env):
  23. src = source[0]
  24. dst = target[0]
  25. with open(src, "rb") as f:
  26. buf = f.read()
  27. with open(dst, "w") as g:
  28. g.write("/* THIS FILE IS GENERATED DO NOT EDIT */\n")
  29. g.write("#ifndef BOOT_SPLASH_EDITOR_H\n")
  30. g.write("#define BOOT_SPLASH_EDITOR_H\n")
  31. g.write('static const Color boot_splash_editor_bg_color = Color::html("#232323");\n')
  32. g.write("static const unsigned char boot_splash_editor_png[] = {\n")
  33. for i in range(len(buf)):
  34. g.write(byte_to_str(buf[i]) + ",\n")
  35. g.write("};\n")
  36. g.write("#endif")
  37. def make_app_icon(target, source, env):
  38. src = source[0]
  39. dst = target[0]
  40. with open(src, "rb") as f:
  41. buf = f.read()
  42. with open(dst, "w") as g:
  43. g.write("/* THIS FILE IS GENERATED DO NOT EDIT */\n")
  44. g.write("#ifndef APP_ICON_H\n")
  45. g.write("#define APP_ICON_H\n")
  46. g.write("static const unsigned char app_icon_png[] = {\n")
  47. for i in range(len(buf)):
  48. g.write(byte_to_str(buf[i]) + ",\n")
  49. g.write("};\n")
  50. g.write("#endif")
  51. def make_default_controller_mappings(target, source, env):
  52. dst = target[0]
  53. g = open(dst, "w")
  54. g.write("/* THIS FILE IS GENERATED DO NOT EDIT */\n")
  55. g.write("#include \"core/typedefs.h\"\n")
  56. g.write("#include \"main/default_controller_mappings.h\"\n")
  57. # ensure mappings have a consistent order
  58. platform_mappings = OrderedDict()
  59. for src_path in source:
  60. with open(src_path, "r") as f:
  61. # read mapping file and skip header
  62. mapping_file_lines = f.readlines()[2:]
  63. current_platform = None
  64. for line in mapping_file_lines:
  65. if not line:
  66. continue
  67. line = line.strip()
  68. if len(line) == 0:
  69. continue
  70. if line[0] == "#":
  71. current_platform = line[1:].strip()
  72. if current_platform not in platform_mappings:
  73. platform_mappings[current_platform] = {}
  74. elif current_platform:
  75. line_parts = line.split(",")
  76. guid = line_parts[0]
  77. if guid in platform_mappings[current_platform]:
  78. g.write("// WARNING - DATABASE {} OVERWROTE PRIOR MAPPING: {} {}\n".format(src_path, current_platform, platform_mappings[current_platform][guid]))
  79. valid_mapping = True
  80. for input_map in line_parts[2:]:
  81. if "+" in input_map or "-" in input_map or "~" in input_map:
  82. g.write("// WARNING - DISCARDED UNSUPPORTED MAPPING TYPE FROM DATABASE {}: {} {}\n".format(src_path, current_platform, line))
  83. valid_mapping = False
  84. break
  85. if valid_mapping:
  86. platform_mappings[current_platform][guid] = line
  87. platform_variables = {
  88. "Linux": "#if X11_ENABLED",
  89. "Windows": "#ifdef WINDOWS_ENABLED",
  90. "Mac OS X": "#ifdef OSX_ENABLED",
  91. "Android": "#if defined(__ANDROID__)",
  92. "iOS": "#ifdef IPHONE_ENABLED",
  93. "Javascript": "#ifdef JAVASCRIPT_ENABLED",
  94. "UWP": "#ifdef UWP_ENABLED",
  95. }
  96. g.write("const char* DefaultControllerMappings::mappings[] = {\n")
  97. for platform, mappings in platform_mappings.items():
  98. variable = platform_variables[platform]
  99. g.write("{}\n".format(variable))
  100. for mapping in mappings.values():
  101. g.write("\t\"{}\",\n".format(mapping))
  102. g.write("#endif\n")
  103. g.write("\tNULL\n};\n")
  104. g.close()
  105. if __name__ == '__main__':
  106. subprocess_main(globals())