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