buildGoPackage: also support pname

Following python-modules and stdenv.mkDerivation.
Hopefully zero rebuilds.

Motivation: https://github.com/NixOS/nixpkgs/pull/52409
This commit is contained in:
Jörg Thalheim 2018-12-18 03:55:02 +01:00
parent b0ccd8be8e
commit b4593b4629
No known key found for this signature in database
GPG Key ID: CA4106B8D7CC79FA

View File

@ -1,7 +1,10 @@
{ go, govers, lib, fetchgit, fetchhg, fetchbzr, rsync { go, govers, lib, fetchgit, fetchhg, fetchbzr, rsync
, removeReferencesTo, fetchFromGitHub, stdenv }: , removeReferencesTo, fetchFromGitHub, stdenv }:
{ name, buildInputs ? [], nativeBuildInputs ? [], passthru ? {}, preFixup ? "" { buildInputs ? []
, nativeBuildInputs ? []
, passthru ? {}
, preFixup ? ""
, shellHook ? "" , shellHook ? ""
# We want parallel builds by default # We want parallel builds by default
@ -34,7 +37,6 @@
, meta ? {}, ... } @ args': , meta ? {}, ... } @ args':
if disabled then throw "${name} not supported for go ${go.meta.branch}" else
with builtins; with builtins;
@ -72,170 +74,171 @@ let
goPath = if goDeps != null then importGodeps { depsFile = goDeps; } ++ extraSrcs goPath = if goDeps != null then importGodeps { depsFile = goDeps; } ++ extraSrcs
else extraSrcs; else extraSrcs;
in package = go.stdenv.mkDerivation (
(builtins.removeAttrs args [ "goPackageAliases" "disabled" ]) // {
go.stdenv.mkDerivation ( nativeBuildInputs = [ removeReferencesTo go ]
(builtins.removeAttrs args [ "goPackageAliases" "disabled" ]) // { ++ (lib.optional (!dontRenameImports) govers) ++ nativeBuildInputs;
buildInputs = buildInputs;
inherit name; inherit (go) GOOS GOARCH;
nativeBuildInputs = [ removeReferencesTo go ]
++ (lib.optional (!dontRenameImports) govers) ++ nativeBuildInputs;
buildInputs = buildInputs;
inherit (go) GOOS GOARCH; configurePhase = args.configurePhase or ''
runHook preConfigure
configurePhase = args.configurePhase or '' # Extract the source
runHook preConfigure cd "$NIX_BUILD_TOP"
mkdir -p "go/src/$(dirname "$goPackagePath")"
mv "$sourceRoot" "go/src/$goPackagePath"
# Extract the source '' + lib.flip lib.concatMapStrings goPath ({ src, goPackagePath }: ''
cd "$NIX_BUILD_TOP" mkdir goPath
mkdir -p "go/src/$(dirname "$goPackagePath")" (cd goPath; unpackFile "${src}")
mv "$sourceRoot" "go/src/$goPackagePath" mkdir -p "go/src/$(dirname "${goPackagePath}")"
chmod -R u+w goPath/*
mv goPath/* "go/src/${goPackagePath}"
rmdir goPath
'' + lib.flip lib.concatMapStrings goPath ({ src, goPackagePath }: '' '') + (lib.optionalString (extraSrcPaths != []) ''
mkdir goPath ${rsync}/bin/rsync -a ${lib.concatMapStringsSep " " (p: "${p}/src") extraSrcPaths} go
(cd goPath; unpackFile "${src}")
mkdir -p "go/src/$(dirname "${goPackagePath}")"
chmod -R u+w goPath/*
mv goPath/* "go/src/${goPackagePath}"
rmdir goPath
'') + (lib.optionalString (extraSrcPaths != []) '' '') + ''
${rsync}/bin/rsync -a ${lib.concatMapStringsSep " " (p: "${p}/src") extraSrcPaths} go export GOPATH=$NIX_BUILD_TOP/go:$GOPATH
export GOCACHE=$TMPDIR/go-cache
'') + '' runHook postConfigure
export GOPATH=$NIX_BUILD_TOP/go:$GOPATH '';
export GOCACHE=$TMPDIR/go-cache
runHook postConfigure renameImports = args.renameImports or (
''; let
inputsWithAliases = lib.filter (x: x ? goPackageAliases)
(buildInputs ++ (args.propagatedBuildInputs or [ ]));
rename = to: from: "echo Renaming '${from}' to '${to}'; govers -d -m ${from} ${to}";
renames = p: lib.concatMapStringsSep "\n" (rename p.goPackagePath) p.goPackageAliases;
in lib.concatMapStringsSep "\n" renames inputsWithAliases);
renameImports = args.renameImports or ( buildPhase = args.buildPhase or ''
let runHook preBuild
inputsWithAliases = lib.filter (x: x ? goPackageAliases)
(buildInputs ++ (args.propagatedBuildInputs or [ ]));
rename = to: from: "echo Renaming '${from}' to '${to}'; govers -d -m ${from} ${to}";
renames = p: lib.concatMapStringsSep "\n" (rename p.goPackagePath) p.goPackageAliases;
in lib.concatMapStringsSep "\n" renames inputsWithAliases);
buildPhase = args.buildPhase or '' runHook renameImports
runHook preBuild
runHook renameImports buildGoDir() {
local d; local cmd;
buildGoDir() { cmd="$1"
local d; local cmd; d="$2"
cmd="$1" . $TMPDIR/buildFlagsArray
d="$2" echo "$d" | grep -q "\(/_\|examples\|Godeps\)" && return 0
. $TMPDIR/buildFlagsArray [ -n "$excludedPackages" ] && echo "$d" | grep -q "$excludedPackages" && return 0
echo "$d" | grep -q "\(/_\|examples\|Godeps\)" && return 0 local OUT
[ -n "$excludedPackages" ] && echo "$d" | grep -q "$excludedPackages" && return 0 if ! OUT="$(go $cmd $buildFlags "''${buildFlagsArray[@]}" -v $d 2>&1)"; then
local OUT if ! echo "$OUT" | grep -qE '(no( buildable| non-test)?|build constraints exclude all) Go (source )?files'; then
if ! OUT="$(go $cmd $buildFlags "''${buildFlagsArray[@]}" -v $d 2>&1)"; then echo "$OUT" >&2
if ! echo "$OUT" | grep -qE '(no( buildable| non-test)?|build constraints exclude all) Go (source )?files'; then return 1
echo "$OUT" >&2 fi
return 1
fi fi
fi if [ -n "$OUT" ]; then
if [ -n "$OUT" ]; then echo "$OUT" >&2
echo "$OUT" >&2 fi
fi return 0
return 0 }
}
getGoDirs() { getGoDirs() {
local type; local type;
type="$1" type="$1"
if [ -n "$subPackages" ]; then if [ -n "$subPackages" ]; then
echo "$subPackages" | sed "s,\(^\| \),\1$goPackagePath/,g" echo "$subPackages" | sed "s,\(^\| \),\1$goPackagePath/,g"
else
pushd "$NIX_BUILD_TOP/go/src" >/dev/null
find "$goPackagePath" -type f -name \*$type.go -exec dirname {} \; | grep -v "/vendor/" | sort | uniq
popd >/dev/null
fi
}
if (( "''${NIX_DEBUG:-0}" >= 1 )); then
buildFlagsArray+=(-x)
fi
if [ ''${#buildFlagsArray[@]} -ne 0 ]; then
declare -p buildFlagsArray > $TMPDIR/buildFlagsArray
else else
pushd "$NIX_BUILD_TOP/go/src" >/dev/null touch $TMPDIR/buildFlagsArray
find "$goPackagePath" -type f -name \*$type.go -exec dirname {} \; | grep -v "/vendor/" | sort | uniq
popd >/dev/null
fi fi
} export -f buildGoDir # xargs needs to see the function
if [ -z "$enableParallelBuilding" ]; then
if (( "''${NIX_DEBUG:-0}" >= 1 )); then export NIX_BUILD_CORES=1
buildFlagsArray+=(-x)
fi
if [ ''${#buildFlagsArray[@]} -ne 0 ]; then
declare -p buildFlagsArray > $TMPDIR/buildFlagsArray
else
touch $TMPDIR/buildFlagsArray
fi
export -f buildGoDir # xargs needs to see the function
if [ -z "$enableParallelBuilding" ]; then
export NIX_BUILD_CORES=1
fi
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 fi
if [[ -d $dir ]]; then getGoDirs "" | xargs -n1 -P $NIX_BUILD_CORES bash -c 'buildGoDir install "$@"' --
rmdir $dir '' + lib.optionalString (stdenv.hostPlatform != stdenv.buildPlatform) ''
fi # normalize cross-compiled builds w.r.t. native builds
) (
'' + '' dir=$NIX_BUILD_TOP/go/bin/${go.GOOS}_${go.GOARCH}
runHook postBuild if [[ -n "$(shopt -s nullglob; echo $dir/*)" ]]; then
''; mv $dir/* $dir/..
fi
if [[ -d $dir ]]; then
rmdir $dir
fi
)
'' + ''
runHook postBuild
'';
doCheck = args.doCheck or false; doCheck = args.doCheck or false;
checkPhase = args.checkPhase or '' checkPhase = args.checkPhase or ''
runHook preCheck runHook preCheck
getGoDirs test | xargs -n1 -P $NIX_BUILD_CORES bash -c 'buildGoDir test "$@"' -- getGoDirs test | xargs -n1 -P $NIX_BUILD_CORES bash -c 'buildGoDir test "$@"' --
runHook postCheck runHook postCheck
''; '';
installPhase = args.installPhase or '' installPhase = args.installPhase or ''
runHook preInstall runHook preInstall
mkdir -p $bin mkdir -p $bin
dir="$NIX_BUILD_TOP/go/bin" dir="$NIX_BUILD_TOP/go/bin"
[ -e "$dir" ] && cp -r $dir $bin [ -e "$dir" ] && cp -r $dir $bin
runHook postInstall runHook postInstall
''; '';
preFixup = preFixup + '' preFixup = preFixup + ''
find $bin/bin -type f -exec ${removeExpr removeReferences} '{}' + || true find $bin/bin -type f -exec ${removeExpr removeReferences} '{}' + || true
''; '';
shellHook = '' shellHook = ''
d=$(mktemp -d "--suffix=-$name") d=$(mktemp -d "--suffix=-$name")
'' + toString (map (dep: '' '' + toString (map (dep: ''
mkdir -p "$d/src/$(dirname "${dep.goPackagePath}")" mkdir -p "$d/src/$(dirname "${dep.goPackagePath}")"
ln -s "${dep.src}" "$d/src/${dep.goPackagePath}" ln -s "${dep.src}" "$d/src/${dep.goPackagePath}"
'' ''
) goPath) + '' ) goPath) + ''
export GOPATH=${lib.concatStringsSep ":" ( ["$d"] ++ ["$GOPATH"] ++ ["$PWD"] ++ extraSrcPaths)} export GOPATH=${lib.concatStringsSep ":" ( ["$d"] ++ ["$GOPATH"] ++ ["$PWD"] ++ extraSrcPaths)}
'' + shellHook; '' + shellHook;
disallowedReferences = lib.optional (!allowGoReference) go disallowedReferences = lib.optional (!allowGoReference) go
++ lib.optional (!dontRenameImports) govers; ++ lib.optional (!dontRenameImports) govers;
passthru = passthru // passthru = passthru //
{ inherit go; } // { inherit go; } //
lib.optionalAttrs (goPackageAliases != []) { inherit goPackageAliases; }; lib.optionalAttrs (goPackageAliases != []) { inherit goPackageAliases; };
enableParallelBuilding = enableParallelBuilding; enableParallelBuilding = enableParallelBuilding;
# I prefer to call this dev but propagatedBuildInputs expects $out to exist # I prefer to call this dev but propagatedBuildInputs expects $out to exist
outputs = args.outputs or [ "bin" "out" ]; outputs = args.outputs or [ "bin" "out" ];
meta = { meta = {
# Add default meta information # Add default meta information
homepage = "https://${goPackagePath}"; homepage = "https://${goPackagePath}";
platforms = go.meta.platforms or lib.platforms.all; platforms = go.meta.platforms or lib.platforms.all;
} // meta // { } // meta // {
# add an extra maintainer to every package # add an extra maintainer to every package
maintainers = (meta.maintainers or []) ++ maintainers = (meta.maintainers or []) ++
[ lib.maintainers.ehmry lib.maintainers.lethalman ]; [ lib.maintainers.ehmry lib.maintainers.lethalman ];
}; };
}) });
in if disabled then
throw "${package.name} not supported for go ${go.meta.branch}"
else
package