Rust Howto (Request)

With 3.4 rust support was announced, but I couldn’t find any documentation on how to get started with rust for sailfish. I understand that probably many things are not yet figured out or subject to change.

There is whisperfish, which uses rust and Actix. Asides from that there should be gecko/mozilla for which the rust support was introduced (haven’t found the place where this is build yet).

Any hints, samples, etc. are welcome

How about:

[nemo@Sailfish ~]$ pkcon install cargo rust
Resolving
Testing changes
Finished                                                                 [                                    ] (0%)
The following packages have to be installed:
[...]

Agreed, it doesn’t contain documentation … and doesn’t help with developing Rust apps with the SDK.

4 Likes

The gecko engine, which includes the rust parts of the browser, can be found here: https://git.sailfishos.org/mer-core/gecko-dev

Gecko has a complex build process, so it’s probably not the best place to start with testing out rust. Something better might be the Cargo documentation first steps. I tried them out for my own interest and it worked as expected. The steps I used are below and maybe they’re useful for you too. You may need to alter some of these commands depending on your sfdk targets etc.

# Install tools into the sfdk
sfdk tools target package-install SailfishOS-3.4.0.22EA-armv7hl rust cargo

# Create example project
sfdk engine exec sb2 -t SailfishOS-3.4.0.22EA-armv7hl cargo new hello_world
cd hello_world
cat Cargo.toml

# Build the project
sfdk engine exec sb2 -t SailfishOS-3.4.0.22EA-armv7hl cargo build

# Copy executable binary to device
scp target/debug/hello_world nemo@192.168.1.8:~

# Run the executable on device
[nemo@Columba ~]$ ./hello_world 
Hello, world!
4 Likes

Thanks a lot. This looks really good already. However I’m getting strange errors if I add a dependency (e.g. pest = “2.1.3”). It works totally fine on my machine, but not if I execute it with:

$  sfdk engine exec sb2 -t SailfishOS-3.4.0.22EA-armv7hl cargo build --verbose
       Fresh ucd-trie v0.1.3
   Compiling pest v2.1.3
     Running `rustc --crate-name pest /home/mersdk/.cargo/registry/src/github.com-1ecc6299db9ec823/pest-2.1.3/src/lib.rs --error-format=json --json=diagnostic-rendered-ansi --crate-type lib --emit=dep-info,metadata,link -C debuginfo=2 -C metadata=ef32bfed2f3a56dc -C extra-filename=-ef32bfed2f3a56dc --out-dir /home/thilo/git/private/sailfish/rustinit/rust/hello_world/target/debug/deps -L dependency=/home/thilo/git/private/sailfish/rustinit/rust/hello_world/target/debug/deps --extern ucd_trie=/home/thilo/git/private/sailfish/rustinit/rust/hello_world/target/debug/deps/libucd_trie-a67709440dd0c9c5.rmeta --cap-lints allow`
error[E0463]: can't find crate for `ucd_trie`
  --> /home/mersdk/.cargo/registry/src/github.com-1ecc6299db9ec823/pest-2.1.3/src/lib.rs:66:1
   |
66 | extern crate ucd_trie;
   | ^^^^^^^^^^^^^^^^^^^^^^ can't find crate

error: aborting due to previous error

For more information about this error, try `rustc --explain E0463`.
error: could not compile `pest`.

Caused by:
  process didn't exit successfully: `rustc --crate-name pest /home/mersdk/.cargo/registry/src/github.com-1ecc6299db9ec823/pest-2.1.3/src/lib.rs --error-format=json --json=diagnostic-rendered-ansi --crate-type lib --emit=dep-info,metadata,link -C debuginfo=2 -C metadata=ef32bfed2f3a56dc -C extra-filename=-ef32bfed2f3a56dc --out-dir /home/thilo/git/private/sailfish/rustinit/rust/hello_world/target/debug/deps -L dependency=/home/thilo/git/private/sailfish/rustinit/rust/hello_world/target/debug/deps --extern ucd_trie=/home/thilo/git/private/sailfish/rustinit/rust/hello_world/target/debug/deps/libucd_trie-a67709440dd0c9c5.rmeta --cap-lints allow` (exit code: 1)

EDIT: it seems to be a problem with transitive dependencies. ucd-trie itsself is building fine.
Probably a filesystem issue?

1 Like

I’m not sure about this I’m afraid. It does work okay for me, so may be a local error.

$ sfdk engine exec sb2 -t SailfishOS-3.4.0.22EA-armv7hl cargo build
    Updating crates.io index
  Downloaded ucd-trie v0.1.3
  Downloaded pest v2.1.3
  Downloaded 2 crates (122.6 KB) in 1.66s
   Compiling ucd-trie v0.1.3
   Compiling pest v2.1.3
   Compiling hello_world v0.1.0 (/home/flypig/Documents/Development/sailfish/rustexample/hello_world)
    Finished dev [unoptimized + debuginfo] target(s) in 2m 57s
1 Like

Hm I did a fresh install of the sfdk and do run into the same error. Any ideas where I could start looking?

If you really did do a complete reinstall then I’m not sure I’m afraid. On the off chance it survived the reinstall, you could try deleting the crate registry, like this:

sfdk engine exec sb2 -t SailfishOS-3.4.0.22EA-armv7hl rm -rf /home/mersdk/.cargo

Works now. I tried to reinstall sfdk and even made sure the machine is deleted from virtualbox, but it didnt help.
As mentioned I only tried VirtualBox before.

Now I installed the early access SDK with docker. One of both solved the problem. (docker seems likely. What were you using @flypig?)

