summaryrefslogtreecommitdiffstats
path: root/pkgs/development/r-modules/generic-builder.nix
blob: e05b735d79010ea8498e1d6fdc500742830a423f (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
126
127
128
{
  stdenv,
  lib,
  R,
  xvfb-run,
  util-linux,
  libintl,
}:

attrs:

stdenv.mkDerivation (
  finalAttrs:
  {
    __structuredAttrs = true;
    strictDeps = true;

    name = "r-${attrs.name or "${attrs.pname}-${attrs.version}"}";

    requireX = false;

    nativeBuildInputs =
      (attrs.nativeBuildInputs or [ ])
      ++ [
        R
      ]
      ++ lib.optionals finalAttrs.requireX [
        util-linux
        xvfb-run
      ];

    buildInputs =
      (attrs.buildInputs or [ ])
      ++ lib.optionals stdenv.hostPlatform.isDarwin [
        libintl
      ];

    enableParallelBuilding = true;

    env =
      (attrs.env or { })
      // (lib.optionalAttrs (stdenv.hostPlatform.isDarwin) {
        NIX_CFLAGS_COMPILE = "${
          attrs.env.NIX_CFLAGS_COMPILE or ""
        } -I${lib.getInclude stdenv.cc.libcxx}/include/c++/v1";

      });

    configurePhase = ''
      runHook preConfigure

      export MAKEFLAGS+="''${enableParallelBuilding:+-j$NIX_BUILD_CORES}"
      export R_LIBS_SITE="$R_LIBS_SITE''${R_LIBS_SITE:+:}$out/library"

      if [ -f ./configure ] && [ -z "''${dontPatchShebangsInConfigure:-}" ]; then
        patchShebangs --build ./configure
      fi

      runHook postConfigure
    '';

    buildPhase = ''
      runHook preBuild
      runHook postBuild
    '';

    doCheck = true;

    checkPhase = ''
      # noop since R CMD INSTALL tests packages
    '';

    rCommand =
      if finalAttrs.requireX then
        # Unfortunately, xvfb-run has a race condition even with -a option, so that
        # we acquire a lock explicitly.
        "flock ${xvfb-run} xvfb-run -a -e xvfb-error R"
      else
        "R";

    installFlags = (attrs.installFlags or [ ]) ++ (lib.optional (!finalAttrs.doCheck) "--no-test-load");

    installPhase = ''
      runHook preInstall

      mkdir -p "$out/library"

      # logic inside R CMD INSTALL essentially just expands `./configure $configureArgs` and runs it in a system shell
      # so we need to escape configureFlags if we want to support things like spaces in arguments
      local configureArgs=""
      if ((''${#configureFlags[@]})); then
        printf -v configureArgs '%q ' "''${configureFlags[@]}"
      fi

      $rCommand CMD INSTALL \
        --library="$out/library" \
        --built-timestamp='1970-01-01 00:00:00 UTC' \
        --configure-args="$configureArgs" \
        "''${installFlags[@]}" \
        .

      runHook postInstall
    '';

    stripDebugList = [
      "library/${attrs.pname}/libs"
      # Note: this is non-standard, but some packages do place binaries here via custom install logic (e.g. via install.libs.R)
      "library/${attrs.pname}/bin"
    ];

    postFixup = ''
      if test -e $out/nix-support/propagated-build-inputs; then
        ln -s $out/nix-support/propagated-build-inputs $out/nix-support/propagated-user-env-packages
      fi
    ''
    + (attrs.postFixup or "");
  }
  // (lib.removeAttrs attrs [
    # list of attrs that have custom logic for combining the default and the passed values
    # if not listed, the passed value will override the default value
    "name"
    "nativeBuildInputs"
    "buildInputs"
    "env"
    "installFlags"
    "postFixup"
  ])
)