Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Supporting the nightly flag -Zmultitarget #19

Merged
merged 24 commits into from
Mar 24, 2022
Merged
Show file tree
Hide file tree
Changes from 21 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
6ba9e80
Modified target to be a vector of multiple possible target hosts
TheOnlyArtz Mar 23, 2022
34c063e
Cargo fmt
TheOnlyArtz Mar 23, 2022
b736f9d
Not using options since it's unnecessary
TheOnlyArtz Mar 23, 2022
3626c56
Add macOS `SDKROOT` support
messense Mar 23, 2022
bcee417
Merge pull request #20 from messense/sdkroot
messense Mar 23, 2022
d56275e
Bump version to 0.6.8
messense Mar 23, 2022
fd71c5d
Binding each linker to it's desired target
TheOnlyArtz Mar 24, 2022
9764da5
Lowercased CC linkers
TheOnlyArtz Mar 24, 2022
355ab77
Target should stay uppercased
TheOnlyArtz Mar 24, 2022
8a3f2f3
LINKER should stay uppercased
TheOnlyArtz Mar 24, 2022
82ee218
Tet x86_64 glibc 2.17 run on CentOS 7
messense Mar 24, 2022
3c9675e
Modified target to be a vector of multiple possible target hosts
TheOnlyArtz Mar 23, 2022
274be51
Cargo fmt
TheOnlyArtz Mar 23, 2022
651d414
Not using options since it's unnecessary
TheOnlyArtz Mar 23, 2022
265886f
Binding each linker to it's desired target
TheOnlyArtz Mar 24, 2022
27fc4ce
Lowercased CC linkers
TheOnlyArtz Mar 24, 2022
b8e24b7
Target should stay uppercased
TheOnlyArtz Mar 24, 2022
d49c1ce
LINKER should stay uppercased
TheOnlyArtz Mar 24, 2022
6166534
Removed target redundancy
TheOnlyArtz Mar 24, 2022
9d3f49e
preserving raw targets when needed
TheOnlyArtz Mar 24, 2022
19ca895
Merge after rebase resolution
TheOnlyArtz Mar 24, 2022
ad29abe
Providing zig linker with raw target
TheOnlyArtz Mar 24, 2022
8809154
Zipping parsed and raw
TheOnlyArtz Mar 24, 2022
e35dfe9
fmt
TheOnlyArtz Mar 24, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 23 additions & 5 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,20 @@ jobs:
- uses: actions-rs/cargo@v1
with:
command: build
- name: macOS - Test build
run: |
rustup target add aarch64-apple-darwin
cargo run zigbuild --target aarch64-apple-darwin
cargo run zigbuild --target aarch64-apple-darwin --release
- name: macOS - Test build with SDKROOT
if: matrix.os == 'ubuntu-latest'
env:
SDK: MacOSX11.3.sdk
run: |
curl -sqL https://github.com/phracker/MacOSX-SDKs/releases/download/11.3/$SDK.tar.xz | tar -Jx
export SDKROOT=$PWD/$SDK

cargo run zigbuild --target aarch64-apple-darwin --manifest-path tests/hello-tls/Cargo.toml
- name: Linux - Test x86_64 build
if: matrix.os == 'ubuntu-latest'
run: |
Expand All @@ -85,6 +99,15 @@ jobs:
if: ${{ matrix.os == 'ubuntu-latest' && matrix.toolchain == 'nightly' }}
run: |
cargo run zigbuild --target x86_64-unknown-linux-gnu.2.17
- name: Linux - Test x86_64 glibc run
if: ${{ matrix.os == 'ubuntu-latest' && matrix.toolchain == 'nightly' }}
uses: addnab/docker-run-action@v3
with:
image: centos:7
options: -v ${{ github.workspace }}:/io -w /io
run: |
./target/x86_64-unknown-linux-gnu/debug/cargo-zigbuild --help
ldd -r -v ./target/x86_64-unknown-linux-gnu/debug/cargo-zigbuild
- name: Linux - Test glibc build
run: |
rustup target add aarch64-unknown-linux-gnu
Expand All @@ -111,11 +134,6 @@ jobs:
rustup target add aarch64-unknown-linux-musl
cargo run zigbuild --target aarch64-unknown-linux-musl
cargo run zigbuild --target aarch64-unknown-linux-musl --manifest-path tests/hello-rustls/Cargo.toml
- name: macOS - Test build
run: |
rustup target add aarch64-apple-darwin
cargo run zigbuild --target aarch64-apple-darwin
cargo run zigbuild --target aarch64-apple-darwin --release
- name: Windows - Test gnu build
run: |
rustup target add x86_64-pc-windows-gnu
Expand Down
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "cargo-zigbuild"
version = "0.6.7"
version = "0.6.8"
edition = "2018"
description = "Compile Cargo project with zig as linker"
license = "MIT"
Expand Down
65 changes: 40 additions & 25 deletions src/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,13 @@ pub struct Build {
pub no_default_features: bool,

/// Build for the target triple
#[clap(long, value_name = "TRIPLE", env = "CARGO_BUILD_TARGET")]
pub target: Option<String>,
#[clap(
long,
value_name = "TRIPLE",
env = "CARGO_BUILD_TARGET",
multiple_occurrences = true
)]
pub target: Vec<String>,

