1{
2 lib,
3 stdenv,
4 fetchurl,
5 patchelf,
6 gmp,
7}:
8
9let
10 version = "20130715";
11
12 usr_prefix = if stdenv.hostPlatform.isDarwin then "usr/local" else "usr";
13
14 dynamic_linker = stdenv.cc.bintools.dynamicLinker;
15in
16
17stdenv.mkDerivation rec {
18 pname = "mlton";
19 inherit version;
20
21 binSrc =
22 if stdenv.hostPlatform.system == "i686-linux" then
23 (fetchurl {
24 url = "mirror://sourceforge/project/mlton/mlton/${version}/${pname}-${version}-1.x86-linux.tgz";
25 sha256 = "1kxjjmnw4xk2d9hpvz43w9dvyhb3025k4zvjx785c33nrwkrdn4j";
26 })
27 else if stdenv.hostPlatform.system == "x86_64-linux" then
28 (fetchurl {
29 url = "mirror://sourceforge/project/mlton/mlton/${version}/${pname}-${version}-1.amd64-linux.tgz";
30 sha256 = "0fyhwxb4nmpirjbjcvk9f6w67gmn2gkz7xcgz0xbfih9kc015ygn";
31 })
32 else if stdenv.hostPlatform.system == "x86_64-darwin" then
33 (fetchurl {
34 url = "mirror://sourceforge/project/mlton/mlton/${version}/${pname}-${version}-1.amd64-darwin.gmp-macports.tgz";
35 sha256 = "044wnh9hhg6if886xy805683k0as347xd37r0r1yi4x7qlxzzgx9";
36 })
37 else
38 throw "Architecture not supported";
39
40 codeSrc = fetchurl {
41 url = "mirror://sourceforge/project/mlton/mlton/${version}/${pname}-${version}.src.tgz";
42 sha256 = "0v1x2hrh9hiqkvnbq11kf34v4i5a2x0ffxbzqaa8skyl26nmfn11";
43 };
44
45 srcs = [
46 binSrc
47 codeSrc
48 ];
49
50 sourceRoot = "${pname}-${version}";
51
52 buildInputs = [ gmp ];
53 nativeBuildInputs = lib.optional stdenv.hostPlatform.isLinux patchelf;
54
55 makeFlags = [ "all-no-docs" ];
56
57 configurePhase = ''
58 # Fix paths in the source.
59 find . -type f | grep -v -e '\.tgz''$' | xargs sed -i "s@/usr/bin/env bash@$(type -p bash)@"
60
61 substituteInPlace $(pwd)/Makefile --replace '/bin/cp' $(type -p cp)
62 substituteInPlace bin/mlton-script --replace gcc cc
63 substituteInPlace bin/regression --replace gcc cc
64 substituteInPlace lib/mlnlffi-lib/Makefile --replace gcc cc
65 substituteInPlace mlnlffigen/gen-cppcmd --replace gcc cc
66 substituteInPlace runtime/Makefile --replace gcc cc
67 substituteInPlace ../${usr_prefix}/bin/mlton --replace gcc cc
68
69 # Fix paths in the binary distribution.
70 BIN_DIST_DIR="$(pwd)/../${usr_prefix}"
71 for f in "bin/mlton" "lib/mlton/platform" "lib/mlton/static-library" ; do
72 substituteInPlace "$BIN_DIST_DIR/$f" --replace '/${usr_prefix}/bin/env bash' $(type -p bash)
73 done
74
75 substituteInPlace $(pwd)/../${usr_prefix}/bin/mlton --replace '/${usr_prefix}/lib/mlton' $(pwd)/../${usr_prefix}/lib/mlton
76 ''
77 + lib.optionalString stdenv.cc.isClang ''
78 sed -i "s_ patch -s -p0 <gdtoa.hide-public-fns.patch_ patch -s -p0 <gdtoa.hide-public-fns.patch\n\tsed -i 's|printf(emptyfmt|printf(\"\"|g' ./gdtoa/arithchk.c_" ./runtime/Makefile
79 ''
80 + lib.optionalString stdenv.hostPlatform.isDarwin ''
81 sed -i 's|XCFLAGS += -I/usr/local/include -I/sw/include -I/opt/local/include||' ./runtime/Makefile
82 '';
83
84 preBuild = ''
85 # To build the source we have to put the binary distribution in the $PATH.
86 export PATH="$PATH:$(pwd)/../${usr_prefix}/bin/"
87
88 # Let the builder execute the binary distribution.
89 chmod u+x $(pwd)/../${usr_prefix}/bin/mllex
90 chmod u+x $(pwd)/../${usr_prefix}/bin/mlyacc
91 chmod u+x $(pwd)/../${usr_prefix}/bin/mlton
92
93 # So the builder runs the binary compiler with gmp.
94 export LD_LIBRARY_PATH=${gmp.out}/lib''${LD_LIBRARY_PATH:+:}$LD_LIBRARY_PATH
95
96 ''
97 + lib.optionalString stdenv.hostPlatform.isLinux ''
98 # Patch ELF interpreter.
99 patchelf --set-interpreter ${dynamic_linker} $(pwd)/../${usr_prefix}/lib/mlton/mlton-compile
100 for e in mllex mlyacc ; do
101 patchelf --set-interpreter ${dynamic_linker} $(pwd)/../${usr_prefix}/bin/$e
102 done
103 ''
104 + lib.optionalString stdenv.hostPlatform.isDarwin ''
105 # Patch libgmp linking
106 install_name_tool -change /opt/local/lib/libgmp.10.dylib ${gmp}/lib/libgmp.10.dylib $(pwd)/../${usr_prefix}/lib/mlton/mlton-compile
107 install_name_tool -change /opt/local/lib/libgmp.10.dylib ${gmp}/lib/libgmp.10.dylib $(pwd)/../${usr_prefix}/bin/mlyacc
108 install_name_tool -change /opt/local/lib/libgmp.10.dylib ${gmp}/lib/libgmp.10.dylib $(pwd)/../${usr_prefix}/bin/mllex
109 '';
110
111 doCheck = true;
112
113 installTargets = [ "install-no-docs" ];
114
115 postInstall = ''
116 # Fix path to mlton libraries.
117 substituteInPlace $(pwd)/install/${usr_prefix}/bin/mlton --replace '/${usr_prefix}/lib/mlton' $out/lib/mlton
118
119 # Path to libgmp.
120 substituteInPlace $(pwd)/install/${usr_prefix}/bin/mlton --replace "-link-opt '-lm -lgmp'" "-link-opt '-lm -lgmp -L${gmp.out}/lib'"
121
122 # Path to gmp.h.
123 substituteInPlace $(pwd)/install/${usr_prefix}/bin/mlton --replace "-cc-opt '-O1 -fno-common'" "-cc-opt '-O1 -fno-common -I${gmp.dev}/include'"
124
125 # Path to the same cc used in the build; needed at runtime.
126 substituteInPlace $(pwd)/install/${usr_prefix}/bin/mlton --replace "gcc='gcc'" "gcc='"$(type -p cc)"'"
127
128 # Copy files to final positions.
129 cp -r $(pwd)/install/${usr_prefix}/bin $out
130 cp -r $(pwd)/install/${usr_prefix}/lib $out
131 cp -r $(pwd)/install/${usr_prefix}/man $out
132 '';
133
134 meta = import ./meta.nix;
135}