"Error loading target specification" when building a WebAssembly module with Cargo - rust

I've constructed a simple Rust app as follows:
cargo new hello_world --bin
I can compile to WebAssembly using the Rust compiler as follows:
rustc +nightly --target wasm32-unknown-unknown -O src/main.rs
I'd like to use Cargo to manage external dependencies, but when I build for the same target using cargo:
cargo build --release --target wasm32-unknown-unknown
It fails as follows:
$ cargo build --release --target wasm32-unknown-unknown
error: failed to run `rustc` to learn about target-specific information
Caused by:
process didn't exit successfully: `rustc - --crate-name ___ --print=file-names --target wasm32-unknown-unknown --crate-type bin --crate-type rlib` (exit code: 101)
--- stderr
error: Error loading target specification: Could not find specification for target "wasm32-unknown-unknown"
|
= help: Use `--print target-list` for a list of built-in targets

As the wasm32-unknown-unknown target has only recently been added to Rust, it's only available in the nightly toolchain. You have to specify that you wish to use the nightly toolchain:
cargo +nightly build --release --target wasm32-unknown-unknown
# ^^^^^^^^
You were already doing this when calling rustc directly:
rustc +nightly --target wasm32-unknown-unknown -O src/main.rs
# ^^^^^^^^
It's probably easier if you:
Use a toolchain override in your project directory:
$ rustup override set nightly
Set a default target in your project's .cargo/config:
[build]
target = "wasm32-unknown-unknown"
Then you can just cargo build --release. (It's recommended to avoid debug mode for now).

Related

How to pass llc arguments through Rust Cargo?

I am compiling code through Rust cargo. I need to enable the llc option --nozero-initialized-in-bss to work around a bug in lld.
I tried to pass the argument through
RUSTFLAGS="-C llvm-args=--nozero-initialized-in-bss" cargo build
but it did not work, generating the following error
error: failed to run `rustc` to learn about target-specific information
Caused by:
process didn't exit successfully: `rustc - --crate-name ___ --print=file-names -C llvm-args=--nozero-initialized-in-bss --target thumbv7em-none-eabi --crate-type bin --crate-type rlib --crate-type dylib --crate-type cdylib --crate-type staticlib --crate-type proc-macro --print=sysroot --print=cfg` (exit status: 1)
--- stderr
rustc -Cllvm-args="..." with: Unknown command line argument '--nozero-initialized-in-bss'. Try: 'rustc -Cllvm-args="..." with --help'
rustc -Cllvm-args="..." with: Did you mean '--lower-interleaved-accesses'?
Is it because rustc only recognizes a subset of all llc options? Or my way of passing llc options is incorrect?

Failed to run `rustc`: unknown codegen option: `-Zshare-generics`

So I am trying to build a rust project using cargo build and receive:
error: failed to run `rustc` to learn about target-specific information
Caused by:
process didn't exit successfully: `rustc - --crate-name ___ --print=file-names -C -Zshare-generics=y -Csplit-debuginfo=unpacked --crate-type bin --crate-type rlib --crate-type dylib --crate-type cdylib --crate-type staticlib --crate-type proc-macro --print=sysroot --print=cfg` (exit status: 1)
--- stderr
error: unknown codegen option: `-Zshare-generics`
I tried multiple projects, non of them worked. So tried creating a new one. Still didn't work. Removed the config file and still nothing.
My cargo version is cargo 1.66.1 (ad779e08b 2023-01-10) and rustc rustc 1.66.1 (90743e729 2023-01-10). Tried uninstalling rust (rustup self uninstall and rm -rf $HOME/.cargo) and reinstalling but still get the error.
$ rustup show
Default host: x86_64-apple-darwin
rustup home: /Users/USER/.rustup
stable-x86_64-apple-darwin (default)
rustc 1.66.1 (90743e729 2023-01-10)
I tried creating a new project after the reinstall. Still nothing so any help is appreciated.

Linking Rust to LLVM API in Cargo.toml

I'm using a library that wraps the LLVM-C API (inkwell) so I need to link my Rust binary to the LLVM library. If I export the following rust flags with:
export RUSTFLAGS=-lLLVM-12 -lm -ldl -lc -lpthread -lutil -lgcc_s -C link-args=-L/usr/lib/llvm/12/lib64
Then compilation runs fine.
If I then insert these lines into my Cargo.toml file for the project however:
[build]
rustflags = ["-lLLVM-12", "-lm","-ldl","-lc","-lpthread","-lutil","-lgcc_s", "-C", "link-args=-L/usr/lib/llvm/12/lib64"]
Then I get linking errors against LLVM-C functions.
Why does this work with an environment variable but not in my cargo config file? Am I msiconfiguring cargo in some way?
According to Cargo documentation, rustflags property is documented via .cargo/config.toml, not Cargo.toml.

How can I specify linker flags/arguments in a build script?

I'm using Rust, bindgen, and a build script to work on some FFI bindings to a library.
This library is built using OpenMP, so when linking against it, I'd normally pass the -fopenmp flag to the compiler.
How can I get this flag to be set by build.rs when the library is built by Cargo?
Currently, building using Cargo fails, with the failing command being something like:
cc -Wl,--as-needed -Wl,-z,noexecstack -m64 -l gomp -l stdc++
...skipping dozens of paths/files...
-Wl,-Bdynamic -l dl -l rt -l pthread -l gcc_s -l c -l m -l rt -l pthread -l util
which fails with hundreds of undefined reference to 'GOMP_parallel_end' errors.
Rerunning the generated command above with the -fopenmp flag manually added succeeds.
I can specify the flag using RUSTFLAGS='-C link-args=-fopenmp' before compiling, but is there a way of specifying it from within build.rs?
This feature has been added to Cargo and was stabilized in Cargo 1.56. The accepted answer is now out-of-date.
Linker arguments can be specified in build.rs like so:
// Pass `-fopenmp` to the linker.
println!("cargo:rustc-link-arg=-fopenmp");
You cannot could not. See the sibling answer from ecstaticm0rse for an updated answer.
Before then, you can use a Cargo configuration file.
.cargo/config
[build]
rustflags = ["-C", "link-args=-fsome-artisanal-option"]
Execution
$ cargo build --verbose
Compiling example v0.1.0 (file:///private/tmp/example)
Running `rustc ...blah blah blah... -C link-args=-fsome-artisanal-option`
error: linking with `cc` failed: exit code: 1
|
= note: "cc" "-m64" ...blah blah blah... "-fsome-artisanal-option"
= note: clang: error: unknown argument: '-fsome-artisanal-option'
See also:
How to get the linker to produce a map file using Cargo
How can I globally configure a Cargo profile option?
Is it possible to specify `panic = "abort"` for a specific target?

Cross-compilation to x86_64-unknown-linux-gnu fails on Mac OSX

I tried to compile one of my Rust projects to the x86_64-unknown-linux-gnu target:
$ cargo build --target=x86_64-unknown-linux-gnu
Compiling deployer v0.1.0 (file:///Users/raphael/web/deployer)
error: linking with `cc` failed: exit code: 1
|
= note: "cc"
= note: clang: warning: argument unused during compilation: '-pie'
ld: unknown option: --as-needed
clang: error: linker command failed with exit code 1 (use -v to see invocation)
I don't know what to do with such a message. What should I do to make it work?
Here is my Cargo.toml file:
[package]
name = "deployer"
version = "0.1.0"
authors = ["..."]
[dependencies]
clap = "2.14.0"
time = "0.1.35"
slack-hook = "0.2"
Cargo version:
cargo 0.13.0-nightly (109cb7c 2016-08-19)
Rust version:
rustc 1.12.0 (3191fbae9 2016-09-23)
I tried to update everything with rustup, but I still get the same problem.
Inspired from cross-compile-rust-from-mac-to-linux, I typically install these dependencies to cross compile rust from Mac OS to Linux (e.g. for Docker containers):
rustup target add x86_64-unknown-linux-gnu
# Install a pre-built cross compiler
brew tap SergioBenitez/osxct
brew install x86_64-unknown-linux-gnu
And finally I can cross compile to Linux on Mac OS with:
CARGO_TARGET_X86_64_UNKNOWN_LINUX_GNU_LINKER=x86_64-unknown-linux-gnu-gcc \
cargo build --target=x86_64-unknown-linux-gnu

Resources