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

go: fix cross, enable ppc64le #99511

Merged
merged 1 commit into from Oct 5, 2020
Merged

go: fix cross, enable ppc64le #99511

merged 1 commit into from Oct 5, 2020

Conversation

@r-burns
Copy link
Contributor

@r-burns r-burns commented Oct 3, 2020

The CC_FOR_TARGET will run on the host platform so should be from pkgsHostTarget.
Cross-compiled Go then works for me on ppc64le.
(tested resulting pkgsCross.powernv.go on https://github.com/cpliakas/cgo-example)

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.
@r-burns
Copy link
Contributor Author

@r-burns r-burns commented Oct 3, 2020

Sorry, I think this needs more work. This is able to cross-build a native go compiler, but breaks when trying to build a cross-compiler.

@r-burns r-burns force-pushed the go-cross branch 2 times, most recently from 9f06f47 to e8fa489 Oct 4, 2020
@r-burns
Copy link
Contributor Author

@r-burns r-burns commented Oct 4, 2020

Ok, this now works for cross compiling go packages and cross building the go compiler, but is still very ugly ;)
I suspect the HostTarget/TargetTarget thing is a bit of a hack, to trick go's monolithic bootstrap script into building for the proper host/target. However this is working for me and is very useful since go won't bootstrap natively on ppc64le (not a supported go 1.4 target) so thought I'd share.

targetCC = (if stdenv.hostPlatform == stdenv.targetPlatform
then pkgsHostTarget else pkgsTargetTarget).stdenv.cc;
Copy link
Member

@Mic92 Mic92 Oct 4, 2020

Please have a look: cc @Ericson2314

@Ericson2314
Copy link
Member

@Ericson2314 Ericson2314 commented Oct 4, 2020

I don't think you should need that conditional definition of targetCC. when host == target, pkgsHostTarget should also equal pkgsTargetTarget.

Remember that stdenv (confusingly) has to tools to build the stage, not the tools built in that stage, so pkgsTargetTarget.stdenv.cc in fact would be something like pkgsHostTarget.{gcc.clang}.

@r-burns
Copy link
Contributor Author

@r-burns r-burns commented Oct 4, 2020

Hmm, well I agree it doesn't make sense, but without the targetcc hack, pkgsCross.powernv.go fails to build with

Building Go cmd/dist using /nix/store/5z4x1875l94bb66jk9zin41am0664xw0-go-bootstrap/share/go. ()
go tool dist: cannot invoke C compiler "/nix/store/ixanm098dmc7nyxmnp7jb3gcypcsm816-gcc-debug-wrapper-9.3.0/bin/cc": exit status 1

Go needs a system C compiler for use with cgo.
To set a C compiler, set CC=the-compiler.
To disable cgo, set CGO_ENABLED=0.

Command output:

/nix/store/ixanm098dmc7nyxmnp7jb3gcypcsm816-gcc-debug-wrapper-9.3.0/bin/cc: line 195: /nix/store/mvn4chpqxmaab4ccldinjyis1nffrpwz-gcc-debug-9.3.0-powerpc64le-unknown-linux-gnu/bin/gcc: cannot execute binary file: Exec format error

@Ericson2314
Copy link
Member

@Ericson2314 Ericson2314 commented Oct 4, 2020

Ah, I see the problem: that compiler isn't runnable at build time.

You want pkgsBuildTarget.targetPackages.stdenv.cc; the idea is that the targetPackages and stdenv cancel out, so one is left with something like pkgsBuildTarget.clang, as desired.

@r-burns
Copy link
Contributor Author

@r-burns r-burns commented Oct 4, 2020

Thank you so much, that is working beautifully now. I've kept the targetCC variable, let me know if you have a better name for it.

@r-burns r-burns marked this pull request as ready for review Oct 4, 2020
@Ericson2314 Ericson2314 merged commit 0794fb0 into NixOS:master Oct 5, 2020
18 checks passed
@r-burns r-burns deleted the go-cross branch Oct 5, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked issues

Successfully merging this pull request may close these issues.

None yet

3 participants