base/config.h
Go to the documentation of this file.
1 //
2 // Copyright 2017 The Abseil Authors.
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // https://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16 // -----------------------------------------------------------------------------
17 // File: config.h
18 // -----------------------------------------------------------------------------
19 //
20 // This header file defines a set of macros for checking the presence of
21 // important compiler and platform features. Such macros can be used to
22 // produce portable code by parameterizing compilation based on the presence or
23 // lack of a given feature.
24 //
25 // We define a "feature" as some interface we wish to program to: for example,
26 // a library function or system call. A value of `1` indicates support for
27 // that feature; any other value indicates the feature support is undefined.
28 //
29 // Example:
30 //
31 // Suppose a programmer wants to write a program that uses the 'mmap()' system
32 // call. The Abseil macro for that feature (`ABSL_HAVE_MMAP`) allows you to
33 // selectively include the `mmap.h` header and bracket code using that feature
34 // in the macro:
35 //
36 // #include "absl/base/config.h"
37 //
38 // #ifdef ABSL_HAVE_MMAP
39 // #include "sys/mman.h"
40 // #endif //ABSL_HAVE_MMAP
41 //
42 // ...
43 // #ifdef ABSL_HAVE_MMAP
44 // void *ptr = mmap(...);
45 // ...
46 // #endif // ABSL_HAVE_MMAP
47 
48 #ifndef ABSL_BASE_CONFIG_H_
49 #define ABSL_BASE_CONFIG_H_
50 
51 // Included for the __GLIBC__ macro (or similar macros on other systems).
52 #include <limits.h>
53 
54 #ifdef __cplusplus
55 // Included for __GLIBCXX__, _LIBCPP_VERSION
56 #include <cstddef>
57 #endif // __cplusplus
58 
59 #if defined(__APPLE__)
60 // Included for TARGET_OS_IPHONE, __IPHONE_OS_VERSION_MIN_REQUIRED,
61 // __IPHONE_8_0.
62 #include <Availability.h>
63 #include <TargetConditionals.h>
64 #endif
65 
67 
68 // -----------------------------------------------------------------------------
69 // Compiler Feature Checks
70 // -----------------------------------------------------------------------------
71 
72 // ABSL_HAVE_BUILTIN()
73 //
74 // Checks whether the compiler supports a Clang Feature Checking Macro, and if
75 // so, checks whether it supports the provided builtin function "x" where x
76 // is one of the functions noted in
77 // https://clang.llvm.org/docs/LanguageExtensions.html
78 //
79 // Note: Use this macro to avoid an extra level of #ifdef __has_builtin check.
80 // http://releases.llvm.org/3.3/tools/clang/docs/LanguageExtensions.html
81 #ifdef __has_builtin
82 #define ABSL_HAVE_BUILTIN(x) __has_builtin(x)
83 #else
84 #define ABSL_HAVE_BUILTIN(x) 0
85 #endif
86 
87 // ABSL_HAVE_TLS is defined to 1 when __thread should be supported.
88 // We assume __thread is supported on Linux when compiled with Clang or compiled
89 // against libstdc++ with _GLIBCXX_HAVE_TLS defined.
90 #ifdef ABSL_HAVE_TLS
91 #error ABSL_HAVE_TLS cannot be directly set
92 #elif defined(__linux__) && (defined(__clang__) || defined(_GLIBCXX_HAVE_TLS))
93 #define ABSL_HAVE_TLS 1
94 #endif
95 
96 // ABSL_HAVE_STD_IS_TRIVIALLY_DESTRUCTIBLE
97 //
98 // Checks whether `std::is_trivially_destructible<T>` is supported.
99 //
100 // Notes: All supported compilers using libc++ support this feature, as does
101 // gcc >= 4.8.1 using libstdc++, and Visual Studio.
102 #ifdef ABSL_HAVE_STD_IS_TRIVIALLY_DESTRUCTIBLE
103 #error ABSL_HAVE_STD_IS_TRIVIALLY_DESTRUCTIBLE cannot be directly set
104 #elif defined(_LIBCPP_VERSION) || \
105  (!defined(__clang__) && defined(__GNUC__) && defined(__GLIBCXX__) && \
106  (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 8))) || \
107  defined(_MSC_VER)
108 #define ABSL_HAVE_STD_IS_TRIVIALLY_DESTRUCTIBLE 1
109 #endif
110 
111 // ABSL_HAVE_STD_IS_TRIVIALLY_CONSTRUCTIBLE
112 //
113 // Checks whether `std::is_trivially_default_constructible<T>` and
114 // `std::is_trivially_copy_constructible<T>` are supported.
115 
116 // ABSL_HAVE_STD_IS_TRIVIALLY_ASSIGNABLE
117 //
118 // Checks whether `std::is_trivially_copy_assignable<T>` is supported.
119 
120 // Notes: Clang with libc++ supports these features, as does gcc >= 5.1 with
121 // either libc++ or libstdc++, and Visual Studio (but not NVCC).
122 #if defined(ABSL_HAVE_STD_IS_TRIVIALLY_CONSTRUCTIBLE)
123 #error ABSL_HAVE_STD_IS_TRIVIALLY_CONSTRUCTIBLE cannot be directly set
124 #elif defined(ABSL_HAVE_STD_IS_TRIVIALLY_ASSIGNABLE)
125 #error ABSL_HAVE_STD_IS_TRIVIALLY_ASSIGNABLE cannot directly set
126 #elif (defined(__clang__) && defined(_LIBCPP_VERSION)) || \
127  (!defined(__clang__) && defined(__GNUC__) && \
128  (__GNUC__ > 5 || (__GNUC__ == 5 && __GNUC_MINOR__ >= 1)) && \
129  (defined(_LIBCPP_VERSION) || defined(__GLIBCXX__))) || \
130  (defined(_MSC_VER) && !defined(__NVCC__))
131 #define ABSL_HAVE_STD_IS_TRIVIALLY_CONSTRUCTIBLE 1
132 #define ABSL_HAVE_STD_IS_TRIVIALLY_ASSIGNABLE 1
133 #endif
134 
135 // ABSL_HAVE_THREAD_LOCAL
136 //
137 // Checks whether C++11's `thread_local` storage duration specifier is
138 // supported.
139 #ifdef ABSL_HAVE_THREAD_LOCAL
140 #error ABSL_HAVE_THREAD_LOCAL cannot be directly set
141 #elif defined(__APPLE__)
142 // Notes:
143 // * Xcode's clang did not support `thread_local` until version 8, and
144 // even then not for all iOS < 9.0.
145 // * Xcode 9.3 started disallowing `thread_local` for 32-bit iOS simulator
146 // targeting iOS 9.x.
147 // * Xcode 10 moves the deployment target check for iOS < 9.0 to link time
148 // making __has_feature unreliable there.
149 //
150 // Otherwise, `__has_feature` is only supported by Clang so it has be inside
151 // `defined(__APPLE__)` check.
152 #if __has_feature(cxx_thread_local) && \
153  !(TARGET_OS_IPHONE && __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_9_0)
154 #define ABSL_HAVE_THREAD_LOCAL 1
155 #endif
156 #else // !defined(__APPLE__)
157 #define ABSL_HAVE_THREAD_LOCAL 1
158 #endif
159 
160 // There are platforms for which TLS should not be used even though the compiler
161 // makes it seem like it's supported (Android NDK < r12b for example).
162 // This is primarily because of linker problems and toolchain misconfiguration:
163 // Abseil does not intend to support this indefinitely. Currently, the newest
164 // toolchain that we intend to support that requires this behavior is the
165 // r11 NDK - allowing for a 5 year support window on that means this option
166 // is likely to be removed around June of 2021.
167 // TLS isn't supported until NDK r12b per
168 // https://developer.android.com/ndk/downloads/revision_history.html
169 // Since NDK r16, `__NDK_MAJOR__` and `__NDK_MINOR__` are defined in
170 // <android/ndk-version.h>. For NDK < r16, users should define these macros,
171 // e.g. `-D__NDK_MAJOR__=11 -D__NKD_MINOR__=0` for NDK r11.
172 #if defined(__ANDROID__) && defined(__clang__)
173 #if __has_include(<android/ndk-version.h>)
174 #include <android/ndk-version.h>
175 #endif // __has_include(<android/ndk-version.h>)
176 #if defined(__ANDROID__) && defined(__clang__) && defined(__NDK_MAJOR__) && \
177  defined(__NDK_MINOR__) && \
178  ((__NDK_MAJOR__ < 12) || ((__NDK_MAJOR__ == 12) && (__NDK_MINOR__ < 1)))
179 #undef ABSL_HAVE_TLS
180 #undef ABSL_HAVE_THREAD_LOCAL
181 #endif
182 #endif // defined(__ANDROID__) && defined(__clang__)
183 
184 // ABSL_HAVE_INTRINSIC_INT128
185 //
186 // Checks whether the __int128 compiler extension for a 128-bit integral type is
187 // supported.
188 //
189 // Note: __SIZEOF_INT128__ is defined by Clang and GCC when __int128 is
190 // supported, but we avoid using it in certain cases:
191 // * On Clang:
192 // * Building using Clang for Windows, where the Clang runtime library has
193 // 128-bit support only on LP64 architectures, but Windows is LLP64.
194 // * On Nvidia's nvcc:
195 // * nvcc also defines __GNUC__ and __SIZEOF_INT128__, but not all versions
196 // actually support __int128.
197 #ifdef ABSL_HAVE_INTRINSIC_INT128
198 #error ABSL_HAVE_INTRINSIC_INT128 cannot be directly set
199 #elif defined(__SIZEOF_INT128__)
200 #if (defined(__clang__) && !defined(_WIN32)) || \
201  (defined(__CUDACC__) && __CUDACC_VER_MAJOR__ >= 9) || \
202  (defined(__GNUC__) && !defined(__clang__) && !defined(__CUDACC__))
203 #define ABSL_HAVE_INTRINSIC_INT128 1
204 #elif defined(__CUDACC__)
205 // __CUDACC_VER__ is a full version number before CUDA 9, and is defined to a
206 // string explaining that it has been removed starting with CUDA 9. We use
207 // nested #ifs because there is no short-circuiting in the preprocessor.
208 // NOTE: `__CUDACC__` could be undefined while `__CUDACC_VER__` is defined.
209 #if __CUDACC_VER__ >= 70000
210 #define ABSL_HAVE_INTRINSIC_INT128 1
211 #endif // __CUDACC_VER__ >= 70000
212 #endif // defined(__CUDACC__)
213 #endif // ABSL_HAVE_INTRINSIC_INT128
214 
215 // ABSL_HAVE_EXCEPTIONS
216 //
217 // Checks whether the compiler both supports and enables exceptions. Many
218 // compilers support a "no exceptions" mode that disables exceptions.
219 //
220 // Generally, when ABSL_HAVE_EXCEPTIONS is not defined:
221 //
222 // * Code using `throw` and `try` may not compile.
223 // * The `noexcept` specifier will still compile and behave as normal.
224 // * The `noexcept` operator may still return `false`.
225 //
226 // For further details, consult the compiler's documentation.
227 #ifdef ABSL_HAVE_EXCEPTIONS
228 #error ABSL_HAVE_EXCEPTIONS cannot be directly set.
229 
230 #elif defined(__clang__)
231 // TODO(calabrese)
232 // Switch to using __cpp_exceptions when we no longer support versions < 3.6.
233 // For details on this check, see:
234 // http://releases.llvm.org/3.6.0/tools/clang/docs/ReleaseNotes.html#the-exceptions-macro
235 #if defined(__EXCEPTIONS) && __has_feature(cxx_exceptions)
236 #define ABSL_HAVE_EXCEPTIONS 1
237 #endif // defined(__EXCEPTIONS) && __has_feature(cxx_exceptions)
238 
239 // Handle remaining special cases and default to exceptions being supported.
240 #elif !(defined(__GNUC__) && (__GNUC__ < 5) && !defined(__EXCEPTIONS)) && \
241  !(defined(__GNUC__) && (__GNUC__ >= 5) && !defined(__cpp_exceptions)) && \
242  !(defined(_MSC_VER) && !defined(_CPPUNWIND))
243 #define ABSL_HAVE_EXCEPTIONS 1
244 #endif
245 
246 // -----------------------------------------------------------------------------
247 // Platform Feature Checks
248 // -----------------------------------------------------------------------------
249 
250 // Currently supported operating systems and associated preprocessor
251 // symbols:
252 //
253 // Linux and Linux-derived __linux__
254 // Android __ANDROID__ (implies __linux__)
255 // Linux (non-Android) __linux__ && !__ANDROID__
256 // Darwin (Mac OS X and iOS) __APPLE__
257 // Akaros (http://akaros.org) __ros__
258 // Windows _WIN32
259 // NaCL __native_client__
260 // AsmJS __asmjs__
261 // WebAssembly __wasm__
262 // Fuchsia __Fuchsia__
263 //
264 // Note that since Android defines both __ANDROID__ and __linux__, one
265 // may probe for either Linux or Android by simply testing for __linux__.
266 
267 // ABSL_HAVE_MMAP
268 //
269 // Checks whether the platform has an mmap(2) implementation as defined in
270 // POSIX.1-2001.
271 #ifdef ABSL_HAVE_MMAP
272 #error ABSL_HAVE_MMAP cannot be directly set
273 #elif defined(__linux__) || defined(__APPLE__) || defined(__FreeBSD__) || \
274  defined(__ros__) || defined(__native_client__) || defined(__asmjs__) || \
275  defined(__wasm__) || defined(__Fuchsia__) || defined(__sun) || \
276  defined(__ASYLO__)
277 #define ABSL_HAVE_MMAP 1
278 #endif
279 
280 // ABSL_HAVE_PTHREAD_GETSCHEDPARAM
281 //
282 // Checks whether the platform implements the pthread_(get|set)schedparam(3)
283 // functions as defined in POSIX.1-2001.
284 #ifdef ABSL_HAVE_PTHREAD_GETSCHEDPARAM
285 #error ABSL_HAVE_PTHREAD_GETSCHEDPARAM cannot be directly set
286 #elif defined(__linux__) || defined(__APPLE__) || defined(__FreeBSD__) || \
287  defined(__ros__)
288 #define ABSL_HAVE_PTHREAD_GETSCHEDPARAM 1
289 #endif
290 
291 // ABSL_HAVE_SCHED_YIELD
292 //
293 // Checks whether the platform implements sched_yield(2) as defined in
294 // POSIX.1-2001.
295 #ifdef ABSL_HAVE_SCHED_YIELD
296 #error ABSL_HAVE_SCHED_YIELD cannot be directly set
297 #elif defined(__linux__) || defined(__ros__) || defined(__native_client__)
298 #define ABSL_HAVE_SCHED_YIELD 1
299 #endif
300 
301 // ABSL_HAVE_SEMAPHORE_H
302 //
303 // Checks whether the platform supports the <semaphore.h> header and sem_open(3)
304 // family of functions as standardized in POSIX.1-2001.
305 //
306 // Note: While Apple provides <semaphore.h> for both iOS and macOS, it is
307 // explicitly deprecated and will cause build failures if enabled for those
308 // platforms. We side-step the issue by not defining it here for Apple
309 // platforms.
310 #ifdef ABSL_HAVE_SEMAPHORE_H
311 #error ABSL_HAVE_SEMAPHORE_H cannot be directly set
312 #elif defined(__linux__) || defined(__ros__)
313 #define ABSL_HAVE_SEMAPHORE_H 1
314 #endif
315 
316 // ABSL_HAVE_ALARM
317 //
318 // Checks whether the platform supports the <signal.h> header and alarm(2)
319 // function as standardized in POSIX.1-2001.
320 #ifdef ABSL_HAVE_ALARM
321 #error ABSL_HAVE_ALARM cannot be directly set
322 #elif defined(__GOOGLE_GRTE_VERSION__)
323 // feature tests for Google's GRTE
324 #define ABSL_HAVE_ALARM 1
325 #elif defined(__GLIBC__)
326 // feature test for glibc
327 #define ABSL_HAVE_ALARM 1
328 #elif defined(_MSC_VER)
329 // feature tests for Microsoft's library
330 #elif defined(__EMSCRIPTEN__)
331 // emscripten doesn't support signals
332 #elif defined(__native_client__)
333 #else
334 // other standard libraries
335 #define ABSL_HAVE_ALARM 1
336 #endif
337 
338 // ABSL_IS_LITTLE_ENDIAN
339 // ABSL_IS_BIG_ENDIAN
340 //
341 // Checks the endianness of the platform.
342 //
343 // Notes: uses the built in endian macros provided by GCC (since 4.6) and
344 // Clang (since 3.2); see
345 // https://gcc.gnu.org/onlinedocs/cpp/Common-Predefined-Macros.html.
346 // Otherwise, if _WIN32, assume little endian. Otherwise, bail with an error.
347 #if defined(ABSL_IS_BIG_ENDIAN)
348 #error "ABSL_IS_BIG_ENDIAN cannot be directly set."
349 #endif
350 #if defined(ABSL_IS_LITTLE_ENDIAN)
351 #error "ABSL_IS_LITTLE_ENDIAN cannot be directly set."
352 #endif
353 
354 #if (defined(__BYTE_ORDER__) && defined(__ORDER_LITTLE_ENDIAN__) && \
355  __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__)
356 #define ABSL_IS_LITTLE_ENDIAN 1
357 #elif defined(__BYTE_ORDER__) && defined(__ORDER_BIG_ENDIAN__) && \
358  __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
359 #define ABSL_IS_BIG_ENDIAN 1
360 #elif defined(_WIN32)
361 #define ABSL_IS_LITTLE_ENDIAN 1
362 #else
363 #error "absl endian detection needs to be set up for your compiler"
364 #endif
365 
366 // MacOS 10.13 doesn't let you use <any>, <optional>, or <variant> even though
367 // the headers exist and are publicly noted to work. See
368 // https://github.com/abseil/abseil-cpp/issues/207 and
369 // https://developer.apple.com/documentation/xcode_release_notes/xcode_10_release_notes
370 #if defined(__APPLE__) && defined(_LIBCPP_VERSION) && \
371  defined(__MAC_OS_X_VERSION_MIN_REQUIRED__) && \
372  __ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ < 101400
373 #define ABSL_INTERNAL_MACOS_CXX17_TYPES_UNAVAILABLE 1
374 #else
375 #define ABSL_INTERNAL_MACOS_CXX17_TYPES_UNAVAILABLE 0
376 #endif
377 
378 // ABSL_HAVE_STD_ANY
379 //
380 // Checks whether C++17 std::any is available by checking whether <any> exists.
381 #ifdef ABSL_HAVE_STD_ANY
382 #error "ABSL_HAVE_STD_ANY cannot be directly set."
383 #endif
384 
385 #ifdef __has_include
386 #if __has_include(<any>) && __cplusplus >= 201703L && \
387  !ABSL_INTERNAL_MACOS_CXX17_TYPES_UNAVAILABLE
388 #define ABSL_HAVE_STD_ANY 1
389 #endif
390 #endif
391 
392 // ABSL_HAVE_STD_OPTIONAL
393 //
394 // Checks whether C++17 std::optional is available.
395 #ifdef ABSL_HAVE_STD_OPTIONAL
396 #error "ABSL_HAVE_STD_OPTIONAL cannot be directly set."
397 #endif
398 
399 #ifdef __has_include
400 #if __has_include(<optional>) && __cplusplus >= 201703L && \
401  !ABSL_INTERNAL_MACOS_CXX17_TYPES_UNAVAILABLE
402 #define ABSL_HAVE_STD_OPTIONAL 1
403 #endif
404 #endif
405 
406 // ABSL_HAVE_STD_VARIANT
407 //
408 // Checks whether C++17 std::variant is available.
409 #ifdef ABSL_HAVE_STD_VARIANT
410 #error "ABSL_HAVE_STD_VARIANT cannot be directly set."
411 #endif
412 
413 #ifdef __has_include
414 #if __has_include(<variant>) && __cplusplus >= 201703L && \
415  !ABSL_INTERNAL_MACOS_CXX17_TYPES_UNAVAILABLE
416 #define ABSL_HAVE_STD_VARIANT 1
417 #endif
418 #endif
419 
420 // ABSL_HAVE_STD_STRING_VIEW
421 //
422 // Checks whether C++17 std::string_view is available.
423 #ifdef ABSL_HAVE_STD_STRING_VIEW
424 #error "ABSL_HAVE_STD_STRING_VIEW cannot be directly set."
425 #endif
426 
427 #ifdef __has_include
428 #if __has_include(<string_view>) && __cplusplus >= 201703L
429 #define ABSL_HAVE_STD_STRING_VIEW 1
430 #endif
431 #endif
432 
433 // For MSVC, `__has_include` is supported in VS 2017 15.3, which is later than
434 // the support for <optional>, <any>, <string_view>, <variant>. So we use
435 // _MSC_VER to check whether we have VS 2017 RTM (when <optional>, <any>,
436 // <string_view>, <variant> is implemented) or higher. Also, `__cplusplus` is
437 // not correctly set by MSVC, so we use `_MSVC_LANG` to check the language
438 // version.
439 // TODO(zhangxy): fix tests before enabling aliasing for `std::any`.
440 #if defined(_MSC_VER) && _MSC_VER >= 1910 && \
441  ((defined(_MSVC_LANG) && _MSVC_LANG > 201402) || __cplusplus > 201402)
442 // #define ABSL_HAVE_STD_ANY 1
443 #define ABSL_HAVE_STD_OPTIONAL 1
444 #define ABSL_HAVE_STD_VARIANT 1
445 #define ABSL_HAVE_STD_STRING_VIEW 1
446 #endif
447 
448 // In debug mode, MSVC 2017's std::variant throws a EXCEPTION_ACCESS_VIOLATION
449 // SEH exception from emplace for variant<SomeStruct> when constructing the
450 // struct can throw. This defeats some of variant_test and
451 // variant_exception_safety_test.
452 #if defined(_MSC_VER) && _MSC_VER >= 1700 && defined(_DEBUG)
453 #define ABSL_INTERNAL_MSVC_2017_DBG_MODE
454 #endif
455 
456 #endif // ABSL_BASE_CONFIG_H_


abseil_cpp
Author(s):
autogenerated on Tue Jun 18 2019 19:44:35