Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I find the current Rust compiler's default LLVM target triple?

Tags:

rust

llvm

I want to override a build script, which means adding a configuration section that looks like this:

[target.x86_64-unknown-linux-gnu.foo]
rustc-link-search = ["/path/to/foo"]
rustc-link-lib = ["foo"]
root = "/path/to/foo"
key = "value"

But I'm using a Mac, so x86_64-unknown-linux-gnu isn't the correct target triple.

How do I discover which target triple rustc or cargo are currently using?

rustc --print cfg prints a list of values that don't seem to correspond to the triple (there's no unknown in there, in particular).

rustc --print target-list displays all available targets; I just want the default.

like image 621
Roger Lipscombe Avatar asked Oct 25 '18 19:10

Roger Lipscombe


People also ask

What is a target triple Rust?

Tier 3 targets are those which the Rust codebase has support for, but which the Rust project does not build or test automatically, so they may or may not work.

What is Rustup target?

rustup is a toolchain multiplexer. It installs and manages many Rust toolchains and presents them all through a single set of tools installed to ~/. cargo/bin .

Does Rust use LLVM?

Tradeoff #4: LLVM and poor LLVM IR generationrustc uses LLVM to generate code. LLVM can generate very fast code, but it comes at a cost. LLVM is a very big system. In fact, LLVM code makes up the majority of the Rust codebase.


2 Answers

cargo uses rustc -vV to detect the default target triple (source). We can do the same thing:

use std::process::Command;

use anyhow::{format_err, Context, Result};
use std::str;

fn get_target() -> Result<String> {
    let output = Command::new("rustc")
        .arg("-vV")
        .output()
        .context("Failed to run rustc to get the host target")?;
    let output = str::from_utf8(&output.stdout).context("`rustc -vV` didn't return utf8 output")?;

    let field = "host: ";
    let host = output
        .lines()
        .find(|l| l.starts_with(field))
        .map(|l| &l[field.len()..])
        .ok_or_else(|| {
            format_err!(
                "`rustc -vV` didn't have a line for `{}`, got:\n{}",
                field.trim(),
                output
            )
        })?
        .to_string();
    Ok(host)
}

fn main() -> Result<()> {
    let host = get_target()?;
    println!("target triple: {}", host);
    Ok(())
}
like image 110
konstin Avatar answered Sep 21 '22 08:09

konstin


rustc --print cfg will output something like the following:

$ rustc --print cfg
debug_assertions
target_arch="x86_64"
target_endian="little"
target_env="gnu"
target_family="unix"
target_feature="fxsr"
target_feature="sse"
target_feature="sse2"
target_os="linux"
target_pointer_width="64"
target_vendor="unknown"
unix

The target is <arch>-<vendor>-<os>-<env>.

like image 40
Roger Lipscombe Avatar answered Sep 20 '22 08:09

Roger Lipscombe