platform_methods.py 5.4 KB

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