648db22b |
1 | /* |
2 | * Copyright (c) Meta Platforms, Inc. and affiliates. |
3 | * All rights reserved. |
4 | * |
5 | * This source code is licensed under both the BSD-style license (found in the |
6 | * LICENSE file in the root directory of this source tree) and the GPLv2 (found |
7 | * in the COPYING file in the root directory of this source tree). |
8 | * You may select, at your option, one of the above-listed licenses. |
9 | */ |
10 | |
11 | #ifndef PLATFORM_H_MODULE |
12 | #define PLATFORM_H_MODULE |
13 | |
14 | #if defined (__cplusplus) |
15 | extern "C" { |
16 | #endif |
17 | |
18 | |
19 | |
20 | /* ************************************** |
21 | * Compiler Options |
22 | ****************************************/ |
23 | #if defined(_MSC_VER) |
24 | # define _CRT_SECURE_NO_WARNINGS /* Disable Visual Studio warning messages for fopen, strncpy, strerror */ |
25 | # define _CRT_NONSTDC_NO_WARNINGS /* Disable C4996 complaining about posix function names */ |
26 | # if (_MSC_VER <= 1800) /* 1800 == Visual Studio 2013 */ |
27 | # define _CRT_SECURE_NO_DEPRECATE /* VS2005 - must be declared before <io.h> and <windows.h> */ |
28 | # define snprintf sprintf_s /* snprintf unsupported by Visual <= 2013 */ |
29 | # endif |
30 | # pragma warning(disable : 4127) /* disable: C4127: conditional expression is constant */ |
31 | #endif |
32 | |
33 | |
34 | /* ************************************** |
35 | * Detect 64-bit OS |
36 | * https://nadeausoftware.com/articles/2012/02/c_c_tip_how_detect_processor_type_using_compiler_predefined_macros |
37 | ****************************************/ |
38 | #if defined __ia64 || defined _M_IA64 /* Intel Itanium */ \ |
39 | || defined __powerpc64__ || defined __ppc64__ || defined __PPC64__ /* POWER 64-bit */ \ |
40 | || (defined __sparc && (defined __sparcv9 || defined __sparc_v9__ || defined __arch64__)) || defined __sparc64__ /* SPARC 64-bit */ \ |
41 | || defined __x86_64__s || defined _M_X64 /* x86 64-bit */ \ |
42 | || defined __arm64__ || defined __aarch64__ || defined __ARM64_ARCH_8__ /* ARM 64-bit */ \ |
43 | || (defined __mips && (__mips == 64 || __mips == 4 || __mips == 3)) /* MIPS 64-bit */ \ |
44 | || defined _LP64 || defined __LP64__ /* NetBSD, OpenBSD */ || defined __64BIT__ /* AIX */ || defined _ADDR64 /* Cray */ \ |
45 | || (defined __SIZEOF_POINTER__ && __SIZEOF_POINTER__ == 8) /* gcc */ |
46 | # if !defined(__64BIT__) |
47 | # define __64BIT__ 1 |
48 | # endif |
49 | #endif |
50 | |
51 | |
52 | /* ********************************************************* |
53 | * Turn on Large Files support (>4GB) for 32-bit Linux/Unix |
54 | ***********************************************************/ |
55 | #if !defined(__64BIT__) || defined(__MINGW32__) /* No point defining Large file for 64 bit but MinGW-w64 requires it */ |
56 | # if !defined(_FILE_OFFSET_BITS) |
57 | # define _FILE_OFFSET_BITS 64 /* turn off_t into a 64-bit type for ftello, fseeko */ |
58 | # endif |
59 | # if !defined(_LARGEFILE_SOURCE) /* obsolete macro, replaced with _FILE_OFFSET_BITS */ |
60 | # define _LARGEFILE_SOURCE 1 /* Large File Support extension (LFS) - fseeko, ftello */ |
61 | # endif |
62 | # if defined(_AIX) || defined(__hpux) |
63 | # define _LARGE_FILES /* Large file support on 32-bits AIX and HP-UX */ |
64 | # endif |
65 | #endif |
66 | |
67 | |
68 | /* ************************************************************ |
69 | * Detect POSIX version |
70 | * PLATFORM_POSIX_VERSION = 0 for non-Unix e.g. Windows |
71 | * PLATFORM_POSIX_VERSION = 1 for Unix-like but non-POSIX |
72 | * PLATFORM_POSIX_VERSION > 1 is equal to found _POSIX_VERSION |
73 | * Value of PLATFORM_POSIX_VERSION can be forced on command line |
74 | ***************************************************************/ |
75 | #ifndef PLATFORM_POSIX_VERSION |
76 | |
77 | # if (defined(__APPLE__) && defined(__MACH__)) || defined(__SVR4) || defined(_AIX) || defined(__hpux) /* POSIX.1-2001 (SUSv3) conformant */ \ |
78 | || defined(__DragonFly__) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) /* BSD distros */ |
79 | /* exception rule : force posix version to 200112L, |
80 | * note: it's better to use unistd.h's _POSIX_VERSION whenever possible */ |
81 | # define PLATFORM_POSIX_VERSION 200112L |
82 | |
83 | /* try to determine posix version through official unistd.h's _POSIX_VERSION (https://pubs.opengroup.org/onlinepubs/7908799/xsh/unistd.h.html). |
84 | * note : there is no simple way to know in advance if <unistd.h> is present or not on target system, |
85 | * Posix specification mandates its presence and its content, but target system must respect this spec. |
86 | * It's necessary to _not_ #include <unistd.h> whenever target OS is not unix-like |
87 | * otherwise it will block preprocessing stage. |
88 | * The following list of build macros tries to "guess" if target OS is likely unix-like, and therefore can #include <unistd.h> |
89 | */ |
90 | # elif !defined(_WIN32) \ |
91 | && ( defined(__unix__) || defined(__unix) \ |
92 | || defined(__midipix__) || defined(__VMS) || defined(__HAIKU__) ) |
93 | |
94 | # if defined(__linux__) || defined(__linux) || defined(__CYGWIN__) |
95 | # ifndef _POSIX_C_SOURCE |
96 | # define _POSIX_C_SOURCE 200809L /* feature test macro : https://www.gnu.org/software/libc/manual/html_node/Feature-Test-Macros.html */ |
97 | # endif |
98 | # endif |
99 | # include <unistd.h> /* declares _POSIX_VERSION */ |
100 | # if defined(_POSIX_VERSION) /* POSIX compliant */ |
101 | # define PLATFORM_POSIX_VERSION _POSIX_VERSION |
102 | # else |
103 | # define PLATFORM_POSIX_VERSION 1 |
104 | # endif |
105 | |
106 | # ifdef __UCLIBC__ |
107 | # ifndef __USE_MISC |
108 | # define __USE_MISC /* enable st_mtim on uclibc */ |
109 | # endif |
110 | # endif |
111 | |
112 | # else /* non-unix target platform (like Windows) */ |
113 | # define PLATFORM_POSIX_VERSION 0 |
114 | # endif |
115 | |
116 | #endif /* PLATFORM_POSIX_VERSION */ |
117 | |
118 | |
119 | #if PLATFORM_POSIX_VERSION > 1 |
120 | /* glibc < 2.26 may not expose struct timespec def without this. |
121 | * See issue #1920. */ |
122 | # ifndef _ATFILE_SOURCE |
123 | # define _ATFILE_SOURCE |
124 | # endif |
125 | #endif |
126 | |
127 | |
128 | /*-********************************************* |
129 | * Detect if isatty() and fileno() are available |
130 | * |
131 | * Note: Use UTIL_isConsole() for the zstd CLI |
132 | * instead, as it allows faking is console for |
133 | * testing. |
134 | ************************************************/ |
135 | #if (defined(__linux__) && (PLATFORM_POSIX_VERSION > 1)) \ |
136 | || (PLATFORM_POSIX_VERSION >= 200112L) \ |
137 | || defined(__DJGPP__) |
138 | # include <unistd.h> /* isatty */ |
139 | # include <stdio.h> /* fileno */ |
140 | # define IS_CONSOLE(stdStream) isatty(fileno(stdStream)) |
141 | #elif defined(MSDOS) || defined(OS2) |
142 | # include <io.h> /* _isatty */ |
143 | # define IS_CONSOLE(stdStream) _isatty(_fileno(stdStream)) |
144 | #elif defined(WIN32) || defined(_WIN32) |
145 | # include <io.h> /* _isatty */ |
146 | # include <windows.h> /* DeviceIoControl, HANDLE, FSCTL_SET_SPARSE */ |
147 | # include <stdio.h> /* FILE */ |
148 | static __inline int IS_CONSOLE(FILE* stdStream) { |
149 | DWORD dummy; |
150 | return _isatty(_fileno(stdStream)) && GetConsoleMode((HANDLE)_get_osfhandle(_fileno(stdStream)), &dummy); |
151 | } |
152 | #else |
153 | # define IS_CONSOLE(stdStream) 0 |
154 | #endif |
155 | |
156 | |
157 | /****************************** |
158 | * OS-specific IO behaviors |
159 | ******************************/ |
160 | #if defined(MSDOS) || defined(OS2) || defined(WIN32) || defined(_WIN32) |
161 | # include <fcntl.h> /* _O_BINARY */ |
162 | # include <io.h> /* _setmode, _fileno, _get_osfhandle */ |
163 | # if !defined(__DJGPP__) |
164 | # include <windows.h> /* DeviceIoControl, HANDLE, FSCTL_SET_SPARSE */ |
165 | # include <winioctl.h> /* FSCTL_SET_SPARSE */ |
166 | # define SET_BINARY_MODE(file) { int const unused=_setmode(_fileno(file), _O_BINARY); (void)unused; } |
167 | # define SET_SPARSE_FILE_MODE(file) { DWORD dw; DeviceIoControl((HANDLE) _get_osfhandle(_fileno(file)), FSCTL_SET_SPARSE, 0, 0, 0, 0, &dw, 0); } |
168 | # else |
169 | # define SET_BINARY_MODE(file) setmode(fileno(file), O_BINARY) |
170 | # define SET_SPARSE_FILE_MODE(file) |
171 | # endif |
172 | #else |
173 | # define SET_BINARY_MODE(file) |
174 | # define SET_SPARSE_FILE_MODE(file) |
175 | #endif |
176 | |
177 | |
178 | #ifndef ZSTD_SPARSE_DEFAULT |
179 | # if (defined(__APPLE__) && defined(__MACH__)) |
180 | # define ZSTD_SPARSE_DEFAULT 0 |
181 | # else |
182 | # define ZSTD_SPARSE_DEFAULT 1 |
183 | # endif |
184 | #endif |
185 | |
186 | |
187 | #ifndef ZSTD_START_SYMBOLLIST_FRAME |
188 | # ifdef __linux__ |
189 | # define ZSTD_START_SYMBOLLIST_FRAME 2 |
190 | # elif defined __APPLE__ |
191 | # define ZSTD_START_SYMBOLLIST_FRAME 4 |
192 | # else |
193 | # define ZSTD_START_SYMBOLLIST_FRAME 0 |
194 | # endif |
195 | #endif |
196 | |
197 | |
198 | #ifndef ZSTD_SETPRIORITY_SUPPORT |
199 | /* mandates presence of <sys/resource.h> and support for setpriority() : https://man7.org/linux/man-pages/man2/setpriority.2.html */ |
200 | # define ZSTD_SETPRIORITY_SUPPORT (PLATFORM_POSIX_VERSION >= 200112L) |
201 | #endif |
202 | |
203 | |
204 | #ifndef ZSTD_NANOSLEEP_SUPPORT |
205 | /* mandates support of nanosleep() within <time.h> : https://man7.org/linux/man-pages/man2/nanosleep.2.html */ |
206 | # if (defined(__linux__) && (PLATFORM_POSIX_VERSION >= 199309L)) \ |
207 | || (PLATFORM_POSIX_VERSION >= 200112L) |
208 | # define ZSTD_NANOSLEEP_SUPPORT 1 |
209 | # else |
210 | # define ZSTD_NANOSLEEP_SUPPORT 0 |
211 | # endif |
212 | #endif |
213 | |
214 | |
215 | #if defined (__cplusplus) |
216 | } |
217 | #endif |
218 | |
219 | #endif /* PLATFORM_H_MODULE */ |