diff --git a/lib/systems/examples.nix b/lib/systems/examples.nix index 5059f6e0c8384..ee44f50134542 100644 --- a/lib/systems/examples.nix +++ b/lib/systems/examples.nix @@ -353,6 +353,7 @@ rec { wasi32 = { config = "wasm32-unknown-wasi"; + rustc.config = "wasm32-wasip1"; useLLVM = true; }; diff --git a/pkgs/build-support/bintools-wrapper/default.nix b/pkgs/build-support/bintools-wrapper/default.nix index b4736bc960fa4..cc0e699d15bfd 100644 --- a/pkgs/build-support/bintools-wrapper/default.nix +++ b/pkgs/build-support/bintools-wrapper/default.nix @@ -235,6 +235,12 @@ stdenvNoCC.mkDerivation { basename=$(basename "$variant") wrap $basename ${./ld-wrapper.sh} $variant done + '' + optionalString targetPlatform.isWasi '' + wrap ${targetPrefix}wasm-ld ${./ld-wrapper.sh} $ldPath/${targetPrefix}wasm-ld + # clang doesn't properly normalize the target triple[1] before using it as a search path, + # and rustc passes wasm32-wasi as `-target`, which gets used as is. Work around. + # [1] https://github.com/llvm/llvm-project/blob/ad7aeb0ff58ebd29f68adb85c64e8010639e2a76/clang/lib/Driver/Driver.cpp#L6187 + ln -s $out/bin/${targetPrefix}wasm-ld $out/bin/wasm32-wasi-wasm-ld ''; strictDeps = true; diff --git a/pkgs/build-support/rust/lib/default.nix b/pkgs/build-support/rust/lib/default.nix index c23f779bfe5d9..bbd01721ad5ea 100644 --- a/pkgs/build-support/rust/lib/default.nix +++ b/pkgs/build-support/rust/lib/default.nix @@ -14,18 +14,23 @@ rec { # passed on a build=x86_64/host=aarch64 compilation. envVars = let - ccForBuild = "${pkgsBuildHost.stdenv.cc}/bin/${pkgsBuildHost.stdenv.cc.targetPrefix}cc"; - cxxForBuild = "${pkgsBuildHost.stdenv.cc}/bin/${pkgsBuildHost.stdenv.cc.targetPrefix}c++"; + # should match pkgs/development/compilers/rust/rustc.nix + prefixForStdenv = stdenv: "${stdenv.cc}/bin/${stdenv.cc.targetPrefix}"; + ccForStdenv = stdenv: "${prefixForStdenv stdenv}${if (stdenv.cc.isClang or false) then "clang" else "gcc"}"; + cxxForStdenv = stdenv: "${prefixForStdenv stdenv}${if (stdenv.cc.isClang or false) then "clang++" else "g++"}"; - ccForHost = "${stdenv.cc}/bin/${stdenv.cc.targetPrefix}cc"; - cxxForHost = "${stdenv.cc}/bin/${stdenv.cc.targetPrefix}c++"; + ccForBuild = ccForStdenv pkgsBuildHost.stdenv; + cxxForBuild = cxxForStdenv pkgsBuildHost.stdenv; + + ccForHost = ccForStdenv stdenv; + cxxForHost = cxxForStdenv stdenv; # Unfortunately we must use the dangerous `pkgsTargetTarget` here # because hooks are artificially phase-shifted one slot earlier # (they go in nativeBuildInputs, so the hostPlatform looks like # a targetPlatform to them). - ccForTarget = "${pkgsTargetTarget.stdenv.cc}/bin/${pkgsTargetTarget.stdenv.cc.targetPrefix}cc"; - cxxForTarget = "${pkgsTargetTarget.stdenv.cc}/bin/${pkgsTargetTarget.stdenv.cc.targetPrefix}c++"; + ccForTarget = ccForStdenv pkgsTargetTarget.stdenv; + cxxForTarget = cxxForStdenv pkgsTargetTarget.stdenv; rustBuildPlatform = stdenv.buildPlatform.rust.rustcTarget; rustBuildPlatformSpec = stdenv.buildPlatform.rust.rustcTargetSpec; diff --git a/pkgs/development/compilers/rust/1_81.nix b/pkgs/development/compilers/rust/1_81.nix index b433504c81541..c482e23099c59 100644 --- a/pkgs/development/compilers/rust/1_81.nix +++ b/pkgs/development/compilers/rust/1_81.nix @@ -32,18 +32,21 @@ let llvmSharedFor = pkgSet: - pkgSet.llvmPackages_18.libllvm.override ( - { - enableSharedLibraries = true; - } - // lib.optionalAttrs (stdenv.targetPlatform.useLLVM or false) { - # Force LLVM to compile using clang + LLVM libs when targeting pkgsLLVM - stdenv = pkgSet.stdenv.override { - allowedRequisites = null; - cc = pkgSet.llvmPackages_18.clangUseLLVM; - }; - } - ); + if !stdenv.targetPlatform.isWasi then + pkgSet.llvmPackages_18.libllvm.override ( + { + enableSharedLibraries = true; + } + // lib.optionalAttrs (stdenv.targetPlatform.useLLVM or false) { + # Force LLVM to compile using clang + LLVM libs when targeting pkgsLLVM + stdenv = pkgSet.stdenv.override { + allowedRequisites = null; + cc = pkgSet.llvmPackages_18.clangUseLLVM; + }; + } + ) + else + pkgSet.llvmPackages_18.libllvm; in import ./default.nix { @@ -59,7 +62,7 @@ import ./default.nix # Expose llvmPackages used for rustc from rustc via passthru for LTO in Firefox llvmPackages = - if (stdenv.targetPlatform.useLLVM or false) then + if ((stdenv.targetPlatform.useLLVM or false) && !stdenv.targetPlatform.isWasi) then callPackage ( { pkgs, diff --git a/pkgs/development/compilers/rust/rustc.nix b/pkgs/development/compilers/rust/rustc.nix index f22add43a6d8c..5352e845d9ad7 100644 --- a/pkgs/development/compilers/rust/rustc.nix +++ b/pkgs/development/compilers/rust/rustc.nix @@ -84,8 +84,8 @@ in stdenv.mkDerivation (finalAttrs: { # Reference: https://github.com/rust-lang/rust/blob/master/src/bootstrap/configure.py configureFlags = let prefixForStdenv = stdenv: "${stdenv.cc}/bin/${stdenv.cc.targetPrefix}"; - ccPrefixForStdenv = stdenv: "${prefixForStdenv stdenv}${if (stdenv.cc.isClang or false) then "clang" else "cc"}"; - cxxPrefixForStdenv = stdenv: "${prefixForStdenv stdenv}${if (stdenv.cc.isClang or false) then "clang++" else "c++"}"; + ccPrefixForStdenv = stdenv: "${prefixForStdenv stdenv}${if (stdenv.cc.isClang or false) then "clang" else "gcc"}"; + cxxPrefixForStdenv = stdenv: "${prefixForStdenv stdenv}${if (stdenv.cc.isClang or false) then "clang++" else "g++"}"; setBuild = "--set=target.${stdenv.buildPlatform.rust.rustcTarget}"; setHost = "--set=target.${stdenv.hostPlatform.rust.rustcTarget}"; setTarget = "--set=target.${stdenv.targetPlatform.rust.rustcTarget}"; @@ -167,6 +167,8 @@ in stdenv.mkDerivation (finalAttrs: { "${setHost}.musl-root=${pkgsBuildHost.targetPackages.stdenv.cc.libc}" ] ++ optionals stdenv.targetPlatform.isMusl [ "${setTarget}.musl-root=${pkgsBuildTarget.targetPackages.stdenv.cc.libc}" + ] ++ optionals stdenv.targetPlatform.isWasi [ + "${setTarget}.wasi-root=${pkgsBuildTarget.targetPackages.stdenv.cc.libc}" ] ++ optionals stdenv.targetPlatform.rust.isNoStdTarget [ "--disable-docs" ] ++ optionals (stdenv.hostPlatform.isDarwin && stdenv.hostPlatform.isx86_64) [ @@ -223,6 +225,10 @@ in stdenv.mkDerivation (finalAttrs: { substituteInPlace compiler/rustc_target/src/spec/*/*.rs \ --replace-quiet '"rust-lld"' '"lld"' + # Figuring out when this should be False is a rustc FIXME. Nixpkgs needs it false. + substituteInPlace compiler/rustc_target/src/spec/targets/wasm32_wasi*.rs \ + --replace LinkSelfContainedDefault::True LinkSelfContainedDefault::False + ${optionalString (!withBundledLLVM) "rm -rf src/llvm"} # Useful debugging parameter @@ -262,7 +268,7 @@ in stdenv.mkDerivation (finalAttrs: { buildInputs = [ openssl ] ++ optionals stdenv.hostPlatform.isDarwin [ libiconv Security zlib ] ++ optional (!withBundledLLVM) llvmShared.lib - ++ optional (useLLVM && !withBundledLLVM) [ + ++ optional (useLLVM && !withBundledLLVM && !stdenv.targetPlatform.isWasi) [ llvmPackages.libunwind # Hack which is used upstream https://github.com/gentoo/gentoo/blob/master/dev-lang/rust/rust-1.78.0.ebuild#L284 (runCommandLocal "libunwind-libgcc" {} '' diff --git a/pkgs/development/libraries/wasilibc/default.nix b/pkgs/development/libraries/wasilibc/default.nix index c0714a78556a3..83d990e5b94aa 100644 --- a/pkgs/development/libraries/wasilibc/default.nix +++ b/pkgs/development/libraries/wasilibc/default.nix @@ -51,6 +51,11 @@ stdenv.mkDerivation { preFixup = '' ln -s $share/share/undefined-symbols.txt $out/lib/wasi.imports + # rustc build expects lib at the path where a make install would put them without the SYSROOT_* overrides + TARGET_TRIPLE=wasm32-wasip1 + ln -s $out/lib/ $out/lib/$TARGET_TRIPLE + ln -s $dev/include $dev/include/$TARGET_TRIPLE + ln -s $share/share $share/share/$TARGET_TRIPLE ''; passthru.tests = {