cargo build-bpf command not working for solana - rust

I'm getting this error after I execute the command cargo build-bpf to build the program for Solana on Windows 10.
info: uninstalling toolchain 'bpf'
info: toolchain 'bpf' uninstalled
error: no such command: `+bpf`
Cargo does not handle `+toolchain` directives.
Did you mean to invoke `cargo` through `rustup` instead?`

Related

Cross-compiling rust with cargo tries to link with host libs

I'm trying to cross-compile a crate from a FreeBSD 13.1 x86_64 host to a FreeBSD 13.1 aarch64 target. I have already succeeded in cross-compiling my crate from FreeBSD 12.1 x86_64 to FreeBSD 12.1 aarch64.
To do so, I have the following environment:
cargo version
cargo 1.67.0-nightly (f6e737b1e 2022-12-02)
rustc --version
rustc 1.67.0-nightly (01fbc5ae7 2022-12-07)
export AARCH64_SYSROOT=/path/to/aarch64/root
export PKG_CONFIG_SYSROOT_DIR=$AARCH64_SYSROOT
export RUSTFLAGS="-C link-args=-fuse-ld=lld -C link-args=-L$AARCH64_SYSROOT/usr/lib -C link-args=-L$AARCH64_SYSROOT/lib -C link-args=-L$AARCH64_SYSROOT/usr/local/lib -C link-args=--sysroot=$AARCH64_SYSROOT"
The command I run to build is the following, and I get this error on FreeBSD 13.1:
cargo build -Z build-std --release --target aarch64-unknown-freebsd
ld.lld: error: /usr/local/lib/libssl.so is incompatible with $AARCH64_SYSROOT/usr/lib/Scrt1.o
ld.lld: error: /usr/local/lib/libcrypto.so is incompatible with $AARCH64_SYSROOT/usr/lib/Scrt1.o
ld.lld: error: /usr/local/lib/libcurl.so is incompatible with $AARCH64_SYSROOT/usr/lib/Scrt1.o
cc: error: linker command failed with exit code 1 (use -v to see invocation)
It looks like the linker is trying to link with my host libraries even though I specify the link paths in RUSTFLAGS.
I ran cargo with the --verbose flags on both machines and saw that -L native=/usr/local/lib appears on FreeBSD 13.1 but -L native=$AARCH64_SYSROOT/usr/local/lib is shown on FreeBSD 12.1 so I think this is a pretty big clue but I might be completly wrong.
Why does my host libs path appear on FreeBSD13 but not on FreeBSD12 ? How can I remove this default flags when running my cargo build command ?
I tried invoking rustc directly with the command shown by cargo build --verbose (removing the -L native=/usr/local/lib option) which seems to work. However this is not practical as my crate has many dependencies and building properly without using cargo would be a mess...

Why is "anchor build" and "Cargo build-bpf" showing wrong rustc version?

I'm trying to build the (https://github.com/betterclever/solend-anchor) package using anchor build, however I get the following error
error: package `uint v0.9.3` cannot be built because it requires rustc 1.56.1 or newer, while the currently active rustc version is 1.56.0-dev
I updated rustc and running the command rustup default nightlyshows:
info: using existing install for 'nightly-x86_64-apple-darwin'
info: default toolchain set to 'nightly-x86_64-apple-darwin'
nightly-x86_64-apple-darwin unchanged - rustc 1.61.0-nightly
So it shows me that the installed and active rustc version is 1.61, however anchor build is not finding that for some reason. I also tried running cargo build-bpf but the same thing kept happening. cargo buildseemed to work fine.
I'm wondering what is causing the problem when running anchor build and cargo build-bpf?
anchor build and cargo build-bpf use a different compiler than the normal rustc compiler included in the system, so it's normal that they report a different version. The BPF compiler comes with the Solana tool suite.
If you already have the Solana tools installed on your computer, you can simply run:
solana-install init 1.9.13
And if you don't, you can run:
sh -c "$(curl -sSfL https://release.solana.com/v1.9.13/install)"
That will give you all of the newest tools, including the BPF compiler.
I have Solana latest version, I faced the same error while compiling one of the downloaded program.
Command
solana-install update
Worked for me.

can't install cargo wasm-pack

When i run cargo install wasm-pack on windows 10 64-bit i get this error:
error: failed to run custom build command for `openssl-sys v0.9.65`
Caused by:
process didn't exit successfully: `C:\Users\vilgo\AppData\Local\Temp\cargo-install2J8ZNz\release\build\openssl-sys-932395a164949059\build-script-main` (exit code: 101)
--- stdout
cargo:rustc-cfg=const_fn
cargo:rerun-if-env-changed=X86_64_PC_WINDOWS_MSVC_OPENSSL_NO_VENDOR
X86_64_PC_WINDOWS_MSVC_OPENSSL_NO_VENDOR unset
cargo:rerun-if-env-changed=OPENSSL_NO_VENDOR
OPENSSL_NO_VENDOR unset
openssl-src: Enable the assembly language routines in building OpenSSL.
running "perl" "./Configure" "--prefix=C:\\Users\\vilgo\\AppData\\Local\\Temp\\cargo-install2J8ZNz\\release\\build\\openssl-sys-a51d272dcebf1fc5\\out\\openssl-build\\install" "no-dso" "no-shared" "no-ssl3" "no-unit-test" "no-comp" "no-zlib" "no-zlib-dynamic" "no-md2" "no-rc5" "no-weak-ssl-ciphers" "no-camellia" "no-idea" "no-seed" "no-engine" "VC-WIN64A"
--- stderr
thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: Os { code: 2, kind: NotFound, message: "Det går inte att hitta filen." }', C:\Users\vilgo\.cargo\registry\src\github.com-1ecc6299db9ec823\openssl-src-111.15.0+1.1.1k\src\lib.rs:469:39
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
warning: build failed, waiting for other jobs to finish...
error: failed to compile `wasm-pack v0.10.0`, intermediate artifacts can be found at `C:\Users\vilgo\AppData\Local\Temp\cargo-install2J8ZNz`
Caused by:
build failed
How can i fix it?
I ran it in regular cmd.
The problem
I think your problem happened because all three of these things happened:
You probably started the wasm-pack build from an msys shell.
wasm-pack depends on Rust's OpenSSL bindings, which by default try to build OpenSSL by source.
OpenSSL's build scripts are written in Perl. The msys Perl doesn't create Windows paths with \ as a directory separator, which causes the OpenSSL build to fail.
The solutions
Any one of these three solutions should solve your problem:
Fix Step #3: Compile OpenSSL with the native Windows Perl
Make sure your default Perl installation is a "native" Windows Perl like Strawberry Perl. Make sure your build environment does not default to the msys perl. Then, retry compiling both wasm-pack and OpenSSL from source.
Fix Step #2: Use a precompiled OpenSSL library
You can build wasm-pack from source, but instruct the Rust OpenSSL bindings to look for a precompiled OpenSSL.
If you don't have it already, download and install vcpkg, which we'll use to install OpenSSL:
git clone https://github.com/Microsoft/vcpkg.git
.\vcpkg\bootstrap-vcpkg.bat
Then use vcpkg to install OpenSSL:
vcpkg install openssl:x64-windows-static-md
(If this does not work, try vcpkg install openssl:x64-windows.)
And then try compiling wasm-pack. Set VCPKG_ROOT to tell the Rust OpenSSL build script where to look, and also set OPENSSL_NO_VENDOR=1 to discourage the build script from compiling OpenSSL from source.
set VCPKG_ROOT=c:\path\to\vcpkg\installation
set OPENSSL_NO_VENDOR=1
cargo install wasm-pack
Fix Step #1: Use a pre-compiled wasm-pack binary on Windows.
If you do not want to compile either wasm-pack or OpenSSL, you can use the Windows installer (wasm-pack-init.exe) on the rustwasm downloads page. Alternatively, you could also run your wasm-pack builds in Windows Subsystem for Linux (WSL).
Make sure you have the development packages of Open SSL installed.
For example, libssl-dev on Ubuntu or openssl-devel on Fedora. If OpenSSL is already installed and the crate still had trouble finding it, you can set the OPENSSL_DIR environment variable to specify the path for your Open SSL installation. If you are using windows you can use the Win32/Win64 OpenSSL Installation Project to provide a simple installation of OpenSSL on windows.

Rust discovery, config file not working as expected

I am trying to avoid entering the same commands in each GDB sessions. For this, I have followed the instructions in rust discovery book but the program is not working as mentioned in the book when I run the program through cargo run it is giving the following error:
ts/project/discovery/src/06-hello-world$ cargo run
error: could not load Cargo configuration
cargo run --target thumbv7em-none-eabihf
Finished dev [unoptimized + debuginfo] target(s) in 0.04s
Running `arm-none-eabi-gdb -q -x openocd.gdb /home/jawwad-turabi/Documents/project/discovery/target/thumbv7em-none-eabihf/debug/led-roulette`
error: could not execute process `arm-none-eabi-gdb -q -x openocd.gdb /home/jawwad-turabi/Documents/project/discovery/target/thumbv7em-none-eabihf/debug/led-roulette` (never executed)
Caused by:
No such file or directory (os error 2)
My openocd.gdb file contains these content:
target remote: 3333
load
break main
continue
My config file contain these content:
[target.thumbv7em-none-eabihf]
runner = "arm-none-eabi-gdb -q -x openocd.gdb"
rustflags = [
"-C", "link-arg=-Tlink.x",
]
+[build]
+target = "thumbv7em-none-eabihf"
Please change runner = "arm-none-eabi-gdb -q -x openocd.gdb" to this
runner = "gdb-multiarch -q -x openocd.gdb".
Because, if you are using the Ubuntu 18.04 LTS version then this command will be used as the book mention.
Ubuntu 18.04 or newer / Debian stretch or newer
NOTE gdb-multiarch is the GDB command you'll use to debug your ARM
Cortex-M programs
Ubuntu 14.04 and 16.04
NOTE arm-none-eabi-gdb is the GDB command you'll use to debug your ARM
Cortex-M programs
While flashing the STM32F3, we have to connect to the respective GDB server. It may be arm-none-eabi-gdb, gdb-multiarch or gdb. You may have to try all the three.
Now, as far as your question is concerned, you have to use the same parameter in your openocd.gdb. In my case, I have successfully tried with arm-none-eabi-gdb. Remember, I am using rust on Windows 10.

How to cross-compile Rust code for the Raspberry Pi Zero W

I am attempting to cross-compile Rust code on my 64-bit x86 laptop to run on the Raspberry Pi Zero W. I installed the arm-unknown-linux-gnueabihf toolchain using Rustup, but when I run cargo build --target arm-unknown-linux-gnueabihf, I get this error:
Internal error occurred: Failed to find tool. Is arm-linux-gnueabihf-gcc installed?
I tried to install the arm-linux-gnueabihf-gcc package from AUR, but that kept failing because of some sort of GPG key error, so I just decided to make my own cross-compiler using crosstool-ng.
I tried to point Cargo at my newly built cross-compiler using a ~/.cargo/config file:
[target.arm-unknown-linux-gnueabihf]
linker = "/home/ibi/x-tools/arm-unknown-linux-gnueabihf/bin/arm-unknown-linux-gnueabihf-gcc"
But Cargo seems to be ignoring this and giving me the same error. How do I fix this?
It worked when I set an environment variable:
$ export CC="/home/ibi/x-tools/arm-unknown-linux-gnueabihf/bin/arm-unknown-linux-gnueabihf-gcc"

Resources