Skip to content

Commit f0153a4

Browse files
authored
refactor target flags (#873)
* Refactor apple/non-apple target flags handling * reuse min_version for clang targets
1 parent 561543c commit f0153a4

File tree

1 file changed

+87
-119
lines changed

1 file changed

+87
-119
lines changed

src/lib.rs

Lines changed: 87 additions & 119 deletions
Original file line numberDiff line numberDiff line change
@@ -1890,93 +1890,39 @@ impl Build {
18901890
}
18911891

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

22842197
fn has_flags(&self) -> bool {
@@ -2469,8 +2382,7 @@ impl Build {
24692382
Ok(())
24702383
}
24712384

2472-
fn apple_flags(&self, cmd: &mut Tool) -> Result<(), Error> {
2473-
let target = self.get_target()?;
2385+
fn apple_flags(&self, cmd: &mut Tool, target: &str) -> Result<(), Error> {
24742386
let os = if target.contains("-darwin") {
24752387
AppleOs::MacOs
24762388
} else if target.contains("-watchos") {
@@ -2601,6 +2513,62 @@ impl Build {
26012513
cmd.args.push(sdk_path);
26022514
}
26032515

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

0 commit comments

Comments
 (0)