diff --git a/Library/Homebrew/extend/ENV/super.rb b/Library/Homebrew/extend/ENV/super.rb index f46d88744a..977c30fd45 100644 --- a/Library/Homebrew/extend/ENV/super.rb +++ b/Library/Homebrew/extend/ENV/super.rb @@ -105,6 +105,8 @@ module Superenv # have runtime detection of CPU features. # w - Pass -no_weak_imports to the linker # D - Generate debugging information + # f - Pass `-no_fixup_chains` to `ld` whenever it + # is invoked with `-undefined dynamic_lookup` # # These flags will also be present: # a - apply fix for apr-1-config path diff --git a/Library/Homebrew/extend/os/mac/extend/ENV/shared.rb b/Library/Homebrew/extend/os/mac/extend/ENV/shared.rb index e2d47c2e76..c0b490425b 100644 --- a/Library/Homebrew/extend/os/mac/extend/ENV/shared.rb +++ b/Library/Homebrew/extend/os/mac/extend/ENV/shared.rb @@ -23,4 +23,12 @@ module SharedEnvExtension true end + + 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" + + true + end end diff --git a/Library/Homebrew/extend/os/mac/extend/ENV/std.rb b/Library/Homebrew/extend/os/mac/extend/ENV/std.rb index d17b35c64d..c4df6cc552 100644 --- a/Library/Homebrew/extend/os/mac/extend/ENV/std.rb +++ b/Library/Homebrew/extend/os/mac/extend/ENV/std.rb @@ -101,4 +101,8 @@ module Stdenv def no_weak_imports append "LDFLAGS", "-Wl,-no_weak_imports" if no_weak_imports_support? end + + def no_fixup_chains + append "LDFLAGS", "-Wl,-no_fixup_chains" if no_fixup_chains_support? + 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 e141f559dd..c67ff0bbd9 100644 --- a/Library/Homebrew/extend/os/mac/extend/ENV/super.rb +++ b/Library/Homebrew/extend/os/mac/extend/ENV/super.rb @@ -136,4 +136,11 @@ module Superenv def no_weak_imports append_to_cccfg "w" if no_weak_imports_support? 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 90b7f0e318..020ee5642b 100755 --- a/Library/Homebrew/shims/super/cc +++ b/Library/Homebrew/shims/super/cc @@ -318,9 +318,11 @@ class Cmd when :ld args << "-headerpad_max_install_names" args << "-no_weak_imports" if no_weak_imports? + args << "-no_fixup_chains" if no_fixup_chains? when :ccld, :cxxld args << "-Wl,-headerpad_max_install_names" args << "-Wl,-no_weak_imports" if no_weak_imports? + args << "-Wl,-no_fixup_chains" if no_fixup_chains? end args end @@ -416,6 +418,18 @@ class Cmd config.include?("D") end + def no_fixup_chains? + return false unless config.include?("f") + return true if @args.include?("-Wl,-undefined,dynamic_lookup") + + args_consecutive_pairs = @args.each_cons(2) + return true if args_consecutive_pairs.include?(["-undefined", "dynamic_lookup"]) + return true if args_consecutive_pairs.include?(["-Wl,-undefined", "-Wl,dynamic_lookup"]) + + # The next flag would produce an error, but we fix it in `refurbish_arg`. + @args.include?("-undefineddynamic_lookup") + end + def canonical_path(path) path = Pathname.new(path) path = path.realpath if path.exist?