platform.h 8.6 KB

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