1{ stdenv, fetchurl, fetchgit, fetchzip, file, python2, tzdata, procps
2, llvmPackages_37, jemalloc, ncurses
3
4, shortVersion, isRelease
5, forceBundledLLVM ? false
6, srcSha, srcRev ? ""
7, snapshotHashLinux686, snapshotHashLinux64
8, snapshotHashDarwin686, snapshotHashDarwin64
9, snapshotDate, snapshotRev
10, configureFlags ? []
11
12, patches
13}:
14
15assert !stdenv.isFreeBSD;
16
17/* Rust's build process has a few quirks :
18
19- The Rust compiler is written is Rust, so it requires a bootstrap
20 compiler, which is downloaded during the build. To make the build
21 pure, we download it ourself before and put it where it is
22 expected. Once the language is stable (1.0) , we might want to
23 switch it to use nix's packaged rust compiler. This might not be possible
24 as the compiler is highly coupled to the bootstrap.
25
26NOTE : some derivation depend on rust. When updating this, please make
27sure those derivations still compile. (racer, for example).
28
29*/
30
31assert (if isRelease then srcRev == "" else srcRev != "");
32
33let version = if isRelease then
34 "${shortVersion}"
35 else
36 "${shortVersion}-g${builtins.substring 0 7 srcRev}";
37
38 name = "rustc-${version}";
39
40 platform = if stdenv.system == "i686-linux"
41 then "linux-i386"
42 else if stdenv.system == "x86_64-linux"
43 then "linux-x86_64"
44 else if stdenv.system == "i686-darwin"
45 then "macos-i386"
46 else if stdenv.system == "x86_64-darwin"
47 then "macos-x86_64"
48 else abort "no snapshot to bootstrap for this platform (missing platform url suffix)";
49
50 target = if stdenv.system == "i686-linux"
51 then "i686-unknown-linux-gnu"
52 else if stdenv.system == "x86_64-linux"
53 then "x86_64-unknown-linux-gnu"
54 else if stdenv.system == "i686-darwin"
55 then "i686-apple-darwin"
56 else if stdenv.system == "x86_64-darwin"
57 then "x86_64-apple-darwin"
58 else abort "no snapshot to bootstrap for this platform (missing target triple)";
59
60 meta = with stdenv.lib; {
61 homepage = http://www.rust-lang.org/;
62 description = "A safe, concurrent, practical language";
63 maintainers = with maintainers; [ madjar cstrahan wizeman globin havvy wkennington ];
64 license = [ licenses.mit licenses.asl20 ];
65 platforms = platforms.linux;
66 };
67
68 snapshotHash = if stdenv.system == "i686-linux"
69 then snapshotHashLinux686
70 else if stdenv.system == "x86_64-linux"
71 then snapshotHashLinux64
72 else if stdenv.system == "i686-darwin"
73 then snapshotHashDarwin686
74 else if stdenv.system == "x86_64-darwin"
75 then snapshotHashDarwin64
76 else abort "no snapshot for platform ${stdenv.system}";
77 snapshotName = "rust-stage0-${snapshotDate}-${snapshotRev}-${platform}-${snapshotHash}.tar.bz2";
78in
79
80with stdenv.lib; stdenv.mkDerivation {
81 inherit name;
82 inherit version;
83 inherit meta;
84
85 __impureHostDeps = [ "/usr/lib/libedit.3.dylib" ];
86
87 src = if isRelease then
88 fetchzip {
89 url = "http://static.rust-lang.org/dist/rustc-${version}-src.tar.gz";
90 sha256 = srcSha;
91 }
92 else
93 fetchgit {
94 url = https://github.com/rust-lang/rust;
95 rev = srcRev;
96 sha256 = srcSha;
97 };
98
99 # We need rust to build rust. If we don't provide it, configure will try to download it.
100 snapshot = stdenv.mkDerivation {
101 name = "rust-stage0";
102 src = fetchurl {
103 url = "http://static.rust-lang.org/stage0-snapshots/${snapshotName}";
104 sha1 = snapshotHash;
105 };
106 dontStrip = true;
107 installPhase = ''
108 mkdir -p "$out"
109 cp -r bin "$out/bin"
110 '' + optionalString stdenv.isLinux ''
111 patchelf --interpreter "${stdenv.glibc}/lib/${stdenv.cc.dynamicLinker}" \
112 --set-rpath "${stdenv.cc.cc}/lib/:${stdenv.cc.cc}/lib64/" \
113 "$out/bin/rustc"
114 '';
115 };
116
117 configureFlags = configureFlags
118 ++ [ "--enable-local-rust" "--local-rust-root=$snapshot" "--enable-rpath" ]
119 # ++ [ "--jemalloc-root=${jemalloc}/lib"
120 ++ [ "--default-linker=${stdenv.cc}/bin/cc" "--default-ar=${stdenv.cc.binutils}/bin/ar" ]
121 ++ optional (stdenv.cc.cc ? isClang) "--enable-clang"
122 ++ optional (!forceBundledLLVM) "--llvm-root=${llvmPackages_37.llvm}";
123
124 inherit patches;
125
126 postPatch = ''
127 substituteInPlace src/rust-installer/gen-install-script.sh \
128 --replace /bin/echo "$(type -P echo)"
129 substituteInPlace src/rust-installer/gen-installer.sh \
130 --replace /bin/echo "$(type -P echo)"
131
132 # Workaround for NixOS/nixpkgs#8676
133 substituteInPlace mk/rustllvm.mk \
134 --replace "\$\$(subst /,//," "\$\$(subst /,/,"
135
136 # Fix dynamic linking against llvm
137 ${optionalString (!forceBundledLLVM) ''sed -i 's/, kind = \\"static\\"//g' src/etc/mklldeps.py''}
138
139 # Fix the configure script to not require curl as we won't use it
140 sed -i configure \
141 -e '/probe_need CFG_CURLORWGET/d'
142
143 # Fix the use of jemalloc prefixes which our jemalloc doesn't have
144 # TODO: reenable if we can figure out how to get our jemalloc to work
145 #[ -f src/liballoc_jemalloc/lib.rs ] && sed -i 's,je_,,g' src/liballoc_jemalloc/lib.rs
146 #[ -f src/liballoc/heap.rs ] && sed -i 's,je_,,g' src/liballoc/heap.rs # Remove for 1.4.0+
147
148 # Useful debugging parameter
149 #export VERBOSE=1
150 '';
151
152 preConfigure = ''
153 # Needed flags as the upstream configure script has a broken prefix substitution
154 configureFlagsArray+=("--datadir=$out/share")
155 configureFlagsArray+=("--infodir=$out/share/info")
156 '';
157
158 # Procps is needed for one of the test cases
159 nativeBuildInputs = [ file python2 ]
160 ++ optionals stdenv.isLinux [ procps ];
161 buildInputs = [ ncurses ]
162 ++ optional (!forceBundledLLVM) llvmPackages_37.llvm;
163
164 enableParallelBuilding = true;
165
166 outputs = [ "out" "doc" ];
167
168 preCheck = "export TZDIR=${tzdata}/share/zoneinfo";
169
170 doCheck = true;
171}