Is there way to use xbuild instead of build when calling cargo-binutils commands? - rust

I try to use binutils commands for binary of custom build target with cargo xbuild command.
However, I get an error like below message.
$ cargo nm --target i586-rust_dos.json --bin rust_dos --verbose
"cargo" "build" "--target" "i586-rust_dos.json" "--bin" "rust_dos" "--message-format=json"
Compiling rust_dos v0.1.0 (/home/soya/Documents/src/rust/prac/rust_dos)
error[E0463]: can't find crate for `core` ] 0/2
|
= note: the `i586-rust_dos-8410465322435951119` target may not be installed
error: aborting due to previous error
For more information about this error, try `rustc --explain E0463`.
error: could not compile `rust_dos`.
To learn more, run the command again with --verbose.
error: Failed to parse crate metadata
I realized that cargo nm uses cargo build command,
so I think this error should be solved if I make cargo nm to use cargo xbuild instead of cargo build.
However, I cannot find how to do that.
Please teach me if there is a way.

To detect core crate built with cargo xbuild, I need to add sysroot flag in rustflags in .cargo/config.
example is below.
rustflags = ["--sysroot", "/full/path/to/sysroot/directory"]
There is sysroot directory in project's target directory.
If adding these flags, cargo-bintuils commands are enabled to use as far as in my environment.

Related

error: could not compile `time` when using cargo to compile

