From 6406751e1c5c774c5389d3bc3de6535bf3356175 Mon Sep 17 00:00:00 2001 From: Adam Joseph Date: Thu, 9 Mar 2023 19:38:28 -0800 Subject: [PATCH] build-rust-crate: point rustc to the correct linker I've been having problems with cross-compiled builds of crate2nix'ed crates failing at the final link step because rustc defaults to `cc` (from $PATH) as the linker. This is, of course, the buildPlatform's linker. See example below. Let's tell rustc unambiguously to use the target platform linker. I've added the flag *before* `baseRustcOpts` because that includes `extraRustcOpts`. This ensures that users of the package can override this with their own `-C linker` choice. Unfortunately because build-rust-crate (currently) sets binary-crate-specific flags when building library crates, this causes a global rebuild of everything that uses buildRustCrate. ``` rust_html2text> Building html2text (src/main.rs) rust_html2text> Running rustc --crate-name html2text src/main.rs --crate-type bin -C opt-level=3 -C codegen-units=1 --remap-path-prefix=/build=/ --extern argparse=/nix/store/y4m00swv4aka73vfl9rbb2abq7y1vmg6-rust_argparse-0.2.2-aarch64-unknown-linux-gnu-lib/lib/libargparse-5dc9592bf7.rlib --extern html2text=/nix/store/j3p1pgh3iyjdhp8681zab7yjwpzs4kg2-rust_html2text-0.4.4-aarch64-unknown-linux-gnu-lib/lib/libhtml2text-d6e376dd78.rlib --cfg feature="default" --target aarch64-unknown-linux-gnu --edition 2021 --out-dir target/bin -L dependency=target/deps --cap-lints allow --color always rust_html2text> error: linking with `cc` failed: exit status: 1 rust_html2text> | rust_html2text> = note: "cc" "/build/rustcgg6BNC/symbols.o" "target/bin/html2text.html2text.a6551786-cgu.0.rcgu.o" "target/bin/html2text.82b4749yb02lf9k.rcgu.o" "-Wl,--as-needed" "-L" "target/deps" "-L" "/nix/store/bhx7z81c5w1d7b1qny9s198mkqzj6a1r-rustc-1.67.0/lib/rustlib/aarch64-unknown-linux-gnu/lib" "-Wl,-Bstatic" "/nix/store/j3p1pgh3iyjdhp8681zab7yjwpzs4kg2-rust_html2text-0.4.4-aarch64-unknown-linux-gnu-lib/lib/libhtml2text-d6e376dd78.rlib" "/nix/store/rbal9imr15fb674mwjjhd75p45ajql25-rust_unicode-width-0.1.10-aarch64-unknown-linux-gnu-lib/lib/libunicode_width-a3b8f6ce6d.rlib" "/nix/store/5j6pf8bbk3920klpn2zlv4q6d0bbjk26-rust_html5ever-0.26.0-aarch64-unknown-linux-gnu-lib/lib/libhtml5ever-7c514c0d78.rlib" "/nix/store/2k5ncqkn1ngq1q7mjccsh3di2r9x7b3m-rust_log-0.4.17-aarch64-unknown-linux-gnu-lib/lib/liblog-bbb1d2abad.rlib" "/nix/store/yklfi1k7iaw4khbay1avvgmrrnb3sw8f-rust_markup5ever-0.11.0-aarch64-unknown-linux-gnu-lib/lib/libmarkup5ever-fa92c5d6d6.rlib" "/nix/store/cp0raar4bvl551n7521rrvk8cka9dkcv-rust_string_cache-0.8.4-aarch64-unknown-linux-gnu-lib/lib/libstring_cache-3ae24c7e2e.rlib" "/nix/store/w45v78l58wcpnwrhf83ysix1nb3177d3-rust_precomputed-hash-0.1.1-aarch64-unknown-linux-gnu-lib/lib/libprecomputed_hash-dc2ae88c2c.rlib" "/nix/store/d5gv2frk6aav6y9hwv0w6a8fw3wd2sh3-rust_serde-1.0.152-aarch64-unknown-linux-gnu-lib/lib/libserde-881972cdc0.rlib" "/nix/store/zapn48k4pfma65s304xfpqakprl0lz0n-rust_parking_lot-0.12.1-aarch64-unknown-linux-gnu-lib/lib/libparking_lot-a8eeb0af3b.rlib" "/nix/store/5jjayhj13mcy52ybdagvmdqvp2i2kfc6-rust_parking_lot_core-0.9.5-aarch64-unknown-linux-gnu-lib/lib/libparking_lot_core-7b731df3c6.rlib" "/nix/store/nka1dv01my0ivfpv14jc3969lala9yj0-rust_libc-0.2.139-aarch64-unknown-linux-gnu-lib/lib/liblibc-7a4e291ec8.rlib" "/nix/store/40i3315py200bzjd9nzds6l73drfs5rq-rust_cfg-if-1.0.0-aarch64-unknown-linux-gnu-lib/lib/libcfg_if-36a232ac81.rlib" "/nix/store/mj38xbiskkz39gdwm6sg4xr73k3wiixh-rust_smallvec-1.10.0-aarch64-unknown-linux-gnu-lib/lib/libsmallvec-e668ae4802.rlib" "/nix/store/j15ldgxp88dz06cgfl6ksvbnhk5kg1l7-rust_lock_api-0.4.9-aarch64-unknown-linux-gnu-lib/lib/liblock_api-bd3f4cafbe.rlib" "/nix/store/w7f8q6idl5pl7hssfq0gn2h7nr33556g-rust_scopeguard-1.1.0-aarch64-unknown-linux-gnu-lib/lib/libscopeguard-2ef121592d.rlib" "/nix/store/4vsr3kifpdb1dkac5j2l9rafjsypcdj6-rust_once_cell-1.17.0-aarch64-unknown-linux-gnu-lib/lib/libonce_cell-8a9caa1812.rlib" "/nix/store/sivn38f9bffq7pwiwhrisbpk16slci51-rust_phf-0.10.1-aarch64-unknown-linux-gnu-lib/lib/libphf-c5c68bbc03.rlib" "/nix/store/dfl1c90pddaf9kqpdpmy546s21vmgy90-rust_phf_shared-0.10.0-aarch64-unknown-linux-gnu-lib/lib/libphf_shared-0da64f265f.rlib" "/nix/store/vcq3zp2l7bjldknvg3894nvmafndl4p3-rust_siphasher-0.3.10-aarch64-unknown-linux-gnu-lib/lib/libsiphasher-57560c9ade.rlib" "/nix/store/ysycp6rj77l0ymdrvcndpdykidyhjapd-rust_tendril-0.4.3-aarch64-unknown-linux-gnu-lib/lib/libtendril-c7a013fefe.rlib" "/nix/store/fgc3j0s0rlpmgi4avy0gvkcwx813v74v-rust_utf-8-0.7.6-aarch64-unknown-linux-gnu-lib/lib/libutf8-a31a1dfa96.rlib" "/nix/store/vkqy90dmg0h4qkmcfr8nfa8l8nv3b0wa-rust_futf-0.1.5-aarch64-unknown-linux-gnu-lib/lib/libfutf-74eb51f206.rlib" "/nix/store/lpy11ncw9hjp8514y6qh5dalz73cammg-rust_new_debug_unreachable-1.0.4-aarch64-unknown-linux-gnu-lib/lib/libdebug_unreachable-798bc09edd.rlib" "/nix/store/972pbraa9nx2w2r9rgw9ihxcnnyjj7pq-rust_mac-0.1.1-aarch64-unknown-linux-gnu-lib/lib/libmac-5f22857ac1.rlib" "/nix/store/y4m00swv4aka73vfl9rbb2abq7y1vmg6-rust_argparse-0.2.2-aarch64-unknown-linux-gnu-lib/lib/libargparse-5dc9592bf7.rlib" "/nix/store/bhx7z81c5w1d7b1qny9s198mkqzj6a1r-rustc-1.67.0/lib/rustlib/aarch64-unknown-linux-gnu/lib/libstd-6dfb26da5641245c.rlib" "/nix/store/bhx7z81c5w1d7b1qny9s198mkqzj6a1r-rustc-1.67.0/lib/rustlib/aarch64-unknown-linux-gnu/lib/libpanic_unwind-17fe203b55bc0ad4.rlib" "/nix/store/bhx7z81c5w1d7b1qny9s198mkqzj6a1r-rustc-1.67.0/lib/rustlib/aarch64-unknown-linux-gnu/lib/libobject-fcd32571c597d1d2.rlib" "/nix/store/bhx7z81c5w1d7b1qny9s198mkqzj6a1r-rustc-1.67.0/lib/rustlib/aarch64-unknown-linux-gnu/lib/libmemchr-9bea2c040bb6c3bb.rlib" "/nix/store/bhx7z81c5w1d7b1qny9s198mkqzj6a1r-rustc-1.67.0/lib/rustlib/aarch64-unknown-linux-gnu/lib/libaddr2line-50f1168d6595a5a4.rlib" "/nix/store/bhx7z81c5w1d7b1qny9s198mkqzj6a1r-rustc-1.67.0/lib/rustlib/aarch64-unknown-linux-gnu/lib/libgimli-ab3cce9cf22eb54e.rlib" "/nix/store/bhx7z81c5w1d7b1qny9s198mkqzj6a1r-rustc-1.67.0/lib/rustlib/aarch64-unknown-linux-gnu/lib/librustc_demangle-195cb4e110dfcf64.rlib" "/nix/store/bhx7z81c5w1d7b1qny9s198mkqzj6a1r-rustc-1.67.0/lib/rustlib/aarch64-unknown-linux-gnu/lib/libstd_detect-6e60da103a3086ca.rlib" "/nix/store/bhx7z81c5w1d7b1qny9s198mkqzj6a1r-rustc-1.67.0/lib/rustlib/aarch64-unknown-linux-gnu/lib/libhashbrown-1967902c6f47ed4d.rlib" "/nix/store/bhx7z81c5w1d7b1qny9s198mkqzj6a1r-rustc-1.67.0/lib/rustlib/aarch64-unknown-linux-gnu/lib/libminiz_oxide-e67a9163a25e7f3e.rlib" "/nix/store/bhx7z81c5w1d7b1qny9s198mkqzj6a1r-rustc-1.67.0/lib/rustlib/aarch64-unknown-linux-gnu/lib/libadler-3815f9058309549d.rlib" "/nix/store/bhx7z81c5w1d7b1qny9s198mkqzj6a1r-rustc-1.67.0/lib/rustlib/aarch64-unknown-linux-gnu/lib/librustc_std_workspace_alloc-761ef9e339906fe9.rlib" "/nix/store/bhx7z81c5w1d7b1qny9s198mkqzj6a1r-rustc-1.67.0/lib/rustlib/aarch64-unknown-linux-gnu/lib/libunwind-8882f6e2dc28b312.rlib" "/nix/store/bhx7z81c5w1d7b1qny9s198mkqzj6a1r-rustc-1.67.0/lib/rustlib/aarch64-unknown-linux-gnu/lib/libcfg_if-50cea0e51cd18705.rlib" "/nix/store/bhx7z81c5w1d7b1qny9s198mkqzj6a1r-rustc-1.67.0/lib/rustlib/aarch64-unknown-linux-gnu/lib/liblibc-da1cdc1aff291994.rlib" "/nix/store/bhx7z81c5w1d7b1qny9s198mkqzj6a1r-rustc-1.67.0/lib/rustlib/aarch64-unknown-linux-gnu/lib/liballoc-7019f3ab0cdf1cfd.rlib" "/nix/store/bhx7z81c5w1d7b1qny9s198mkqzj6a1r-rustc-1.67.0/lib/rustlib/aarch64-unknown-linux-gnu/lib/librustc_std_workspace_core-1944b99f47799b69.rlib" "/nix/store/bhx7z81c5w1d7b1qny9s198mkqzj6a1r-rustc-1.67.0/lib/rustlib/aarch64-unknown-linux-gnu/lib/libcore-06b1d7727c68137a.rlib" "/nix/store/bhx7z81c5w1d7b1qny9s198mkqzj6a1r-rustc-1.67.0/lib/rustlib/aarch64-unknown-linux-gnu/lib/libcompiler_builtins-bc101476bec73dcf.rlib" "-Wl,-Bdynamic" "-lgcc_s" "-lutil" "-lrt" "-lpthread" "-lm" "-ldl" "-lc" "-Wl,--eh-frame-hdr" "-Wl,-znoexecstack" "-L" "/nix/store/bhx7z81c5w1d7b1qny9s198mkqzj6a1r-rustc-1.67.0/lib/rustlib/aarch64-unknown-linux-gnu/lib" "-o" "target/bin/html2text" "-Wl,--gc-sections" "-pie" "-Wl,-zrelro,-znow" "-Wl,-O1" "-nodefaultlibs" rust_html2text> = note: /nix/store/2arn2lz66q49qhiyldkahiphfmzlixdf-binutils-2.40/bin/ld: target/bin/html2text.html2text.a6551786-cgu.0.rcgu.o: Relocations in generic ELF (EM: 183) rust_html2text> /nix/store/2arn2lz66q49qhiyldkahiphfmzlixdf-binutils-2.40/bin/ld: target/bin/html2text.html2text.a6551786-cgu.0.rcgu.o: Relocations in generic ELF (EM: 183) rust_html2text> /nix/store/2arn2lz66q49qhiyldkahiphfmzlixdf-binutils-2.40/bin/ld: target/bin/html2text.html2text.a6551786-cgu.0.rcgu.o: Relocations in generic ELF (EM: 183) rust_html2text> /nix/store/2arn2lz66q49qhiyldkahiphfmzlixdf-binutils-2.40/bin/ld: target/bin/html2text.html2text.a6551786-cgu.0.rcgu.o: Relocations in generic ELF (EM: 183) ``` --- pkgs/build-support/rust/build-rust-crate/build-crate.nix | 1 + 1 file changed, 1 insertion(+) diff --git a/pkgs/build-support/rust/build-rust-crate/build-crate.nix b/pkgs/build-support/rust/build-rust-crate/build-crate.nix index 37bf3ec26f770..944434472b0b8 100644 --- a/pkgs/build-support/rust/build-rust-crate/build-crate.nix +++ b/pkgs/build-support/rust/build-rust-crate/build-crate.nix @@ -41,6 +41,7 @@ ); binRustcOpts = lib.concatStringsSep " " ( + [ "-C linker=${stdenv.cc}/bin/${stdenv.cc.targetPrefix}cc" ] ++ baseRustcOpts );