Wrong linker for Rust cross-compiling

Sailfish SDK for 4.1.0 introduced Rust cross-compiling. But it is not working for me. A really simple Rust example compiles fine, but when a C++ source have to be compiled in to use Qt i686-unknown-linux-gnu-gcc is used as linker and it fail terribly, because it is the linker for the wrong architecture.

Here is a minimal example: https://github.com/R1tschY/minimal-sfos-rust-example/tree/link_qt
It fails with (when using build target SailfishOS-4.2.0.21-armv7hl):

Loading repository data...
Reading installed packages...
'pkgconfig(Qt5Core)' not found in package names. Trying capabilities.
'qt5-qtcore-devel' providing 'pkgconfig(Qt5Core)' is already installed.
'rust' is already installed.
No update candidate for 'rust-1.52.1+git1-1.6.2.jolla.armv7hl'. The highest available version is already installed.
'cargo' is already installed.
No update candidate for 'cargo-1.52.1+git1-1.6.2.jolla.armv7hl'. The highest available version is already installed.
Resolving package dependencies...

Nothing to do.
Building target platforms: armv7hl-meego-linux
Building for target armv7hl-meego-linux
Executing(%build): /bin/sh -e /var/tmp/rpm-tmp.uDjK0j
+ umask 022
+ cd /home/richard/dev/minimal-sfos-rust-example
+ LANG=C
+ export LANG
+ unset DISPLAY
+ CFLAGS='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -Wformat -Wformat-security -fmessage-length=0 -march=armv7-a -mfloat-abi=hard -mfpu=neon -mthumb -Wno-psabi'
+ export CFLAGS
+ CXXFLAGS='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -Wformat -Wformat-security -fmessage-length=0 -march=armv7-a -mfloat-abi=hard -mfpu=neon -mthumb -Wno-psabi'
+ export CXXFLAGS
+ FFLAGS='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -Wformat -Wformat-security -fmessage-length=0 -march=armv7-a -mfloat-abi=hard -mfpu=neon -mthumb -Wno-psabi -I/usr/lib/gfortran/modules'
+ export FFLAGS
+ LD_AS_NEEDED=1
+ export LD_AS_NEEDED
+ export CARGO_INCREMENTAL=0
+ CARGO_INCREMENTAL=0
+ export CRATE_CC_NO_DEFAULTS=1
+ CRATE_CC_NO_DEFAULTS=1
+ cargo build --release --target-dir=target --manifest-path /home/richard/dev/minimal-sfos-rust-example/rpm/../Cargo.toml
   Compiling pkg-config v0.3.22
   Compiling cc v1.0.72
   Compiling ffi v0.1.0 (/home/richard/dev/minimal-sfos-rust-example/ffi)
   Compiling minimal-rust-example v0.1.0 (/home/richard/dev/minimal-sfos-rust-example)
