platform_methods.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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"]
  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. }
  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 ""