blob: a4d7850fd714113d3217baeb92ba141e44de8ae5 [file] [log] [blame]
Yann Collet32fb4072017-08-18 16:52:05 -07001/*
Yann Collet232d62b2017-08-21 11:24:32 -07002 * Copyright (c) 2016-present, Przemyslaw Skibinski, Yann Collet, Facebook, Inc.
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)
24# define _CRT_SECURE_NO_WARNINGS /* Disable Visual Studio warning messages for fopen, strncpy, strerror */
Nick Terrella0f90062017-02-08 17:25:01 -080025# define _CRT_SECURE_NO_DEPRECATE /* VS2005 - must be declared before <io.h> and <windows.h> */
Przemyslaw Skibinski7a8a03c2016-12-21 15:08:44 +010026# if (_MSC_VER <= 1800) /* (1800 = Visual Studio 2013) */
27# define snprintf sprintf_s /* snprintf unsupported by Visual <= 2013 */
28# endif
29#endif
30
31
Przemyslaw Skibinskib3843af2016-12-16 14:13:15 +010032/* **************************************
Przemyslaw Skibinskiead350b2016-12-21 09:04:59 +010033* Detect 64-bit OS
34* http://nadeausoftware.com/articles/2012/02/c_c_tip_how_detect_processor_type_using_compiler_predefined_macros
Przemyslaw Skibinskib866e722016-12-16 14:24:01 +010035****************************************/
Przemyslaw Skibinskiead350b2016-12-21 09:04:59 +010036#if defined __ia64 || defined _M_IA64 /* Intel Itanium */ \
37 || defined __powerpc64__ || defined __ppc64__ || defined __PPC64__ /* POWER 64-bit */ \
38 || (defined __sparc && (defined __sparcv9 || defined __sparc_v9__ || defined __arch64__)) || defined __sparc64__ /* SPARC 64-bit */ \
39 || defined __x86_64__s || defined _M_X64 /* x86 64-bit */ \
40 || defined __arm64__ || defined __aarch64__ || defined __ARM64_ARCH_8__ /* ARM 64-bit */ \
41 || (defined __mips && (__mips == 64 || __mips == 4 || __mips == 3)) /* MIPS 64-bit */ \
42 || defined _LP64 || defined __LP64__ /* NetBSD, OpenBSD */ || defined __64BIT__ /* AIX */ || defined _ADDR64 /* Cray */ \
43 || (defined __SIZEOF_POINTER__ && __SIZEOF_POINTER__ == 8) /* gcc */
44# if !defined(__64BIT__)
45# define __64BIT__ 1
46# endif
47#endif
48
49
50/* *********************************************************
51* Turn on Large Files support (>4GB) for 32-bit Linux/Unix
52***********************************************************/
Przemyslaw Skibinski35bf23c2017-02-13 13:57:29 +010053#if !defined(__64BIT__) || defined(__MINGW32__) /* No point defining Large file for 64 bit but MinGW-w64 requires it */
54# if !defined(_FILE_OFFSET_BITS)
Przemyslaw Skibinskiead350b2016-12-21 09:04:59 +010055# define _FILE_OFFSET_BITS 64 /* turn off_t into a 64-bit type for ftello, fseeko */
56# endif
57# if !defined(_LARGEFILE_SOURCE) /* obsolete macro, replaced with _FILE_OFFSET_BITS */
58# define _LARGEFILE_SOURCE 1 /* Large File Support extension (LFS) - fseeko, ftello */
59# endif
60# if defined(_AIX) || defined(__hpux)
61# define _LARGE_FILES /* Large file support on 32-bits AIX and HP-UX */
Przemyslaw Skibinskib3843af2016-12-16 14:13:15 +010062# endif
63#endif
64
65
Przemyslaw Skibinski3cdfe262016-12-16 15:00:50 +010066/* ************************************************************
Przemyslaw Skibinskib866e722016-12-16 14:24:01 +010067* Detect POSIX version
Przemyslaw Skibinski3cdfe262016-12-16 15:00:50 +010068* PLATFORM_POSIX_VERSION = -1 for non-Unix e.g. Windows
69* PLATFORM_POSIX_VERSION = 0 for Unix-like non-POSIX
70* PLATFORM_POSIX_VERSION >= 1 is equal to found _POSIX_VERSION
71***************************************************************/
Przemyslaw Skibinskiead350b2016-12-21 09:04:59 +010072#if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)) /* UNIX-like OS */ \
73 || defined(__midipix__) || defined(__VMS))
74# if (defined(__APPLE__) && defined(__MACH__)) || defined(__SVR4) || defined(_AIX) || defined(__hpux) /* POSIX.1–2001 (SUSv3) conformant */ \
75 || defined(__DragonFly__) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) /* BSD distros */
Przemyslaw Skibinskib3843af2016-12-16 14:13:15 +010076# define PLATFORM_POSIX_VERSION 200112L
77# else
78# if defined(__linux__) || defined(__linux)
Nick Terrelld0801982017-02-09 14:20:52 -080079# ifndef _POSIX_C_SOURCE
80# define _POSIX_C_SOURCE 200112L /* use feature test macro */
Nick Terrella0f90062017-02-08 17:25:01 -080081# endif
Przemyslaw Skibinskib3843af2016-12-16 14:13:15 +010082# endif
83# include <unistd.h> /* declares _POSIX_VERSION */
84# if defined(_POSIX_VERSION) /* POSIX compliant */
85# define PLATFORM_POSIX_VERSION _POSIX_VERSION
Przemyslaw Skibinskib866e722016-12-16 14:24:01 +010086# else
Przemyslaw Skibinski3cdfe262016-12-16 15:00:50 +010087# define PLATFORM_POSIX_VERSION 0
Przemyslaw Skibinskib3843af2016-12-16 14:13:15 +010088# endif
89# endif
90#endif
Przemyslaw Skibinskib3843af2016-12-16 14:13:15 +010091#if !defined(PLATFORM_POSIX_VERSION)
Przemyslaw Skibinski3cdfe262016-12-16 15:00:50 +010092# define PLATFORM_POSIX_VERSION -1
Przemyslaw Skibinskib3843af2016-12-16 14:13:15 +010093#endif
94
95
Przemyslaw Skibinskiead350b2016-12-21 09:04:59 +010096/*-*********************************************
97* Detect if isatty() and fileno() are available
98************************************************/
99#if (defined(__linux__) && (PLATFORM_POSIX_VERSION >= 1)) || (PLATFORM_POSIX_VERSION >= 200112L) || defined(__DJGPP__)
Przemyslaw Skibinski2f6ccee2016-12-21 13:23:34 +0100100# include <unistd.h> /* isatty */
Przemyslaw Skibinskiead350b2016-12-21 09:04:59 +0100101# define IS_CONSOLE(stdStream) isatty(fileno(stdStream))
Sean Purcell894bf492017-03-27 12:19:30 -0700102#elif defined(MSDOS) || defined(OS2) || defined(__CYGWIN__)
Przemyslaw Skibinskiead350b2016-12-21 09:04:59 +0100103# include <io.h> /* _isatty */
104# define IS_CONSOLE(stdStream) _isatty(_fileno(stdStream))
Sean Purcell894bf492017-03-27 12:19:30 -0700105#elif defined(WIN32) || defined(_WIN32)
106# include <io.h> /* _isatty */
107# include <windows.h> /* DeviceIoControl, HANDLE, FSCTL_SET_SPARSE */
108# include <stdio.h> /* FILE */
Sean Purcellefdaf8b2017-03-27 12:26:40 -0700109static __inline int IS_CONSOLE(FILE* stdStream)
Sean Purcell894bf492017-03-27 12:19:30 -0700110{
111 DWORD dummy;
112 return _isatty(_fileno(stdStream)) && GetConsoleMode((HANDLE)_get_osfhandle(_fileno(stdStream)), &dummy);
113}
Przemyslaw Skibinskiead350b2016-12-21 09:04:59 +0100114#else
115# define IS_CONSOLE(stdStream) 0
116#endif
117
118
119/******************************
120* OS-specific Includes
121******************************/
122#if defined(MSDOS) || defined(OS2) || defined(WIN32) || defined(_WIN32)
123# include <fcntl.h> /* _O_BINARY */
124# include <io.h> /* _setmode, _fileno, _get_osfhandle */
125# if !defined(__DJGPP__)
126# include <windows.h> /* DeviceIoControl, HANDLE, FSCTL_SET_SPARSE */
127# include <winioctl.h> /* FSCTL_SET_SPARSE */
128# define SET_BINARY_MODE(file) { int unused=_setmode(_fileno(file), _O_BINARY); (void)unused; }
129# define SET_SPARSE_FILE_MODE(file) { DWORD dw; DeviceIoControl((HANDLE) _get_osfhandle(_fileno(file)), FSCTL_SET_SPARSE, 0, 0, 0, 0, &dw, 0); }
130# else
131# define SET_BINARY_MODE(file) setmode(fileno(file), O_BINARY)
132# define SET_SPARSE_FILE_MODE(file)
133# endif
134#else
135# define SET_BINARY_MODE(file)
136# define SET_SPARSE_FILE_MODE(file)
137#endif
138
139
Nick Terrell96fe5452017-03-31 15:16:43 -0700140#ifndef ZSTD_SPARSE_DEFAULT
141# if (defined(__APPLE__) && defined(__MACH__))
142# define ZSTD_SPARSE_DEFAULT 0
143# else
144# define ZSTD_SPARSE_DEFAULT 1
145# endif
146#endif
147
Przemyslaw Skibinskib866e722016-12-16 14:24:01 +0100148
Przemyslaw Skibinskib3843af2016-12-16 14:13:15 +0100149#if defined (__cplusplus)
150}
151#endif
152
153#endif /* PLATFORM_H_MODULE */