Skip to content

Commit

Permalink
Merge pull request #487 from messense/target_env
Browse files Browse the repository at this point in the history
Read host target triple from `rustc -vV`
  • Loading branch information
messense authored Apr 8, 2021
2 parents 1239c4f + fe41da6 commit 3685180
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 22 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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ __pycache__/
dist/
build
dist
tags
19 changes: 8 additions & 11 deletions src/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,7 @@ pub fn compile(
if context.target.is_macos() && context.universal2 {
compile_universal2(context, python_interpreter, bindings_crate)
} else {
compile_target(
context,
python_interpreter,
bindings_crate,
context.target.target_triple(),
)
compile_target(context, python_interpreter, bindings_crate, None)
}
}

Expand All @@ -45,7 +40,7 @@ fn compile_universal2(
context,
python_interpreter,
bindings_crate,
"aarch64-apple-darwin",
Some("aarch64-apple-darwin"),
)
.context("Failed to build a aarch64 library through cargo")?
.get(build_type)
Expand All @@ -64,7 +59,7 @@ fn compile_universal2(
context,
python_interpreter,
bindings_crate,
"x86_64-apple-darwin",
Some("x86_64-apple-darwin"),
)
.context("Failed to build a x86_64 library through cargo")?
.get(build_type)
Expand Down Expand Up @@ -107,7 +102,7 @@ fn compile_target(
context: &BuildContext,
python_interpreter: Option<&PythonInterpreter>,
bindings_crate: &BridgeModel,
target: &str,
target: Option<&str>,
) -> Result<HashMap<String, PathBuf>> {
let mut shared_args = vec!["--manifest-path", context.manifest_path.to_str().unwrap()];

Expand All @@ -116,8 +111,10 @@ fn compile_target(
if context.release {
shared_args.push("--release");
}
shared_args.push("--target");
shared_args.push(target);
if let Some(target) = target {
shared_args.push("--target");
shared_args.push(target);
}

let mut rustc_args: Vec<&str> = context
.rustc_extra_args
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 3685180

Please sign in to comment.