Skip to content

Commit

Permalink
buildDotnetModule: fix structured attributes support
Browse files Browse the repository at this point in the history
This change refactors internal hooks used by buildDotnetModule to
support derivations with structured attributes. Note that this changes
variable names that the internal hooks expect.
  • Loading branch information
tie committed May 29, 2024
1 parent 22a5fc0 commit f7d8046
Show file tree
Hide file tree
Showing 19 changed files with 440 additions and 176 deletions.
2 changes: 1 addition & 1 deletion pkgs/applications/misc/ArchiSteamFarm/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ buildDotnetModule rec {
doCheck = true;

preBuild = ''
export projectFile=(ArchiSteamFarm)
dotnetProjectFiles=(ArchiSteamFarm)
'';

preInstall = ''
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ buildDotnetModule (args // {
] ++ (nugetDeps fetchNuGet);
};

projectFile = "";
dotnetGlobalTool = true;

useDotnetFromEnv = true;

Expand Down
36 changes: 24 additions & 12 deletions pkgs/build-support/dotnet/build-dotnet-module/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
, disabledTests ? [ ]
# The project file to run unit tests against. This is usually referenced in the regular project file, but sometimes it needs to be manually set.
# It gets restored and build, but not installed. You may need to regenerate your nuget lockfile after setting this.
, testProjectFile ? ""
, testProjectFile ? null

# The type of build to perform. This is passed to `dotnet` with the `--configuration` flag. Possible values are `Release`, `Debug`, etc.
, buildType ? "Release"
Expand All @@ -88,17 +88,18 @@
} @ args:

let
projectFiles =
lib.optionals (projectFile != null) (lib.toList projectFile);
testProjectFiles =
lib.optionals (testProjectFile != null) (lib.toList testProjectFile);

platforms =
if args ? meta.platforms
then lib.intersectLists args.meta.platforms dotnet-sdk.meta.platforms
else dotnet-sdk.meta.platforms;

inherit (callPackage ./hooks {
inherit dotnet-sdk disabledTests nuget-source dotnet-runtime runtimeDeps buildType;
runtimeId =
if runtimeId != null
then runtimeId
else dotnetCorePackages.systemToDotnetRid stdenvNoCC.targetPlatform.system;
inherit dotnet-sdk dotnet-runtime;
}) dotnetConfigureHook dotnetBuildHook dotnetCheckHook dotnetInstallHook dotnetFixupHook;

