Is there an option to get rustc to show a "successful" message? - rust

When I compile a program using rustc I usually get errors. Once I've eliminated the errors, I get no message which means that compile was successful.
Is there an option to get rustc to show a "successful" message? It would be nice to see positive feedback.

Most Rust programmers don't invoke rustc directly, but instead do it through cargo, which prints a green success message for each crate that is compiled:
$ cargo build
Compiling cfg-if v0.1.10
Compiling lazy_static v1.4.0
Compiling bytes v0.5.6
Compiling mycrate v0.2.0 (/dev/rust/mycrate)
Finished dev [unoptimized + debuginfo] target(s) in 13.17s
You will also get a progress bar tracking the build process:
$ cargo build
Compiling cfg-if v0.1.10
Compiling lazy_static v1.4.0
Building [====================> ] 3/4: bytes
rustc is more bare-bones and does not output any success messages. However, you can use && to print a message manually if the compilation was successful:
$ rustc main.rs && echo "Compiled successfully"
Compiled successfully
If you want to get even more fancy, you can use ASCII escape codes to make the message green!
$ rustc main.rs && echo "\033[0;32mCompiled successfully"
Compiled successfully # <- this is green!

Related

Compile and run rust programs without creating separate cargo projects

While going through Rust by Example,
I found myself creating a new cargo project for each program in the tutorial.
This quickly turned cumbersome.
Another strategy I tried was having my working directory structured like this:
src\
guessing_game.rs
main.rs
temp.rs
where main.rs contains
mod guessing_game;
mod temp;
/// Hello
fn main() {
// guessing_game::play();
println!("{}", temp::is_prime(6));
}
and cargo.toml contains
[package]
name = "rust_prog_dump"
version = "0.1.0"
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
rand = "0.8.4"
I would call the target function in main() and comment out the others.
Do we have an alternative?
I have seen this issue and Jon Cairns' post.
I use Windows, hence that script does not work for me.
Do we have an alternative?
One alternative is to compile by hand using rustc directly, however that is annoying if you want dependencies.
An other alternative is to have multiple binaries in the crate, then you can select the binary to compile (and run) using --bin:
> cargo new multibin
Created binary (application) `multibin` package
> cd multibin
> mkdir src/bin
> echo 'fn main() { println!("test 1"); }' > src/bin/test1.rs
> echo 'fn main() { println!("test 2"); }' > src/bin/test2.rs
> echo 'fn main() { println!("primary"); }' >| src/main.rs
> cargo r
error: `cargo run` could not determine which binary to run. Use the `--bin` option to specify a binary, or the `default-run` manifest key.
available binaries: multibin, test1, test2
> cargo r --bin multibin
Compiling multibin v0.1.0 (/private/tmp/multibin)
Finished dev [unoptimized + debuginfo] target(s) in 0.44s
Running `target/debug/multibin`
primary
> cargo r --bin test1
Compiling multibin v0.1.0 (/private/tmp/multibin)
Finished dev [unoptimized + debuginfo] target(s) in 0.10s
Running `target/debug/test1`
test 1
> cargo r --bin test2
Compiling multibin v0.1.0 (/private/tmp/multibin)
Finished dev [unoptimized + debuginfo] target(s) in 0.10s
Running `target/debug/test2`
test 2
As you can see you can have a src/main.rs which will inherit the crate's name, but usually if you start having multiple binaries you put them all in src/bin.
A third alternative is to use something like cargo-script (by far the most convenient but long unmaintained and IIRC only compatible with edition 2015) or runner or something along those lines (there are probably others).
Masklinn's multiple-binary solution works like a charm (one-click execution) when paired with a good IDE.
With IntelliJ Community Edition + the Rust and TOML plugins
Choose the TOML file when prompted. (Only when importing the project)
Go to Project Settings > Modules. Mark src/bin as a Source folder.
Restart the IDE. You will have Current File as an available run configuration.
With VS Code + the Code Runner extension
Click on the Run|Debug button that appears above fn main(). Note that the regular Run Code shortcut (Ctrl+Alt+N) may not work if you have dependencies.

Is it possible to override the crate-type specified in Cargo.toml from the command line when calling cargo build?

I want to generate a static library from a Rust project that I do not maintain. The project allows building a dynamic library — the Cargo.toml specifies crate-type = ["cdylib"].
Modifying the crate type in the file works, but I want to keep the unmodified original project as git submodule in my project if possible.
Is there is any flag that can be passed to the cargo build command to override this setting?
You can't override it, but you can supplement it. Use cargo rustc and pass --crate-type=staticlib directly to the compiler:
% cargo build
Compiling example v0.1.0 (/private/tmp/example)
Finished dev [unoptimized + debuginfo] target(s) in 0.31s
% find target -name '*.a'
% cargo rustc -- --crate-type=staticlib
Compiling example v0.1.0 (/private/tmp/example)
Finished dev [unoptimized + debuginfo] target(s) in 0.29s
% find target -name '*.a'
target/debug/deps/libexample.a
You can provide the crate-type, but you cannot override the one specified in your Cargo.toml:
$ cargo rustc -- --crate-type=staticlib
Compiling example v0.1.0 (/dev/tmp)
Finished dev [unoptimized + debuginfo] target(s) in 0.34s
There is an tracking issue to add a --crate-type override. In the meantime, a workaround is to use cargo-crate-type:
$ cargo install cargo-crate-type
$ cargo crate-type static
$ cargo build
Note that this command will alter your Cargo.toml
Since Rust 1.64 it is possible to override the crate-type for libraries and examples. Please note the missing -- compared to the other answers. The --crate-type flag in this answer is a cargo rustc flag, while in the other answers cargo rustc was used to pass an additional flag to rustc (as opposed to replacing the flag like cargo rustc does).
$ cargo rustc --crate-type=staticlib

