127b27ec6Sopenharmony_ci/*
227b27ec6Sopenharmony_ci    platform.h - compiler and OS detection
327b27ec6Sopenharmony_ci    Copyright (C) 2016-2020, Przemyslaw Skibinski, Yann Collet
427b27ec6Sopenharmony_ci
527b27ec6Sopenharmony_ci    This program is free software; you can redistribute it and/or modify
627b27ec6Sopenharmony_ci    it under the terms of the GNU General Public License as published by
727b27ec6Sopenharmony_ci    the Free Software Foundation; either version 2 of the License, or
827b27ec6Sopenharmony_ci    (at your option) any later version.
927b27ec6Sopenharmony_ci
1027b27ec6Sopenharmony_ci    This program is distributed in the hope that it will be useful,
1127b27ec6Sopenharmony_ci    but WITHOUT ANY WARRANTY; without even the implied warranty of
1227b27ec6Sopenharmony_ci    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1327b27ec6Sopenharmony_ci    GNU General Public License for more details.
1427b27ec6Sopenharmony_ci
1527b27ec6Sopenharmony_ci    You should have received a copy of the GNU General Public License along
1627b27ec6Sopenharmony_ci    with this program; if not, write to the Free Software Foundation, Inc.,
1727b27ec6Sopenharmony_ci    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
1827b27ec6Sopenharmony_ci*/
1927b27ec6Sopenharmony_ci
2027b27ec6Sopenharmony_ci#ifndef PLATFORM_H_MODULE
2127b27ec6Sopenharmony_ci#define PLATFORM_H_MODULE
2227b27ec6Sopenharmony_ci
2327b27ec6Sopenharmony_ci#if defined (__cplusplus)
2427b27ec6Sopenharmony_ciextern "C" {
2527b27ec6Sopenharmony_ci#endif
2627b27ec6Sopenharmony_ci
2727b27ec6Sopenharmony_ci
2827b27ec6Sopenharmony_ci
2927b27ec6Sopenharmony_ci/* **************************************
3027b27ec6Sopenharmony_ci*  Compiler Options
3127b27ec6Sopenharmony_ci****************************************/
3227b27ec6Sopenharmony_ci#if defined(_MSC_VER)
3327b27ec6Sopenharmony_ci#  define _CRT_SECURE_NO_WARNINGS    /* Disable Visual Studio warning messages for fopen, strncpy, strerror */
3427b27ec6Sopenharmony_ci#  if (_MSC_VER <= 1800)             /* (1800 = Visual Studio 2013) */
3527b27ec6Sopenharmony_ci#    define _CRT_SECURE_NO_DEPRECATE /* VS2005 - must be declared before <io.h> and <windows.h> */
3627b27ec6Sopenharmony_ci#    define snprintf sprintf_s       /* snprintf unsupported by Visual <= 2013 */
3727b27ec6Sopenharmony_ci#  endif
3827b27ec6Sopenharmony_ci#endif
3927b27ec6Sopenharmony_ci
4027b27ec6Sopenharmony_ci
4127b27ec6Sopenharmony_ci/* **************************************
4227b27ec6Sopenharmony_ci*  Detect 64-bit OS
4327b27ec6Sopenharmony_ci*  http://nadeausoftware.com/articles/2012/02/c_c_tip_how_detect_processor_type_using_compiler_predefined_macros
4427b27ec6Sopenharmony_ci****************************************/
4527b27ec6Sopenharmony_ci#if defined __ia64 || defined _M_IA64                                                                               /* Intel Itanium */ \
4627b27ec6Sopenharmony_ci  || defined __powerpc64__ || defined __ppc64__ || defined __PPC64__                                                /* POWER 64-bit */  \
4727b27ec6Sopenharmony_ci  || (defined __sparc && (defined __sparcv9 || defined __sparc_v9__ || defined __arch64__)) || defined __sparc64__  /* SPARC 64-bit */  \
4827b27ec6Sopenharmony_ci  || defined __x86_64__s || defined _M_X64                                                                          /* x86 64-bit */    \
4927b27ec6Sopenharmony_ci  || defined __arm64__ || defined __aarch64__ || defined __ARM64_ARCH_8__                                           /* ARM 64-bit */    \
5027b27ec6Sopenharmony_ci  || (defined __mips  && (__mips == 64 || __mips == 4 || __mips == 3))                                              /* MIPS 64-bit */   \
5127b27ec6Sopenharmony_ci  || defined _LP64 || defined __LP64__ /* NetBSD, OpenBSD */ || defined __64BIT__ /* AIX */ || defined _ADDR64 /* Cray */               \
5227b27ec6Sopenharmony_ci  || (defined __SIZEOF_POINTER__ && __SIZEOF_POINTER__ == 8) /* gcc */
5327b27ec6Sopenharmony_ci#  if !defined(__64BIT__)
5427b27ec6Sopenharmony_ci#    define __64BIT__  1
5527b27ec6Sopenharmony_ci#  endif
5627b27ec6Sopenharmony_ci#endif
5727b27ec6Sopenharmony_ci
5827b27ec6Sopenharmony_ci
5927b27ec6Sopenharmony_ci/* *********************************************************
6027b27ec6Sopenharmony_ci*  Turn on Large Files support (>4GB) for 32-bit Linux/Unix
6127b27ec6Sopenharmony_ci***********************************************************/
6227b27ec6Sopenharmony_ci#if !defined(__64BIT__) || defined(__MINGW32__)       /* No point defining Large file for 64 bit but MinGW-w64 requires it */
6327b27ec6Sopenharmony_ci#  if !defined(_FILE_OFFSET_BITS)
6427b27ec6Sopenharmony_ci#    define _FILE_OFFSET_BITS 64                      /* turn off_t into a 64-bit type for ftello, fseeko */
6527b27ec6Sopenharmony_ci#  endif
6627b27ec6Sopenharmony_ci#  if !defined(_LARGEFILE_SOURCE)                     /* obsolete macro, replaced with _FILE_OFFSET_BITS */
6727b27ec6Sopenharmony_ci#    define _LARGEFILE_SOURCE 1                       /* Large File Support extension (LFS) - fseeko, ftello */
6827b27ec6Sopenharmony_ci#  endif
6927b27ec6Sopenharmony_ci#  if defined(_AIX) || defined(__hpux)
7027b27ec6Sopenharmony_ci#    define _LARGE_FILES                              /* Large file support on 32-bits AIX and HP-UX */
7127b27ec6Sopenharmony_ci#  endif
7227b27ec6Sopenharmony_ci#endif
7327b27ec6Sopenharmony_ci
7427b27ec6Sopenharmony_ci
7527b27ec6Sopenharmony_ci/* ************************************************************
7627b27ec6Sopenharmony_ci*  Detect POSIX version
7727b27ec6Sopenharmony_ci*  PLATFORM_POSIX_VERSION = -1 for non-Unix e.g. Windows
7827b27ec6Sopenharmony_ci*  PLATFORM_POSIX_VERSION = 0 for Unix-like non-POSIX
7927b27ec6Sopenharmony_ci*  PLATFORM_POSIX_VERSION >= 1 is equal to found _POSIX_VERSION
8027b27ec6Sopenharmony_ci************************************************************** */
8127b27ec6Sopenharmony_ci#if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)) /* UNIX-like OS */ \
8227b27ec6Sopenharmony_ci   || defined(__midipix__) || defined(__VMS))
8327b27ec6Sopenharmony_ci#  if (defined(__APPLE__) && defined(__MACH__)) || defined(__SVR4) || defined(_AIX) || defined(__hpux) /* POSIX.1-2001 (SUSv3) conformant */ \
8427b27ec6Sopenharmony_ci     || defined(__DragonFly__) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__)  || defined(__MidnightBSD__) /* BSD distros */ \
8527b27ec6Sopenharmony_ci     || defined(__HAIKU__)
8627b27ec6Sopenharmony_ci#    define PLATFORM_POSIX_VERSION 200112L
8727b27ec6Sopenharmony_ci#  else
8827b27ec6Sopenharmony_ci#    if defined(__linux__) || defined(__linux)
8927b27ec6Sopenharmony_ci#      ifndef _POSIX_C_SOURCE
9027b27ec6Sopenharmony_ci#        define _POSIX_C_SOURCE 200809L  /* use feature test macro */
9127b27ec6Sopenharmony_ci#      endif
9227b27ec6Sopenharmony_ci#    endif
9327b27ec6Sopenharmony_ci#    include <unistd.h>  /* declares _POSIX_VERSION */
9427b27ec6Sopenharmony_ci#    if defined(_POSIX_VERSION)  /* POSIX compliant */
9527b27ec6Sopenharmony_ci#      define PLATFORM_POSIX_VERSION _POSIX_VERSION
9627b27ec6Sopenharmony_ci#    else
9727b27ec6Sopenharmony_ci#      define PLATFORM_POSIX_VERSION 0
9827b27ec6Sopenharmony_ci#    endif
9927b27ec6Sopenharmony_ci#  endif
10027b27ec6Sopenharmony_ci#endif
10127b27ec6Sopenharmony_ci#if !defined(PLATFORM_POSIX_VERSION)
10227b27ec6Sopenharmony_ci#  define PLATFORM_POSIX_VERSION -1
10327b27ec6Sopenharmony_ci#endif
10427b27ec6Sopenharmony_ci
10527b27ec6Sopenharmony_ci
10627b27ec6Sopenharmony_ci/*-*********************************************
10727b27ec6Sopenharmony_ci*  Detect if isatty() and fileno() are available
10827b27ec6Sopenharmony_ci*********************************************** */
10927b27ec6Sopenharmony_ci#if (defined(__linux__) && (PLATFORM_POSIX_VERSION >= 1)) || (PLATFORM_POSIX_VERSION >= 200112L) || defined(__DJGPP__)
11027b27ec6Sopenharmony_ci#  include <unistd.h>   /* isatty */
11127b27ec6Sopenharmony_ci#  define IS_CONSOLE(stdStream) isatty(fileno(stdStream))
11227b27ec6Sopenharmony_ci#elif defined(MSDOS) || defined(OS2) || defined(__CYGWIN__)
11327b27ec6Sopenharmony_ci#  include <io.h>       /* _isatty */
11427b27ec6Sopenharmony_ci#  define IS_CONSOLE(stdStream) _isatty(_fileno(stdStream))
11527b27ec6Sopenharmony_ci#elif defined(WIN32) || defined(_WIN32)
11627b27ec6Sopenharmony_ci#  include <io.h>      /* _isatty */
11727b27ec6Sopenharmony_ci#  include <windows.h> /* DeviceIoControl, HANDLE, FSCTL_SET_SPARSE */
11827b27ec6Sopenharmony_ci#  include <stdio.h>   /* FILE */
11927b27ec6Sopenharmony_cistatic __inline int IS_CONSOLE(FILE* stdStream)
12027b27ec6Sopenharmony_ci{
12127b27ec6Sopenharmony_ci    DWORD dummy;
12227b27ec6Sopenharmony_ci    return _isatty(_fileno(stdStream)) && GetConsoleMode((HANDLE)_get_osfhandle(_fileno(stdStream)), &dummy);
12327b27ec6Sopenharmony_ci}
12427b27ec6Sopenharmony_ci#else
12527b27ec6Sopenharmony_ci#  define IS_CONSOLE(stdStream) 0
12627b27ec6Sopenharmony_ci#endif
12727b27ec6Sopenharmony_ci
12827b27ec6Sopenharmony_ci
12927b27ec6Sopenharmony_ci/******************************
13027b27ec6Sopenharmony_ci*  OS-specific Includes
13127b27ec6Sopenharmony_ci***************************** */
13227b27ec6Sopenharmony_ci#if defined(MSDOS) || defined(OS2) || defined(WIN32) || defined(_WIN32)
13327b27ec6Sopenharmony_ci#  include <fcntl.h>   /* _O_BINARY */
13427b27ec6Sopenharmony_ci#  include <io.h>      /* _setmode, _fileno, _get_osfhandle */
13527b27ec6Sopenharmony_ci#  if !defined(__DJGPP__)
13627b27ec6Sopenharmony_ci#    include <windows.h> /* DeviceIoControl, HANDLE, FSCTL_SET_SPARSE */
13727b27ec6Sopenharmony_ci#    include <winioctl.h> /* FSCTL_SET_SPARSE */
13827b27ec6Sopenharmony_ci#    define SET_BINARY_MODE(file) { int unused=_setmode(_fileno(file), _O_BINARY); (void)unused; }
13927b27ec6Sopenharmony_ci#    define SET_SPARSE_FILE_MODE(file) { DWORD dw; DeviceIoControl((HANDLE) _get_osfhandle(_fileno(file)), FSCTL_SET_SPARSE, 0, 0, 0, 0, &dw, 0); }
14027b27ec6Sopenharmony_ci#  else
14127b27ec6Sopenharmony_ci#    define SET_BINARY_MODE(file) setmode(fileno(file), O_BINARY)
14227b27ec6Sopenharmony_ci#    define SET_SPARSE_FILE_MODE(file)
14327b27ec6Sopenharmony_ci#  endif
14427b27ec6Sopenharmony_ci#else
14527b27ec6Sopenharmony_ci#  define SET_BINARY_MODE(file)
14627b27ec6Sopenharmony_ci#  define SET_SPARSE_FILE_MODE(file)
14727b27ec6Sopenharmony_ci#endif
14827b27ec6Sopenharmony_ci
14927b27ec6Sopenharmony_ci
15027b27ec6Sopenharmony_ci
15127b27ec6Sopenharmony_ci#if defined (__cplusplus)
15227b27ec6Sopenharmony_ci}
15327b27ec6Sopenharmony_ci#endif
15427b27ec6Sopenharmony_ci
15527b27ec6Sopenharmony_ci#endif /* PLATFORM_H_MODULE */
156