platform_methods.py 8.0 KB

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