Clone of https://github.com/NixOS/nixpkgs.git (to stress-test knotserver)
at 24.05-beta 288 lines 12 kB view raw
1{ lib, stdenv, removeReferencesTo, pkgsBuildBuild, pkgsBuildHost, pkgsBuildTarget, targetPackages 2, llvmShared, llvmSharedForBuild, llvmSharedForHost, llvmSharedForTarget, llvmPackages 3, fetchurl, file, python3 4, darwin, cargo, cmake, rustc, rustfmt 5, pkg-config, openssl, xz 6, libiconv 7, which, libffi 8, withBundledLLVM ? false 9, enableRustcDev ? true 10, version 11, sha256 12, patches ? [] 13, fd 14, ripgrep 15, wezterm 16, firefox 17, thunderbird 18# This only builds std for target and reuses the rustc from build. 19, fastCross 20, lndir 21, makeWrapper 22}: 23 24let 25 inherit (lib) optionals optional optionalString concatStringsSep; 26 inherit (darwin.apple_sdk.frameworks) Security; 27in stdenv.mkDerivation (finalAttrs: { 28 pname = "${targetPackages.stdenv.cc.targetPrefix}rustc"; 29 inherit version; 30 31 src = fetchurl { 32 url = "https://static.rust-lang.org/dist/rustc-${version}-src.tar.gz"; 33 inherit sha256; 34 # See https://nixos.org/manual/nixpkgs/stable/#using-git-bisect-on-the-rust-compiler 35 passthru.isReleaseTarball = true; 36 }; 37 38 __darwinAllowLocalNetworking = true; 39 40 # rustc complains about modified source files otherwise 41 dontUpdateAutotoolsGnuConfigScripts = true; 42 43 # Running the default `strip -S` command on Darwin corrupts the 44 # .rlib files in "lib/". 45 # 46 # See https://github.com/NixOS/nixpkgs/pull/34227 47 # 48 # Running `strip -S` when cross compiling can harm the cross rlibs. 49 # See: https://github.com/NixOS/nixpkgs/pull/56540#issuecomment-471624656 50 stripDebugList = [ "bin" ]; 51 52 # The Rust pkg-config crate does not support prefixed pkg-config executables[1], 53 # but it does support checking these idiosyncratic PKG_CONFIG_${TRIPLE} 54 # environment variables. 55 # [1]: https://github.com/rust-lang/pkg-config-rs/issues/53 56 "PKG_CONFIG_${builtins.replaceStrings ["-"] ["_"] stdenv.buildPlatform.rust.rustcTarget}" = 57 "${pkgsBuildHost.stdenv.cc.targetPrefix}pkg-config"; 58 59 NIX_LDFLAGS = toString ( 60 # when linking stage1 libstd: cc: undefined reference to `__cxa_begin_catch' 61 optional (stdenv.isLinux && !withBundledLLVM) "--push-state --as-needed -lstdc++ --pop-state" 62 ++ optional (stdenv.isDarwin && !withBundledLLVM) "-lc++ -lc++abi" 63 ++ optional stdenv.isDarwin "-rpath ${llvmSharedForHost}/lib"); 64 65 # Increase codegen units to introduce parallelism within the compiler. 66 RUSTFLAGS = "-Ccodegen-units=10"; 67 68 RUSTDOCFLAGS = "-A rustdoc::broken-intra-doc-links"; 69 70 # We need rust to build rust. If we don't provide it, configure will try to download it. 71 # Reference: https://github.com/rust-lang/rust/blob/master/src/bootstrap/configure.py 72 configureFlags = let 73 prefixForStdenv = stdenv: "${stdenv.cc}/bin/${stdenv.cc.targetPrefix}"; 74 ccPrefixForStdenv = stdenv: "${prefixForStdenv stdenv}${if (stdenv.cc.isClang or false) then "clang" else "cc"}"; 75 cxxPrefixForStdenv = stdenv: "${prefixForStdenv stdenv}${if (stdenv.cc.isClang or false) then "clang++" else "c++"}"; 76 setBuild = "--set=target.${stdenv.buildPlatform.rust.rustcTarget}"; 77 setHost = "--set=target.${stdenv.hostPlatform.rust.rustcTarget}"; 78 setTarget = "--set=target.${stdenv.targetPlatform.rust.rustcTarget}"; 79 ccForBuild = ccPrefixForStdenv pkgsBuildBuild.targetPackages.stdenv; 80 cxxForBuild = cxxPrefixForStdenv pkgsBuildBuild.targetPackages.stdenv; 81 ccForHost = ccPrefixForStdenv pkgsBuildHost.targetPackages.stdenv; 82 cxxForHost = cxxPrefixForStdenv pkgsBuildHost.targetPackages.stdenv; 83 ccForTarget = ccPrefixForStdenv pkgsBuildTarget.targetPackages.stdenv; 84 cxxForTarget = cxxPrefixForStdenv pkgsBuildTarget.targetPackages.stdenv; 85 in [ 86 "--sysconfdir=${placeholder "out"}/etc" 87 "--release-channel=stable" 88 "--set=build.rustc=${rustc}/bin/rustc" 89 "--set=build.cargo=${cargo}/bin/cargo" 90 ] ++ lib.optionals (!(finalAttrs.src.passthru.isReleaseTarball or false)) [ 91 # release tarballs vendor the rustfmt source; when 92 # git-bisect'ing from upstream's git repo we must prevent 93 # attempts to download the missing source tarball 94 "--set=build.rustfmt=${rustfmt}/bin/rustfmt" 95 ] ++ [ 96 "--tools=rustc,rustdoc,rust-analyzer-proc-macro-srv" 97 "--enable-rpath" 98 "--enable-vendor" 99 "--build=${stdenv.buildPlatform.rust.rustcTargetSpec}" 100 "--host=${stdenv.hostPlatform.rust.rustcTargetSpec}" 101 # std is built for all platforms in --target. 102 "--target=${concatStringsSep "," ([ 103 stdenv.targetPlatform.rust.rustcTargetSpec 104 105 # Other targets that don't need any extra dependencies to build. 106 ] ++ optionals (!fastCross) [ 107 "wasm32-unknown-unknown" 108 109 # (build!=target): When cross-building a compiler we need to add 110 # the build platform as well so rustc can compile build.rs 111 # scripts. 112 ] ++ optionals (stdenv.buildPlatform != stdenv.targetPlatform && !fastCross) [ 113 stdenv.buildPlatform.rust.rustcTargetSpec 114 115 # (host!=target): When building a cross-targeting compiler we 116 # need to add the host platform as well so rustc can compile 117 # build.rs scripts. 118 ] ++ optionals (stdenv.hostPlatform != stdenv.targetPlatform && !fastCross) [ 119 stdenv.hostPlatform.rust.rustcTargetSpec 120 ])}" 121 122 "${setBuild}.cc=${ccForBuild}" 123 "${setHost}.cc=${ccForHost}" 124 "${setTarget}.cc=${ccForTarget}" 125 126 "${setBuild}.linker=${ccForBuild}" 127 "${setHost}.linker=${ccForHost}" 128 "${setTarget}.linker=${ccForTarget}" 129 130 "${setBuild}.cxx=${cxxForBuild}" 131 "${setHost}.cxx=${cxxForHost}" 132 "${setTarget}.cxx=${cxxForTarget}" 133 134 "${setBuild}.crt-static=${lib.boolToString stdenv.buildPlatform.isStatic}" 135 "${setHost}.crt-static=${lib.boolToString stdenv.hostPlatform.isStatic}" 136 "${setTarget}.crt-static=${lib.boolToString stdenv.targetPlatform.isStatic}" 137 ] ++ optionals (!withBundledLLVM) [ 138 "--enable-llvm-link-shared" 139 "${setBuild}.llvm-config=${llvmSharedForBuild.dev}/bin/llvm-config" 140 "${setHost}.llvm-config=${llvmSharedForHost.dev}/bin/llvm-config" 141 "${setTarget}.llvm-config=${llvmSharedForTarget.dev}/bin/llvm-config" 142 ] ++ optionals (stdenv.isLinux && !stdenv.targetPlatform.isRedox) [ 143 "--enable-profiler" # build libprofiler_builtins 144 ] ++ optionals stdenv.buildPlatform.isMusl [ 145 "${setBuild}.musl-root=${pkgsBuildBuild.targetPackages.stdenv.cc.libc}" 146 ] ++ optionals stdenv.hostPlatform.isMusl [ 147 "${setHost}.musl-root=${pkgsBuildHost.targetPackages.stdenv.cc.libc}" 148 ] ++ optionals stdenv.targetPlatform.isMusl [ 149 "${setTarget}.musl-root=${pkgsBuildTarget.targetPackages.stdenv.cc.libc}" 150 ] ++ optionals stdenv.targetPlatform.rust.isNoStdTarget [ 151 "--disable-docs" 152 ] ++ optionals (stdenv.isDarwin && stdenv.isx86_64) [ 153 # https://github.com/rust-lang/rust/issues/92173 154 "--set rust.jemalloc" 155 ]; 156 157 # if we already have a rust compiler for build just compile the target std 158 # library and reuse compiler 159 buildPhase = if fastCross then " 160 runHook preBuild 161 162 mkdir -p build/${stdenv.hostPlatform.rust.rustcTargetSpec}/stage0-{std,rustc}/${stdenv.hostPlatform.rust.rustcTargetSpec}/release/ 163 ln -s ${rustc.unwrapped}/lib/rustlib/${stdenv.hostPlatform.rust.rustcTargetSpec}/libstd-*.so build/${stdenv.hostPlatform.rust.rustcTargetSpec}/stage0-std/${stdenv.hostPlatform.rust.rustcTargetSpec}/release/libstd.so 164 ln -s ${rustc.unwrapped}/lib/rustlib/${stdenv.hostPlatform.rust.rustcTargetSpec}/librustc_driver-*.so build/${stdenv.hostPlatform.rust.rustcTargetSpec}/stage0-rustc/${stdenv.hostPlatform.rust.rustcTargetSpec}/release/librustc.so 165 ln -s ${rustc.unwrapped}/bin/rustc build/${stdenv.hostPlatform.rust.rustcTargetSpec}/stage0-rustc/${stdenv.hostPlatform.rust.rustcTargetSpec}/release/rustc-main 166 touch build/${stdenv.hostPlatform.rust.rustcTargetSpec}/stage0-std/${stdenv.hostPlatform.rust.rustcTargetSpec}/release/.libstd.stamp 167 touch build/${stdenv.hostPlatform.rust.rustcTargetSpec}/stage0-rustc/${stdenv.hostPlatform.rust.rustcTargetSpec}/release/.librustc.stamp 168 python ./x.py --keep-stage=0 --stage=1 build library 169 170 runHook postBuild 171 " else null; 172 173 installPhase = if fastCross then '' 174 runHook preInstall 175 176 python ./x.py --keep-stage=0 --stage=1 install library/std 177 mkdir -v $out/bin $doc $man 178 ln -s ${rustc.unwrapped}/bin/{rustc,rustdoc} $out/bin 179 ln -s ${rustc.unwrapped}/lib/rustlib/{manifest-rust-std-,}${stdenv.hostPlatform.rust.rustcTargetSpec} $out/lib/rustlib/ 180 echo rust-std-${stdenv.hostPlatform.rust.rustcTargetSpec} >> $out/lib/rustlib/components 181 lndir ${rustc.doc} $doc 182 lndir ${rustc.man} $man 183 184 runHook postInstall 185 '' else null; 186 187 # the rust build system complains that nix alters the checksums 188 dontFixLibtool = true; 189 190 inherit patches; 191 192 postPatch = '' 193 patchShebangs src/etc 194 195 ${optionalString (!withBundledLLVM) "rm -rf src/llvm"} 196 197 # Useful debugging parameter 198 # export VERBOSE=1 199 '' + lib.optionalString (stdenv.isDarwin && stdenv.isx86_64) '' 200 # See https://github.com/jemalloc/jemalloc/issues/1997 201 # Using a value of 48 should work on both emulated and native x86_64-darwin. 202 export JEMALLOC_SYS_WITH_LG_VADDR=48 203 '' + lib.optionalString (!(finalAttrs.src.passthru.isReleaseTarball or false)) '' 204 mkdir .cargo 205 cat > .cargo/config <<\EOF 206 [source.crates-io] 207 replace-with = "vendored-sources" 208 [source.vendored-sources] 209 directory = "vendor" 210 EOF 211 ''; 212 213 # rustc unfortunately needs cmake to compile llvm-rt but doesn't 214 # use it for the normal build. This disables cmake in Nix. 215 dontUseCmakeConfigure = true; 216 217 depsBuildBuild = [ pkgsBuildHost.stdenv.cc pkg-config ]; 218 219 nativeBuildInputs = [ 220 file python3 rustc cmake 221 which libffi removeReferencesTo pkg-config xz 222 ] 223 ++ optionals fastCross [ lndir makeWrapper ]; 224 225 buildInputs = [ openssl ] 226 ++ optionals stdenv.isDarwin [ libiconv Security ] 227 ++ optional (!withBundledLLVM) llvmShared; 228 229 outputs = [ "out" "man" "doc" ]; 230 setOutputFlags = false; 231 232 postInstall = lib.optionalString (enableRustcDev && !fastCross) '' 233 # install rustc-dev components. Necessary to build rls, clippy... 234 python x.py dist rustc-dev 235 tar xf build/dist/rustc-dev*tar.gz 236 cp -r rustc-dev*/rustc-dev*/lib/* $out/lib/ 237 rm $out/lib/rustlib/install.log 238 for m in $out/lib/rustlib/manifest-rust* 239 do 240 sort --output=$m < $m 241 done 242 243 '' + '' 244 # remove references to llvm-config in lib/rustlib/x86_64-unknown-linux-gnu/codegen-backends/librustc_codegen_llvm-llvm.so 245 # and thus a transitive dependency on ncurses 246 find $out/lib -name "*.so" -type f -exec remove-references-to -t ${llvmShared} '{}' '+' 247 248 # remove uninstall script that doesn't really make sense for Nix. 249 rm $out/lib/rustlib/uninstall.sh 250 ''; 251 252 configurePlatforms = []; 253 254 enableParallelBuilding = true; 255 256 setupHooks = ./setup-hook.sh; 257 258 requiredSystemFeatures = [ "big-parallel" ]; 259 260 passthru = { 261 llvm = llvmShared; 262 inherit llvmPackages; 263 tests = { 264 inherit fd ripgrep wezterm; 265 } // lib.optionalAttrs stdenv.hostPlatform.isLinux { inherit firefox thunderbird; }; 266 }; 267 268 meta = with lib; { 269 homepage = "https://www.rust-lang.org/"; 270 description = "A safe, concurrent, practical language"; 271 maintainers = with maintainers; [ havvy ] ++ teams.rust.members; 272 license = [ licenses.mit licenses.asl20 ]; 273 platforms = [ 274 # Platforms with host tools from 275 # https://doc.rust-lang.org/nightly/rustc/platform-support.html 276 "x86_64-darwin" "i686-darwin" "aarch64-darwin" 277 "i686-freebsd" "x86_64-freebsd" 278 "x86_64-solaris" 279 "aarch64-linux" "armv6l-linux" "armv7l-linux" "i686-linux" 280 "loongarch64-linux" "powerpc64-linux" "powerpc64le-linux" 281 "riscv64-linux" "s390x-linux" "x86_64-linux" 282 "aarch64-netbsd" "armv7l-netbsd" "i686-netbsd" "powerpc-netbsd" 283 "x86_64-netbsd" 284 "i686-openbsd" "x86_64-openbsd" 285 "i686-windows" "x86_64-windows" 286 ]; 287 }; 288})