diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 34e9f395a..ce83daf23 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -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) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 25368b0cb..d336d24fe 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -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: @@ -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 diff --git a/.gitignore b/.gitignore index 91b55c654..c059a7105 100644 --- a/.gitignore +++ b/.gitignore @@ -10,3 +10,4 @@ __pycache__/ dist/ build dist +tags diff --git a/src/compile.rs b/src/compile.rs index b884fd54e..b8ceed192 100644 --- a/src/compile.rs +++ b/src/compile.rs @@ -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) } } @@ -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) @@ -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) @@ -107,7 +102,7 @@ fn compile_target( context: &BuildContext, python_interpreter: Option<&PythonInterpreter>, bindings_crate: &BridgeModel, - target: &str, + target: Option<&str>, ) -> Result> { let mut shared_args = vec!["--manifest-path", context.manifest_path.to_str().unwrap()]; @@ -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 diff --git a/src/target.rs b/src/target.rs index f875c4419..544d33a1b 100644 --- a/src/target.rs +++ b/src/target.rs @@ -1,5 +1,5 @@ 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; @@ -7,6 +7,8 @@ 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)] @@ -47,7 +49,6 @@ pub struct Target { os: Os, arch: Arch, env: Option, - triple: &'static str, } impl Target { @@ -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 { @@ -111,7 +113,6 @@ impl Target { os, arch, env: platform.target_env, - triple: platform.target_triple, }) } @@ -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 @@ -316,3 +312,26 @@ impl Target { (tag, tags) } } + +fn get_host_target() -> Result { + 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) +}