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

Revert "refactor target flags" #992

Merged
merged 1 commit into from
Mar 5, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
206 changes: 119 additions & 87 deletions src/lib.rs
Expand Up @@ -1892,39 +1892,93 @@ impl Build {
}

// Target flags
if target.contains("-apple-") {
self.apple_flags(cmd, target)?;
} else {
self.target_flags(cmd, target);
}

if self.static_flag.unwrap_or(false) {
cmd.args.push("-static".into());
}
if self.shared_flag.unwrap_or(false) {
cmd.args.push("-shared".into());
}

if self.cpp {
match (self.cpp_set_stdlib.as_ref(), cmd.family) {
(None, _) => {}
(Some(stdlib), ToolFamily::Gnu) | (Some(stdlib), ToolFamily::Clang) => {
cmd.push_cc_arg(format!("-stdlib=lib{}", stdlib).into());
}
_ => {
self.cargo_output.print_warning(&format_args!("cpp_set_stdlib is specified, but the {:?} compiler does not support this option, ignored", cmd.family));
}
}
}

Ok(())
}

fn target_flags(&self, cmd: &mut Tool, target: &str) {
match cmd.family {
ToolFamily::Clang => {
if !(target.contains("android") && cmd.has_internal_target_arg) {
if target.starts_with("riscv64gc-") {
if target.contains("darwin") {
if let Some(arch) =
map_darwin_target_from_rust_to_compiler_architecture(target)
{
cmd.args
.push(format!("--target={}-apple-darwin", arch).into());
}
} else if target.contains("macabi") {
if let Some(arch) =
map_darwin_target_from_rust_to_compiler_architecture(target)
{
cmd.args
.push(format!("--target={}-apple-ios-macabi", arch).into());
}
} else if target.contains("ios-sim") {
if let Some(arch) =
map_darwin_target_from_rust_to_compiler_architecture(target)
{
let sdk_details =
apple_os_sdk_parts(AppleOs::Ios, &AppleArchSpec::Simulator(""));
let deployment_target =
self.apple_deployment_version(AppleOs::Ios, None, &sdk_details.sdk);
cmd.args.push(
format!(
"--target={}-apple-ios{}-simulator",
arch, deployment_target
)
.into(),
);
}
} else if target.contains("watchos-sim") {
if let Some(arch) =
map_darwin_target_from_rust_to_compiler_architecture(target)
{
let sdk_details =
apple_os_sdk_parts(AppleOs::WatchOs, &AppleArchSpec::Simulator(""));
let deployment_target = self.apple_deployment_version(
AppleOs::WatchOs,
None,
&sdk_details.sdk,
);
cmd.args.push(
format!(
"--target={}-apple-watchos{}-simulator",
arch, deployment_target
)
.into(),
);
}
} else if target.contains("tvos-sim") || target.contains("x86_64-apple-tvos") {
if let Some(arch) =
map_darwin_target_from_rust_to_compiler_architecture(target)
{
let sdk_details =
apple_os_sdk_parts(AppleOs::TvOs, &AppleArchSpec::Simulator(""));
let deployment_target = self.apple_deployment_version(
AppleOs::TvOs,
None,
&sdk_details.sdk,
);
cmd.args.push(
format!(
"--target={}-apple-tvos{}-simulator",
arch, deployment_target
)
.into(),
);
}
} else if target.contains("aarch64-apple-tvos") {
if let Some(arch) =
map_darwin_target_from_rust_to_compiler_architecture(target)
{
let sdk_details =
apple_os_sdk_parts(AppleOs::TvOs, &AppleArchSpec::Device(""));
let deployment_target = self.apple_deployment_version(
AppleOs::TvOs,
None,
&sdk_details.sdk,
);
cmd.args.push(
format!("--target={}-apple-tvos{}", arch, deployment_target).into(),
);
}
} else if target.starts_with("riscv64gc-") {
cmd.args.push(
format!("--target={}", target.replace("riscv64gc", "riscv64")).into(),
);
Expand Down Expand Up @@ -2007,6 +2061,14 @@ impl Build {
}
}
ToolFamily::Gnu => {
if target.contains("darwin") {
if let Some(arch) = map_darwin_target_from_rust_to_compiler_architecture(target)
{
cmd.args.push("-arch".into());
cmd.args.push(arch.into());
}
}

if target.contains("-kmc-solid_") {
cmd.args.push("-finput-charset=utf-8".into());
}
Expand Down Expand Up @@ -2194,6 +2256,31 @@ impl Build {
}
}
}

if target.contains("-apple-") {
self.apple_flags(cmd)?;
}

if self.static_flag.unwrap_or(false) {
cmd.args.push("-static".into());
}
if self.shared_flag.unwrap_or(false) {
cmd.args.push("-shared".into());
}

if self.cpp {
match (self.cpp_set_stdlib.as_ref(), cmd.family) {
(None, _) => {}
(Some(stdlib), ToolFamily::Gnu) | (Some(stdlib), ToolFamily::Clang) => {
cmd.push_cc_arg(format!("-stdlib=lib{}", stdlib).into());
}
_ => {
self.cargo_output.print_warning(&format_args!("cpp_set_stdlib is specified, but the {:?} compiler does not support this option, ignored", cmd.family));
}
}
}

Ok(())
}

fn has_flags(&self) -> bool {
Expand Down Expand Up @@ -2384,7 +2471,8 @@ impl Build {
Ok(())
}

fn apple_flags(&self, cmd: &mut Tool, target: &str) -> Result<(), Error> {
fn apple_flags(&self, cmd: &mut Tool) -> Result<(), Error> {
let target = self.get_target()?;
let os = if target.contains("-darwin") {
AppleOs::MacOs
} else if target.contains("-watchos") {
Expand Down Expand Up @@ -2515,62 +2603,6 @@ impl Build {
cmd.args.push(sdk_path);
}

match cmd.family {
ToolFamily::Gnu => {
if target.contains("darwin") {
if let Some(arch) = map_darwin_target_from_rust_to_compiler_architecture(target)
{
cmd.args.push("-arch".into());
cmd.args.push(arch.into());
}
}
}
ToolFamily::Clang => {
if target.contains("darwin") {
if let Some(arch) = map_darwin_target_from_rust_to_compiler_architecture(target)
{
cmd.args
.push(format!("--target={}-apple-darwin", arch).into());
}
} else if target.contains("macabi") {
if let Some(arch) = map_darwin_target_from_rust_to_compiler_architecture(target)
{
cmd.args
.push(format!("--target={}-apple-ios-macabi", arch).into());
}
} else if target.contains("ios-sim") {
if let Some(arch) = map_darwin_target_from_rust_to_compiler_architecture(target)
{
cmd.args.push(
format!("--target={}-apple-ios{}-simulator", arch, min_version).into(),
);
}
} else if target.contains("watchos-sim") {
if let Some(arch) = map_darwin_target_from_rust_to_compiler_architecture(target)
{
cmd.args.push(
format!("--target={}-apple-watchos{}-simulator", arch, min_version)
.into(),
);
}
} else if target.contains("tvos-sim") || target.contains("x86_64-apple-tvos") {
if let Some(arch) = map_darwin_target_from_rust_to_compiler_architecture(target)
{
cmd.args.push(
format!("--target={}-apple-tvos{}-simulator", arch, min_version).into(),
);
}
} else if target.contains("aarch64-apple-tvos") {
if let Some(arch) = map_darwin_target_from_rust_to_compiler_architecture(target)
{
cmd.args
.push(format!("--target={}-apple-tvos{}", arch, min_version).into());
}
}
}
_ => unreachable!("unexpected compiler for apple architectures"),
}

if let AppleArchSpec::Catalyst(_) = arch {
// Mac Catalyst uses the macOS SDK, but to compile against and
// link to iOS-specific frameworks, we should have the support
Expand Down