platform_methods.py 4.4 KB

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