GRPC C++  1.3.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, 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_WINDOWS_SUBPROCESS 1
94 #define GPR_WINDOWS_ENV
95 #ifdef __MSYS__
96 #define GPR_GETPID_IN_UNISTD_H 1
97 #define GPR_MSYS_TMPFILE
98 #define GPR_POSIX_LOG
99 #define GPR_POSIX_STRING
100 #define GPR_POSIX_TIME
101 #else
102 #define GPR_GETPID_IN_PROCESS_H 1
103 #define GPR_WINDOWS_TMPFILE
104 #define GPR_WINDOWS_LOG
105 #define GPR_WINDOWS_CRASH_HANDLER 1
106 #define GPR_WINDOWS_STRING
107 #define GPR_WINDOWS_TIME
108 #endif
109 #ifdef __GNUC__
110 #define GPR_GCC_ATOMIC 1
111 #define GPR_GCC_TLS 1
112 #else
113 #define GPR_WINDOWS_ATOMIC 1
114 #define GPR_MSVC_TLS 1
115 #endif
116 #elif defined(GPR_MANYLINUX1)
117 // TODO(atash): manylinux1 is just another __linux__ but with ancient
118 // libraries; it should be integrated with the `__linux__` definitions below.
119 #define GPR_PLATFORM_STRING "manylinux"
120 #define GPR_POSIX_CRASH_HANDLER 1
121 #define GPR_CPU_POSIX 1
122 #define GPR_GCC_ATOMIC 1
123 #define GPR_GCC_TLS 1
124 #define GPR_LINUX 1
125 #define GPR_LINUX_LOG 1
126 #define GPR_SUPPORT_CHANNELS_FROM_FD 1
127 #define GPR_LINUX_ENV 1
128 #define GPR_POSIX_TMPFILE 1
129 #define GPR_POSIX_STRING 1
130 #define GPR_POSIX_SUBPROCESS 1
131 #define GPR_POSIX_SYNC 1
132 #define GPR_POSIX_TIME 1
133 #define GPR_GETPID_IN_UNISTD_H 1
134 #ifdef _LP64
135 #define GPR_ARCH_64 1
136 #else /* _LP64 */
137 #define GPR_ARCH_32 1
138 #endif /* _LP64 */
139 #elif defined(ANDROID) || defined(__ANDROID__)
140 #define GPR_PLATFORM_STRING "android"
141 #define GPR_ANDROID 1
142 #ifdef _LP64
143 #define GPR_ARCH_64 1
144 #else /* _LP64 */
145 #define GPR_ARCH_32 1
146 #endif /* _LP64 */
147 #define GPR_CPU_POSIX 1
148 #define GPR_GCC_SYNC 1
149 #define GPR_GCC_TLS 1
150 #define GPR_POSIX_ENV 1
151 #define GPR_POSIX_TMPFILE 1
152 #define GPR_ANDROID_LOG 1
153 #define GPR_POSIX_STRING 1
154 #define GPR_POSIX_SUBPROCESS 1
155 #define GPR_POSIX_SYNC 1
156 #define GPR_POSIX_TIME 1
157 #define GPR_GETPID_IN_UNISTD_H 1
158 #define GPR_SUPPORT_CHANNELS_FROM_FD 1
159 #elif defined(__linux__)
160 #define GPR_PLATFORM_STRING "linux"
161 #ifndef _BSD_SOURCE
162 #define _BSD_SOURCE
163 #endif
164 #ifndef _DEFAULT_SOURCE
165 #define _DEFAULT_SOURCE
166 #endif
167 #ifndef _GNU_SOURCE
168 #define _GNU_SOURCE
169 #endif
170 #include <features.h>
171 #define GPR_CPU_LINUX 1
172 #define GPR_GCC_ATOMIC 1
173 #define GPR_GCC_TLS 1
174 #define GPR_LINUX 1
175 #define GPR_LINUX_LOG
176 #define GPR_SUPPORT_CHANNELS_FROM_FD 1
177 #define GPR_LINUX_ENV 1
178 #define GPR_POSIX_TMPFILE 1
179 #define GPR_POSIX_STRING 1
180 #define GPR_POSIX_SUBPROCESS 1
181 #define GPR_POSIX_SYNC 1
182 #define GPR_POSIX_TIME 1
183 #define GPR_GETPID_IN_UNISTD_H 1
184 #ifdef _LP64
185 #define GPR_ARCH_64 1
186 #else /* _LP64 */
187 #define GPR_ARCH_32 1
188 #endif /* _LP64 */
189 #ifdef __GLIBC__
190 #define GPR_POSIX_CRASH_HANDLER 1
191 #else /* musl libc */
192 #define GPR_MUSL_LIBC_COMPAT 1
193 #endif
194 #elif defined(__APPLE__)
195 #include <Availability.h>
196 #include <TargetConditionals.h>
197 #ifndef _BSD_SOURCE
198 #define _BSD_SOURCE
199 #endif
200 #if TARGET_OS_IPHONE
201 #define GPR_FORBID_UNREACHABLE_CODE 1
202 #define GPR_PLATFORM_STRING "ios"
203 #define GPR_CPU_IPHONE 1
204 #define GPR_PTHREAD_TLS 1
205 #else /* TARGET_OS_IPHONE */
206 #define GPR_PLATFORM_STRING "osx"
207 #ifdef __MAC_OS_X_VERSION_MIN_REQUIRED
208 #if __MAC_OS_X_VERSION_MIN_REQUIRED < __MAC_10_7
209 #define GPR_CPU_IPHONE 1
210 #define GPR_PTHREAD_TLS 1
211 #else /* __MAC_OS_X_VERSION_MIN_REQUIRED < __MAC_10_7 */
212 #define GPR_CPU_POSIX 1
213 #define GPR_GCC_TLS 1
214 #endif
215 #else /* __MAC_OS_X_VERSION_MIN_REQUIRED */
216 #define GPR_CPU_POSIX 1
217 #define GPR_GCC_TLS 1
218 #endif
219 #define GPR_POSIX_CRASH_HANDLER 1
220 #endif
221 #define GPR_APPLE 1
222 #define GPR_GCC_ATOMIC 1
223 #define GPR_POSIX_LOG 1
224 #define GPR_POSIX_ENV 1
225 #define GPR_POSIX_TMPFILE 1
226 #define GPR_POSIX_STRING 1
227 #define GPR_POSIX_SUBPROCESS 1
228 #define GPR_POSIX_SYNC 1
229 #define GPR_POSIX_TIME 1
230 #define GPR_GETPID_IN_UNISTD_H 1
231 #define GPR_SUPPORT_CHANNELS_FROM_FD 1
232 #ifdef _LP64
233 #define GPR_ARCH_64 1
234 #else /* _LP64 */
235 #define GPR_ARCH_32 1
236 #endif /* _LP64 */
237 #elif defined(__FreeBSD__)
238 #define GPR_PLATFORM_STRING "freebsd"
239 #ifndef _BSD_SOURCE
240 #define _BSD_SOURCE
241 #endif
242 #define GPR_FREEBSD 1
243 #define GPR_CPU_POSIX 1
244 #define GPR_GCC_ATOMIC 1
245 #define GPR_GCC_TLS 1
246 #define GPR_POSIX_LOG 1
247 #define GPR_POSIX_ENV 1
248 #define GPR_POSIX_TMPFILE 1
249 #define GPR_POSIX_STRING 1
250 #define GPR_POSIX_SUBPROCESS 1
251 #define GPR_POSIX_SYNC 1
252 #define GPR_POSIX_TIME 1
253 #define GPR_GETPID_IN_UNISTD_H 1
254 #define GPR_SUPPORT_CHANNELS_FROM_FD 1
255 #ifdef _LP64
256 #define GPR_ARCH_64 1
257 #else /* _LP64 */
258 #define GPR_ARCH_32 1
259 #endif /* _LP64 */
260 #elif defined(__native_client__)
261 #define GPR_PLATFORM_STRING "nacl"
262 #ifndef _BSD_SOURCE
263 #define _BSD_SOURCE
264 #endif
265 #ifndef _DEFAULT_SOURCE
266 #define _DEFAULT_SOURCE
267 #endif
268 #ifndef _GNU_SOURCE
269 #define _GNU_SOURCE
270 #endif
271 #define GPR_NACL 1
272 #define GPR_CPU_POSIX 1
273 #define GPR_GCC_ATOMIC 1
274 #define GPR_GCC_TLS 1
275 #define GPR_POSIX_LOG 1
276 #define GPR_POSIX_ENV 1
277 #define GPR_POSIX_TMPFILE 1
278 #define GPR_POSIX_STRING 1
279 #define GPR_POSIX_SUBPROCESS 1
280 #define GPR_POSIX_SYNC 1
281 #define GPR_POSIX_TIME 1
282 #define GPR_GETPID_IN_UNISTD_H 1
283 #ifdef _LP64
284 #define GPR_ARCH_64 1
285 #else /* _LP64 */
286 #define GPR_ARCH_32 1
287 #endif /* _LP64 */
288 #else
289 #error "Could not auto-detect platform"
290 #endif
291 #endif /* GPR_NO_AUTODETECT_PLATFORM */
292 
293 #ifndef GPR_PLATFORM_STRING
294 #warning "GPR_PLATFORM_STRING not auto-detected"
295 #define GPR_PLATFORM_STRING "unknown"
296 #endif
297 
298 #ifdef GPR_GCOV
299 #undef GPR_FORBID_UNREACHABLE_CODE
300 #define GPR_FORBID_UNREACHABLE_CODE 1
301 #endif
302 
303 #ifdef _MSC_VER
304 #ifdef _PYTHON_MSVC
305 // The Python 3.5 Windows runtime is missing InetNtop
306 #define GPR_WIN_INET_NTOP
307 #endif // _PYTHON_MSVC
308 #if _MSC_VER < 1700
309 typedef __int8 int8_t;
310 typedef __int16 int16_t;
311 typedef __int32 int32_t;
312 typedef __int64 int64_t;
313 typedef unsigned __int8 uint8_t;
314 typedef unsigned __int16 uint16_t;
315 typedef unsigned __int32 uint32_t;
316 typedef unsigned __int64 uint64_t;
317 #else
318 #include <stdint.h>
319 #endif /* _MSC_VER < 1700 */
320 #else
321 #include <stdint.h>
322 #endif /* _MSC_VER */
323 
324 /* Cache line alignment */
325 #ifndef GPR_CACHELINE_SIZE_LOG
326 #if defined(__i386__) || defined(__x86_64__)
327 #define GPR_CACHELINE_SIZE_LOG 6
328 #endif
329 #ifndef GPR_CACHELINE_SIZE_LOG
330 /* A reasonable default guess. Note that overestimates tend to waste more
331  space, while underestimates tend to waste more time. */
332 #define GPR_CACHELINE_SIZE_LOG 6
333 #endif /* GPR_CACHELINE_SIZE_LOG */
334 #endif /* GPR_CACHELINE_SIZE_LOG */
335 
336 #define GPR_CACHELINE_SIZE (1 << GPR_CACHELINE_SIZE_LOG)
337 
338 /* scrub GCC_ATOMIC if it's not available on this compiler */
339 #if defined(GPR_GCC_ATOMIC) && !defined(__ATOMIC_RELAXED)
340 #undef GPR_GCC_ATOMIC
341 #define GPR_GCC_SYNC 1
342 #endif
343 
344 /* Validate platform combinations */
345 #if defined(GPR_GCC_ATOMIC) + defined(GPR_GCC_SYNC) + \
346  defined(GPR_WINDOWS_ATOMIC) != \
347  1
348 #error Must define exactly one of GPR_GCC_ATOMIC, GPR_GCC_SYNC, GPR_WINDOWS_ATOMIC
349 #endif
350 
351 #if defined(GPR_ARCH_32) + defined(GPR_ARCH_64) != 1
352 #error Must define exactly one of GPR_ARCH_32, GPR_ARCH_64
353 #endif
354 
355 #if defined(GPR_CPU_LINUX) + defined(GPR_CPU_POSIX) + defined(GPR_WINDOWS) + \
356  defined(GPR_CPU_IPHONE) + defined(GPR_CPU_CUSTOM) != \
357  1
358 #error Must define exactly one of GPR_CPU_LINUX, GPR_CPU_POSIX, GPR_WINDOWS, GPR_CPU_IPHONE, GPR_CPU_CUSTOM
359 #endif
360 
361 #if defined(GPR_MSVC_TLS) + defined(GPR_GCC_TLS) + defined(GPR_PTHREAD_TLS) + \
362  defined(GPR_CUSTOM_TLS) != \
363  1
364 #error Must define exactly one of GPR_MSVC_TLS, GPR_GCC_TLS, GPR_PTHREAD_TLS, GPR_CUSTOM_TLS
365 #endif
366 
367 /* maximum alignment needed for any type on this platform, rounded up to a
368  power of two */
369 #define GPR_MAX_ALIGNMENT 16
370 
371 #ifndef GRPC_ARES
372 #ifdef GPR_WINDOWS
373 #define GRPC_ARES 0
374 #else
375 #define GRPC_ARES 1
376 #endif
377 #endif
378 
379 #ifndef GRPC_MUST_USE_RESULT
380 #if defined(__GNUC__) && !defined(__MINGW32__)
381 #define GRPC_MUST_USE_RESULT __attribute__((warn_unused_result))
382 #define GPR_ALIGN_STRUCT(n) __attribute__((aligned(n)))
383 #else
384 #define GRPC_MUST_USE_RESULT
385 #define GPR_ALIGN_STRUCT(n)
386 #endif
387 #endif
388 
389 #ifndef GPR_PRINT_FORMAT_CHECK
390 #ifdef __GNUC__
391 #define GPR_PRINT_FORMAT_CHECK(FORMAT_STR, ARGS) \
392  __attribute__((format(printf, FORMAT_STR, ARGS)))
393 #else
394 #define GPR_PRINT_FORMAT_CHECK(FORMAT_STR, ARGS)
395 #endif
396 #endif /* GPR_PRINT_FORMAT_CHECK */
397 
398 #if GPR_FORBID_UNREACHABLE_CODE
399 #define GPR_UNREACHABLE_CODE(STATEMENT)
400 #else
401 #define GPR_UNREACHABLE_CODE(STATEMENT) \
402  do { \
403  gpr_log(GPR_ERROR, "Should never reach here."); \
404  abort(); \
405  STATEMENT; \
406  } while (0)
407 #endif /* GPR_FORBID_UNREACHABLE_CODE */
408 
409 #ifndef GPRAPI
410 #define GPRAPI
411 #endif
412 
413 #ifndef GRPCAPI
414 #define GRPCAPI GPRAPI
415 #endif
416 
417 #ifndef CENSUSAPI
418 #define CENSUSAPI GRPCAPI
419 #endif
420 
421 #endif /* GRPC_IMPL_CODEGEN_PORT_PLATFORM_H */