blob: 4d2b9490e6df502d4d809f443159cec62b642dd2 [file] [log] [blame]
Yann Collet32fb4072017-08-18 16:52:05 -07001/*
W. Felix Handte5d693cc2022-12-20 12:49:47 -05002 * Copyright (c) Meta Platforms, Inc. and affiliates.
Przemyslaw Skibinski7a8a03c2016-12-21 15:08:44 +01003 * All rights reserved.
4 *
Yann Collet32fb4072017-08-18 16:52:05 -07005 * 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).
Yann Collet3128e032017-09-08 00:09:23 -07008 * You may select, at your option, one of the above-listed licenses.
Przemyslaw Skibinski7a8a03c2016-12-21 15:08:44 +01009 */
Przemyslaw Skibinskib3843af2016-12-16 14:13:15 +010010
11#ifndef PLATFORM_H_MODULE
12#define PLATFORM_H_MODULE
13
14#if defined (__cplusplus)
15extern "C" {
16#endif
17
Przemyslaw Skibinskiead350b2016-12-21 09:04:59 +010018
Przemyslaw Skibinski7a8a03c2016-12-21 15:08:44 +010019
20/* **************************************
21* Compiler Options
22****************************************/
23#if defined(_MSC_VER)
Yann Colletc857ee82017-11-24 16:44:28 -080024# define _CRT_SECURE_NO_WARNINGS /* Disable Visual Studio warning messages for fopen, strncpy, strerror */
W. Felix Handte45c49182021-03-09 01:24:11 -050025# define _CRT_NONSTDC_NO_WARNINGS /* Disable C4996 complaining about posix function names */
Yann Colletc857ee82017-11-24 16:44:28 -080026# 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 */
Przemyslaw Skibinski7a8a03c2016-12-21 15:08:44 +010029# endif
Yann Collet1fdba692018-12-25 15:04:49 -080030# pragma warning(disable : 4127) /* disable: C4127: conditional expression is constant */
Przemyslaw Skibinski7a8a03c2016-12-21 15:08:44 +010031#endif
32
33
Przemyslaw Skibinskib3843af2016-12-16 14:13:15 +010034/* **************************************
Przemyslaw Skibinskiead350b2016-12-21 09:04:59 +010035* Detect 64-bit OS
Danielle Rozenblit4dffc352022-12-14 06:58:35 -080036* https://nadeausoftware.com/articles/2012/02/c_c_tip_how_detect_processor_type_using_compiler_predefined_macros
Przemyslaw Skibinskib866e722016-12-16 14:24:01 +010037****************************************/
Przemyslaw Skibinskiead350b2016-12-21 09:04:59 +010038#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***********************************************************/
Yann Collet4a85b122018-10-03 15:34:41 -070055#if !defined(__64BIT__) || defined(__MINGW32__) /* No point defining Large file for 64 bit but MinGW-w64 requires it */
Przemyslaw Skibinski35bf23c2017-02-13 13:57:29 +010056# if !defined(_FILE_OFFSET_BITS)
Yann Collet4a85b122018-10-03 15:34:41 -070057# define _FILE_OFFSET_BITS 64 /* turn off_t into a 64-bit type for ftello, fseeko */
Przemyslaw Skibinskiead350b2016-12-21 09:04:59 +010058# endif
Yann Collet4a85b122018-10-03 15:34:41 -070059# if !defined(_LARGEFILE_SOURCE) /* obsolete macro, replaced with _FILE_OFFSET_BITS */
60# define _LARGEFILE_SOURCE 1 /* Large File Support extension (LFS) - fseeko, ftello */
Przemyslaw Skibinskiead350b2016-12-21 09:04:59 +010061# endif
62# if defined(_AIX) || defined(__hpux)
Yann Collet4a85b122018-10-03 15:34:41 -070063# define _LARGE_FILES /* Large file support on 32-bits AIX and HP-UX */
Przemyslaw Skibinskib3843af2016-12-16 14:13:15 +010064# endif
65#endif
66
67
Przemyslaw Skibinski3cdfe262016-12-16 15:00:50 +010068/* ************************************************************
Przemyslaw Skibinskib866e722016-12-16 14:24:01 +010069* Detect POSIX version
Yann Collet549c19b2018-10-03 14:54:33 -070070* 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
Przemyslaw Skibinski3cdfe262016-12-16 15:00:50 +010074***************************************************************/
Yann Collet549c19b2018-10-03 14:54:33 -070075#ifndef PLATFORM_POSIX_VERSION
76
W. Felix Handtef99a4502024-03-11 15:20:06 -040077# if (defined(__APPLE__) && defined(__MACH__)) || defined(__SVR4) || defined(_AIX) || defined(__hpux) /* POSIX.1-2001 (SUSv3) conformant */
Yann Collet549c19b2018-10-03 14:54:33 -070078 /* exception rule : force posix version to 200112L,
79 * note: it's better to use unistd.h's _POSIX_VERSION whenever possible */
Przemyslaw Skibinskib3843af2016-12-16 14:13:15 +010080# define PLATFORM_POSIX_VERSION 200112L
Yann Collet549c19b2018-10-03 14:54:33 -070081
Danielle Rozenblit4dffc352022-12-14 06:58:35 -080082/* try to determine posix version through official unistd.h's _POSIX_VERSION (https://pubs.opengroup.org/onlinepubs/7908799/xsh/unistd.h.html).
Yann Collet549c19b2018-10-03 14:54:33 -070083 * note : there is no simple way to know in advance if <unistd.h> is present or not on target system,
84 * Posix specification mandates its presence and its content, but target system must respect this spec.
85 * It's necessary to _not_ #include <unistd.h> whenever target OS is not unix-like
86 * otherwise it will block preprocessing stage.
87 * The following list of build macros tries to "guess" if target OS is likely unix-like, and therefore can #include <unistd.h>
88 */
89# elif !defined(_WIN32) \
Yann Collet4b8185c2019-04-10 13:26:27 -070090 && ( defined(__unix__) || defined(__unix) \
klausholstjacobsen839c7932023-09-03 10:10:23 +020091 || defined(_QNX_SOURCE) || defined(__midipix__) || defined(__VMS) || defined(__HAIKU__) )
Yann Collet549c19b2018-10-03 14:54:33 -070092
Christoph Reiterddd4c392020-01-11 01:04:14 +010093# if defined(__linux__) || defined(__linux) || defined(__CYGWIN__)
Nick Terrelld0801982017-02-09 14:20:52 -080094# ifndef _POSIX_C_SOURCE
Rosen Penev41e90652019-07-30 17:17:07 -070095# define _POSIX_C_SOURCE 200809L /* feature test macro : https://www.gnu.org/software/libc/manual/html_node/Feature-Test-Macros.html */
Nick Terrella0f90062017-02-08 17:25:01 -080096# endif
Przemyslaw Skibinskib3843af2016-12-16 14:13:15 +010097# endif
98# include <unistd.h> /* declares _POSIX_VERSION */
99# if defined(_POSIX_VERSION) /* POSIX compliant */
100# define PLATFORM_POSIX_VERSION _POSIX_VERSION
Przemyslaw Skibinskib866e722016-12-16 14:24:01 +0100101# else
Yann Collet549c19b2018-10-03 14:54:33 -0700102# define PLATFORM_POSIX_VERSION 1
Przemyslaw Skibinskib3843af2016-12-16 14:13:15 +0100103# endif
Przemyslaw Skibinskib3843af2016-12-16 14:13:15 +0100104
Yann Collet1dcc4782020-07-13 14:16:33 -0700105# ifdef __UCLIBC__
106# ifndef __USE_MISC
107# define __USE_MISC /* enable st_mtim on uclibc */
108# endif
109# endif
110
Yann Collet549c19b2018-10-03 14:54:33 -0700111# else /* non-unix target platform (like Windows) */
112# define PLATFORM_POSIX_VERSION 0
113# endif
114
115#endif /* PLATFORM_POSIX_VERSION */
Przemyslaw Skibinskib3843af2016-12-16 14:13:15 +0100116
Yann Collet4b8185c2019-04-10 13:26:27 -0700117
W. Felix Handtee2a99db2019-12-05 12:02:35 -0500118#if PLATFORM_POSIX_VERSION > 1
119 /* glibc < 2.26 may not expose struct timespec def without this.
120 * See issue #1920. */
121# ifndef _ATFILE_SOURCE
122# define _ATFILE_SOURCE
123# endif
124#endif
125
126
Przemyslaw Skibinskiead350b2016-12-21 09:04:59 +0100127/*-*********************************************
128* Detect if isatty() and fileno() are available
Nick Terrelle58a39f2022-01-14 12:37:32 -0800129*
130* Note: Use UTIL_isConsole() for the zstd CLI
131* instead, as it allows faking is console for
132* testing.
Przemyslaw Skibinskiead350b2016-12-21 09:04:59 +0100133************************************************/
Yann Collet549c19b2018-10-03 14:54:33 -0700134#if (defined(__linux__) && (PLATFORM_POSIX_VERSION > 1)) \
cyan4973f741fb82018-06-26 01:22:45 -0700135 || (PLATFORM_POSIX_VERSION >= 200112L) \
Christoph Reiterddd4c392020-01-11 01:04:14 +0100136 || defined(__DJGPP__)
Przemyslaw Skibinski2f6ccee2016-12-21 13:23:34 +0100137# include <unistd.h> /* isatty */
Christoph Reiterddd4c392020-01-11 01:04:14 +0100138# include <stdio.h> /* fileno */
Przemyslaw Skibinskiead350b2016-12-21 09:04:59 +0100139# define IS_CONSOLE(stdStream) isatty(fileno(stdStream))
Christoph Reiterddd4c392020-01-11 01:04:14 +0100140#elif defined(MSDOS) || defined(OS2)
Przemyslaw Skibinskiead350b2016-12-21 09:04:59 +0100141# include <io.h> /* _isatty */
142# define IS_CONSOLE(stdStream) _isatty(_fileno(stdStream))
Dimitri Papadopoulos585aaa02023-09-23 19:03:18 +0200143#elif defined(_WIN32)
Sean Purcell894bf492017-03-27 12:19:30 -0700144# include <io.h> /* _isatty */
145# include <windows.h> /* DeviceIoControl, HANDLE, FSCTL_SET_SPARSE */
146# include <stdio.h> /* FILE */
Yann Collet353117c2018-03-20 13:40:29 -0700147static __inline int IS_CONSOLE(FILE* stdStream) {
Sean Purcell894bf492017-03-27 12:19:30 -0700148 DWORD dummy;
149 return _isatty(_fileno(stdStream)) && GetConsoleMode((HANDLE)_get_osfhandle(_fileno(stdStream)), &dummy);
150}
Przemyslaw Skibinskiead350b2016-12-21 09:04:59 +0100151#else
152# define IS_CONSOLE(stdStream) 0
153#endif
154
155
156/******************************
Yann Colletc857ee82017-11-24 16:44:28 -0800157* OS-specific IO behaviors
Przemyslaw Skibinskiead350b2016-12-21 09:04:59 +0100158******************************/
Dimitri Papadopoulos585aaa02023-09-23 19:03:18 +0200159#if defined(MSDOS) || defined(OS2) || defined(_WIN32)
Przemyslaw Skibinskiead350b2016-12-21 09:04:59 +0100160# include <fcntl.h> /* _O_BINARY */
161# include <io.h> /* _setmode, _fileno, _get_osfhandle */
162# if !defined(__DJGPP__)
163# include <windows.h> /* DeviceIoControl, HANDLE, FSCTL_SET_SPARSE */
164# include <winioctl.h> /* FSCTL_SET_SPARSE */
Yann Colletc857ee82017-11-24 16:44:28 -0800165# define SET_BINARY_MODE(file) { int const unused=_setmode(_fileno(file), _O_BINARY); (void)unused; }
Przemyslaw Skibinskiead350b2016-12-21 09:04:59 +0100166# define SET_SPARSE_FILE_MODE(file) { DWORD dw; DeviceIoControl((HANDLE) _get_osfhandle(_fileno(file)), FSCTL_SET_SPARSE, 0, 0, 0, 0, &dw, 0); }
167# else
168# define SET_BINARY_MODE(file) setmode(fileno(file), O_BINARY)
169# define SET_SPARSE_FILE_MODE(file)
170# endif
171#else
172# define SET_BINARY_MODE(file)
173# define SET_SPARSE_FILE_MODE(file)
174#endif
175
176
Nick Terrell96fe5452017-03-31 15:16:43 -0700177#ifndef ZSTD_SPARSE_DEFAULT
178# if (defined(__APPLE__) && defined(__MACH__))
179# define ZSTD_SPARSE_DEFAULT 0
180# else
181# define ZSTD_SPARSE_DEFAULT 1
182# endif
183#endif
184
Przemyslaw Skibinskib866e722016-12-16 14:24:01 +0100185
Casey McGintyd4337b62018-09-11 11:39:49 -0700186#ifndef ZSTD_START_SYMBOLLIST_FRAME
187# ifdef __linux__
188# define ZSTD_START_SYMBOLLIST_FRAME 2
189# elif defined __APPLE__
190# define ZSTD_START_SYMBOLLIST_FRAME 4
191# else
192# define ZSTD_START_SYMBOLLIST_FRAME 0
193# endif
194#endif
195
196
Yann Collet549c19b2018-10-03 14:54:33 -0700197#ifndef ZSTD_SETPRIORITY_SUPPORT
Danielle Rozenblit4dffc352022-12-14 06:58:35 -0800198 /* mandates presence of <sys/resource.h> and support for setpriority() : https://man7.org/linux/man-pages/man2/setpriority.2.html */
Yann Collet549c19b2018-10-03 14:54:33 -0700199# define ZSTD_SETPRIORITY_SUPPORT (PLATFORM_POSIX_VERSION >= 200112L)
200#endif
201
202
203#ifndef ZSTD_NANOSLEEP_SUPPORT
Danielle Rozenblit4dffc352022-12-14 06:58:35 -0800204 /* mandates support of nanosleep() within <time.h> : https://man7.org/linux/man-pages/man2/nanosleep.2.html */
Yann Collet4a85b122018-10-03 15:34:41 -0700205# if (defined(__linux__) && (PLATFORM_POSIX_VERSION >= 199309L)) \
206 || (PLATFORM_POSIX_VERSION >= 200112L)
207# define ZSTD_NANOSLEEP_SUPPORT 1
208# else
209# define ZSTD_NANOSLEEP_SUPPORT 0
210# endif
Yann Collet549c19b2018-10-03 14:54:33 -0700211#endif
212
213
Przemyslaw Skibinskib3843af2016-12-16 14:13:15 +0100214#if defined (__cplusplus)
215}
216#endif
217
218#endif /* PLATFORM_H_MODULE */