GRPC C++  1.8.0
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
port_platform.h
Go to the documentation of this file.
1 /*
2  *
3  * Copyright 2015 gRPC authors.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */
18 
19 #ifndef GRPC_IMPL_CODEGEN_PORT_PLATFORM_H
20 #define GRPC_IMPL_CODEGEN_PORT_PLATFORM_H
21 
22 /*
23  * Define GPR_BACKWARDS_COMPATIBILITY_MODE to try harder to be ABI
24  * compatible with older platforms (currently only on Linux)
25  * Causes:
26  * - some libc calls to be gotten via dlsym
27  * - some syscalls to be made directly
28  */
29 
30 /* Get windows.h included everywhere (we need it) */
31 #if defined(_WIN64) || defined(WIN64) || defined(_WIN32) || defined(WIN32)
32 #ifndef WIN32_LEAN_AND_MEAN
33 #define GRPC_WIN32_LEAN_AND_MEAN_WAS_NOT_DEFINED
34 #define WIN32_LEAN_AND_MEAN
35 #endif /* WIN32_LEAN_AND_MEAN */
36 
37 #ifndef NOMINMAX
38 #define GRPC_NOMINMX_WAS_NOT_DEFINED
39 #define NOMINMAX
40 #endif /* NOMINMAX */
41 
42 #ifndef _WIN32_WINNT
43 #error \
44  "Please compile grpc with _WIN32_WINNT of at least 0x600 (aka Windows Vista)"
45 #else /* !defined(_WIN32_WINNT) */
46 #if (_WIN32_WINNT < 0x0600)
47 #error \
48  "Please compile grpc with _WIN32_WINNT of at least 0x600 (aka Windows Vista)"
49 #endif /* _WIN32_WINNT < 0x0600 */
50 #endif /* defined(_WIN32_WINNT) */
51 
52 #include <windows.h>
53 
54 #ifdef GRPC_WIN32_LEAN_AND_MEAN_WAS_NOT_DEFINED
55 #undef GRPC_WIN32_LEAN_AND_MEAN_WAS_NOT_DEFINED
56 #undef WIN32_LEAN_AND_MEAN
57 #endif /* GRPC_WIN32_LEAN_AND_MEAN_WAS_NOT_DEFINED */
58 
59 #ifdef GRPC_NOMINMAX_WAS_NOT_DEFINED
60 #undef GRPC_NOMINMAX_WAS_NOT_DEFINED
61 #undef NOMINMAX
62 #endif /* GRPC_WIN32_LEAN_AND_MEAN_WAS_NOT_DEFINED */
63 #endif /* defined(_WIN64) || defined(WIN64) || defined(_WIN32) || \
64  defined(WIN32) */
65 
66 /* Override this file with one for your platform if you need to redefine
67  things. */
68 
69 #if !defined(GPR_NO_AUTODETECT_PLATFORM)
70 #if defined(_WIN64) || defined(WIN64) || defined(_WIN32) || defined(WIN32)
71 #if defined(_WIN64) || defined(WIN64)
72 #define GPR_ARCH_64 1
73 #else
74 #define GPR_ARCH_32 1
75 #endif
76 #define GPR_PLATFORM_STRING "windows"
77 #define GPR_WINDOWS 1
78 #define GPR_WINDOWS_SUBPROCESS 1
79 #define GPR_WINDOWS_ENV
80 #ifdef __MSYS__
81 #define GPR_GETPID_IN_UNISTD_H 1
82 #define GPR_MSYS_TMPFILE
83 #define GPR_POSIX_LOG
84 #define GPR_POSIX_STRING
85 #define GPR_POSIX_TIME
86 #else
87 #define GPR_GETPID_IN_PROCESS_H 1
88 #define GPR_WINDOWS_TMPFILE
89 #define GPR_WINDOWS_LOG
90 #define GPR_WINDOWS_CRASH_HANDLER 1
91 #define GPR_WINDOWS_STRING
92 #define GPR_WINDOWS_TIME
93 #endif
94 #ifdef __GNUC__
95 #define GPR_GCC_ATOMIC 1
96 #define GPR_GCC_TLS 1
97 #else
98 #define GPR_WINDOWS_ATOMIC 1
99 #define GPR_MSVC_TLS 1
100 #endif
101 #elif defined(GPR_MANYLINUX1)
102 // TODO(atash): manylinux1 is just another __linux__ but with ancient
103 // libraries; it should be integrated with the `__linux__` definitions below.
104 #define GPR_PLATFORM_STRING "manylinux"
105 #define GPR_POSIX_CRASH_HANDLER 1
106 #define GPR_CPU_POSIX 1
107 #define GPR_GCC_ATOMIC 1
108 #define GPR_GCC_TLS 1
109 #define GPR_LINUX 1
110 #define GPR_LINUX_LOG 1
111 #define GPR_SUPPORT_CHANNELS_FROM_FD 1
112 #define GPR_LINUX_ENV 1
113 #define GPR_POSIX_TMPFILE 1
114 #define GPR_POSIX_STRING 1
115 #define GPR_POSIX_SUBPROCESS 1
116 #define GPR_POSIX_SYNC 1
117 #define GPR_POSIX_TIME 1
118 #define GPR_GETPID_IN_UNISTD_H 1
119 #ifdef _LP64
120 #define GPR_ARCH_64 1
121 #else /* _LP64 */
122 #define GPR_ARCH_32 1
123 #endif /* _LP64 */
124 #elif defined(ANDROID) || defined(__ANDROID__)
125 #define GPR_PLATFORM_STRING "android"
126 #define GPR_ANDROID 1
127 #ifdef _LP64
128 #define GPR_ARCH_64 1
129 #else /* _LP64 */
130 #define GPR_ARCH_32 1
131 #endif /* _LP64 */
132 #define GPR_CPU_POSIX 1
133 #define GPR_GCC_SYNC 1
134 #define GPR_GCC_TLS 1
135 #define GPR_POSIX_ENV 1
136 #define GPR_POSIX_TMPFILE 1
137 #define GPR_ANDROID_LOG 1
138 #define GPR_POSIX_STRING 1
139 #define GPR_POSIX_SUBPROCESS 1
140 #define GPR_POSIX_SYNC 1
141 #define GPR_POSIX_TIME 1
142 #define GPR_GETPID_IN_UNISTD_H 1
143 #define GPR_SUPPORT_CHANNELS_FROM_FD 1
144 #elif defined(__linux__)
145 #define GPR_PLATFORM_STRING "linux"
146 #ifndef _BSD_SOURCE
147 #define _BSD_SOURCE
148 #endif
149 #ifndef _DEFAULT_SOURCE
150 #define _DEFAULT_SOURCE
151 #endif
152 #ifndef _GNU_SOURCE
153 #define _GNU_SOURCE
154 #endif
155 #include <features.h>
156 #define GPR_CPU_LINUX 1
157 #define GPR_GCC_ATOMIC 1
158 #define GPR_GCC_TLS 1
159 #define GPR_LINUX 1
160 #define GPR_LINUX_LOG
161 #define GPR_SUPPORT_CHANNELS_FROM_FD 1
162 #define GPR_LINUX_ENV 1
163 #define GPR_POSIX_TMPFILE 1
164 #define GPR_POSIX_STRING 1
165 #define GPR_POSIX_SUBPROCESS 1
166 #define GPR_POSIX_SYNC 1
167 #define GPR_POSIX_TIME 1
168 #define GPR_GETPID_IN_UNISTD_H 1
169 #ifdef _LP64
170 #define GPR_ARCH_64 1
171 #else /* _LP64 */
172 #define GPR_ARCH_32 1
173 #endif /* _LP64 */
174 #ifdef __GLIBC__
175 #define GPR_POSIX_CRASH_HANDLER 1
176 #else /* musl libc */
177 #define GPR_MUSL_LIBC_COMPAT 1
178 #endif
179 #elif defined(__APPLE__)
180 #include <Availability.h>
181 #include <TargetConditionals.h>
182 #ifndef _BSD_SOURCE
183 #define _BSD_SOURCE
184 #endif
185 #if TARGET_OS_IPHONE
186 #define GPR_PLATFORM_STRING "ios"
187 #define GPR_CPU_IPHONE 1
188 #define GPR_PTHREAD_TLS 1
189 #else /* TARGET_OS_IPHONE */
190 #define GPR_PLATFORM_STRING "osx"
191 #ifdef __MAC_OS_X_VERSION_MIN_REQUIRED
192 #if __MAC_OS_X_VERSION_MIN_REQUIRED < __MAC_10_7
193 #define GPR_CPU_IPHONE 1
194 #define GPR_PTHREAD_TLS 1
195 #else /* __MAC_OS_X_VERSION_MIN_REQUIRED < __MAC_10_7 */
196 #define GPR_CPU_POSIX 1
197 #define GPR_GCC_TLS 1
198 #endif
199 #else /* __MAC_OS_X_VERSION_MIN_REQUIRED */
200 #define GPR_CPU_POSIX 1
201 #define GPR_GCC_TLS 1
202 #endif
203 #define GPR_POSIX_CRASH_HANDLER 1
204 #endif
205 #define GPR_APPLE 1
206 #define GPR_GCC_ATOMIC 1
207 #define GPR_POSIX_LOG 1
208 #define GPR_POSIX_ENV 1
209 #define GPR_POSIX_TMPFILE 1
210 #define GPR_POSIX_STRING 1
211 #define GPR_POSIX_SUBPROCESS 1
212 #define GPR_POSIX_SYNC 1
213 #define GPR_POSIX_TIME 1
214 #define GPR_GETPID_IN_UNISTD_H 1
215 #define GPR_SUPPORT_CHANNELS_FROM_FD 1
216 #ifdef _LP64
217 #define GPR_ARCH_64 1
218 #else /* _LP64 */
219 #define GPR_ARCH_32 1
220 #endif /* _LP64 */
221 #elif defined(__FreeBSD__)
222 #define GPR_PLATFORM_STRING "freebsd"
223 #ifndef _BSD_SOURCE
224 #define _BSD_SOURCE
225 #endif
226 #define GPR_FREEBSD 1
227 #define GPR_CPU_POSIX 1
228 #define GPR_GCC_ATOMIC 1
229 #define GPR_GCC_TLS 1
230 #define GPR_POSIX_LOG 1
231 #define GPR_POSIX_ENV 1
232 #define GPR_POSIX_TMPFILE 1
233 #define GPR_POSIX_STRING 1
234 #define GPR_POSIX_SUBPROCESS 1
235 #define GPR_POSIX_SYNC 1
236 #define GPR_POSIX_TIME 1
237 #define GPR_GETPID_IN_UNISTD_H 1
238 #define GPR_SUPPORT_CHANNELS_FROM_FD 1
239 #ifdef _LP64
240 #define GPR_ARCH_64 1
241 #else /* _LP64 */
242 #define GPR_ARCH_32 1
243 #endif /* _LP64 */
244 #elif defined(__OpenBSD__)
245 #define GPR_PLATFORM_STRING "openbsd"
246 #ifndef _BSD_SOURCE
247 #define _BSD_SOURCE
248 #endif
249 #define GPR_OPENBSD 1
250 #define GPR_CPU_POSIX 1
251 #define GPR_GCC_ATOMIC 1
252 #define GPR_GCC_TLS 1
253 #define GPR_POSIX_LOG 1
254 #define GPR_POSIX_ENV 1
255 #define GPR_POSIX_TMPFILE 1
256 #define GPR_POSIX_STRING 1
257 #define GPR_POSIX_SUBPROCESS 1
258 #define GPR_POSIX_SYNC 1
259 #define GPR_POSIX_TIME 1
260 #define GPR_GETPID_IN_UNISTD_H 1
261 #define GPR_SUPPORT_CHANNELS_FROM_FD 1
262 #ifdef _LP64
263 #define GPR_ARCH_64 1
264 #else /* _LP64 */
265 #define GPR_ARCH_32 1
266 #endif /* _LP64 */
267 #elif defined(__native_client__)
268 #define GPR_PLATFORM_STRING "nacl"
269 #ifndef _BSD_SOURCE
270 #define _BSD_SOURCE
271 #endif
272 #ifndef _DEFAULT_SOURCE
273 #define _DEFAULT_SOURCE
274 #endif
275 #ifndef _GNU_SOURCE
276 #define _GNU_SOURCE
277 #endif
278 #define GPR_NACL 1
279 #define GPR_CPU_POSIX 1
280 #define GPR_GCC_ATOMIC 1
281 #define GPR_GCC_TLS 1
282 #define GPR_POSIX_LOG 1
283 #define GPR_POSIX_ENV 1
284 #define GPR_POSIX_TMPFILE 1
285 #define GPR_POSIX_STRING 1
286 #define GPR_POSIX_SUBPROCESS 1
287 #define GPR_POSIX_SYNC 1
288 #define GPR_POSIX_TIME 1
289 #define GPR_GETPID_IN_UNISTD_H 1
290 #ifdef _LP64
291 #define GPR_ARCH_64 1
292 #else /* _LP64 */
293 #define GPR_ARCH_32 1
294 #endif /* _LP64 */
295 #else
296 #error "Could not auto-detect platform"
297 #endif
298 #endif /* GPR_NO_AUTODETECT_PLATFORM */
299 
300 /*
301  * There are platforms for which TLS should not be used even though the
302  * compiler makes it seem like it's supported (Android NDK < r12b for example).
303  * This is primarily because of linker problems and toolchain misconfiguration:
304  * TLS isn't supported until NDK r12b per
305  * https://developer.android.com/ndk/downloads/revision_history.html
306  * Since NDK r16, `__NDK_MAJOR__` and `__NDK_MINOR__` are defined in
307  * <android/ndk-version.h>. For NDK < r16, users should define these macros,
308  * e.g. `-D__NDK_MAJOR__=11 -D__NKD_MINOR__=0` for NDK r11. */
309 #if defined(__ANDROID__) && defined(__clang__) && defined(GPR_GCC_TLS)
310 #if __has_include(<android/ndk-version.h>)
311 #include <android/ndk-version.h>
312 #endif /* __has_include(<android/ndk-version.h>) */
313 #if defined(__ANDROID__) && defined(__clang__) && defined(__NDK_MAJOR__) && \
314  defined(__NDK_MINOR__) && \
315  ((__NDK_MAJOR__ < 12) || ((__NDK_MAJOR__ == 12) && (__NDK_MINOR__ < 1)))
316 #undef GPR_GCC_TLS
317 #define GPR_PTHREAD_TLS 1
318 #endif
319 #endif /*defined(__ANDROID__) && defined(__clang__) && defined(GPR_GCC_TLS) */
320 
321 #if defined(__has_include)
322 #if __has_include(<atomic>)
323 #define GRPC_HAS_CXX11_ATOMIC
324 #endif /* __has_include(<atomic>) */
325 #endif /* defined(__has_include) */
326 
327 #ifndef GPR_PLATFORM_STRING
328 #warning "GPR_PLATFORM_STRING not auto-detected"
329 #define GPR_PLATFORM_STRING "unknown"
330 #endif
331 
332 #ifdef GPR_GCOV
333 #undef GPR_FORBID_UNREACHABLE_CODE
334 #define GPR_FORBID_UNREACHABLE_CODE 1
335 #endif
336 
337 #ifdef _MSC_VER
338 #if _MSC_VER < 1700
339 typedef __int8 int8_t;
340 typedef __int16 int16_t;
341 typedef __int32 int32_t;
342 typedef __int64 int64_t;
343 typedef unsigned __int8 uint8_t;
344 typedef unsigned __int16 uint16_t;
345 typedef unsigned __int32 uint32_t;
346 typedef unsigned __int64 uint64_t;
347 #else
348 #include <stdint.h>
349 #endif /* _MSC_VER < 1700 */
350 #else
351 #include <stdint.h>
352 #endif /* _MSC_VER */
353 
354 /* Cache line alignment */
355 #ifndef GPR_CACHELINE_SIZE_LOG
356 #if defined(__i386__) || defined(__x86_64__)
357 #define GPR_CACHELINE_SIZE_LOG 6
358 #endif
359 #ifndef GPR_CACHELINE_SIZE_LOG
360 /* A reasonable default guess. Note that overestimates tend to waste more
361  space, while underestimates tend to waste more time. */
362 #define GPR_CACHELINE_SIZE_LOG 6
363 #endif /* GPR_CACHELINE_SIZE_LOG */
364 #endif /* GPR_CACHELINE_SIZE_LOG */
365 
366 #define GPR_CACHELINE_SIZE (1 << GPR_CACHELINE_SIZE_LOG)
367 
368 /* scrub GCC_ATOMIC if it's not available on this compiler */
369 #if defined(GPR_GCC_ATOMIC) && !defined(__ATOMIC_RELAXED)
370 #undef GPR_GCC_ATOMIC
371 #define GPR_GCC_SYNC 1
372 #endif
373 
374 /* Validate platform combinations */
375 #if defined(GPR_GCC_ATOMIC) + defined(GPR_GCC_SYNC) + \
376  defined(GPR_WINDOWS_ATOMIC) != \
377  1
378 #error Must define exactly one of GPR_GCC_ATOMIC, GPR_GCC_SYNC, GPR_WINDOWS_ATOMIC
379 #endif
380 
381 #if defined(GPR_ARCH_32) + defined(GPR_ARCH_64) != 1
382 #error Must define exactly one of GPR_ARCH_32, GPR_ARCH_64
383 #endif
384 
385 #if defined(GPR_CPU_LINUX) + defined(GPR_CPU_POSIX) + defined(GPR_WINDOWS) + \
386  defined(GPR_CPU_IPHONE) + defined(GPR_CPU_CUSTOM) != \
387  1
388 #error Must define exactly one of GPR_CPU_LINUX, GPR_CPU_POSIX, GPR_WINDOWS, GPR_CPU_IPHONE, GPR_CPU_CUSTOM
389 #endif
390 
391 #if defined(GPR_MSVC_TLS) + defined(GPR_GCC_TLS) + defined(GPR_PTHREAD_TLS) + \
392  defined(GPR_CUSTOM_TLS) != \
393  1
394 #error Must define exactly one of GPR_MSVC_TLS, GPR_GCC_TLS, GPR_PTHREAD_TLS, GPR_CUSTOM_TLS
395 #endif
396 
397 /* maximum alignment needed for any type on this platform, rounded up to a
398  power of two */
399 #define GPR_MAX_ALIGNMENT 16
400 
401 #ifndef GRPC_ARES
402 #ifdef GPR_WINDOWS
403 #define GRPC_ARES 0
404 #else
405 #define GRPC_ARES 1
406 #endif
407 #endif
408 
409 #ifndef GRPC_MUST_USE_RESULT
410 #if defined(__GNUC__) && !defined(__MINGW32__)
411 #define GRPC_MUST_USE_RESULT __attribute__((warn_unused_result))
412 #define GPR_ALIGN_STRUCT(n) __attribute__((aligned(n)))
413 #else
414 #define GRPC_MUST_USE_RESULT
415 #define GPR_ALIGN_STRUCT(n)
416 #endif
417 #endif
418 
419 #ifndef GPR_PRINT_FORMAT_CHECK
420 #ifdef __GNUC__
421 #define GPR_PRINT_FORMAT_CHECK(FORMAT_STR, ARGS) \
422  __attribute__((format(printf, FORMAT_STR, ARGS)))
423 #else
424 #define GPR_PRINT_FORMAT_CHECK(FORMAT_STR, ARGS)
425 #endif
426 #endif /* GPR_PRINT_FORMAT_CHECK */
427 
428 #if GPR_FORBID_UNREACHABLE_CODE
429 #define GPR_UNREACHABLE_CODE(STATEMENT)
430 #else
431 #define GPR_UNREACHABLE_CODE(STATEMENT) \
432  do { \
433  gpr_log(GPR_ERROR, "Should never reach here."); \
434  abort(); \
435  STATEMENT; \
436  } while (0)
437 #endif /* GPR_FORBID_UNREACHABLE_CODE */
438 
439 #ifndef GPRAPI
440 #define GPRAPI
441 #endif
442 
443 #ifndef GRPCAPI
444 #define GRPCAPI GPRAPI
445 #endif
446 
447 #ifndef CENSUSAPI
448 #define CENSUSAPI GRPCAPI
449 #endif
450 
451 #ifndef GPR_ATTRIBUTE_NO_TSAN /* (1) */
452 #if defined(__has_feature)
453 #if __has_feature(thread_sanitizer)
454 #define GPR_ATTRIBUTE_NO_TSAN __attribute__((no_sanitize("thread")))
455 #endif /* __has_feature(thread_sanitizer) */
456 #endif /* defined(__has_feature) */
457 #ifndef GPR_ATTRIBUTE_NO_TSAN /* (2) */
458 #define GPR_ATTRIBUTE_NO_TSAN
459 #endif /* GPR_ATTRIBUTE_NO_TSAN (2) */
460 #endif /* GPR_ATTRIBUTE_NO_TSAN (1) */
461 
462 #ifndef __STDC_FORMAT_MACROS
463 #define __STDC_FORMAT_MACROS
464 #endif
465 
466 #endif /* GRPC_IMPL_CODEGEN_PORT_PLATFORM_H */