Merge pull request #155257 from IvarWithoutBones/dotnetModule-setuphook

buildDotnetModule: use setup hooks
This commit is contained in:
Jörg Thalheim 2022-02-27 06:46:21 +00:00 committed by GitHub
commit 9cbcf56032
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 309 additions and 154 deletions

View File

@ -1,6 +1,7 @@
{ lib, stdenvNoCC, linkFarmFromDrvs, nuget-to-nix, writeScript, makeWrapper, fetchurl, xml2, dotnetCorePackages, dotnetPackages, cacert }:
{ lib, stdenvNoCC, linkFarmFromDrvs, callPackage, nuget-to-nix, writeScript, makeWrapper, fetchurl, xml2, dotnetCorePackages, dotnetPackages, cacert }:
{ name ? "${args.pname}-${args.version}"
, pname ? name
, enableParallelBuilding ? true
, doCheck ? false
# Flags to pass to `makeWrapper`. This is done to avoid double wrapping.
@ -19,6 +20,8 @@
# Flags to pass to dotnet in all phases.
, dotnetFlags ? []
# The path to publish the project to. When unset, the directory "$out/lib/$pname" is used.
, installPath ? null
# The binaries that should get installed to `$out/bin`, relative to `$out/lib/$pname/`. These get wrapped accordingly.
# Unfortunately, dotnet has no method for doing this automatically.
# If unset, all executables in the projects root will get installed. This may cause bloat!
@ -67,6 +70,10 @@ assert projectFile == null -> throw "Defining the `projectFile` attribute is req
assert nugetDeps == null -> throw "Defining the `nugetDeps` attribute is required, as to lock the NuGet dependencies. This file can be generated by running the `passthru.fetch-deps` script.";
let
inherit (callPackage ./hooks {
inherit dotnet-sdk dotnet-test-sdk disabledTests nuget-source dotnet-runtime runtimeDeps buildType;
}) dotnetConfigureHook dotnetBuildHook dotnetCheckHook dotnetInstallHook dotnetFixupHook;
_nugetDeps = linkFarmFromDrvs "${name}-nuget-deps" (import nugetDeps {
fetchNuGet = { pname, version, sha256 }: fetchurl {
name = "${pname}-${version}.nupkg";
@ -77,8 +84,8 @@ let
_localDeps = linkFarmFromDrvs "${name}-local-nuget-deps" projectReferences;
nuget-source = stdenvNoCC.mkDerivation rec {
name = "${args.pname}-nuget-source";
meta.description = "A Nuget source with the dependencies for ${args.pname}";
name = "${pname}-nuget-source";
meta.description = "A Nuget source with the dependencies for ${pname}";
nativeBuildInputs = [ dotnetPackages.Nuget xml2 ];
buildCommand = ''
@ -103,168 +110,63 @@ let
)));
};
package = stdenvNoCC.mkDerivation (args // {
inherit buildType;
in stdenvNoCC.mkDerivation (args // {
nativeBuildInputs = args.nativeBuildInputs or [] ++ [
dotnetConfigureHook
dotnetBuildHook
dotnetCheckHook
dotnetInstallHook
dotnetFixupHook
nativeBuildInputs = args.nativeBuildInputs or [] ++ [ dotnet-sdk cacert makeWrapper ];
dotnet-sdk
cacert
makeWrapper
];
# Stripping breaks the executable
dontStrip = true;
# Stripping breaks the executable
dontStrip = args.dontStrip or true;
# gappsWrapperArgs gets included when wrapping for dotnet, as to avoid double wrapping
dontWrapGApps = true;
# gappsWrapperArgs gets included when wrapping for dotnet, as to avoid double wrapping
dontWrapGApps = args.dontWrapGApps or true;
DOTNET_NOLOGO = true; # This disables the welcome message.
DOTNET_CLI_TELEMETRY_OPTOUT = true;
DOTNET_NOLOGO = args.DOTNET_NOLOGO or true; # This disables the welcome message.
DOTNET_CLI_TELEMETRY_OPTOUT = args.DOTNET_CLI_TELEMETRY_OPTOUT or true;
passthru = {
fetch-deps = writeScript "fetch-${args.pname}-deps" ''
set -euo pipefail
cd "$(dirname "''${BASH_SOURCE[0]}")"
export HOME=$(mktemp -d)
deps_file="/tmp/${args.pname}-deps.nix"
store_src="${package.src}"
src="$(mktemp -d /tmp/${args.pname}.XXX)"
cp -rT "$store_src" "$src"
chmod -R +w "$src"
trap "rm -rf $src $HOME" EXIT
pushd "$src"
export DOTNET_NOLOGO=1
export DOTNET_CLI_TELEMETRY_OPTOUT=1
mkdir -p "$HOME/nuget_pkgs"
for project in "${lib.concatStringsSep "\" \"" ((lib.toList projectFile) ++ lib.optionals (testProjectFile != "") (lib.toList testProjectFile))}"; do
${dotnet-sdk}/bin/dotnet restore "$project" \
${lib.optionalString (!enableParallelBuilding) "--disable-parallel"} \
-p:ContinuousIntegrationBuild=true \
-p:Deterministic=true \
--packages "$HOME/nuget_pkgs" \
"''${dotnetRestoreFlags[@]}" \
"''${dotnetFlags[@]}"
done
echo "Writing lockfile..."
${nuget-to-nix}/bin/nuget-to-nix "$HOME/nuget_pkgs" > "$deps_file"
echo "Succesfully wrote lockfile to: $deps_file"
'';
} // args.passthru or {};
configurePhase = args.configurePhase or ''
runHook preConfigure
passthru = {
fetch-deps = writeScript "fetch-${pname}-deps" ''
set -euo pipefail
cd "$(dirname "''${BASH_SOURCE[0]}")"
export HOME=$(mktemp -d)
deps_file="/tmp/${pname}-deps.nix"
for project in ''${projectFile[@]} ''${testProjectFile[@]}; do
dotnet restore "$project" \
store_src="${args.src}"
src="$(mktemp -d /tmp/${pname}.XXX)"
cp -rT "$store_src" "$src"
chmod -R +w "$src"
trap "rm -rf $src $HOME" EXIT
pushd "$src"
export DOTNET_NOLOGO=1
export DOTNET_CLI_TELEMETRY_OPTOUT=1
mkdir -p "$HOME/nuget_pkgs"
for project in "${lib.concatStringsSep "\" \"" ((lib.toList projectFile) ++ lib.optionals (testProjectFile != "") (lib.toList testProjectFile))}"; do
${dotnet-sdk}/bin/dotnet restore "$project" \
${lib.optionalString (!enableParallelBuilding) "--disable-parallel"} \
-p:ContinuousIntegrationBuild=true \
-p:Deterministic=true \
--source "${nuget-source}/lib" \
"''${dotnetRestoreFlags[@]}" \
"''${dotnetFlags[@]}"
-p:RestoreUseStaticGraphEvaluation=true \
--packages "$HOME/nuget_pkgs" \
${lib.optionalString (dotnetRestoreFlags != []) (builtins.toString dotnetRestoreFlags)} \
${lib.optionalString (dotnetFlags != []) (builtins.toString dotnetFlags)}
done
runHook postConfigure
echo "Writing lockfile..."
${nuget-to-nix}/bin/nuget-to-nix "$HOME/nuget_pkgs" > "$deps_file"
echo "Succesfully wrote lockfile to: $deps_file"
'';
buildPhase = args.buildPhase or ''
runHook preBuild
for project in ''${projectFile[@]} ''${testProjectFile[@]}; do
dotnet build "$project" \
-maxcpucount:${if enableParallelBuilding then "$NIX_BUILD_CORES" else "1"} \
-p:BuildInParallel=${if enableParallelBuilding then "true" else "false"} \
-p:ContinuousIntegrationBuild=true \
-p:Deterministic=true \
-p:Version=${args.version} \
--configuration "$buildType" \
--no-restore \
"''${dotnetBuildFlags[@]}" \
"''${dotnetFlags[@]}"
done
runHook postBuild
'';
checkPhase = args.checkPhase or ''
runHook preCheck
for project in ''${testProjectFile[@]}; do
${lib.getBin dotnet-test-sdk}/bin/dotnet test "$project" \
-maxcpucount:${if enableParallelBuilding then "$NIX_BUILD_CORES" else "1"} \
-p:ContinuousIntegrationBuild=true \
-p:Deterministic=true \
--configuration "$buildType" \
--no-build \
--logger "console;verbosity=normal" \
${lib.optionalString (disabledTests != []) "--filter \"FullyQualifiedName!=${lib.concatStringsSep "&FullyQualifiedName!=" disabledTests}\""} \
"''${dotnetTestFlags[@]}" \
"''${dotnetFlags[@]}"
done
runHook postCheck
'';
installPhase = args.installPhase or ''
runHook preInstall
for project in ''${projectFile[@]}; do
dotnet publish "$project" \
-p:ContinuousIntegrationBuild=true \
-p:Deterministic=true \
--output $out/lib/${args.pname} \
--configuration "$buildType" \
--no-build \
--no-self-contained \
"''${dotnetInstallFlags[@]}" \
"''${dotnetFlags[@]}"
done
'' + lib.optionalString packNupkg ''
for project in ''${projectFile[@]}; do
dotnet pack "$project" \
-p:ContinuousIntegrationBuild=true \
-p:Deterministic=true \
--output $out/share \
--configuration "$buildType" \
--no-build \
"''${dotnetPackFlags[@]}" \
"''${dotnetFlags[@]}"
done
'' + ''
runHook postInstall
'';
preFixup = ''
_wrapDotnetProgram() {
makeWrapper "$1" "$out/bin/$(basename "$executable")" \
--set DOTNET_ROOT "${dotnet-runtime}" \
--suffix LD_LIBRARY_PATH : "${lib.makeLibraryPath runtimeDeps}" \
"''${gappsWrapperArgs[@]}" \
"''${makeWrapperArgs[@]}"
}
'' + (if executables != null then ''
for executable in ''${executables[@]}; do
execPath="$out/lib/${args.pname}/$executable"
if [[ -f "$execPath" && -x "$execPath" ]]; then
_wrapDotnetProgram $execPath
else
echo "Specified binary \"$executable\" is either not an executable, or does not exist!"
exit 1
fi
done
'' else ''
for executable in $out/lib/${args.pname}/*; do
if [[ -f "$executable" && -x "$executable" && "$executable" != *"dll"* ]]; then
_wrapDotnetProgram $executable
fi
done
'');
});
in
package
} // args.passthru or {};
})

View File

@ -0,0 +1,62 @@
{ lib
, callPackage
, makeSetupHook
, makeWrapper
, dotnet-sdk
, dotnet-test-sdk
, disabledTests
, nuget-source
, dotnet-runtime
, runtimeDeps
, buildType
}:
{
dotnetConfigureHook = callPackage ({ }:
makeSetupHook {
name = "dotnet-configure-hook";
deps = [ dotnet-sdk nuget-source ];
substitutions = {
nugetSource = nuget-source;
};
} ./dotnet-configure-hook.sh) { };
dotnetBuildHook = callPackage ({ }:
makeSetupHook {
name = "dotnet-build-hook";
deps = [ dotnet-sdk ];
substitutions = {
inherit buildType;
};
} ./dotnet-build-hook.sh) { };
dotnetCheckHook = callPackage ({ }:
makeSetupHook {
name = "dotnet-check-hook";
deps = [ dotnet-test-sdk ];
substitutions = {
inherit buildType;
disabledTests = lib.optionalString (disabledTests != [])
(lib.concatStringsSep "&FullyQualifiedName!=" disabledTests);
};
} ./dotnet-check-hook.sh) { };
dotnetInstallHook = callPackage ({ }:
makeSetupHook {
name = "dotnet-install-hook";
deps = [ dotnet-sdk ];
substitutions = {
inherit buildType;
};
} ./dotnet-install-hook.sh) { };
dotnetFixupHook = callPackage ({ }:
makeSetupHook {
name = "dotnet-fixup-hook";
deps = [ dotnet-runtime makeWrapper ];
substitutions = {
dotnetRuntime = dotnet-runtime;
runtimeDeps = lib.makeLibraryPath runtimeDeps;
};
} ./dotnet-fixup-hook.sh) { };
}

View File

@ -0,0 +1,41 @@
declare -a projectFile testProjectFile dotnetBuildFlags dotnetFlags
dotnetBuildHook() {
echo "Executing dotnetBuildHook"
runHook preBuild
if [ "${enableParallelBuilding-}" ]; then
maxCpuFlag="$NIX_BUILD_CORES"
parallelBuildFlag="true"
else
maxCpuFlag="1"
parallelBuildFlag="false"
fi
if [ "${version-}" ]; then
versionFlag="-p:Version=${version-}"
fi
for project in ${projectFile[@]} ${testProjectFile[@]}; do
env \
dotnet build "$project" \
-maxcpucount:$maxCpuFlag \
-p:BuildInParallel=$parallelBuildFlag \
-p:ContinuousIntegrationBuild=true \
-p:Deterministic=true \
--configuration "@buildType@" \
--no-restore \
${versionFlag-} \
"${dotnetBuildFlags[@]}" \
"${dotnetFlags[@]}"
done
runHook postBuild
echo "Finished dotnetBuildHook"
}
if [[ -z "${dontDotnetBuild-}" && -z "${buildPhase-}" ]]; then
buildPhase=dotnetBuildHook
fi

View File

@ -0,0 +1,33 @@
declare -a testProjectFile dotnetTestFlags dotnetFlags
dotnetCheckHook() {
echo "Executing dotnetCheckHook"
runHook preCheck
if [ "${disabledTests-}" ]; then
disabledTestsFlag="--filter FullyQualifiedName!=@disabledTests@"
fi
for project in ${testProjectFile[@]}; do
env \
dotnet test "$project" \
-maxcpucount:$maxCpuFlag \
-p:ContinuousIntegrationBuild=true \
-p:Deterministic=true \
--configuration "@buildType@" \
--no-build \
--logger "console;verbosity=normal" \
${disabledTestsFlag-} \
"${dotnetTestFlags[@]}" \
"${dotnetFlags[@]}"
done
runHook postCheck
echo "Finished dotnetCheckHook"
}
if [[ -z "${dontDotnetCheck-}" && -z "${checkPhase-}" ]]; then
checkPhase=dotnetCheckHook
fi

View File

@ -0,0 +1,33 @@
declare -a projectFile testProjectFile dotnetRestoreFlags dotnetFlags
dotnetConfigureHook() {
echo "Executing dotnetConfigureHook"
runHook preConfigure
if [ -z "${enableParallelBuilding-}" ]; then
parallelFlag="--disable-parallel"
fi
export HOME=$(mktemp -d)
for project in ${projectFile[@]} ${testProjectFile[@]}; do
env \
dotnet restore "$project" \
-p:ContinuousIntegrationBuild=true \
-p:Deterministic=true \
-p:RestoreUseStaticGraphEvaluation=true \
--source "@nugetSource@/lib" \
${parallelFlag-} \
"${dotnetRestoreFlags[@]}" \
"${dotnetFlags[@]}"
done
runHook postConfigure
echo "Finished dotnetConfigureHook"
}
if [[ -z "${dontDotnetConfigure-}" && -z "${configurePhase-}" ]]; then
configurePhase=dotnetConfigureHook
fi

View File

@ -0,0 +1,42 @@
declare -a makeWrapperArgs gappsWrapperArgs
# First argument is the executable you want to wrap,
# the second is the destination for the wrapper.
wrapDotnetProgram() {
makeWrapper "$1" "$2" \
--set "DOTNET_ROOT" "@dotnetRuntime@" \
--suffix "LD_LIBRARY_PATH" : "@runtimeDeps@" \
"${gappsWrapperArgs[@]}" \
"${makeWrapperArgs[@]}"
echo "Installed wrapper to: "$2""
}
dotnetFixupHook() {
echo "Executing dotnetFixupPhase"
if [ "${executables-}" ]; then
for executable in ${executables[@]}; do
execPath="$out/lib/${pname-}/$executable"
if [[ -f "$execPath" && -x "$execPath" ]]; then
wrapDotnetProgram "$execPath" "$out/bin/$(basename "$executable")"
else
echo "Specified binary \"$executable\" is either not an executable, or does not exist!"
exit 1
fi
done
else
for executable in $out/lib/${pname-}/*; do
if [[ -f "$executable" && -x "$executable" && "$executable" != *"dll"* ]]; then
wrapDotnetProgram "$executable" "$out/bin/$(basename "$executable")"
fi
done
fi
echo "Finished dotnetFixupPhase"
}
if [[ -z "${dontDotnetFixup-}" ]]; then
preFixupPhases+=" dotnetFixupHook"
fi

View File

@ -0,0 +1,42 @@
declare -a projectFile dotnetInstallFlags dotnetFlags
dotnetInstallHook() {
echo "Executing dotnetInstallHook"
runHook preInstall
for project in ${projectFile[@]}; do
env \
dotnet publish "$project" \
-p:ContinuousIntegrationBuild=true \
-p:Deterministic=true \
--output "$out/lib/${pname}" \
--configuration "@buildType@" \
--no-build \
--no-self-contained \
"${dotnetInstallFlags[@]}" \
"${dotnetFlags[@]}"
done
if [[ "${packNupkg-}" ]]; then
for project in ${projectFile[@]}; do
env \
dotnet pack "$project" \
-p:ContinuousIntegrationBuild=true \
-p:Deterministic=true \
--output "$out/share" \
--configuration "@buildType@" \
--no-build \
"${dotnetPackFlags[@]}" \
"${dotnetFlags[@]}"
done
fi
runHook postInstall
echo "Finished dotnetInstallHook"
}
if [[ -z "${dontDotnetInstall-}" && -z "${installPhase-}" ]]; then
installPhase=dotnetInstallHook
fi