Skip to content

Commit

Permalink
Read host target triple from rustc -vV
Browse files Browse the repository at this point in the history
Co-authored-by: konstin <[email protected]>
  • Loading branch information
messense and konstin committed Apr 5, 2021
1 parent 0ea33a2 commit fe41da6
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 11 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ jobs:

- name: Install musl tools
if: matrix.os == 'ubuntu-latest'
run: sudo apt install -y musl-tools
run: sudo apt-get install -y musl-tools

# Those two will also create target/${{ matrix.target }}/maturin
- name: Publish to pypi (with sdist)
Expand Down
9 changes: 9 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ jobs:
- name: Install x86_64-unknown-linux-musl Rust target
if: matrix.os == 'ubuntu-latest'
run: rustup target add x86_64-unknown-linux-musl
- name: Install musl tools
if: matrix.os == 'ubuntu-latest'
run: sudo apt-get install -y musl-tools
#- name: Cache cargo registry
# uses: actions/cache@v2
# with:
Expand Down Expand Up @@ -72,6 +75,12 @@ jobs:
uses: actions-rs/cargo@v1
with:
command: test
- name: cargo test with musl
if: matrix.os == 'ubuntu-latest'
uses: actions-rs/cargo@v1
with:
command: test
args: --target x86_64-unknown-linux-musl

test-auditwheel:
name: Test Auditwheel
Expand Down
39 changes: 29 additions & 10 deletions src/target.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
use crate::{Manylinux, PythonInterpreter};
use anyhow::{bail, format_err, Result};
use anyhow::{bail, format_err, Context, Result};
use platform_info::*;
use platforms::target::Env;
use platforms::Platform;
use std::env;
use std::fmt;
use std::path::Path;
use std::path::PathBuf;
use std::process::Command;
use std::str;

/// All supported operating system
#[derive(Debug, Clone, Eq, PartialEq)]
Expand Down Expand Up @@ -47,7 +49,6 @@ pub struct Target {
os: Os,
arch: Arch,
env: Option<Env>,
triple: &'static str,
}

impl Target {
Expand All @@ -60,8 +61,9 @@ impl Target {
Platform::find(target_triple)
.ok_or_else(|| format_err!("Unknown target triple {}", target_triple))?
} else {
Platform::guess_current()
.ok_or_else(|| format_err!("Could guess the current platform"))?
let target_triple = get_host_target()?;
Platform::find(&target_triple)
.ok_or_else(|| format_err!("Unknown target triple {}", target_triple))?
};

let os = match platform.target_os {
Expand Down Expand Up @@ -111,7 +113,6 @@ impl Target {
os,
arch,
env: platform.target_env,
triple: platform.target_triple,
})
}

Expand All @@ -132,11 +133,6 @@ impl Target {
self.arch
}

/// Returns target triple string
pub fn target_triple(&self) -> &str {
&self.triple
}

/// Returns true if the current platform is linux or mac os
pub fn is_unix(&self) -> bool {
self.os != Os::Windows
Expand Down Expand Up @@ -316,3 +312,26 @@ impl Target {
(tag, tags)
}
}

fn get_host_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)
}

0 comments on commit fe41da6

Please sign in to comment.