1{ stdenv, fetchurl, fetchgit, fetchzip, file, python2, tzdata, procps 2, llvmPackages_37, jemalloc, ncurses, darwin 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} @ args: 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 procps = if stdenv.isDarwin then darwin.ps else args.procps; 41 42 llvmShared = llvmPackages_37.llvm.override { enableSharedLibraries = true; }; 43 44 platform = if stdenv.system == "i686-linux" 45 then "linux-i386" 46 else if stdenv.system == "x86_64-linux" 47 then "linux-x86_64" 48 else if stdenv.system == "i686-darwin" 49 then "macos-i386" 50 else if stdenv.system == "x86_64-darwin" 51 then "macos-x86_64" 52 else abort "no snapshot to bootstrap for this platform (missing platform url suffix)"; 53 54 target = if stdenv.system == "i686-linux" 55 then "i686-unknown-linux-gnu" 56 else if stdenv.system == "x86_64-linux" 57 then "x86_64-unknown-linux-gnu" 58 else if stdenv.system == "i686-darwin" 59 then "i686-apple-darwin" 60 else if stdenv.system == "x86_64-darwin" 61 then "x86_64-apple-darwin" 62 else abort "no snapshot to bootstrap for this platform (missing target triple)"; 63 64 meta = with stdenv.lib; { 65 homepage = http://www.rust-lang.org/; 66 description = "A safe, concurrent, practical language"; 67 maintainers = with maintainers; [ madjar cstrahan wizeman globin havvy wkennington ]; 68 license = [ licenses.mit licenses.asl20 ]; 69 platforms = platforms.linux; 70 }; 71 72 snapshotHash = if stdenv.system == "i686-linux" 73 then snapshotHashLinux686 74 else if stdenv.system == "x86_64-linux" 75 then snapshotHashLinux64 76 else if stdenv.system == "i686-darwin" 77 then snapshotHashDarwin686 78 else if stdenv.system == "x86_64-darwin" 79 then snapshotHashDarwin64 80 else abort "no snapshot for platform ${stdenv.system}"; 81 snapshotName = "rust-stage0-${snapshotDate}-${snapshotRev}-${platform}-${snapshotHash}.tar.bz2"; 82in 83 84with stdenv.lib; stdenv.mkDerivation { 85 inherit name; 86 inherit version; 87 inherit meta; 88 89 __impureHostDeps = [ "/usr/lib/libedit.3.dylib" ]; 90 91 NIX_LDFLAGS = stdenv.lib.optionalString stdenv.isDarwin "-rpath ${llvmShared}/lib"; 92 93 src = if isRelease then 94 fetchzip { 95 url = "http://static.rust-lang.org/dist/rustc-${version}-src.tar.gz"; 96 sha256 = srcSha; 97 } 98 else 99 fetchgit { 100 url = https://github.com/rust-lang/rust; 101 rev = srcRev; 102 sha256 = srcSha; 103 }; 104 105 # We need rust to build rust. If we don't provide it, configure will try to download it. 106 snapshot = stdenv.mkDerivation { 107 name = "rust-stage0"; 108 src = fetchurl { 109 url = "http://static.rust-lang.org/stage0-snapshots/${snapshotName}"; 110 sha1 = snapshotHash; 111 }; 112 dontStrip = true; 113 installPhase = '' 114 mkdir -p "$out" 115 cp -r bin "$out/bin" 116 '' + optionalString stdenv.isLinux '' 117 patchelf --interpreter "${stdenv.glibc}/lib/${stdenv.cc.dynamicLinker}" \ 118 --set-rpath "${stdenv.cc.cc}/lib/:${stdenv.cc.cc}/lib64/" \ 119 "$out/bin/rustc" 120 ''; 121 }; 122 123 configureFlags = configureFlags 124 ++ [ "--enable-local-rust" "--local-rust-root=$snapshot" "--enable-rpath" ] 125 # ++ [ "--jemalloc-root=${jemalloc}/lib" 126 ++ [ "--default-linker=${stdenv.cc}/bin/cc" "--default-ar=${stdenv.cc.binutils}/bin/ar" ] 127 ++ optional (stdenv.cc.cc ? isClang) "--enable-clang" 128 ++ optional (!forceBundledLLVM) "--llvm-root=${llvmShared}"; 129 130 inherit patches; 131 132 postPatch = '' 133 substituteInPlace src/rust-installer/gen-install-script.sh \ 134 --replace /bin/echo "$(type -P echo)" 135 substituteInPlace src/rust-installer/gen-installer.sh \ 136 --replace /bin/echo "$(type -P echo)" 137 138 # Workaround for NixOS/nixpkgs#8676 139 substituteInPlace mk/rustllvm.mk \ 140 --replace "\$\$(subst /,//," "\$\$(subst /,/," 141 142 # Fix dynamic linking against llvm 143 ${optionalString (!forceBundledLLVM) ''sed -i 's/, kind = \\"static\\"//g' src/etc/mklldeps.py''} 144 145 # Fix the configure script to not require curl as we won't use it 146 sed -i configure \ 147 -e '/probe_need CFG_CURLORWGET/d' 148 149 # Fix the use of jemalloc prefixes which our jemalloc doesn't have 150 # TODO: reenable if we can figure out how to get our jemalloc to work 151 #[ -f src/liballoc_jemalloc/lib.rs ] && sed -i 's,je_,,g' src/liballoc_jemalloc/lib.rs 152 #[ -f src/liballoc/heap.rs ] && sed -i 's,je_,,g' src/liballoc/heap.rs # Remove for 1.4.0+ 153 154 # Useful debugging parameter 155 #export VERBOSE=1 156 ''; 157 158 preConfigure = '' 159 # Needed flags as the upstream configure script has a broken prefix substitution 160 configureFlagsArray+=("--datadir=$out/share") 161 configureFlagsArray+=("--infodir=$out/share/info") 162 ''; 163 164 # ps is needed for one of the test cases 165 nativeBuildInputs = [ file python2 procps ]; 166 buildInputs = [ ncurses ] 167 ++ optional (!forceBundledLLVM) llvmShared; 168 169 enableParallelBuilding = true; 170 171 outputs = [ "out" "doc" ]; 172 173 preCheck = "export TZDIR=${tzdata}/share/zoneinfo"; 174 175 doCheck = true; 176}