localDeps =
Expand Down Expand Up @@ -143,6 +144,19 @@ let
nugetDepsFile = _nugetDeps.sourceFile;
in
stdenvNoCC.mkDerivation (args // {
dotnetInstallPath = installPath;
dotnetExecutables = executables;
dotnetBuildType = buildType;
dotnetProjectFiles = projectFiles;
dotnetTestProjectFiles = testProjectFiles;
dotnetDisabledTests = disabledTests;
dotnetRuntimeId = runtimeId;
nugetSource = nuget-source;
dotnetRuntimeDeps = map lib.getLib runtimeDeps;
dotnetSelfContainedBuild = selfContainedBuild;
dotnetUseAppHost = useAppHost;
inherit useDotnetFromEnv;

nativeBuildInputs = args.nativeBuildInputs or [ ] ++ [
dotnetConfigureHook
dotnetBuildHook
Expand Down Expand Up @@ -172,7 +186,7 @@ stdenvNoCC.mkDerivation (args // {
else [ ]));

makeWrapperArgs = args.makeWrapperArgs or [ ] ++ [
"--prefix LD_LIBRARY_PATH : ${dotnet-sdk.icu}/lib"
"--prefix" "LD_LIBRARY_PATH" ":" "${dotnet-sdk.icu}/lib"
];

# Stripping breaks the executable
Expand All @@ -181,8 +195,6 @@ stdenvNoCC.mkDerivation (args // {
# gappsWrapperArgs gets included when wrapping for dotnet, as to avoid double wrapping
dontWrapGApps = args.dontWrapGApps or true;

inherit selfContainedBuild useAppHost useDotnetFromEnv;

# propagate the runtime sandbox profile since the contents apply to published
# executables
propagatedSandboxProfile = toString dotnet-runtime.__propagatedSandboxProfile;
Expand Down Expand Up @@ -267,11 +279,11 @@ stdenvNoCC.mkDerivation (args // {
--no-cache \
--force \
${lib.optionalString (!enableParallelBuilding) "--disable-parallel"} \
${lib.optionalString (flags != []) (toString flags)}
${lib.escapeShellArgs flags}
}
declare -a projectFiles=( ${toString (lib.toList projectFile)} )
declare -a testProjectFiles=( ${toString (lib.toList testProjectFile)} )
declare -a projectFiles=( ${lib.escapeShellArgs projectFiles} )
declare -a testProjectFiles=( ${lib.escapeShellArgs testProjectFiles} )
export DOTNET_NOLOGO=1
export DOTNET_CLI_TELEMETRY_OPTOUT=1
Expand Down
36 changes: 8 additions & 28 deletions pkgs/build-support/dotnet/build-dotnet-module/hooks/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,21 @@
, coreutils
, zlib
, openssl
, callPackage
, makeSetupHook
, makeWrapper
, dotnetCorePackages
# Passed from ../default.nix
, dotnet-sdk
, disabledTests
, nuget-source
, dotnet-runtime
, runtimeDeps
, buildType
, runtimeId
}:
assert (builtins.isString runtimeId);

let
libraryPath = lib.makeLibraryPath runtimeDeps;
runtimeId = dotnetCorePackages.systemToDotnetRid stdenv.hostPlatform.system;
in
{
dotnetConfigureHook = makeSetupHook
{
name = "dotnet-configure-hook";
substitutions = {
nugetSource = nuget-source;
runtimeId = lib.escapeShellArg runtimeId;
dynamicLinker = "${stdenv.cc}/nix-support/dynamic-linker";
libPath = lib.makeLibraryPath [
stdenv.cc.cc.lib
Expand All @@ -34,7 +27,6 @@ in
zlib
openssl
];
inherit runtimeId;
};
}
./dotnet-configure-hook.sh;
Expand All @@ -43,7 +35,7 @@ in
{
name = "dotnet-build-hook";
substitutions = {
inherit buildType runtimeId;
runtimeId = lib.escapeShellArg runtimeId;
};
}
./dotnet-build-hook.sh;
Expand All @@ -52,15 +44,7 @@ in
{
name = "dotnet-check-hook";
substitutions = {
inherit buildType runtimeId libraryPath;
disabledTests = lib.optionalString (disabledTests != [ ])
(
let
escapedNames = lib.lists.map (n: lib.replaceStrings [ "," ] [ "%2C" ] n) disabledTests;
filters = lib.lists.map (n: "FullyQualifiedName!=${n}") escapedNames;
in
"${lib.concatStringsSep "&" filters}"
);
runtimeId = lib.escapeShellArg runtimeId;
};
}
./dotnet-check-hook.sh;
Expand All @@ -69,7 +53,7 @@ in
{
name = "dotnet-install-hook";
substitutions = {
inherit buildType runtimeId;
runtimeId = lib.escapeShellArg runtimeId;
};
}
./dotnet-install-hook.sh;
Expand All @@ -79,11 +63,7 @@ in
name = "dotnet-fixup-hook";
substitutions = {
dotnetRuntime = dotnet-runtime;
runtimeDeps = libraryPath;
shell = stdenv.shell;
which = "${which}/bin/which";
dirname = "${coreutils}/bin/dirname";
realpath = "${coreutils}/bin/realpath";
wrapperPath = lib.makeBinPath [ which coreutils ];
};
}
./dotnet-fixup-hook.sh;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,70 +1,86 @@
# inherit arguments from derivation
dotnetBuildFlags=( ${dotnetBuildFlags[@]-} )

dotnetBuildHook() {
echo "Executing dotnetBuildHook"

runHook preBuild

if [ "${enableParallelBuilding-}" ]; then
local -r hostRuntimeId=@runtimeId@
local -r dotnetBuildType="${dotnetBuildType-Release}"
local -r dotnetRuntimeId="${dotnetRuntimeId-$hostRuntimeId}"

if [[ -n $__structuredAttrs ]]; then
local dotnetProjectFilesArray=( "${dotnetProjectFiles[@]}" )
local dotnetTestProjectFilesArray=( "${dotnetTestProjectFiles[@]}" )
local dotnetFlagsArray=( "${dotnetFlags[@]}" )
local dotnetBuildFlagsArray=( "${dotnetBuildFlags[@]}" )
else
local dotnetProjectFilesArray=($dotnetProjectFiles)
local dotnetTestProjectFilesArray=($dotnetTestProjectFiles)
local dotnetFlagsArray=($dotnetFlags)
local dotnetBuildFlagsArray=($dotnetBuildFlags)
fi

if [[ -n "${enableParallelBuilding-}" ]]; then
local -r maxCpuFlag="$NIX_BUILD_CORES"
local -r parallelBuildFlag="true"
else
local -r maxCpuFlag="1"
local -r parallelBuildFlag="false"
fi

if [ "${selfContainedBuild-}" ]; then
dotnetBuildFlags+=("-p:SelfContained=true")
if [[ -n ${dotnetSelfContainedBuild-} ]]; then
dotnetBuildFlagsArray+=("-p:SelfContained=true")
else
dotnetBuildFlags+=("-p:SelfContained=false")
dotnetBuildFlagsArray+=("-p:SelfContained=false")
fi

if [ "${useAppHost-}" ]; then
dotnetBuildFlags+=("-p:UseAppHost=true")
if [[ -n ${dotnetUseAppHost-} ]]; then
dotnetBuildFlagsArray+=("-p:UseAppHost=true")
fi

local versionFlags=()
if [ "${version-}" ]; then
versionFlags+=("-p:InformationalVersion=${version-}")
local versionFlagsArray=()
if [[ -n ${version-} ]]; then
versionFlagsArray+=("-p:InformationalVersion=$version")
fi

if [ "${versionForDotnet-}" ]; then
versionFlags+=("-p:Version=${versionForDotnet-}")
if [[ -n ${versionForDotnet-} ]]; then
versionFlagsArray+=("-p:Version=$versionForDotnet")
fi

dotnetBuild() {
local -r project="${1-}"
local -r projectFile="${1-}"

runtimeIdFlags=()
if [[ "$project" == *.csproj ]] || [ "${selfContainedBuild-}" ]; then
runtimeIdFlags+=("--runtime @runtimeId@")
local runtimeIdFlagsArray=()
if [[ $projectFile == *.csproj || -n ${dotnetSelfContainedBuild-} ]]; then
runtimeIdFlagsArray+=("--runtime" "$dotnetRuntimeId")
fi

dotnet build ${project-} \
-maxcpucount:$maxCpuFlag \
-p:BuildInParallel=$parallelBuildFlag \
dotnet build ${1+"$projectFile"} \
-maxcpucount:"$maxCpuFlag" \
-p:BuildInParallel="$parallelBuildFlag" \
-p:ContinuousIntegrationBuild=true \
-p:Deterministic=true \
--configuration "@buildType@" \
--configuration "$dotnetBuildType" \
--no-restore \
${versionFlags[@]} \
${runtimeIdFlags[@]} \
${dotnetBuildFlags[@]} \
${dotnetFlags[@]}
"${versionFlagsArray[@]}" \
"${runtimeIdFlagsArray[@]}" \
"${dotnetBuildFlagsArray[@]}" \
"${dotnetFlagsArray[@]}"
}

(( "${#projectFile[@]}" == 0 )) && dotnetBuild
if (( ${#dotnetProjectFilesArray[@]} == 0 )); then
dotnetBuild
fi

for project in ${projectFile[@]} ${testProjectFile[@]-}; do
dotnetBuild "$project"
local projectFile
for projectFile in "${dotnetProjectFilesArray[@]}" "${dotnetTestProjectFilesArray[@]}"; do
dotnetBuild "$projectFile"
done

runHook postBuild

echo "Finished dotnetBuildHook"
}

if [[ -z "${dontDotnetBuild-}" && -z "${buildPhase-}" ]]; then
if [[ -z ${dontDotnetBuild-} && -z ${buildPhase-} ]]; then
buildPhase=dotnetBuildHook
fi
Original file line number Diff line number Diff line change
@@ -1,39 +1,65 @@
# inherit arguments from derivation
dotnetTestFlags=( ${dotnetTestFlags[@]-} )

dotnetCheckHook() {
echo "Executing dotnetCheckHook"

runHook preCheck

if [ "${disabledTests-}" ]; then
local -r disabledTestsFlag="--filter @disabledTests@"
local -r hostRuntimeId=@runtimeId@
local -r dotnetBuildType="${dotnetBuildType-Release}"
local -r dotnetRuntimeId="${dotnetRuntimeId-$hostRuntimeId}"

if [[ -n $__structuredAttrs ]]; then
local dotnetProjectFilesArray=( "${dotnetProjectFiles[@]}" )
local dotnetTestProjectFilesArray=( "${dotnetTestProjectFiles[@]}" )
local dotnetTestFlagsArray=( "${dotnetTestFlags[@]}" )
local dotnetDisabledTestsArray=( "${dotnetDisabledTests[@]}" )
local dotnetRuntimeDepsArray=( "${dotnetRuntimeDeps[@]}" )
else
local dotnetProjectFilesArray=($dotnetProjectFiles)
local dotnetTestProjectFilesArray=($dotnetTestProjectFiles)
local dotnetTestFlagsArray=($dotnetTestFlags)
local dotnetDisabledTestsArray=($dotnetDisabledTests)
local dotnetRuntimeDepsArray=($dotnetRuntimeDeps)
fi

if (( ${#dotnetDisabledTestsArray[@]} > 0 )); then
local disabledTestsFilters=("${dotnetDisabledTestsArray[@]/#/FullyQualifiedName!=}")
local OLDIFS="$IFS" IFS='&'
dotnetTestFlagsArray+=("--filter:${disabledTestsFilters[*]//,/%2C}")
IFS="$OLDIFS"
fi

local libraryPath="${LD_LIBRARY_PATH-}"
if (( ${#dotnetRuntimeDepsArray[@]} > 0 )); then
local libraryPathArray=("${dotnetRuntimeDepsArray[@]/%//lib}")
local OLDIFS="$IFS" IFS=':'
libraryPath="${libraryPathArray[*]}${libraryPath:+':'}$libraryPath"
IFS="$OLDIFS"
fi

if [ "${enableParallelBuilding-}" ]; then
if [[ -n ${enableParallelBuilding-} ]]; then
local -r maxCpuFlag="$NIX_BUILD_CORES"
else
local -r maxCpuFlag="1"
fi

for project in ${testProjectFile[@]-${projectFile[@]}}; do
runtimeIdFlags=()
if [[ "$project" == *.csproj ]]; then
runtimeIdFlags=("--runtime @runtimeId@")
local projectFile
for projectFile in "${dotnetTestProjectFilesArray[@]-${dotnetProjectFilesArray[@]}}"; do
local runtimeIdFlagsArray=()
if [[ $projectFile == *.csproj ]]; then
runtimeIdFlagsArray=("--runtime" "$dotnetRuntimeId")
fi

LD_LIBRARY_PATH="@libraryPath@" \
dotnet test "$project" \
-maxcpucount:$maxCpuFlag \
LD_LIBRARY_PATH=$libraryPath \
dotnet test "$projectFile" \
-maxcpucount:"$maxCpuFlag" \
-p:ContinuousIntegrationBuild=true \
-p:Deterministic=true \
--configuration "@buildType@" \
--configuration "$dotnetBuildType" \
--no-build \
--logger "console;verbosity=normal" \
${disabledTestsFlag-} \
${runtimeIdFlags[@]} \
"${dotnetTestFlags[@]}" \
"${dotnetFlags[@]}"
"${runtimeIdFlagsArray[@]}" \
"${dotnetTestFlagsArray[@]}" \
"${dotnetFlagsArray[@]}"
done

runHook postCheck
Expand Down
Loading

0 comments on commit f7d8046

Please sign in to comment.