copts.py 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. """Abseil compiler options.
  2. This is the source of truth for Abseil compiler options. To modify Abseil
  3. compilation options:
  4. (1) Edit the appropriate list in this file based on the platform the flag is
  5. needed on.
  6. (2) Run `<path_to_absl>/copts/generate_copts.py`.
  7. The generated copts are consumed by configure_copts.bzl and
  8. AbseilConfigureCopts.cmake.
  9. """
  10. ABSL_GCC_FLAGS = [
  11. "-Wall",
  12. "-Wextra",
  13. "-Wcast-qual",
  14. "-Wconversion-null",
  15. "-Wformat-security",
  16. "-Wmissing-declarations",
  17. "-Wnon-virtual-dtor",
  18. "-Woverlength-strings",
  19. "-Wpointer-arith",
  20. "-Wundef",
  21. "-Wunused-local-typedefs",
  22. "-Wunused-result",
  23. "-Wvarargs",
  24. "-Wvla", # variable-length array
  25. "-Wwrite-strings",
  26. # Don't define min and max macros (Build on Windows using gcc)
  27. "-DNOMINMAX",
  28. ]
  29. ABSL_GCC_TEST_ADDITIONAL_FLAGS = [
  30. "-Wno-deprecated-declarations",
  31. "-Wno-missing-declarations",
  32. "-Wno-self-move",
  33. "-Wno-sign-compare",
  34. "-Wno-unused-function",
  35. "-Wno-unused-parameter",
  36. "-Wno-unused-private-field",
  37. ]
  38. ABSL_LLVM_FLAGS = [
  39. "-Wall",
  40. "-Wextra",
  41. "-Wc++98-compat-extra-semi",
  42. "-Wcast-qual",
  43. "-Wconversion",
  44. "-Wdeprecated-pragma",
  45. "-Wfloat-overflow-conversion",
  46. "-Wfloat-zero-conversion",
  47. "-Wfor-loop-analysis",
  48. "-Wformat-security",
  49. "-Wgnu-redeclared-enum",
  50. "-Winfinite-recursion",
  51. "-Winvalid-constexpr",
  52. "-Wliteral-conversion",
  53. "-Wmissing-declarations",
  54. "-Woverlength-strings",
  55. "-Wpointer-arith",
  56. "-Wself-assign",
  57. "-Wshadow-all",
  58. "-Wshorten-64-to-32",
  59. "-Wsign-conversion",
  60. "-Wstring-conversion",
  61. "-Wtautological-overlap-compare",
  62. "-Wtautological-unsigned-zero-compare",
  63. "-Wundef",
  64. "-Wuninitialized",
  65. "-Wunreachable-code",
  66. "-Wunused-comparison",
  67. "-Wunused-local-typedefs",
  68. "-Wunused-result",
  69. "-Wvla",
  70. "-Wwrite-strings",
  71. # Warnings that are enabled by group warning flags like -Wall that we
  72. # explicitly disable.
  73. "-Wno-float-conversion",
  74. "-Wno-implicit-float-conversion",
  75. "-Wno-implicit-int-float-conversion",
  76. # Disable warnings on unknown warning flags (when warning flags are
  77. # unknown on older compiler versions)
  78. "-Wno-unknown-warning-option",
  79. # Don't define min and max macros (Build on Windows using clang)
  80. "-DNOMINMAX",
  81. ]
  82. ABSL_LLVM_TEST_ADDITIONAL_FLAGS = [
  83. "-Wno-deprecated-declarations",
  84. "-Wno-implicit-int-conversion",
  85. "-Wno-missing-prototypes",
  86. "-Wno-missing-variable-declarations",
  87. "-Wno-shadow",
  88. "-Wno-shorten-64-to-32",
  89. "-Wno-sign-compare",
  90. "-Wno-sign-conversion",
  91. "-Wno-unreachable-code-loop-increment",
  92. "-Wno-unused-function",
  93. "-Wno-unused-member-function",
  94. "-Wno-unused-parameter",
  95. "-Wno-unused-private-field",
  96. "-Wno-unused-template",
  97. "-Wno-used-but-marked-unused",
  98. # gtest depends on this GNU extension being offered.
  99. "-Wno-gnu-zero-variadic-macro-arguments",
  100. ]
  101. # /Wall with msvc includes unhelpful warnings such as C4711, C4710, ...
  102. MSVC_BIG_WARNING_FLAGS = [
  103. "/W3",
  104. ]
  105. MSVC_WARNING_FLAGS = [
  106. # Increase the number of sections available in object files
  107. "/bigobj",
  108. "/wd4005", # macro-redefinition
  109. "/wd4068", # unknown pragma
  110. # qualifier applied to function type has no meaning; ignored
  111. "/wd4180",
  112. # conversion from 'type1' to 'type2', possible loss of data
  113. "/wd4244",
  114. # conversion from 'size_t' to 'type', possible loss of data
  115. "/wd4267",
  116. # The decorated name was longer than the compiler limit
  117. "/wd4503",
  118. # forcing value to bool 'true' or 'false' (performance warning)
  119. "/wd4800",
  120. ]
  121. MSVC_DEFINES = [
  122. "/DNOMINMAX", # Don't define min and max macros (windows.h)
  123. # Don't bloat namespace with incompatible winsock versions.
  124. "/DWIN32_LEAN_AND_MEAN",
  125. # Don't warn about usage of insecure C functions.
  126. "/D_CRT_SECURE_NO_WARNINGS",
  127. "/D_SCL_SECURE_NO_WARNINGS",
  128. # Introduced in VS 2017 15.8, allow overaligned types in aligned_storage
  129. "/D_ENABLE_EXTENDED_ALIGNED_STORAGE",
  130. ]
  131. def GccStyleFilterAndCombine(default_flags, test_flags):
  132. """Merges default_flags and test_flags for GCC and LLVM.
  133. Args:
  134. default_flags: A list of default compiler flags
  135. test_flags: A list of flags that are only used in tests
  136. Returns:
  137. A combined list of default_flags and test_flags, but with all flags of the
  138. form '-Wwarning' removed if test_flags contains a flag of the form
  139. '-Wno-warning'
  140. """
  141. remove = set(["-W" + f[5:] for f in test_flags if f[:5] == "-Wno-"])
  142. return [f for f in default_flags if f not in remove] + test_flags
  143. COPT_VARS = {
  144. "ABSL_GCC_FLAGS": ABSL_GCC_FLAGS,
  145. "ABSL_GCC_TEST_FLAGS": GccStyleFilterAndCombine(
  146. ABSL_GCC_FLAGS, ABSL_GCC_TEST_ADDITIONAL_FLAGS),
  147. "ABSL_LLVM_FLAGS": ABSL_LLVM_FLAGS,
  148. "ABSL_LLVM_TEST_FLAGS": GccStyleFilterAndCombine(
  149. ABSL_LLVM_FLAGS, ABSL_LLVM_TEST_ADDITIONAL_FLAGS),
  150. "ABSL_CLANG_CL_FLAGS":
  151. MSVC_BIG_WARNING_FLAGS + MSVC_DEFINES,
  152. "ABSL_CLANG_CL_TEST_FLAGS":
  153. MSVC_BIG_WARNING_FLAGS + MSVC_DEFINES + ABSL_LLVM_TEST_ADDITIONAL_FLAGS,
  154. "ABSL_MSVC_FLAGS":
  155. MSVC_BIG_WARNING_FLAGS + MSVC_WARNING_FLAGS + MSVC_DEFINES,
  156. "ABSL_MSVC_TEST_FLAGS":
  157. MSVC_BIG_WARNING_FLAGS + MSVC_WARNING_FLAGS + MSVC_DEFINES + [
  158. "/wd4018", # signed/unsigned mismatch
  159. "/wd4101", # unreferenced local variable
  160. "/wd4503", # decorated name length exceeded, name was truncated
  161. "/wd4996", # use of deprecated symbol
  162. "/DNOMINMAX", # disable the min() and max() macros from <windows.h>
  163. ],
  164. "ABSL_MSVC_LINKOPTS": [
  165. # Object file doesn't export any previously undefined symbols
  166. "-ignore:4221",
  167. ],
  168. # "HWAES" is an abbreviation for "hardware AES" (AES - Advanced Encryption
  169. # Standard). These flags are used for detecting whether or not the target
  170. # architecture has hardware support for AES instructions which can be used
  171. # to improve performance of some random bit generators.
  172. "ABSL_RANDOM_HWAES_ARM64_FLAGS": ["-march=armv8-a+crypto"],
  173. "ABSL_RANDOM_HWAES_ARM32_FLAGS": ["-mfpu=neon"],
  174. "ABSL_RANDOM_HWAES_X64_FLAGS": [
  175. "-maes",
  176. "-msse4.1",
  177. ],
  178. "ABSL_RANDOM_HWAES_MSVC_X64_FLAGS": [],
  179. }