platform_methods.py 4.7 KB

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