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