GRPC C++  1.2.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_POSIX_CRASH_HANDLER 1
161 #define GPR_PLATFORM_STRING "linux"
162 #ifndef _BSD_SOURCE
163 #define _BSD_SOURCE
164 #endif
165 #ifndef _DEFAULT_SOURCE
166 #define _DEFAULT_SOURCE
167 #endif
168 #ifndef _GNU_SOURCE
169 #define _GNU_SOURCE
170 #endif
171 #include <features.h>
172 #define GPR_CPU_LINUX 1
173 #define GPR_GCC_ATOMIC 1
174 #define GPR_GCC_TLS 1
175 #define GPR_LINUX 1
176 #define GPR_LINUX_LOG
177 #define GPR_SUPPORT_CHANNELS_FROM_FD 1
178 #define GPR_LINUX_ENV 1
179 #define GPR_POSIX_TMPFILE 1
180 #define GPR_POSIX_STRING 1
181 #define GPR_POSIX_SUBPROCESS 1
182 #define GPR_POSIX_SYNC 1
183 #define GPR_POSIX_TIME 1
184 #define GPR_GETPID_IN_UNISTD_H 1
185 #ifdef _LP64
186 #define GPR_ARCH_64 1
187 #else /* _LP64 */
188 #define GPR_ARCH_32 1
189 #endif /* _LP64 */
190 #elif defined(__APPLE__)
191 #include <Availability.h>
192 #include <TargetConditionals.h>
193 #ifndef _BSD_SOURCE
194 #define _BSD_SOURCE
195 #endif
196 #if TARGET_OS_IPHONE
197 #define GPR_FORBID_UNREACHABLE_CODE 1
198 #define GPR_PLATFORM_STRING "ios"
199 #define GPR_CPU_IPHONE 1
200 #define GPR_PTHREAD_TLS 1
201 #else /* TARGET_OS_IPHONE */
202 #define GPR_PLATFORM_STRING "osx"
203 #ifdef __MAC_OS_X_VERSION_MIN_REQUIRED
204 #if __MAC_OS_X_VERSION_MIN_REQUIRED < __MAC_10_7
205 #define GPR_CPU_IPHONE 1
206 #define GPR_PTHREAD_TLS 1
207 #else /* __MAC_OS_X_VERSION_MIN_REQUIRED < __MAC_10_7 */
208 #define GPR_CPU_POSIX 1
209 #define GPR_GCC_TLS 1
210 #endif
211 #else /* __MAC_OS_X_VERSION_MIN_REQUIRED */
212 #define GPR_CPU_POSIX 1
213 #define GPR_GCC_TLS 1
214 #endif
215 #define GPR_POSIX_CRASH_HANDLER 1
216 #endif
217 #define GPR_APPLE 1
218 #define GPR_GCC_ATOMIC 1
219 #define GPR_POSIX_LOG 1
220 #define GPR_POSIX_ENV 1
221 #define GPR_POSIX_TMPFILE 1
222 #define GPR_POSIX_STRING 1
223 #define GPR_POSIX_SUBPROCESS 1
224 #define GPR_POSIX_SYNC 1
225 #define GPR_POSIX_TIME 1
226 #define GPR_GETPID_IN_UNISTD_H 1
227 #define GPR_SUPPORT_CHANNELS_FROM_FD 1
228 #ifdef _LP64
229 #define GPR_ARCH_64 1
230 #else /* _LP64 */
231 #define GPR_ARCH_32 1
232 #endif /* _LP64 */
233 #elif defined(__FreeBSD__)
234 #define GPR_PLATFORM_STRING "freebsd"
235 #ifndef _BSD_SOURCE
236 #define _BSD_SOURCE
237 #endif
238 #define GPR_FREEBSD 1
239 #define GPR_CPU_POSIX 1
240 #define GPR_GCC_ATOMIC 1
241 #define GPR_GCC_TLS 1
242 #define GPR_POSIX_LOG 1
243 #define GPR_POSIX_ENV 1
244 #define GPR_POSIX_TMPFILE 1
245 #define GPR_POSIX_STRING 1
246 #define GPR_POSIX_SUBPROCESS 1
247 #define GPR_POSIX_SYNC 1
248 #define GPR_POSIX_TIME 1
249 #define GPR_GETPID_IN_UNISTD_H 1
250 #define GPR_SUPPORT_CHANNELS_FROM_FD 1
251 #ifdef _LP64
252 #define GPR_ARCH_64 1
253 #else /* _LP64 */
254 #define GPR_ARCH_32 1
255 #endif /* _LP64 */
256 #elif defined(__native_client__)
257 #define GPR_PLATFORM_STRING "nacl"
258 #ifndef _BSD_SOURCE
259 #define _BSD_SOURCE
260 #endif
261 #ifndef _DEFAULT_SOURCE
262 #define _DEFAULT_SOURCE
263 #endif
264 #ifndef _GNU_SOURCE
265 #define _GNU_SOURCE
266 #endif
267 #define GPR_NACL 1
268 #define GPR_CPU_POSIX 1
269 #define GPR_GCC_ATOMIC 1
270 #define GPR_GCC_TLS 1
271 #define GPR_POSIX_LOG 1
272 #define GPR_POSIX_ENV 1
273 #define GPR_POSIX_TMPFILE 1
274 #define GPR_POSIX_STRING 1
275 #define GPR_POSIX_SUBPROCESS 1
276 #define GPR_POSIX_SYNC 1
277 #define GPR_POSIX_TIME 1
278 #define GPR_GETPID_IN_UNISTD_H 1
279 #ifdef _LP64
280 #define GPR_ARCH_64 1
281 #else /* _LP64 */
282 #define GPR_ARCH_32 1
283 #endif /* _LP64 */
284 #else
285 #error "Could not auto-detect platform"
286 #endif
287 #endif /* GPR_NO_AUTODETECT_PLATFORM */
288 
289 #ifndef GPR_PLATFORM_STRING
290 #warning "GPR_PLATFORM_STRING not auto-detected"
291 #define GPR_PLATFORM_STRING "unknown"
292 #endif
293 
294 #ifdef GPR_GCOV
295 #undef GPR_FORBID_UNREACHABLE_CODE
296 #define GPR_FORBID_UNREACHABLE_CODE 1
297 #endif
298 
299 #ifdef _MSC_VER
300 #ifdef _PYTHON_MSVC
301 // The Python 3.5 Windows runtime is missing InetNtop
302 #define GPR_WIN_INET_NTOP
303 #endif // _PYTHON_MSVC
304 #if _MSC_VER < 1700
305 typedef __int8 int8_t;
306 typedef __int16 int16_t;
307 typedef __int32 int32_t;
308 typedef __int64 int64_t;
309 typedef unsigned __int8 uint8_t;
310 typedef unsigned __int16 uint16_t;
311 typedef unsigned __int32 uint32_t;
312 typedef unsigned __int64 uint64_t;
313 #else
314 #include <stdint.h>
315 #endif /* _MSC_VER < 1700 */
316 #else
317 #include <stdint.h>
318 #endif /* _MSC_VER */
319 
320 /* Cache line alignment */
321 #ifndef GPR_CACHELINE_SIZE_LOG
322 #if defined(__i386__) || defined(__x86_64__)
323 #define GPR_CACHELINE_SIZE_LOG 6
324 #endif
325 #ifndef GPR_CACHELINE_SIZE_LOG
326 /* A reasonable default guess. Note that overestimates tend to waste more
327  space, while underestimates tend to waste more time. */
328 #define GPR_CACHELINE_SIZE_LOG 6
329 #endif /* GPR_CACHELINE_SIZE_LOG */
330 #endif /* GPR_CACHELINE_SIZE_LOG */
331 
332 #define GPR_CACHELINE_SIZE (1 << GPR_CACHELINE_SIZE_LOG)
333 
334 /* scrub GCC_ATOMIC if it's not available on this compiler */
335 #if defined(GPR_GCC_ATOMIC) && !defined(__ATOMIC_RELAXED)
336 #undef GPR_GCC_ATOMIC
337 #define GPR_GCC_SYNC 1
338 #endif
339 
340 /* Validate platform combinations */
341 #if defined(GPR_GCC_ATOMIC) + defined(GPR_GCC_SYNC) + \
342  defined(GPR_WINDOWS_ATOMIC) != \
343  1
344 #error Must define exactly one of GPR_GCC_ATOMIC, GPR_GCC_SYNC, GPR_WINDOWS_ATOMIC
345 #endif
346 
347 #if defined(GPR_ARCH_32) + defined(GPR_ARCH_64) != 1
348 #error Must define exactly one of GPR_ARCH_32, GPR_ARCH_64
349 #endif
350 
351 #if defined(GPR_CPU_LINUX) + defined(GPR_CPU_POSIX) + defined(GPR_WINDOWS) + \
352  defined(GPR_CPU_IPHONE) + defined(GPR_CPU_CUSTOM) != \
353  1
354 #error Must define exactly one of GPR_CPU_LINUX, GPR_CPU_POSIX, GPR_WINDOWS, GPR_CPU_IPHONE, GPR_CPU_CUSTOM
355 #endif
356 
357 #if defined(GPR_MSVC_TLS) + defined(GPR_GCC_TLS) + defined(GPR_PTHREAD_TLS) + \
358  defined(GPR_CUSTOM_TLS) != \
359  1
360 #error Must define exactly one of GPR_MSVC_TLS, GPR_GCC_TLS, GPR_PTHREAD_TLS, GPR_CUSTOM_TLS
361 #endif
362 
363 /* maximum alignment needed for any type on this platform, rounded up to a
364  power of two */
365 #define GPR_MAX_ALIGNMENT 16
366 
367 #ifndef GRPC_MUST_USE_RESULT
368 #if defined(__GNUC__) && !defined(__MINGW32__)
369 #define GRPC_MUST_USE_RESULT __attribute__((warn_unused_result))
370 #else
371 #define GRPC_MUST_USE_RESULT
372 #endif
373 #endif
374 
375 #ifndef GPR_PRINT_FORMAT_CHECK
376 #ifdef __GNUC__
377 #define GPR_PRINT_FORMAT_CHECK(FORMAT_STR, ARGS) \
378  __attribute__((format(printf, FORMAT_STR, ARGS)))
379 #else
380 #define GPR_PRINT_FORMAT_CHECK(FORMAT_STR, ARGS)
381 #endif
382 #endif /* GPR_PRINT_FORMAT_CHECK */
383 
384 #if GPR_FORBID_UNREACHABLE_CODE
385 #define GPR_UNREACHABLE_CODE(STATEMENT)
386 #else
387 #define GPR_UNREACHABLE_CODE(STATEMENT) \
388  do { \
389  gpr_log(GPR_ERROR, "Should never reach here."); \
390  abort(); \
391  STATEMENT; \
392  } while (0)
393 #endif /* GPR_FORBID_UNREACHABLE_CODE */
394 
395 #ifndef GPRAPI
396 #define GPRAPI
397 #endif
398 
399 #ifndef GRPCAPI
400 #define GRPCAPI GPRAPI
401 #endif
402 
403 #ifndef CENSUSAPI
404 #define CENSUSAPI GRPCAPI
405 #endif
406 
407 #endif /* GRPC_IMPL_CODEGEN_PORT_PLATFORM_H */