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