platform_methods.py 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. import os
  2. import platform
  3. import shutil
  4. import subprocess
  5. import sys
  6. import methods
  7. # NOTE: The multiprocessing module is not compatible with SCons due to conflict on cPickle
  8. compatibility_platform_aliases = {
  9. "osx": "macos",
  10. "iphone": "ios",
  11. "x11": "linuxbsd",
  12. "javascript": "web",
  13. }
  14. # CPU architecture options.
  15. architectures = ["x86_32", "x86_64", "arm32", "arm64", "rv64", "ppc64", "wasm32", "loongarch64"]
  16. architecture_aliases = {
  17. "x86": "x86_32",
  18. "x64": "x86_64",
  19. "amd64": "x86_64",
  20. "armv7": "arm32",
  21. "armv8": "arm64",
  22. "arm64v8": "arm64",
  23. "aarch64": "arm64",
  24. "rv": "rv64",
  25. "riscv": "rv64",
  26. "riscv64": "rv64",
  27. "ppc64le": "ppc64",
  28. "loong64": "loongarch64",
  29. }
  30. def detect_arch():
  31. host_machine = platform.machine().lower()
  32. if host_machine in architectures:
  33. return host_machine
  34. elif host_machine in architecture_aliases.keys():
  35. return architecture_aliases[host_machine]
  36. elif "86" in host_machine:
  37. # Catches x86, i386, i486, i586, i686, etc.
  38. return "x86_32"
  39. else:
  40. methods.print_warning(f'Unsupported CPU architecture: "{host_machine}". Falling back to x86_64.')
  41. return "x86_64"
  42. def validate_arch(arch, platform_name, supported_arches):
  43. if arch not in supported_arches:
  44. methods.print_error(
  45. 'Unsupported CPU architecture "%s" for %s. Supported architectures are: %s.'
  46. % (arch, platform_name, ", ".join(supported_arches))
  47. )
  48. sys.exit(255)
  49. def get_build_version(short):
  50. import version
  51. name = "custom_build"
  52. if os.getenv("BUILD_NAME") is not None:
  53. name = os.getenv("BUILD_NAME")
  54. v = "%d.%d" % (version.major, version.minor)
  55. if version.patch > 0:
  56. v += ".%d" % version.patch
  57. status = version.status
  58. if not short:
  59. if os.getenv("GODOT_VERSION_STATUS") is not None:
  60. status = str(os.getenv("GODOT_VERSION_STATUS"))
  61. v += ".%s.%s" % (status, name)
  62. return v
  63. def lipo(prefix, suffix):
  64. from pathlib import Path
  65. target_bin = ""
  66. lipo_command = ["lipo", "-create"]
  67. arch_found = 0
  68. for arch in architectures:
  69. bin_name = prefix + "." + arch + suffix
  70. if Path(bin_name).is_file():
  71. target_bin = bin_name
  72. lipo_command += [bin_name]
  73. arch_found += 1
  74. if arch_found > 1:
  75. target_bin = prefix + ".fat" + suffix
  76. lipo_command += ["-output", target_bin]
  77. subprocess.run(lipo_command)
  78. return target_bin
  79. def get_mvk_sdk_path(osname):
  80. def int_or_zero(i):
  81. try:
  82. return int(i)
  83. except (TypeError, ValueError):
  84. return 0
  85. def ver_parse(a):
  86. return [int_or_zero(i) for i in a.split(".")]
  87. dirname = os.path.expanduser("~/VulkanSDK")
  88. if not os.path.exists(dirname):
  89. return ""
  90. ver_min = ver_parse("1.3.231.0")
  91. ver_num = ver_parse("0.0.0.0")
  92. files = os.listdir(dirname)
  93. lib_name_out = dirname
  94. for file in files:
  95. if os.path.isdir(os.path.join(dirname, file)):
  96. ver_comp = ver_parse(file)
  97. if ver_comp > ver_num and ver_comp >= ver_min:
  98. # Try new SDK location.
  99. lib_name = os.path.join(os.path.join(dirname, file), "macOS/lib/MoltenVK.xcframework/" + osname + "/")
  100. if os.path.isfile(os.path.join(lib_name, "libMoltenVK.a")):
  101. ver_num = ver_comp
  102. lib_name_out = os.path.join(os.path.join(dirname, file), "macOS/lib/MoltenVK.xcframework")
  103. else:
  104. # Try old SDK location.
  105. lib_name = os.path.join(
  106. os.path.join(dirname, file), "MoltenVK/MoltenVK.xcframework/" + osname + "/"
  107. )
  108. if os.path.isfile(os.path.join(lib_name, "libMoltenVK.a")):
  109. ver_num = ver_comp
  110. lib_name_out = os.path.join(os.path.join(dirname, file), "MoltenVK/MoltenVK.xcframework")
  111. return lib_name_out
  112. def detect_mvk(env, osname):
  113. mvk_list = [
  114. get_mvk_sdk_path(osname),
  115. "/opt/homebrew/Frameworks/MoltenVK.xcframework",
  116. "/usr/local/homebrew/Frameworks/MoltenVK.xcframework",
  117. "/opt/local/Frameworks/MoltenVK.xcframework",
  118. ]
  119. if env["vulkan_sdk_path"] != "":
  120. mvk_list.insert(0, os.path.expanduser(env["vulkan_sdk_path"]))
  121. mvk_list.insert(
  122. 0,
  123. os.path.join(os.path.expanduser(env["vulkan_sdk_path"]), "macOS/lib/MoltenVK.xcframework"),
  124. )
  125. mvk_list.insert(
  126. 0,
  127. os.path.join(os.path.expanduser(env["vulkan_sdk_path"]), "MoltenVK/MoltenVK.xcframework"),
  128. )
  129. for mvk_path in mvk_list:
  130. if mvk_path and os.path.isfile(os.path.join(mvk_path, f"{osname}/libMoltenVK.a")):
  131. print(f"MoltenVK found at: {mvk_path}")
  132. return mvk_path
  133. return ""
  134. def combine_libs_apple_embedded(target, source, env):
  135. lib_path = target[0].srcnode().abspath
  136. if "osxcross" in env:
  137. libtool = "$APPLE_TOOLCHAIN_PATH/usr/bin/${apple_target_triple}libtool"
  138. else:
  139. libtool = "$APPLE_TOOLCHAIN_PATH/usr/bin/libtool"
  140. env.Execute(
  141. libtool + ' -static -o "' + lib_path + '" ' + " ".join([('"' + lib.srcnode().abspath + '"') for lib in source])
  142. )
  143. def generate_bundle_apple_embedded(platform, framework_dir, framework_dir_sim, use_mkv, target, source, env):
  144. bin_dir = env.Dir("#bin").abspath
  145. # Template bundle.
  146. app_prefix = "godot." + platform
  147. rel_prefix = "libgodot." + platform + "." + "template_release"
  148. dbg_prefix = "libgodot." + platform + "." + "template_debug"
  149. if env.dev_build:
  150. app_prefix += ".dev"
  151. rel_prefix += ".dev"
  152. dbg_prefix += ".dev"
  153. if env["precision"] == "double":
  154. app_prefix += ".double"
  155. rel_prefix += ".double"
  156. dbg_prefix += ".double"
  157. # Lipo template libraries.
  158. #
  159. # env.extra_suffix contains ".simulator" when building for simulator,
  160. # but it's undesired when calling lipo()
  161. extra_suffix = env.extra_suffix.replace(".simulator", "")
  162. rel_target_bin = lipo(bin_dir + "/" + rel_prefix, extra_suffix + ".a")
  163. dbg_target_bin = lipo(bin_dir + "/" + dbg_prefix, extra_suffix + ".a")
  164. rel_target_bin_sim = lipo(bin_dir + "/" + rel_prefix, ".simulator" + extra_suffix + ".a")
  165. dbg_target_bin_sim = lipo(bin_dir + "/" + dbg_prefix, ".simulator" + extra_suffix + ".a")
  166. # Assemble Xcode project bundle.
  167. app_dir = env.Dir("#bin/" + platform + "_xcode").abspath
  168. templ = env.Dir("#misc/dist/" + platform + "_xcode").abspath
  169. if os.path.exists(app_dir):
  170. shutil.rmtree(app_dir)
  171. shutil.copytree(templ, app_dir)
  172. if rel_target_bin != "":
  173. print(f' Copying "{platform}" release framework')
  174. shutil.copy(
  175. rel_target_bin, app_dir + "/libgodot." + platform + ".release.xcframework/" + framework_dir + "/libgodot.a"
  176. )
  177. if dbg_target_bin != "":
  178. print(f' Copying "{platform}" debug framework')
  179. shutil.copy(
  180. dbg_target_bin, app_dir + "/libgodot." + platform + ".debug.xcframework/" + framework_dir + "/libgodot.a"
  181. )
  182. if rel_target_bin_sim != "":
  183. print(f' Copying "{platform}" (simulator) release framework')
  184. shutil.copy(
  185. rel_target_bin_sim,
  186. app_dir + "/libgodot." + platform + ".release.xcframework/" + framework_dir_sim + "/libgodot.a",
  187. )
  188. if dbg_target_bin_sim != "":
  189. print(f' Copying "{platform}" (simulator) debug framework')
  190. shutil.copy(
  191. dbg_target_bin_sim,
  192. app_dir + "/libgodot." + platform + ".debug.xcframework/" + framework_dir_sim + "/libgodot.a",
  193. )
  194. if use_mkv:
  195. mvk_path = detect_mvk(env, "ios-arm64")
  196. if mvk_path != "":
  197. shutil.copytree(mvk_path, app_dir + "/MoltenVK.xcframework")
  198. # ZIP Xcode project bundle.
  199. zip_dir = env.Dir("#bin/" + (app_prefix + extra_suffix).replace(".", "_")).abspath
  200. shutil.make_archive(zip_dir, "zip", root_dir=app_dir)
  201. shutil.rmtree(app_dir)