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

cross-compilation fails when dependencies have build-scripts #181

Closed
noonien opened this issue Jul 5, 2021 · 4 comments · Fixed by #256
Closed

cross-compilation fails when dependencies have build-scripts #181

noonien opened this issue Jul 5, 2021 · 4 comments · Fixed by #256

Comments

@noonien
Copy link

noonien commented Jul 5, 2021

To reproduce, one can copy the cross-windows example, and add this as a dependency:

serde = { version = "1.0.125", features = [ "derive" ] }

Remember to cargo update to update the lock file!

This is the output i get:

$ nix build
error: builder for '/nix/store/8n1iakh91cyin8ljvnl7nphrdgys5al2-cross-windows-0.1.0.drv' failed with exit code 101;
       last 10 log lines:
       >   = note: "cc" "-Wl,--as-needed" "-Wl,-z,noexecstack" "-m64" "-Wl,--eh-frame-hdr" "-L" "/nix/store/7817a002q6061ly4w913mqd81r8wbg7w-rust-nightly-mixed/lib/rustlib/x86_64-unknown-linux-gnu/lib" "/build/source/target/release/build/proc-macro2-3a9ea7babb00e00e/build_script_build-3a9ea7babb00e00e.build_script_build.87w02aon-cgu.0.rcgu.o" "/build/source/target/release/build/proc-macro2-3a9ea7babb00e00e/build_script_build-3a9ea7babb00e00e.build_script_build.87w02aon-cgu.1.rcgu.o" "/build/source/target/release/build/proc-macro2-3a9ea7babb00e00e/build_script_build-3a9ea7babb00e00e.build_script_build.87w02aon-cgu.10.rcgu.o" "/build/source/target/release/build/proc-macro2-3a9ea7babb00e00e/build_script_build-3a9ea7babb00e00e.build_script_build.87w02aon-cgu.11.rcgu.o" "/build/source/target/release/build/proc-macro2-3a9ea7babb00e00e/build_script_build-3a9ea7babb00e00e.build_script_build.87w02aon-cgu.12.rcgu.o" "/build/source/target/release/build/proc-macro2-3a9ea7babb00e00e/build_script_build-3a9ea7babb00e00e.build_script_build.87w02aon-cgu.13.rcgu.o" "/build/source/target/release/build/proc-macro2-3a9ea7babb00e00e/build_script_build-3a9ea7babb00e00e.build_script_build.87w02aon-cgu.14.rcgu.o" "/build/source/target/release/build/proc-macro2-3a9ea7babb00e00e/build_script_build-3a9ea7babb00e00e.build_script_build.87w02aon-cgu.15.rcgu.o" "/build/source/target/release/build/proc-macro2-3a9ea7babb00e00e/build_script_build-3a9ea7babb00e00e.build_script_build.87w02aon-cgu.2.rcgu.o" "/build/source/target/release/build/proc-macro2-3a9ea7babb00e00e/build_script_build-3a9ea7babb00e00e.build_script_build.87w02aon-cgu.3.rcgu.o" "/build/source/target/release/build/proc-macro2-3a9ea7babb00e00e/build_script_build-3a9ea7babb00e00e.build_script_build.87w02aon-cgu.4.rcgu.o" "/build/source/target/release/build/proc-macro2-3a9ea7babb00e00e/build_script_build-3a9ea7babb00e00e.build_script_build.87w02aon-cgu.5.rcgu.o" "/build/source/target/release/build/proc-macro2-3a9ea7babb00e00e/build_script_build-3a9ea7babb00e00e.build_script_build.87w02aon-cgu.6.rcgu.o" "/build/source/target/release/build/proc-macro2-3a9ea7babb00e00e/build_script_build-3a9ea7babb00e00e.build_script_build.87w02aon-cgu.7.rcgu.o" "/build/source/target/release/build/proc-macro2-3a9ea7babb00e00e/build_script_build-3a9ea7babb00e00e.build_script_build.87w02aon-cgu.8.rcgu.o" "/build/source/target/release/build/proc-macro2-3a9ea7babb00e00e/build_script_build-3a9ea7babb00e00e.build_script_build.87w02aon-cgu.9.rcgu.o" "-o" "/build/source/target/release/build/proc-macro2-3a9ea7babb00e00e/build_script_build-3a9ea7babb00e00e" "/build/source/target/release/build/proc-macro2-3a9ea7babb00e00e/build_script_build-3a9ea7babb00e00e.1hszvpc0qdb2i936.rcgu.o" "-Wl,--gc-sections" "-pie" "-Wl,-zrelro" "-Wl,-znow" "-nodefaultlibs" "-L" "/build/source/target/release/deps" "-L" "/nix/store/7817a002q6061ly4w913mqd81r8wbg7w-rust-nightly-mixed/lib/rustlib/x86_64-unknown-linux-gnu/lib" "-Wl,--start-group" "-Wl,-Bstatic" "/nix/store/m3j63f9br5qi3za971bj2jxjpbm6pba6-rust-std-nightly-2021-03-25/lib/rustlib/x86_64-unknown-linux-gnu/lib/libstd-61556e43b375c2c0.rlib" "/nix/store/m3j63f9br5qi3za971bj2jxjpbm6pba6-rust-std-nightly-2021-03-25/lib/rustlib/x86_64-unknown-linux-gnu/lib/libpanic_unwind-787faa8b02fbd963.rlib" "/nix/store/m3j63f9br5qi3za971bj2jxjpbm6pba6-rust-std-nightly-2021-03-25/lib/rustlib/x86_64-unknown-linux-gnu/lib/libminiz_oxide-dda4c0b69607e93b.rlib" "/nix/store/m3j63f9br5qi3za971bj2jxjpbm6pba6-rust-std-nightly-2021-03-25/lib/rustlib/x86_64-unknown-linux-gnu/lib/libadler-4b7dae8949ac132c.rlib" "/nix/store/m3j63f9br5qi3za971bj2jxjpbm6pba6-rust-std-nightly-2021-03-25/lib/rustlib/x86_64-unknown-linux-gnu/lib/libobject-978e97832b309706.rlib" "/nix/store/m3j63f9br5qi3za971bj2jxjpbm6pba6-rust-std-nightly-2021-03-25/lib/rustlib/x86_64-unknown-linux-gnu/lib/libaddr2line-073b1b693304b876.rlib" "/nix/store/m3j63f9br5qi3za971bj2jxjpbm6pba6-rust-std-nightly-2021-03-25/lib/rustlib/x86_64-unknown-linux-gnu/lib/libgimli-c07f996a53ee6558.rlib" "/nix/store/m3j63f9br5qi3za971bj2jxjpbm6pba6-rust-std-nightly-2021-03-25/lib/rustlib/x86_64-unknown-linux-gnu/lib/librustc_demangle-0ae8ed6a282247d0.rlib" "/nix/store/m3j63f9br5qi3za971bj2jxjpbm6pba6-rust-std-nightly-2021-03-25/lib/rustlib/x86_64-unknown-linux-gnu/lib/libhashbrown-978dd04958b6ebcc.rlib" "/nix/store/m3j63f9br5qi3za971bj2jxjpbm6pba6-rust-std-nightly-2021-03-25/lib/rustlib/x86_64-unknown-linux-gnu/lib/librustc_std_workspace_alloc-14b94bdd9a47d665.rlib" "/nix/store/m3j63f9br5qi3za971bj2jxjpbm6pba6-rust-std-nightly-2021-03-25/lib/rustlib/x86_64-unknown-linux-gnu/lib/libunwind-bff7534e4dfcef6c.rlib" "/nix/store/m3j63f9br5qi3za971bj2jxjpbm6pba6-rust-std-nightly-2021-03-25/lib/rustlib/x86_64-unknown-linux-gnu/lib/libcfg_if-022f1a0e7cd794ec.rlib" "/nix/store/m3j63f9br5qi3za971bj2jxjpbm6pba6-rust-std-nightly-2021-03-25/lib/rustlib/x86_64-unknown-linux-gnu/lib/liblibc-ff456575f1773ef0.rlib" "/nix/store/m3j63f9br5qi3za971bj2jxjpbm6pba6-rust-std-nightly-2021-03-25/lib/rustlib/x86_64-unknown-linux-gnu/lib/liballoc-3aeb407930ebd519.rlib" "/nix/store/m3j63f9br5qi3za971bj2jxjpbm6pba6-rust-std-nightly-2021-03-25/lib/rustlib/x86_64-unknown-linux-gnu/lib/librustc_std_workspace_core-6ab1ee6dbc17ad08.rlib" "/nix/store/m3j63f9br5qi3za971bj2jxjpbm6pba6-rust-std-nightly-2021-03-25/lib/rustlib/x86_64-unknown-linux-gnu/lib/libcore-166dae07beec0398.rlib" "-Wl,--end-group" "/nix/store/m3j63f9br5qi3za971bj2jxjpbm6pba6-rust-std-nightly-2021-03-25/lib/rustlib/x86_64-unknown-linux-gnu/lib/libcompiler_builtins-761b290f47712921.rlib" "-Wl,-Bdynamic" "-lgcc_s" "-lutil" "-lrt" "-lpthread" "-lm" "-ldl" "-lc"
       >   = note: /nix/store/xdii8qvch5h8chyp0z2is2qzky565w68-binutils-2.35.1/bin/ld: /nix/store/qah4dp8h83cqyc5n19a5rw85hlmmmrwh-pthreads-w32-2.9.1-x86_64-w64-mingw32/lib/libpthread.a: error adding symbols: file format not recognized
       >           collect2: error: ld returned 1 exit status
       >           
       >
       >
       > error: aborting due to previous error
       >
       >
       > [naersk] cargo returned with exit code 101, exiting
       For full logs, run 'nix log /nix/store/8n1iakh91cyin8ljvnl7nphrdgys5al2-cross-windows-0.1.0.drv'
