platform.h 6.7 KB

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