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