From c4fe6e76171bc13c5afe2daccd9c59d209783d15 Mon Sep 17 00:00:00 2001 From: Carlo Cabrera <30379873+carlocab@users.noreply.github.com> Date: Sun, 5 Mar 2023 16:55:00 +0800 Subject: [PATCH] Incoroporate feedback from code review - check the version of `/usr/bin/ld` for support of `-no_fixup_chains` - check for usage of the `-fuse-ld` flag, since this flag is only supported by Apple ld64 Also, call `no_fixup_chains` when setting up the build environment. --- .../Homebrew/extend/os/mac/extend/ENV/shared.rb | 9 ++++++--- .../Homebrew/extend/os/mac/extend/ENV/super.rb | 8 +++++--- Library/Homebrew/shims/super/cc | 16 +++++++++++++++- 3 files changed, 26 insertions(+), 7 deletions(-) diff --git a/Library/Homebrew/extend/os/mac/extend/ENV/shared.rb b/Library/Homebrew/extend/os/mac/extend/ENV/shared.rb index c0b490425b..6e051bc5b5 100644 --- a/Library/Homebrew/extend/os/mac/extend/ENV/shared.rb +++ b/Library/Homebrew/extend/os/mac/extend/ENV/shared.rb @@ -26,9 +26,12 @@ module SharedEnvExtension sig { returns(T::Boolean) } def no_fixup_chains_support? - return false if !MacOS::CLT.version.null? && MacOS::CLT.version < "13.0" - return false if !MacOS::Xcode.version.null? && MacOS::Xcode.version < "13.0" + ld_v = Utils.safe_popen_read("/usr/bin/ld", "-v", err: :out).lines.first.chomp + ld_version = Version.parse(ld_v[/\d+(\.\d+)*$/]) - true + # This is supported starting Xcode 13, which ships ld64-711. + # https://developer.apple.com/documentation/xcode-release-notes/xcode-13-release-notes + # https://en.wikipedia.org/wiki/Xcode#Xcode_11.0_-_14.x_(since_SwiftUI_framework)_2 + ld_version >= 711 end end diff --git a/Library/Homebrew/extend/os/mac/extend/ENV/super.rb b/Library/Homebrew/extend/os/mac/extend/ENV/super.rb index c67ff0bbd9..b736f4ca3e 100644 --- a/Library/Homebrew/extend/os/mac/extend/ENV/super.rb +++ b/Library/Homebrew/extend/os/mac/extend/ENV/super.rb @@ -131,6 +131,11 @@ module Superenv # Notably, Xcode 10.2 fixes issues where ZERO_AR_DATE affected file mtimes. # Xcode 11.0 contains fixes for lldb reading things built with ZERO_AR_DATE. self["ZERO_AR_DATE"] = "1" if MacOS::Xcode.version >= "11.0" || MacOS::CLT.version >= "11.0" + + # Pass `-no_fixup_chains` whenever the linker is invoked with `-undefined dynamic_lookup`. + # See: https://github.com/python/cpython/issues/97524 + # https://github.com/pybind/pybind11/pull/4301 + no_fixup_chains end def no_weak_imports @@ -138,9 +143,6 @@ module Superenv end def no_fixup_chains - # Pass `-no_fixup_chains` whenever the linker is invoked with `-undefined dynamic_lookup`. - # See: https://github.com/python/cpython/issues/97524 - # https://github.com/pybind/pybind11/pull/4301 append_to_cccfg "f" if no_fixup_chains_support? end end diff --git a/Library/Homebrew/shims/super/cc b/Library/Homebrew/shims/super/cc index 020ee5642b..3531f6d360 100755 --- a/Library/Homebrew/shims/super/cc +++ b/Library/Homebrew/shims/super/cc @@ -221,7 +221,7 @@ class Cmd args << "-Wl,-undefined,dynamic_lookup" when /^-isysroot=/, /^--sysroot=/ if mac? - sdk = arg.split("=")[1..-1].join("=") + sdk = arg.split("=", 2).last # We set the sysroot for macOS SDKs args << arg unless sdk.downcase.include? "osx" else @@ -420,6 +420,7 @@ class Cmd def no_fixup_chains? return false unless config.include?("f") + return false unless calls_ld? return true if @args.include?("-Wl,-undefined,dynamic_lookup") args_consecutive_pairs = @args.each_cons(2) @@ -430,6 +431,19 @@ class Cmd @args.include?("-undefineddynamic_lookup") end + def calls_ld? + return true if mode == :ld + return false unless [:ccld, :cxxld].include?(mode) + + fuse_ld_flags = @args.find_all { |arg| arg.match?(/^-fuse-ld=/) } + return true if fuse_ld_flags.empty? + + fuse_ld_flag = fuse_ld_flags.last.strip + fuse_ld_arg = fuse_ld_flag.split("=", 2).last + + (fuse_ld_arg == "ld") || fuse_ld_arg.end_with("/usr/bin/ld") + end + def canonical_path(path) path = Pathname.new(path) path = path.realpath if path.exist?