• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2     platform.h - compiler and OS detection
3     Copyright (C) 2016-present, Przemyslaw Skibinski, Yann Collet
4 
5     This program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation; either version 2 of the License, or
8     (at your option) any later version.
9 
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14 
15     You should have received a copy of the GNU General Public License along
16     with this program; if not, write to the Free Software Foundation, Inc.,
17     51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19 
20 #ifndef PLATFORM_H_MODULE
21 #define PLATFORM_H_MODULE
22 
23 #if defined (__cplusplus)
24 extern "C" {
25 #endif
26 
27 
28 
29 /* **************************************
30 *  Compiler Options
31 ****************************************/
32 #if defined(_MSC_VER)
33 #  define _CRT_SECURE_NO_WARNINGS    /* Disable Visual Studio warning messages for fopen, strncpy, strerror */
34 #  if (_MSC_VER <= 1800)             /* (1800 = Visual Studio 2013) */
35 #    define _CRT_SECURE_NO_DEPRECATE /* VS2005 - must be declared before <io.h> and <windows.h> */
36 #    define snprintf sprintf_s       /* snprintf unsupported by Visual <= 2013 */
37 #  endif
38 #endif
39 
40 
41 /* **************************************
42 *  Detect 64-bit OS
43 *  http://nadeausoftware.com/articles/2012/02/c_c_tip_how_detect_processor_type_using_compiler_predefined_macros
44 ****************************************/
45 #if defined __ia64 || defined _M_IA64                                                                               /* Intel Itanium */ \
46   || defined __powerpc64__ || defined __ppc64__ || defined __PPC64__                                                /* POWER 64-bit */  \
47   || (defined __sparc && (defined __sparcv9 || defined __sparc_v9__ || defined __arch64__)) || defined __sparc64__  /* SPARC 64-bit */  \
48   || defined __x86_64__s || defined _M_X64                                                                          /* x86 64-bit */    \
49   || defined __arm64__ || defined __aarch64__ || defined __ARM64_ARCH_8__                                           /* ARM 64-bit */    \
50   || (defined __mips  && (__mips == 64 || __mips == 4 || __mips == 3))                                              /* MIPS 64-bit */   \
51   || defined _LP64 || defined __LP64__ /* NetBSD, OpenBSD */ || defined __64BIT__ /* AIX */ || defined _ADDR64 /* Cray */               \
52   || (defined __SIZEOF_POINTER__ && __SIZEOF_POINTER__ == 8) /* gcc */
53 #  if !defined(__64BIT__)
54 #    define __64BIT__  1
55 #  endif
56 #endif
57 
58 
59 /* *********************************************************
60 *  Turn on Large Files support (>4GB) for 32-bit Linux/Unix
61 ***********************************************************/
62 #if !defined(__64BIT__) || defined(__MINGW32__)       /* No point defining Large file for 64 bit but MinGW-w64 requires it */
63 #  if !defined(_FILE_OFFSET_BITS)
64 #    define _FILE_OFFSET_BITS 64                      /* turn off_t into a 64-bit type for ftello, fseeko */
65 #  endif
66 #  if !defined(_LARGEFILE_SOURCE)                     /* obsolete macro, replaced with _FILE_OFFSET_BITS */
67 #    define _LARGEFILE_SOURCE 1                       /* Large File Support extension (LFS) - fseeko, ftello */
68 #  endif
69 #  if defined(_AIX) || defined(__hpux)
70 #    define _LARGE_FILES                              /* Large file support on 32-bits AIX and HP-UX */
71 #  endif
72 #endif
73 
74 
75 /* ************************************************************
76 *  Detect POSIX version
77 *  PLATFORM_POSIX_VERSION = -1 for non-Unix e.g. Windows
78 *  PLATFORM_POSIX_VERSION = 0 for Unix-like non-POSIX
79 *  PLATFORM_POSIX_VERSION >= 1 is equal to found _POSIX_VERSION
80 ************************************************************** */
81 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)) /* UNIX-like OS */ \
82    || defined(__midipix__) || defined(__VMS))
83 #  if (defined(__APPLE__) && defined(__MACH__)) || defined(__SVR4) || defined(_AIX) || defined(__hpux) /* POSIX.1–2001 (SUSv3) conformant */ \
84      || defined(__DragonFly__) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__)  || defined(__MidnightBSD__) /* BSD distros */ \
85      || defined(__HAIKU__)
86 #    define PLATFORM_POSIX_VERSION 200112L
87 #  else
88 #    if defined(__linux__) || defined(__linux)
89 #      ifndef _POSIX_C_SOURCE
90 #        define _POSIX_C_SOURCE 200809L  /* use feature test macro */
91 #      endif
92 #    endif
93 #    include <unistd.h>  /* declares _POSIX_VERSION */
94 #    if defined(_POSIX_VERSION)  /* POSIX compliant */
95 #      define PLATFORM_POSIX_VERSION _POSIX_VERSION
96 #    else
97 #      define PLATFORM_POSIX_VERSION 0
98 #    endif
99 #  endif
100 #endif
101 #if !defined(PLATFORM_POSIX_VERSION)
102 #  define PLATFORM_POSIX_VERSION -1
103 #endif
104 
105 
106 /*-*********************************************
107 *  Detect if isatty() and fileno() are available
108 *********************************************** */
109 #if (defined(__linux__) && (PLATFORM_POSIX_VERSION >= 1)) || (PLATFORM_POSIX_VERSION >= 200112L) || defined(__DJGPP__)
110 #  include <unistd.h>   /* isatty */
111 #  define IS_CONSOLE(stdStream) isatty(fileno(stdStream))
112 #elif defined(MSDOS) || defined(OS2) || defined(__CYGWIN__)
113 #  include <io.h>       /* _isatty */
114 #  define IS_CONSOLE(stdStream) _isatty(_fileno(stdStream))
115 #elif defined(WIN32) || defined(_WIN32)
116 #  include <io.h>      /* _isatty */
117 #  include <windows.h> /* DeviceIoControl, HANDLE, FSCTL_SET_SPARSE */
118 #  include <stdio.h>   /* FILE */
119 static __inline int IS_CONSOLE(FILE* stdStream)
120 {
121     DWORD dummy;
122     return _isatty(_fileno(stdStream)) && GetConsoleMode((HANDLE)_get_osfhandle(_fileno(stdStream)), &dummy);
123 }
124 #else
125 #  define IS_CONSOLE(stdStream) 0
126 #endif
127 
128 
129 /******************************
130 *  OS-specific Includes
131 ***************************** */
132 #if defined(MSDOS) || defined(OS2) || defined(WIN32) || defined(_WIN32)
133 #  include <fcntl.h>   /* _O_BINARY */
134 #  include <io.h>      /* _setmode, _fileno, _get_osfhandle */
135 #  if !defined(__DJGPP__)
136 #    include <windows.h> /* DeviceIoControl, HANDLE, FSCTL_SET_SPARSE */
137 #    include <winioctl.h> /* FSCTL_SET_SPARSE */
138 #    define SET_BINARY_MODE(file) { int unused=_setmode(_fileno(file), _O_BINARY); (void)unused; }
139 #    define SET_SPARSE_FILE_MODE(file) { DWORD dw; DeviceIoControl((HANDLE) _get_osfhandle(_fileno(file)), FSCTL_SET_SPARSE, 0, 0, 0, 0, &dw, 0); }
140 #  else
141 #    define SET_BINARY_MODE(file) setmode(fileno(file), O_BINARY)
142 #    define SET_SPARSE_FILE_MODE(file)
143 #  endif
144 #else
145 #  define SET_BINARY_MODE(file)
146 #  define SET_SPARSE_FILE_MODE(file)
147 #endif
148 
149 
150 
151 #if defined (__cplusplus)
152 }
153 #endif
154 
155 #endif /* PLATFORM_H_MODULE */
156