tweag / rules_nixpkgs

Rules for importing Nixpkgs packages into Bazel.
Apache License 2.0
288 stars 80 forks source link

Clang compiler is misidentified on macOS #190

Closed YorikSar closed 2 years ago

YorikSar commented 2 years ago

Describe the bug On macOS toolchain created by nixpkgs_cc_configure is misidentified as not being Clang.

To Reproduce

$ cd examples/toolchains/cc
$ bazel build --config=nix @nixpkgs_config_cc_info//:CC_TOOLCHAIN_INFO
...
$ cat ./bazel-cc/external/nixpkgs_config_cc_info/CC_TOOLCHAIN_INFO | grep IS_CLANG
IS_CLANG:False

Expected behavior On Darwin this toolchain uses Clang, so IS_CLANG should be True.

Environment

Additional context It looks like the script that generates CC_TOOLCHAIN_INFO tries running .../bin/cc -v which fails because of the wrapper created by rules_nixpkgs:

% /nix/store/1q1yvxiirik8azmllr27h5wkf1320qhb-bazel-nixpkgs-cc/bin/cc -v
clang version 7.1.0 (tags/RELEASE_710/final)
Target: x86_64-apple-darwin
Thread model: posix
InstalledDir: /nix/store/zfh3npfhfjjgwi0dqpriklip5k15ppmj-clang-7.1.0/bin
 "/nix/store/gsfxxazc51sx6vjdrz6cq8s19x7h5mwh-clang-wrapper-7.1.0/bin/ld" -demangle -lto_library /nix/store/zfh3npfhfjjgwi0dqpriklip5k15ppmj-clang-7.1.0/lib/libLTO.dylib -dynamic -arch x86_64 -macosx_version_min 10.12.0 -o a.out -L/nix/store/5ygwml3yjcdvv5y35c2p9z4qavknfs0i-libcxx-7.1.0/lib -L/nix/store/a1sp2lg1lc67kwgcq5iw6ar2wdjfqqcc-objc4-709.1/lib -L/nix/store/1vp7fr7dm20khrz9zhzbwq8h9k37phhc-Libsystem-1238.60.2/lib -L/nix/store/66crk4qirnyz08zw91nhlz4x8lrxprc0-clang-7.1.0-lib/lib -L/nix/store/5ygwml3yjcdvv5y35c2p9z4qavknfs0i-libcxx-7.1.0/lib -no_uuid -arch x86_64 -lSystem /nix/store/gsfxxazc51sx6vjdrz6cq8s19x7h5mwh-clang-wrapper-7.1.0/resource-root/lib/darwin/libclang_rt.osx.a -F/nix/store/nvn3ldqg86k6x05pbq6yblg39p469rfd-apple-framework-CoreFoundation/Library/Frameworks -F/nix/store/8qc9kz9h8paijgxnhap7gslvvsyvry0f-apple-framework-CoreServices/Library/Frameworks -F/nix/store/f64r0ps4phxl3cnar09gi9y4pp1q5n7h-apple-framework-Security/Library/Frameworks -F/nix/store/pk9s46a3pi32h6x99k6ipyj8qy5gvrzj-apple-framework-Foundation/Library/Frameworks
Undefined symbols for architecture x86_64:
  "_main", referenced from:
     implicit entry/start for main executable
ld: symbol(s) not found for architecture x86_64
clang-7: error: linker command failed with exit code 1 (use -v to see invocation)

But if we try running it directly from the wrapper generated by nixpkgs, it works fine:

 % cat /nix/store/1q1yvxiirik8azmllr27h5wkf1320qhb-bazel-nixpkgs-cc/bin/cc