My guess is that it was the early access SDK which made the difference. There might have been some subtle bug in the old one.

Yes, it was the docker image. I don’t know why this would make a difference though (and @vige is the expert).

I’m trying to create an example qt application with qmetaobject (patched to support qt5.6 by rubdos). I had to patch to set QT_SELECT to 5 instead of qt5. (You can use qmetaobject = { version = "0.1.4", git = "https://gitlab.com/thigg/qmetaobject-sfos-rs", branch = "sailfishos" })

It builds fine, but if I now try to run the example from the readme, I get the follwing:

[12489.691178] hello_world[28540]: undefined instruction: pc=00000000f6698bd4
[12489.691192] Code: 00000000 00000000 00000000 00000000 (ffffffff)

And if I run it from gdb, I get:

Thread 1 "hello_world" received signal SIGILL, Illegal instruction.
0xf6de4bd4 in QArrayData::shared_null () from /usr/lib/libQt5Core.so.5

If both messaged point to the same problem (what it looks like) that would mean that I got an undefined instruction in libQt5Core?

You can find the hello_world application here

Bump, anyone has an idea?

Actually: No, not really. This isn’t only an illegal instruction, it’s no instruction at all, it doesn’t seem to be a TEXT segment. With gdb, what does bt tell us? How did it get there?

Getting an illegal insrtruction from a shared_null bytearray sounds a bit irrealistic. maybe some danglinig pointer somewhere?
also, where are you compiling this? Docker / SB2?
llvm can produce the stack trace; it might be interesting to have that too (BACKTRACE_FULL or something env var)

I am compiling in docker with: sfdk engine exec sb2 -t SailfishOS-3.4.0.24-armv7hl.

i486
I tried with i486, but after sfdk tools package-install SailfishOS-3.4.0.24-i486 cargo rust I get this:

$  sfdk engine exec sb2 -t SailfishOS-3.4.0.24-i486   cargo build
error: process didn't exit successfully: `rustc -vV` (exit code: 127)
--- stderr
rustc: error while loading shared libraries: librustc_driver-001f982dd4742f21.so: cannot open shared object file: No such file or directory

ldconfig in there reports:

$ ldconfig -p | grep rust
        p11-kit-trust.so (libc6) => /usr/lib/p11-kit-trust.so
        librustc_macros-68b44492c560bf2e.so (libc6) => /usr/lib/librustc_macros-68b44492c560bf2e.so
        librustc_driver-001f982dd4742f21.so (libc6) => /usr/lib/librustc_driver-001f982dd4742f21.so

So I cant compare my phone to the emulator ( or run non-arm builds at all).

ARM

I tried to get more backtrace but failed (I have very little expertise here however, mainly just googling around). Tried RUST_BACKTRACE, but that didnt give anything.
The only further thing I found was putting a backtrace into gdb (or rust-gdb, doesnt seem to make a difference)

(gdb) bt full  
#0  0xf6de4bd4 in QArrayData::shared_null () from /usr/lib/libQt5Core.so.5
No symbol table info available.
#1  0xf68a0ce4 in ?? () from /usr/lib/libQt5Qml.so.5
No symbol table info available.
Backtrace stopped: previous frame identical to this frame (corrupt stack?)

If the RUST_BACKTRACE=full is not working (thanks for reminding the name btw), I gues the whole stack does not end up in rust at all.

FWIW, my fork was a hack to get around cross compiling Qt5.6. You might be interested in checking out this PR, which adds Qt5.6 detection to qmetaobject-rs https://github.com/woboq/qmetaobject-rs/pull/123

I’m trying to build it myself in sfdk now, to see whether it works, but I get

error: linker `arm-linux-gnueabihf-gcc` not found
  |
  = note: No such file or directory (os error 2)

… in the sb2 environment (sb2 -t SailfishOS-3.4.0.24-armv7hl cargo test --target-dir=sfdk-armv7hl/).

Here is how I compiled hello_world or @thigg Qt demo application:using @coderus Docker-based build image:

# run Docker container 
docker run --name sdk --rm -d coderus/sailfishos-platform-sdk:latest tail -f /dev/null

# launch a container shell
docker exec -it sdk bash

# install cargo and rust packages
USER=`whoami` su -p -c "sb2 -m sdk-install -t SailfishOS-3.4.0.22-armv7hl zypper -n in rust cargo"

# let's build @thigg demo application
git clone https://gitlab.com/thigg/rust-qt-sailfish-example
cd rust-qt-sailfish-example/
USER=`whoami` su -p -c "sb2 -t SailfishOS-3.4.0.22-armv7hl cargo build"

Now take a nap as building takes some time.

The final binary can be copied from running SDK container to Docker host by executing the following command on the Docker host:

docker cp sdk:/home/nemo/rust-qt-sailfish-example/target/debug/hello_world ./

From there you can copy it to your device or emulator alas it will crash with the illegal instruction error.

FYI, @coderus has just split their Docker image in three, cfr. https://hub.docker.com/r/coderus/, https://github.com/CODeRUS/docker-sailfishos-platform-sdk/issues/2

These new images contain 3.4.0.24, you might want to retry against that (former all-in image was 3.4.0.22 iirc).


Based on these images, I have built three others https://gitlab.com/rubdos/sailo-rs/, these contain the dependencies I need for Whisperfish (which is very few, on top of Rust, Cargo and Qt5-devel). These may interest you, although the build process is very different (cross compiling and -rpath-hacking, instead of emulated compiling). At least the source might interest you.

1 Like

AIO image is still and will be available as before.

1 Like