platform_methods.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. import os
  2. import platform
  3. import subprocess
  4. import sys
  5. import methods
  6. # NOTE: The multiprocessing module is not compatible with SCons due to conflict on cPickle
  7. compatibility_platform_aliases = {
  8. "osx": "macos",
  9. "iphone": "ios",
  10. "x11": "linuxbsd",
  11. "javascript": "web",
  12. }
  13. # CPU architecture options.
  14. architectures = ["x86_32", "x86_64", "arm32", "arm64", "rv64", "ppc32", "ppc64", "wasm32", "loongarch64"]
  15. architecture_aliases = {
  16. "x86": "x86_32",
  17. "x64": "x86_64",
  18. "amd64": "x86_64",
  19. "armv7": "arm32",
  20. "armv8": "arm64",
  21. "arm64v8": "arm64",
  22. "aarch64": "arm64",
  23. "rv": "rv64",
  24. "riscv": "rv64",
  25. "riscv64": "rv64",
  26. "ppcle": "ppc32",
  27. "ppc": "ppc32",
  28. "ppc64le": "ppc64",
  29. "loong64": "loongarch64",
  30. }
  31. def detect_arch():
  32. host_machine = platform.machine().lower()
  33. if host_machine in architectures:
  34. return host_machine
  35. elif host_machine in architecture_aliases.keys():
  36. return architecture_aliases[host_machine]
  37. elif "86" in host_machine:
  38. # Catches x86, i386, i486, i586, i686, etc.
  39. return "x86_32"
  40. else:
  41. methods.print_warning(f'Unsupported CPU architecture: "{host_machine}". Falling back to x86_64.')
  42. return "x86_64"
  43. def validate_arch(arch, platform_name, supported_arches):
  44. if arch not in supported_arches:
  45. methods.print_error(
  46. 'Unsupported CPU architecture "%s" for %s. Supported architectures are: %s.'
  47. % (arch, platform_name, ", ".join(supported_arches))
  48. )
  49. sys.exit(255)
  50. def get_build_version(short):
  51. import version
  52. name = "custom_build"
  53. if os.getenv("BUILD_NAME") is not None:
  54. name = os.getenv("BUILD_NAME")
  55. v = "%d.%d" % (version.major, version.minor)
  56. if version.patch > 0:
  57. v += ".%d" % version.patch
  58. status = version.status
  59. if not short:
  60. if os.getenv("GODOT_VERSION_STATUS") is not None:
  61. status = str(os.getenv("GODOT_VERSION_STATUS"))
  62. v += ".%s.%s" % (status, name)
  63. return v
  64. def lipo(prefix, suffix):
  65. from pathlib import Path
  66. target_bin = ""
  67. lipo_command = ["lipo", "-create"]
  68. arch_found = 0
  69. for arch in architectures:
  70. bin_name = prefix + "." + arch + suffix
  71. if Path(bin_name).is_file():
  72. target_bin = bin_name
  73. lipo_command += [bin_name]
  74. arch_found += 1
  75. if arch_found > 1:
  76. target_bin = prefix + ".fat" + suffix
  77. lipo_command += ["-output", target_bin]
  78. subprocess.run(lipo_command)
  79. return target_bin
  80. def get_mvk_sdk_path(osname):
  81. def int_or_zero(i):
  82. try:
  83. return int(i)
  84. except (TypeError, ValueError):
  85. return 0
  86. def ver_parse(a):
  87. return [int_or_zero(i) for i in a.split(".")]
  88. dirname = os.path.expanduser("~/VulkanSDK")
  89. if not os.path.exists(dirname):
  90. return ""
  91. ver_min = ver_parse("1.3.231.0")
  92. ver_num = ver_parse("0.0.0.0")
  93. files = os.listdir(dirname)
  94. lib_name_out = dirname
  95. for file in files:
  96. if os.path.isdir(os.path.join(dirname, file)):
  97. ver_comp = ver_parse(file)
  98. if ver_comp > ver_num and ver_comp >= ver_min:
  99. # Try new SDK location.
  100. lib_name = os.path.join(os.path.join(dirname, file), "macOS/lib/MoltenVK.xcframework/" + osname + "/")
  101. if os.path.isfile(os.path.join(lib_name, "libMoltenVK.a")):
  102. ver_num = ver_comp
  103. lib_name_out = os.path.join(os.path.join(dirname, file), "macOS/lib/MoltenVK.xcframework")
  104. else:
  105. # Try old SDK location.
  106. lib_name = os.path.join(
  107. os.path.join(dirname, file), "MoltenVK/MoltenVK.xcframework/" + osname + "/"
  108. )
  109. if os.path.isfile(os.path.join(lib_name, "libMoltenVK.a")):
  110. ver_num = ver_comp
  111. lib_name_out = os.path.join(os.path.join(dirname, file), "MoltenVK/MoltenVK.xcframework")
  112. return lib_name_out
  113. def detect_mvk(env, osname):
  114. mvk_list = [
  115. get_mvk_sdk_path(osname),
  116. "/opt/homebrew/Frameworks/MoltenVK.xcframework",
  117. "/usr/local/homebrew/Frameworks/MoltenVK.xcframework",
  118. "/opt/local/Frameworks/MoltenVK.xcframework",
  119. ]
  120. if env["vulkan_sdk_path"] != "":
  121. mvk_list.insert(0, os.path.expanduser(env["vulkan_sdk_path"]))
  122. mvk_list.insert(
  123. 0,
  124. os.path.join(os.path.expanduser(env["vulkan_sdk_path"]), "macOS/lib/MoltenVK.xcframework"),
  125. )
  126. mvk_list.insert(
  127. 0,
  128. os.path.join(os.path.expanduser(env["vulkan_sdk_path"]), "MoltenVK/MoltenVK.xcframework"),
  129. )
  130. for mvk_path in mvk_list:
  131. if mvk_path and os.path.isfile(os.path.join(mvk_path, f"{osname}/libMoltenVK.a")):
  132. print(f"MoltenVK found at: {mvk_path}")
  133. return mvk_path
  134. return ""