rust compile x86 library on x86_64 machine - rust

I have ubuntu x86_64 container and cargo build goes well.
But i need to build x86 library version too.
As far as I understand i need to add i686 toolchain and target.
rustup target add i686-unknown-linux-gnu done successful
rustup toolchain install stable-i686-unknown-linux-gnu finished with error
$ rustup toolchain install stable-i686-unknown-linux-gnu
info: syncing channel updates for 'stable-i686-unknown-linux-gnu'
info: latest update on 2018-11-08, rust version 1.30.1 (1433507eb 2018-11-07)
info: downloading component 'rustc'
info: downloading component 'rust-std'
info: downloading component 'cargo'
info: downloading component 'rust-docs'
info: installing component 'rustc'
info: installing component 'rust-std'
info: installing component 'cargo'
info: installing component 'rust-docs'
stable-i686-unknown-linux-gnu installed - (error reading rustc version)
and
$ rustup default stable-i686
info: using existing install for 'stable-i686-unknown-linux-gnu'
info: default toolchain set to 'stable-i686-unknown-linux-gnu'
stable-i686-unknown-linux-gnu unchanged - (error reading rustc version)
Do I missed something or took wrong approach?

Instead of changing your toolchain, you have to add the target to your current toolchain (make sure to switch back to your original toolchain first).
$ rustup target install i686-unknown-linux-gnu
$ cargo build --target=i686-unknown-linux-gnu
Of course, you need to install the 32-bit libraries on your system as well, e.g. on ubuntu you install them by
$ sudo apt install gcc-multilib
(for more information about that see How to Compile 32-bit Apps on 64-bit Ubuntu?)

Related

How to resolve linker error in RUST Cross compilation

I am trying to achieve cross compilation in rust to the raspberry pi. However there was a linkage error while compiler:
error: linking with `cc` failed: exit status: 1
However for resolving that I have to download new linker and not use the default.
While doing so I checked the information of my raspberry system by using:
rustup show
Got the following result:
Default host: x86_64-unknown-linux-gnu
rustup home: /home/akumar/.rustup
installed targets for active toolchain
--------------------------------------
arm-unknown-linux-gnueabihf
armv7-unknown-linux-gnueabihf
x86_64-unknown-linux-gnu
active toolchain
----------------
stable-x86_64-unknown-linux-gnu (default)
rustc 1.64.0 (a55dd71d5 2022-09-19)
Now for resolving the issue I have to download the package using the following command:
sudo apt install gcc-armv7-unknown-linux-gnueabihf
but after running the command, I am getting the following error:
Reading package lists... Done
Building dependency tree
Reading state information... Done
E: Unable to locate package gcc-armv7-unknown-linux-gnueabihf
Please try this:
sudo apt install gcc-arm-linux-gnueabihf
for build your rust project:
cargo build --target=armv7-unknown-linux-gnueabihf
Please check tool - cross: https://github.com/cross-rs/cross
Cross uses docker for build and makes libraries isolation.

some error failed to run custom build command for `librocksdb-sys v6.11.4`

I tried to create my first substrate chain. Create Your First Substrate Chain
But I got some error while compiling substrate.
failed to run custom build command for librocksdb-sys v6.11.4
Does anybody know how to fix this problem?
You need to install additional library:
sudo apt install clang should help.
sudo apt-get update
sudo apt install make clang pkg-config libssl-dev
This should do the trick.
Reference: https://github.com/paritytech/polkadot/issues/65
From #apopiak
Are you on Apple M1? If, so there are currently issues with building rocksdb there. See here an approach: vikiival.medium.com/run-substrate-on-apple-m1-a2699743fae8
Also others have reported needing to:
You need clone the rust-rocksdb repo and checkout the commit listed here: https://github.com/substrate-developer-hub/substrate-node-template/issues/122 then you can create a config.toml file in your .cargo folder in your home directory and add a path to that cloned repo
You had better update cargo to the nightly version and then try it.
If it keep in stuck error try comment commands, you may will need to study link.
#rustup component add --toolchain=nightly rust-src rustfmt
rustup target add wasm32-unknown-unknown
#apt-get install llvm clang linux-headers-"$(uname -r)" #
apt install llvm clang
cargo build --release
#cargo fix --allow-dirty #cargo fix --edition
#lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 20.04.3 LTS
Release: 20.04
Codename: focal
#ldconfig --version
ldconfig (Ubuntu GLIBC 2.31-0ubuntu9.2) 2.31
#cargo --version
cargo 1.60.0-nightly (25fcb13 2022-02-01)
#rustc --version
rustc 1.60.0-nightly (f624427f8 2022-02-06)
#rustup show
Default host: x86_64-unknown-linux-gnu
rustup home: /root/.rustup
installed targets for active toolchain
--------------------------------------
wasm32-unknown-unknown
x86_64-unknown-linux-gnu
active toolchain
----------------
nightly-x86_64-unknown-linux-gnu (default)
rustc 1.60.0-nightly (f624427f8 2022-02-06)
#ArmanRiazi.blockchain#Substrate#Dr.GavinWood
For details:
Walk-Through/Substrate/NodeSetup
You need to update the to this version: "6.20.3"
checksum = "c309a9d2470844aceb9a4a098cf5286154d20596868b75a6b36357d2bb9ca25d"
am running on M1!

