1 { lib, stdenv, fetchurl, patchelf, gmp }:
6 usr_prefix = if stdenv.isDarwin then "usr/local" else "usr";
8 dynamic_linker = stdenv.cc.bintools.dynamicLinker;
11 stdenv.mkDerivation rec {
16 if stdenv.hostPlatform.system == "i686-linux" then (fetchurl {
17 url = "mirror://sourceforge/project/mlton/mlton/${version}/${pname}-${version}-1.x86-linux.tgz";
18 sha256 = "1kxjjmnw4xk2d9hpvz43w9dvyhb3025k4zvjx785c33nrwkrdn4j";
20 else if stdenv.hostPlatform.system == "x86_64-linux" then (fetchurl {
21 url = "mirror://sourceforge/project/mlton/mlton/${version}/${pname}-${version}-1.amd64-linux.tgz";
22 sha256 = "0fyhwxb4nmpirjbjcvk9f6w67gmn2gkz7xcgz0xbfih9kc015ygn";
24 else if stdenv.hostPlatform.system == "x86_64-darwin" then (fetchurl {
25 url = "mirror://sourceforge/project/mlton/mlton/${version}/${pname}-${version}-1.amd64-darwin.gmp-macports.tgz";
26 sha256 = "044wnh9hhg6if886xy805683k0as347xd37r0r1yi4x7qlxzzgx9";
28 else throw "Architecture not supported";
32 url = "mirror://sourceforge/project/mlton/mlton/${version}/${pname}-${version}.src.tgz";
33 sha256 = "0v1x2hrh9hiqkvnbq11kf34v4i5a2x0ffxbzqaa8skyl26nmfn11";
36 srcs = [ binSrc codeSrc ];
38 sourceRoot = "${pname}-${version}";
40 buildInputs = [ gmp ];
41 nativeBuildInputs = lib.optional stdenv.isLinux patchelf;
43 makeFlags = [ "all-no-docs" ];
46 # Fix paths in the source.
47 find . -type f | grep -v -e '\.tgz''$' | xargs sed -i "s@/usr/bin/env bash@$(type -p bash)@"
49 substituteInPlace $(pwd)/Makefile --replace '/bin/cp' $(type -p cp)
50 substituteInPlace bin/mlton-script --replace gcc cc
51 substituteInPlace bin/regression --replace gcc cc
52 substituteInPlace lib/mlnlffi-lib/Makefile --replace gcc cc
53 substituteInPlace mlnlffigen/gen-cppcmd --replace gcc cc
54 substituteInPlace runtime/Makefile --replace gcc cc
55 substituteInPlace ../${usr_prefix}/bin/mlton --replace gcc cc
57 # Fix paths in the binary distribution.
58 BIN_DIST_DIR="$(pwd)/../${usr_prefix}"
59 for f in "bin/mlton" "lib/mlton/platform" "lib/mlton/static-library" ; do
60 substituteInPlace "$BIN_DIST_DIR/$f" --replace '/${usr_prefix}/bin/env bash' $(type -p bash)
63 substituteInPlace $(pwd)/../${usr_prefix}/bin/mlton --replace '/${usr_prefix}/lib/mlton' $(pwd)/../${usr_prefix}/lib/mlton
64 '' + lib.optionalString stdenv.cc.isClang ''
65 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
66 '' + lib.optionalString stdenv.isDarwin ''
67 sed -i 's|XCFLAGS += -I/usr/local/include -I/sw/include -I/opt/local/include||' ./runtime/Makefile
71 # To build the source we have to put the binary distribution in the $PATH.
72 export PATH="$PATH:$(pwd)/../${usr_prefix}/bin/"
74 # Let the builder execute the binary distribution.
75 chmod u+x $(pwd)/../${usr_prefix}/bin/mllex
76 chmod u+x $(pwd)/../${usr_prefix}/bin/mlyacc
77 chmod u+x $(pwd)/../${usr_prefix}/bin/mlton
79 # So the builder runs the binary compiler with gmp.
80 export LD_LIBRARY_PATH=${gmp.out}/lib''${LD_LIBRARY_PATH:+:}$LD_LIBRARY_PATH
82 '' + lib.optionalString stdenv.isLinux ''
83 # Patch ELF interpreter.
84 patchelf --set-interpreter ${dynamic_linker} $(pwd)/../${usr_prefix}/lib/mlton/mlton-compile
85 for e in mllex mlyacc ; do
86 patchelf --set-interpreter ${dynamic_linker} $(pwd)/../${usr_prefix}/bin/$e
88 '' + lib.optionalString stdenv.isDarwin ''
89 # Patch libgmp linking
90 install_name_tool -change /opt/local/lib/libgmp.10.dylib ${gmp}/lib/libgmp.10.dylib $(pwd)/../${usr_prefix}/lib/mlton/mlton-compile
91 install_name_tool -change /opt/local/lib/libgmp.10.dylib ${gmp}/lib/libgmp.10.dylib $(pwd)/../${usr_prefix}/bin/mlyacc
92 install_name_tool -change /opt/local/lib/libgmp.10.dylib ${gmp}/lib/libgmp.10.dylib $(pwd)/../${usr_prefix}/bin/mllex
97 installTargets = [ "install-no-docs" ];
100 # Fix path to mlton libraries.
101 substituteInPlace $(pwd)/install/${usr_prefix}/bin/mlton --replace '/${usr_prefix}/lib/mlton' $out/lib/mlton
104 substituteInPlace $(pwd)/install/${usr_prefix}/bin/mlton --replace "-link-opt '-lm -lgmp'" "-link-opt '-lm -lgmp -L${gmp.out}/lib'"
107 substituteInPlace $(pwd)/install/${usr_prefix}/bin/mlton --replace "-cc-opt '-O1 -fno-common'" "-cc-opt '-O1 -fno-common -I${gmp.dev}/include'"
109 # Path to the same cc used in the build; needed at runtime.
110 substituteInPlace $(pwd)/install/${usr_prefix}/bin/mlton --replace "gcc='gcc'" "gcc='"$(type -p cc)"'"
112 # Copy files to final positions.
113 cp -r $(pwd)/install/${usr_prefix}/bin $out
114 cp -r $(pwd)/install/${usr_prefix}/lib $out
115 cp -r $(pwd)/install/${usr_prefix}/man $out
118 meta = import ./meta.nix;