core/pkgs/build-support/dotnet/build-dotnet-package/default.nix

109 lines
2.9 KiB
Nix
Raw Normal View History

2024-05-02 00:46:19 +00:00
{ stdenv, lib, makeWrapper, pkg-config, mono, dotnetbuildhelpers }:
2024-05-13 21:24:10 +00:00
attrsOrig@{ pname, version, nativeBuildInputs ? [ ], xBuildFiles ? [ ]
2024-05-02 00:46:19 +00:00
, xBuildFlags ? [ "/p:Configuration=Release" ]
2024-05-13 21:24:10 +00:00
, outputFiles ? [ "bin/Release/*" ], dllFiles ? [ "*.dll" ], exeFiles ? [
"*.exe"
]
2024-05-02 00:46:19 +00:00
# Additional arguments to pass to the makeWrapper function, which wraps
# generated binaries.
2024-05-13 21:24:10 +00:00
, makeWrapperArgs ? [ ], ... }:
let
arrayToShell =
(a: toString (map (lib.escape (lib.stringToCharacters "\\ ';$`()|<> ")) a));
2024-05-02 00:46:19 +00:00
2024-05-13 21:24:10 +00:00
attrs = {
inherit pname version;
2024-05-02 00:46:19 +00:00
2024-05-13 21:24:10 +00:00
nativeBuildInputs = [ pkg-config makeWrapper dotnetbuildhelpers mono ]
++ nativeBuildInputs;
2024-05-02 00:46:19 +00:00
2024-05-13 21:24:10 +00:00
configurePhase = ''
runHook preConfigure
2024-05-02 00:46:19 +00:00
2024-05-13 21:24:10 +00:00
[ -z "''${dontPlacateNuget-}" ] && placate-nuget.sh
[ -z "''${dontPlacatePaket-}" ] && placate-paket.sh
[ -z "''${dontPatchFSharpTargets-}" ] && patch-fsharp-targets.sh
2024-05-02 00:46:19 +00:00
2024-05-13 21:24:10 +00:00
runHook postConfigure
'';
2024-05-02 00:46:19 +00:00
2024-05-13 21:24:10 +00:00
buildPhase = ''
runHook preBuild
2024-05-02 00:46:19 +00:00
2024-05-13 21:24:10 +00:00
echo Building dotNET packages...
2024-05-02 00:46:19 +00:00
2024-05-13 21:24:10 +00:00
# Probably needs to be moved to fsharp
if pkg-config FSharp.Core
then
export FSharpTargetsPath="$(dirname $(pkg-config FSharp.Core --variable=Libraries))/Microsoft.FSharp.Targets"
fi
2024-05-02 00:46:19 +00:00
2024-05-13 21:24:10 +00:00
ran=""
for xBuildFile in ${arrayToShell xBuildFiles} ''${xBuildFilesExtra}
do
ran="yes"
xbuild ${arrayToShell xBuildFlags} ''${xBuildFlagsArray} $xBuildFile
done
2024-05-02 00:46:19 +00:00
2024-05-13 21:24:10 +00:00
[ -z "$ran" ] && xbuild ${arrayToShell xBuildFlags} ''${xBuildFlagsArray}
2024-05-02 00:46:19 +00:00
2024-05-13 21:24:10 +00:00
runHook postBuild
'';
2024-05-02 00:46:19 +00:00
2024-05-13 21:24:10 +00:00
dontStrip = true;
2024-05-02 00:46:19 +00:00
2024-05-13 21:24:10 +00:00
installPhase = ''
runHook preInstall
2024-05-02 00:46:19 +00:00
2024-05-13 21:24:10 +00:00
target="$out/lib/dotnet/${pname}"
mkdir -p "$target"
2024-05-02 00:46:19 +00:00
2024-05-13 21:24:10 +00:00
cp -rv ${arrayToShell outputFiles} "''${outputFilesArray[@]}" "$target"
2024-05-02 00:46:19 +00:00
2024-05-13 21:24:10 +00:00
if [ -z "''${dontRemoveDuplicatedDlls-}" ]
then
pushd "$out"
remove-duplicated-dlls.sh
popd
fi
2024-05-02 00:46:19 +00:00
2024-05-13 21:24:10 +00:00
set -f
for dllPattern in ${arrayToShell dllFiles} ''${dllFilesArray[@]}
do
set +f
for dll in "$target"/$dllPattern
2024-05-02 00:46:19 +00:00
do
2024-05-13 21:24:10 +00:00
[ -f "$dll" ] || continue
if pkg-config $(basename -s .dll "$dll")
then
echo "$dll already exported by a buildInputs, not re-exporting"
else
create-pkg-config-for-dll.sh "$out/lib/pkgconfig" "$dll"
fi
2024-05-02 00:46:19 +00:00
done
2024-05-13 21:24:10 +00:00
done
2024-05-02 00:46:19 +00:00
2024-05-13 21:24:10 +00:00
set -f
for exePattern in ${arrayToShell exeFiles} ''${exeFilesArray[@]}
do
set +f
for exe in "$target"/$exePattern
2024-05-02 00:46:19 +00:00
do
2024-05-13 21:24:10 +00:00
[ -f "$exe" ] || continue
mkdir -p "$out"/bin
commandName="$(basename -s .exe "$(echo "$exe" | tr "[A-Z]" "[a-z]")")"
makeWrapper \
"${mono}/bin/mono" \
"$out"/bin/"$commandName" \
--add-flags "\"$exe\"" \
''${makeWrapperArgs}
2024-05-02 00:46:19 +00:00
done
2024-05-13 21:24:10 +00:00
done
2024-05-02 00:46:19 +00:00
2024-05-13 21:24:10 +00:00
runHook postInstall
'';
};
in stdenv.mkDerivation
(attrs // (builtins.removeAttrs attrsOrig [ "nativeBuildInputs" ]))