Cross-compiling Rust's SerialPort crate

I am developing my Rust project on Ubuntu 20.04.2 LTS 64bit. I have setup the project to compile for the host architecture (using cargo build) and to cross-compile for the target archicture of ARM 64bit (using cargo build --target=aarch64-unknown-linux-gnu). The project has been cross-compiling fine up until now. On the target hardware platform there are serial ports that the application will be interacting with so I have added the Serial Port crate to my project. The project still compiles fine for the Ubuntu host architecture but I can no longer get it to cross-compile for the aarch64 taget.
I have added the target to rustup:
$ rustup target list
...
aarch64-fuchsia
aarch64-linux-android
aarch64-pc-windows-msvc
aarch64-unknown-linux-gnu (installed)
aarch64-unknown-linux-musl
aarch64-unknown-none
aarch64-unknown-none-softfloat
...
And I have installed the toolchain to rustup:
$ rustup toolchain list
stable-aarch64-unknown-linux-gnu
stable-x86_64-unknown-linux-gnu (default)
When I run the cargo build command of cargo build --target=aarch64-unknown-linux-gnu I get the following error during compiling:
$ cargo build --target=aarch64-unknown-linux-gnu
Compiling libudev-sys v0.1.4
error: failed to run custom build command for `libudev-sys v0.1.4`
Caused by:
process didn't exit successfully: `/mnt/hgfs/trunk/src/application/target/debug/build/libudev-sys-c6234f1891f55461/build-script-build` (exit code: 101)
--- stdout
cargo:rerun-if-env-changed=LIBUDEV_NO_PKG_CONFIG
cargo:rerun-if-env-changed=PKG_CONFIG_ALLOW_CROSS_aarch64-unknown-linux-gnu
cargo:rerun-if-env-changed=PKG_CONFIG_ALLOW_CROSS_aarch64_unknown_linux_gnu
cargo:rerun-if-env-changed=TARGET_PKG_CONFIG_ALLOW_CROSS
cargo:rerun-if-env-changed=PKG_CONFIG_ALLOW_CROSS
cargo:rerun-if-env-changed=PKG_CONFIG_aarch64-unknown-linux-gnu
cargo:rerun-if-env-changed=PKG_CONFIG_aarch64_unknown_linux_gnu
cargo:rerun-if-env-changed=TARGET_PKG_CONFIG
cargo:rerun-if-env-changed=PKG_CONFIG
cargo:rerun-if-env-changed=PKG_CONFIG_SYSROOT_DIR_aarch64-unknown-linux-gnu
cargo:rerun-if-env-changed=PKG_CONFIG_SYSROOT_DIR_aarch64_unknown_linux_gnu
cargo:rerun-if-env-changed=TARGET_PKG_CONFIG_SYSROOT_DIR
cargo:rerun-if-env-changed=PKG_CONFIG_SYSROOT_DIR
--- stderr
thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: "pkg-config has not been configured to support cross-compilation.\n\n Install a sysroot for the target platform and configure it via\n PKG_CONFIG_SYSROOT_DIR and PKG_CONFIG_PATH, or install a\n cross-compiling wrapper for pkg-config and set it via\n PKG_CONFIG environment variable."', /home/nocker/.cargo/registry/src/github.com-1ecc6299db9ec823/libudev-sys-0.1.4/build.rs:38:41
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
warning: build failed, waiting for other jobs to finish...
error: build failed
I have installed pkg-config and libudev-dev. From the terminal I can get this output to confirm that pkg-config is installed:
$ pkg-config --version
0.29.1
but when I try to check that libudev is installed I get an error response:
$ libudev
libudev: command not found
$ libudev-dev
libudev-dev: command not found
When I try to install libudev again I get:
$ sudo apt install libudev-dev
Reading package lists... Done
Building dependency tree
Reading state information... Done
libudev-dev is already the newest version (245.4-4ubuntu3.4).
0 upgraded, 0 newly installed, 0 to remove and 15 not upgraded.
When I try to install libudev-sys I get:
$ sudo apt install libudev-sys
Reading package lists... Done
Building dependency tree
Reading state information... Done
E: Unable to locate package libudev-sys
I'm suspecting the reason that I cannot cross-compile is because of libudev but cannot figure this out. I have been trying to research how to solve this issue for a few days now but no luck with a solution. Any assistance or direction will be greatly appreciated. TIA.
If you are not dependent on some extra features you can disable the default libudev feature. Adapt the Cargo.toml this way:
serialport = {version = "4.0.1", default-features = false}
See also the description here: https://crates.io/crates/serialport
Otherwise you have to install a sysroot or wrapper. But I was not able to figure out how to do this (the solution above works fine for me). The error message I got is a little bit more meaningful:
pkg-config has not been configured to support cross-compilation.
Install a sysroot for the target platform and configure it via
PKG_CONFIG_SYSROOT_DIR and PKG_CONFIG_PATH, or install a
cross-compiling wrapper for pkg-config and set it via PKG_CONFIG
environment variable.

