Skip to content

Commit f24f140

Browse files
committed
separated section for gcc or clang-only flags
1 parent ffdf356 commit f24f140

File tree

1 file changed

+52
-54
lines changed

1 file changed

+52
-54
lines changed

src/lib.rs

+52-54
Original file line numberDiff line numberDiff line change
@@ -1722,77 +1722,75 @@ impl Build {
17221722
) -> Result<(), Error> {
17231723
// Non-target flags
17241724
// If the flag is not conditioned on target variable, it belongs here :)
1725-
match cmd.family {
1726-
ToolFamily::Msvc { .. } => {
1727-
cmd.push_cc_arg("-nologo".into());
1728-
1729-
let crt_flag = match self.static_crt {
1730-
Some(true) => "-MT",
1731-
Some(false) => "-MD",
1732-
None => {
1733-
let features = self.getenv("CARGO_CFG_TARGET_FEATURE");
1734-
let features = features.as_deref().unwrap_or_default();
1735-
if features.contains("crt-static") {
1736-
"-MT"
1737-
} else {
1738-
"-MD"
1739-
}
1725+
if cmd.is_like_msvc() {
1726+
cmd.push_cc_arg("-nologo".into());
1727+
1728+
let crt_flag = match self.static_crt {
1729+
Some(true) => "-MT",
1730+
Some(false) => "-MD",
1731+
None => {
1732+
let features = self.getenv("CARGO_CFG_TARGET_FEATURE");
1733+
let features = features.as_deref().unwrap_or_default();
1734+
if features.contains("crt-static") {
1735+
"-MT"
1736+
} else {
1737+
"-MD"
17401738
}
1741-
};
1742-
cmd.push_cc_arg(crt_flag.into());
1743-
1744-
match &opt_level[..] {
1745-
// Msvc uses /O1 to enable all optimizations that minimize code size.
1746-
"z" | "s" | "1" => cmd.push_opt_unless_duplicate("-O1".into()),
1747-
// -O3 is a valid value for gcc and clang compilers, but not msvc. Cap to /O2.
1748-
"2" | "3" => cmd.push_opt_unless_duplicate("-O2".into()),
1749-
_ => {}
17501739
}
1740+
};
1741+
cmd.push_cc_arg(crt_flag.into());
1742+
1743+
match &opt_level[..] {
1744+
// Msvc uses /O1 to enable all optimizations that minimize code size.
1745+
"z" | "s" | "1" => cmd.push_opt_unless_duplicate("-O1".into()),
1746+
// -O3 is a valid value for gcc and clang compilers, but not msvc. Cap to /O2.
1747+
"2" | "3" => cmd.push_opt_unless_duplicate("-O2".into()),
1748+
_ => {}
17511749
}
1752-
ToolFamily::Gnu | ToolFamily::Clang => {
1753-
// arm-linux-androideabi-gcc 4.8 shipped with Android NDK does
1754-
// not support '-Oz'
1755-
if opt_level == "z" && cmd.family != ToolFamily::Clang {
1756-
cmd.push_opt_unless_duplicate("-Os".into());
1757-
} else {
1758-
cmd.push_opt_unless_duplicate(format!("-O{}", opt_level).into());
1759-
}
1760-
1761-
if cmd.family == ToolFamily::Clang && target.contains("windows") {
1750+
} else {
1751+
// arm-linux-androideabi-gcc 4.8 shipped with Android NDK does
1752+
// not support '-Oz'
1753+
if opt_level == "z" && cmd.family != ToolFamily::Clang {
1754+
cmd.push_opt_unless_duplicate("-Os".into());
1755+
} else {
1756+
cmd.push_opt_unless_duplicate(format!("-O{}", opt_level).into());
1757+
}
1758+
if cmd.family == ToolFamily::Clang {
1759+
if target.contains("windows") {
17621760
// Disambiguate mingw and msvc on Windows. Problem is that
17631761
// depending on the origin clang can default to a mismatchig
17641762
// run-time.
17651763
cmd.push_cc_arg(format!("--target={}", target).into());
17661764
}
17671765

1768-
if cmd.family == ToolFamily::Clang && target.contains("android") {
1766+
if target.contains("android") {
17691767
// For compatibility with code that doesn't use pre-defined `__ANDROID__` macro.
17701768
// If compiler used via ndk-build or cmake (officially supported build methods)
17711769
// this macros is defined.
17721770
// See https://android.googlesource.com/platform/ndk/+/refs/heads/ndk-release-r21/build/cmake/android.toolchain.cmake#456
17731771
// https://android.googlesource.com/platform/ndk/+/refs/heads/ndk-release-r21/build/core/build-binary.mk#141
17741772
cmd.push_opt_unless_duplicate("-DANDROID".into());
17751773
}
1774+
}
17761775

1777-
if !target.contains("apple-ios")
1778-
&& !target.contains("apple-watchos")
1779-
&& !target.contains("apple-tvos")
1780-
{
1781-
cmd.push_cc_arg("-ffunction-sections".into());
1782-
cmd.push_cc_arg("-fdata-sections".into());
1783-
}
1784-
// Disable generation of PIC on bare-metal for now: rust-lld doesn't support this yet
1785-
if self.pic.unwrap_or(
1786-
!target.contains("windows")
1787-
&& !target.contains("-none-")
1788-
&& !target.contains("uefi"),
1789-
) {
1790-
cmd.push_cc_arg("-fPIC".into());
1791-
// PLT only applies if code is compiled with PIC support,
1792-
// and only for ELF targets.
1793-
if target.contains("linux") && !self.use_plt.unwrap_or(true) {
1794-
cmd.push_cc_arg("-fno-plt".into());
1795-
}
1776+
if !target.contains("apple-ios")
1777+
&& !target.contains("apple-watchos")
1778+
&& !target.contains("apple-tvos")
1779+
{
1780+
cmd.push_cc_arg("-ffunction-sections".into());
1781+
cmd.push_cc_arg("-fdata-sections".into());
1782+
}
1783+
// Disable generation of PIC on bare-metal for now: rust-lld doesn't support this yet
1784+
if self.pic.unwrap_or(
1785+
!target.contains("windows")
1786+
&& !target.contains("-none-")
1787+
&& !target.contains("uefi"),
1788+
) {
1789+
cmd.push_cc_arg("-fPIC".into());
1790+
// PLT only applies if code is compiled with PIC support,
1791+
// and only for ELF targets.
1792+
if target.contains("linux") && !self.use_plt.unwrap_or(true) {
1793+
cmd.push_cc_arg("-fno-plt".into());
17961794
}
17971795
}
17981796
}

0 commit comments

Comments
 (0)