330 lines · c
1// -*- C++ -*-2//===----------------------------------------------------------------------===//3//4// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.5// See https://llvm.org/LICENSE.txt for license information.6// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception7//8//===----------------------------------------------------------------------===//9 10#ifndef _LIBCPP___CONFIGURATION_AVAILABILITY_H11#define _LIBCPP___CONFIGURATION_AVAILABILITY_H12 13#include <__configuration/compiler.h>14#include <__configuration/language.h>15 16#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)17# pragma GCC system_header18#endif19 20// Libc++ is shipped by various vendors. In particular, it is used as a system21// library on macOS, iOS and other Apple platforms. In order for users to be22// able to compile a binary that is intended to be deployed to an older version23// of a platform, Clang provides availability attributes [1]. These attributes24// can be placed on declarations and are used to describe the life cycle of a25// symbol in the library.26//27// The main goal is to ensure a compile-time error if a symbol that hasn't been28// introduced in a previously released library is used in a program that targets29// that previously released library. Normally, this would be a load-time error30// when one tries to launch the program against the older library.31//32// For example, the filesystem library was introduced in the dylib in LLVM 9.33// On Apple platforms, this corresponds to macOS 10.15. If a user compiles on34// a macOS 10.15 host but targets macOS 10.13 with their program, the compiler35// would normally not complain (because the required declarations are in the36// headers), but the dynamic loader would fail to find the symbols when actually37// trying to launch the program on macOS 10.13. To turn this into a compile-time38// issue instead, declarations are annotated with when they were introduced, and39// the compiler can produce a diagnostic if the program references something that40// isn't available on the deployment target.41//42// This mechanism is general in nature, and any vendor can add their markup to43// the library (see below). Whenever a new feature is added that requires support44// in the shared library, two macros are added below to allow marking the feature45// as unavailable:46// 1. A macro named `_LIBCPP_AVAILABILITY_HAS_<feature>` which must be defined47// to `_LIBCPP_INTRODUCED_IN_<version>` for the appropriate LLVM version.48// 2. A macro named `_LIBCPP_AVAILABILITY_<feature>`, which must be defined to49// `_LIBCPP_INTRODUCED_IN_<version>_MARKUP` for the appropriate LLVM version.50//51// When vendors decide to ship the feature as part of their shared library, they52// can update the `_LIBCPP_INTRODUCED_IN_<version>` macro (and the markup counterpart)53// based on the platform version they shipped that version of LLVM in. The library54// will then use this markup to provide an optimal user experience on these platforms.55//56// Furthermore, many features in the standard library have corresponding57// feature-test macros. The `_LIBCPP_AVAILABILITY_HAS_<feature>` macros58// are checked by the corresponding feature-test macros generated by59// generate_feature_test_macro_components.py to ensure that the library60// doesn't announce a feature as being implemented if it is unavailable on61// the deployment target.62//63// Note that this mechanism is disabled by default in the "upstream" libc++.64// Availability annotations are only meaningful when shipping libc++ inside65// a platform (i.e. as a system library), and so vendors that want them should66// turn those annotations on at CMake configuration time.67//68// [1]: https://clang.llvm.org/docs/AttributeReference.html#availability69 70// Availability markup is disabled when building the library, or when a non-Clang71// compiler is used because only Clang supports the necessary attributes.72//73// We also allow users to force-disable availability markup via the `_LIBCPP_DISABLE_AVAILABILITY`74// macro because that is the only way to work around a Clang bug related to availability75// attributes: https://llvm.org/PR134151.76// Once that bug has been fixed, we should remove the macro.77#if defined(_LIBCPP_BUILDING_LIBRARY) || defined(_LIBCXXABI_BUILDING_LIBRARY) || \78 !defined(_LIBCPP_COMPILER_CLANG_BASED) || defined(_LIBCPP_DISABLE_AVAILABILITY)79# undef _LIBCPP_HAS_VENDOR_AVAILABILITY_ANNOTATIONS80# define _LIBCPP_HAS_VENDOR_AVAILABILITY_ANNOTATIONS 081#endif82 83// When availability annotations are disabled, we take for granted that features introduced84// in all versions of the library are available.85#if !_LIBCPP_HAS_VENDOR_AVAILABILITY_ANNOTATIONS86 87# define _LIBCPP_INTRODUCED_IN_LLVM_21 188# define _LIBCPP_INTRODUCED_IN_LLVM_21_ATTRIBUTE /* nothing */89 90# define _LIBCPP_INTRODUCED_IN_LLVM_20 191# define _LIBCPP_INTRODUCED_IN_LLVM_20_ATTRIBUTE /* nothing */92 93# define _LIBCPP_INTRODUCED_IN_LLVM_19 194# define _LIBCPP_INTRODUCED_IN_LLVM_19_ATTRIBUTE /* nothing */95 96# define _LIBCPP_INTRODUCED_IN_LLVM_18 197# define _LIBCPP_INTRODUCED_IN_LLVM_18_ATTRIBUTE /* nothing */98 99# define _LIBCPP_INTRODUCED_IN_LLVM_16 1100# define _LIBCPP_INTRODUCED_IN_LLVM_16_ATTRIBUTE /* nothing */101 102# define _LIBCPP_INTRODUCED_IN_LLVM_15 1103# define _LIBCPP_INTRODUCED_IN_LLVM_15_ATTRIBUTE /* nothing */104 105# define _LIBCPP_INTRODUCED_IN_LLVM_14 1106# define _LIBCPP_INTRODUCED_IN_LLVM_14_ATTRIBUTE /* nothing */107 108# define _LIBCPP_INTRODUCED_IN_LLVM_12 1109# define _LIBCPP_INTRODUCED_IN_LLVM_12_ATTRIBUTE /* nothing */110 111#elif defined(__APPLE__)112 113// clang-format off114 115// LLVM 21116// TODO: Fill this in117# define _LIBCPP_INTRODUCED_IN_LLVM_21 0118# define _LIBCPP_INTRODUCED_IN_LLVM_21_ATTRIBUTE __attribute__((unavailable))119 120// LLVM 20121//122// Note that versions for most Apple OSes were bumped forward and aligned in that release.123# if (defined(__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__) && __ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ < 260000) || \124 (defined(__ENVIRONMENT_IPHONE_OS_VERSION_MIN_REQUIRED__) && __ENVIRONMENT_IPHONE_OS_VERSION_MIN_REQUIRED__ < 260000) || \125 (defined(__ENVIRONMENT_TV_OS_VERSION_MIN_REQUIRED__) && __ENVIRONMENT_TV_OS_VERSION_MIN_REQUIRED__ < 260000) || \126 (defined(__ENVIRONMENT_WATCH_OS_VERSION_MIN_REQUIRED__) && __ENVIRONMENT_WATCH_OS_VERSION_MIN_REQUIRED__ < 260000) || \127 (defined(__ENVIRONMENT_BRIDGE_OS_VERSION_MIN_REQUIRED__) && __ENVIRONMENT_BRIDGE_OS_VERSION_MIN_REQUIRED__ < 100000)128# define _LIBCPP_INTRODUCED_IN_LLVM_20 0129# else130# define _LIBCPP_INTRODUCED_IN_LLVM_20 1131# endif132# define _LIBCPP_INTRODUCED_IN_LLVM_20_ATTRIBUTE \133 __attribute__((availability(macos, strict, introduced = 26.0))) \134 __attribute__((availability(ios, strict, introduced = 26.0))) \135 __attribute__((availability(tvos, strict, introduced = 26.0))) \136 __attribute__((availability(watchos, strict, introduced = 26.0))) \137 __attribute__((availability(bridgeos, strict, introduced = 10.0)))138 139// LLVM 19140# if (defined(__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__) && __ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ < 150400) || \141 (defined(__ENVIRONMENT_IPHONE_OS_VERSION_MIN_REQUIRED__) && __ENVIRONMENT_IPHONE_OS_VERSION_MIN_REQUIRED__ < 180400) || \142 (defined(__ENVIRONMENT_TV_OS_VERSION_MIN_REQUIRED__) && __ENVIRONMENT_TV_OS_VERSION_MIN_REQUIRED__ < 180400) || \143 (defined(__ENVIRONMENT_WATCH_OS_VERSION_MIN_REQUIRED__) && __ENVIRONMENT_WATCH_OS_VERSION_MIN_REQUIRED__ < 110400) || \144 (defined(__ENVIRONMENT_BRIDGE_OS_VERSION_MIN_REQUIRED__) && __ENVIRONMENT_BRIDGE_OS_VERSION_MIN_REQUIRED__ < 90400)145# define _LIBCPP_INTRODUCED_IN_LLVM_19 0146# else147# define _LIBCPP_INTRODUCED_IN_LLVM_19 1148# endif149# define _LIBCPP_INTRODUCED_IN_LLVM_19_ATTRIBUTE \150 __attribute__((availability(macos, strict, introduced = 15.4))) \151 __attribute__((availability(ios, strict, introduced = 18.4))) \152 __attribute__((availability(tvos, strict, introduced = 18.4))) \153 __attribute__((availability(watchos, strict, introduced = 11.4))) \154 __attribute__((availability(bridgeos, strict, introduced = 9.4)))155 156// LLVM 18157# if (defined(__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__) && __ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ < 150000) || \158 (defined(__ENVIRONMENT_IPHONE_OS_VERSION_MIN_REQUIRED__) && __ENVIRONMENT_IPHONE_OS_VERSION_MIN_REQUIRED__ < 180000) || \159 (defined(__ENVIRONMENT_TV_OS_VERSION_MIN_REQUIRED__) && __ENVIRONMENT_TV_OS_VERSION_MIN_REQUIRED__ < 180000) || \160 (defined(__ENVIRONMENT_WATCH_OS_VERSION_MIN_REQUIRED__) && __ENVIRONMENT_WATCH_OS_VERSION_MIN_REQUIRED__ < 110000) || \161 (defined(__ENVIRONMENT_BRIDGE_OS_VERSION_MIN_REQUIRED__) && __ENVIRONMENT_BRIDGE_OS_VERSION_MIN_REQUIRED__ < 90000) || \162 (defined(__ENVIRONMENT_DRIVERKIT_VERSION_MIN_REQUIRED__) && __ENVIRONMENT_DRIVERKIT_VERSION_MIN_REQUIRED__ < 240000)163# define _LIBCPP_INTRODUCED_IN_LLVM_18 0164# else165# define _LIBCPP_INTRODUCED_IN_LLVM_18 1166# endif167# define _LIBCPP_INTRODUCED_IN_LLVM_18_ATTRIBUTE \168 __attribute__((availability(macos, strict, introduced = 15.0))) \169 __attribute__((availability(ios, strict, introduced = 18.0))) \170 __attribute__((availability(tvos, strict, introduced = 18.0))) \171 __attribute__((availability(watchos, strict, introduced = 11.0))) \172 __attribute__((availability(bridgeos, strict, introduced = 9.0))) \173 __attribute__((availability(driverkit, strict, introduced = 24.0)))174 175// LLVM 16176# if (defined(__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__) && __ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ < 140000) || \177 (defined(__ENVIRONMENT_IPHONE_OS_VERSION_MIN_REQUIRED__) && __ENVIRONMENT_IPHONE_OS_VERSION_MIN_REQUIRED__ < 170000) || \178 (defined(__ENVIRONMENT_TV_OS_VERSION_MIN_REQUIRED__) && __ENVIRONMENT_TV_OS_VERSION_MIN_REQUIRED__ < 170000) || \179 (defined(__ENVIRONMENT_WATCH_OS_VERSION_MIN_REQUIRED__) && __ENVIRONMENT_WATCH_OS_VERSION_MIN_REQUIRED__ < 100000) || \180 (defined(__ENVIRONMENT_BRIDGE_OS_VERSION_MIN_REQUIRED__) && __ENVIRONMENT_BRIDGE_OS_VERSION_MIN_REQUIRED__ < 80000) || \181 (defined(__ENVIRONMENT_DRIVERKIT_VERSION_MIN_REQUIRED__) && __ENVIRONMENT_DRIVERKIT_VERSION_MIN_REQUIRED__ < 230000)182# define _LIBCPP_INTRODUCED_IN_LLVM_16 0183# else184# define _LIBCPP_INTRODUCED_IN_LLVM_16 1185# endif186# define _LIBCPP_INTRODUCED_IN_LLVM_16_ATTRIBUTE \187 __attribute__((availability(macos, strict, introduced = 14.0))) \188 __attribute__((availability(ios, strict, introduced = 17.0))) \189 __attribute__((availability(tvos, strict, introduced = 17.0))) \190 __attribute__((availability(watchos, strict, introduced = 10.0))) \191 __attribute__((availability(bridgeos, strict, introduced = 8.0))) \192 __attribute__((availability(driverkit, strict, introduced = 23.0)))193 194// LLVM 15195# if (defined(__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__) && __ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ < 130300) || \196 (defined(__ENVIRONMENT_IPHONE_OS_VERSION_MIN_REQUIRED__) && __ENVIRONMENT_IPHONE_OS_VERSION_MIN_REQUIRED__ < 160300) || \197 (defined(__ENVIRONMENT_TV_OS_VERSION_MIN_REQUIRED__) && __ENVIRONMENT_TV_OS_VERSION_MIN_REQUIRED__ < 160300) || \198 (defined(__ENVIRONMENT_WATCH_OS_VERSION_MIN_REQUIRED__) && __ENVIRONMENT_WATCH_OS_VERSION_MIN_REQUIRED__ < 90300) || \199 (defined(__ENVIRONMENT_BRIDGE_OS_VERSION_MIN_REQUIRED__) && __ENVIRONMENT_BRIDGE_OS_VERSION_MIN_REQUIRED__ < 70500) || \200 (defined(__ENVIRONMENT_DRIVERKIT_VERSION_MIN_REQUIRED__) && __ENVIRONMENT_DRIVERKIT_VERSION_MIN_REQUIRED__ < 220400)201# define _LIBCPP_INTRODUCED_IN_LLVM_15 0202# else203# define _LIBCPP_INTRODUCED_IN_LLVM_15 1204# endif205# define _LIBCPP_INTRODUCED_IN_LLVM_15_ATTRIBUTE \206 __attribute__((availability(macos, strict, introduced = 13.3))) \207 __attribute__((availability(ios, strict, introduced = 16.3))) \208 __attribute__((availability(tvos, strict, introduced = 16.3))) \209 __attribute__((availability(watchos, strict, introduced = 9.3))) \210 __attribute__((availability(bridgeos, strict, introduced = 7.5))) \211 __attribute__((availability(driverkit, strict, introduced = 22.4)))212 213// LLVM 14214# define _LIBCPP_INTRODUCED_IN_LLVM_14 _LIBCPP_INTRODUCED_IN_LLVM_15215# define _LIBCPP_INTRODUCED_IN_LLVM_14_ATTRIBUTE _LIBCPP_INTRODUCED_IN_LLVM_15_ATTRIBUTE216 217// LLVM 12218# if (defined(__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__) && __ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ < 120300) || \219 (defined(__ENVIRONMENT_IPHONE_OS_VERSION_MIN_REQUIRED__) && __ENVIRONMENT_IPHONE_OS_VERSION_MIN_REQUIRED__ < 150300) || \220 (defined(__ENVIRONMENT_TV_OS_VERSION_MIN_REQUIRED__) && __ENVIRONMENT_TV_OS_VERSION_MIN_REQUIRED__ < 150300) || \221 (defined(__ENVIRONMENT_WATCH_OS_VERSION_MIN_REQUIRED__) && __ENVIRONMENT_WATCH_OS_VERSION_MIN_REQUIRED__ < 80300) || \222 (defined(__ENVIRONMENT_BRIDGE_OS_VERSION_MIN_REQUIRED__) && __ENVIRONMENT_BRIDGE_OS_VERSION_MIN_REQUIRED__ < 60000) || \223 (defined(__ENVIRONMENT_DRIVERKIT_VERSION_MIN_REQUIRED__) && __ENVIRONMENT_DRIVERKIT_VERSION_MIN_REQUIRED__ < 210300)224# define _LIBCPP_INTRODUCED_IN_LLVM_12 0225# else226# define _LIBCPP_INTRODUCED_IN_LLVM_12 1227# endif228# define _LIBCPP_INTRODUCED_IN_LLVM_12_ATTRIBUTE \229 __attribute__((availability(macos, strict, introduced = 12.3))) \230 __attribute__((availability(ios, strict, introduced = 15.3))) \231 __attribute__((availability(tvos, strict, introduced = 15.3))) \232 __attribute__((availability(watchos, strict, introduced = 8.3))) \233 __attribute__((availability(bridgeos, strict, introduced = 6.0))) \234 __attribute__((availability(driverkit, strict, introduced = 21.3)))235 236#else237 238// ...New vendors can add availability markup here...239 240# error \241 "It looks like you're trying to enable vendor availability markup, but you haven't defined the corresponding macros yet!"242 243#endif244 245// Enable additional explicit instantiations of iostreams components. This246// reduces the number of weak definitions generated in programs that use247// iostreams by providing a single strong definition in the shared library.248//249// TODO: Enable additional explicit instantiations on GCC once it supports exclude_from_explicit_instantiation,250// or once libc++ doesn't use the attribute anymore.251// TODO: Enable them on Windows once https://llvm.org/PR41018 has been fixed.252#if !defined(_LIBCPP_COMPILER_GCC) && !defined(_WIN32)253# define _LIBCPP_AVAILABILITY_HAS_ADDITIONAL_IOSTREAM_EXPLICIT_INSTANTIATIONS_1 _LIBCPP_INTRODUCED_IN_LLVM_12254#else255# define _LIBCPP_AVAILABILITY_HAS_ADDITIONAL_IOSTREAM_EXPLICIT_INSTANTIATIONS_1 0256#endif257 258// This controls the availability of floating-point std::to_chars functions.259// These overloads were added later than the integer overloads.260#define _LIBCPP_AVAILABILITY_HAS_TO_CHARS_FLOATING_POINT _LIBCPP_INTRODUCED_IN_LLVM_14261#define _LIBCPP_AVAILABILITY_TO_CHARS_FLOATING_POINT _LIBCPP_INTRODUCED_IN_LLVM_14_ATTRIBUTE262 263// This controls whether the library claims to provide a default verbose264// termination function, and consequently whether the headers will try265// to use it when the mechanism isn't overriden at compile-time.266#define _LIBCPP_AVAILABILITY_HAS_VERBOSE_ABORT _LIBCPP_INTRODUCED_IN_LLVM_15267#define _LIBCPP_AVAILABILITY_VERBOSE_ABORT _LIBCPP_INTRODUCED_IN_LLVM_15_ATTRIBUTE268 269// This controls the availability of the C++17 std::pmr library,270// which is implemented in large part in the built library.271//272// TODO: Enable std::pmr markup once https://llvm.org/PR40340 has been fixed273// Until then, it is possible for folks to try to use `std::pmr` when back-deploying to targets that don't support274// it and it'll be a load-time error, but we don't have a good alternative because the library won't compile if we275// use availability annotations until that bug has been fixed.276#define _LIBCPP_AVAILABILITY_HAS_PMR _LIBCPP_INTRODUCED_IN_LLVM_16277#define _LIBCPP_AVAILABILITY_PMR278 279// These macros controls the availability of __cxa_init_primary_exception280// in the built library, which std::make_exception_ptr might use281// (see libcxx/include/__exception/exception_ptr.h).282#define _LIBCPP_AVAILABILITY_HAS_INIT_PRIMARY_EXCEPTION _LIBCPP_INTRODUCED_IN_LLVM_18283#define _LIBCPP_AVAILABILITY_INIT_PRIMARY_EXCEPTION _LIBCPP_INTRODUCED_IN_LLVM_18_ATTRIBUTE284 285// This controls the availability of C++23 <print>, which286// has a dependency on the built library (it needs access to287// the underlying buffer types of std::cout, std::cerr, and std::clog.288#define _LIBCPP_AVAILABILITY_HAS_PRINT _LIBCPP_INTRODUCED_IN_LLVM_18289#define _LIBCPP_AVAILABILITY_PRINT _LIBCPP_INTRODUCED_IN_LLVM_18_ATTRIBUTE290 291// This controls the availability of the C++20 time zone database.292// The parser code is built in the library.293#define _LIBCPP_AVAILABILITY_HAS_TZDB _LIBCPP_INTRODUCED_IN_LLVM_19294#define _LIBCPP_AVAILABILITY_TZDB _LIBCPP_INTRODUCED_IN_LLVM_19_ATTRIBUTE295 296// These macros determine whether we assume that std::bad_function_call and297// std::bad_expected_access provide a key function in the dylib. This allows298// centralizing their vtable and typeinfo instead of having all TUs provide299// a weak definition that then gets deduplicated.300#define _LIBCPP_AVAILABILITY_HAS_BAD_FUNCTION_CALL_KEY_FUNCTION _LIBCPP_INTRODUCED_IN_LLVM_19301#define _LIBCPP_AVAILABILITY_BAD_FUNCTION_CALL_KEY_FUNCTION _LIBCPP_INTRODUCED_IN_LLVM_19_ATTRIBUTE302#define _LIBCPP_AVAILABILITY_HAS_BAD_EXPECTED_ACCESS_KEY_FUNCTION _LIBCPP_INTRODUCED_IN_LLVM_19303#define _LIBCPP_AVAILABILITY_BAD_EXPECTED_ACCESS_KEY_FUNCTION _LIBCPP_INTRODUCED_IN_LLVM_19_ATTRIBUTE304 305// This controls the availability of floating-point std::from_chars functions.306// These overloads were added later than the integer overloads.307#define _LIBCPP_AVAILABILITY_HAS_FROM_CHARS_FLOATING_POINT _LIBCPP_INTRODUCED_IN_LLVM_20308#define _LIBCPP_AVAILABILITY_FROM_CHARS_FLOATING_POINT _LIBCPP_INTRODUCED_IN_LLVM_20_ATTRIBUTE309 310// This controls whether `std::__hash_memory` is available in the dylib, which311// is used for some `std::hash` specializations.312#define _LIBCPP_AVAILABILITY_HAS_HASH_MEMORY _LIBCPP_INTRODUCED_IN_LLVM_21313// No attribute, since we've had hash in the headers before314 315// This controls whether we provide a message for `bad_function_call::what()` that specific to `std::bad_function_call`.316// See https://wg21.link/LWG2233. This requires `std::bad_function_call::what()` to be available in the dylib.317#define _LIBCPP_AVAILABILITY_HAS_BAD_FUNCTION_CALL_GOOD_WHAT_MESSAGE _LIBCPP_INTRODUCED_IN_LLVM_21318// No attribute, since we've had bad_function_call::what() in the headers before319 320// Define availability attributes that depend on both321// _LIBCPP_HAS_EXCEPTIONS and _LIBCPP_HAS_RTTI.322#if !_LIBCPP_HAS_EXCEPTIONS || !_LIBCPP_HAS_RTTI323# undef _LIBCPP_AVAILABILITY_HAS_INIT_PRIMARY_EXCEPTION324# undef _LIBCPP_AVAILABILITY_INIT_PRIMARY_EXCEPTION325# define _LIBCPP_AVAILABILITY_HAS_INIT_PRIMARY_EXCEPTION 0326# define _LIBCPP_AVAILABILITY_INIT_PRIMARY_EXCEPTION327#endif328 329#endif // _LIBCPP___CONFIGURATION_AVAILABILITY_H330