Skip to content

cc-wrapper: fix on darwin - #11798

Closed
acowley wants to merge 2 commits into
NixOS:masterfrom
acowley:readlink
Closed

acowley wants to merge 2 commits into
NixOS:masterfrom
acowley:readlink

Conversation

@acowley

@acowley acowley commented Dec 17, 2015

Copy link
Copy Markdown
Contributor

The ld-wrapper.sh script calls readlink in some circumstances. We need
to ensure that this is the readlink from the coreutils package so
that flag support is as expected.

Without doing this, the following happens with a trivial main.c:

nix-env -f "<nixpkgs>" -iA pkgs.clang
$ clang main.c -L /nix/../nix/store/2ankvagznq062x1gifpxwkk7fp3xwy63-xnu-2422.115.4/Library -o a.out
readlink: illegal option -- f
usage: readlink [-n] [file ...]

The key element is the .. in the path supplied to the linker via a
-L flag. With this patch, the above invocation works correctly on
darwin, whose native /usr/bin/readlink does not support the -f flag.

Fixes #6447

@mention-bot

Copy link
Copy Markdown

By analyzing the blame information on this pull request, we identified @edolstra, @gridaphobe and @copumpkin to be potential reviewers

@copumpkin

Copy link
Copy Markdown
Member

Ah, so the use case here is for interactive invocations of clang/cc/ld, rather than via Nix scripts, right?

@copumpkin copumpkin added 6.topic: darwin Running or building packages on Darwin 1.severity: mass-darwin-rebuild and removed 1.severity: mass-darwin-rebuild 6.topic: darwin Running or building packages on Darwin labels Dec 17, 2015
@acowley

acowley commented Dec 17, 2015

Copy link
Copy Markdown
Contributor Author

Yes, that is correct!

@copumpkin

Copy link
Copy Markdown
Member

Given that it's a mass rebuild, this should probably go into staging rather than master.

@acowley

acowley commented Dec 17, 2015

Copy link
Copy Markdown
Contributor Author

Ah, okay. Can you move it or shall I?

@copumpkin

Copy link
Copy Markdown
Member

I don't think GitHub allows one to repoint a PR. Someone could just merge the patch manually to staging, but let's get some feedback first. As I mentioned in the linked issue, this will resolve the particular instance but I'm wondering if there are more robust ways to do this more broadly that might apply here too.

@acowley

acowley commented Dec 17, 2015

Copy link
Copy Markdown
Contributor Author

Yes, what you said sounded reasonable. I figured that since nothing has happened for several months, fixing this instance now is expedient, and perhaps the lack of broader outcry indicates the problem doesn't arise as frequently as it might. It's a pretty substantial nuisance, as it makes development tools installed via nix much less useful than they otherwise ought to be.

@vcunat

vcunat commented Dec 18, 2015

Copy link
Copy Markdown
Member

There were multiple impure references in cc-wrapper scripts. I wonder if it's better to hardcode the paths directly 093f58b or just specify PATH='@coreutils@/bin' at the beginning of a script. (IIRC that shouldn't affect $PATH value exported by the script, but I'm not certain.)

@copumpkin

Copy link
Copy Markdown
Member

