summaryrefslogtreecommitdiff
path: root/pkgs/development/compilers/factor-lang/wrapper.nix
blob: 5f8e4ff37020313d0df0ef9f903a07452741da64 (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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
{
  lib,
  stdenv,
  makeWrapper,
  buildEnv,
  copyDesktopItems,
  makeDesktopItem,
  factor-unwrapped,
  cairo,
  freealut,
  gdk-pixbuf,
  glib,
  gnome2,
  gtk2-x11,
  libGL,
  libGLU,
  librsvg,
  graphviz,
  libogg,
  libvorbis,
  openal,
  openssl,
  pango,
  pcre,
  udis86,
  zlib,
  # Enable factor GUI support
  guiSupport ? true,
  # Libraries added to ld.so.cache
  extraLibs ? [ ],
  # Packages added to path (and ld.so.cache)
  binPackages ? [ ],
  # Extra vocabularies added to out/lib/factor
  extraVocabs ? [ ],
  # Enable default libs and bins to run most of the standard library code.
  enableDefaults ? true,
  doInstallCheck ? true,
}:
let
  inherit (lib)
    optional
    optionals
    optionalString
    versionOlder
    versionAtLeast
    ;
  # missing from lib/strings
  escapeNixString = s: lib.escape [ "$" ] (builtins.toJSON s);
  toFactorArgs = x: lib.concatStringsSep " " (map escapeNixString x);
  defaultLibs = optionals enableDefaults [
    libogg
    libvorbis
    openal
    openssl
    pcre
    udis86
    zlib
  ];
  defaultBins = optionals enableDefaults [ graphviz ];
  runtimeLibs =
    defaultLibs
    ++ extraLibs
    ++ binPackages
    ++ (lib.flatten (map (v: v.extraLibs or [ ]) extraVocabs))
    ++ optionals guiSupport [
      cairo
      freealut
      gdk-pixbuf
      glib
      gnome2.gtkglext
      gtk2-x11
      libGL
      libGLU
      pango
    ];
  bins = binPackages ++ defaultBins ++ (lib.flatten (map (v: v.extraPaths or [ ]) extraVocabs));
  vocabTree = buildEnv {
    name = "${factor-unwrapped.pname}-vocabs";
    ignoreCollisions = true;
    pathsToLink = map (r: "/lib/factor/${r}") [
      "basis"
      "core"
      "extra"
    ];
    paths = [ factor-unwrapped ] ++ extraVocabs;
  };

in
stdenv.mkDerivation (finalAttrs: {
  pname = "${factor-unwrapped.pname}-env";
  inherit (factor-unwrapped) version;

  nativeBuildInputs = [
    copyDesktopItems
    makeWrapper
  ];
  buildInputs = optional guiSupport gdk-pixbuf;

  dontUnpack = true;

  desktopItems = [
    (makeDesktopItem {
      name = "Factor";
      desktopName = "Factor";
      comment = "A practical stack language";
      exec = "factor";
      icon = "factor";
      categories = [
        "Development"
        "IDE"
      ];
    })
  ];

  installPhase = ''
    runHook preInstall
  ''
  + optionalString guiSupport (
    ''
      # Set Gdk pixbuf loaders file to the one from the build dependencies here
      unset GDK_PIXBUF_MODULE_FILE
      # Defined in gdk-pixbuf setup hook
      findGdkPixbufLoaders "${librsvg}"
      makeWrapperArgs+=(--set GDK_PIXBUF_MODULE_FILE "$GDK_PIXBUF_MODULE_FILE")
    ''
    + optionalString (versionAtLeast finalAttrs.version "0.101") ''
      mkdir -p $out/share/applications $out/share/icons/hicolor/scalable/apps
      cp ${factor-unwrapped}/lib/factor/misc/icons/icon.svg $out/share/icons/hicolor/scalable/apps/factor.svg
      cp ${factor-unwrapped}/lib/factor/misc/icons/icon.ico $out/share/icons/hicolor/scalable/apps/factor.ico
      for i in 16 32 48 64 128 256 512; do
          mkdir -p $out/share/icons/hicolor/''${i}x''${i}/apps
          cp ${factor-unwrapped}/lib/factor/misc/icons/icon_''${i}x''${i}.png $out/share/icons/hicolor/''${i}x''${i}/apps/factor.png
      done
    ''
  )
  + ''
    makeWrapperArgs+=(
      --prefix LD_LIBRARY_PATH : /run/opengl-driver/lib:${lib.makeLibraryPath runtimeLibs}
      --prefix PATH : ${lib.makeBinPath bins})
    mkdir -p "$out/bin"
    cp -r "${factor-unwrapped}/lib" "$out/"
    chmod -R u+w "$out/lib"
    rm -rf "$out/lib/factor/misc"
    (
        cd ${vocabTree}
        for f in "lib/factor/"* ; do
            rm -r "$out/$f"
            cp -rL "${vocabTree}/$f" "$out/$f"
        done
    )

    # There is no ld.so.cache in NixOS so we construct one
    # out of known libraries. The side effect is that find-lib
    # will work only on the known libraries. There does not seem
    # to be a generic solution here.
    find $(echo ${
      lib.makeLibraryPath ([ stdenv.cc.libc ] ++ runtimeLibs)
    } | sed -e 's#:# #g') -name \*.so.\* > $TMPDIR/so.lst
    (echo $(cat $TMPDIR/so.lst | wc -l) "libs found in cache \`/etc/ld.so.cache'";
      for l in $(<$TMPDIR/so.lst); do
        echo " $(basename $l) (libc6,x86-64) => $l";
      done)> $out/lib/factor/ld.so.cache

    # Create a wrapper in bin/ and lib/factor/
    wrapProgram "$out/lib/factor/factor" \
      --argv0 factor \
      --set FACTOR_LD_SO_CACHE "$out/lib/factor/ld.so.cache" \
      "''${makeWrapperArgs[@]}"
    mv $out/lib/factor/factor.image $out/lib/factor/.factor-wrapped.image
    cp $out/lib/factor/factor $out/bin/

    # Emacs fuel expects the image being named `factor.image` in the factor base dir
    ln -rs $out/lib/factor/.factor-wrapped.image $out/lib/factor/factor.image

  ''
  + optionalString (versionOlder finalAttrs.version "0.101") ''
    mkdir -p "$out/share/emacs/site-lisp"
    cp -r "${factor-unwrapped}/lib/factor/misc/fuel/"*.el "$out/share/emacs/site-lisp"
    chmod -R u+wr "$out/share/emacs"
    # Update default paths in fuel-listener.el to new output
    sed -E -i -e 's#(\(defcustom fuel-factor-root-dir ").*(")#'"\1$out/lib/factor\2#" \
        "$out/share/emacs/site-lisp/fuel-listener.el"
  ''
  + ''
    runHook postInstall
  '';

  inherit doInstallCheck;
  disabledTests = toFactorArgs [
    "io.files.info.unix"
    "io.launcher.unix"
    "io.ports"
    "io.sockets"
    "io.sockets.unix"
    "io.sockets.secure.openssl"
    "io.sockets.secure.unix"
  ];
  installCheckPhase = ''
    runHook preInstallCheck

    export HOME=$TMPDIR
    $out/bin/factor -e='USING: tools.test tools.test.private
      zealot.factor sequences namespaces formatting ;
      zealot-core-vocabs
      { ${finalAttrs.disabledTests} } without
      "compiler" suffix
      [ test-vocab ] each :test-failures
      test-failures get length "Number of failed tests: %d\n" printf'

    runHook postInstallCheck
  '';

  passthru = {
    inherit
      defaultLibs
      defaultBins
      extraLibs
      runtimeLibs
      binPackages
      extraVocabs
      vocabTree
      ;
  };

  meta = factor-unwrapped.meta // {
    mainProgram = "factor";
  };
})