1 //! Android ABI-compatibility module 2 //! 3 //! The ABI of Android has changed quite a bit over time, and std attempts to be 4 //! both forwards and backwards compatible as much as possible. We want to 5 //! always work with the most recent version of Android, but we also want to 6 //! work with older versions of Android for whenever projects need to. 7 //! 8 //! Our current minimum supported Android version is `android-9`, e.g., Android 9 //! with API level 9. We then in theory want to work on that and all future 10 //! versions of Android! 11 //! 12 //! Some of the detection here is done at runtime via `dlopen` and 13 //! introspection. Other times no detection is performed at all and we just 14 //! provide a fallback implementation as some versions of Android we support 15 //! don't have the function. 16 //! 17 //! You'll find more details below about why each compatibility shim is needed. 18 19 #![cfg(target_os = "android")] 20 21 use dlibc::{c_int, sighandler_t}; 22 23 use super::weak::weak; 24 25 // The `log2` and `log2f` functions apparently appeared in android-18, or at 26 // least you can see they're not present in the android-17 header [1] and they 27 // are present in android-18 [2]. 28 // 29 // [1]: https://chromium.googlesource.com/android_tools/+/20ee6d20/ndk/platforms 30 // /android-17/arch-arm/usr/include/math.h 31 // [2]: https://chromium.googlesource.com/android_tools/+/20ee6d20/ndk/platforms 32 // /android-18/arch-arm/usr/include/math.h 33 // 34 // Note that these shims are likely less precise than directly calling `log2`, 35 // but hopefully that should be enough for now... 36 // 37 // Note that mathematically, for any arbitrary `y`: 38 // 39 // log_2(x) = log_y(x) / log_y(2) 40 // = log_y(x) / (1 / log_2(y)) 41 // = log_y(x) * log_2(y) 42 // 43 // Hence because `ln` (log_e) is available on all Android we just choose `y = e` 44 // and get: 45 // 46 // log_2(x) = ln(x) * log_2(e) 47 48 #[cfg(not(test))] 49 pub fn log2f32(f: f32) -> f32 { 50 f.ln() * crate::std::f32::consts::LOG2_E 51 } 52 53 #[cfg(not(test))] 54 pub fn log2f64(f: f64) -> f64 { 55 f.ln() * crate::std::f64::consts::LOG2_E 56 } 57 58 // Back in the day [1] the `signal` function was just an inline wrapper 59 // around `bsd_signal`, but starting in API level android-20 the `signal` 60 // symbols was introduced [2]. Finally, in android-21 the API `bsd_signal` was 61 // removed [3]. 62 // 63 // Basically this means that if we want to be binary compatible with multiple 64 // Android releases (oldest being 9 and newest being 21) then we need to check 65 // for both symbols and not actually link against either. 66 // 67 // [1]: https://chromium.googlesource.com/android_tools/+/20ee6d20/ndk/platforms 68 // /android-18/arch-arm/usr/include/signal.h 69 // [2]: https://chromium.googlesource.com/android_tools/+/fbd420/ndk_experimental 70 // /platforms/android-20/arch-arm 71 // /usr/include/signal.h 72 // [3]: https://chromium.googlesource.com/android_tools/+/20ee6d/ndk/platforms 73 // /android-21/arch-arm/usr/include/signal.h 74 pub unsafe fn signal(signum: c_int, handler: sighandler_t) -> sighandler_t { 75 weak!(fn signal(c_int, sighandler_t) -> sighandler_t); 76 weak!(fn bsd_signal(c_int, sighandler_t) -> sighandler_t); 77 78 let f = signal.get().or_else(|| bsd_signal.get()); 79 let f = f.expect("neither `signal` nor `bsd_signal` symbols found"); 80 f(signum, handler) 81 } 82