platform_methods.py 4.4 KB

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