Werror.mk 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. #
  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. # This sets WARNING_CFLAGS for gcc-like compilers.
  6. ifndef CC_IS_CLANG
  7. CC_IS_CLANG := $(and $(findstring clang, $(shell $(CC) --version 2>&1)), 1)
  8. # Export CC_IS_CLANG to save a shell invocation when recursing.
  9. export CC_IS_CLANG
  10. endif
  11. ifdef CC_IS_CLANG
  12. # Clang claims GCC 4.2.1 compatibility, see GCC_VERSION
  13. CC_IS_GCC = 1
  14. # Export CC_IS_GCC to save a shell invocation when recursing.
  15. export CC_IS_GCC
  16. endif
  17. ifndef CC_IS_GCC
  18. CC_IS_GCC := $(shell $(CC) -x c -E -Wall -Werror /dev/null >/dev/null 2>&1 && echo 1)
  19. # Export CC_IS_GCC to save a shell invocation when recursing.
  20. export CC_IS_GCC
  21. endif
  22. ifndef CC_NAME
  23. ifeq (1,$(CC_IS_GCC))
  24. CC_NAME := $(shell $(CC) -? 2>&1 >/dev/null | sed -e 's/:.*//;1q')
  25. else
  26. CC_NAME := $(notdir $(CC))
  27. endif
  28. # Export CC_NAME to save a shell invocation when recursing.
  29. export CC_NAME
  30. endif
  31. ifndef GCC_VERSION
  32. ifeq (1,$(CC_IS_GCC))
  33. GCC_VERSION := $(subst ., ,$(shell $(CC) -dumpversion || echo x.x.x))
  34. # Export GCC_VERSION to save a shell invocation when recursing.
  35. export GCC_VERSION
  36. endif
  37. endif
  38. ifndef WARNING_CFLAGS
  39. ifneq (1,$(CC_IS_GCC))
  40. WARNING_CFLAGS = $(NULL)
  41. else
  42. # This tests to see if enabling the warning is possible before
  43. # setting an option to disable it.
  44. set_warning = $(shell $(CC) -x c -E -Werror -W$(1) /dev/null >/dev/null 2>&1 && echo -W$(2)$(1))
  45. enable_warning = $(call set_warning,$(1),)
  46. disable_warning = $(call set_warning,$(1),no-)
  47. WARNING_CFLAGS = -Wall $(call enable_warning,shadow)
  48. ifdef CC_IS_CLANG
  49. # -Qunused-arguments : clang objects to arguments that it doesn't understand
  50. # and fixing this would require rearchitecture
  51. WARNING_CFLAGS += -Qunused-arguments
  52. # -Wno-parentheses-equality : because clang warns about macro expansions
  53. WARNING_CFLAGS += $(call disable_warning,parentheses-equality)
  54. ifdef BUILD_OPT
  55. # clang is unable to handle glib's expansion of strcmp and similar for optimized
  56. # builds, so ignore the resulting errors.
  57. # See https://llvm.org/bugs/show_bug.cgi?id=20144
  58. WARNING_CFLAGS += $(call disable_warning,array-bounds)
  59. WARNING_CFLAGS += $(call disable_warning,unevaluated-expression)
  60. endif
  61. endif # if clang
  62. ifndef NSS_ENABLE_WERROR
  63. ifeq ($(OS_TARGET),Android)
  64. # Android lollipop generates the following warning:
  65. # error: call to 'sprintf' declared with attribute warning:
  66. # sprintf is often misused; please use snprintf [-Werror]
  67. # So, just suppress -Werror entirely on Android
  68. NSS_ENABLE_WERROR = 0
  69. $(warning OS_TARGET is Android, disabling -Werror)
  70. else
  71. ifdef CC_IS_CLANG
  72. # Clang reports its version as an older gcc, but it's OK
  73. NSS_ENABLE_WERROR = 1
  74. else
  75. ifneq (,$(filter 4.8 4.9,$(word 1,$(GCC_VERSION)).$(word 2,$(GCC_VERSION))))
  76. NSS_ENABLE_WERROR = 1
  77. endif
  78. ifeq (,$(filter 0 1 2 3 4,$(word 1,$(GCC_VERSION))))
  79. NSS_ENABLE_WERROR = 1
  80. endif
  81. endif
  82. ifndef NSS_ENABLE_WERROR
  83. $(warning Unable to find gcc 4.8 or greater, disabling -Werror)
  84. NSS_ENABLE_WERROR = 0
  85. endif
  86. endif
  87. endif #ndef NSS_ENABLE_WERROR
  88. ifeq ($(NSS_ENABLE_WERROR),1)
  89. WARNING_CFLAGS += -Werror
  90. else
  91. # Old versions of gcc (< 4.8) don't support #pragma diagnostic in functions.
  92. # Use this to disable use of that #pragma and the warnings it suppresses.
  93. WARNING_CFLAGS += -DNSS_NO_GCC48
  94. endif
  95. endif
  96. export WARNING_CFLAGS
  97. endif # ndef WARNING_CFLAGS