Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cc-wrapper: fix -Bprefix to not confuse lib/libc++.so and bin/c++ #317224

Merged
merged 1 commit into from
Jul 29, 2024

Conversation

ju1m
Copy link
Contributor

@ju1m ju1m commented Jun 4, 2024

Before this commit, pkgs/build-support/cc-wrapper/add-flags.sh was using -B@out@/bin instead of -B@bintools@/bin to force cc to use ld-wrapper.sh when calling ld. That was confusing cc when asked to print the location of a library precisely named c++ because -B prefixes are also used by cc to find libraries, see https://gcc.gnu.org/onlinedocs/gcc/Directory-Options.html#index-B

Indeed, instead of having cc --print-file-name c++ failing to find a c++ library and just returning the given c++ string to let a linker resolve it thereafter, it was finding that @out@/bin/c++ executable, mistaking it for a library and returning its absolute path, forcing the linker to load an executable as a library.

Users should likely ask for libc++.so instead of c++, but that's not what's done,
For instance this confusion was breaking with GHC on Darwin:
https://gitlab.haskell.org/ghc/ghc/-/issues/23138#note_567034
It's not clear to me why only the Darwin port of GHC was affected since this -B was also injected in the Linux toolchain.
And I don't have a Darwin host to test whether this fix is enough to solve the GHC bug mentioned.
But the result of cc --print-file-name c++ is clearly wrong to motivate this fix:

Before this commit:

$ nix run -f . stdenv.cc -- --print-file-name c++
/nix/store/9bv7dcvmfcjnmg5mnqwqlq2wxfn8d7yi-gcc-wrapper-13.2.0/bin/c++

After this commit:

$ nix run -f . stdenv.cc -- --print-file-name c++
c++

Description of changes

Things done

  • Built on platform(s)
    • x86_64-linux
    • aarch64-linux
    • x86_64-darwin
    • aarch64-darwin
  • For non-Linux: Is sandboxing enabled in nix.conf? (See Nix manual)
    • sandbox = relaxed
    • sandbox = true
  • Tested, as applicable:
  • Tested compilation of all packages that depend on this change using nix-shell -p nixpkgs-review --run "nixpkgs-review rev HEAD". Note: all changes have to be committed, also see nixpkgs-review usage
  • Tested basic functionality of all binary files (usually in ./result/bin/)
  • 24.11 Release Notes (or backporting 23.11 and 24.05 Release notes)
    • (Package updates) Added a release notes entry if the change is major or breaking
    • (Module updates) Added a release notes entry if the change is significant
    • (Module addition) Added a release notes entry if adding a new NixOS module
  • Fits CONTRIBUTING.md.

Add a 👍 reaction to pull requests you find important.

@414owen
Copy link
Contributor

414owen commented Jun 16, 2024

@ju1m This fixed all our haskell builds on Arm64 darwin, which were trying to link in clang++...

@doyougnu
Copy link
Contributor

doyougnu commented Jul 9, 2024

ping. What is this PR waiting on and can it move forward?

I would like this to be merged so that we can drop the patch in haskell.nix. I recently duplicated this work (https://gitlab.haskell.org/ghc/ghc/-/merge_requests/13005) because I was unaware that this had been upstreamed. Similarly, landing this PR would close https://gitlab.haskell.org/ghc/ghc/-/issues/23138. cc @Ericson2314

@nixos-discourse
Copy link

This pull request has been mentioned on NixOS Discourse. There might be relevant details there:

https://discourse.nixos.org/t/prs-already-reviewed/2617/1812

@414owen
Copy link
Contributor

414owen commented Jul 25, 2024

@Ericson2314 are you able to help out with a review here, or do you know if any of the other stdenv.cc maintainers are more Darwin-focused?

@reckenrode
Copy link
Contributor

I found https://gitlab.haskell.org/ghc/ghc/-/issues/23138 while investigating the failed built of fmt on staging-next #328673 due to ld64 upgrade in #307880, which lead me to this PR.

@Ericson2314 are you able to help out with a review here, or do you know if any of the other stdenv.cc maintainers are more Darwin-focused?

I’m probably the one you want. This seems fine. I did a hacky test modifying add-flags.sh in the store to smoke test that it worked (and it did). I reverted that, and I’m now doing a bootstrap build with your commit cherry-picked onto staging-next.

I don’t know if any other cc-wrapper maintainers want to weigh in, but once I confirm it builds, I’ll approve and merge.

Before this commit, `pkgs/build-support/cc-wrapper/add-flags.sh`
was using `-B@out@/bin` instead of `-B@bintools@/bin`
to force `cc` to use `ld-wrapper.sh` when calling `ld`.
That was confusing `cc` when asked to print
the location of a library precisely named `c++`
because `-B` prefixes are also used by `cc` to find libraries,
see https://gcc.gnu.org/onlinedocs/gcc/Directory-Options.html#index-B

Indeed, instead of having `cc --print-file-name c++`
failing to found a `c++` library and just returning the given `c++` string
to let a linker resolve it thereafter,
it was finding that `@out@/bin/c++` executable,
mistaking it for a library and returning its absolute path,
forcing the linker to load an executable as a library.

Before this commit:

```console
$ nix run -f . stdenv.cc -- --print-file-name c++
/nix/store/9bv7dcvmfcjnmg5mnqwqlq2wxfn8d7yi-gcc-wrapper-13.2.0/bin/c++
```

After this commit:

```console
$ nix run -f . stdenv.cc -- --print-file-name c++
c++
```

Fixes https://gitlab.haskell.org/ghc/ghc/-/issues/23138#note_567034
where this behavior was breaking GHC on Darwin.

[Confirmed by @414owen](NixOS#317224 (comment)):

> This fixed all our haskell builds on Arm64 darwin, which were trying
> to link in clang++...
@ju1m
Copy link
Contributor Author

ju1m commented Jul 28, 2024

  • Rebase to fix conflict with 9f589ea

Copy link
Contributor

@reckenrode reckenrode left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm. I successfully bootstrapped and built haskellPackages.fmt on aarch64-darwin, x86_64-darwin, and x86_64-linux.

@reckenrode
Copy link
Contributor

Once checks are green, this can be merged.

@reckenrode reckenrode merged commit 9a4c598 into NixOS:staging Jul 29, 2024
23 checks passed
@ju1m ju1m deleted the NIX_CFLAGS_COMPILE branch July 29, 2024 18:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants