Compiler.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. /* Various compiler checks. */
  6. #ifndef mozilla_Compiler_h
  7. #define mozilla_Compiler_h
  8. #define MOZ_IS_GCC 0
  9. #define MOZ_IS_MSVC 0
  10. #if !defined(__clang__) && defined(__GNUC__)
  11. # undef MOZ_IS_GCC
  12. # define MOZ_IS_GCC 1
  13. /*
  14. * These macros should simplify gcc version checking. For example, to check
  15. * for gcc 4.7.1 or later, check `#if MOZ_GCC_VERSION_AT_LEAST(4, 7, 1)`.
  16. */
  17. # define MOZ_GCC_VERSION_AT_LEAST(major, minor, patchlevel) \
  18. ((__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__) \
  19. >= ((major) * 10000 + (minor) * 100 + (patchlevel)))
  20. # define MOZ_GCC_VERSION_AT_MOST(major, minor, patchlevel) \
  21. ((__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__) \
  22. <= ((major) * 10000 + (minor) * 100 + (patchlevel)))
  23. # if !MOZ_GCC_VERSION_AT_LEAST(4, 9, 0)
  24. # error "mfbt (and Goanna) require at least gcc 4.9 to build."
  25. # endif
  26. #elif defined(_MSC_VER)
  27. # undef MOZ_IS_MSVC
  28. # define MOZ_IS_MSVC 1
  29. #endif
  30. /*
  31. * The situation with standard libraries is a lot worse than with compilers,
  32. * particularly as clang and gcc could end up using one of three or so standard
  33. * libraries, and they may not be up-to-snuff with newer C++11 versions. To
  34. * detect the library, we're going to include cstddef (which is a small header
  35. * which will be transitively included by everybody else at some point) to grab
  36. * the version macros and deduce macros from there.
  37. */
  38. #ifdef __cplusplus
  39. # include <cstddef>
  40. # ifdef _STLPORT_MAJOR
  41. # define MOZ_USING_STLPORT 1
  42. # define MOZ_STLPORT_VERSION_AT_LEAST(major, minor, patch) \
  43. (_STLPORT_VERSION >= ((major) << 8 | (minor) << 4 | (patch)))
  44. # elif defined(_LIBCPP_VERSION)
  45. /*
  46. * libc++, unfortunately, doesn't appear to have useful versioning macros.
  47. * Hopefully, the recommendations of N3694 with respect to standard libraries
  48. * will get applied instead and we won't need to worry about version numbers
  49. * here.
  50. */
  51. # define MOZ_USING_LIBCXX 1
  52. # elif defined(__GLIBCXX__)
  53. # define MOZ_USING_LIBSTDCXX 1
  54. /*
  55. * libstdc++ is also annoying and doesn't give us useful versioning macros
  56. * for the library. If we're using gcc, then assume that libstdc++ matches
  57. * the compiler version. If we're using clang, we're going to have to fake
  58. * major/minor combinations by looking for newly-defined config macros.
  59. */
  60. # if MOZ_IS_GCC
  61. # define MOZ_LIBSTDCXX_VERSION_AT_LEAST(major, minor, patch) \
  62. MOZ_GCC_VERSION_AT_LEAST(major, minor, patch)
  63. # elif defined(_GLIBCXX_THROW_OR_ABORT)
  64. # define MOZ_LIBSTDCXX_VERSION_AT_LEAST(major, minor, patch) \
  65. ((major) < 4 || ((major) == 4 && (minor) <= 8))
  66. # elif defined(_GLIBCXX_NOEXCEPT)
  67. # define MOZ_LIBSTDCXX_VERSION_AT_LEAST(major, minor, patch) \
  68. ((major) < 4 || ((major) == 4 && (minor) <= 7))
  69. # elif defined(_GLIBCXX_USE_DEPRECATED)
  70. # define MOZ_LIBSTDCXX_VERSION_AT_LEAST(major, minor, patch) \
  71. ((major) < 4 || ((major) == 4 && (minor) <= 6))
  72. # elif defined(_GLIBCXX_PSEUDO_VISIBILITY)
  73. # define MOZ_LIBSTDCXX_VERSION_AT_LEAST(major, minor, patch) \
  74. ((major) < 4 || ((major) == 4 && (minor) <= 5))
  75. # elif defined(_GLIBCXX_BEGIN_EXTERN_C)
  76. # define MOZ_LIBSTDCXX_VERSION_AT_LEAST(major, minor, patch) \
  77. ((major) < 4 || ((major) == 4 && (minor) <= 4))
  78. # elif defined(_GLIBCXX_VISIBILITY_ATTR)
  79. # define MOZ_LIBSTDCXX_VERSION_AT_LEAST(major, minor, patch) \
  80. ((major) < 4 || ((major) == 4 && (minor) <= 3))
  81. # elif defined(_GLIBCXX_VISIBILITY)
  82. # define MOZ_LIBSTDCXX_VERSION_AT_LEAST(major, minor, patch) \
  83. ((major) < 4 || ((major) == 4 && (minor) <= 2))
  84. # else
  85. # error "Your version of libstdc++ is unknown to us and is likely too old."
  86. # endif
  87. # endif
  88. // Flesh out the defines for everyone else
  89. # ifndef MOZ_USING_STLPORT
  90. # define MOZ_USING_STLPORT 0
  91. # define MOZ_STLPORT_VERSION_AT_LEAST(major, minor, patch) 0
  92. # endif
  93. # ifndef MOZ_USING_LIBCXX
  94. # define MOZ_USING_LIBCXX 0
  95. # endif
  96. # ifndef MOZ_USING_LIBSTDCXX
  97. # define MOZ_USING_LIBSTDCXX 0
  98. # define MOZ_LIBSTDCXX_VERSION_AT_LEAST(major, minor, patch) 0
  99. # endif
  100. #endif /* __cplusplus */
  101. #endif /* mozilla_Compiler_h */