buildGo: support Go 1.20+
This commit is contained in:
parent
9647245abb
commit
8141fbfccf
1 changed files with 41 additions and 5 deletions
46
third_party/tvl/nix/buildGo/default.nix
vendored
46
third_party/tvl/nix/buildGo/default.nix
vendored
|
@ -23,8 +23,8 @@ let
|
||||||
replaceStrings
|
replaceStrings
|
||||||
toString;
|
toString;
|
||||||
|
|
||||||
inherit (pkgs) lib runCommand runCommandCC fetchFromGitHub protobuf symlinkJoin;
|
inherit (pkgs) lib runCommand runCommandCC fetchFromGitHub protobuf symlinkJoin go;
|
||||||
go = pkgs.go_1_19;
|
goStdlib = buildStdlib go;
|
||||||
|
|
||||||
# Helpers for low-level Go compiler invocations
|
# Helpers for low-level Go compiler invocations
|
||||||
spaceOut = lib.concatStringsSep " ";
|
spaceOut = lib.concatStringsSep " ";
|
||||||
|
@ -56,6 +56,40 @@ let
|
||||||
overrideGo = new: makeOverridable f (orig // (new orig));
|
overrideGo = new: makeOverridable f (orig // (new orig));
|
||||||
};
|
};
|
||||||
|
|
||||||
|
buildStdlib = go: runCommandCC "go-stdlib-${go.version}" {
|
||||||
|
nativeBuildInputs = [ go ];
|
||||||
|
} ''
|
||||||
|
HOME=$NIX_BUILD_TOP/home
|
||||||
|
mkdir $HOME
|
||||||
|
|
||||||
|
goroot="$(go env GOROOT)"
|
||||||
|
cp -R "$goroot/src" "$goroot/pkg" .
|
||||||
|
|
||||||
|
chmod -R +w .
|
||||||
|
GODEBUG=installgoroot=all GOROOT=$NIX_BUILD_TOP go install -v --trimpath std
|
||||||
|
|
||||||
|
mkdir $out
|
||||||
|
cp -r pkg/*_*/* $out
|
||||||
|
|
||||||
|
find $out -name '*.a' | while read -r ARCHIVE_FULL; do
|
||||||
|
ARCHIVE="''${ARCHIVE_FULL#"$out/"}"
|
||||||
|
PACKAGE="''${ARCHIVE%.a}"
|
||||||
|
echo "packagefile $PACKAGE=$ARCHIVE_FULL"
|
||||||
|
done > $out/importcfg
|
||||||
|
'';
|
||||||
|
|
||||||
|
importcfgCmd = { name, deps, out ? "importcfg" }: ''
|
||||||
|
echo "# nix buildGo ${name}" > "${out}"
|
||||||
|
cat "${goStdlib}/importcfg" >> "${out}"
|
||||||
|
${lib.concatStringsSep "\n" (map (dep: ''
|
||||||
|
find "${dep}" -name '*.a' | while read -r pkgp; do
|
||||||
|
relpath="''${pkgp#"${dep}/"}"
|
||||||
|
pkgname="''${relpath%.a}"
|
||||||
|
echo "packagefile $pkgname=$pkgp"
|
||||||
|
done >> "${out}"
|
||||||
|
'') deps)}
|
||||||
|
'';
|
||||||
|
|
||||||
# High-level build functions
|
# High-level build functions
|
||||||
|
|
||||||
# Build a Go program out of the specified files and dependencies.
|
# Build a Go program out of the specified files and dependencies.
|
||||||
|
@ -69,10 +103,11 @@ let
|
||||||
in runCommand name {
|
in runCommand name {
|
||||||
buildInputs = cgoBuildInputs;
|
buildInputs = cgoBuildInputs;
|
||||||
} ''
|
} ''
|
||||||
${go}/bin/go tool compile -o ${name}.a -trimpath=$PWD -trimpath=${go} -p main ${includeSources uniqueDeps} ${spaceOut srcs}
|
${importcfgCmd { inherit name; deps = uniqueDeps; }}
|
||||||
|
${go}/bin/go tool compile -o ${name}.a -importcfg=importcfg -trimpath=$PWD -trimpath=${go} -p main ${includeSources uniqueDeps} ${spaceOut srcs}
|
||||||
mkdir -p $out/bin
|
mkdir -p $out/bin
|
||||||
export GOROOT_FINAL=go
|
export GOROOT_FINAL=go
|
||||||
${go}/bin/go tool link -o $out/bin/${name} -buildid nix \
|
${go}/bin/go tool link -o $out/bin/${name} -importcfg=importcfg -buildid nix \
|
||||||
-extldflags '${toString cgoLDFLAGS}' \
|
-extldflags '${toString cgoLDFLAGS}' \
|
||||||
${xFlags x_defs} ${includeLibs uniqueDeps} ${name}.a
|
${xFlags x_defs} ${includeLibs uniqueDeps} ${name}.a
|
||||||
'';
|
'';
|
||||||
|
@ -123,10 +158,11 @@ let
|
||||||
} ''
|
} ''
|
||||||
mkdir -p $out/${path}
|
mkdir -p $out/${path}
|
||||||
EXTRAGO=""
|
EXTRAGO=""
|
||||||
|
${importcfgCmd { inherit name; deps = uniqueDeps; }}
|
||||||
${srcList path (map (s: "${s}") srcs)}
|
${srcList path (map (s: "${s}") srcs)}
|
||||||
${asmBuild}
|
${asmBuild}
|
||||||
${cgoBuild}
|
${cgoBuild}
|
||||||
${go}/bin/go tool compile -pack ${asmLink} -o $out/${path}.a -trimpath=$PWD -trimpath=${go} -trimpath=$out/${path} -p ${path} ${includeSources uniqueDeps} ${spaceOut (map (srcDest path) srcs)} $EXTRAGO
|
${go}/bin/go tool compile -pack ${asmLink} -o $out/${path}.a -importcfg=importcfg -trimpath=$PWD -trimpath=${go} -trimpath=$out/${path} -p ${path} ${includeSources uniqueDeps} ${spaceOut (map (srcDest path) srcs)} $EXTRAGO
|
||||||
${asmPack}
|
${asmPack}
|
||||||
${cgoPack}
|
${cgoPack}
|
||||||
'').overrideAttrs (_: {
|
'').overrideAttrs (_: {
|
||||||
|
|
Loading…
Reference in a new issue