summaryrefslogtreecommitdiffstats
path: root/pkgs/build-support/dotnet/build-dotnet-package/default.nix
blob: 803dc2c7ddfeb59769488a7b46be514b285f2b23 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
{
  stdenv,
  lib,
  makeWrapper,
  pkg-config,
  mono,
  dotnetbuildhelpers,
}:

attrsOrig@{
  pname,
  version,
  nativeBuildInputs ? [ ],
  xBuildFiles ? [ ],
  xBuildFlags ? [ "/p:Configuration=Release" ],
  outputFiles ? [ "bin/Release/*" ],
  dllFiles ? [ "*.dll" ],
  exeFiles ? [ "*.exe" ],
  # Additional arguments to pass to the makeWrapper function, which wraps
  # generated binaries.
  makeWrapperArgs ? [ ],
  ...
}:
let
  arrayToShell = (a: toString (map (lib.escape (lib.stringToCharacters "\\ ';$`()|<>\t")) a));

  attrs = {
    inherit pname version;

    nativeBuildInputs = [
      pkg-config
      makeWrapper
      dotnetbuildhelpers
      mono
    ]
    ++ nativeBuildInputs;

    configurePhase = ''
      runHook preConfigure

      [ -z "''${dontPlacateNuget-}" ] && placate-nuget.sh
      [ -z "''${dontPlacatePaket-}" ] && placate-paket.sh
      [ -z "''${dontPatchFSharpTargets-}" ] && patch-fsharp-targets.sh

      runHook postConfigure
    '';

    buildPhase = ''
      runHook preBuild

      echo Building dotNET packages...

      # 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

      ran=""
      for xBuildFile in ${arrayToShell xBuildFiles} ''${xBuildFilesExtra}
      do
        ran="yes"
        xbuild ${arrayToShell xBuildFlags} ''${xBuildFlagsArray} $xBuildFile
      done

      [ -z "$ran" ] && xbuild ${arrayToShell xBuildFlags} ''${xBuildFlagsArray}

      runHook postBuild
    '';

    dontStrip = true;

    installPhase = ''
      runHook preInstall

      target="$out/lib/dotnet/${pname}"
      mkdir -p "$target"

      cp -rv ${arrayToShell outputFiles} "''${outputFilesArray[@]}" "$target"

      if [ -z "''${dontRemoveDuplicatedDlls-}" ]
      then
        pushd "$out"
        remove-duplicated-dlls.sh
        popd
      fi

      set -f
      for dllPattern in ${arrayToShell dllFiles} ''${dllFilesArray[@]}
      do
        set +f
        for dll in "$target"/$dllPattern
        do
          [ -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
        done
      done

      set -f
      for exePattern in ${arrayToShell exeFiles} ''${exeFilesArray[@]}
      do
        set +f
        for exe in "$target"/$exePattern
        do
          [ -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}
        done
      done

      runHook postInstall
    '';
  };
in
stdenv.mkDerivation (attrs // (removeAttrs attrsOrig [ "nativeBuildInputs" ]))