Skip to content

Commit

Permalink
Yeet.
Browse files Browse the repository at this point in the history
  • Loading branch information
viferga committed Sep 3, 2024
1 parent bb588b5 commit 2861d1e
Showing 1 changed file with 27 additions and 21 deletions.
48 changes: 27 additions & 21 deletions brew-pkg.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,28 +41,34 @@ def patchelf(root_dir, prefix_path, binary, format='@executable_path')
lib_paths.each do |lib|
lib_path = (File.realpath(File.join(root_dir, lib)) rescue nil)

if lib_path && lib_path != binary_path
# Obtain the relative path from the executable
lib_relative_path = lib_path.delete_prefix(full_prefix_path)
binary_relative_path = File.dirname(binary_path).delete_prefix(full_prefix_path)
relative_path = Pathname.new(lib_relative_path).relative_path_from(Pathname.new(binary_relative_path))
new_lib = File.join(format, relative_path)

# Patch the library path relative to the binary path
ohai "install_name_tool -change #{lib} #{new_lib} #{binary_path}"
system("install_name_tool", "-change", lib, new_lib, binary_path)

# Debug information
stdout, status = Open3.capture2("otool -L #{binary_path}")
ohai "After patching:"
ohai "#{stdout}"
ohai "patchelf(#{root_dir}, #{prefix_path}, #{lib.delete_prefix(prefix_path)})"

# Recursively iterate through libraries
patchelf(root_dir, prefix_path, lib.delete_prefix(prefix_path), '@rpath')
else
if lib_path == nil
opoo "File 'File.realpath(File.join(#{root_dir}, #{lib})' not found"
next
end

if lib_path == binary_path
opoo "The link '#{File.join(root_dir, lib)}' refers to itself: '#{binary_path}'"
next
end

# Obtain the relative path from the executable
lib_relative_path = lib_path.delete_prefix(full_prefix_path)
binary_relative_path = File.dirname(binary_path).delete_prefix(full_prefix_path)
relative_path = Pathname.new(lib_relative_path).relative_path_from(Pathname.new(binary_relative_path))
new_lib = File.join(format, relative_path)

# Patch the library path relative to the binary path
ohai "install_name_tool -change #{lib} #{new_lib} #{binary_path}"
system("install_name_tool", "-change", lib, new_lib, binary_path)

# Debug information
stdout, status = Open3.capture2("otool -L #{binary_path}")
ohai "After patching:"
ohai "#{stdout}"
ohai "patchelf(#{root_dir}, #{prefix_path}, #{lib.delete_prefix(prefix_path)})"

# Recursively iterate through libraries
patchelf(root_dir, prefix_path, lib.delete_prefix(prefix_path), '@rpath')
end
end

Expand Down Expand Up @@ -210,7 +216,7 @@ def pkg
safe_system "mkdir", "-p", "#{staging_root}/Cellar/#{formula.name}/"
safe_system "rsync", "-a", "#{HOMEBREW_CELLAR}/#{formula.name}/#{dep_version}", "#{staging_root}/Cellar/#{formula.name}/"
safe_system "mkdir", "-p", "#{staging_root}/opt"
safe_system "ln", "-s", "#{staging_root}/Cellar/#{formula.name}/#{dep_version}", "#{staging_root}/opt/#{formula.name}"
safe_system "ln", "-s", "../Cellar/#{formula.name}/#{dep_version}", "#{staging_root}/opt/#{formula.name}"
end
end

Expand Down

0 comments on commit 2861d1e

Please sign in to comment.