llvm-sys - Didn't find usable system-wide LLVM

I'm trying to learn LLVM to make a programming language using Rust. I am using the llvm-sys crate that wraps around the LLVM API.
I have installed LLVM : LLVM-8.0.0-win64
My dependencies in cargo.toml
[dependencies]
llvm-sys = "80.1.1"
When I run cargo run I get the following error,
Updating crates.io index
Compiling memchr v2.2.1
Compiling lazy_static v1.4.0
Compiling regex-syntax v0.6.12
Compiling semver-parser v0.7.0
Compiling libc v0.2.65
Compiling cc v1.0.47
Compiling thread_local v0.3.6
Compiling semver v0.9.0
Compiling aho-corasick v0.7.6
Compiling regex v1.3.1
Compiling llvm-sys v80.1.1
error: failed to run custom build command for `llvm-sys v80.1.1`
Caused by:
process didn't exit successfully: `C:\Users\Name\Desktop\Carbon\carbon-lang\target\debug\build\llvm-sys-ed5d351b1ae6a41b\build-script-build` (exit code: 101)
--- stdout
cargo:rerun-if-env-changed=LLVM_SYS_80_PREFIX
cargo:rerun-if-env-changed=LLVM_SYS_80_IGNORE_BLACKLIST
cargo:rerun-if-env-changed=LLVM_SYS_80_STRICT_VERSIONING
cargo:rerun-if-env-changed=LLVM_SYS_80_NO_CLEAN_CFLAGS
cargo:rerun-if-env-changed=LLVM_SYS_80_USE_DEBUG_MSVCRT
cargo:rerun-if-env-changed=LLVM_SYS_80_FFI_WORKAROUND
Didn't find usable system-wide LLVM.
--- stderr
thread 'main' panicked at 'Failed to execute "C:\\Program Files\\LLVM\\bin\\llvm-config": Os { code: 2, kind: NotFound, message: "The system cannot find the file specified." }', src\libcore\result.rs:1165:5
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace.
But I have set the LLVM_SYS_80_PREFIX as C:\Program Files\LLVM which is where the bin folder is.
Should the llvm-config file be separately installed? I could not find it anywhere.
FYI: I'm on Windows 10
Thanks for the help!
llvm-config is a developer-side tool, it is not shipped with binaries. You need to build LLVM from sources.
LLVM is not shipped with a component called llvm-config.exe, I am not sure how to bypass though.

Could not able to release a file. getting error: could not compile 'libc'

I am very new to this language and coding field. Beginner to coding field as well.
I tried to build and release file but getting an error Compiling libc v0.2.62
error: Could not compile `libc`
pi#raspberrypi:~/Ganesh_Rust/Real_time/led_blink/src $ cargo run
Finished dev [unoptimized + debuginfo] target(s) in 0.09s
Running `/home/pi/Ganesh_Rust/Real_time/led_blink/target/debug/led_blink`
pi#raspberrypi:~/Ganesh_Rust/Real_time/led_blink/src $ cargo build --release
Compiling libc v0.2.62
error: Could not compile `libc`.
Caused by:
process didn't exit successfully: `rustc --crate-name build_script_build /home/pi/.cargo/registry/src/github.com-1ecc6299db9ec823/libc-0.2.62/build.rs --color always --crate-type bin --emit=dep-info,link -C opt-level=3 --cfg 'feature="default"' --cfg 'feature="std"' -C metadata=b79e3ef31fa8c249 -C extra-filename=-b79e3ef31fa8c249 --out-dir /home/pi/Ganesh_Rust/Real_time/led_blink/target/release/build/libc-b79e3ef31fa8c249 -L dependency=/home/pi/Ganesh_Rust/Real_time/led_blink/target/release/deps --cap-lints allow` (signal: 11, SIGSEGV: invalid memory reference)
code: this program which i wrote in VS code to blink LED on raspberry pi 3
use rust_gpiozero::*;
use std::thread;
use std::time::Duration;
fn main() {
//create a new LEd attached to pin 17 of raspberry pi
let led = LED::new(17);
//blink the led 5 times
for _ in 0.. 5{
led.on();
thread::sleep(Duration::from_secs(10));
led.off();
thread::sleep(Duration::from_secs(10));
}
}
cargo.toml file:
[package]
name = "led_blink"
version = "0.1.0"
authors = ["pi"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
rust_gpiozero = "0.2.0"
I am getting output on Raspberry pi but executable file and binary files are large (5MB). So i thought if i do release maybe i can reduce size so tried to release using cargo build --release command but getting this error.
If you're using rustup-provided binaries, then this is a known issue upstream. There is a workaround in that issue, which is to set the following in Cargo.toml:
[profile.release]
codegen-units = 1
As an alternative, you can use the Debian rustc and cargo packages instead of rustup, which should work just fine. You can either download the packages from https://packages.debian.org/rustc and https://packages.debian.org/cargo, or you can add an appropriate APT line into /etc/sources.list (see https://deb.debian.org/ for an example). Note that Debian does not always have the latest version, but they 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.

Resources