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

buildGoModule: fix cross-compilation with CGO_ENABLED=1 #107553

Merged
merged 1 commit into from Dec 26, 2020

Conversation

flokli
Copy link
Contributor

@flokli flokli commented Dec 25, 2020

When building a go binary that's linking against some .so/.a, while
cross-compiling, we need to pass the correct compiler, otherwise
go build will fail with the not-so helpful error message:

gcc_arm64.S: Assembler messages:
gcc_arm64.S:28: Error: no such instruction: `stp x29,x30,[sp,'
gcc_arm64.S:32: Error: too many memory references for `mov'
gcc_arm64.S:34: Error: no such instruction: `stp x19,x20,[sp,'
gcc_arm64.S:37: Error: no such instruction: `stp x21,x22,[sp,'
gcc_arm64.S:40: Error: no such instruction: `stp x23,x24,[sp,'
gcc_arm64.S:43: Error: no such instruction: `stp x25,x26,[sp,'
gcc_arm64.S:46: Error: no such instruction: `stp x27,x28,[sp,'
gcc_arm64.S:50: Error: too many memory references for `mov'
gcc_arm64.S:51: Error: too many memory references for `mov'
gcc_arm64.S:52: Error: too many memory references for `mov'
gcc_arm64.S:54: Error: no such instruction: `blr x20'
gcc_arm64.S:55: Error: no such instruction: `blr x19'
gcc_arm64.S:57: Error: no such instruction: `ldp x27,x28,[sp,'
gcc_arm64.S:60: Error: no such instruction: `ldp x25,x26,[sp,'
gcc_arm64.S:63: Error: no such instruction: `ldp x23,x24,[sp,'
gcc_arm64.S:66: Error: no such instruction: `ldp x21,x22,[sp,'
gcc_arm64.S:69: Error: no such instruction: `ldp x19,x20,[sp,'
gcc_arm64.S:72: Error: no such instruction: `ldp x29,x30,[sp],'
Motivation for this change
Things done
  • Tested using sandboxing (nix.useSandbox on NixOS, or option sandbox in nix.conf on non-NixOS linux)
  • Built on platform(s)
    • NixOS
    • macOS
    • other Linux distributions
  • Tested via one or more NixOS test(s) if existing and applicable for the change (look inside nixos/tests)
  • Tested compilation of all pkgs that depend on this change using nix-shell -p nixpkgs-review --run "nixpkgs-review wip"
  • Tested execution of all binary files (usually in ./result/bin/)
  • Determined the impact on package closure size (by running nix path-info -S before and after)
  • Ensured that relevant documentation is up to date
  • Fits CONTRIBUTING.md.

@Ericson2314
Copy link
Member

Is there a reason why the cc-wrapper setup hook isn't setting it?

@flokli
Copy link
Contributor Author

flokli commented Dec 25, 2020

@Ericson2314

Before that line introduced in this PR, things are set as follows:

declare -x CC="gcc"
declare -x NIX_CC="/nix/store/b96dqbx6pri2xp2xxlq6i269virrdaw6-gcc-wrapper-9.3.0"
declare -x NIX_CC_WRAPPER_TARGET_HOST_x86_64_unknown_linux_gnu="1"

The change sets CC to this:

export CC=/nix/store/lni6s5gfxaw9qf51nwi3zc466lf6ll3f-aarch64-unknown-linux-gnu-stage-final-gcc-debug-wrapper-9.3.0/bin/aarch64-unknown-linux-gnu-cc

I'm not really sure where that previous gcc is pointing to and how to interpret this - I'm not involved enough with our cc wrappers.

@Ericson2314
Copy link
Member

Ah the issue is that it's using go.stdenv, which is the stdenv used to build the go compiler. It's been that way since the beginning of this file in 28435e4, so can't tell why it's that way.

If you make it stdenv instead, then it will be the right stdenv and you will get the right env vars.

@kalbasit
Copy link
Member

Ah the issue is that it's using go.stdenv, which is the stdenv used to build the go compiler. It's been that way since the beginning of this file in 28435e4, so can't tell why it's that way.

IIRC I copied it from the buildGoPackage function without giving it much thought. I'm not sure why it's this way either.

@Mic92
Copy link
Member

Mic92 commented Dec 25, 2020

Ah the issue is that it's using go.stdenv, which is the stdenv used to build the go compiler. It's been that way since the beginning of this file in 28435e4, so can't tell why it's that way.

If you make it stdenv instead, then it will be the right stdenv and you will get the right env vars.

That sounds like the preferred way. It would also fix nix-shell environments.

@Ericson2314
Copy link
Member

IIRC I copied it from the buildGoPackage function without giving it much thought. I'm not sure why it's this way either.

Ah, sorry I missed the renaming involved. It does indeed in fact go back to faa5dcc, and my suggested fix was made on the buildGoPackage side in 6a27d63.

@flokli
Copy link
Contributor Author

flokli commented Dec 25, 2020

I updated the PR. Do we also need some of the GOHOSTARCH, GOHOSTOS, GOARM that were added there?

@Mic92
Copy link
Member

Mic92 commented Dec 26, 2020

I updated the PR. Do we also need some of the GOHOSTARCH, GOHOSTOS, GOARM that were added there?

I think we do.

When building a go binary that's linking against some .so/.a, while
cross-compiling, we need to pass the correct compiler, otherwise
`go build` will fail with the not-so helpful error message:

```
gcc_arm64.S: Assembler messages:
gcc_arm64.S:28: Error: no such instruction: `stp x29,x30,[sp,'
gcc_arm64.S:32: Error: too many memory references for `mov'
gcc_arm64.S:34: Error: no such instruction: `stp x19,x20,[sp,'
gcc_arm64.S:37: Error: no such instruction: `stp x21,x22,[sp,'
gcc_arm64.S:40: Error: no such instruction: `stp x23,x24,[sp,'
gcc_arm64.S:43: Error: no such instruction: `stp x25,x26,[sp,'
gcc_arm64.S:46: Error: no such instruction: `stp x27,x28,[sp,'
gcc_arm64.S:50: Error: too many memory references for `mov'
gcc_arm64.S:51: Error: too many memory references for `mov'
gcc_arm64.S:52: Error: too many memory references for `mov'
gcc_arm64.S:54: Error: no such instruction: `blr x20'
gcc_arm64.S:55: Error: no such instruction: `blr x19'
gcc_arm64.S:57: Error: no such instruction: `ldp x27,x28,[sp,'
gcc_arm64.S:60: Error: no such instruction: `ldp x25,x26,[sp,'
gcc_arm64.S:63: Error: no such instruction: `ldp x23,x24,[sp,'
gcc_arm64.S:66: Error: no such instruction: `ldp x21,x22,[sp,'
gcc_arm64.S:69: Error: no such instruction: `ldp x19,x20,[sp,'
gcc_arm64.S:72: Error: no such instruction: `ldp x29,x30,[sp],'
```
@flokli
Copy link
Contributor Author

flokli commented Dec 26, 2020

I updated the PR. Do we also need some of the GOHOSTARCH, GOHOSTOS, GOARM that were added there?

I think we do.

I decided to keep this out of this PR - this might already be autodetected properly, and we can even remove it from the other derivation: https://binx.io/blog/2018/11/25/go-cross-compilation/

Also, I managed to produce some binaries with this that did run, and failed to build at all before this PR.

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.

None yet

4 participants