Today when I run cargo build with my project in Fedora 32, shows compile error like this:
Compiling devise v0.3.1
thread 'rustc' panicked at 'assertion failed: sentinel == STR_SENTINEL', /rustc/0b42deaccc2cbe17a68067aa5fdb76104369e1fd/compiler/rustc_serialize/src/opaque.rs:669:9
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
error: internal compiler error: unexpected panic
note: the compiler unexpectedly panicked. this is a bug.
note: we would appreciate a bug report: https://github.com/rust-lang/rust/issues/new?labels=C-bug%2C+I-ICE%2C+T-compiler&template=ice.md
note: rustc 1.59.0-nightly (0b42deacc 2021-12-09) running on x86_64-unknown-linux-gnu
note: compiler flags: -C embed-bitcode=no -C debuginfo=2 --crate-type lib
note: some of the compiler flags provided by cargo are hidden
query stack during panic:
end of query stack
Compiling async-stream v0.3.2
error: could not compile `time`
warning: build failed, waiting for other jobs to finish...
error: build failed
my cargo version is:
[dolphin#MiWiFi-R4CM-srv]~/Documents/GitHub/rust_wheel% cargo version
cargo 1.58.0-nightly (40dc28175 2021-12-06)
anybody facing the same problem? any fix suggestion? I searching from Google seem no one facing this same issue.
Looks like this internal compiler error is tracked by this issue:
Try cleaning your build artifacts: cargo clean, in most cases the crash should go away after a rebuild.
should be fixed with nightly 2021-12-11

Error: "linker 'cc' not found" when cross compiling a rust project from windows to linux using cargo

I have a basic rust/cargo project with a single main file and some basic dependencies. The cargo build command works fine when the target is not specified (I am using windows so it builds to windows), but when I try to cross compile the program to linux using cargo build --target=x86_64-unknown-linux-gnu or cargo build --target=x86_64-unknown-linux-musl, the process fails with the following error: linker 'cc' not found.
Does anyone have an idea how to get around this? Is there a specific linker I need to install?
Thanks.
I've just figured it out.
It turns out you need to tell cargo to use the LLVM linker instead. You do this by creating a new directory called .cargo in your base directory, and then a new file called config.toml in this directory. Here you can add the lines:
[target.x86_64-unknown-linux-musl]
rustflags = ["-C", "linker-flavor=ld.lld"]
Then building with the command cargo build --target=x86_64-unknown-linux-musl should work!

How to emit LLVM-IR from Cargo

How can I get cargo to emit LLVM-IR instead of a binary for my project? I know that you can use the --emit=llvm-ir flag in rustc, but I've read some Github issues that show it's impossible to pass arbitrary compiler flags to cargo.
Is there any way I can get cargo to emit LLVM-IR directly?
There is cargo rustc to pass arbitrary compiler flags through Cargo to rustc. So I think:
cargo rustc -- --emit=llvm-ir
is what you want!
This will genarate a ll file in target\debug\deps\.
EDIT: You should use Jacob's answer instead; a lot easier and less hacky.
Build the project with cargo normally but add on the -v flag to show verbose output. The command will have a result like this:
casey#casey-ubuntu:~/Documents/project$ cargo build -v
Fresh aster v0.22.1
Fresh num-traits v0.1.34
Fresh itoa v0.1.1
...
Compiling project v0.1.0 (file:///home/casey/Documents/project)
Running `rustc src/main.rs --crate-name ...`
Finished debug [unoptimized + debuginfo] target(s) in 3.54 secs
If the command produces no output, make a change somewhere in your project code to trick the compiler into rebuilding it, since it will only rebuild if it detects a change in one of the files.
Copy the rustc command from inside the ` markers on the line starting with "Running `rustc..." and append --emit=llvm-ir to it.
This will produce a .ll file in your /target/debug folder.

Rust compiler can't find crate for 'std'

I recently downloaded and unpacked the Rust Language from this site (Linux 64-bit).
I then installed Rust using the given script in the download install.sh:
root#kali:~# /root/rust-1.9.0-x86_64-unknown-linux-gnu/install.sh
install: uninstalling component 'rustc'
install: creating uninstall script at /usr/local/lib/rustlib/uninstall.sh
install: installing component 'rustc'
install: installing component 'rust-std-x86_64-unknown-linux-gnu'
install: installing component 'rust-docs'
install: installing component 'cargo'
Rust is ready to roll.
I am trying to install a crate with cargo, but I keep running into this error:
root#kali:~# cargo install racer
Updating registry `https://github.com/rust-lang/crates.io-index`
Compiling winapi v0.2.7
Compiling bitflags v0.5.0
error: can't find crate for `std` [E0463]
error: aborting due to previous error
Build failed, waiting for other jobs to finish...
error: can't find crate for `std` [E0463]
error: aborting due to previous error
error: failed to compile `racer v1.2.10`, intermediate artifacts can be found at `/root/target-install`
cargo install cargo-edit failed with the same result as above, so it's not limited to one particular package.
Even putting a simple program:
fn main() {
println!("Hello, world!");
}
in a file named hello.rs and running rustc hello.rs does not compile; it gives the same error: error: can't find crate for 'std' [E0463].
The download came with a directory named rust-std-x86_64-unknown-linux-gnu, which I assume is the std crate. How do I instruct rustc to find this directory when trying to locate the std crate?
The following will work for the simplest of compilations.
Assuming you extracted the tar file to, say
$HOME/rust-1.10.0-x86_64-unknown-linux-gnu
Then run
arch=x86_64-unknown-linux-gnu
dl=$HOME/rust-1.10.0-$arch
$dl/rustc/bin/rustc -L $dl/rustc/lib \
-L $dl/rust-std-$arch/lib/rustlib/$arch/lib \
hello.rs
But I'm sure a better way would be to run rustup as Chris Morgan suggest.
Coupla more points
You shouldn't compile code as root.
You may have to relogin or run bash -l to get the environment setup by rustup.
(Fellow rust newb here)
For me (Arch Linux) removing system's Rust fixed the issue.
pacman -Rc rust
I think there was a conflict among user installed Rust and system installed one.

failure to build rust-libc using cargo when rustc is musl-enabled

I successfully created a musl configured rustc by following this link
My attempt to build a project (which builds fine using non-musl configured rust) failed when I used cargo rustc -- --target=x86_64-unknown-linux-musl
'error: could not find crate `libc` with expected target triple x86_64-unknown-linux-musl'
Then, I tried to create rust-libc library using the code from crate. To be more accurate, I used the command provided by cargo to build rust-libc, I've only added --target=x86_64-unknown-linux-musl to the command. This time it failed reporting:
'error: could not find native static library `c`, perhaps an -L flag is missing?`'
I have two questions:
Is it mandatory to build musl configured cargo to be able to use cargo build --target=x86_64-unknown-linux-musl?
How can I address this:
'error: could not find native static library `c`, perhaps an -L flag is missing?'
This worked for me to build libc:
rustc --target=x86_64-unknown-linux-musl /address-of-libc/lib.rs --crate-name libc --crate-type lib -L /address-of-musldist/musldist/lib/ --out-dir=/your-chosen-address/target --cfg feature=\"default\" --cfg feature=\"cargo-build\" --emit=dep-info,link

Resources