@noonien
Copy link
Author

noonien commented Jul 5, 2021

From what I can tell, compilation is done in two stages, first it compiles and runs the build scripts (for procmacros and build.rs), then the cross-compilation phase occurs.

The first phase requires a native compiler, but because

NIX_LDFLAGS= -L/nix/store/rp555fl22y5c89whw9b541c34165a5kc-mcfgthreads-x86_64-w64-mingw32-git/lib -L/nix/store/8mrj7m3r50gfhz964n72rlyhin8qpbm1-mingw-w64-x86_64-w64-mingw32-6.0.0-pthreads-x86_64-w64-mingw32/lib -L/nix/store/72r6kaspk4sf7dzzlyc9n0m5l9lhylr8-pthreads-w32-x86_64-w64-mingw32-2.9.1/lib

the native compiler cannot interpret libpthread.a when compiling the build scripts.

To verify this, running export NIX_LDFLAGS= before manually running a cargo build seems to advance to the cross compilation stage, which then obviously fails, because it cannot find libpthread.a anymore.

@noonien
Copy link
Author

noonien commented Jul 5, 2021

export CARGO_TARGET_X86_64_PC_WINDOWS_GNU_RUSTFLAGS="-C link-args=$NIX_LDFLAGS" 
export NIX_LDFLAGS=
cargo build --release --target x86_64-pc-windows-gnu --verbose

doing this seems to fix the build for me, however, this will probably fail if the build scripts have dependencies that need to be specified

@noonien
Copy link
Author

noonien commented Jul 5, 2021

preBuild = ''
  export CARGO_TARGET_X86_64_PC_WINDOWS_GNU_RUSTFLAGS="-C link-args=''$(echo $NIX_LDFLAGS | tr ' ' '\n' | grep -- '^-L' | tr '\n' ' ')"
  export NIX_LDFLAGS=
'';

adding this seems to fix compilation, however, this is pretty much a hack.

@Patryk27
Copy link
Contributor

FWIW, something like that should work (following the cross-windows + serde example):

strictDeps = true;

depsBuildBuild = with pkgs; [
  pkgsCross.mingwW64.stdenv.cc
  pkgsCross.mingwW64.windows.pthreads
];

I'll try to refresh the example once I find some time 🙂

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants