Merge pull request #50835 from Mic92/go-cross

Go cross compilation
This commit is contained in:
Jörg Thalheim 2018-11-24 10:39:11 +00:00 committed by GitHub
commit 6f2475f5bf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 100 additions and 45 deletions

View File

@ -1,5 +1,6 @@
{ stdenv, fetchFromGitHub, tzdata, iana-etc, go_bootstrap, runCommand, writeScriptBin { stdenv, fetchFromGitHub, tzdata, iana-etc, go_bootstrap, runCommand, writeScriptBin
, perl, which, pkgconfig, patch, procps, pcre, cacert, llvm, Security, Foundation }: , perl, which, pkgconfig, patch, procps, pcre, cacert, llvm, Security, Foundation
, buildPackages, targetPackages }:
let let
@ -7,12 +8,22 @@ let
goBootstrap = runCommand "go-bootstrap" {} '' goBootstrap = runCommand "go-bootstrap" {} ''
mkdir $out mkdir $out
cp -rf ${go_bootstrap}/* $out/ cp -rf ${buildPackages.go_bootstrap}/* $out/
chmod -R u+w $out chmod -R u+w $out
find $out -name "*.c" -delete find $out -name "*.c" -delete
cp -rf $out/bin/* $out/share/go/bin/ cp -rf $out/bin/* $out/share/go/bin/
''; '';
goarch = platform: {
"i686" = "386";
"x86_64" = "amd64";
"aarch64" = "arm64";
"arm" = "arm";
"armv5tel" = "arm";
"armv6l" = "arm";
"armv7l" = "arm";
}.${platform.parsed.cpu.name} or (throw "Unsupported system");
in in
stdenv.mkDerivation rec { stdenv.mkDerivation rec {
@ -26,13 +37,13 @@ stdenv.mkDerivation rec {
sha256 = "0pk7pxfm3ij2ksdrg49jz501fr1d103zr4mjjwv821if9g279jc9"; sha256 = "0pk7pxfm3ij2ksdrg49jz501fr1d103zr4mjjwv821if9g279jc9";
}; };
GOCACHE = "off";
# perl is used for testing go vet # perl is used for testing go vet
nativeBuildInputs = [ perl which pkgconfig patch procps ]; nativeBuildInputs = [ perl which pkgconfig patch procps ];
buildInputs = [ cacert pcre ] buildInputs = [ cacert pcre ]
++ optionals stdenv.isLinux [ stdenv.cc.libc.out ] ++ optionals stdenv.isLinux [ stdenv.cc.libc.out ]
++ optionals (stdenv.hostPlatform.libc == "glibc") [ stdenv.cc.libc.static ]; ++ optionals (stdenv.hostPlatform.libc == "glibc") [ stdenv.cc.libc.static ];
propagatedBuildInputs = optionals stdenv.isDarwin [ Security Foundation ]; propagatedBuildInputs = optionals stdenv.isDarwin [ Security Foundation ];
hardeningDisable = [ "all" ]; hardeningDisable = [ "all" ];
@ -126,62 +137,93 @@ stdenv.mkDerivation rec {
substituteInPlace "src/cmd/link/internal/ld/lib.go" --replace dsymutil ${llvm}/bin/llvm-dsymutil substituteInPlace "src/cmd/link/internal/ld/lib.go" --replace dsymutil ${llvm}/bin/llvm-dsymutil
''; '';
GOOS = stdenv.hostPlatform.parsed.kernel.name; GOOS = stdenv.targetPlatform.parsed.kernel.name;
GOARCH = { GOARCH = goarch stdenv.targetPlatform;
"i686" = "386"; # GOHOSTOS/GOHOSTARCH must match the building system, not the host system.
"x86_64" = "amd64"; # Go will nevertheless build a for host system that we will copy over in
"aarch64" = "arm64"; # the install phase.
"arm" = "arm"; GOHOSTOS = stdenv.buildPlatform.parsed.kernel.name;
"armv5tel" = "arm"; GOHOSTARCH = goarch stdenv.buildPlatform;
"armv6l" = "arm";
"armv7l" = "arm"; # {CC,CXX}_FOR_TARGET must be only set for cross compilation case as go expect those
}.${stdenv.hostPlatform.parsed.cpu.name} or (throw "Unsupported system"); # to be different from CC/CXX
CC_FOR_TARGET = if (stdenv.hostPlatform != stdenv.targetPlatform) then
"${targetPackages.stdenv.cc}/bin/${targetPackages.stdenv.cc.targetPrefix}cc"
else if (stdenv.buildPlatform != stdenv.targetPlatform) then
"${stdenv.cc.targetPrefix}cc"
else
null;
CXX_FOR_TARGET = if (stdenv.hostPlatform != stdenv.targetPlatform) then
"${targetPackages.stdenv.cc}/bin/${targetPackages.stdenv.cc.targetPrefix}c++"
else if (stdenv.buildPlatform != stdenv.targetPlatform) then
"${stdenv.cc.targetPrefix}c++"
else
null;
GOARM = toString (stdenv.lib.intersectLists [(stdenv.hostPlatform.parsed.cpu.version or "")] ["5" "6" "7"]); GOARM = toString (stdenv.lib.intersectLists [(stdenv.hostPlatform.parsed.cpu.version or "")] ["5" "6" "7"]);
GO386 = 387; # from Arch: don't assume sse2 on i686 GO386 = 387; # from Arch: don't assume sse2 on i686
CGO_ENABLED = 1; CGO_ENABLED = 1;
GOROOT_BOOTSTRAP = "${goBootstrap}/share/go";
# Hopefully avoids test timeouts on Hydra # Hopefully avoids test timeouts on Hydra
GO_TEST_TIMEOUT_SCALE = 3; GO_TEST_TIMEOUT_SCALE = 3;
# The go build actually checks for CC=*/clang and does something different, so we don't
# just want the generic `cc` here.
CC = if stdenv.isDarwin then "clang" else "cc";
configurePhase = ''
# Indicate that we are running on build infrastructure # Indicate that we are running on build infrastructure
# Some tests assume things like home directories and users exists # Some tests assume things like home directories and users exists
export GO_BUILDER_NAME=nix GO_BUILDER_NAME = "nix";
mkdir -p $out/share/go/bin GOROOT_BOOTSTRAP="${goBootstrap}/share/go";
export GOROOT=$out/share/go
export GOBIN=$GOROOT/bin postConfigure = ''
export PATH=$GOBIN:$PATH export GOCACHE=$TMPDIR/go-cache
# this is compiled into the binary
export GOROOT_FINAL=$out/share/go
export PATH=$(pwd)/bin:$PATH
# Independent from host/target, CC should produce code for the building system.
export CC=${buildPackages.stdenv.cc}/bin/cc
ulimit -a ulimit -a
''; '';
installPhase = '' postBuild = ''
runHook preInstall (cd src && ./make.bash)
cp -r . $GOROOT
( cd $GOROOT/src && ./make.bash )
runHook postInstall
''; '';
doInstallCheck = true; doCheck = stdenv.hostPlatform == stdenv.targetPlatform;
installCheckPhase = ''
checkPhase = ''
runHook preCheck runHook preCheck
( cd $GOROOT/src && ./run.bash --no-rebuild) (cd src && ./run.bash --no-rebuild)
runHook postCheck runHook postCheck
''; '';
preFixup = '' preInstall = ''
rm -r $out/share/go/pkg/bootstrap # Contains the wrong perl shebang when cross compiling,
rm -r $out/share/go/pkg/obj # since it is not used for anything we can deleted as well.
ln -s $out/share/go/bin $out/bin rm src/regexp/syntax/make_perl_groups.pl
'' + (if (stdenv.buildPlatform != stdenv.hostPlatform) then ''
mv bin/*_*/* bin
rmdir bin/*_*
${optionalString (!(GOHOSTARCH == GOARCH && GOOS == GOHOSTOS)) ''
rm -rf pkg/${GOHOSTOS}_${GOHOSTARCH} pkg/tool/${GOHOSTOS}_${GOHOSTARCH}
''}
'' else if (stdenv.hostPlatform != stdenv.targetPlatform) then ''
rm -rf bin/*_*
${optionalString (!(GOHOSTARCH == GOARCH && GOOS == GOHOSTOS)) ''
rm -rf pkg/${GOOS}_${GOARCH} pkg/tool/${GOOS}_${GOARCH}
''}
'' else "");
installPhase = ''
runHook preInstall
mkdir -p $GOROOT_FINAL
cp -a bin pkg src lib misc api doc $GOROOT_FINAL
ln -s $GOROOT_FINAL/bin $out/bin
runHook postInstall
''; '';
setupHook = ./setup-hook.sh; setupHook = ./setup-hook.sh;
disallowedReferences = [ go_bootstrap ]; disallowedReferences = [ goBootstrap ];
meta = with stdenv.lib; { meta = with stdenv.lib; {
branch = "1.11"; branch = "1.11";

View File

@ -1,5 +1,5 @@
{ go, govers, lib, fetchgit, fetchhg, fetchbzr, rsync { go, govers, lib, fetchgit, fetchhg, fetchbzr, rsync
, removeReferencesTo, fetchFromGitHub }: , removeReferencesTo, fetchFromGitHub, stdenv }:
{ name, buildInputs ? [], nativeBuildInputs ? [], passthru ? {}, preFixup ? "" { name, buildInputs ? [], nativeBuildInputs ? [], passthru ? {}, preFixup ? ""
, shellHook ? "" , shellHook ? ""
@ -80,7 +80,9 @@ go.stdenv.mkDerivation (
inherit name; inherit name;
nativeBuildInputs = [ removeReferencesTo go ] nativeBuildInputs = [ removeReferencesTo go ]
++ (lib.optional (!dontRenameImports) govers) ++ nativeBuildInputs; ++ (lib.optional (!dontRenameImports) govers) ++ nativeBuildInputs;
buildInputs = [ go ] ++ buildInputs; buildInputs = buildInputs;
inherit (go) GOOS GOARCH;
configurePhase = args.configurePhase or '' configurePhase = args.configurePhase or ''
runHook preConfigure runHook preConfigure
@ -167,7 +169,18 @@ go.stdenv.mkDerivation (
export NIX_BUILD_CORES=1 export NIX_BUILD_CORES=1
fi fi
getGoDirs "" | xargs -n1 -P $NIX_BUILD_CORES bash -c 'buildGoDir install "$@"' -- getGoDirs "" | xargs -n1 -P $NIX_BUILD_CORES bash -c 'buildGoDir install "$@"' --
'' + lib.optionalString (stdenv.hostPlatform != stdenv.buildPlatform) ''
# normalize cross-compiled builds w.r.t. native builds
(
dir=$NIX_BUILD_TOP/go/bin/${go.GOOS}_${go.GOARCH}
if [[ -n "$(shopt -s nullglob; echo $dir/*)" ]]; then
mv $dir/* $dir/..
fi
if [[ -d $dir ]]; then
rmdir $dir
fi
)
'' + ''
runHook postBuild runHook postBuild
''; '';

View File

@ -13029,13 +13029,13 @@ with pkgs;
### DEVELOPMENT / GO MODULES ### DEVELOPMENT / GO MODULES
buildGo19Package = callPackage ../development/go-modules/generic { buildGo19Package = callPackage ../development/go-modules/generic {
go = go_1_9; go = buildPackages.go_1_9;
}; };
buildGo110Package = callPackage ../development/go-modules/generic { buildGo110Package = callPackage ../development/go-modules/generic {
go = go_1_10; go = buildPackages.go_1_10;
}; };
buildGo111Package = callPackage ../development/go-modules/generic { buildGo111Package = callPackage ../development/go-modules/generic {
go = go_1_11; go = buildPackages.go_1_11;
}; };
buildGoPackage = buildGo111Package; buildGoPackage = buildGo111Package;