/// Directory for all generated artifacts
#[clap(long, value_name = "DIRECTORY", parse(from_os_str))]
Expand Down Expand Up @@ -190,10 +195,11 @@ impl Build {
let mut build = Command::new("cargo");
build.arg(subcommand);

let rust_target = self
let rust_targets = self
.target
.as_ref()
.map(|target| target.split_once('.').map(|(t, _)| t).unwrap_or(target));
.iter()
.map(|target| target.split_once('.').map(|(t, _)| t).unwrap_or(target))
.collect::<Vec<&str>>();

// collect cargo build arguments
if self.quiet {
Expand Down Expand Up @@ -259,9 +265,11 @@ impl Build {
if self.no_default_features {
build.arg("--no-default-features");
}
if let Some(rust_target) = rust_target {
build.arg("--target").arg(&rust_target);
}

rust_targets.iter().for_each(|target| {
build.arg("--target").arg(&target);
});

if let Some(dir) = self.target_dir.as_ref() {
build.arg("--target-dir").arg(dir);
}
Expand Down Expand Up @@ -310,34 +318,40 @@ impl Build {

if !self.disable_zig_linker {
// setup zig as linker
if let Some(target) = self.target.as_ref() {
let rustc_meta = rustc_version::version_meta()?;
let host_target = &rustc_meta.host;
// we only setup zig as linker when target isn't exactly the same as host target
if host_target != target {
TheOnlyArtz marked this conversation as resolved.
Show resolved Hide resolved
if let Some(rust_target) = rust_target {
let env_target = rust_target.to_uppercase().replace('-', "_");
let (zig_cc, zig_cxx) = prepare_zig_linker(target)?;
let rustc_meta = rustc_version::version_meta()?;
let host_target = &rustc_meta.host;
for (i, parsed_target) in rust_targets.iter().enumerate() {
TheOnlyArtz marked this conversation as resolved.
Show resolved Hide resolved
if let Some(raw_target) = self.target.get(i) {
// we only setup zig as linker when target isn't exactly the same as host target
if host_target != raw_target {
let env_target = parsed_target.replace('-', "_");
let (zig_cc, zig_cxx) = prepare_zig_linker(parsed_target)?;
TheOnlyArtz marked this conversation as resolved.
Show resolved Hide resolved
if is_mingw_shell() {
let zig_cc = zig_cc.to_slash_lossy();
build.env("TARGET_CC", &zig_cc);
build.env("TARGET_CXX", &zig_cxx.to_slash_lossy());
build.env(format!("CARGO_TARGET_{}_LINKER", env_target), &zig_cc);
build.env(format!("CC_{}", env_target), &zig_cc);
build.env(format!("CXX_{}", env_target), &zig_cxx.to_slash_lossy());
build.env(
format!("CARGO_TARGET_{}_LINKER", env_target.to_uppercase()),
&zig_cc,
);
} else {
build.env("TARGET_CC", &zig_cc);
build.env("TARGET_CXX", &zig_cxx);
build.env(format!("CARGO_TARGET_{}_LINKER", env_target), &zig_cc);
build.env(format!("CC_{}", env_target), &zig_cc);
build.env(format!("CXX_{}", env_target), &zig_cxx);
build.env(
format!("CARGO_TARGET_{}_LINKER", env_target.to_uppercase()),
&zig_cc,
);
}

self.setup_os_deps()?;

if rust_target.contains("windows-gnu") {
if raw_target.contains("windows-gnu") {
build.env("WINAPI_NO_BUNDLED_LIBRARIES", "1");
}

// Enable unstable `target-applies-to-host` option automatically for nightly Rust
// when target is the same as host but may have specified glibc version
if host_target == rust_target
if host_target == raw_target
&& matches!(rustc_meta.channel, rustc_version::Channel::Nightly)
{
build.env("CARGO_UNSTABLE_TARGET_APPLIES_TO_HOST", "true");
Expand All @@ -347,11 +361,12 @@ impl Build {
}
}
}

Ok(build)
}

fn setup_os_deps(&self) -> Result<()> {
if let Some(target) = self.target.as_ref() {
for target in &self.target {
if target.contains("apple") {
let target_dir = if let Some(target_dir) = self.target_dir.clone() {
target_dir.join(target)
Expand Down
9 changes: 7 additions & 2 deletions src/rustc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,13 @@ pub struct Rustc {
pub no_default_features: bool,

/// Build for the target triple
#[clap(long, value_name = "TRIPLE", env = "CARGO_BUILD_TARGET")]
pub target: Option<String>,
#[clap(
long,
value_name = "TRIPLE",
env = "CARGO_BUILD_TARGET",
multiple_occurrences = true
)]
pub target: Vec<String>,

/// Output compiler information without compiling
#[clap(long, value_name = "INFO")]
Expand Down
12 changes: 12 additions & 0 deletions src/zig.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ impl Zig {
.map(|x| x.contains("windows-gnu"))
.unwrap_or_default();
let is_arm = target.map(|x| x.contains("arm")).unwrap_or_default();
let is_macos = target.map(|x| x.contains("macos")).unwrap_or_default();

let rustc_ver = rustc_version::version()?;

Expand Down Expand Up @@ -128,6 +129,17 @@ impl Zig {
new_cmd_args.push("-Wl,-undefined=dynamic_lookup".to_string());
}

if is_macos {
if let Ok(sdkroot) = env::var("SDKROOT") {
new_cmd_args.extend_from_slice(&[
format!("--sysroot={}", sdkroot),
"-I/usr/include".to_string(),
"-L/usr/lib".to_string(),
"-F/System/Library/Frameworks".to_string(),
]);
}
}

let mut child = Self::command()?
.arg(cmd)
.args(new_cmd_args)
Expand Down