1*8d67ca89SAndroid Build Coastguard Worker /* 2*8d67ca89SAndroid Build Coastguard Worker * Copyright (C) 2016 The Android Open Source Project 3*8d67ca89SAndroid Build Coastguard Worker * 4*8d67ca89SAndroid Build Coastguard Worker * Licensed under the Apache License, Version 2.0 (the "License"); 5*8d67ca89SAndroid Build Coastguard Worker * you may not use this file except in compliance with the License. 6*8d67ca89SAndroid Build Coastguard Worker * You may obtain a copy of the License at 7*8d67ca89SAndroid Build Coastguard Worker * 8*8d67ca89SAndroid Build Coastguard Worker * http://www.apache.org/licenses/LICENSE-2.0 9*8d67ca89SAndroid Build Coastguard Worker * 10*8d67ca89SAndroid Build Coastguard Worker * Unless required by applicable law or agreed to in writing, software 11*8d67ca89SAndroid Build Coastguard Worker * distributed under the License is distributed on an "AS IS" BASIS, 12*8d67ca89SAndroid Build Coastguard Worker * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13*8d67ca89SAndroid Build Coastguard Worker * See the License for the specific language governing permissions and 14*8d67ca89SAndroid Build Coastguard Worker * limitations under the License. 15*8d67ca89SAndroid Build Coastguard Worker */ 16*8d67ca89SAndroid Build Coastguard Worker 17*8d67ca89SAndroid Build Coastguard Worker #pragma once 18*8d67ca89SAndroid Build Coastguard Worker 19*8d67ca89SAndroid Build Coastguard Worker /** 20*8d67ca89SAndroid Build Coastguard Worker * @def __ANDROID_UNAVAILABLE_SYMBOLS_ARE_WEAK__ 21*8d67ca89SAndroid Build Coastguard Worker * 22*8d67ca89SAndroid Build Coastguard Worker * Controls whether calling APIs newer than the developer's minSdkVersion are a 23*8d67ca89SAndroid Build Coastguard Worker * build error (when not defined) or allowed as a weak reference with a 24*8d67ca89SAndroid Build Coastguard Worker * __builtin_available() guard (when defined). 25*8d67ca89SAndroid Build Coastguard Worker * 26*8d67ca89SAndroid Build Coastguard Worker * See https://developer.android.com/ndk/guides/using-newer-apis for more 27*8d67ca89SAndroid Build Coastguard Worker * details. 28*8d67ca89SAndroid Build Coastguard Worker */ 29*8d67ca89SAndroid Build Coastguard Worker #if defined(__ANDROID_UNAVAILABLE_SYMBOLS_ARE_WEAK__) 30*8d67ca89SAndroid Build Coastguard Worker // In this mode, Clang will emit weak references to the APIs if the 31*8d67ca89SAndroid Build Coastguard Worker // minSdkVersion is less than the __what argument. This allows the libraries to 32*8d67ca89SAndroid Build Coastguard Worker // load even on systems too old to contain the API, but calls must be guarded 33*8d67ca89SAndroid Build Coastguard Worker // with `__builtin_available(android api_level, *)` to avoid segfaults. 34*8d67ca89SAndroid Build Coastguard Worker #define __BIONIC_AVAILABILITY(__what, ...) __attribute__((__availability__(android,__what __VA_OPT__(,) __VA_ARGS__))) 35*8d67ca89SAndroid Build Coastguard Worker 36*8d67ca89SAndroid Build Coastguard Worker // When the caller is using weak API references, we should expose the decls for 37*8d67ca89SAndroid Build Coastguard Worker // APIs which are not available in the caller's minSdkVersion, otherwise there's 38*8d67ca89SAndroid Build Coastguard Worker // no way to take advantage of the weak references. 39*8d67ca89SAndroid Build Coastguard Worker #define __BIONIC_AVAILABILITY_GUARD(api_level) 1 40*8d67ca89SAndroid Build Coastguard Worker #else 41*8d67ca89SAndroid Build Coastguard Worker // The 'strict' flag is required for NDK clients where the code was not written 42*8d67ca89SAndroid Build Coastguard Worker // to handle the case where the API was available at build-time but not at 43*8d67ca89SAndroid Build Coastguard Worker // run-time. Most 3p code ported to Android was not written to use 44*8d67ca89SAndroid Build Coastguard Worker // `__builtin_available()` for run-time availability checking, and so would not 45*8d67ca89SAndroid Build Coastguard Worker // compile in this mode (or worse, if the build doesn't use 46*8d67ca89SAndroid Build Coastguard Worker // -Werror=unguarded-availability, it would build but crash at runtime). 47*8d67ca89SAndroid Build Coastguard Worker #define __BIONIC_AVAILABILITY(__what, ...) __attribute__((__availability__(android,strict,__what __VA_OPT__(,) __VA_ARGS__))) 48*8d67ca89SAndroid Build Coastguard Worker 49*8d67ca89SAndroid Build Coastguard Worker // When the caller is using strict API references, we hide APIs which are not 50*8d67ca89SAndroid Build Coastguard Worker // available in the caller's minSdkVersion. This is a bionic-only deviation in 51*8d67ca89SAndroid Build Coastguard Worker // behavior from the rest of the NDK headers, but it's necessary to maintain 52*8d67ca89SAndroid Build Coastguard Worker // source compatibility with 3p libraries that either can't correctly detect API 53*8d67ca89SAndroid Build Coastguard Worker // availability (either incorrectly detecting as always-available or as 54*8d67ca89SAndroid Build Coastguard Worker // never-available, but neither is true), or define their own polyfills which 55*8d67ca89SAndroid Build Coastguard Worker // conflict with our declarations. 56*8d67ca89SAndroid Build Coastguard Worker // 57*8d67ca89SAndroid Build Coastguard Worker // https://github.com/android/ndk/issues/2081 58*8d67ca89SAndroid Build Coastguard Worker #define __BIONIC_AVAILABILITY_GUARD(api_level) (__ANDROID_MIN_SDK_VERSION__ >= (api_level)) 59*8d67ca89SAndroid Build Coastguard Worker #endif 60*8d67ca89SAndroid Build Coastguard Worker 61*8d67ca89SAndroid Build Coastguard Worker #pragma clang diagnostic push 62*8d67ca89SAndroid Build Coastguard Worker #pragma clang diagnostic ignored "-Wc23-extensions" 63*8d67ca89SAndroid Build Coastguard Worker // Passing no argument for the '...' parameter of a variadic macro is a C23 extension 64*8d67ca89SAndroid Build Coastguard Worker #define __INTRODUCED_IN(api_level) __BIONIC_AVAILABILITY(introduced=api_level) 65*8d67ca89SAndroid Build Coastguard Worker #pragma clang diagnostic pop 66*8d67ca89SAndroid Build Coastguard Worker 67*8d67ca89SAndroid Build Coastguard Worker #define __DEPRECATED_IN(api_level, msg) __BIONIC_AVAILABILITY(deprecated=api_level, message=msg) 68*8d67ca89SAndroid Build Coastguard Worker #define __REMOVED_IN(api_level, msg) __BIONIC_AVAILABILITY(obsoleted=api_level, message=msg) 69*8d67ca89SAndroid Build Coastguard Worker 70*8d67ca89SAndroid Build Coastguard Worker // The same availability attribute can't be annotated multiple times. Therefore, the macros are 71*8d67ca89SAndroid Build Coastguard Worker // defined for the configuration that it is valid for so that declarations like the below doesn't 72*8d67ca89SAndroid Build Coastguard Worker // cause inconsistent availability values which is an error with -Wavailability: 73*8d67ca89SAndroid Build Coastguard Worker // 74*8d67ca89SAndroid Build Coastguard Worker // void foo() __INTRODUCED_IN_32(30) __INTRODUCED_IN_64(31); 75*8d67ca89SAndroid Build Coastguard Worker // 76*8d67ca89SAndroid Build Coastguard Worker #if !defined(__LP64__) 77*8d67ca89SAndroid Build Coastguard Worker #define __INTRODUCED_IN_32(api_level) __BIONIC_AVAILABILITY(introduced=api_level) 78*8d67ca89SAndroid Build Coastguard Worker #define __INTRODUCED_IN_64(api_level) 79*8d67ca89SAndroid Build Coastguard Worker #else 80*8d67ca89SAndroid Build Coastguard Worker #define __INTRODUCED_IN_32(api_level) 81*8d67ca89SAndroid Build Coastguard Worker #define __INTRODUCED_IN_64(api_level) __BIONIC_AVAILABILITY(introduced=api_level) 82*8d67ca89SAndroid Build Coastguard Worker #endif 83