[netcore] Add libc name transition
[mono-project.git] / mono / utils / mono-compiler.h
blob323c7cc201c7eb18b1d8b58d96b40a2b9209040d
1 /**
2 * \file
3 */
5 #ifndef __UTILS_MONO_COMPILER_H__
6 #define __UTILS_MONO_COMPILER_H__
8 /*
9 * This file includes macros used in the runtime to encapsulate different
10 * compiler behaviours.
12 #include <config.h>
13 #if defined(HAVE_UNISTD_H)
14 #include <unistd.h>
15 #endif
17 #ifdef __GNUC__
18 #define MONO_ATTR_USED __attribute__ ((__used__))
19 #else
20 #define MONO_ATTR_USED
21 #endif
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
29 /* Deal with Microsoft C compiler differences */
30 #ifdef _MSC_VER
32 #include <math.h>
33 #include <float.h>
35 #define popen _popen
36 #define pclose _pclose
37 #include <direct.h>
38 #define mkdir(x) _mkdir(x)
40 #define __func__ __FUNCTION__
42 #include <stddef.h>
43 #include <stdint.h>
45 // ssize_t and SSIZE_MAX are Posix, define for Windows.
46 typedef ptrdiff_t ssize_t;
47 #ifndef SSIZE_MAX
48 #define SSIZE_MAX INTPTR_MAX
49 #endif
51 #endif /* _MSC_VER */
53 // Quiet Visual Studio linker warning, LNK4221: This object file does not define any previously
54 // undefined public symbols, so it will not be used by any link operation that consumes this library.
55 // And other linkers, e.g. older Apple.
56 #define MONO_EMPTY_SOURCE_FILE(x) extern const char mono_quash_linker_empty_file_warning_ ## x; \
57 const char mono_quash_linker_empty_file_warning_ ## x = 0;
59 #ifdef _MSC_VER
60 #define MONO_PRAGMA_WARNING_PUSH() __pragma(warning (push))
61 #define MONO_PRAGMA_WARNING_DISABLE(x) __pragma(warning (disable:x))
62 #define MONO_PRAGMA_WARNING_POP() __pragma(warning (pop))
64 #define MONO_DISABLE_WARNING(x) \
65 MONO_PRAGMA_WARNING_PUSH() \
66 MONO_PRAGMA_WARNING_DISABLE(x)
68 #define MONO_RESTORE_WARNING \
69 MONO_PRAGMA_WARNING_POP()
70 #else
71 #define MONO_PRAGMA_WARNING_PUSH()
72 #define MONO_PRAGMA_WARNING_DISABLE(x)
73 #define MONO_PRAGMA_WARNING_POP()
74 #define MONO_DISABLE_WARNING(x)
75 #define MONO_RESTORE_WARNING
76 #endif
78 // If MONO_LLVM_INTERNAL changes, update mono_debug_method_lookup_location declaration.
79 #if !defined(_MSC_VER) && !defined(HOST_SOLARIS) && !defined(_WIN32) && !defined(__CYGWIN__) && !defined(MONOTOUCH) && HAVE_VISIBILITY_HIDDEN
80 #if MONO_LLVM_LOADED
81 #define MONO_LLVM_INTERNAL MONO_API_NO_EXTERN_C
82 #else
83 #define MONO_LLVM_INTERNAL
84 #endif
85 #else
86 #define MONO_LLVM_INTERNAL
87 #endif
89 /* Used to mark internal functions used by the profiler modules */
90 #define MONO_PROFILER_API MONO_API
92 /* Used to mark internal functions used by the CoreFX PAL library */
93 #define MONO_PAL_API MONO_API
95 #ifdef __GNUC__
96 #define MONO_ALWAYS_INLINE __attribute__ ((__always_inline__))
97 #elif defined(_MSC_VER)
98 #define MONO_ALWAYS_INLINE __forceinline
99 #else
100 #define MONO_ALWAYS_INLINE
101 #endif
103 #ifdef __GNUC__
104 #define MONO_NEVER_INLINE __attribute__ ((__noinline__))
105 #elif defined(_MSC_VER)
106 #define MONO_NEVER_INLINE __declspec(noinline)
107 #else
108 #define MONO_NEVER_INLINE
109 #endif
111 #ifdef __GNUC__
112 #define MONO_COLD __attribute__ ((__cold__))
113 #else
114 #define MONO_COLD
115 #endif
117 #if defined (__clang__)
118 #define MONO_NO_OPTIMIZATION __attribute__ ((optnone))
119 #elif __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 4)
120 #define MONO_NO_OPTIMIZATION __attribute__ ((optimize("O0")))
121 #else
122 #define MONO_NO_OPTIMIZATION /* nothing */
123 #endif
125 #if defined (__GNUC__) && defined (__GNUC_MINOR__) && defined (__GNUC_PATCHLEVEL__)
126 #define MONO_GNUC_VERSION (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__)
127 #endif
129 #if defined(__has_feature)
131 #if __has_feature(thread_sanitizer)
132 #define MONO_HAS_CLANG_THREAD_SANITIZER 1
133 #else
134 #define MONO_HAS_CLANG_THREAD_SANITIZER 0
135 #endif
137 #if __has_feature(address_sanitizer)
138 #define MONO_HAS_CLANG_ADDRESS_SANITIZER 1
139 #else
140 #define MONO_HAS_CLANG_ADDRESS_SANITIZER 0
141 #endif
143 #else
144 #define MONO_HAS_CLANG_THREAD_SANITIZER 0
145 #define MONO_HAS_CLANG_ADDRESS_SANITIZER 0
146 #endif
148 /* Used to tell Clang's ThreadSanitizer to not report data races that occur within a certain function */
149 #if MONO_HAS_CLANG_THREAD_SANITIZER
150 #define MONO_NO_SANITIZE_THREAD __attribute__ ((no_sanitize("thread")))
151 #else
152 #define MONO_NO_SANITIZE_THREAD
153 #endif
155 /* Used to tell Clang's AddressSanitizer to turn off instrumentation for a certain function */
156 #if MONO_HAS_CLANG_ADDRESS_SANITIZER
157 #define MONO_NO_SANITIZE_ADDRESS __attribute__ ((no_sanitize("address")))
158 #else
159 #define MONO_NO_SANITIZE_ADDRESS
160 #endif
162 /* Used when building with Android NDK's unified headers */
163 #if defined(HOST_ANDROID) && defined (ANDROID_UNIFIED_HEADERS)
164 #ifdef HAVE_ANDROID_NDK_VERSION_H
165 #include <android/ndk-version.h>
166 #endif
168 #if __ANDROID_API__ < 21
170 typedef int32_t __mono_off32_t;
172 #ifdef HAVE_SYS_MMAN_H
173 #include <sys/mman.h>
174 #endif
176 #if __NDK_MAJOR__ < 18
178 #ifdef __cplusplus
179 extern "C" {
180 #endif
182 /* Unified headers before API 21 do not declare mmap when LARGE_FILES are used (via -D_FILE_OFFSET_BITS=64)
183 * which is always the case when Mono build targets Android. The problem here is that the unified headers
184 * map `mmap` to `mmap64` if large files are enabled but this api exists only in API21 onwards. Therefore
185 * we must carefully declare the 32-bit mmap here without changing the ABI along the way. Carefully because
186 * in this instance off_t is redeclared to be 64-bit and that's not what we want.
188 void* mmap (void*, size_t, int, int, int, __mono_off32_t);
190 #ifdef __cplusplus
191 } // extern C
192 #endif
194 #endif /* __NDK_MAJOR__ < 18 */
196 #ifdef HAVE_SYS_SENDFILE_H
197 #include <sys/sendfile.h>
198 #endif
200 #ifdef __cplusplus
201 extern "C" {
202 #endif
204 /* Similar thing as with mmap happens with sendfile, except that the off_t is always used (and
205 * mono expects 64-bit offset here */
206 ssize_t sendfile (int out_fd, int in_fd, off_t* offset, size_t count);
208 #ifdef __cplusplus
209 } // extern C
210 #endif
212 #endif /* __ANDROID_API__ < 21 */
213 #endif /* HOST_ANDROID && ANDROID_UNIFIED_HEADERS */
215 #endif /* __UTILS_MONO_COMPILER_H__*/