Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rust: make pkgsCross.wasi32.rustPlatform.buildRustPackage work #323161

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/systems/examples.nix
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,7 @@ rec {

wasi32 = {
config = "wasm32-unknown-wasi";
rustc.config = "wasm32-wasip1";
useLLVM = true;
};

Expand Down
6 changes: 6 additions & 0 deletions pkgs/build-support/bintools-wrapper/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
17 changes: 11 additions & 6 deletions pkgs/build-support/rust/lib/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
29 changes: 16 additions & 13 deletions pkgs/development/compilers/rust/1_81.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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,
Expand Down
12 changes: 9 additions & 3 deletions pkgs/development/compilers/rust/rustc.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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}";
Expand Down Expand Up @@ -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) [
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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" {} ''
Expand Down
5 changes: 5 additions & 0 deletions pkgs/development/libraries/wasilibc/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
Loading