diff --git a/Library/Homebrew/extend/os/linux/install.rb b/Library/Homebrew/extend/os/linux/install.rb index b4f9cfdc8e..0ea7690616 100644 --- a/Library/Homebrew/extend/os/linux/install.rb +++ b/Library/Homebrew/extend/os/linux/install.rb @@ -38,13 +38,13 @@ module Homebrew def perform_preinstall_checks(all_fatal: false, cc: nil) generic_perform_preinstall_checks(all_fatal: all_fatal, cc: cc) symlink_ld_so - symlink_gcc_libs + setup_preferred_gcc_libs end def global_post_install generic_global_post_install symlink_ld_so - symlink_gcc_libs + setup_preferred_gcc_libs end def check_cpu @@ -77,23 +77,53 @@ module Homebrew end private_class_method :symlink_ld_so - def symlink_gcc_libs + def setup_preferred_gcc_libs gcc_opt_prefix = HOMEBREW_PREFIX/"opt/#{OS::LINUX_PREFERRED_GCC_RUNTIME_FORMULA}" + glibc_installed = (HOMEBREW_PREFIX/"opt/glibc/lib/ld-linux-x86-64.so.2").readable? + + return unless gcc_opt_prefix.readable? + + if glibc_installed + ld_so_conf_d = HOMEBREW_PREFIX/"etc/ld.so.conf.d" + unless ld_so_conf_d.exist? + ld_so_conf_d.mkpath + FileUtils.chmod "go-w", ld_so_conf_d + end + + # Add gcc to ld search paths + ld_gcc_conf = ld_so_conf_d/"50-homebrew-preferred-gcc.conf" + unless ld_gcc_conf.exist? + ld_gcc_conf.atomic_write <<~EOS + # This file is generated by Homebrew. Do not modify. + #{gcc_opt_prefix}/lib/gcc/#{PREFERRED_GCC_RUNTIME_VERSION} + EOS + FileUtils.chmod "u=rw,go-wx", ld_gcc_conf + + FileUtils.rm_f HOMEBREW_PREFIX/"etc/ld.so.cache" + system HOMEBREW_PREFIX/"opt/glibc/sbin/ldconfig" + end + else + odie "#{HOMEBREW_PREFIX}/lib does not exist!" unless (HOMEBREW_PREFIX/"lib").readable? + end GCC_RUNTIME_LIBS.each do |library| - gcc_library = gcc_opt_prefix/"lib/gcc/#{PREFERRED_GCC_RUNTIME_VERSION}/#{library}" gcc_library_symlink = HOMEBREW_PREFIX/"lib/#{library}" - # Skip if the link target doesn't exist. - next unless gcc_library.readable? - # Also skip if the symlink already exists. - next if gcc_library_symlink.readable? && (gcc_library_symlink.readlink == gcc_library) + if glibc_installed + # Remove legacy symlinks + FileUtils.rm gcc_library_symlink if gcc_library_symlink.symlink? + else + gcc_library = gcc_opt_prefix/"lib/gcc/#{PREFERRED_GCC_RUNTIME_VERSION}/#{library}" + # Skip if the link target doesn't exist. + next unless gcc_library.readable? - odie "#{HOMEBREW_PREFIX}/lib does not exist!" unless (HOMEBREW_PREFIX/"lib").readable? + # Also skip if the symlink already exists. + next if gcc_library_symlink.readable? && (gcc_library_symlink.readlink == gcc_library) - FileUtils.ln_sf gcc_library, gcc_library_symlink + FileUtils.ln_sf gcc_library, gcc_library_symlink + end end end - private_class_method :symlink_gcc_libs + private_class_method :setup_preferred_gcc_libs end end