#! /nix/store/r42x7q316gznkm5y9b0cl4564g174zyl-bash-5.1-p8/bin/bash -e
exec "/nix/store/gsfxxazc51sx6vjdrz6cq8s19x7h5mwh-clang-wrapper-7.1.0/bin/cc"  -Wno-unused-command-line-argument   -isystem /nix/store/5ygwml3yjcdvv5y35c2p9z4qavknfs0i-libcxx-7.1.0/include/c++/v1   -F/nix/store/nvn3ldqg86k6x05pbq6yblg39p469rfd-apple-framework-CoreFoundation/Library/Frameworks   -F/nix/store/8qc9kz9h8paijgxnhap7gslvvsyvry0f-apple-framework-CoreServices/Library/Frameworks   -F/nix/store/f64r0ps4phxl3cnar09gi9y4pp1q5n7h-apple-framework-Security/Library/Frameworks   -F/nix/store/pk9s46a3pi32h6x99k6ipyj8qy5gvrzj-apple-framework-Foundation/Library/Frameworks   -L/nix/store/5ygwml3yjcdvv5y35c2p9z4qavknfs0i-libcxx-7.1.0/lib   -L/nix/store/a1sp2lg1lc67kwgcq5iw6ar2wdjfqqcc-objc4-709.1/lib "$@"
 % /nix/store/gsfxxazc51sx6vjdrz6cq8s19x7h5mwh-clang-wrapper-7.1.0/bin/cc -v
clang version 7.1.0 (tags/RELEASE_710/final)
Target: x86_64-apple-darwin21.3.0
Thread model: posix
InstalledDir: /nix/store/zfh3npfhfjjgwi0dqpriklip5k15ppmj-clang-7.1.0/bin

Since the decision is made based on the return value of the ${cc}/bin/cc -v 2>&1 | grep -q clang pipeline, this failure leads to False.

aherrmann commented 2 years ago
% /nix/store/1q1yvxiirik8azmllr27h5wkf1320qhb-bazel-nixpkgs-cc/bin/cc -v
clang version 7.1.0 (tags/RELEASE_710/final)
Target: x86_64-apple-darwin
Thread model: posix
InstalledDir: /nix/store/zfh3npfhfjjgwi0dqpriklip5k15ppmj-clang-7.1.0/bin
 "/nix/store/gsfxxazc51sx6vjdrz6cq8s19x7h5mwh-clang-wrapper-7.1.0/bin/ld" -demangle -lto_library /nix/store/zfh3npfhfjjgwi0dqpriklip5k15ppmj-clang-7.1.0/lib/libLTO.dylib -dynamic -arch x86_64 -macosx_version_min 10.12.0 -o a.out -L/nix/store/5ygwml3yjcdvv5y35c2p9z4qavknfs0i-libcxx-7.1.0/lib -L/nix/store/a1sp2lg1lc67kwgcq5iw6ar2wdjfqqcc-objc4-709.1/lib -L/nix/store/1vp7fr7dm20khrz9zhzbwq8h9k37phhc-Libsystem-1238.60.2/lib -L/nix/store/66crk4qirnyz08zw91nhlz4x8lrxprc0-clang-7.1.0-lib/lib -L/nix/store/5ygwml3yjcdvv5y35c2p9z4qavknfs0i-libcxx-7.1.0/lib -no_uuid -arch x86_64 -lSystem /nix/store/gsfxxazc51sx6vjdrz6cq8s19x7h5mwh-clang-wrapper-7.1.0/resource-root/lib/darwin/libclang_rt.osx.a -F/nix/store/nvn3ldqg86k6x05pbq6yblg39p469rfd-apple-framework-CoreFoundation/Library/Frameworks -F/nix/store/8qc9kz9h8paijgxnhap7gslvvsyvry0f-apple-framework-CoreServices/Library/Frameworks -F/nix/store/f64r0ps4phxl3cnar09gi9y4pp1q5n7h-apple-framework-Security/Library/Frameworks -F/nix/store/pk9s46a3pi32h6x99k6ipyj8qy5gvrzj-apple-framework-Foundation/Library/Frameworks
Undefined symbols for architecture x86_64:
  "_main", referenced from:
     implicit entry/start for main executable
ld: symbol(s) not found for architecture x86_64
clang-7: error: linker command failed with exit code 1 (use -v to see invocation)

I recognize this error, I've seen this in the context of rules_haskell on M1. Defining the cc toolchain like this avoided the issue in that context.

avdv commented 2 years ago

This was fixed by https://github.com/tweag/rules_nixpkgs/pull/216