Skip to content

Commit 81146d0

Browse files
committed
Clean up the GCC triple targeting code
Identify which if statements can coincide and which are mutually exclusive and then reorganize them into `if .. else if ..` blocks to make it clearer which code may or may not apply to any given target. Remove duplicated handling of certain triple cases (e.g. armv7a). Move everything that's not to do with the target architecture/instruction set/calling convention/fpu to the top, then deal with the rest (the messiest and most complicated bits of the code).
1 parent 0d9a0f8 commit 81146d0

File tree

1 file changed

+86
-110
lines changed

1 file changed

+86
-110
lines changed

src/lib.rs

+86-110
Original file line numberDiff line numberDiff line change
@@ -1781,6 +1781,19 @@ impl Build {
17811781
}
17821782
}
17831783
ToolFamily::Gnu => {
1784+
if self.static_flag.is_none()
1785+
&& self
1786+
.getenv("CARGO_CFG_TARGET_FEATURE")
1787+
.map(|feature| feature.contains("crt-static"))
1788+
.unwrap_or(false)
1789+
{
1790+
cmd.args.push("-static".into());
1791+
};
1792+
1793+
if target.contains("-kmc-solid_") {
1794+
cmd.args.push("-finput-charset=utf-8".into());
1795+
}
1796+
17841797
if target.contains("i686") || target.contains("i586") {
17851798
cmd.args.push("-m32".into());
17861799
} else if target == "x86_64-unknown-linux-gnux32" {
@@ -1797,130 +1810,32 @@ impl Build {
17971810
}
17981811
}
17991812

1800-
if target.contains("-kmc-solid_") {
1801-
cmd.args.push("-finput-charset=utf-8".into());
1802-
}
1803-
1804-
if self.static_flag.is_none() {
1805-
let features = self
1806-
.getenv("CARGO_CFG_TARGET_FEATURE")
1807-
.unwrap_or(String::new());
1808-
if features.contains("crt-static") {
1809-
cmd.args.push("-static".into());
1810-
}
1811-
}
1812-
1813-
// armv7 targets get to use armv7 instructions
1814-
if (target.starts_with("armv7") || target.starts_with("thumbv7"))
1815-
&& (target.contains("-linux-") || target.contains("-kmc-solid_"))
1816-
{
1817-
cmd.args.push("-march=armv7-a".into());
1818-
1819-
if target.ends_with("eabihf") {
1820-
// lowest common denominator FPU
1821-
cmd.args.push("-mfpu=vfpv3-d16".into());
1822-
}
1823-
}
1824-
1825-
// (x86 Android doesn't say "eabi")
1826-
if target.contains("-androideabi") && target.contains("v7") {
1827-
// -march=armv7-a handled above
1828-
cmd.args.push("-mthumb".into());
1829-
if !target.contains("neon") {
1830-
// On android we can guarantee some extra float instructions
1831-
// (specified in the android spec online)
1832-
// NEON guarantees even more; see below.
1833-
cmd.args.push("-mfpu=vfpv3-d16".into());
1834-
}
1835-
cmd.args.push("-mfloat-abi=softfp".into());
1836-
}
1837-
1838-
if target.contains("neon") {
1839-
cmd.args.push("-mfpu=neon-vfpv4".into());
1840-
}
1841-
18421813
if target.starts_with("armv4t-unknown-linux-") {
18431814
cmd.args.push("-march=armv4t".into());
18441815
cmd.args.push("-marm".into());
18451816
cmd.args.push("-mfloat-abi=soft".into());
1846-
}
1847-
1848-
if target.starts_with("armv5te-unknown-linux-") {
1817+
} else if target.starts_with("armv5te-unknown-linux-") {
18491818
cmd.args.push("-march=armv5te".into());
18501819
cmd.args.push("-marm".into());
18511820
cmd.args.push("-mfloat-abi=soft".into());
18521821
}
1853-
18541822
// For us arm == armv6 by default
1855-
if target.starts_with("arm-unknown-linux-") {
1823+
else if target.starts_with("arm-unknown-linux-") {
18561824
cmd.args.push("-march=armv6".into());
18571825
cmd.args.push("-marm".into());
18581826
if target.ends_with("hf") {
18591827
cmd.args.push("-mfpu=vfp".into());
18601828
} else {
18611829
cmd.args.push("-mfloat-abi=soft".into());
18621830
}
1863-
}
1864-
1865-
// We can guarantee some settings for FRC
1866-
if target.starts_with("arm-frc-") {
1831+
} else if target.starts_with("armv7a") {
18671832
cmd.args.push("-march=armv7-a".into());
1868-
cmd.args.push("-mcpu=cortex-a9".into());
1869-
cmd.args.push("-mfpu=vfpv3".into());
1870-
cmd.args.push("-mfloat-abi=softfp".into());
1871-
cmd.args.push("-marm".into());
1872-
}
1873-
1874-
// Turn codegen down on i586 to avoid some instructions.
1875-
if target.starts_with("i586-unknown-linux-") {
1876-
cmd.args.push("-march=pentium".into());
1877-
}
1878-
1879-
// Set codegen level for i686 correctly
1880-
if target.starts_with("i686-unknown-linux-") {
1881-
cmd.args.push("-march=i686".into());
1882-
}
1883-
1884-
// Looks like `musl-gcc` makes it hard for `-m32` to make its way
1885-
// all the way to the linker, so we need to actually instruct the
1886-
// linker that we're generating 32-bit executables as well. This'll
1887-
// typically only be used for build scripts which transitively use
1888-
// these flags that try to compile executables.
1889-
if target == "i686-unknown-linux-musl" || target == "i586-unknown-linux-musl" {
1890-
cmd.args.push("-Wl,-melf_i386".into());
1891-
}
1892-
1893-
if target.starts_with("thumb") {
1894-
cmd.args.push("-mthumb".into());
1895-
1896-
if target.ends_with("eabihf") {
1897-
cmd.args.push("-mfloat-abi=hard".into())
1898-
}
1899-
}
1900-
if target.starts_with("thumbv6m") {
1901-
cmd.args.push("-march=armv6s-m".into());
1902-
}
1903-
if target.starts_with("thumbv7em") {
1904-
cmd.args.push("-march=armv7e-m".into());
19051833

19061834
if target.ends_with("eabihf") {
1907-
cmd.args.push("-mfpu=fpv4-sp-d16".into())
1908-
}
1909-
}
1910-
if target.starts_with("thumbv7m") {
1911-
cmd.args.push("-march=armv7-m".into());
1912-
}
1913-
if target.starts_with("thumbv8m.base") {
1914-
cmd.args.push("-march=armv8-m.base".into());
1915-
}
1916-
if target.starts_with("thumbv8m.main") {
1917-
cmd.args.push("-march=armv8-m.main".into());
1918-
1919-
if target.ends_with("eabihf") {
1920-
cmd.args.push("-mfpu=fpv5-sp-d16".into())
1835+
// lowest common denominator FPU
1836+
cmd.args.push("-mfpu=vfpv3-d16".into());
19211837
}
1922-
}
1923-
if target.starts_with("armebv7r") | target.starts_with("armv7r") {
1838+
} else if target.starts_with("armebv7r") || target.starts_with("armv7r") {
19241839
if target.starts_with("armeb") {
19251840
cmd.args.push("-mbig-endian".into());
19261841
} else {
@@ -1929,7 +1844,6 @@ impl Build {
19291844

19301845
// ARM mode
19311846
cmd.args.push("-marm".into());
1932-
19331847
// R Profile
19341848
cmd.args.push("-march=armv7-r".into());
19351849

@@ -1945,15 +1859,50 @@ impl Build {
19451859
cmd.args.push("-mfloat-abi=soft".into());
19461860
}
19471861
}
1948-
if target.starts_with("armv7a") {
1862+
// We can guarantee some settings for FRC
1863+
else if target.starts_with("arm-frc-") {
19491864
cmd.args.push("-march=armv7-a".into());
1865+
cmd.args.push("-mcpu=cortex-a9".into());
1866+
cmd.args.push("-mfpu=vfpv3".into());
1867+
cmd.args.push("-mfloat-abi=softfp".into());
1868+
cmd.args.push("-marm".into());
1869+
}
1870+
// Turn codegen down on i586 to avoid some instructions.
1871+
else if target.starts_with("i586-unknown-linux-") {
1872+
cmd.args.push("-march=pentium".into());
1873+
}
1874+
// Set codegen level for i686 correctly
1875+
else if target.starts_with("i686-unknown-linux-") {
1876+
cmd.args.push("-march=i686".into());
1877+
}
1878+
// Shared branch for all thumb targets
1879+
else if target.starts_with("thumb") {
1880+
cmd.args.push("-mthumb".into());
19501881

19511882
if target.ends_with("eabihf") {
1952-
// lowest common denominator FPU
1953-
cmd.args.push("-mfpu=vfpv3-d16".into());
1883+
cmd.args.push("-mfloat-abi=hard".into())
19541884
}
1955-
}
1956-
if target.starts_with("riscv32") || target.starts_with("riscv64") {
1885+
1886+
if target.starts_with("thumbv6m") {
1887+
cmd.args.push("-march=armv6s-m".into());
1888+
} else if target.starts_with("thumbv7em") {
1889+
cmd.args.push("-march=armv7e-m".into());
1890+
1891+
if target.ends_with("eabihf") {
1892+
cmd.args.push("-mfpu=fpv4-sp-d16".into())
1893+
}
1894+
} else if target.starts_with("thumbv7m") {
1895+
cmd.args.push("-march=armv7-m".into());
1896+
} else if target.starts_with("thumbv8m.base") {
1897+
cmd.args.push("-march=armv8-m.base".into());
1898+
} else if target.starts_with("thumbv8m.main") {
1899+
cmd.args.push("-march=armv8-m.main".into());
1900+
1901+
if target.ends_with("eabihf") {
1902+
cmd.args.push("-mfpu=fpv5-sp-d16".into())
1903+
}
1904+
}
1905+
} else if target.starts_with("riscv32") || target.starts_with("riscv64") {
19571906
// get the 32i/32imac/32imc/64gc/64imac/... part
19581907
let mut parts = target.split('-');
19591908
if let Some(arch) = parts.next() {
@@ -1980,6 +1929,32 @@ impl Build {
19801929
cmd.args.push("-mcmodel=medany".into());
19811930
}
19821931
}
1932+
1933+
// (x86 Android doesn't say "eabi")
1934+
if target.contains("-androideabi") && target.contains("v7") {
1935+
// -march=armv7-a handled above
1936+
cmd.args.push("-mthumb".into());
1937+
if !target.contains("neon") {
1938+
// On android we can guarantee some extra float instructions
1939+
// (specified in the android spec online)
1940+
// NEON guarantees even more; see below.
1941+
cmd.args.push("-mfpu=vfpv3-d16".into());
1942+
}
1943+
cmd.args.push("-mfloat-abi=softfp".into());
1944+
}
1945+
1946+
// Looks like `musl-gcc` makes it hard for `-m32` to make its way
1947+
// all the way to the linker, so we need to actually instruct the
1948+
// linker that we're generating 32-bit executables as well. This'll
1949+
// typically only be used for build scripts which transitively use
1950+
// these flags that try to compile executables.
1951+
if target == "i686-unknown-linux-musl" || target == "i586-unknown-linux-musl" {
1952+
cmd.args.push("-Wl,-melf_i386".into());
1953+
}
1954+
1955+
if target.contains("neon") {
1956+
cmd.args.push("-mfpu=neon-vfpv4".into());
1957+
}
19831958
}
19841959
}
19851960

@@ -1990,6 +1965,7 @@ impl Build {
19901965
if self.static_flag.unwrap_or(false) {
19911966
cmd.args.push("-static".into());
19921967
}
1968+
19931969
if self.shared_flag.unwrap_or(false) {
19941970
cmd.args.push("-shared".into());
19951971
}

0 commit comments

Comments
 (0)