error: linking with `i686-unknown-linux-gnu-gcc` failed: exit code: 1
  |
  = note: "i686-unknown-linux-gnu-gcc" "-Wl,--as-needed" "-Wl,-z,noexecstack" "-m32" "-Wl,--eh-frame-hdr" "-L" "/usr/lib/rustlib/i686-unknown-linux-gnu/lib" "/home/richard/dev/minimal-sfos-rust-example/target/release/deps/minimal_rust_example-7b337e6f84df763c.minimal_rust_example.iv24sfil-cgu.0.rcgu.o" "/home/richard/dev/minimal-sfos-rust-example/target/release/deps/minimal_rust_example-7b337e6f84df763c.minimal_rust_example.iv24sfil-cgu.1.rcgu.o" "/home/richard/dev/minimal-sfos-rust-example/target/release/deps/minimal_rust_example-7b337e6f84df763c.minimal_rust_example.iv24sfil-cgu.2.rcgu.o" "/home/richard/dev/minimal-sfos-rust-example/target/release/deps/minimal_rust_example-7b337e6f84df763c.minimal_rust_example.iv24sfil-cgu.3.rcgu.o" "/home/richard/dev/minimal-sfos-rust-example/target/release/deps/minimal_rust_example-7b337e6f84df763c.minimal_rust_example.iv24sfil-cgu.4.rcgu.o" "-o" "/home/richard/dev/minimal-sfos-rust-example/target/release/deps/minimal_rust_example-7b337e6f84df763c" "/home/richard/dev/minimal-sfos-rust-example/target/release/deps/minimal_rust_example-7b337e6f84df763c.4lrqdte2wlrae2xs.rcgu.o" "-Wl,--gc-sections" "-pie" "-Wl,-zrelro" "-Wl,-znow" "-Wl,-O1" "-nodefaultlibs" "-L" "/home/richard/dev/minimal-sfos-rust-example/target/release/deps" "-L" "/home/richard/dev/minimal-sfos-rust-example/target/release/build/ffi-fa967afc240def32/out" "-L" "/usr/lib/rustlib/i686-unknown-linux-gnu/lib" "-Wl,-Bstatic" "/home/richard/dev/minimal-sfos-rust-example/target/release/deps/libffi-16c5cdc88445cd57.rlib" "-Wl,--start-group" "/usr/lib/rustlib/i686-unknown-linux-gnu/lib/libstd-9fdbcd50dc856ced.rlib" "/usr/lib/rustlib/i686-unknown-linux-gnu/lib/libpanic_unwind-382b309729554ac4.rlib" "/usr/lib/rustlib/i686-unknown-linux-gnu/lib/libminiz_oxide-597309c6f6994155.rlib" "/usr/lib/rustlib/i686-unknown-linux-gnu/lib/libadler-66708cc1137ca9b4.rlib" "/usr/lib/rustlib/i686-unknown-linux-gnu/lib/libobject-814778a6f0bfff0f.rlib" "/usr/lib/rustlib/i686-unknown-linux-gnu/lib/libaddr2line-3b5b26b4f30457a5.rlib" "/usr/lib/rustlib/i686-unknown-linux-gnu/lib/libgimli-705c284a8c4a0c58.rlib" "/usr/lib/rustlib/i686-unknown-linux-gnu/lib/librustc_demangle-b29e16ab125e9694.rlib" "/usr/lib/rustlib/i686-unknown-linux-gnu/lib/libhashbrown-2363a1643aea78e5.rlib" "/usr/lib/rustlib/i686-unknown-linux-gnu/lib/librustc_std_workspace_alloc-f472bbd4e5548251.rlib" "/usr/lib/rustlib/i686-unknown-linux-gnu/lib/libunwind-e240609c19b20160.rlib" "/usr/lib/rustlib/i686-unknown-linux-gnu/lib/libcfg_if-8918a773de47e88d.rlib" "/usr/lib/rustlib/i686-unknown-linux-gnu/lib/liblibc-f163f354038dfc38.rlib" "/usr/lib/rustlib/i686-unknown-linux-gnu/lib/liballoc-2f36af8c8881dbfa.rlib" "/usr/lib/rustlib/i686-unknown-linux-gnu/lib/librustc_std_workspace_core-cc03642065cc5f53.rlib" "/usr/lib/rustlib/i686-unknown-linux-gnu/lib/libcore-b342d510d097874b.rlib" "-Wl,--end-group" "/usr/lib/rustlib/i686-unknown-linux-gnu/lib/libcompiler_builtins-dbc029aed652ac18.rlib" "-Wl,-Bdynamic" "-lQt5Core" "-lstdc++" "-lgcc_s" "-lutil" "-lrt" "-lpthread" "-lm" "-ldl" "-lc"
  = note: /usr/bin/ld: /home/richard/dev/minimal-sfos-rust-example/target/release/deps/libffi-16c5cdc88445cd57.rlib(lib.o): relocations in generic ELF (EM: 40)
          /usr/bin/ld: /home/richard/dev/minimal-sfos-rust-example/target/release/deps/libffi-16c5cdc88445cd57.rlib(lib.o): relocations in generic ELF (EM: 40)
          /usr/bin/ld: /home/richard/dev/minimal-sfos-rust-example/target/release/deps/libffi-16c5cdc88445cd57.rlib(lib.o): relocations in generic ELF (EM: 40)
          /usr/bin/ld: /home/richard/dev/minimal-sfos-rust-example/target/release/deps/libffi-16c5cdc88445cd57.rlib(lib.o): relocations in generic ELF (EM: 40)
          /usr/bin/ld: /home/richard/dev/minimal-sfos-rust-example/target/release/deps/libffi-16c5cdc88445cd57.rlib(lib.o): relocations in generic ELF (EM: 40)
          /usr/bin/ld: /home/richard/dev/minimal-sfos-rust-example/target/release/deps/libffi-16c5cdc88445cd57.rlib(lib.o): relocations in generic ELF (EM: 40)
          /usr/bin/ld: /home/richard/dev/minimal-sfos-rust-example/target/release/deps/libffi-16c5cdc88445cd57.rlib: error adding symbols: file in wrong format
          collect2: error: ld returned 1 exit status
          

error: aborting due to previous error

error: could not compile `minimal-rust-example`

To learn more, run the command again with --verbose.
error: Bad exit status from /var/tmp/rpm-tmp.uDjK0j (%build)


RPM build errors:
    Bad exit status from /var/tmp/rpm-tmp.uDjK0j (%build)

The wrong linker and rust-libs are used for armv7hl target. It works when using build target SailfishOS-4.0.1.48-armv7hl.

What am I doing wrong? And is there a way to deactivate Rust cross-compiling?

After studying the spec of the xul-runner (https://github.com/sailfishos/gecko-dev/blob/master/rpm/xulrunner-qt5.spec) I’ve got it working now.

I’m new to Sailfish OS but an experienced rustacean. I wanted to start with a minimal example but everything I found was breaking apart at some point or it supposedly works but is a hugely complex project.

You mentioned it worked for you but the last commit on your minmal example is from 2021-11-22 and that version fails with the linker error.

Could you please commit your changes or at least publish it here for posterity? I’d be very grateful :slight_smile:

Hey, the example was only there so everybody can reproduce it. Now I fixed it. For a more complex example please look at harbour-sailify/harbour-sailify.spec at master · R1tschY/harbour-sailify · GitHub