Fix __VERSIONER_NO_GUARD cases for availability.

libc++ still depends on these being declared even if they are
unavailable. This results in a worse error message (a link error
rather than a compiler diagnostic) if these functions end up being
used, but without the decl headers like libc++'s math.h can't be
included because it refers to an undeclared function.

For the cases where weak symbols aren't being used, don't annotate
these functions with their availability information.

Also need to avoid using __builtin_available for this case because the
NDK doesn't have __isOSVersionAtLeast yet.

__ANDROID_UNGUARDED_AVAILABILITY__ is being used as a proxy for
"building for the NDK" here because we don't have a good signal for
that which works for both the NDK proper and the NDK-in-the-platform
case.

Test: imported into the NDK, built and tested NDK
Bug: None
Change-Id: I9ef3e19a8fa083bca0be47b80dfef7ba52a94866
diff --git a/libc/include/android/legacy_signal_inlines.h b/libc/include/android/legacy_signal_inlines.h
index a673446..e968c45 100644
--- a/libc/include/android/legacy_signal_inlines.h
+++ b/libc/include/android/legacy_signal_inlines.h
@@ -45,14 +45,32 @@
 int __libc_current_sigrtmin() __attribute__((__weak__)) __VERSIONER_NO_GUARD;
 
 static __inline int __ndk_legacy___libc_current_sigrtmax() {
+  /*
+   * The NDK doesn't use libclang_rt.builtins yet (https://github.com/android/ndk/issues/1231) so it
+   * can't use __builtin_available, but the platform builds with -Werror=unguarded-availability so
+   * it requires __builtin_available.
+   */
+#if defined(__ANDROID_UNGUARDED_AVAILABILITY__)
   if (__builtin_available(android 21, *)) {
+#else
+  if (__libc_current_sigrtmax) {
+#endif
     return __libc_current_sigrtmax();
   }
   return __SIGRTMAX; /* Should match __libc_current_sigrtmax. */
 }
 
 static __inline int __ndk_legacy___libc_current_sigrtmin() {
+  /*
+   * The NDK doesn't use libclang_rt.builtins yet (https://github.com/android/ndk/issues/1231) so it
+   * can't use __builtin_available, but the platform builds with -Werror=unguarded-availability so
+   * it requires __builtin_available.
+   */
+#if defined(__ANDROID_UNGUARDED_AVAILABILITY__)
   if (__builtin_available(android 21, *)) {
+#else
+  if (__libc_current_sigrtmin) {
+#endif
     return __libc_current_sigrtmin();
   }
   return __SIGRTMIN + 7; /* Should match __libc_current_sigrtmin. */
diff --git a/libc/include/android/versioning.h b/libc/include/android/versioning.h
index 2b9a027..54ffc91 100644
--- a/libc/include/android/versioning.h
+++ b/libc/include/android/versioning.h
@@ -21,12 +21,14 @@
 #if defined(__BIONIC_VERSIONER)
 
 #define __INTRODUCED_IN(api_level) __attribute__((annotate("introduced_in=" #api_level)))
+#define __INTRODUCED_IN_NO_GUARD_FOR_NDK(api_level) __attribute__((annotate("introduced_in=" #api_level))) __VERSIONER_NO_GUARD
 #define __DEPRECATED_IN(api_level) __attribute__((annotate("deprecated_in=" #api_level)))
 #define __REMOVED_IN(api_level) __attribute__((annotate("obsoleted_in=" #api_level)))
 #define __INTRODUCED_IN_32(api_level) __attribute__((annotate("introduced_in_32=" #api_level)))
 #define __INTRODUCED_IN_64(api_level) __attribute__((annotate("introduced_in_64=" #api_level)))
 #define __INTRODUCED_IN_ARM(api_level) __attribute__((annotate("introduced_in_arm=" #api_level)))
 #define __INTRODUCED_IN_X86(api_level) __attribute__((annotate("introduced_in_x86=" #api_level)))
+#define __INTRODUCED_IN_X86_NO_GUARD_FOR_NDK(api_level) __attribute__((annotate("introduced_in_x86=" #api_level))) __VERSIONER_NO_GUARD
 
 #define __VERSIONER_NO_GUARD __attribute__((annotate("versioner_no_guard")))
 #define __VERSIONER_FORTIFY_INLINE __attribute__((annotate("versioner_fortify_inline")))
@@ -42,10 +44,19 @@
 // be enforced. In the case, the absence of 'strict' makes it possible to call an unavailable API
 // without the __builtin_available check, which will cause a link error at runtime.
 // Android platform build system defines this macro along with -Wunguarded-availability
+//
+// The _NO_GUARD_FOR_NDK variants keep the __VERSIONER_NO_GUARD behavior working for the NDK. This
+// allows libc++ to refer to these functions in inlines without needing to guard them, needed since
+// libc++ doesn't currently guard these calls. There's no risk to the apps though because using
+// those APIs will still cause a link error.
 #if defined(__ANDROID_UNGUARDED_AVAILABILITY__)
 #define __MAYBE_STRICT
+#define __INTRODUCED_IN_NO_GUARD_FOR_NDK(api_level) __INTRODUCED_IN_X86(api_level)
+#define __INTRODUCED_IN_X86_NO_GUARD_FOR_NDK(api_level) __INTRODUCED_IN_X86(api_level)
 #else
 #define __MAYBE_STRICT ,strict
+#define __INTRODUCED_IN_NO_GUARD_FOR_NDK(api_level)
+#define __INTRODUCED_IN_X86_NO_GUARD_FOR_NDK(api_level)
 #endif
 
 #define __INTRODUCED_IN(api_level) \
diff --git a/libc/include/bits/wctype.h b/libc/include/bits/wctype.h
index a51eca2..0015261 100644
--- a/libc/include/bits/wctype.h
+++ b/libc/include/bits/wctype.h
@@ -58,8 +58,8 @@
 int iswctype(wint_t __wc, wctype_t __type);
 
 typedef const void* wctrans_t;
-wint_t towctrans(wint_t __wc, wctrans_t __transform) __INTRODUCED_IN(26) __VERSIONER_NO_GUARD;
-wctrans_t wctrans(const char* __name) __INTRODUCED_IN(26) __VERSIONER_NO_GUARD;
+wint_t towctrans(wint_t __wc, wctrans_t __transform) __INTRODUCED_IN_NO_GUARD_FOR_NDK(26);
+wctrans_t wctrans(const char* __name) __INTRODUCED_IN_NO_GUARD_FOR_NDK(26);
 
 __END_DECLS
 
diff --git a/libc/include/locale.h b/libc/include/locale.h
index 4290992..8785b24 100644
--- a/libc/include/locale.h
+++ b/libc/include/locale.h
@@ -96,7 +96,7 @@
   char int_n_sign_posn;
 };
 
-struct lconv* localeconv(void) __INTRODUCED_IN(21) __VERSIONER_NO_GUARD;
+struct lconv* localeconv(void) __INTRODUCED_IN_NO_GUARD_FOR_NDK(21);
 
 locale_t duplocale(locale_t __l) __INTRODUCED_IN(21);
 void freelocale(locale_t __l) __INTRODUCED_IN(21);
diff --git a/libc/include/math.h b/libc/include/math.h
index 8c084b2..040dc96 100644
--- a/libc/include/math.h
+++ b/libc/include/math.h
@@ -178,9 +178,9 @@
 long double scalbnl(long double __x, int __exponent) __RENAME_LDBL(scalbn, 3, 3);
 
 /* TODO: once the NDK only supports >= 18, use __RENAME_LDBL here too. */
-double scalbln(double __x, long __exponent) __INTRODUCED_IN_X86(18) __VERSIONER_NO_GUARD;
-float scalblnf(float __x, long __exponent) __INTRODUCED_IN_X86(18) __VERSIONER_NO_GUARD;
-long double scalblnl(long double __x, long __exponent) __INTRODUCED_IN_X86(18) __VERSIONER_NO_GUARD;
+double scalbln(double __x, long __exponent) __INTRODUCED_IN_X86_NO_GUARD_FOR_NDK(18);
+float scalblnf(float __x, long __exponent) __INTRODUCED_IN_X86_NO_GUARD_FOR_NDK(18);
+long double scalblnl(long double __x, long __exponent) __INTRODUCED_IN_X86_NO_GUARD_FOR_NDK(18);
 
 double cbrt(double __x);
 float cbrtf(float __x);
@@ -280,11 +280,11 @@
 
 double nextafter(double __x, double __y);
 float nextafterf(float __x, float __y);
-long double nextafterl(long double __x, long double __y) __RENAME_LDBL(nextafter, 3, 21) __VERSIONER_NO_GUARD;
+long double nextafterl(long double __x, long double __y) __RENAME_LDBL_NO_GUARD_FOR_NDK(nextafter, 3, 21);
 
-double nexttoward(double __x, long double __y) __INTRODUCED_IN(18) __VERSIONER_NO_GUARD;
+double nexttoward(double __x, long double __y) __INTRODUCED_IN_NO_GUARD_FOR_NDK(18);
 float nexttowardf(float __x, long double __y);
-long double nexttowardl(long double __x, long double __y) __RENAME_LDBL(nexttoward, 18, 18) __VERSIONER_NO_GUARD;
+long double nexttowardl(long double __x, long double __y) __RENAME_LDBL_NO_GUARD_FOR_NDK(nexttoward, 18, 18);
 
 double fdim(double __x, double __y);
 float fdimf(float __x, float __y);
@@ -300,7 +300,7 @@
 
 double fma(double __x, double __y, double __z);
 float fmaf(float __x, float __y, float __z);
-long double fmal(long double __x, long double __y, long double __z) __RENAME_LDBL(fma, 3, 21) __VERSIONER_NO_GUARD;
+long double fmal(long double __x, long double __y, long double __z) __RENAME_LDBL_NO_GUARD_FOR_NDK(fma, 3, 21);
 
 #define isgreater(x, y) __builtin_isgreater((x), (y))
 #define isgreaterequal(x, y) __builtin_isgreaterequal((x), (y))
diff --git a/libc/include/stdlib.h b/libc/include/stdlib.h
index dc7b694..d7fdb4a 100644
--- a/libc/include/stdlib.h
+++ b/libc/include/stdlib.h
@@ -160,10 +160,10 @@
 const char* getprogname(void) __INTRODUCED_IN(21);
 void setprogname(const char* __name) __INTRODUCED_IN(21);
 
-int mblen(const char* __s, size_t __n) __INTRODUCED_IN(26) __VERSIONER_NO_GUARD;
+int mblen(const char* __s, size_t __n) __INTRODUCED_IN_NO_GUARD_FOR_NDK(26);
 size_t mbstowcs(wchar_t* __dst, const char* __src, size_t __n);
-int mbtowc(wchar_t* __wc_ptr, const char* __s, size_t __n) __INTRODUCED_IN(21) __VERSIONER_NO_GUARD;
-int wctomb(char* __dst, wchar_t __wc) __INTRODUCED_IN(21) __VERSIONER_NO_GUARD;
+int mbtowc(wchar_t* __wc_ptr, const char* __s, size_t __n) __INTRODUCED_IN_NO_GUARD_FOR_NDK(21);
+int wctomb(char* __dst, wchar_t __wc) __INTRODUCED_IN_NO_GUARD_FOR_NDK(21);
 
 size_t wcstombs(char* __dst, const wchar_t* __src, size_t __n);
 
diff --git a/libc/include/sys/cdefs.h b/libc/include/sys/cdefs.h
index b4556a7..b1bd713 100644
--- a/libc/include/sys/cdefs.h
+++ b/libc/include/sys/cdefs.h
@@ -202,11 +202,18 @@
  * Note that some functions have their __RENAME_LDBL commented out as a sign that although we could
  * use __RENAME_LDBL it would actually cause the function to be introduced later because the
  * `long double` variant appeared before the `double` variant.
+ *
+ * The _NO_GUARD_FOR_NDK variants keep the __VERSIONER_NO_GUARD behavior working for the NDK. This
+ * allows libc++ to refer to these functions in inlines without needing to guard them, needed since
+ * libc++ doesn't currently guard these calls. There's no risk to the apps though because using
+ * those APIs will still cause a link error.
  */
 #if defined(__LP64__) || defined(__BIONIC_LP32_USE_LONG_DOUBLE)
 #define __RENAME_LDBL(rewrite,rewrite_api_level,regular_api_level) __INTRODUCED_IN(regular_api_level)
+#define __RENAME_LDBL_NO_GUARD_FOR_NDK(rewrite,rewrite_api_level,regular_api_level) __INTRODUCED_IN_NO_GUARD_FOR_NDK(regular_api_level)
 #else
 #define __RENAME_LDBL(rewrite,rewrite_api_level,regular_api_level) __RENAME(rewrite) __INTRODUCED_IN(rewrite_api_level)
+#define __RENAME_LDBL_NO_GUARD_FOR_NDK(rewrite,rewrite_api_level,regular_api_level) __RENAME(rewrite) __INTRODUCED_IN_NO_GUARD_FOR_NDK(rewrite_api_level)
 #endif
 
 /*