2021-04-16 00:59:52 +00:00
|
|
|
#!/usr/bin/env python3
|
2014-04-26 21:49:42 +00:00
|
|
|
|
2024-09-15 12:08:30 +00:00
|
|
|
import json
|
2014-04-26 21:49:42 +00:00
|
|
|
import os
|
2024-09-15 12:08:30 +00:00
|
|
|
import re
|
2014-04-26 21:49:42 +00:00
|
|
|
import shutil
|
|
|
|
import subprocess
|
2024-09-15 12:08:30 +00:00
|
|
|
import sys
|
2014-04-26 21:49:42 +00:00
|
|
|
from functools import partial
|
|
|
|
|
|
|
|
sys_re = re.compile("^/System")
|
|
|
|
usr_re = re.compile("^/usr/lib/")
|
|
|
|
exe_re = re.compile("@executable_path")
|
|
|
|
|
|
|
|
def is_user_lib(objfile, libname):
|
|
|
|
return not sys_re.match(libname) and \
|
|
|
|
not usr_re.match(libname) and \
|
|
|
|
not exe_re.match(libname) and \
|
2024-09-13 17:22:05 +00:00
|
|
|
"libobjc." not in libname and \
|
|
|
|
"libSystem." not in libname and \
|
|
|
|
"libc." not in libname and \
|
|
|
|
"libgcc." not in libname and \
|
2024-09-15 12:08:30 +00:00
|
|
|
os.path.basename(libname) != "Python" and \
|
2024-09-13 17:22:05 +00:00
|
|
|
os.path.basename(objfile) not in libname and \
|
|
|
|
"libswift" not in libname
|
2014-04-26 21:49:42 +00:00
|
|
|
|
2023-11-13 15:55:57 +00:00
|
|
|
def otool(objfile, rapths):
|
2024-09-15 12:08:30 +00:00
|
|
|
command = f"otool -L '{objfile}' | grep -e '\t' | awk '{{ print $1 }}'"
|
|
|
|
output = subprocess.check_output(command, shell=True, universal_newlines=True)
|
2023-11-13 15:55:57 +00:00
|
|
|
libs = set(filter(partial(is_user_lib, objfile), output.split()))
|
|
|
|
|
|
|
|
libs_resolved = set()
|
|
|
|
libs_relative = set()
|
|
|
|
for lib in libs:
|
|
|
|
lib_path = resolve_lib_path(objfile, lib, rapths)
|
|
|
|
libs_resolved.add(lib_path)
|
|
|
|
if lib_path != lib:
|
|
|
|
libs_relative.add(lib)
|
|
|
|
|
|
|
|
return libs_resolved, libs_relative
|
|
|
|
|
|
|
|
def get_rapths(objfile):
|
|
|
|
rpaths = []
|
2024-09-15 12:08:30 +00:00
|
|
|
command = f"otool -l '{objfile}' | grep -A2 LC_RPATH | grep path"
|
|
|
|
path_re = re.compile(r"^\s*path (.*) \(offset \d*\)$")
|
2023-11-13 15:55:57 +00:00
|
|
|
|
|
|
|
try:
|
2024-09-15 12:08:30 +00:00
|
|
|
result = subprocess.check_output(command, shell=True, universal_newlines=True)
|
2024-09-13 17:22:05 +00:00
|
|
|
except Exception:
|
2023-11-13 15:55:57 +00:00
|
|
|
return rpaths
|
|
|
|
|
|
|
|
for line in result.splitlines():
|
2024-09-15 12:08:30 +00:00
|
|
|
line_clean = path_re.search(line).group(1).strip()
|
2024-02-21 23:44:19 +00:00
|
|
|
# resolve @loader_path
|
2024-09-15 00:55:28 +00:00
|
|
|
if line_clean.startswith("@loader_path/"):
|
|
|
|
line_clean = line_clean[len("@loader_path/"):]
|
2024-09-15 00:43:15 +00:00
|
|
|
line_clean = os.path.join(os.path.dirname(objfile), line_clean)
|
|
|
|
line_clean = os.path.normpath(line_clean)
|
2024-02-21 23:44:19 +00:00
|
|
|
rpaths.append(line_clean)
|
2023-11-13 15:55:57 +00:00
|
|
|
|
|
|
|
return rpaths
|
2014-04-26 21:49:42 +00:00
|
|
|
|
2019-09-21 22:32:40 +00:00
|
|
|
def get_rpaths_dev_tools(binary):
|
2024-09-15 00:43:15 +00:00
|
|
|
command = (
|
|
|
|
f"otool -l '{binary}' | grep -A2 LC_RPATH | grep path | "
|
2024-09-15 00:55:28 +00:00
|
|
|
'grep "Xcode\\|CommandLineTools"'
|
2024-09-15 00:43:15 +00:00
|
|
|
)
|
2019-09-21 22:32:40 +00:00
|
|
|
result = subprocess.check_output(command, shell = True, universal_newlines=True)
|
2024-09-15 12:08:30 +00:00
|
|
|
path_re = re.compile(r"^\s*path (.*) \(offset \d*\)$")
|
|
|
|
return [
|
|
|
|
path_re.search(line).group(1).strip()
|
|
|
|
for line in result.splitlines()
|
|
|
|
]
|
2019-09-21 22:32:40 +00:00
|
|
|
|
2023-11-13 15:55:57 +00:00
|
|
|
def resolve_lib_path(objfile, lib, rapths):
|
|
|
|
if os.path.exists(lib):
|
|
|
|
return lib
|
|
|
|
|
2024-09-15 00:55:28 +00:00
|
|
|
if lib.startswith("@rpath/"):
|
|
|
|
lib = lib[len("@rpath/"):]
|
2023-11-13 15:55:57 +00:00
|
|
|
for rpath in rapths:
|
|
|
|
lib_path = os.path.join(rpath, lib)
|
|
|
|
if os.path.exists(lib_path):
|
|
|
|
return lib_path
|
2024-09-15 00:55:28 +00:00
|
|
|
elif lib.startswith("@loader_path/"):
|
|
|
|
lib = lib[len("@loader_path/"):]
|
2023-11-13 15:55:57 +00:00
|
|
|
lib_path = os.path.normpath(os.path.join(objfile, lib))
|
|
|
|
if os.path.exists(lib_path):
|
|
|
|
return lib_path
|
|
|
|
|
2024-09-15 00:55:28 +00:00
|
|
|
raise Exception("Could not resolve library: " + lib)
|
2023-11-13 15:55:57 +00:00
|
|
|
|
2024-02-17 18:54:03 +00:00
|
|
|
def check_vulkan_max_version(version):
|
|
|
|
try:
|
2024-09-15 00:43:15 +00:00
|
|
|
subprocess.check_output(
|
|
|
|
f"pkg-config vulkan --max-version={version}",
|
|
|
|
shell=True,
|
|
|
|
)
|
2024-02-17 18:54:03 +00:00
|
|
|
return True
|
2024-09-13 17:22:05 +00:00
|
|
|
except Exception:
|
2024-02-17 18:54:03 +00:00
|
|
|
return False
|
|
|
|
|
2024-02-23 21:18:28 +00:00
|
|
|
def get_homebrew_prefix():
|
2024-09-15 00:43:15 +00:00
|
|
|
# set default to standard ARM path, intel path is already in the vulkan
|
|
|
|
# loader search array
|
2024-02-29 14:07:34 +00:00
|
|
|
result = "/opt/homebrew"
|
|
|
|
try:
|
2024-09-15 00:43:15 +00:00
|
|
|
result = subprocess.check_output(
|
2024-09-15 12:08:30 +00:00
|
|
|
["brew", "--prefix"],
|
2024-09-15 00:43:15 +00:00
|
|
|
universal_newlines=True,
|
2024-09-15 12:08:30 +00:00
|
|
|
stderr=subprocess.DEVNULL,
|
2024-09-15 00:43:15 +00:00
|
|
|
).strip()
|
2024-09-13 17:22:05 +00:00
|
|
|
except Exception:
|
2024-02-29 14:07:34 +00:00
|
|
|
pass
|
|
|
|
|
|
|
|
return result
|
2024-02-23 21:18:28 +00:00
|
|
|
|
2014-04-26 21:49:42 +00:00
|
|
|
def install_name_tool_change(old, new, objfile):
|
2024-09-15 00:43:15 +00:00
|
|
|
subprocess.call(
|
|
|
|
["install_name_tool", "-change", old, new, objfile],
|
|
|
|
stderr=subprocess.DEVNULL,
|
|
|
|
)
|
2014-04-26 21:49:42 +00:00
|
|
|
|
|
|
|
def install_name_tool_id(name, objfile):
|
2024-09-15 00:43:15 +00:00
|
|
|
subprocess.call(
|
|
|
|
["install_name_tool", "-id", name, objfile],
|
|
|
|
stderr=subprocess.DEVNULL,
|
|
|
|
)
|
2014-04-26 21:49:42 +00:00
|
|
|
|
2019-04-22 10:49:02 +00:00
|
|
|
def install_name_tool_add_rpath(rpath, binary):
|
|
|
|
subprocess.call(["install_name_tool", "-add_rpath", rpath, binary])
|
|
|
|
|
2019-09-21 22:32:40 +00:00
|
|
|
def install_name_tool_delete_rpath(rpath, binary):
|
|
|
|
subprocess.call(["install_name_tool", "-delete_rpath", rpath, binary])
|
|
|
|
|
2024-09-15 12:08:30 +00:00
|
|
|
def libraries(objfile, result=None, result_relative=None, rapths=None):
|
|
|
|
if result is None:
|
|
|
|
result = {}
|
|
|
|
|
|
|
|
if result_relative is None:
|
|
|
|
result_relative = set()
|
|
|
|
|
|
|
|
if rapths is None:
|
|
|
|
rapths = []
|
|
|
|
|
2023-11-13 15:55:57 +00:00
|
|
|
rapths = get_rapths(objfile) + rapths
|
|
|
|
libs_list, libs_relative = otool(objfile, rapths)
|
2018-03-06 19:48:15 +00:00
|
|
|
result[objfile] = libs_list
|
2023-11-13 15:55:57 +00:00
|
|
|
result_relative |= libs_relative
|
2014-04-26 21:49:42 +00:00
|
|
|
|
|
|
|
for lib in libs_list:
|
|
|
|
if lib not in result:
|
2023-11-13 15:55:57 +00:00
|
|
|
libraries(lib, result, result_relative, rapths)
|
2014-04-26 21:49:42 +00:00
|
|
|
|
2023-11-13 15:55:57 +00:00
|
|
|
return result, result_relative
|
2014-04-26 21:49:42 +00:00
|
|
|
|
|
|
|
def lib_path(binary):
|
2024-09-15 00:55:28 +00:00
|
|
|
return os.path.join(os.path.dirname(binary), "lib")
|
2014-04-26 21:49:42 +00:00
|
|
|
|
2024-02-17 18:54:03 +00:00
|
|
|
def resources_path(binary):
|
2024-09-15 00:55:28 +00:00
|
|
|
return os.path.join(os.path.dirname(binary), "../Resources")
|
2024-02-17 18:54:03 +00:00
|
|
|
|
2014-04-26 21:49:42 +00:00
|
|
|
def lib_name(lib):
|
|
|
|
return os.path.join("@executable_path", "lib", os.path.basename(lib))
|
|
|
|
|
2023-11-13 15:55:57 +00:00
|
|
|
def process_libraries(libs_dict, libs_dyn, binary):
|
2019-12-09 00:38:27 +00:00
|
|
|
libs_set = set(libs_dict)
|
2022-05-12 15:12:31 +00:00
|
|
|
# Remove binary from libs_set to prevent a duplicate of the binary being
|
2019-12-09 00:38:27 +00:00
|
|
|
# added to the libs directory.
|
|
|
|
libs_set.remove(binary)
|
2014-04-26 21:49:42 +00:00
|
|
|
|
2019-12-09 00:38:27 +00:00
|
|
|
for src in libs_set:
|
2014-04-26 21:49:42 +00:00
|
|
|
name = lib_name(src)
|
2023-11-13 15:55:57 +00:00
|
|
|
dst = os.path.join(lib_path(binary), os.path.basename(src))
|
2014-04-26 21:49:42 +00:00
|
|
|
|
|
|
|
shutil.copy(src, dst)
|
|
|
|
os.chmod(dst, 0o755)
|
|
|
|
install_name_tool_id(name, dst)
|
|
|
|
|
|
|
|
if src in libs_dict[binary]:
|
|
|
|
install_name_tool_change(src, name, binary)
|
|
|
|
|
2019-12-09 00:38:27 +00:00
|
|
|
for p in libs_set:
|
2014-04-26 21:49:42 +00:00
|
|
|
if p in libs_dict[src]:
|
|
|
|
install_name_tool_change(p, lib_name(p), dst)
|
|
|
|
|
2023-11-13 15:55:57 +00:00
|
|
|
for lib in libs_dyn:
|
|
|
|
install_name_tool_change(lib, lib_name(lib), dst)
|
|
|
|
|
|
|
|
for lib in libs_dyn:
|
|
|
|
install_name_tool_change(lib, lib_name(lib), binary)
|
|
|
|
|
2019-04-22 10:49:02 +00:00
|
|
|
def process_swift_libraries(binary):
|
2024-09-15 12:08:30 +00:00
|
|
|
swift_stdlib_tool = subprocess.check_output(
|
|
|
|
["xcrun", "--find", "swift-stdlib-tool"],
|
|
|
|
universal_newlines=True,
|
|
|
|
).strip()
|
2019-09-20 13:14:59 +00:00
|
|
|
# from xcode11 on the dynamic swift libs reside in a separate directory from
|
|
|
|
# the std one, might need versioned paths for future swift versions
|
2024-09-15 12:08:30 +00:00
|
|
|
swift_lib_path = os.path.join(swift_stdlib_tool, "../../lib/swift-5.0/macosx")
|
|
|
|
swift_lib_path = os.path.abspath(swift_lib_path)
|
2019-04-22 10:49:02 +00:00
|
|
|
|
2024-09-15 00:43:15 +00:00
|
|
|
command = [
|
2024-09-15 12:08:30 +00:00
|
|
|
swift_stdlib_tool, "--copy", "--platform", "macosx",
|
|
|
|
"--scan-executable", binary, "--destination", lib_path(binary),
|
2024-09-15 00:43:15 +00:00
|
|
|
]
|
2019-09-20 13:14:59 +00:00
|
|
|
|
2024-09-15 12:08:30 +00:00
|
|
|
if os.path.exists(swift_lib_path):
|
|
|
|
command.extend(["--source-libraries", swift_lib_path])
|
2019-09-20 13:14:59 +00:00
|
|
|
|
2019-04-22 10:49:02 +00:00
|
|
|
subprocess.check_output(command, universal_newlines=True)
|
|
|
|
|
|
|
|
print(">> setting additional rpath for swift libraries")
|
|
|
|
install_name_tool_add_rpath("@executable_path/lib", binary)
|
|
|
|
|
2024-09-15 12:08:30 +00:00
|
|
|
def process_vulkan_loader(binary, loader_name, loader_relative_folder, library_node):
|
2024-02-17 18:54:03 +00:00
|
|
|
# https://github.com/KhronosGroup/Vulkan-Loader/blob/main/docs/LoaderDriverInterface.md#example-macos-driver-search-path
|
|
|
|
# https://github.com/KhronosGroup/Vulkan-Loader/blob/main/docs/LoaderLayerInterface.md#macos-layer-discovery
|
|
|
|
loaderSystemSearchFolders = [
|
2024-09-15 12:08:30 +00:00
|
|
|
os.path.join(os.path.expanduser("~"), ".config", loader_relative_folder),
|
|
|
|
os.path.join("/etc/xdg", loader_relative_folder),
|
|
|
|
os.path.join("/usr/local/etc", loader_relative_folder),
|
|
|
|
os.path.join("/etc", loader_relative_folder),
|
|
|
|
os.path.join(os.path.expanduser("~"), ".local/share", loader_relative_folder),
|
|
|
|
os.path.join("/usr/local/share", loader_relative_folder),
|
|
|
|
os.path.join("/usr/share/vulkan", loader_relative_folder),
|
|
|
|
os.path.join(get_homebrew_prefix(), "share", loader_relative_folder),
|
2024-02-17 18:54:03 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
loaderSystemFolder = ""
|
|
|
|
for loaderSystemSearchFolder in loaderSystemSearchFolders:
|
|
|
|
if os.path.exists(loaderSystemSearchFolder):
|
|
|
|
loaderSystemFolder = loaderSystemSearchFolder
|
|
|
|
break
|
|
|
|
|
|
|
|
if not loaderSystemFolder:
|
2024-09-15 12:08:30 +00:00
|
|
|
print(">>> could not find loader folder " + loader_relative_folder)
|
2024-02-17 18:54:03 +00:00
|
|
|
return
|
|
|
|
|
2024-09-15 12:08:30 +00:00
|
|
|
loaderBundleFolder = os.path.join(resources_path(binary), loader_relative_folder)
|
|
|
|
loaderSystemPath = os.path.join(loaderSystemFolder, loader_name)
|
|
|
|
loaderBundlePath = os.path.join(loaderBundleFolder, loader_name)
|
2024-02-17 18:54:03 +00:00
|
|
|
libraryRelativeFolder = "../../../Frameworks/"
|
|
|
|
|
|
|
|
if not os.path.exists(loaderSystemPath):
|
2024-09-15 12:08:30 +00:00
|
|
|
print(">>> could not find loader " + loader_name)
|
2024-02-17 18:54:03 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
if not os.path.exists(loaderBundleFolder):
|
|
|
|
os.makedirs(loaderBundleFolder)
|
|
|
|
|
2024-09-15 12:08:30 +00:00
|
|
|
loaderSystemFile = open(loaderSystemPath)
|
2024-02-17 18:54:03 +00:00
|
|
|
loaderJsonData = json.load(loaderSystemFile)
|
2024-09-15 12:08:30 +00:00
|
|
|
libraryPath = loaderJsonData[library_node]["library_path"]
|
2024-09-15 00:43:15 +00:00
|
|
|
librarySystemPath = os.path.join(loaderSystemFolder, libraryPath)
|
2024-02-17 18:54:03 +00:00
|
|
|
|
|
|
|
if not os.path.exists(librarySystemPath):
|
|
|
|
print(">>> could not find loader library " + librarySystemPath)
|
|
|
|
return
|
|
|
|
|
2024-09-15 12:08:30 +00:00
|
|
|
print(">>> modifiying and writing loader json " + loader_name)
|
2024-09-15 00:55:28 +00:00
|
|
|
loaderBundleFile = open(loaderBundlePath, "w")
|
2024-02-17 18:54:03 +00:00
|
|
|
loaderLibraryName = os.path.basename(librarySystemPath)
|
2024-09-15 00:43:15 +00:00
|
|
|
library_path = os.path.join(libraryRelativeFolder, loaderLibraryName)
|
2024-09-15 12:08:30 +00:00
|
|
|
loaderJsonData[library_node]["library_path"] = library_path
|
2024-02-17 18:54:03 +00:00
|
|
|
json.dump(loaderJsonData, loaderBundleFile, indent=4)
|
|
|
|
|
|
|
|
print(">>> copying loader library " + loaderLibraryName)
|
|
|
|
frameworkBundleFolder = os.path.join(loaderBundleFolder, libraryRelativeFolder)
|
|
|
|
if not os.path.exists(frameworkBundleFolder):
|
|
|
|
os.makedirs(frameworkBundleFolder)
|
2024-09-15 00:43:15 +00:00
|
|
|
library_target_path = os.path.join(frameworkBundleFolder, loaderLibraryName)
|
|
|
|
shutil.copy(librarySystemPath, library_target_path)
|
2024-02-17 18:54:03 +00:00
|
|
|
|
2019-09-21 22:32:40 +00:00
|
|
|
def remove_dev_tools_rapths(binary):
|
|
|
|
for path in get_rpaths_dev_tools(binary):
|
|
|
|
install_name_tool_delete_rpath(path, binary)
|
|
|
|
|
2024-02-22 18:38:46 +00:00
|
|
|
def process(binary):
|
|
|
|
binary = os.path.abspath(binary)
|
2014-04-26 21:49:42 +00:00
|
|
|
if not os.path.exists(lib_path(binary)):
|
|
|
|
os.makedirs(lib_path(binary))
|
2019-04-22 10:49:02 +00:00
|
|
|
print(">> gathering all linked libraries")
|
2023-11-13 15:55:57 +00:00
|
|
|
libs, libs_rel = libraries(binary)
|
2018-03-06 19:48:15 +00:00
|
|
|
|
2019-04-22 10:49:02 +00:00
|
|
|
print(">> copying and processing all linked libraries")
|
2023-11-13 15:55:57 +00:00
|
|
|
process_libraries(libs, libs_rel, binary)
|
2014-04-26 21:49:42 +00:00
|
|
|
|
2019-09-21 22:32:40 +00:00
|
|
|
print(">> removing rpath definitions towards dev tools")
|
|
|
|
remove_dev_tools_rapths(binary)
|
|
|
|
|
2019-04-22 10:49:02 +00:00
|
|
|
print(">> copying and processing swift libraries")
|
|
|
|
process_swift_libraries(binary)
|
|
|
|
|
2024-02-17 18:54:03 +00:00
|
|
|
print(">> copying and processing vulkan loader")
|
|
|
|
process_vulkan_loader(binary, "MoltenVK_icd.json", "vulkan/icd.d", "ICD")
|
|
|
|
if check_vulkan_max_version("1.3.261.1"):
|
2024-09-15 00:43:15 +00:00
|
|
|
process_vulkan_loader(
|
|
|
|
binary,
|
|
|
|
"VkLayer_khronos_synchronization2.json",
|
|
|
|
"vulkan/explicit_layer.d",
|
|
|
|
"layer",
|
|
|
|
)
|
2024-02-17 18:54:03 +00:00
|
|
|
|
2014-04-26 21:49:42 +00:00
|
|
|
if __name__ == "__main__":
|
2024-02-22 18:38:46 +00:00
|
|
|
process(sys.argv[1])
|