platform_methods.py 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. import os
  2. import sys
  3. import json
  4. import platform
  5. import uuid
  6. import functools
  7. import subprocess
  8. # NOTE: The multiprocessing module is not compatible with SCons due to conflict on cPickle
  9. JSON_SERIALIZABLE_TYPES = (bool, int, float, str)
  10. def run_in_subprocess(builder_function):
  11. @functools.wraps(builder_function)
  12. def wrapper(target, source, env):
  13. # Convert SCons Node instances to absolute paths
  14. target = [node.srcnode().abspath for node in target]
  15. source = [node.srcnode().abspath for node in source]
  16. # Short circuit on non-Windows platforms, no need to run in subprocess
  17. if sys.platform not in ("win32", "cygwin"):
  18. return builder_function(target, source, env)
  19. # Identify module
  20. module_name = builder_function.__module__
  21. function_name = builder_function.__name__
  22. module_path = sys.modules[module_name].__file__
  23. if module_path.endswith(".pyc") or module_path.endswith(".pyo"):
  24. module_path = module_path[:-1]
  25. # Subprocess environment
  26. subprocess_env = os.environ.copy()
  27. subprocess_env["PYTHONPATH"] = os.pathsep.join([os.getcwd()] + sys.path)
  28. # Keep only JSON serializable environment items
  29. filtered_env = dict((key, value) for key, value in env.items() if isinstance(value, JSON_SERIALIZABLE_TYPES))
  30. # Save parameters
  31. args = (target, source, filtered_env)
  32. data = dict(fn=function_name, args=args)
  33. json_path = os.path.join(os.environ["TMP"], uuid.uuid4().hex + ".json")
  34. with open(json_path, "wt", encoding="utf-8", newline="\n") as json_file:
  35. json.dump(data, json_file, indent=2)
  36. json_file_size = os.stat(json_path).st_size
  37. if env["verbose"]:
  38. print(
  39. "Executing builder function in subprocess: "
  40. "module_path=%r, parameter_file=%r, parameter_file_size=%r, target=%r, source=%r"
  41. % (module_path, json_path, json_file_size, target, source)
  42. )
  43. try:
  44. exit_code = subprocess.call([sys.executable, module_path, json_path], env=subprocess_env)
  45. finally:
  46. try:
  47. os.remove(json_path)
  48. except OSError as e:
  49. # Do not fail the entire build if it cannot delete a temporary file
  50. print(
  51. "WARNING: Could not delete temporary file: path=%r; [%s] %s" % (json_path, e.__class__.__name__, e)
  52. )
  53. # Must succeed
  54. if exit_code:
  55. raise RuntimeError(
  56. "Failed to run builder function in subprocess: module_path=%r; data=%r" % (module_path, data)
  57. )
  58. return wrapper
  59. def subprocess_main(namespace):
  60. with open(sys.argv[1]) as json_file:
  61. data = json.load(json_file)
  62. fn = namespace[data["fn"]]
  63. fn(*data["args"])
  64. # CPU architecture options.
  65. architectures = ["x86_32", "x86_64", "arm32", "arm64", "rv64", "ppc32", "ppc64", "wasm32"]
  66. architecture_aliases = {
  67. "x86": "x86_32",
  68. "x64": "x86_64",
  69. "amd64": "x86_64",
  70. "armv7": "arm32",
  71. "armv8": "arm64",
  72. "arm64v8": "arm64",
  73. "aarch64": "arm64",
  74. "rv": "rv64",
  75. "riscv": "rv64",
  76. "riscv64": "rv64",
  77. "ppcle": "ppc32",
  78. "ppc": "ppc32",
  79. "ppc64le": "ppc64",
  80. }
  81. def detect_arch():
  82. host_machine = platform.machine().lower()
  83. if host_machine in architectures:
  84. return host_machine
  85. elif host_machine in architecture_aliases.keys():
  86. return architecture_aliases[host_machine]
  87. elif "86" in host_machine:
  88. # Catches x86, i386, i486, i586, i686, etc.
  89. return "x86_32"
  90. else:
  91. print("Unsupported CPU architecture: " + host_machine)
  92. print("Falling back to x86_64.")
  93. return "x86_64"
  94. def generate_export_icons(platform_path, platform_name):
  95. """
  96. Generate headers for logo and run icon for the export plugin.
  97. """
  98. export_path = platform_path + "/export"
  99. svg_names = []
  100. if os.path.isfile(export_path + "/logo.svg"):
  101. svg_names.append("logo")
  102. if os.path.isfile(export_path + "/run_icon.svg"):
  103. svg_names.append("run_icon")
  104. for name in svg_names:
  105. with open(export_path + "/" + name + ".svg", "rb") as svgf:
  106. b = svgf.read(1)
  107. svg_str = " /* AUTOGENERATED FILE, DO NOT EDIT */ \n"
  108. svg_str += " static const char *_" + platform_name + "_" + name + '_svg = "'
  109. while len(b) == 1:
  110. svg_str += "\\" + hex(ord(b))[1:]
  111. b = svgf.read(1)
  112. svg_str += '";\n'
  113. # NOTE: It is safe to generate this file here, since this is still executed serially.
  114. wf = export_path + "/" + name + "_svg.gen.h"
  115. with open(wf, "w", encoding="utf-8", newline="\n") as svgw:
  116. svgw.write(svg_str)
  117. def get_build_version(short):
  118. import version
  119. name = "custom_build"
  120. if os.getenv("BUILD_NAME") != None:
  121. name = os.getenv("BUILD_NAME")
  122. v = "%d.%d" % (version.major, version.minor)
  123. if version.patch > 0:
  124. v += ".%d" % version.patch
  125. status = version.status
  126. if not short:
  127. if os.getenv("GODOT_VERSION_STATUS") != None:
  128. status = str(os.getenv("GODOT_VERSION_STATUS"))
  129. v += ".%s.%s" % (status, name)
  130. return v
  131. def lipo(prefix, suffix):
  132. from pathlib import Path
  133. target_bin = ""
  134. lipo_command = ["lipo", "-create"]
  135. arch_found = 0
  136. for arch in architectures:
  137. bin_name = prefix + "." + arch + suffix
  138. if Path(bin_name).is_file():
  139. target_bin = bin_name
  140. lipo_command += [bin_name]
  141. arch_found += 1
  142. if arch_found > 1:
  143. target_bin = prefix + ".fat" + suffix
  144. lipo_command += ["-output", target_bin]
  145. subprocess.run(lipo_command)
  146. return target_bin
  147. def get_mvk_sdk_path(osname):
  148. def int_or_zero(i):
  149. try:
  150. return int(i)
  151. except:
  152. return 0
  153. def ver_parse(a):
  154. return [int_or_zero(i) for i in a.split(".")]
  155. dirname = os.path.expanduser("~/VulkanSDK")
  156. if not os.path.exists(dirname):
  157. return ""
  158. ver_min = ver_parse("1.3.231.0")
  159. ver_num = ver_parse("0.0.0.0")
  160. files = os.listdir(dirname)
  161. lib_name_out = dirname
  162. for file in files:
  163. if os.path.isdir(os.path.join(dirname, file)):
  164. ver_comp = ver_parse(file)
  165. if ver_comp > ver_num and ver_comp >= ver_min:
  166. # Try new SDK location.
  167. lib_name = os.path.join(os.path.join(dirname, file), "macOS/lib/MoltenVK.xcframework/" + osname + "/")
  168. if os.path.isfile(os.path.join(lib_name, "libMoltenVK.a")):
  169. ver_num = ver_comp
  170. lib_name_out = os.path.join(os.path.join(dirname, file), "macOS/lib/MoltenVK.xcframework")
  171. else:
  172. # Try old SDK location.
  173. lib_name = os.path.join(
  174. os.path.join(dirname, file), "MoltenVK/MoltenVK.xcframework/" + osname + "/"
  175. )
  176. if os.path.isfile(os.path.join(lib_name, "libMoltenVK.a")):
  177. ver_num = ver_comp
  178. lib_name_out = os.path.join(os.path.join(dirname, file), "MoltenVK/MoltenVK.xcframework")
  179. return lib_name_out
  180. def detect_mvk(env, osname):
  181. mvk_list = [
  182. get_mvk_sdk_path(osname),
  183. "/opt/homebrew/Frameworks/MoltenVK.xcframework",
  184. "/usr/local/homebrew/Frameworks/MoltenVK.xcframework",
  185. "/opt/local/Frameworks/MoltenVK.xcframework",
  186. ]
  187. if env["vulkan_sdk_path"] != "":
  188. mvk_list.insert(0, os.path.expanduser(env["vulkan_sdk_path"]))
  189. mvk_list.insert(
  190. 0,
  191. os.path.join(os.path.expanduser(env["vulkan_sdk_path"]), "macOS/lib/MoltenVK.xcframework"),
  192. )
  193. mvk_list.insert(
  194. 0,
  195. os.path.join(os.path.expanduser(env["vulkan_sdk_path"]), "MoltenVK/MoltenVK.xcframework"),
  196. )
  197. for mvk_path in mvk_list:
  198. if mvk_path and os.path.isfile(os.path.join(mvk_path, osname + "/libMoltenVK.a")):
  199. mvk_found = True
  200. print("MoltenVK found at: " + mvk_path)
  201. return mvk_path
  202. return ""