2020-10-10 14:16:11 +02:00
|
|
|
# typed: true
|
2019-04-19 15:38:03 +09:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-07-09 13:52:05 +01:00
|
|
|
class Keg
|
2018-04-07 20:28:56 +01:00
|
|
|
class << self
|
|
|
|
undef file_linked_libraries
|
|
|
|
|
|
|
|
def file_linked_libraries(file, string)
|
|
|
|
# Check dynamic library linkage. Importantly, do not perform for static
|
|
|
|
# libraries, which will falsely report "linkage" to themselves.
|
|
|
|
if file.mach_o_executable? || file.dylib? || file.mach_o_bundle?
|
|
|
|
file.dynamically_linked_libraries.select { |lib| lib.include? string }
|
|
|
|
else
|
|
|
|
[]
|
2016-07-09 13:52:05 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-04-02 12:38:39 +01:00
|
|
|
undef relocate_dynamic_linkage
|
2018-04-07 20:28:56 +01:00
|
|
|
|
2016-10-23 23:48:07 -04:00
|
|
|
def relocate_dynamic_linkage(relocation)
|
2016-07-09 13:52:05 +01:00
|
|
|
mach_o_files.each do |file|
|
|
|
|
file.ensure_writable do
|
|
|
|
if file.dylib?
|
2021-05-12 21:37:33 +01:00
|
|
|
id = relocated_name_for(file.dylib_id, relocation)
|
|
|
|
change_dylib_id(id, file)
|
2016-07-09 13:52:05 +01:00
|
|
|
end
|
|
|
|
|
2021-05-12 03:57:09 +01:00
|
|
|
each_linkage_for(file, :dynamically_linked_libraries) do |old_name|
|
2021-05-10 17:42:35 +01:00
|
|
|
new_name = relocated_name_for(old_name, relocation)
|
2017-09-27 02:08:23 -07:00
|
|
|
change_install_name(old_name, new_name, file) if new_name
|
2016-07-09 13:52:05 +01:00
|
|
|
end
|
2021-05-05 23:43:43 +01:00
|
|
|
|
2022-01-18 15:16:01 +08:00
|
|
|
each_linkage_for(file, :rpaths) do |old_name|
|
|
|
|
new_name = relocated_name_for(old_name, relocation)
|
|
|
|
change_rpath(old_name, new_name, file) if new_name
|
2021-05-05 23:43:43 +01:00
|
|
|
end
|
2016-07-09 13:52:05 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-04-07 20:28:56 +01:00
|
|
|
def fix_dynamic_linkage
|
|
|
|
mach_o_files.each do |file|
|
|
|
|
file.ensure_writable do
|
|
|
|
change_dylib_id(dylib_id_for(file), file) if file.dylib?
|
|
|
|
|
2021-05-12 03:57:09 +01:00
|
|
|
each_linkage_for(file, :dynamically_linked_libraries) do |bad_name|
|
2018-04-07 20:28:56 +01:00
|
|
|
# Don't fix absolute paths unless they are rooted in the build directory
|
|
|
|
next if bad_name.start_with?("/") &&
|
2023-05-22 17:18:43 +08:00
|
|
|
!rooted_in_build_directory?(bad_name)
|
2018-04-07 20:28:56 +01:00
|
|
|
|
|
|
|
new_name = fixed_name(file, bad_name)
|
2023-04-18 15:06:50 -07:00
|
|
|
change_install_name(bad_name, new_name, file) if new_name != bad_name
|
2018-04-07 20:28:56 +01:00
|
|
|
end
|
2021-05-14 00:00:01 +01:00
|
|
|
|
|
|
|
each_linkage_for(file, :rpaths) do |bad_name|
|
2022-01-18 19:29:58 +08:00
|
|
|
# Strip duplicate rpaths and rpaths rooted in the build directory.
|
2023-05-22 17:18:43 +08:00
|
|
|
next if !rooted_in_build_directory?(bad_name) &&
|
2022-01-19 00:34:39 +08:00
|
|
|
(file.rpaths.count(bad_name) == 1)
|
2021-05-14 00:00:01 +01:00
|
|
|
|
|
|
|
delete_rpath(bad_name, file)
|
|
|
|
end
|
2018-04-07 20:28:56 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
generic_fix_dynamic_linkage
|
|
|
|
end
|
|
|
|
|
2016-07-09 13:52:05 +01:00
|
|
|
# If file is a dylib or bundle itself, look for the dylib named by
|
|
|
|
# bad_name relative to the lib directory, so that we can skip the more
|
|
|
|
# expensive recursive search if possible.
|
|
|
|
def fixed_name(file, bad_name)
|
|
|
|
if bad_name.start_with? PREFIX_PLACEHOLDER
|
2017-04-27 20:17:06 +02:00
|
|
|
bad_name.sub(PREFIX_PLACEHOLDER, HOMEBREW_PREFIX)
|
2016-07-09 13:52:05 +01:00
|
|
|
elsif bad_name.start_with? CELLAR_PLACEHOLDER
|
2017-04-27 20:17:06 +02:00
|
|
|
bad_name.sub(CELLAR_PLACEHOLDER, HOMEBREW_CELLAR)
|
2020-06-02 16:35:36 +01:00
|
|
|
elsif (file.dylib? || file.mach_o_bundle?) && (file.dirname/bad_name).exist?
|
2019-03-11 20:58:04 -04:00
|
|
|
"@loader_path/#{bad_name}"
|
2020-06-02 16:35:36 +01:00
|
|
|
elsif file.mach_o_executable? && (lib/bad_name).exist?
|
2016-07-09 13:52:05 +01:00
|
|
|
"#{lib}/#{bad_name}"
|
2020-06-02 16:35:36 +01:00
|
|
|
elsif file.mach_o_executable? && (libexec/"lib"/bad_name).exist?
|
|
|
|
"#{libexec}/lib/#{bad_name}"
|
2016-07-09 13:52:05 +01:00
|
|
|
elsif (abs_name = find_dylib(bad_name)) && abs_name.exist?
|
|
|
|
abs_name.to_s
|
|
|
|
else
|
|
|
|
opoo "Could not fix #{bad_name} in #{file}"
|
|
|
|
bad_name
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-05-12 03:57:09 +01:00
|
|
|
def each_linkage_for(file, linkage_type, &block)
|
|
|
|
links = file.method(linkage_type)
|
|
|
|
.call
|
2021-09-30 10:13:43 +01:00
|
|
|
.grep_v(/^@(loader_|executable_|r)path/)
|
2021-05-12 03:57:09 +01:00
|
|
|
links.each(&block)
|
2021-05-05 23:43:43 +01:00
|
|
|
end
|
|
|
|
|
2016-07-09 13:52:05 +01:00
|
|
|
def dylib_id_for(file)
|
|
|
|
# The new dylib ID should have the same basename as the old dylib ID, not
|
|
|
|
# the basename of the file itself.
|
|
|
|
basename = File.basename(file.dylib_id)
|
|
|
|
relative_dirname = file.dirname.relative_path_from(path)
|
2017-06-01 16:06:51 +02:00
|
|
|
(opt_record/relative_dirname/basename).to_s
|
2016-07-09 13:52:05 +01:00
|
|
|
end
|
|
|
|
|
2021-05-10 17:42:35 +01:00
|
|
|
def relocated_name_for(old_name, relocation)
|
|
|
|
old_prefix, new_prefix = relocation.replacement_pair_for(:prefix)
|
|
|
|
old_cellar, new_cellar = relocation.replacement_pair_for(:cellar)
|
|
|
|
|
|
|
|
if old_name.start_with? old_cellar
|
|
|
|
old_name.sub(old_cellar, new_cellar)
|
|
|
|
elsif old_name.start_with? old_prefix
|
|
|
|
old_name.sub(old_prefix, new_prefix)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-07-09 13:52:05 +01:00
|
|
|
# Matches framework references like `XXX.framework/Versions/YYY/XXX` and
|
|
|
|
# `XXX.framework/XXX`, both with or without a slash-delimited prefix.
|
2018-11-02 17:18:07 +00:00
|
|
|
FRAMEWORK_RX = %r{(?:^|/)(([^/]+)\.framework/(?:Versions/[^/]+/)?\2)$}.freeze
|
2016-07-09 13:52:05 +01:00
|
|
|
|
|
|
|
def find_dylib_suffix_from(bad_name)
|
|
|
|
if (framework = bad_name.match(FRAMEWORK_RX))
|
|
|
|
framework[1]
|
|
|
|
else
|
|
|
|
File.basename(bad_name)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def find_dylib(bad_name)
|
|
|
|
return unless lib.directory?
|
2018-09-17 02:45:00 +02:00
|
|
|
|
2016-07-09 13:52:05 +01:00
|
|
|
suffix = "/#{find_dylib_suffix_from(bad_name)}"
|
|
|
|
lib.find { |pn| break pn if pn.to_s.end_with?(suffix) }
|
|
|
|
end
|
|
|
|
|
|
|
|
def mach_o_files
|
|
|
|
hardlinks = Set.new
|
|
|
|
mach_o_files = []
|
|
|
|
path.find do |pn|
|
|
|
|
next if pn.symlink? || pn.directory?
|
2021-01-07 13:49:05 -08:00
|
|
|
next if !pn.dylib? && !pn.mach_o_bundle? && !pn.mach_o_executable?
|
2016-07-09 13:52:05 +01:00
|
|
|
# if we've already processed a file, ignore its hardlinks (which have the same dev ID and inode)
|
|
|
|
# this prevents relocations from being performed on a binary more than once
|
|
|
|
next unless hardlinks.add? [pn.stat.dev, pn.stat.ino]
|
2018-09-17 02:45:00 +02:00
|
|
|
|
2016-07-09 13:52:05 +01:00
|
|
|
mach_o_files << pn
|
|
|
|
end
|
|
|
|
|
|
|
|
mach_o_files
|
|
|
|
end
|
|
|
|
|
2021-04-29 17:52:10 +01:00
|
|
|
def prepare_relocation_to_locations
|
|
|
|
relocation = generic_prepare_relocation_to_locations
|
|
|
|
|
|
|
|
brewed_perl = runtime_dependencies&.any? { |dep| dep["full_name"] == "perl" && dep["declared_directly"] }
|
2021-05-23 11:05:17 +01:00
|
|
|
perl_path = if brewed_perl || name == "perl"
|
2021-04-29 17:52:10 +01:00
|
|
|
"#{HOMEBREW_PREFIX}/opt/perl/bin/perl"
|
2023-03-14 22:48:15 -07:00
|
|
|
elsif tab.built_on.present?
|
|
|
|
perl_path = "/usr/bin/perl#{tab.built_on["preferred_perl"]}"
|
2021-05-31 16:29:23 +01:00
|
|
|
|
|
|
|
# For `:all` bottles, we could have built this bottle with a Perl we don't have.
|
|
|
|
# Such bottles typically don't have strict version requirements.
|
|
|
|
perl_path = "/usr/bin/perl#{MacOS.preferred_perl_version}" unless File.exist?(perl_path)
|
|
|
|
|
|
|
|
perl_path
|
2021-04-29 17:52:10 +01:00
|
|
|
else
|
2021-05-31 16:29:23 +01:00
|
|
|
"/usr/bin/perl#{MacOS.preferred_perl_version}"
|
2021-04-29 17:52:10 +01:00
|
|
|
end
|
|
|
|
relocation.add_replacement_pair(:perl, PERL_PLACEHOLDER, perl_path)
|
|
|
|
|
2021-10-27 18:25:59 +08:00
|
|
|
if (openjdk = openjdk_dep_name_if_applicable)
|
2021-10-27 15:01:17 +08:00
|
|
|
openjdk_path = HOMEBREW_PREFIX/"opt"/openjdk/"libexec/openjdk.jdk/Contents/Home"
|
|
|
|
relocation.add_replacement_pair(:java, JAVA_PLACEHOLDER, openjdk_path.to_s)
|
|
|
|
end
|
|
|
|
|
2021-04-29 17:52:10 +01:00
|
|
|
relocation
|
|
|
|
end
|
|
|
|
|
2017-04-02 09:04:49 -07:00
|
|
|
def recursive_fgrep_args
|
|
|
|
# Don't recurse into symlinks; the man page says this is the default, but
|
|
|
|
# it's wrong. -O is a BSD-grep-only option.
|
|
|
|
"-lrO"
|
|
|
|
end
|
2022-02-16 21:30:04 -08:00
|
|
|
|
|
|
|
def egrep_args
|
|
|
|
grep_bin = "egrep"
|
2022-03-05 19:24:27 -08:00
|
|
|
grep_args = "--files-with-matches"
|
2022-02-16 21:30:04 -08:00
|
|
|
[grep_bin, grep_args]
|
|
|
|
end
|
2023-05-22 17:18:43 +08:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def rooted_in_build_directory?(filename)
|
|
|
|
# CMake normalises `/private/tmp` to `/tmp`.
|
|
|
|
# https://gitlab.kitware.com/cmake/cmake/-/issues/23251
|
|
|
|
return true if HOMEBREW_TEMP.to_s == "/private/tmp" && filename.start_with?("/tmp")
|
|
|
|
|
|
|
|
filename.start_with?(HOMEBREW_TEMP.to_s) || filename.start_with?(HOMEBREW_TEMP.realpath.to_s)
|
|
|
|
end
|
2016-07-09 13:52:05 +01:00
|
|
|
end
|