Compiler.h 4.5 KB

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