GRPC C++  1.0.0
port_platform.h
Go to the documentation of this file.
1 /*
2  *
3  * Copyright 2015, Google Inc.
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are
8  * met:
9  *
10  * * Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  * * Redistributions in binary form must reproduce the above
13  * copyright notice, this list of conditions and the following disclaimer
14  * in the documentation and/or other materials provided with the
15  * distribution.
16  * * Neither the name of Google Inc. nor the names of its
17  * contributors may be used to endorse or promote products derived from
18  * this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  *
32  */
33 
34 #ifndef GRPC_IMPL_CODEGEN_PORT_PLATFORM_H
35 #define GRPC_IMPL_CODEGEN_PORT_PLATFORM_H
36 
37 /*
38  * Define GPR_BACKWARDS_COMPATIBILITY_MODE to try harder to be ABI
39  * compatible with older platforms (currently only on Linux)
40  * Causes:
41  * - some libc calls to be gotten via dlsym
42  * - some syscalls to be made directly
43  */
44 
45 /* Get windows.h included everywhere (we need it) */
46 #if defined(_WIN64) || defined(WIN64) || defined(_WIN32) || defined(WIN32)
47 #ifndef WIN32_LEAN_AND_MEAN
48 #define GRPC_WIN32_LEAN_AND_MEAN_WAS_NOT_DEFINED
49 #define WIN32_LEAN_AND_MEAN
50 #endif /* WIN32_LEAN_AND_MEAN */
51 
52 #ifndef NOMINMAX
53 #define GRPC_NOMINMX_WAS_NOT_DEFINED
54 #define NOMINMAX
55 #endif /* NOMINMAX */
56 
57 #ifndef _WIN32_WINNT
58 #error \
59  "Please compile grpc with _WIN32_WINNT of at least 0x600 (aka Windows Vista)"
60 #else /* !defined(_WIN32_WINNT) */
61 #if (_WIN32_WINNT < 0x0600)
62 #error \
63  "Please compile grpc with _WIN32_WINNT of at least 0x600 (aka Windows Vista)"
64 #endif /* _WIN32_WINNT < 0x0600 */
65 #endif /* defined(_WIN32_WINNT) */
66 
67 #include <windows.h>
68 
69 #ifdef GRPC_WIN32_LEAN_AND_MEAN_WAS_NOT_DEFINED
70 #undef GRPC_WIN32_LEAN_AND_MEAN_WAS_NOT_DEFINED
71 #undef WIN32_LEAN_AND_MEAN
72 #endif /* GRPC_WIN32_LEAN_AND_MEAN_WAS_NOT_DEFINED */
73 
74 #ifdef GRPC_NOMINMAX_WAS_NOT_DEFINED
75 #undef GRPC_NOMINMAX_WAS_NOT_DEFINED
76 #undef NOMINMAX
77 #endif /* GRPC_WIN32_LEAN_AND_MEAN_WAS_NOT_DEFINED */
78 #endif /* defined(_WIN64) || defined(WIN64) || defined(_WIN32) || \
79  defined(WIN32) */
80 
81 /* Override this file with one for your platform if you need to redefine
82  things. */
83 
84 #if !defined(GPR_NO_AUTODETECT_PLATFORM)
85 #if defined(_WIN64) || defined(WIN64) || defined(_WIN32) || defined(WIN32)
86 #if defined(_WIN64) || defined(WIN64)
87 #define GPR_ARCH_64 1
88 #else
89 #define GPR_ARCH_32 1
90 #endif
91 #define GPR_PLATFORM_STRING "windows"
92 #define GPR_WINDOWS 1
93 #define GPR_WINSOCK_SOCKET 1
94 #define GPR_WINDOWS_SUBPROCESS 1
95 #define GPR_WINDOWS_ENV
96 #ifdef __MSYS__
97 #define GPR_GETPID_IN_UNISTD_H 1
98 #define GPR_MSYS_TMPFILE
99 #define GPR_POSIX_LOG
100 #define GPR_POSIX_STRING
101 #define GPR_POSIX_TIME
102 #else
103 #define GPR_GETPID_IN_PROCESS_H 1
104 #define GPR_WINDOWS_TMPFILE
105 #define GPR_WINDOWS_LOG
106 #define GPR_WINDOWS_CRASH_HANDLER 1
107 #define GPR_WINDOWS_STRING
108 #define GPR_WINDOWS_TIME
109 #endif
110 #ifdef __GNUC__
111 #define GPR_GCC_ATOMIC 1
112 #define GPR_GCC_TLS 1
113 #else
114 #define GPR_WINDOWS_ATOMIC 1
115 #define GPR_MSVC_TLS 1
116 #endif
117 #elif defined(GPR_MANYLINUX1)
118 // TODO(atash): manylinux1 is just another __linux__ but with ancient
119 // libraries; it should be integrated with the `__linux__` definitions below.
120 #define GPR_PLATFORM_STRING "manylinux"
121 #define GPR_POSIX_CRASH_HANDLER 1
122 #define GPR_CPU_POSIX 1
123 #define GPR_GCC_ATOMIC 1
124 #define GPR_GCC_TLS 1
125 #define GPR_LINUX 1
126 #define GPR_LINUX_LOG 1
127 #define GPR_POSIX_SOCKET 1
128 #define GPR_POSIX_WAKEUP_FD 1
129 #define GPR_POSIX_SOCKETADDR 1
130 #define GPR_POSIX_NO_SPECIAL_WAKEUP_FD 1
131 #define GPR_POSIX_SOCKETUTILS 1
132 #define GPR_SUPPORT_CHANNELS_FROM_FD 1
133 #define GPR_HAVE_UNIX_SOCKET 1
134 #define GPR_HAVE_IP_PKTINFO 1
135 #define GPR_HAVE_IPV6_RECVPKTINFO 1
136 #define GPR_LINUX_ENV 1
137 #define GPR_POSIX_FILE 1
138 #define GPR_POSIX_TMPFILE 1
139 #define GPR_POSIX_STRING 1
140 #define GPR_POSIX_SUBPROCESS 1
141 #define GPR_POSIX_SYNC 1
142 #define GPR_POSIX_TIME 1
143 #define GPR_GETPID_IN_UNISTD_H 1
144 #define GPR_HAVE_MSG_NOSIGNAL 1
145 #ifdef _LP64
146 #define GPR_ARCH_64 1
147 #else /* _LP64 */
148 #define GPR_ARCH_32 1
149 #endif /* _LP64 */
150 #elif defined(ANDROID) || defined(__ANDROID__)
151 #define GPR_PLATFORM_STRING "android"
152 #define GPR_ANDROID 1
153 #ifdef _LP64
154 #define GPR_ARCH_64 1
155 #else /* _LP64 */
156 #define GPR_ARCH_32 1
157 #endif /* _LP64 */
158 #define GPR_CPU_LINUX 1
159 #define GPR_GCC_SYNC 1
160 #define GPR_GCC_TLS 1
161 #define GPR_POSIX_MULTIPOLL_WITH_POLL 1
162 #define GPR_POSIX_WAKEUP_FD 1
163 #define GPR_LINUX_EVENTFD 1
164 #define GPR_POSIX_SOCKET 1
165 #define GPR_POSIX_SOCKETADDR 1
166 #define GPR_POSIX_SOCKETUTILS 1
167 #define GPR_POSIX_ENV 1
168 #define GPR_POSIX_FILE 1
169 #define GPR_POSIX_TMPFILE 1
170 #define GPR_POSIX_LOG
171 #define GPR_POSIX_STRING 1
172 #define GPR_POSIX_SUBPROCESS 1
173 #define GPR_POSIX_SYNC 1
174 #define GPR_POSIX_TIME 1
175 #define GPR_GETPID_IN_UNISTD_H 1
176 #define GPR_SUPPORT_CHANNELS_FROM_FD 1
177 #define GPR_HAVE_MSG_NOSIGNAL 1
178 #define GPR_HAVE_UNIX_SOCKET 1
179 #define GPR_HAVE_IP_PKTINFO 1
180 #define GPR_HAVE_IPV6_RECVPKTINFO 1
181 #elif defined(__linux__)
182 #define GPR_POSIX_CRASH_HANDLER 1
183 #define GPR_PLATFORM_STRING "linux"
184 #ifndef _BSD_SOURCE
185 #define _BSD_SOURCE
186 #endif
187 #ifndef _DEFAULT_SOURCE
188 #define _DEFAULT_SOURCE
189 #endif
190 #ifndef _GNU_SOURCE
191 #define _GNU_SOURCE
192 #endif
193 #include <features.h>
194 #define GPR_CPU_LINUX 1
195 #define GPR_GCC_ATOMIC 1
196 #define GPR_GCC_TLS 1
197 #define GPR_LINUX 1
198 #define GPR_LINUX_LOG
199 #define GPR_LINUX_MULTIPOLL_WITH_EPOLL 1
200 #define GPR_POSIX_WAKEUP_FD 1
201 #define GPR_POSIX_SOCKET 1
202 #define GPR_POSIX_SOCKETADDR 1
203 #define GPR_SUPPORT_CHANNELS_FROM_FD 1
204 #define GPR_HAVE_UNIX_SOCKET 1
205 #define GPR_HAVE_IP_PKTINFO 1
206 #define GPR_HAVE_IPV6_RECVPKTINFO 1
207 #ifdef __GLIBC_PREREQ
208 #if __GLIBC_PREREQ(2, 9)
209 #define GPR_LINUX_EVENTFD 1
210 #define GPR_LINUX_EPOLL 1
211 #endif
212 #if __GLIBC_PREREQ(2, 10)
213 #define GPR_LINUX_SOCKETUTILS 1
214 #endif
215 #endif
216 #define GPR_LINUX_ENV 1
217 #ifndef GPR_LINUX_EVENTFD
218 #define GPR_POSIX_NO_SPECIAL_WAKEUP_FD 1
219 #endif
220 #ifndef GPR_LINUX_SOCKETUTILS
221 #define GPR_POSIX_SOCKETUTILS
222 #endif
223 #define GPR_POSIX_FILE 1
224 #define GPR_POSIX_TMPFILE 1
225 #define GPR_POSIX_STRING 1
226 #define GPR_POSIX_SUBPROCESS 1
227 #define GPR_POSIX_SYNC 1
228 #define GPR_POSIX_TIME 1
229 #define GPR_GETPID_IN_UNISTD_H 1
230 #define GPR_HAVE_MSG_NOSIGNAL 1
231 #ifdef _LP64
232 #define GPR_ARCH_64 1
233 #else /* _LP64 */
234 #define GPR_ARCH_32 1
235 #endif /* _LP64 */
236 #elif defined(__APPLE__)
237 #include <TargetConditionals.h>
238 #ifndef _BSD_SOURCE
239 #define _BSD_SOURCE
240 #endif
241 #define GPR_MSG_IOVLEN_TYPE int
242 #if TARGET_OS_IPHONE
243 #define GPR_FORBID_UNREACHABLE_CODE 1
244 #define GPR_PLATFORM_STRING "ios"
245 #define GPR_CPU_IPHONE 1
246 #define GPR_PTHREAD_TLS 1
247 #else /* TARGET_OS_IPHONE */
248 #define GPR_PLATFORM_STRING "osx"
249 #define GPR_CPU_POSIX 1
250 #define GPR_GCC_TLS 1
251 #define GPR_POSIX_CRASH_HANDLER 1
252 #endif
253 #define GPR_GCC_ATOMIC 1
254 #define GPR_POSIX_LOG 1
255 #define GPR_POSIX_MULTIPOLL_WITH_POLL 1
256 #define GPR_POSIX_WAKEUP_FD 1
257 #define GPR_POSIX_NO_SPECIAL_WAKEUP_FD 1
258 #define GPR_POSIX_SOCKET 1
259 #define GPR_POSIX_SOCKETADDR 1
260 #define GPR_POSIX_SOCKETUTILS 1
261 #define GPR_POSIX_ENV 1
262 #define GPR_POSIX_FILE 1
263 #define GPR_POSIX_TMPFILE 1
264 #define GPR_POSIX_STRING 1
265 #define GPR_POSIX_SUBPROCESS 1
266 #define GPR_POSIX_SYNC 1
267 #define GPR_POSIX_TIME 1
268 #define GPR_GETPID_IN_UNISTD_H 1
269 #define GPR_SUPPORT_CHANNELS_FROM_FD 1
270 #define GPR_HAVE_SO_NOSIGPIPE 1
271 #define GPR_HAVE_UNIX_SOCKET 1
272 #define GPR_HAVE_IP_PKTINFO 1
273 #ifdef _LP64
274 #define GPR_ARCH_64 1
275 #else /* _LP64 */
276 #define GPR_ARCH_32 1
277 #endif /* _LP64 */
278 #elif defined(__FreeBSD__)
279 #define GPR_PLATFORM_STRING "freebsd"
280 #ifndef _BSD_SOURCE
281 #define _BSD_SOURCE
282 #endif
283 #define GPR_CPU_POSIX 1
284 #define GPR_GCC_ATOMIC 1
285 #define GPR_GCC_TLS 1
286 #define GPR_POSIX_LOG 1
287 #define GPR_POSIX_MULTIPOLL_WITH_POLL 1
288 #define GPR_POSIX_WAKEUP_FD 1
289 #define GPR_POSIX_NO_SPECIAL_WAKEUP_FD 1
290 #define GPR_POSIX_SOCKET 1
291 #define GPR_POSIX_SOCKETADDR 1
292 #define GPR_POSIX_SOCKETUTILS 1
293 #define GPR_POSIX_ENV 1
294 #define GPR_POSIX_FILE 1
295 #define GPR_POSIX_TMPFILE 1
296 #define GPR_POSIX_STRING 1
297 #define GPR_POSIX_SUBPROCESS 1
298 #define GPR_POSIX_SYNC 1
299 #define GPR_POSIX_TIME 1
300 #define GPR_GETPID_IN_UNISTD_H 1
301 #define GPR_SUPPORT_CHANNELS_FROM_FD 1
302 #define GPR_HAVE_SO_NOSIGPIPE 1
303 #define GPR_HAVE_UNIX_SOCKET 1
304 #define GPR_HAVE_IP_PKTINFO 1
305 #define GPR_HAVE_IPV6_RECVPKTINFO 1
306 #ifdef _LP64
307 #define GPR_ARCH_64 1
308 #else /* _LP64 */
309 #define GPR_ARCH_32 1
310 #endif /* _LP64 */
311 #elif defined(__native_client__)
312 #define GPR_PLATFORM_STRING "nacl"
313 #ifndef _BSD_SOURCE
314 #define _BSD_SOURCE
315 #endif
316 #ifndef _DEFAULT_SOURCE
317 #define _DEFAULT_SOURCE
318 #endif
319 #ifndef _GNU_SOURCE
320 #define _GNU_SOURCE
321 #endif
322 #define GPR_CPU_POSIX 1
323 #define GPR_GCC_ATOMIC 1
324 #define GPR_GCC_TLS 1
325 #define GPR_POSIX_LOG 1
326 #define GPR_POSIX_MULTIPOLL_WITH_POLL 1
327 #define GPR_POSIX_WAKEUP_FD 1
328 #define GPR_POSIX_NO_SPECIAL_WAKEUP_FD 1
329 #define GPR_POSIX_SOCKET 1
330 #define GPR_POSIX_SOCKETADDR 1
331 #define GPR_POSIX_SOCKETUTILS 1
332 #define GPR_POSIX_ENV 1
333 #define GPR_POSIX_FILE 1
334 #define GPR_POSIX_TMPFILE 1
335 #define GPR_POSIX_STRING 1
336 #define GPR_POSIX_SUBPROCESS 1
337 #define GPR_POSIX_SYNC 1
338 #define GPR_POSIX_TIME 1
339 #define GPR_GETPID_IN_UNISTD_H 1
340 #ifdef _LP64
341 #define GPR_ARCH_64 1
342 #else /* _LP64 */
343 #define GPR_ARCH_32 1
344 #endif /* _LP64 */
345 #else
346 #error "Could not auto-detect platform"
347 #endif
348 #endif /* GPR_NO_AUTODETECT_PLATFORM */
349 
350 #ifndef GPR_PLATFORM_STRING
351 #warning "GPR_PLATFORM_STRING not auto-detected"
352 #define GPR_PLATFORM_STRING "unknown"
353 #endif
354 
355 #ifdef GPR_GCOV
356 #undef GPR_FORBID_UNREACHABLE_CODE
357 #define GPR_FORBID_UNREACHABLE_CODE 1
358 #endif
359 
360 #ifdef _MSC_VER
361 #if _MSC_VER < 1700
362 typedef __int8 int8_t;
363 typedef __int16 int16_t;
364 typedef __int32 int32_t;
365 typedef __int64 int64_t;
366 typedef unsigned __int8 uint8_t;
367 typedef unsigned __int16 uint16_t;
368 typedef unsigned __int32 uint32_t;
369 typedef unsigned __int64 uint64_t;
370 #else
371 #include <stdint.h>
372 #endif /* _MSC_VER < 1700 */
373 #else
374 #include <stdint.h>
375 #endif /* _MSC_VER */
376 
377 /* Cache line alignment */
378 #ifndef GPR_CACHELINE_SIZE_LOG
379 #if defined(__i386__) || defined(__x86_64__)
380 #define GPR_CACHELINE_SIZE_LOG 6
381 #endif
382 #ifndef GPR_CACHELINE_SIZE_LOG
383 /* A reasonable default guess. Note that overestimates tend to waste more
384  space, while underestimates tend to waste more time. */
385 #define GPR_CACHELINE_SIZE_LOG 6
386 #endif /* GPR_CACHELINE_SIZE_LOG */
387 #endif /* GPR_CACHELINE_SIZE_LOG */
388 
389 #define GPR_CACHELINE_SIZE (1 << GPR_CACHELINE_SIZE_LOG)
390 
391 /* scrub GCC_ATOMIC if it's not available on this compiler */
392 #if defined(GPR_GCC_ATOMIC) && !defined(__ATOMIC_RELAXED)
393 #undef GPR_GCC_ATOMIC
394 #define GPR_GCC_SYNC 1
395 #endif
396 
397 /* Validate platform combinations */
398 #if defined(GPR_GCC_ATOMIC) + defined(GPR_GCC_SYNC) + \
399  defined(GPR_WINDOWS_ATOMIC) != \
400  1
401 #error Must define exactly one of GPR_GCC_ATOMIC, GPR_GCC_SYNC, GPR_WINDOWS_ATOMIC
402 #endif
403 
404 #if defined(GPR_ARCH_32) + defined(GPR_ARCH_64) != 1
405 #error Must define exactly one of GPR_ARCH_32, GPR_ARCH_64
406 #endif
407 
408 #if defined(GPR_CPU_LINUX) + defined(GPR_CPU_POSIX) + defined(GPR_WINDOWS) + \
409  defined(GPR_CPU_IPHONE) + defined(GPR_CPU_CUSTOM) != \
410  1
411 #error Must define exactly one of GPR_CPU_LINUX, GPR_CPU_POSIX, GPR_WINDOWS, GPR_CPU_IPHONE, GPR_CPU_CUSTOM
412 #endif
413 
414 #if defined(GPR_POSIX_MULTIPOLL_WITH_POLL) && !defined(GPR_POSIX_SOCKET)
415 #error Must define GPR_POSIX_SOCKET to use GPR_POSIX_MULTIPOLL_WITH_POLL
416 #endif
417 
418 #if defined(GPR_POSIX_SOCKET) + defined(GPR_WINSOCK_SOCKET) + \
419  defined(GPR_CUSTOM_SOCKET) != \
420  1
421 #error Must define exactly one of GPR_POSIX_SOCKET, GPR_WINSOCK_SOCKET, GPR_CUSTOM_SOCKET
422 #endif
423 
424 #if defined(GPR_MSVC_TLS) + defined(GPR_GCC_TLS) + defined(GPR_PTHREAD_TLS) + \
425  defined(GPR_CUSTOM_TLS) != \
426  1
427 #error Must define exactly one of GPR_MSVC_TLS, GPR_GCC_TLS, GPR_PTHREAD_TLS, GPR_CUSTOM_TLS
428 #endif
429 
430 /* maximum alignment needed for any type on this platform, rounded up to a
431  power of two */
432 #define GPR_MAX_ALIGNMENT 16
433 
434 #ifndef GRPC_MUST_USE_RESULT
435 #ifdef __GNUC__
436 #define GRPC_MUST_USE_RESULT __attribute__((warn_unused_result))
437 #else
438 #define GRPC_MUST_USE_RESULT
439 #endif
440 #endif
441 
442 #ifndef GPRC_PRINT_FORMAT_CHECK
443 #ifdef __GNUC__
444 #define GPRC_PRINT_FORMAT_CHECK(FORMAT_STR, ARGS) \
445  __attribute__((format(printf, FORMAT_STR, ARGS)))
446 #else
447 #define GPRC_PRINT_FORMAT_CHECK(FORMAT_STR, ARGS)
448 #endif
449 #endif /* GPRC_PRINT_FORMAT_CHECK */
450 
451 #if GPR_FORBID_UNREACHABLE_CODE
452 #define GPR_UNREACHABLE_CODE(STATEMENT)
453 #else
454 #define GPR_UNREACHABLE_CODE(STATEMENT) \
455  do { \
456  gpr_log(GPR_ERROR, "Should never reach here."); \
457  abort(); \
458  STATEMENT; \
459  } while (0)
460 #endif /* GPR_FORBID_UNREACHABLE_CODE */
461 
462 #ifndef GPRAPI
463 #define GPRAPI
464 #endif
465 
466 #ifndef GRPCAPI
467 #define GRPCAPI GPRAPI
468 #endif
469 
470 #ifndef CENSUSAPI
471 #define CENSUSAPI GRPCAPI
472 #endif
473 
474 #endif /* GRPC_IMPL_CODEGEN_PORT_PLATFORM_H */