2 * platform.h - compiler and OS detection
4 * Copyright (c) 2016-present, Przemyslaw Skibinski, Yann Collet, Facebook, Inc.
7 * This source code is licensed under the BSD-style license found in the
8 * LICENSE file in the root directory of this source tree. An additional grant
9 * of patent rights can be found in the PATENTS file in the same directory.
12 #ifndef PLATFORM_H_MODULE
13 #define PLATFORM_H_MODULE
15 #if defined (__cplusplus)
21 /* **************************************
23 ****************************************/
25 # define _CRT_SECURE_NO_WARNINGS /* Disable Visual Studio warning messages for fopen, strncpy, strerror */
26 # define _CRT_SECURE_NO_DEPRECATE /* VS2005 - must be declared before <io.h> and <windows.h> */
27 # if (_MSC_VER <= 1800) /* (1800 = Visual Studio 2013) */
28 # define snprintf sprintf_s /* snprintf unsupported by Visual <= 2013 */
33 /* **************************************
35 * http://nadeausoftware.com/articles/2012/02/c_c_tip_how_detect_processor_type_using_compiler_predefined_macros
36 ****************************************/
37 #if defined __ia64 || defined _M_IA64 /* Intel Itanium */ \
38 || defined __powerpc64__ || defined __ppc64__ || defined __PPC64__ /* POWER 64-bit */ \
39 || (defined __sparc && (defined __sparcv9 || defined __sparc_v9__ || defined __arch64__)) || defined __sparc64__ /* SPARC 64-bit */ \
40 || defined __x86_64__s || defined _M_X64 /* x86 64-bit */ \
41 || defined __arm64__ || defined __aarch64__ || defined __ARM64_ARCH_8__ /* ARM 64-bit */ \
42 || (defined __mips && (__mips == 64 || __mips == 4 || __mips == 3)) /* MIPS 64-bit */ \
43 || defined _LP64 || defined __LP64__ /* NetBSD, OpenBSD */ || defined __64BIT__ /* AIX */ || defined _ADDR64 /* Cray */ \
44 || (defined __SIZEOF_POINTER__ && __SIZEOF_POINTER__ == 8) /* gcc */
45 # if !defined(__64BIT__)
51 /* *********************************************************
52 * Turn on Large Files support (>4GB) for 32-bit Linux/Unix
53 ***********************************************************/
54 #if !defined(__64BIT__) || defined(__MINGW32__) /* No point defining Large file for 64 bit but MinGW-w64 requires it */
55 # if !defined(_FILE_OFFSET_BITS)
56 # define _FILE_OFFSET_BITS 64 /* turn off_t into a 64-bit type for ftello, fseeko */
58 # if !defined(_LARGEFILE_SOURCE) /* obsolete macro, replaced with _FILE_OFFSET_BITS */
59 # define _LARGEFILE_SOURCE 1 /* Large File Support extension (LFS) - fseeko, ftello */
61 # if defined(_AIX) || defined(__hpux)
62 # define _LARGE_FILES /* Large file support on 32-bits AIX and HP-UX */
67 /* ************************************************************
68 * Detect POSIX version
69 * PLATFORM_POSIX_VERSION = -1 for non-Unix e.g. Windows
70 * PLATFORM_POSIX_VERSION = 0 for Unix-like non-POSIX
71 * PLATFORM_POSIX_VERSION >= 1 is equal to found _POSIX_VERSION
72 ***************************************************************/
73 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)) /* UNIX-like OS */ \
74 || defined(__midipix__) || defined(__VMS))
75 # if (defined(__APPLE__) && defined(__MACH__)) || defined(__SVR4) || defined(_AIX) || defined(__hpux) /* POSIX.1–2001 (SUSv3) conformant */ \
76 || defined(__DragonFly__) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) /* BSD distros */
77 # define PLATFORM_POSIX_VERSION 200112L
79 # if defined(__linux__) || defined(__linux)
80 # ifndef _POSIX_C_SOURCE
81 # define _POSIX_C_SOURCE 200112L /* use feature test macro */
84 # include <unistd.h> /* declares _POSIX_VERSION */
85 # if defined(_POSIX_VERSION) /* POSIX compliant */
86 # define PLATFORM_POSIX_VERSION _POSIX_VERSION
88 # define PLATFORM_POSIX_VERSION 0
92 #if !defined(PLATFORM_POSIX_VERSION)
93 # define PLATFORM_POSIX_VERSION -1
97 /*-*********************************************
98 * Detect if isatty() and fileno() are available
99 ************************************************/
100 #if (defined(__linux__) && (PLATFORM_POSIX_VERSION >= 1)) || (PLATFORM_POSIX_VERSION >= 200112L) || defined(__DJGPP__)
101 # include <unistd.h> /* isatty */
102 # define IS_CONSOLE(stdStream) isatty(fileno(stdStream))
103 #elif defined(MSDOS) || defined(OS2) || defined(__CYGWIN__)
104 # include <io.h> /* _isatty */
105 # define IS_CONSOLE(stdStream) _isatty(_fileno(stdStream))
106 #elif defined(WIN32) || defined(_WIN32)
107 # include <io.h> /* _isatty */
108 # include <windows.h> /* DeviceIoControl, HANDLE, FSCTL_SET_SPARSE */
109 # include <stdio.h> /* FILE */
110 static __inline int IS_CONSOLE(FILE* stdStream)
113 return _isatty(_fileno(stdStream)) && GetConsoleMode((HANDLE)_get_osfhandle(_fileno(stdStream)), &dummy);
116 # define IS_CONSOLE(stdStream) 0
120 /******************************
121 * OS-specific Includes
122 ******************************/
123 #if defined(MSDOS) || defined(OS2) || defined(WIN32) || defined(_WIN32)
124 # include <fcntl.h> /* _O_BINARY */
125 # include <io.h> /* _setmode, _fileno, _get_osfhandle */
126 # if !defined(__DJGPP__)
127 # include <windows.h> /* DeviceIoControl, HANDLE, FSCTL_SET_SPARSE */
128 # include <winioctl.h> /* FSCTL_SET_SPARSE */
129 # define SET_BINARY_MODE(file) { int unused=_setmode(_fileno(file), _O_BINARY); (void)unused; }
130 # define SET_SPARSE_FILE_MODE(file) { DWORD dw; DeviceIoControl((HANDLE) _get_osfhandle(_fileno(file)), FSCTL_SET_SPARSE, 0, 0, 0, 0, &dw, 0); }
132 # define SET_BINARY_MODE(file) setmode(fileno(file), O_BINARY)
133 # define SET_SPARSE_FILE_MODE(file)
136 # define SET_BINARY_MODE(file)
137 # define SET_SPARSE_FILE_MODE(file)
141 #ifndef ZSTD_SPARSE_DEFAULT
142 # if (defined(__APPLE__) && defined(__MACH__))
143 # define ZSTD_SPARSE_DEFAULT 0
145 # define ZSTD_SPARSE_DEFAULT 1
150 #if defined (__cplusplus)
154 #endif /* PLATFORM_H_MODULE */