platform_methods.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. wf = export_path + "/" + name + "_svg.gen.h"
  60. methods.write_file_if_needed(wf, svg_str)
  61. def get_build_version(short):
  62. import version
  63. name = "custom_build"
  64. if os.getenv("BUILD_NAME") != None:
  65. name = os.getenv("BUILD_NAME")
  66. v = "%d.%d" % (version.major, version.minor)
  67. if version.patch > 0:
  68. v += ".%d" % version.patch
  69. status = version.status
  70. if not short:
  71. if os.getenv("GODOT_VERSION_STATUS") != None:
  72. status = str(os.getenv("GODOT_VERSION_STATUS"))
  73. v += ".%s.%s" % (status, name)
  74. return v
  75. def lipo(prefix, suffix):
  76. from pathlib import Path
  77. target_bin = ""
  78. lipo_command = ["lipo", "-create"]
  79. arch_found = 0
  80. for arch in architectures:
  81. bin_name = prefix + "." + arch + suffix
  82. if Path(bin_name).is_file():
  83. target_bin = bin_name
  84. lipo_command += [bin_name]
  85. arch_found += 1
  86. if arch_found > 1:
  87. target_bin = prefix + ".fat" + suffix
  88. lipo_command += ["-output", target_bin]
  89. subprocess.run(lipo_command)
  90. return target_bin
  91. def get_mvk_sdk_path(osname):
  92. def int_or_zero(i):
  93. try:
  94. return int(i)
  95. except:
  96. return 0
  97. def ver_parse(a):
  98. return [int_or_zero(i) for i in a.split(".")]
  99. dirname = os.path.expanduser("~/VulkanSDK")
  100. if not os.path.exists(dirname):
  101. return ""
  102. ver_min = ver_parse("1.3.231.0")
  103. ver_num = ver_parse("0.0.0.0")
  104. files = os.listdir(dirname)
  105. lib_name_out = dirname
  106. for file in files:
  107. if os.path.isdir(os.path.join(dirname, file)):
  108. ver_comp = ver_parse(file)
  109. if ver_comp > ver_num and ver_comp >= ver_min:
  110. # Try new SDK location.
  111. lib_name = os.path.join(os.path.join(dirname, file), "macOS/lib/MoltenVK.xcframework/" + osname + "/")
  112. if os.path.isfile(os.path.join(lib_name, "libMoltenVK.a")):
  113. ver_num = ver_comp
  114. lib_name_out = os.path.join(os.path.join(dirname, file), "macOS/lib/MoltenVK.xcframework")
  115. else:
  116. # Try old SDK location.
  117. lib_name = os.path.join(
  118. os.path.join(dirname, file), "MoltenVK/MoltenVK.xcframework/" + osname + "/"
  119. )
  120. if os.path.isfile(os.path.join(lib_name, "libMoltenVK.a")):
  121. ver_num = ver_comp
  122. lib_name_out = os.path.join(os.path.join(dirname, file), "MoltenVK/MoltenVK.xcframework")
  123. return lib_name_out
  124. def detect_mvk(env, osname):
  125. mvk_list = [
  126. get_mvk_sdk_path(osname),
  127. "/opt/homebrew/Frameworks/MoltenVK.xcframework",
  128. "/usr/local/homebrew/Frameworks/MoltenVK.xcframework",
  129. "/opt/local/Frameworks/MoltenVK.xcframework",
  130. ]
  131. if env["vulkan_sdk_path"] != "":
  132. mvk_list.insert(0, os.path.expanduser(env["vulkan_sdk_path"]))
  133. mvk_list.insert(
  134. 0,
  135. os.path.join(os.path.expanduser(env["vulkan_sdk_path"]), "macOS/lib/MoltenVK.xcframework"),
  136. )
  137. mvk_list.insert(
  138. 0,
  139. os.path.join(os.path.expanduser(env["vulkan_sdk_path"]), "MoltenVK/MoltenVK.xcframework"),
  140. )
  141. for mvk_path in mvk_list:
  142. if mvk_path and os.path.isfile(os.path.join(mvk_path, osname + "/libMoltenVK.a")):
  143. mvk_found = True
  144. print("MoltenVK found at: " + mvk_path)
  145. return mvk_path
  146. return ""