Skip to content

Commit 915420c

Browse files
authored
Revert "refactor target flags (#873)" (#992)
1 parent b1c53ad commit 915420c

File tree

1 file changed

+119
-87
lines changed

1 file changed

+119
-87
lines changed

src/lib.rs

Lines changed: 119 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -1892,39 +1892,93 @@ impl Build {
18921892
}
18931893

18941894
// Target flags
1895-
if target.contains("-apple-") {
1896-
self.apple_flags(cmd, target)?;
1897-
} else {
1898-
self.target_flags(cmd, target);
1899-
}
1900-
1901-
if self.static_flag.unwrap_or(false) {
1902-
cmd.args.push("-static".into());
1903-
}
1904-
if self.shared_flag.unwrap_or(false) {
1905-
cmd.args.push("-shared".into());
1906-
}
1907-
1908-
if self.cpp {
1909-
match (self.cpp_set_stdlib.as_ref(), cmd.family) {
1910-
(None, _) => {}
1911-
(Some(stdlib), ToolFamily::Gnu) | (Some(stdlib), ToolFamily::Clang) => {
1912-
cmd.push_cc_arg(format!("-stdlib=lib{}", stdlib).into());
1913-
}
1914-
_ => {
1915-
self.cargo_output.print_warning(&format_args!("cpp_set_stdlib is specified, but the {:?} compiler does not support this option, ignored", cmd.family));
1916-
}
1917-
}
1918-
}
1919-
1920-
Ok(())
1921-
}
1922-
1923-
fn target_flags(&self, cmd: &mut Tool, target: &str) {
19241895
match cmd.family {
19251896
ToolFamily::Clang => {
19261897
if !(target.contains("android") && cmd.has_internal_target_arg) {
1927-
if target.starts_with("riscv64gc-") {
1898+
if target.contains("darwin") {
1899+
if let Some(arch) =
1900+
map_darwin_target_from_rust_to_compiler_architecture(target)
1901+
{
1902+
cmd.args
1903+
.push(format!("--target={}-apple-darwin", arch).into());
1904+
}
1905+
} else if target.contains("macabi") {
1906+
if let Some(arch) =
1907+
map_darwin_target_from_rust_to_compiler_architecture(target)
1908+
{
1909+
cmd.args
1910+
.push(format!("--target={}-apple-ios-macabi", arch).into());
1911+
}
1912+
} else if target.contains("ios-sim") {
1913+
if let Some(arch) =
1914+
map_darwin_target_from_rust_to_compiler_architecture(target)
1915+
{
1916+
let sdk_details =
1917+
apple_os_sdk_parts(AppleOs::Ios, &AppleArchSpec::Simulator(""));
1918+
let deployment_target =
1919+
self.apple_deployment_version(AppleOs::Ios, None, &sdk_details.sdk);
1920+
cmd.args.push(
1921+
format!(
1922+
"--target={}-apple-ios{}-simulator",
1923+
arch, deployment_target
1924+
)
1925+
.into(),
1926+
);
1927+
}
1928+
} else if target.contains("watchos-sim") {
1929+
if let Some(arch) =
1930+
map_darwin_target_from_rust_to_compiler_architecture(target)
1931+
{
1932+
let sdk_details =
1933+
apple_os_sdk_parts(AppleOs::WatchOs, &AppleArchSpec::Simulator(""));
1934+
let deployment_target = self.apple_deployment_version(
1935+
AppleOs::WatchOs,
1936+
None,
1937+
&sdk_details.sdk,
1938+
);
1939+
cmd.args.push(
1940+
format!(
1941+
"--target={}-apple-watchos{}-simulator",
1942+
arch, deployment_target
1943+
)
1944+
.into(),
1945+
);
1946+
}
1947+
} else if target.contains("tvos-sim") || target.contains("x86_64-apple-tvos") {
1948+
if let Some(arch) =
1949+
map_darwin_target_from_rust_to_compiler_architecture(target)
1950+
{
1951+
let sdk_details =
1952+
apple_os_sdk_parts(AppleOs::TvOs, &AppleArchSpec::Simulator(""));
1953+
let deployment_target = self.apple_deployment_version(
1954+
AppleOs::TvOs,
1955+
None,
1956+
&sdk_details.sdk,
1957+
);
1958+
cmd.args.push(
1959+
format!(
1960+
"--target={}-apple-tvos{}-simulator",
1961+
arch, deployment_target
1962+
)
1963+
.into(),
1964+
);
1965+
}
1966+
} else if target.contains("aarch64-apple-tvos") {
1967+
if let Some(arch) =
1968+
map_darwin_target_from_rust_to_compiler_architecture(target)
1969+
{
1970+
let sdk_details =
1971+
apple_os_sdk_parts(AppleOs::TvOs, &AppleArchSpec::Device(""));
1972+
let deployment_target = self.apple_deployment_version(
1973+
AppleOs::TvOs,
1974+
None,
1975+
&sdk_details.sdk,
1976+
);
1977+
cmd.args.push(
1978+
format!("--target={}-apple-tvos{}", arch, deployment_target).into(),
1979+
);
1980+
}
1981+
} else if target.starts_with("riscv64gc-") {
19281982
cmd.args.push(
19291983
format!("--target={}", target.replace("riscv64gc", "riscv64")).into(),
19301984
);
@@ -2007,6 +2061,14 @@ impl Build {
20072061
}
20082062
}
20092063
ToolFamily::Gnu => {
2064+
if target.contains("darwin") {
2065+
if let Some(arch) = map_darwin_target_from_rust_to_compiler_architecture(target)
2066+
{
2067+
cmd.args.push("-arch".into());
2068+
cmd.args.push(arch.into());
2069+
}
2070+
}
2071+
20102072
if target.contains("-kmc-solid_") {
20112073
cmd.args.push("-finput-charset=utf-8".into());
20122074
}
@@ -2194,6 +2256,31 @@ impl Build {
21942256
}
21952257
}
21962258
}
2259+
2260+
if target.contains("-apple-") {
2261+
self.apple_flags(cmd)?;
2262+
}
2263+
2264+
if self.static_flag.unwrap_or(false) {
2265+
cmd.args.push("-static".into());
2266+
}
2267+
if self.shared_flag.unwrap_or(false) {
2268+
cmd.args.push("-shared".into());
2269+
}
2270+
2271+
if self.cpp {
2272+
match (self.cpp_set_stdlib.as_ref(), cmd.family) {
2273+
(None, _) => {}
2274+
(Some(stdlib), ToolFamily::Gnu) | (Some(stdlib), ToolFamily::Clang) => {
2275+
cmd.push_cc_arg(format!("-stdlib=lib{}", stdlib).into());
2276+
}
2277+
_ => {
2278+
self.cargo_output.print_warning(&format_args!("cpp_set_stdlib is specified, but the {:?} compiler does not support this option, ignored", cmd.family));
2279+
}
2280+
}
2281+
}
2282+
2283+
Ok(())
21972284
}
21982285

21992286
fn has_flags(&self) -> bool {
@@ -2384,7 +2471,8 @@ impl Build {
23842471
Ok(())
23852472
}
23862473

2387-
fn apple_flags(&self, cmd: &mut Tool, target: &str) -> Result<(), Error> {
2474+
fn apple_flags(&self, cmd: &mut Tool) -> Result<(), Error> {
2475+
let target = self.get_target()?;
23882476
let os = if target.contains("-darwin") {
23892477
AppleOs::MacOs
23902478
} else if target.contains("-watchos") {
@@ -2515,62 +2603,6 @@ impl Build {
25152603
cmd.args.push(sdk_path);
25162604
}
25172605

2518-
match cmd.family {
2519-
ToolFamily::Gnu => {
2520-
if target.contains("darwin") {
2521-
if let Some(arch) = map_darwin_target_from_rust_to_compiler_architecture(target)
2522-
{
2523-
cmd.args.push("-arch".into());
2524-
cmd.args.push(arch.into());
2525-
}
2526-
}
2527-
}
2528-
ToolFamily::Clang => {
2529-
if target.contains("darwin") {
2530-
if let Some(arch) = map_darwin_target_from_rust_to_compiler_architecture(target)
2531-
{
2532-
cmd.args
2533-
.push(format!("--target={}-apple-darwin", arch).into());
2534-
}
2535-
} else if target.contains("macabi") {
2536-
if let Some(arch) = map_darwin_target_from_rust_to_compiler_architecture(target)
2537-
{
2538-
cmd.args
2539-
.push(format!("--target={}-apple-ios-macabi", arch).into());
2540-
}
2541-
} else if target.contains("ios-sim") {
2542-
if let Some(arch) = map_darwin_target_from_rust_to_compiler_architecture(target)
2543-
{
2544-
cmd.args.push(
2545-
format!("--target={}-apple-ios{}-simulator", arch, min_version).into(),
2546-
);
2547-
}
2548-
} else if target.contains("watchos-sim") {
2549-
if let Some(arch) = map_darwin_target_from_rust_to_compiler_architecture(target)
2550-
{
2551-
cmd.args.push(
2552-
format!("--target={}-apple-watchos{}-simulator", arch, min_version)
2553-
.into(),
2554-
);
2555-
}
2556-
} else if target.contains("tvos-sim") || target.contains("x86_64-apple-tvos") {
2557-
if let Some(arch) = map_darwin_target_from_rust_to_compiler_architecture(target)
2558-
{
2559-
cmd.args.push(
2560-
format!("--target={}-apple-tvos{}-simulator", arch, min_version).into(),
2561-
);
2562-
}
2563-
} else if target.contains("aarch64-apple-tvos") {
2564-
if let Some(arch) = map_darwin_target_from_rust_to_compiler_architecture(target)
2565-
{
2566-
cmd.args
2567-
.push(format!("--target={}-apple-tvos{}", arch, min_version).into());
2568-
}
2569-
}
2570-
}
2571-
_ => unreachable!("unexpected compiler for apple architectures"),
2572-
}
2573-
25742606
if let AppleArchSpec::Catalyst(_) = arch {
25752607
// Mac Catalyst uses the macOS SDK, but to compile against and
25762608
// link to iOS-specific frameworks, we should have the support

0 commit comments

Comments
 (0)