Setting PATH explicitly (and not including its previous value) at the top of our scripts is actually pretty appealing. Advantages (if we make it a policy):

  • We don't accidentally inherit stuff from the user's shell
  • We don't have to adorn every invocation of a utility with its full path
  • We get a loud failure if we forget a dependency, because the utility won't be found
  • We could in theory verify semi-automatically that our scripts follow the policy (by looking for PATH= in the first few lines, and ensuring that PATH isn't referenced on the RHS)

👍 from me on that approach

@acowley

acowley commented Dec 18, 2015

Copy link
Copy Markdown
Contributor Author

That sounds like the right approach.

@acowley

acowley commented Dec 25, 2015

Copy link
Copy Markdown
Contributor Author

This is actually a bit trickier than I anticipated. First, the straightforward idea of just using, e.g., @coreutils@ in the shell scripts to work with substituteAll doesn't work as cc-wrapper has a nativeTools parameter that can cause that variable to not be defined. So I tweaked that a bit as you can see in the new patches, but it still doesn't work; here's the error I get after a while

building path(s) ‘/nix/store/i8lvwn55nbynswx6wdsvw4bvj8aw0scg-stdenv-darwin’
output ‘/nix/store/i8lvwn55nbynswx6wdsvw4bvj8aw0scg-stdenv-darwin’ is not allowed to refer to path ‘/nix/store/7l8l3sp3wk0idx0191qhn4hvdvx0wznq-bootstrap-tools’
error: build of ‘/nix/store/fxsahfv1352licgp95mhwgip109cvj27-stdenv-darwin.drv’ failed

Perhaps one of the more experienced darwin folks (e.g. @copumpkin @shlevy @gridaphobe @pikajude ) might suggest a fix. I can work at it, but it's a slog.

@shlevy

shlevy commented Dec 26, 2015

Copy link
Copy Markdown
Member

The problem is that the final version of cc-wrapper is referencing the bootstrap coreutils, not the final version. One potential fix would be to rewrap the final clang after building the final coreutils.

But is there any reason we need to use identical wrappers for builds and for users? We already have bashInteractive for example.

@acowley

acowley commented Dec 26, 2015

Copy link
Copy Markdown
Contributor Author

I'm not familiar enough with the bring-up of the whole environment to make architectural changes. I had hoped that the nativeTools flag would sufficiently twiddle the configuration between bootstrapping and final builds to avoid issues like this. It seems like adding a ccInteractive would be a fairly intrusive change if it would be necessary to refer to it when building the stdenv.

@shlevy

shlevy commented Dec 26, 2015

Copy link
Copy Markdown
Member

The whole point is that it wouldn't be necessary to refer to it when building stdenv, right? Since cc-wrapper works fine in nix builds, it only fails for the user?

@acowley

acowley commented Dec 26, 2015

Copy link
Copy Markdown
Contributor Author

Oh, I'll have to re-run the test I guess. The build error made me think my attempted rebuild of things broke while building stdenv. These builds take a long time to get back to a development environment I can work with, so I usually kick off a downstream build and check back in a day to read the error messages. I'll try again some other time and only build one thing at a time as that error message is apparently pretty confusing.

@shlevy

shlevy commented Dec 26, 2015

Copy link
Copy Markdown
Member

You'll have to revert your change. The build error is because the final stdenv contains a reference to the bootstrap tools. If you have a separate ccInteractive, it will refer to the newly-built coreutils, not the bootstrap coreutils.

@acowley

acowley commented Dec 26, 2015

Copy link
Copy Markdown
Contributor Author

The linked issue for this PR was the problem being exposed in GHC through its use of the linker. So, if I'm understanding the suggestion, it would be to leave cc-wrapper as it was, but change all downstream dev tools to use a different ccInteractive package?

@shlevy

shlevy commented Dec 26, 2015

Copy link
Copy Markdown
Member

Ah, yeah. I'm not sure what the best solution is there really.

Hmm, I wonder if the better solution is to include coreutils in nix-env environments by default?

@acowley

acowley commented Dec 26, 2015

Copy link
Copy Markdown
Contributor Author

Part of my question that I perhaps didn't make suitably clear is why I'm running into this problem with this change: I don't understand what this change does that introduces the illegal stdenv reference. Perhaps that could suggest an easy way to adjust this approach of explicitly setting PATH as it seems like rather a good idea in general.

It seems like the right fix should be adjacent to the current state of the PR given that my original attempt of just using an explicit @coreutils@ path in the readlink call addressed the motivating symptom without any noticeable adverse consequences.

@shlevy

shlevy commented Dec 26, 2015

Copy link
Copy Markdown
Member

Hmm, are you sure that your original attempt worked? Based on a modern nixpkgs? The allowed-references stuff is somewhat recent.

@acowley

acowley commented Dec 26, 2015

Copy link
Copy Markdown
Contributor Author

Yes, I'm sure. I haven't come across allowed-references, though, so perhaps we're still not on the same page.

EDIT: I realize I asked for input from more people somewhat further along this road than is ideal. The first attempt at fixing this that worked for me was simply replacing the naked call to readlink in ld-wrapper.sh with a call to @coreutils@/bin/readlink.

The more ambitious explicit setting of all PATH variables has caused this reference from stdenv to bootstrap-tools. I can walk back the PATH idea, but I'd like us all to put our heads together to see if we can make it work as this issue slipping through for so long was a bit concerning.

@shlevy

shlevy commented Dec 26, 2015

Copy link
Copy Markdown
Member

Sorry, it's allowedReferences and allowedRequisites. The error message above comes from https://github.com/NixOS/nixpkgs/blob/master/pkgs/stdenv/pure-darwin/default.nix#L296-L302

@acowley

acowley commented Dec 26, 2015

Copy link
Copy Markdown
Contributor Author

Oh, right, I should have recognized that. I'm actually not sure which specific reference in stdenv is causing the trouble. I've ag'ed through the stdenv store path in the error message, but there's no bootstrap-tools path there, so I'm guessing it's a transitive dep but I'm not sure of an easy(-ish) way of tracking it down.

@shlevy

shlevy commented Dec 27, 2015

Copy link
Copy Markdown
Member

You can do nix-store -q --tree to see what's bringing in the reference to bootstrap-tools

@acowley

acowley commented Dec 28, 2015

Copy link
Copy Markdown
Contributor Author

Well, that's quite a large tree, but the bootstrap-tools references are all over the place: bash, bison, perl, etc. I feel like this is related to the stageFun definition that sets dontPatchShebangs=1 to avoid retaining dependencies. The logic there does a path comparison with the bootstrapTools derivation to control the patching behavior. That seems super fragile, so I'm not sure how to leverage it to fix other path substitution issues. It seems like hack-upon-hack, but one option might be to use that same check to cripple the @coreutils@ substitution.

@acowley

acowley commented Dec 29, 2015

Copy link
Copy Markdown
Contributor Author

I misunderstood the suggestion to look atnix-store -q output. Without this PR, bootstrap-tools is still in the nix-store -q --tree output for things like bash, so I don't know what to look at for the build that errors out. I've pasted the tree here, so if anyone can identify the problem, please share. All I know how to do is twiddle things and rebuild, which takes a long time.

@copumpkin

Copy link
Copy Markdown
Member

You can also get something far clearer with --graph, since --tree duplicates common parts of the graph and makes the whole thing look far larger than it is. You can just feed --graph into graphviz 😄

@shlevy

shlevy commented Dec 29, 2015

Copy link
Copy Markdown
Member

@acowley You looked at the graph f or the .drv file. We need to see the graph for the build output, i.e. the runtime dependencies graph.

@acowley

acowley commented Dec 29, 2015

Copy link
Copy Markdown
Contributor Author

I looked at the .drv because,

$ nix-store -q --tree /nix/store/i8lvwn55nbynswx6wdsvw4bvj8aw0scg-stdenv-darwin
/nix/store/i8lvwn55nbynswx6wdsvw4bvj8aw0scg-stdenv-darwin
error: path ‘/nix/store/i8lvwn55nbynswx6wdsvw4bvj8aw0scg-stdenv-darwin’ is not valid

I can read the out paths from the mentioned derivations and query them, but is there a faster way of doing so?

ETA: Doing some manual spelunking of that nature, gnutar pulls in gettext which pulls in bootstrap-tools.

$ nix-store -q --tree /nix/store/7n98nlx182idn240xiyq7w9qb9w5wap9-gnutar-1.28
/nix/store/7n98nlx182idn240xiyq7w9qb9w5wap9-gnutar-1.28
+---/nix/store/ifbd63z0c0fbjnax9pkm9dfxb21smpna-Libsystem-1197.1.1
|   +---/nix/store/ifbd63z0c0fbjnax9pkm9dfxb21smpna-Libsystem-1197.1.1 [...]
+---/nix/store/a0lwyng6d16r6pn8zdr210nb3kr1cswi-gettext-0.19.6
|   +---/nix/store/7l8l3sp3wk0idx0191qhn4hvdvx0wznq-bootstrap-tools
|   |   +---/nix/store/7l8l3sp3wk0idx0191qhn4hvdvx0wznq-bootstrap-tools [...]
|   +---/nix/store/ifbd63z0c0fbjnax9pkm9dfxb21smpna-Libsystem-1197.1.1 [...]
|   +---/nix/store/jnfjlpdqjwryz8j2mbhkhfww5vn9vw35-libiconv-41
|   |   +---/nix/store/ifbd63z0c0fbjnax9pkm9dfxb21smpna-Libsystem-1197.1.1 [...]
|   |   +---/nix/store/jnfjlpdqjwryz8j2mbhkhfww5vn9vw35-libiconv-41 [...]
|   +---/nix/store/xyblkw6p667hdsf5klcf4n6m1wfv702q-libc++-3.6.2
|   |   +---/nix/store/ifbd63z0c0fbjnax9pkm9dfxb21smpna-Libsystem-1197.1.1 [...]
|   |   +---/nix/store/jw5g3f1519fq0v9x66dsl17h2lwv2ync-libc++abi-3.6.2
|   |   |   +---/nix/store/ifbd63z0c0fbjnax9pkm9dfxb21smpna-Libsystem-1197.1.1 [
|   |   |   +---/nix/store/jw5g3f1519fq0v9x66dsl17h2lwv2ync-libc++abi-3.6.2 [...
|   |   +---/nix/store/xyblkw6p667hdsf5klcf4n6m1wfv702q-libc++-3.6.2 [...]
|   +---/nix/store/a0lwyng6d16r6pn8zdr210nb3kr1cswi-gettext-0.19.6 [...]
+---/nix/store/7n98nlx182idn240xiyq7w9qb9w5wap9-gnutar-1.28 [...]

@shlevy

shlevy commented Dec 29, 2015

Copy link
Copy Markdown
Member

Ugh, right, because the build failed. Can you check the dependency tree of the cc-wrapper that's closest to stdenv?

@shlevy

shlevy commented Dec 29, 2015

Copy link
Copy Markdown
Member

@edolstra How difficult would it be to have nix store the dependency info of a build that failed due to disallowed dependencies (either because of allowedReferences or circular reference in multiple outputs) somewhere for investigations like this?

@acowley

acowley commented Dec 29, 2015

Copy link
Copy Markdown
Contributor Author

I don't quite follow what you mean by "closest". I looked at the out path of /nix/store/9zf63nb5x5dy7lznf2xdfh5ba0fdki8g-clang-wrapper-3.6.2.drv (mentioned in the stdenv derivation's tree), and bootstrap-tools didn't show up there, but I"m not sure if that's the same thing.

@vcunat

vcunat commented Dec 29, 2015

Copy link
Copy Markdown
Member

How difficult would it be to have nix store the dependency info of a build that failed due to disallowed dependencies (either because of allowedReferences or circular reference in multiple outputs) somewhere for investigations like this?

The output paths of any failed build remain in the /nix/store (at least for me). You can grep -R through them...

@acowley

acowley commented Jan 5, 2016

Copy link
Copy Markdown
Contributor Author

Small update: I tried taking out the PR lines that clear PATH in gnatlink-wrapper.sh, ld-solaris-wrapper.sh (probably not relevant), and setup-hook.sh. This let me get much further. However, I run into an error building the gnum4 package (needed for building GHC). My suspicion is that I was unsetting a PATH that was being relied on by something like patchShebangs, and the bootstrapTools shell was leaking in as per the warning in the pure-darwin stdenv definition, but that's just a guess.

I dug into the m4 build failure a bit since it's a smaller thing to look at, and it's kind of interesting. The build failures are due to various checks in m4's build system to accommodate Apple's SDK. Since we still pull a version of LibSystem in, we run into trouble with things like redefinitions of sigismember and sigaction (from signal.h in m4's lib directory). I tweaked the C pre-processor guards around a few things to nudge it further along, but then ran into a problem with clock resolution. I stopped digging at this point, but can keep going if anyone thinks it's helpful.

(ETA: If you're curious, one of the macros that changes when built in a nix-shell vs OS X shell is @HAVE_POSIX_SIGNALBLOCKING@ which is set in the ./configure'ed package in the OS X shell, but not in the one ./configure'ed in a nix-shell I ended up with through the PR modification described above.)

Alternatively, if I take the unpacked nixpkgs m4 package, and ./configure && make in a regular OS X shell, it builds just fine with the normal OS X CLTools.

So either explicitly setting the PATH as suggested is still doing unwanted violence, or we previously had unintended dependencies on the PATH not being pruned down so extensively.

To summarize the current situation, the only working version of this PR -- "working" in that the parts of nixpkgs I use all build, and compilers (clang and ghc) can be used outside of a nix shell -- is the one where only the call to readlink is changed to @coreutils@/bin/readlink. Every other variation of this fix has run aground somewhere.

@acowley
acowley force-pushed the readlink branch 2 times, most recently from 6ce4fd1 to 07f12d1 Compare January 19, 2016 22:46
The ld-wrapper.sh script calls `readlink` in some circumstances. We need
to ensure that this is the `readlink` from the `coreutils` package so
that flag support is as expected.

This is accomplished by explicitly setting PATH at the top of each shell
script.

Without doing this, the following happens with a trivial `main.c`:

```
nix-env -f "<nixpkgs>" -iA pkgs.clang
$ clang main.c -L /nix/../nix/store/2ankvagznq062x1gifpxwkk7fp3xwy63-xnu-2422.115.4/Library -o a.out
readlink: illegal option -- f
usage: readlink [-n] [file ...]
```

The key element is the `..` in the path supplied to the linker via a
`-L` flag. With this patch, the above invocation works correctly on
darwin, whose native `/usr/bin/readlink` does not support the `-f` flag.

The explicit path also ensures that the `grep` called by `cc-wrapper.sh`
is the one from Nix.

Fixes NixOS#6447
@acowley

acowley commented Jan 19, 2016

Copy link
Copy Markdown
Contributor Author

I finally got this working. The last holdup was that cc-wrapper.sh also calls grep which we should have been ensuring was the grep from nix.

I've only built this on darwin so far (and that was before the last rebase, so I'll build again tonight), so other stdenvs likely need adjustment to satisfy the explicit gnugrep dependency. Does this approach seem okay to folks?

@vcunat

vcunat commented Jan 20, 2016

Copy link
Copy Markdown
Member

Ah, right, my assumption in #11798 (comment) was wrong – apparently it is necessary to do some kind of save-restore dance. While in these cases it's not too bad due to only having a single external exec at the end, it will probably be too cumbersome as a general technique (at least with bash).

I assume you have tested this well on darwin? Linux doesn't evaluate ATM, but I'll have a look into it.

How bash's environment seems to work

If one makes a change to a variable that is exported, that change is propagated to sub-processes. Even if one marks it local, commands called within the function inherit that (new) value.

@acowley

acowley commented Jan 20, 2016

Copy link
Copy Markdown
Contributor Author

Yes, I tried to avoid the dance, but couldn't make that work, either. It's been a long, boring voyage :)

I did a new build last night, so I'll work it over today to make sure everything is ship shape on Darwin. It seems fine from poking around clang and GHC, but I'll update if I find a problem.

@acowley

acowley commented Jan 21, 2016

Copy link
Copy Markdown
Contributor Author

Everything looks good on Darwin. Can anyone help me navigate getting Linux and NixOS (and WIndows?) validation? I can wrangle VMs, but if there's existing infrastructure I'd rather use that.

ETA: Specifically, we need to check if stage4 in every stdenv needs modification when calling cc-wrapper.

@acowley

acowley commented Jan 23, 2016

Copy link
Copy Markdown
Contributor Author

I spun up a NixOS VM on EC2, set nixpkgs to a checkout of this branch, and did a nixos-rebuild switch without any trouble. Installing clang also works fine. Not sure how much that evidence is worth. I'll try a non-NixOS linux when I have some time.

@acowley

acowley commented Jan 24, 2016

Copy link
Copy Markdown
Contributor Author

The last commit seems to get Linux up and running.

The trick there is that, as far as I can see, Linux was working because the grep executable came along with the bootstrap tools. Explicitly requiring gnugrep required a bit more plumbing.

Any help getting other stdenvs in order would be much appreciated!

@acowley

acowley commented Jan 25, 2016

Copy link
Copy Markdown
Contributor Author

There are rather a lot of arguably problematic shell scripts that make use of utilities. write-mirror-list.sh uses grep, and setup.sh from the generic stdenv uses tar, as examples.

Perhaps this kind of explicit dependency tracking isn't realistic for nixpkgs, and people would rather have these things white listed in some way. It takes hours of compiling to get to a point where these errors come out, and I'm not sure how much more of it I can reasonably do as this seems like perhaps a deliberate choice in the way nixpkgs is designed.

Non-Linux platforms could alternately be supported by patching on an as-needed basis.

@vcunat vcunat self-assigned this Jan 25, 2016
vcunat added a commit that referenced this pull request Jan 25, 2016
@vcunat

vcunat commented Jan 25, 2016

Copy link
Copy Markdown
Member

Staged after some minor fixups: 8f48a97 and 98c7e70.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants