port_platform.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723
  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. #ifndef GRPC_IMPL_CODEGEN_PORT_PLATFORM_H
  19. #define GRPC_IMPL_CODEGEN_PORT_PLATFORM_H
  20. // IWYU pragma: private, include <grpc/support/port_platform.h>
  21. /*
  22. * Define GPR_BACKWARDS_COMPATIBILITY_MODE to try harder to be ABI
  23. * compatible with older platforms (currently only on Linux)
  24. * Causes:
  25. * - some libc calls to be gotten via dlsym
  26. * - some syscalls to be made directly
  27. */
  28. // [[deprecated]] attribute is only available since C++14
  29. #if __cplusplus >= 201402L
  30. #define GRPC_DEPRECATED(reason) [[deprecated(reason)]]
  31. #else
  32. #define GRPC_DEPRECATED(reason)
  33. #endif // __cplusplus >= 201402L
  34. /*
  35. * Defines GPR_ABSEIL_SYNC to use synchronization features from Abseil
  36. */
  37. #ifndef GPR_ABSEIL_SYNC
  38. #if defined(__APPLE__)
  39. // This is disabled on Apple platforms because macos/grpc_basictests_c_cpp
  40. // fails with this. https://github.com/grpc/grpc/issues/23661
  41. #else
  42. #define GPR_ABSEIL_SYNC 1
  43. #endif
  44. #endif // GPR_ABSEIL_SYNC
  45. /*
  46. * Defines GRPC_ERROR_IS_ABSEIL_STATUS to use absl::Status for grpc_error_handle
  47. */
  48. #ifndef GRPC_ERROR_IS_ABSEIL_STATUS
  49. // #define GRPC_ERROR_IS_ABSEIL_STATUS 1
  50. #endif
  51. /* Get windows.h included everywhere (we need it) */
  52. #if defined(_WIN64) || defined(WIN64) || defined(_WIN32) || defined(WIN32)
  53. #ifndef WIN32_LEAN_AND_MEAN
  54. #define GRPC_WIN32_LEAN_AND_MEAN_WAS_NOT_DEFINED
  55. #define WIN32_LEAN_AND_MEAN
  56. #endif /* WIN32_LEAN_AND_MEAN */
  57. #ifndef NOMINMAX
  58. #define GRPC_NOMINMX_WAS_NOT_DEFINED
  59. #define NOMINMAX
  60. #endif /* NOMINMAX */
  61. #include <windows.h>
  62. #ifndef _WIN32_WINNT
  63. #error \
  64. "Please compile grpc with _WIN32_WINNT of at least 0x600 (aka Windows Vista)"
  65. #else /* !defined(_WIN32_WINNT) */
  66. #if (_WIN32_WINNT < 0x0600)
  67. #error \
  68. "Please compile grpc with _WIN32_WINNT of at least 0x600 (aka Windows Vista)"
  69. #endif /* _WIN32_WINNT < 0x0600 */
  70. #endif /* defined(_WIN32_WINNT) */
  71. #ifdef GRPC_WIN32_LEAN_AND_MEAN_WAS_NOT_DEFINED
  72. #undef GRPC_WIN32_LEAN_AND_MEAN_WAS_NOT_DEFINED
  73. #undef WIN32_LEAN_AND_MEAN
  74. #endif /* GRPC_WIN32_LEAN_AND_MEAN_WAS_NOT_DEFINED */
  75. #ifdef GRPC_NOMINMAX_WAS_NOT_DEFINED
  76. #undef GRPC_NOMINMAX_WAS_NOT_DEFINED
  77. #undef NOMINMAX
  78. #endif /* GRPC_WIN32_LEAN_AND_MEAN_WAS_NOT_DEFINED */
  79. #endif /* defined(_WIN64) || defined(WIN64) || defined(_WIN32) || \
  80. defined(WIN32) */
  81. /* Override this file with one for your platform if you need to redefine
  82. things. */
  83. #if !defined(GPR_NO_AUTODETECT_PLATFORM)
  84. #if defined(_WIN64) || defined(WIN64) || defined(_WIN32) || defined(WIN32)
  85. #if defined(_WIN64) || defined(WIN64)
  86. #define GPR_ARCH_64 1
  87. #else
  88. #define GPR_ARCH_32 1
  89. #endif
  90. #define GPR_PLATFORM_STRING "windows"
  91. #define GPR_WINDOWS 1
  92. #define GPR_WINDOWS_SUBPROCESS 1
  93. #define GPR_WINDOWS_ENV
  94. #ifdef __MSYS__
  95. #define GPR_GETPID_IN_UNISTD_H 1
  96. #define GPR_MSYS_TMPFILE
  97. #define GPR_POSIX_LOG
  98. #define GPR_POSIX_STRING
  99. #define GPR_POSIX_TIME
  100. #else
  101. #define GPR_GETPID_IN_PROCESS_H 1
  102. #define GPR_WINDOWS_TMPFILE
  103. #define GPR_WINDOWS_LOG
  104. #define GPR_WINDOWS_CRASH_HANDLER 1
  105. #define GPR_WINDOWS_STAT
  106. #define GPR_WINDOWS_STRING
  107. #define GPR_WINDOWS_TIME
  108. #endif
  109. #ifdef __GNUC__
  110. #define GPR_GCC_ATOMIC 1
  111. #else
  112. #define GPR_WINDOWS_ATOMIC 1
  113. #endif
  114. #elif defined(ANDROID) || defined(__ANDROID__)
  115. #define GPR_PLATFORM_STRING "android"
  116. #define GPR_ANDROID 1
  117. #ifndef __ANDROID_API__
  118. #error "__ANDROID_API__ must be defined for Android builds."
  119. #endif
  120. #if __ANDROID_API__ < 21
  121. #error "Requires Android API v21 and above"
  122. #endif
  123. #define GPR_SUPPORT_BINDER_TRANSPORT 1
  124. // TODO(apolcyn): re-evaluate support for c-ares
  125. // on android after upgrading our c-ares dependency.
  126. // See https://github.com/grpc/grpc/issues/18038.
  127. #define GRPC_ARES 0
  128. #ifdef _LP64
  129. #define GPR_ARCH_64 1
  130. #else /* _LP64 */
  131. #define GPR_ARCH_32 1
  132. #endif /* _LP64 */
  133. #define GPR_CPU_POSIX 1
  134. #define GPR_GCC_SYNC 1
  135. #define GPR_POSIX_ENV 1
  136. #define GPR_POSIX_TMPFILE 1
  137. #define GPR_POSIX_STAT 1
  138. #define GPR_ANDROID_LOG 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_HAS_PTHREAD_H 1
  144. #define GPR_GETPID_IN_UNISTD_H 1
  145. #define GPR_SUPPORT_CHANNELS_FROM_FD 1
  146. #elif defined(__linux__)
  147. #define GPR_PLATFORM_STRING "linux"
  148. #ifndef _BSD_SOURCE
  149. #define _BSD_SOURCE
  150. #endif
  151. #ifndef _DEFAULT_SOURCE
  152. #define _DEFAULT_SOURCE
  153. #endif
  154. #ifndef _GNU_SOURCE
  155. #define _GNU_SOURCE
  156. #endif
  157. #include <features.h>
  158. #define GPR_CPU_LINUX 1
  159. #define GPR_GCC_ATOMIC 1
  160. #define GPR_LINUX 1
  161. #define GPR_LINUX_LOG
  162. #define GPR_SUPPORT_CHANNELS_FROM_FD 1
  163. #define GPR_LINUX_ENV 1
  164. #define GPR_POSIX_TMPFILE 1
  165. #define GPR_POSIX_STAT 1
  166. #define GPR_POSIX_STRING 1
  167. #define GPR_POSIX_SUBPROCESS 1
  168. #define GPR_POSIX_SYNC 1
  169. #define GPR_POSIX_TIME 1
  170. #define GPR_HAS_PTHREAD_H 1
  171. #define GPR_GETPID_IN_UNISTD_H 1
  172. #ifdef _LP64
  173. #define GPR_ARCH_64 1
  174. #else /* _LP64 */
  175. #define GPR_ARCH_32 1
  176. #endif /* _LP64 */
  177. #ifdef __GLIBC__
  178. #define GPR_POSIX_CRASH_HANDLER 1
  179. #ifdef __GLIBC_PREREQ
  180. #if __GLIBC_PREREQ(2, 12)
  181. #define GPR_LINUX_PTHREAD_NAME 1
  182. #endif
  183. #else
  184. // musl libc & others
  185. #define GPR_LINUX_PTHREAD_NAME 1
  186. #endif
  187. #include <linux/version.h>
  188. #else /* musl libc */
  189. #define GPR_MUSL_LIBC_COMPAT 1
  190. #endif
  191. #elif defined(__ASYLO__)
  192. #define GPR_ARCH_64 1
  193. #define GPR_CPU_POSIX 1
  194. #define GPR_PLATFORM_STRING "asylo"
  195. #define GPR_GCC_SYNC 1
  196. #define GPR_POSIX_SYNC 1
  197. #define GPR_POSIX_STRING 1
  198. #define GPR_POSIX_LOG 1
  199. #define GPR_POSIX_TIME 1
  200. #define GPR_POSIX_ENV 1
  201. #define GPR_ASYLO 1
  202. #define GRPC_POSIX_SOCKET 1
  203. #define GRPC_POSIX_SOCKETADDR
  204. #define GRPC_POSIX_SOCKETUTILS 1
  205. #define GRPC_TIMER_USE_GENERIC 1
  206. #define GRPC_POSIX_NO_SPECIAL_WAKEUP_FD 1
  207. #define GRPC_POSIX_WAKEUP_FD 1
  208. #define GRPC_ARES 0
  209. #define GPR_NO_AUTODETECT_PLATFORM 1
  210. #elif defined(__APPLE__)
  211. #include <Availability.h>
  212. #include <TargetConditionals.h>
  213. #ifndef _BSD_SOURCE
  214. #define _BSD_SOURCE
  215. #endif
  216. #if TARGET_OS_IPHONE
  217. #define GPR_PLATFORM_STRING "ios"
  218. #define GPR_CPU_IPHONE 1
  219. #define GRPC_CFSTREAM 1
  220. /* the c-ares resolver isn't safe to enable on iOS */
  221. #define GRPC_ARES 0
  222. #else /* TARGET_OS_IPHONE */
  223. #define GPR_PLATFORM_STRING "osx"
  224. #define GPR_CPU_POSIX 1
  225. #define GPR_POSIX_CRASH_HANDLER 1
  226. #endif
  227. #if !(defined(__has_feature) && __has_feature(cxx_thread_local))
  228. #define GPR_PTHREAD_TLS 1
  229. #endif
  230. #define GPR_APPLE 1
  231. #define GPR_GCC_ATOMIC 1
  232. #define GPR_POSIX_LOG 1
  233. #define GPR_POSIX_ENV 1
  234. #define GPR_POSIX_TMPFILE 1
  235. #define GPR_POSIX_STAT 1
  236. #define GPR_POSIX_STRING 1
  237. #define GPR_POSIX_SUBPROCESS 1
  238. #define GPR_POSIX_SYNC 1
  239. #define GPR_POSIX_TIME 1
  240. #define GPR_HAS_PTHREAD_H 1
  241. #define GPR_GETPID_IN_UNISTD_H 1
  242. #ifndef GRPC_CFSTREAM
  243. #define GPR_SUPPORT_CHANNELS_FROM_FD 1
  244. #endif
  245. #ifdef _LP64
  246. #define GPR_ARCH_64 1
  247. #else /* _LP64 */
  248. #define GPR_ARCH_32 1
  249. #endif /* _LP64 */
  250. #elif defined(__FreeBSD__)
  251. #define GPR_PLATFORM_STRING "freebsd"
  252. #ifndef _BSD_SOURCE
  253. #define _BSD_SOURCE
  254. #endif
  255. #define GPR_FREEBSD 1
  256. #define GPR_CPU_POSIX 1
  257. #define GPR_GCC_ATOMIC 1
  258. #define GPR_POSIX_LOG 1
  259. #define GPR_POSIX_ENV 1
  260. #define GPR_POSIX_TMPFILE 1
  261. #define GPR_POSIX_STAT 1
  262. #define GPR_POSIX_STRING 1
  263. #define GPR_POSIX_SUBPROCESS 1
  264. #define GPR_POSIX_SYNC 1
  265. #define GPR_POSIX_TIME 1
  266. #define GPR_HAS_PTHREAD_H 1
  267. #define GPR_GETPID_IN_UNISTD_H 1
  268. #define GPR_SUPPORT_CHANNELS_FROM_FD 1
  269. #ifdef _LP64
  270. #define GPR_ARCH_64 1
  271. #else /* _LP64 */
  272. #define GPR_ARCH_32 1
  273. #endif /* _LP64 */
  274. #elif defined(__OpenBSD__)
  275. #define GPR_PLATFORM_STRING "openbsd"
  276. #ifndef _BSD_SOURCE
  277. #define _BSD_SOURCE
  278. #endif
  279. #define GPR_OPENBSD 1
  280. #define GPR_CPU_POSIX 1
  281. #define GPR_GCC_ATOMIC 1
  282. #define GPR_POSIX_LOG 1
  283. #define GPR_POSIX_ENV 1
  284. #define GPR_POSIX_TMPFILE 1
  285. #define GPR_POSIX_STAT 1
  286. #define GPR_POSIX_STRING 1
  287. #define GPR_POSIX_SUBPROCESS 1
  288. #define GPR_POSIX_SYNC 1
  289. #define GPR_POSIX_TIME 1
  290. #define GPR_HAS_PTHREAD_H 1
  291. #define GPR_GETPID_IN_UNISTD_H 1
  292. #define GPR_SUPPORT_CHANNELS_FROM_FD 1
  293. #ifdef _LP64
  294. #define GPR_ARCH_64 1
  295. #else /* _LP64 */
  296. #define GPR_ARCH_32 1
  297. #endif /* _LP64 */
  298. #elif defined(__sun) && defined(__SVR4)
  299. #define GPR_PLATFORM_STRING "solaris"
  300. #define GPR_SOLARIS 1
  301. #define GPR_CPU_POSIX 1
  302. #define GPR_GCC_ATOMIC 1
  303. #define GPR_POSIX_LOG 1
  304. #define GPR_POSIX_ENV 1
  305. #define GPR_POSIX_TMPFILE 1
  306. #define GPR_POSIX_STAT 1
  307. #define GPR_POSIX_STRING 1
  308. #define GPR_POSIX_SUBPROCESS 1
  309. #define GPR_POSIX_SYNC 1
  310. #define GPR_POSIX_TIME 1
  311. #define GPR_HAS_PTHREAD_H 1
  312. #define GPR_GETPID_IN_UNISTD_H 1
  313. #ifdef _LP64
  314. #define GPR_ARCH_64 1
  315. #else /* _LP64 */
  316. #define GPR_ARCH_32 1
  317. #endif /* _LP64 */
  318. #elif defined(_AIX)
  319. #define GPR_PLATFORM_STRING "aix"
  320. #ifndef _ALL_SOURCE
  321. #define _ALL_SOURCE
  322. #endif
  323. #define GPR_AIX 1
  324. #define GPR_CPU_POSIX 1
  325. #define GPR_GCC_ATOMIC 1
  326. #define GPR_POSIX_LOG 1
  327. #define GPR_POSIX_ENV 1
  328. #define GPR_POSIX_TMPFILE 1
  329. #define GPR_POSIX_STAT 1
  330. #define GPR_POSIX_STRING 1
  331. #define GPR_POSIX_SUBPROCESS 1
  332. #define GPR_POSIX_SYNC 1
  333. #define GPR_POSIX_TIME 1
  334. #define GPR_HAS_PTHREAD_H 1
  335. #define GPR_GETPID_IN_UNISTD_H 1
  336. #ifdef _LP64
  337. #define GPR_ARCH_64 1
  338. #else /* _LP64 */
  339. #define GPR_ARCH_32 1
  340. #endif /* _LP64 */
  341. #elif defined(__native_client__)
  342. #define GPR_PLATFORM_STRING "nacl"
  343. #ifndef _BSD_SOURCE
  344. #define _BSD_SOURCE
  345. #endif
  346. #ifndef _DEFAULT_SOURCE
  347. #define _DEFAULT_SOURCE
  348. #endif
  349. #ifndef _GNU_SOURCE
  350. #define _GNU_SOURCE
  351. #endif
  352. #define GPR_NACL 1
  353. #define GPR_CPU_POSIX 1
  354. #define GPR_GCC_ATOMIC 1
  355. #define GPR_POSIX_LOG 1
  356. #define GPR_POSIX_ENV 1
  357. #define GPR_POSIX_TMPFILE 1
  358. #define GPR_POSIX_STAT 1
  359. #define GPR_POSIX_STRING 1
  360. #define GPR_POSIX_SUBPROCESS 1
  361. #define GPR_POSIX_SYNC 1
  362. #define GPR_POSIX_TIME 1
  363. #define GPR_HAS_PTHREAD_H 1
  364. #define GPR_GETPID_IN_UNISTD_H 1
  365. #ifdef _LP64
  366. #define GPR_ARCH_64 1
  367. #else /* _LP64 */
  368. #define GPR_ARCH_32 1
  369. #endif /* _LP64 */
  370. #elif defined(__Fuchsia__)
  371. #define GRPC_ARES 0
  372. #define GPR_FUCHSIA 1
  373. #define GPR_ARCH_64 1
  374. #define GPR_PLATFORM_STRING "fuchsia"
  375. #include <features.h>
  376. // Specifying musl libc affects wrap_memcpy.c. It causes memmove() to be
  377. // invoked.
  378. #define GPR_MUSL_LIBC_COMPAT 1
  379. #define GPR_CPU_POSIX 1
  380. #define GPR_GCC_ATOMIC 1
  381. #define GPR_POSIX_LOG 1
  382. #define GPR_POSIX_SYNC 1
  383. #define GPR_POSIX_ENV 1
  384. #define GPR_POSIX_TMPFILE 1
  385. #define GPR_POSIX_STAT 1
  386. #define GPR_POSIX_SUBPROCESS 1
  387. #define GPR_POSIX_SYNC 1
  388. #define GPR_POSIX_STRING 1
  389. #define GPR_POSIX_TIME 1
  390. #define GPR_HAS_PTHREAD_H 1
  391. #define GPR_GETPID_IN_UNISTD_H 1
  392. #define GRPC_ROOT_PEM_PATH "/config/ssl/cert.pem"
  393. #else
  394. #error "Could not auto-detect platform"
  395. #endif
  396. #endif /* GPR_NO_AUTODETECT_PLATFORM */
  397. #if defined(GPR_BACKWARDS_COMPATIBILITY_MODE)
  398. /*
  399. * For backward compatibility mode, reset _FORTIFY_SOURCE to prevent
  400. * a library from having non-standard symbols such as __asprintf_chk.
  401. * This helps non-glibc systems such as alpine using musl to find symbols.
  402. */
  403. #if defined(_FORTIFY_SOURCE) && _FORTIFY_SOURCE > 0
  404. #undef _FORTIFY_SOURCE
  405. #define _FORTIFY_SOURCE 0
  406. #endif
  407. #endif
  408. #if defined(__has_include)
  409. #if __has_include(<atomic>)
  410. #define GRPC_HAS_CXX11_ATOMIC
  411. #endif /* __has_include(<atomic>) */
  412. #endif /* defined(__has_include) */
  413. #ifndef GPR_PLATFORM_STRING
  414. #warning "GPR_PLATFORM_STRING not auto-detected"
  415. #define GPR_PLATFORM_STRING "unknown"
  416. #endif
  417. #ifdef GPR_GCOV
  418. #undef GPR_FORBID_UNREACHABLE_CODE
  419. #define GPR_FORBID_UNREACHABLE_CODE 1
  420. #endif
  421. #ifdef _MSC_VER
  422. #if _MSC_VER < 1700
  423. typedef __int8 int8_t;
  424. typedef __int16 int16_t;
  425. typedef __int32 int32_t;
  426. typedef __int64 int64_t;
  427. typedef unsigned __int8 uint8_t;
  428. typedef unsigned __int16 uint16_t;
  429. typedef unsigned __int32 uint32_t;
  430. typedef unsigned __int64 uint64_t;
  431. #else
  432. #include <stdint.h>
  433. #endif /* _MSC_VER < 1700 */
  434. #else
  435. #include <stdint.h>
  436. #endif /* _MSC_VER */
  437. /* Type of cycle clock implementation */
  438. #ifdef GPR_LINUX
  439. /* Disable cycle clock by default.
  440. TODO(soheil): enable when we support fallback for unstable cycle clocks.
  441. #if defined(__i386__)
  442. #define GPR_CYCLE_COUNTER_RDTSC_32 1
  443. #elif defined(__x86_64__) || defined(__amd64__)
  444. #define GPR_CYCLE_COUNTER_RDTSC_64 1
  445. #else
  446. #define GPR_CYCLE_COUNTER_FALLBACK 1
  447. #endif
  448. */
  449. #define GPR_CYCLE_COUNTER_FALLBACK 1
  450. #else
  451. #define GPR_CYCLE_COUNTER_FALLBACK 1
  452. #endif /* GPR_LINUX */
  453. /* Cache line alignment */
  454. #ifndef GPR_CACHELINE_SIZE_LOG
  455. #if defined(__i386__) || defined(__x86_64__)
  456. #define GPR_CACHELINE_SIZE_LOG 6
  457. #endif
  458. #ifndef GPR_CACHELINE_SIZE_LOG
  459. /* A reasonable default guess. Note that overestimates tend to waste more
  460. space, while underestimates tend to waste more time. */
  461. #define GPR_CACHELINE_SIZE_LOG 6
  462. #endif /* GPR_CACHELINE_SIZE_LOG */
  463. #endif /* GPR_CACHELINE_SIZE_LOG */
  464. #define GPR_CACHELINE_SIZE (1 << GPR_CACHELINE_SIZE_LOG)
  465. /* scrub GCC_ATOMIC if it's not available on this compiler */
  466. #if defined(GPR_GCC_ATOMIC) && !defined(__ATOMIC_RELAXED)
  467. #undef GPR_GCC_ATOMIC
  468. #define GPR_GCC_SYNC 1
  469. #endif
  470. /* Validate platform combinations */
  471. #if defined(GPR_GCC_ATOMIC) + defined(GPR_GCC_SYNC) + \
  472. defined(GPR_WINDOWS_ATOMIC) != \
  473. 1
  474. #error Must define exactly one of GPR_GCC_ATOMIC, GPR_GCC_SYNC, GPR_WINDOWS_ATOMIC
  475. #endif
  476. #if defined(GPR_ARCH_32) + defined(GPR_ARCH_64) != 1
  477. #error Must define exactly one of GPR_ARCH_32, GPR_ARCH_64
  478. #endif
  479. #if defined(GPR_CPU_LINUX) + defined(GPR_CPU_POSIX) + defined(GPR_WINDOWS) + \
  480. defined(GPR_CPU_IPHONE) + defined(GPR_CPU_CUSTOM) != \
  481. 1
  482. #error Must define exactly one of GPR_CPU_LINUX, GPR_CPU_POSIX, GPR_WINDOWS, GPR_CPU_IPHONE, GPR_CPU_CUSTOM
  483. #endif
  484. /* maximum alignment needed for any type on this platform, rounded up to a
  485. power of two */
  486. #define GPR_MAX_ALIGNMENT 16
  487. #ifndef GRPC_ARES
  488. #define GRPC_ARES 1
  489. #endif
  490. #ifndef GRPC_IF_NAMETOINDEX
  491. #define GRPC_IF_NAMETOINDEX 1
  492. #endif
  493. #ifndef GRPC_MUST_USE_RESULT
  494. #if defined(__GNUC__) && !defined(__MINGW32__)
  495. #define GRPC_MUST_USE_RESULT __attribute__((warn_unused_result))
  496. #define GPR_ALIGN_STRUCT(n) __attribute__((aligned(n)))
  497. #else
  498. #define GRPC_MUST_USE_RESULT
  499. #define GPR_ALIGN_STRUCT(n)
  500. #endif
  501. #endif
  502. #ifndef GRPC_UNUSED
  503. #if defined(__GNUC__) && !defined(__MINGW32__)
  504. #define GRPC_UNUSED __attribute__((unused))
  505. #else
  506. #define GRPC_UNUSED
  507. #endif
  508. #endif
  509. #ifndef GPR_PRINT_FORMAT_CHECK
  510. #ifdef __GNUC__
  511. #define GPR_PRINT_FORMAT_CHECK(FORMAT_STR, ARGS) \
  512. __attribute__((format(printf, FORMAT_STR, ARGS)))
  513. #else
  514. #define GPR_PRINT_FORMAT_CHECK(FORMAT_STR, ARGS)
  515. #endif
  516. #endif /* GPR_PRINT_FORMAT_CHECK */
  517. #if GPR_FORBID_UNREACHABLE_CODE
  518. #define GPR_UNREACHABLE_CODE(STATEMENT)
  519. #else
  520. #define GPR_UNREACHABLE_CODE(STATEMENT) \
  521. do { \
  522. gpr_log(GPR_ERROR, "Should never reach here."); \
  523. abort(); \
  524. STATEMENT; \
  525. } while (0)
  526. #endif /* GPR_FORBID_UNREACHABLE_CODE */
  527. #ifndef GPRAPI
  528. #define GPRAPI
  529. #endif
  530. #ifndef GRPCAPI
  531. #define GRPCAPI GPRAPI
  532. #endif
  533. #ifndef CENSUSAPI
  534. #define CENSUSAPI GRPCAPI
  535. #endif
  536. #ifndef GPR_HAS_CPP_ATTRIBUTE
  537. #ifdef __has_cpp_attribute
  538. #define GPR_HAS_CPP_ATTRIBUTE(a) __has_cpp_attribute(a)
  539. #else
  540. #define GPR_HAS_CPP_ATTRIBUTE(a) 0
  541. #endif
  542. #endif /* GPR_HAS_CPP_ATTRIBUTE */
  543. #ifndef GPR_HAS_ATTRIBUTE
  544. #ifdef __has_attribute
  545. #define GPR_HAS_ATTRIBUTE(a) __has_attribute(a)
  546. #else
  547. #define GPR_HAS_ATTRIBUTE(a) 0
  548. #endif
  549. #endif /* GPR_HAS_ATTRIBUTE */
  550. #ifndef GPR_HAS_FEATURE
  551. #ifdef __has_feature
  552. #define GPR_HAS_FEATURE(a) __has_feature(a)
  553. #else
  554. #define GPR_HAS_FEATURE(a) 0
  555. #endif
  556. #endif /* GPR_HAS_FEATURE */
  557. #ifndef GPR_ATTRIBUTE_NOINLINE
  558. #if GPR_HAS_ATTRIBUTE(noinline) || (defined(__GNUC__) && !defined(__clang__))
  559. #define GPR_ATTRIBUTE_NOINLINE __attribute__((noinline))
  560. #define GPR_HAS_ATTRIBUTE_NOINLINE 1
  561. #else
  562. #define GPR_ATTRIBUTE_NOINLINE
  563. #endif
  564. #endif /* GPR_ATTRIBUTE_NOINLINE */
  565. #ifndef GPR_NO_UNIQUE_ADDRESS
  566. #if GPR_HAS_CPP_ATTRIBUTE(no_unique_address)
  567. #define GPR_NO_UNIQUE_ADDRESS [[no_unique_address]]
  568. #else
  569. #define GPR_NO_UNIQUE_ADDRESS
  570. #endif
  571. #endif /* GPR_NO_UNIQUE_ADDRESS */
  572. #ifndef GRPC_DEPRECATED
  573. #if GPR_HAS_CPP_ATTRIBUTE(deprecated)
  574. #define GRPC_DEPRECATED(reason) [[deprecated(reason)]]
  575. #else
  576. #define GRPC_DEPRECATED(reason)
  577. #endif
  578. #endif /* GRPC_DEPRECATED */
  579. #ifndef GPR_ATTRIBUTE_WEAK
  580. /* Attribute weak is broken on LLVM/windows:
  581. * https://bugs.llvm.org/show_bug.cgi?id=37598 */
  582. #if (GPR_HAS_ATTRIBUTE(weak) || (defined(__GNUC__) && !defined(__clang__))) && \
  583. !(defined(__llvm__) && defined(_WIN32))
  584. #define GPR_ATTRIBUTE_WEAK __attribute__((weak))
  585. #define GPR_HAS_ATTRIBUTE_WEAK 1
  586. #else
  587. #define GPR_ATTRIBUTE_WEAK
  588. #endif
  589. #endif /* GPR_ATTRIBUTE_WEAK */
  590. #ifndef GPR_ATTRIBUTE_NO_TSAN /* (1) */
  591. #if GPR_HAS_FEATURE(thread_sanitizer)
  592. #define GPR_ATTRIBUTE_NO_TSAN __attribute__((no_sanitize("thread")))
  593. #endif /* GPR_HAS_FEATURE */
  594. #ifndef GPR_ATTRIBUTE_NO_TSAN /* (2) */
  595. #define GPR_ATTRIBUTE_NO_TSAN
  596. #endif /* GPR_ATTRIBUTE_NO_TSAN (2) */
  597. #endif /* GPR_ATTRIBUTE_NO_TSAN (1) */
  598. /* GRPC_TSAN_ENABLED will be defined, when compiled with thread sanitizer. */
  599. #ifndef GRPC_TSAN_SUPPRESSED
  600. #if defined(__SANITIZE_THREAD__)
  601. #define GRPC_TSAN_ENABLED
  602. #elif GPR_HAS_FEATURE(thread_sanitizer)
  603. #define GRPC_TSAN_ENABLED
  604. #endif
  605. #endif
  606. /* GRPC_ASAN_ENABLED will be defined, when compiled with address sanitizer. */
  607. #ifndef GRPC_ASAN_SUPPRESSED
  608. #if defined(__SANITIZE_ADDRESS__)
  609. #define GRPC_ASAN_ENABLED
  610. #elif GPR_HAS_FEATURE(address_sanitizer)
  611. #define GRPC_ASAN_ENABLED
  612. #endif
  613. #endif
  614. /* GRPC_ALLOW_EXCEPTIONS should be 0 or 1 if exceptions are allowed or not */
  615. #ifndef GRPC_ALLOW_EXCEPTIONS
  616. #ifdef GPR_WINDOWS
  617. #if defined(_MSC_VER) && defined(_CPPUNWIND)
  618. #define GRPC_ALLOW_EXCEPTIONS 1
  619. #elif defined(__EXCEPTIONS)
  620. #define GRPC_ALLOW_EXCEPTIONS 1
  621. #else
  622. #define GRPC_ALLOW_EXCEPTIONS 0
  623. #endif
  624. #else /* GPR_WINDOWS */
  625. #ifdef __EXCEPTIONS
  626. #define GRPC_ALLOW_EXCEPTIONS 1
  627. #else /* __EXCEPTIONS */
  628. #define GRPC_ALLOW_EXCEPTIONS 0
  629. #endif /* __EXCEPTIONS */
  630. #endif /* __GPR_WINDOWS */
  631. #endif /* GRPC_ALLOW_EXCEPTIONS */
  632. /* Use GPR_LIKELY only in cases where you are sure that a certain outcome is the
  633. * most likely. Ideally, also collect performance numbers to justify the claim.
  634. */
  635. #ifdef __GNUC__
  636. #define GPR_LIKELY(x) __builtin_expect((x), 1)
  637. #define GPR_UNLIKELY(x) __builtin_expect((x), 0)
  638. #else /* __GNUC__ */
  639. #define GPR_LIKELY(x) (x)
  640. #define GPR_UNLIKELY(x) (x)
  641. #endif /* __GNUC__ */
  642. #ifndef __STDC_FORMAT_MACROS
  643. #define __STDC_FORMAT_MACROS
  644. #endif
  645. /* Selectively enable EventEngine on specific platforms. This default can be
  646. * overridden using the GRPC_USE_EVENT_ENGINE compiler flag.
  647. */
  648. #ifndef GRPC_USE_EVENT_ENGINE
  649. /* Not enabled by default on any platforms yet. (2021.06) */
  650. #elif GRPC_USE_EVENT_ENGINE == 0
  651. /* Building with `-DGRPC_USE_EVENT_ENGINE=0` will override the default. */
  652. #undef GRPC_USE_EVENT_ENGINE
  653. #endif /* GRPC_USE_EVENT_ENGINE */
  654. #ifdef GRPC_USE_EVENT_ENGINE
  655. #undef GPR_SUPPORT_CHANNELS_FROM_FD
  656. #define GRPC_ARES 0
  657. #endif /* GRPC_USE_EVENT_ENGINE */
  658. #define GRPC_CALLBACK_API_NONEXPERIMENTAL
  659. /* clang 11 with msan miscompiles destruction of [[no_unique_address]] members
  660. * of zero size - for a repro see:
  661. * test/core/compiler_bugs/miscompile_with_no_unique_address_test.cc
  662. */
  663. #ifdef __clang__
  664. #if __clang__ && __clang_major__ <= 11 && __has_feature(memory_sanitizer)
  665. #undef GPR_NO_UNIQUE_ADDRESS
  666. #define GPR_NO_UNIQUE_ADDRESS
  667. #endif
  668. #endif
  669. #endif /* GRPC_IMPL_CODEGEN_PORT_PLATFORM_H */