Open madsmtm opened 1 week ago
See also discussion in https://github.com/Homebrew/homebrew-core/pull/196094#discussion_r1835758617
Works for me:
❯ xcrun -sdk macosx15.1 /opt/homebrew/opt/llvm@19/bin/clang foo.m; echo $?
0
But I'm guessing the important bit that's left out of your reproduction steps is that you must be testing this on macOS 14 (which I am not).
I think this is an upstream bug. You can reproduce this with Apple Clang as well:
❯ xcrun -sdk macosx15.1 clang --config=/opt/homebrew/etc/clang/arm64-apple-darwin23.cfg foo.m
Undefined symbols for architecture arm64:
"_os_unfair_lock_lock_with_flags", referenced from:
_main in foo-28462b.o
ld: symbol(s) not found for architecture arm64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
But I'm guessing the important bit that's left out of your reproduction steps is that you must be testing this on macOS 14 (which I am not).
Indeed, this was on a macOS 14.0 VM.
The problem can also be encountered if you try to cross-compile (assuming you have Xcode installed):
brew install llvm@18 # Different problem on llvm@19 because of #197278
echo """
#import <UIKit/UIKit.h>
int main(int argc, char *argv[]) {
return UIApplicationMain(argc, argv, nil, nil);
}
""" > bar.m
xcrun -sdk iphoneos /opt/homebrew/opt/llvm@18/bin/clang -target arm64-apple-ios -framework UIKit bar.m
I think this is an upstream bug
Dunno, I guess it depends on what the desired behaviour of setting both --sysroot
and -isysroot
is?
But it could be somewhat fixed by Homebrew by setting -isysroot
instead of --sysroot
. Or by creating trampoline binaries similar to those under /usr/bin/*
that set SDKROOT
if not set.
The problem can also be encountered if you try to cross-compile (assuming you have Xcode installed):
brew install llvm@18 # Different problem on llvm@19 because of #197278 echo """ #import <UIKit/UIKit.h> int main(int argc, char *argv[]) { return UIApplicationMain(argc, argv, nil, nil); } """ > bar.m xcrun -sdk iphoneos /opt/homebrew/opt/llvm@18/bin/clang -target arm64-apple-ios -framework UIKit bar.m
This makes me think that the issue is not related to #196094, then, because we set DEFAULT_SYSROOT
instead of using config files on llvm@18
:
But it could be somewhat fixed by Homebrew by setting
-isysroot
instead of--sysroot
.
Not convinced -- setting -isysroot
is exactly what will make clang
ignore SDKROOT
:
Does it work for you if you replace --sysroot=
with -isysroot
in the .cfg
files?
Or by creating trampoline binaries similar to those under
/usr/bin/*
that setSDKROOT
if not set.
Yea, we could maybe just make clang
a wrapper that does xcrun clang "$@"
. Not sure if it's the right behaviour if you actually want to target a non-Apple host, though. (But this was arguably already incorrect when we set DEFAULT_SYSROOT
.)
This makes me think that the issue is not related to #196094, then, because we set
DEFAULT_SYSROOT
instead of using config files onllvm@18
:
Indeed, this is not related to #196094, I only linked it for the context.
But it could be somewhat fixed by Homebrew by setting
-isysroot
instead of--sysroot
.Not convinced -- setting
-isysroot
is exactly what will makeclang
ignoreSDKROOT
Agree that it's not a good solution, since it would completely ignore xcrun
. But at least it would make it consistent, instead of now where we end up pulling in different headers when compiling vs. the libraries used when linking.
Or by creating trampoline binaries similar to those under
/usr/bin/*
that setSDKROOT
if not set.Yea, we could maybe just make
clang
a wrapper that doesxcrun clang "$@"
. Not sure if it's the right behaviour if you actually want to target a non-Apple host, though. (But this was arguably already incorrect when we setDEFAULT_SYSROOT
.)
Agreed that I'm not sure what the right behaviour is either 🤷.
Indeed, this is not related to #196094, I only linked it for the context.
Right -- in that case, this is definitely an upstream bug.
Can you reproduce this with llvm
if you wipe out all the config files in $(brew --prefix)/etc/clang
? (I don't have a macOS 14 VM on hand to test this properly with.)
I removed all the config files in $(brew --prefix)/etc/clang
, and xcrun -sdk macosx15.1 /opt/homebrew/opt/llvm@19/bin/clang foo.m
now compiles and links.
If you're debugging this, then the behaviour here can be reproduced with Apple Clang with:
clang --sysroot $(xcrun -sdk macosx14.5 --show-sdk-path) -isysroot $(xcrun -sdk macosx15.1 --show-sdk-path) foo.m
# Or
xcrun -sdk macosx15.1 clang --sysroot $(xcrun -sdk macosx14.5 --show-sdk-path) foo.m
# Or
SDKROOT=$(xcrun -sdk macosx15.1 --show-sdk-path) clang --sysroot $(xcrun -sdk macosx14.5 --show-sdk-path) foo.m
I.e. specify an old SDK for --sysroot
and a newer SDK for -isysroot
.
I think one workaround here is to pass --no-default-config
when invoking clang
. This only works -- if it does -- now that we use config files. It most likely would not work if we set DEFAULT_SYSROOT
.
You might also be able to do
mkdir -p ~/.config/clang
touch ~/.config/clang/$(clang --print-target-triple).cfg
to make
xcrun -sdk macosx15.1 /opt/homebrew/opt/llvm@19/bin/clang foo.m
just work.
Or, I guess the most obvious workaround: pass --sysroot=$(xcrun -sdk macosx15.1 --show-sdk-path)
when invoking clang
directly. This should override anything we've put in our configuration files.
I am indeed considering whether rustc
needs to pass both -isysroot
and --sysroot
, but that has other problems (we don't really want to do compiler detection, and I suspect not all compilers support both of these flags, and I'm undecided if rustc
wants to override what Homebrew is doing).
You could try passing --target
? I think after #197410 doing
/opt/homebrew/opt/llvm@19/bin/clang --target=arm64-apple-macosx15.1 foo.m
should just work for you, even on macOS 14. Though maybe you don't want to target macOS 15.1, and instead want to target something older but use the macosx15.1
SDK.
The behaviour comes from https://github.com/llvm/llvm-project/commit/8438464b417d219105434b5e30f6e6d764d093fc. Looking at the code that was from a time --sysroot
did override -isysroot
for header includes too.
This is no longer the case so I believe the code is now wrong and should probably be inverted. I think it makes sense to propose this upstream and apply a patch rather than try do weird workarounds here, particularly given the workarounds wouldn't even be consistent between llvm@18
and llvm@19
anyway.
Hard to pinpoint when the header includes logic changed but I suspect it's been a while, so this issue probably affects every LLVM version we ship.
Can confirm that this issue is present in llvm@12
too.
Do you want to work on a fix upstream? If not, then I can try to, but I get the feeling you're more experienced with that.
I'll send the patch -- @Bo98 already has two outstanding PRs waiting for a review upstream.
brew gist-logs <formula>
link ORbrew config
ANDbrew doctor
outputNote that the system is a clean virtual machine with a newly installed homebrew. The problem has also been reproduced here.
Verification
brew doctor
output saysYour system is ready to brew.
and am still able to reproduce my issue.brew update
and am still able to reproduce my issue.brew doctor
and that did not fix my problem.What were you trying to do (and why)?
It is generally expected that setting the
SDKROOT
environment variable (or the-isysroot
flag) when compiling with Clang on macOS selects a different SDK.xcrun
, for example, uses this to make it easy to select a different SDK when compiling.This is somewhat supported by the Clang provided by the
llvm
package, but the SDK thatllvm
was built with is always selected when linking instead of the value set byxcrun
/ inSDKROOT
/ with-isysroot
.This is likely to affect cross compilation (to iOS/tvOS/watchOS/visionOS). I'm investigating this because of https://github.com/rust-lang/rust/pull/131477 and https://github.com/rust-lang/cc-rs/issues/1278, since it is unclear how downstream users such as
rustc
should pass a different SDK root.The following contrived example program fails to link with the macOS 15.1 SDK for this reason:
What happened (include all command output)?
Linking fails with:
What did you expect to happen?
The macOS 15.1 SDK is used, also when linking, and compilation and linking succeeds. OR An error at compile-time because the macOS 14.0 SDK is consistently used.
Step-by-step reproduction instructions (by running
brew
commands)Install
llvm
package (any version):brew install llvm@19
Install Xcode 15.4 and Xcode 16.1 (or equivalent Command Line Tools), so that the macOS 14.0 and macOS 15.1 SDKs are available.
Put the example program above in
foo.m
.Try to compile with the macOS 14.0 SDK (this fails as expected, because the function isn't available in the old SDK):
Compile with Xcode Clang and macOS 15.1 SDK (this works):
Compile with Clang provided by
llvm
package and macOS 15.1 SDK (this fails linking):Use
-v
to see that Clang always passes the same value told
's-syslibroot
.