platform_methods.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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 generate_export_icons(platform_path, platform_name):
  40. """
  41. Generate headers for logo and run icon for the export plugin.
  42. """
  43. export_path = platform_path + "/export"
  44. svg_names = []
  45. if os.path.isfile(export_path + "/logo.svg"):
  46. svg_names.append("logo")
  47. if os.path.isfile(export_path + "/run_icon.svg"):
  48. svg_names.append("run_icon")
  49. for name in svg_names:
  50. with open(export_path + "/" + name + ".svg", "rb") as svgf:
  51. b = svgf.read(1)
  52. svg_str = " /* AUTOGENERATED FILE, DO NOT EDIT */ \n"
  53. svg_str += " static const char *_" + platform_name + "_" + name + '_svg = "'
  54. while len(b) == 1:
  55. svg_str += "\\" + hex(ord(b))[1:]
  56. b = svgf.read(1)
  57. svg_str += '";\n'
  58. wf = export_path + "/" + name + "_svg.gen.h"
  59. methods.write_file_if_needed(wf, svg_str)
  60. def get_build_version(short):
  61. import version
  62. name = "custom_build"
  63. if os.getenv("BUILD_NAME") != None:
  64. name = os.getenv("BUILD_NAME")
  65. v = "%d.%d" % (version.major, version.minor)
  66. if version.patch > 0:
  67. v += ".%d" % version.patch
  68. status = version.status
  69. if not short:
  70. if os.getenv("GODOT_VERSION_STATUS") != None:
  71. status = str(os.getenv("GODOT_VERSION_STATUS"))
  72. v += ".%s.%s" % (status, name)
  73. return v
  74. def lipo(prefix, suffix):
  75. from pathlib import Path
  76. target_bin = ""
  77. lipo_command = ["lipo", "-create"]
  78. arch_found = 0
  79. for arch in architectures:
  80. bin_name = prefix + "." + arch + suffix
  81. if Path(bin_name).is_file():
  82. target_bin = bin_name
  83. lipo_command += [bin_name]
  84. arch_found += 1
  85. if arch_found > 1:
  86. target_bin = prefix + ".fat" + suffix
  87. lipo_command += ["-output", target_bin]
  88. subprocess.run(lipo_command)
  89. return target_bin
  90. def get_mvk_sdk_path(osname):
  91. def int_or_zero(i):
  92. try:
  93. return int(i)
  94. except:
  95. return 0
  96. def ver_parse(a):
  97. return [int_or_zero(i) for i in a.split(".")]
  98. dirname = os.path.expanduser("~/VulkanSDK")
  99. if not os.path.exists(dirname):
  100. return ""
  101. ver_min = ver_parse("1.3.231.0")
  102. ver_num = ver_parse("0.0.0.0")
  103. files = os.listdir(dirname)
  104. lib_name_out = dirname
  105. for file in files:
  106. if os.path.isdir(os.path.join(dirname, file)):
  107. ver_comp = ver_parse(file)
  108. if ver_comp > ver_num and ver_comp >= ver_min:
  109. # Try new SDK location.
  110. lib_name = os.path.join(os.path.join(dirname, file), "macOS/lib/MoltenVK.xcframework/" + osname + "/")
  111. if os.path.isfile(os.path.join(lib_name, "libMoltenVK.a")):
  112. ver_num = ver_comp
  113. lib_name_out = os.path.join(os.path.join(dirname, file), "macOS/lib/MoltenVK.xcframework")
  114. else:
  115. # Try old SDK location.
  116. lib_name = os.path.join(
  117. os.path.join(dirname, file), "MoltenVK/MoltenVK.xcframework/" + osname + "/"
  118. )
  119. if os.path.isfile(os.path.join(lib_name, "libMoltenVK.a")):
  120. ver_num = ver_comp
  121. lib_name_out = os.path.join(os.path.join(dirname, file), "MoltenVK/MoltenVK.xcframework")
  122. return lib_name_out
  123. def detect_mvk(env, osname):
  124. mvk_list = [
  125. get_mvk_sdk_path(osname),
  126. "/opt/homebrew/Frameworks/MoltenVK.xcframework",
  127. "/usr/local/homebrew/Frameworks/MoltenVK.xcframework",
  128. "/opt/local/Frameworks/MoltenVK.xcframework",
  129. ]
  130. if env["vulkan_sdk_path"] != "":
  131. mvk_list.insert(0, os.path.expanduser(env["vulkan_sdk_path"]))
  132. mvk_list.insert(
  133. 0,
  134. os.path.join(os.path.expanduser(env["vulkan_sdk_path"]), "macOS/lib/MoltenVK.xcframework"),
  135. )
  136. mvk_list.insert(
  137. 0,
  138. os.path.join(os.path.expanduser(env["vulkan_sdk_path"]), "MoltenVK/MoltenVK.xcframework"),
  139. )
  140. for mvk_path in mvk_list:
  141. if mvk_path and os.path.isfile(os.path.join(mvk_path, osname + "/libMoltenVK.a")):
  142. mvk_found = True
  143. print("MoltenVK found at: " + mvk_path)
  144. return mvk_path
  145. return ""