Skip to content

Commit

Permalink
Clean up and clarify Android targeting code
Browse files Browse the repository at this point in the history
Use the online NDK compiler docs at [0] as a reference to document some of the
existing behavior and supplement with a linker argument.

Don't rely on vague things like "x86 doesn't contain eabi" and just test for the
architectures we are targeting in the Android-specific code.

[0]: https://developer.android.com/ndk/guides/standalone_toolchain
  • Loading branch information
mqudsi committed Feb 9, 2023
1 parent 81146d0 commit a21b93c
Showing 1 changed file with 23 additions and 10 deletions.
33 changes: 23 additions & 10 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1930,17 +1930,34 @@ impl Build {
}
}

// (x86 Android doesn't say "eabi")
if target.contains("-androideabi") && target.contains("v7") {
// -march=armv7-a handled above
// -march=armv7-a (handled above) and -mthumb (handled above) can be
// present separately or together. In the absence of an explicit --target,
// only -march=armv7-a targets armv7-none-linux-androideabi, only -mthumb
// targets thumb-non-linux-androideabi, while supplying both targets
// thumbv7-non-linux-androideabi. It is recommended to supply -mthumb to
// force the use of 16-bit Thumb-2 instructions instead of 32-bit ARM
// instructions.
// Source: https://developer.android.com/ndk/guides/standalone_toolchain
if (target.starts_with("thumbv7") || target.starts_with("armv7"))
&& target.contains("-androideabi")
{
cmd.args.push("-mthumb".into());
if !target.contains("neon") {
// On android we can guarantee some extra float instructions
// (specified in the android spec online)
// NEON guarantees even more; see below.
// On android we can guarantee some extra float instructions (per
// the online spec). NEON guarantees even more; see below.
cmd.args.push("-mfpu=vfpv3-d16".into());
}
cmd.args.push("-mfloat-abi=softfp".into());

// Android NDK link above says to make sure the following flag is
// passed to the linker to work around a CPU bug on some Cortex-A8
// implementations.
cmd.args.push("-Wl,--fix-cortex-a8".into());
}

if target.contains("neon") {
// This automatically forces the use of VFPv3-D32, per ARM specifications
cmd.args.push("-mfpu=neon-vfpv4".into());
}

// Looks like `musl-gcc` makes it hard for `-m32` to make its way
Expand All @@ -1951,10 +1968,6 @@ impl Build {
if target == "i686-unknown-linux-musl" || target == "i586-unknown-linux-musl" {
cmd.args.push("-Wl,-melf_i386".into());
}

if target.contains("neon") {
cmd.args.push("-mfpu=neon-vfpv4".into());
}
}
}

Expand Down

0 comments on commit a21b93c

Please sign in to comment.