[utils] Add check for ANDROID_UNIFIED_HEADERS to mono-compiler.h (#5742)
[mono.git] / mono / utils / mono-compiler.h
1 /**
2  * \file
3  */
4
5 #ifndef __UTILS_MONO_COMPILER_H__
6 #define __UTILS_MONO_COMPILER_H__
7
8 /*
9  * This file includes macros used in the runtime to encapsulate different
10  * compiler behaviours.
11  */
12 #include <config.h>
13 #if defined(HAVE_UNISTD_H)
14 #include <unistd.h>
15 #endif
16
17 #ifdef __GNUC__
18 #define MONO_ATTR_USED __attribute__ ((__used__))
19 #else
20 #define MONO_ATTR_USED
21 #endif
22
23 #ifdef __GNUC__
24 #define MONO_ATTR_FORMAT_PRINTF(fmt_pos,arg_pos) __attribute__ ((__format__(__printf__,fmt_pos,arg_pos)))
25 #else
26 #define MONO_ATTR_FORMAT_PRINTF(fmt_pos,arg_pos)
27 #endif
28
29 /* Deal with Microsoft C compiler differences */
30 #ifdef _MSC_VER
31
32 #include <math.h>
33
34 #if _MSC_VER < 1800 /* VS 2013 */
35 #define strtoull _strtoui64
36 #endif
37
38 #include <float.h>
39 #define trunc(x)        (((x) < 0) ? ceil((x)) : floor((x)))
40 #if _MSC_VER < 1800 /* VS 2013 */
41 #define isnan(x)        _isnan(x)
42 #define isinf(x)        (_isnan(x) ? 0 : (_fpclass(x) == _FPCLASS_NINF) ? -1 : (_fpclass(x) == _FPCLASS_PINF) ? 1 : 0)
43 #define isnormal(x)     _finite(x)
44 #endif
45
46 #define popen           _popen
47 #define pclose          _pclose
48
49 #include <direct.h>
50 #define mkdir(x)        _mkdir(x)
51
52 #define __func__ __FUNCTION__
53
54 #include <BaseTsd.h>
55 typedef SSIZE_T ssize_t;
56
57 /*
58  * SSIZE_MAX is not defined in MSVC, so define it here.
59  *
60  * These values come from MinGW64, and are public domain.
61  *
62  */
63 #ifndef SSIZE_MAX
64 #ifdef _WIN64
65 #define SSIZE_MAX _I64_MAX
66 #else
67 #define SSIZE_MAX INT_MAX
68 #endif
69 #endif
70
71 #endif /* _MSC_VER */
72
73 #ifdef _MSC_VER
74 // Quiet Visual Studio linker warning, LNK4221, in cases when this source file intentional ends up empty.
75 #define MONO_EMPTY_SOURCE_FILE(x) void __mono_win32_ ## x ## _quiet_lnk4221 (void) {}
76 #else
77 #define MONO_EMPTY_SOURCE_FILE(x)
78 #endif
79
80 #if !defined(_MSC_VER) && !defined(HOST_SOLARIS) && !defined(_WIN32) && !defined(__CYGWIN__) && !defined(MONOTOUCH) && HAVE_VISIBILITY_HIDDEN
81 #if MONO_LLVM_LOADED
82 #define MONO_LLVM_INTERNAL MONO_API
83 #else
84 #define MONO_LLVM_INTERNAL
85 #endif
86 #else
87 #define MONO_LLVM_INTERNAL 
88 #endif
89
90 /* Used to mark internal functions used by the profiler modules */
91 #define MONO_PROFILER_API MONO_API
92
93 #ifdef __GNUC__
94 #define MONO_ALWAYS_INLINE __attribute__ ((__always_inline__))
95 #elif defined(_MSC_VER)
96 #define MONO_ALWAYS_INLINE __forceinline
97 #else
98 #define MONO_ALWAYS_INLINE
99 #endif
100
101 #ifdef __GNUC__
102 #define MONO_NEVER_INLINE __attribute__ ((__noinline__))
103 #elif defined(_MSC_VER)
104 #define MONO_NEVER_INLINE __declspec(noinline)
105 #else
106 #define MONO_NEVER_INLINE
107 #endif
108
109 #ifdef __GNUC__
110 #define MONO_COLD __attribute__ ((__cold__))
111 #else
112 #define MONO_COLD
113 #endif
114
115 #if defined (__GNUC__) && defined (__GNUC_MINOR__) && defined (__GNUC_PATCHLEVEL__)
116 #define MONO_GNUC_VERSION (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__)
117 #endif
118
119 #if defined(__has_feature)
120 #if __has_feature(thread_sanitizer)
121 #define MONO_HAS_CLANG_THREAD_SANITIZER 1
122 #else
123 #define MONO_HAS_CLANG_THREAD_SANITIZER 0
124 #endif
125 #else
126 #define MONO_HAS_CLANG_THREAD_SANITIZER 0
127 #endif
128
129 /* Used to tell Clang's ThreadSanitizer to not report data races that occur within a certain function */
130 #if MONO_HAS_CLANG_THREAD_SANITIZER
131 #define MONO_NO_SANITIZE_THREAD __attribute__ ((no_sanitize("thread")))
132 #else
133 #define MONO_NO_SANITIZE_THREAD
134 #endif
135
136 /* Used when building with Android NDK's unified headers */
137 #if defined(HOST_ANDROID) && defined (ANDROID_UNIFIED_HEADERS)
138 #if __ANDROID_API__ < 21
139
140 typedef int32_t __mono_off32_t;
141
142 #ifdef HAVE_SYS_MMAN_H
143 #include <sys/mman.h>
144 #endif
145
146 #if !defined(mmap)
147 /* Unified headers before API 21 do not declare mmap when LARGE_FILES are used (via -D_FILE_OFFSET_BITS=64)
148  * which is always the case when Mono build targets Android. The problem here is that the unified headers
149  * map `mmap` to `mmap64` if large files are enabled but this api exists only in API21 onwards. Therefore
150  * we must carefully declare the 32-bit mmap here without changing the ABI along the way. Carefully because
151  * in this instance off_t is redeclared to be 64-bit and that's not what we want.
152  */
153 void* mmap (void*, size_t, int, int, int, __mono_off32_t);
154 #endif /* !mmap */
155
156 #ifdef HAVE_SYS_SENDFILE_H
157 #include <sys/sendfile.h>
158 #endif
159
160 #if !defined(sendfile)
161 /* The same thing as with mmap happens with sendfile */
162 ssize_t sendfile (int out_fd, int in_fd, __mono_off32_t* offset, size_t count);
163 #endif /* !sendfile */
164
165 #endif /* __ANDROID_API__ < 21 */
166 #endif /* HOST_ANDROID && ANDROID_UNIFIED_HEADERS */
167
168 #endif /* __UTILS_MONO_COMPILER_H__*/
169