How can I prevent Cargo from automatically trying to download a newer version of the compiler?

I need to compile an older version of Parity which only compiles with version 1.28 of the Rust compiler. To install the older version, I did this:
rustup.sh -y --default-toolchain 1.28.0
This seems to work:
root#2afa3b8dc256:/build# cargo --version
cargo 1.28.0 (96a2c7d16 2018-07-13)
root#2afa3b8dc256:/build# rustc --version
rustc 1.28.0 (9634041f0 2018-07-30)
When I try to compile the project, it immediately tries to download a new version of the compiler:
root#2afa3b8dc256:/parity# cargo build --all
info: syncing channel updates for 'stable-x86_64-unknown-linux-gnu'
320.1 KiB / 320.1 KiB (100 %) 271.0 KiB/s ETA: 0 s
info: latest update on 2018-11-08, rust version 1.30.1 (1433507eb 2018-11-07)
info: downloading component 'rustc'
How can I prevent Cargo from doing that?
You can specify the used toolchain version for a specific directory by using rustup override. For example:
rustup override set 1.28.0

Is there a command to update Cargo to the latest official release?

I seem to have diverging versions of rustc and cargo (I think),
$ rustc -V
rustc 1.9.0 (e4e8b6668 2016-05-18)
$ cargo -V
cargo 0.10.0-nightly (10ddd7d 2016-04-08)
Is there a command akin to
pip install --upgrade pip
for upgrading cargo? I.e. something like
cargo install --upgrade cargo
You should update rustc and cargo based on how you installed it. If you used rustup, a rustup update should suffice. If you used a package manager or a binary installer, check those sources for an update.
rustc and cargo are shipped together, but that doesn't mean that their versions need to match. In fact, they do not match until Rust 1.26.0, when the Cargo binary was changed to print the Rust version.
I have the same versions of rustc and cargo that you do; those are the ones that correspond to the Rust 1.9 release. There's nothing to worry about.
If you really want to, you can download a nightly version of Cargo or compile your own. As long as your version exists in your PATH before the older one, it will be used.
I used to do this with my local Rust builds in order to have a version of Cargo at all, although rustup now automatically uses the cargo from the most recent stable version when there isn't one available in the current toolchain, which is nice.
tl;dr command rustup update will update both Rust and Cargo:
$ rustc --version
rustc 1.27.2 (58cc626de 2018-07-18)
$ cargo --version
cargo 1.27.0 (1e95190e5 2018-05-27)
$ rustup update stable
info: syncing channel updates for 'stable-x86_64-unknown-linux-gnu'
info: latest update on 2018-08-02, rust version 1.28.0 (9634041f0 2018-07-30)
info: downloading component 'rustc'
info: downloading component 'rust-std'
info: downloading component 'cargo'
info: downloading component 'rust-docs'
info: removing component 'rustc'
info: removing component 'rust-std'
info: removing component 'cargo'
info: removing component 'rust-docs'
info: installing component 'rustc'
info: installing component 'rust-std'
info: installing component 'cargo'
info: installing component 'rust-docs'
$ rustc --version
rustc 1.28.0 (9634041f0 2018-07-30)
$ cargo --version
cargo 1.28.0 (96a2c7d16 2018-07-13)
You also need to change the default:
> rustc --version
rustc 1.41.0 (5e1a79984 2020-01-27)
> rustup update stable
> rustc --version
rustc 1.41.0 (5e1a79984 2020-01-27)
> rustup default stable-x86_64-apple-darwin
> rustc --version
rustc 1.47.0 (18bf6b4f0 2020-10-07)
Use cargo to update itself:
cargo install cargo --force
This recompiles the package and installs the latest version.
I decided to post this after seeing that rustup didn't update cargo to 1.57
You can edit the version of cargo and rust you're using by using the rustup cli. You can give it a specific version or specify a channel like nightly or beta.
For example:
rustup override set nightly

Resources