Clone of https://github.com/NixOS/nixpkgs.git (to stress-test knotserver)
at 23.11-beta 302 lines 13 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 # We need rust to build rust. If we don't provide it, configure will try to download it. 69 # Reference: https://github.com/rust-lang/rust/blob/master/src/bootstrap/configure.py 70 configureFlags = let 71 prefixForStdenv = stdenv: "${stdenv.cc}/bin/${stdenv.cc.targetPrefix}"; 72 ccPrefixForStdenv = stdenv: "${prefixForStdenv stdenv}${if (stdenv.cc.isClang or false) then "clang" else "cc"}"; 73 cxxPrefixForStdenv = stdenv: "${prefixForStdenv stdenv}${if (stdenv.cc.isClang or false) then "clang++" else "c++"}"; 74 setBuild = "--set=target.${stdenv.buildPlatform.rust.rustcTarget}"; 75 setHost = "--set=target.${stdenv.hostPlatform.rust.rustcTarget}"; 76 setTarget = "--set=target.${stdenv.targetPlatform.rust.rustcTarget}"; 77 ccForBuild = ccPrefixForStdenv pkgsBuildBuild.targetPackages.stdenv; 78 cxxForBuild = cxxPrefixForStdenv pkgsBuildBuild.targetPackages.stdenv; 79 ccForHost = ccPrefixForStdenv pkgsBuildHost.targetPackages.stdenv; 80 cxxForHost = cxxPrefixForStdenv pkgsBuildHost.targetPackages.stdenv; 81 ccForTarget = ccPrefixForStdenv pkgsBuildTarget.targetPackages.stdenv; 82 cxxForTarget = cxxPrefixForStdenv pkgsBuildTarget.targetPackages.stdenv; 83 in [ 84 "--release-channel=stable" 85 "--set=build.rustc=${rustc}/bin/rustc" 86 "--set=build.cargo=${cargo}/bin/cargo" 87 ] ++ lib.optionals (!(finalAttrs.src.passthru.isReleaseTarball or false)) [ 88 # release tarballs vendor the rustfmt source; when 89 # git-bisect'ing from upstream's git repo we must prevent 90 # attempts to download the missing source tarball 91 "--set=build.rustfmt=${rustfmt}/bin/rustfmt" 92 ] ++ [ 93 "--tools=rustc,rust-analyzer-proc-macro-srv" 94 "--enable-rpath" 95 "--enable-vendor" 96 "--build=${stdenv.buildPlatform.rust.rustcTargetSpec}" 97 "--host=${stdenv.hostPlatform.rust.rustcTargetSpec}" 98 # std is built for all platforms in --target. 99 "--target=${concatStringsSep "," ([ 100 stdenv.targetPlatform.rust.rustcTargetSpec 101 102 # (build!=target): When cross-building a compiler we need to add 103 # the build platform as well so rustc can compile build.rs 104 # scripts. 105 ] ++ optionals (stdenv.buildPlatform != stdenv.targetPlatform && !fastCross) [ 106 stdenv.buildPlatform.rust.rustcTargetSpec 107 108 # (host!=target): When building a cross-targeting compiler we 109 # need to add the host platform as well so rustc can compile 110 # build.rs scripts. 111 ] ++ optionals (stdenv.hostPlatform != stdenv.targetPlatform && !fastCross) [ 112 stdenv.hostPlatform.rust.rustcTargetSpec 113 ])}" 114 115 "${setBuild}.cc=${ccForBuild}" 116 "${setHost}.cc=${ccForHost}" 117 "${setTarget}.cc=${ccForTarget}" 118 119 "${setBuild}.linker=${ccForBuild}" 120 "${setHost}.linker=${ccForHost}" 121 "${setTarget}.linker=${ccForTarget}" 122 123 "${setBuild}.cxx=${cxxForBuild}" 124 "${setHost}.cxx=${cxxForHost}" 125 "${setTarget}.cxx=${cxxForTarget}" 126 127 "${setBuild}.crt-static=${lib.boolToString stdenv.buildPlatform.isStatic}" 128 "${setHost}.crt-static=${lib.boolToString stdenv.hostPlatform.isStatic}" 129 "${setTarget}.crt-static=${lib.boolToString stdenv.targetPlatform.isStatic}" 130 ] ++ optionals (!withBundledLLVM) [ 131 "--enable-llvm-link-shared" 132 "${setBuild}.llvm-config=${llvmSharedForBuild.dev}/bin/llvm-config" 133 "${setHost}.llvm-config=${llvmSharedForHost.dev}/bin/llvm-config" 134 "${setTarget}.llvm-config=${llvmSharedForTarget.dev}/bin/llvm-config" 135 ] ++ optionals (stdenv.isLinux && !stdenv.targetPlatform.isRedox) [ 136 "--enable-profiler" # build libprofiler_builtins 137 ] ++ optionals stdenv.buildPlatform.isMusl [ 138 "${setBuild}.musl-root=${pkgsBuildBuild.targetPackages.stdenv.cc.libc}" 139 ] ++ optionals stdenv.hostPlatform.isMusl [ 140 "${setHost}.musl-root=${pkgsBuildHost.targetPackages.stdenv.cc.libc}" 141 ] ++ optionals stdenv.targetPlatform.isMusl [ 142 "${setTarget}.musl-root=${pkgsBuildTarget.targetPackages.stdenv.cc.libc}" 143 ] ++ optionals stdenv.targetPlatform.rust.isNoStdTarget [ 144 "--disable-docs" 145 ] ++ optionals (stdenv.isDarwin && stdenv.isx86_64) [ 146 # https://github.com/rust-lang/rust/issues/92173 147 "--set rust.jemalloc" 148 ]; 149 150 # if we already have a rust compiler for build just compile the target std 151 # library and reuse compiler 152 buildPhase = if fastCross then " 153 runHook preBuild 154 155 mkdir -p build/${stdenv.hostPlatform.rust.rustcTargetSpec}/stage0-{std,rustc}/${stdenv.hostPlatform.rust.rustcTargetSpec}/release/ 156 ln -s ${rustc}/lib/rustlib/${stdenv.hostPlatform.rust.rustcTargetSpec}/libstd-*.so build/${stdenv.hostPlatform.rust.rustcTargetSpec}/stage0-std/${stdenv.hostPlatform.rust.rustcTargetSpec}/release/libstd.so 157 ln -s ${rustc}/lib/rustlib/${stdenv.hostPlatform.rust.rustcTargetSpec}/librustc_driver-*.so build/${stdenv.hostPlatform.rust.rustcTargetSpec}/stage0-rustc/${stdenv.hostPlatform.rust.rustcTargetSpec}/release/librustc.so 158 ln -s ${rustc}/bin/rustc build/${stdenv.hostPlatform.rust.rustcTargetSpec}/stage0-rustc/${stdenv.hostPlatform.rust.rustcTargetSpec}/release/rustc-main 159 touch build/${stdenv.hostPlatform.rust.rustcTargetSpec}/stage0-std/${stdenv.hostPlatform.rust.rustcTargetSpec}/release/.libstd.stamp 160 touch build/${stdenv.hostPlatform.rust.rustcTargetSpec}/stage0-rustc/${stdenv.hostPlatform.rust.rustcTargetSpec}/release/.librustc.stamp 161 python ./x.py --keep-stage=0 --stage=1 build library/std 162 163 runHook postBuild 164 " else null; 165 166 installPhase = if fastCross then '' 167 runHook preInstall 168 169 python ./x.py --keep-stage=0 --stage=1 install library/std 170 mkdir -v $out/bin $doc $man 171 makeWrapper ${rustc}/bin/rustc $out/bin/rustc --add-flags "--sysroot $out" 172 makeWrapper ${rustc}/bin/rustdoc $out/bin/rustdoc --add-flags "--sysroot $out" 173 ln -s ${rustc}/lib/rustlib/{manifest-rust-std-,}${stdenv.hostPlatform.rust.rustcTargetSpec} $out/lib/rustlib/ 174 echo rust-std-${stdenv.hostPlatform.rust.rustcTargetSpec} >> $out/lib/rustlib/components 175 lndir ${rustc.doc} $doc 176 lndir ${rustc.man} $man 177 178 runHook postInstall 179 '' else null; 180 181 # The bootstrap.py will generated a Makefile that then executes the build. 182 # The BOOTSTRAP_ARGS used by this Makefile must include all flags to pass 183 # to the bootstrap builder. 184 postConfigure = '' 185 substituteInPlace Makefile \ 186 --replace 'BOOTSTRAP_ARGS :=' 'BOOTSTRAP_ARGS := --jobs $(NIX_BUILD_CORES)' 187 ''; 188 189 # the rust build system complains that nix alters the checksums 190 dontFixLibtool = true; 191 192 inherit patches; 193 194 postPatch = '' 195 patchShebangs src/etc 196 197 ${optionalString (!withBundledLLVM) "rm -rf src/llvm"} 198 199 # Useful debugging parameter 200 # export VERBOSE=1 201 '' + lib.optionalString (stdenv.targetPlatform.isMusl && !stdenv.targetPlatform.isStatic) '' 202 # Upstream rustc still assumes that musl = static[1]. The fix for 203 # this is to disable crt-static by default for non-static musl 204 # targets. 205 # 206 # Even though Cargo will build build.rs files for the build platform, 207 # cross-compiling _from_ musl appears to work fine, so we only need 208 # to do this when rustc's target platform is dynamically linked musl. 209 # 210 # [1]: https://github.com/rust-lang/compiler-team/issues/422 211 substituteInPlace compiler/rustc_target/src/spec/linux_musl_base.rs \ 212 --replace "base.crt_static_default = true" "base.crt_static_default = false" 213 '' + lib.optionalString (stdenv.isDarwin && stdenv.isx86_64) '' 214 # See https://github.com/jemalloc/jemalloc/issues/1997 215 # Using a value of 48 should work on both emulated and native x86_64-darwin. 216 export JEMALLOC_SYS_WITH_LG_VADDR=48 217 '' + lib.optionalString (!(finalAttrs.src.passthru.isReleaseTarball or false)) '' 218 mkdir .cargo 219 cat > .cargo/config <<\EOF 220 [source.crates-io] 221 replace-with = "vendored-sources" 222 [source.vendored-sources] 223 directory = "vendor" 224 EOF 225 ''; 226 227 # rustc unfortunately needs cmake to compile llvm-rt but doesn't 228 # use it for the normal build. This disables cmake in Nix. 229 dontUseCmakeConfigure = true; 230 231 depsBuildBuild = [ pkgsBuildHost.stdenv.cc pkg-config ]; 232 233 nativeBuildInputs = [ 234 file python3 rustc cmake 235 which libffi removeReferencesTo pkg-config xz 236 ] 237 ++ optionals fastCross [ lndir makeWrapper ]; 238 239 buildInputs = [ openssl ] 240 ++ optionals stdenv.isDarwin [ libiconv Security ] 241 ++ optional (!withBundledLLVM) llvmShared; 242 243 outputs = [ "out" "man" "doc" ]; 244 setOutputFlags = false; 245 246 postInstall = lib.optionalString (enableRustcDev && !fastCross) '' 247 # install rustc-dev components. Necessary to build rls, clippy... 248 python x.py dist rustc-dev 249 tar xf build/dist/rustc-dev*tar.gz 250 cp -r rustc-dev*/rustc-dev*/lib/* $out/lib/ 251 rm $out/lib/rustlib/install.log 252 for m in $out/lib/rustlib/manifest-rust* 253 do 254 sort --output=$m < $m 255 done 256 257 '' + '' 258 # remove references to llvm-config in lib/rustlib/x86_64-unknown-linux-gnu/codegen-backends/librustc_codegen_llvm-llvm.so 259 # and thus a transitive dependency on ncurses 260 find $out/lib -name "*.so" -type f -exec remove-references-to -t ${llvmShared} '{}' '+' 261 262 # remove uninstall script that doesn't really make sense for Nix. 263 rm $out/lib/rustlib/uninstall.sh 264 ''; 265 266 configurePlatforms = []; 267 268 enableParallelBuilding = true; 269 270 setupHooks = ./setup-hook.sh; 271 272 requiredSystemFeatures = [ "big-parallel" ]; 273 274 passthru = { 275 llvm = llvmShared; 276 inherit llvmPackages; 277 tests = { 278 inherit fd ripgrep wezterm; 279 } // lib.optionalAttrs stdenv.hostPlatform.isLinux { inherit firefox thunderbird; }; 280 }; 281 282 meta = with lib; { 283 homepage = "https://www.rust-lang.org/"; 284 description = "A safe, concurrent, practical language"; 285 maintainers = with maintainers; [ havvy ] ++ teams.rust.members; 286 license = [ licenses.mit licenses.asl20 ]; 287 platforms = [ 288 # Platforms with host tools from 289 # https://doc.rust-lang.org/nightly/rustc/platform-support.html 290 "x86_64-darwin" "i686-darwin" "aarch64-darwin" 291 "i686-freebsd13" "x86_64-freebsd13" 292 "x86_64-solaris" 293 "aarch64-linux" "armv6l-linux" "armv7l-linux" "i686-linux" 294 "loongarch64-linux" "powerpc64-linux" "powerpc64le-linux" 295 "riscv64-linux" "s390x-linux" "x86_64-linux" 296 "aarch64-netbsd" "armv7l-netbsd" "i686-netbsd" "powerpc-netbsd" 297 "x86_64-netbsd" 298 "i686-openbsd" "x86_64-openbsd" 299 "i686-windows" "x86_64-windows" 300 ]; 301 }; 302})