Colin Cross | b98c8b0 | 2016-07-29 13:44:28 -0700 | [diff] [blame] | 1 | package config |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 2 | |
| 3 | import ( |
| 4 | "sort" |
| 5 | "strings" |
| 6 | ) |
| 7 | |
| 8 | // Cflags that should be filtered out when compiling with clang |
Colin Cross | b98c8b0 | 2016-07-29 13:44:28 -0700 | [diff] [blame] | 9 | var ClangUnknownCflags = sorted([]string{ |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 10 | "-finline-functions", |
| 11 | "-finline-limit=64", |
| 12 | "-fno-canonical-system-headers", |
Dan Willemsen | 3bf6b47 | 2015-09-11 17:41:10 -0700 | [diff] [blame] | 13 | "-Wno-clobbered", |
| 14 | "-fno-devirtualize", |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 15 | "-fno-tree-sra", |
Colin Cross | a360e8b | 2015-03-16 16:22:28 -0700 | [diff] [blame] | 16 | "-fprefetch-loop-arrays", |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 17 | "-funswitch-loops", |
Dan Willemsen | e8c5237 | 2016-05-19 16:57:11 -0700 | [diff] [blame] | 18 | "-Werror=unused-but-set-parameter", |
| 19 | "-Werror=unused-but-set-variable", |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 20 | "-Wmaybe-uninitialized", |
Dan Willemsen | 3bf6b47 | 2015-09-11 17:41:10 -0700 | [diff] [blame] | 21 | "-Wno-error=clobbered", |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 22 | "-Wno-error=maybe-uninitialized", |
Colin Cross | 74d1ec0 | 2015-04-28 13:30:13 -0700 | [diff] [blame] | 23 | "-Wno-error=unused-but-set-parameter", |
| 24 | "-Wno-error=unused-but-set-variable", |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 25 | "-Wno-free-nonheap-object", |
| 26 | "-Wno-literal-suffix", |
| 27 | "-Wno-maybe-uninitialized", |
| 28 | "-Wno-old-style-declaration", |
| 29 | "-Wno-psabi", |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 30 | "-Wno-unused-but-set-parameter", |
Colin Cross | 74d1ec0 | 2015-04-28 13:30:13 -0700 | [diff] [blame] | 31 | "-Wno-unused-but-set-variable", |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 32 | "-Wno-unused-local-typedefs", |
Colin Cross | 62ec5f4 | 2015-03-18 17:20:28 -0700 | [diff] [blame] | 33 | "-Wunused-but-set-parameter", |
Colin Cross | 74d1ec0 | 2015-04-28 13:30:13 -0700 | [diff] [blame] | 34 | "-Wunused-but-set-variable", |
Dan Willemsen | e654045 | 2015-10-20 15:21:33 -0700 | [diff] [blame] | 35 | "-fdiagnostics-color", |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 36 | |
| 37 | // arm + arm64 + mips + mips64 |
| 38 | "-fgcse-after-reload", |
| 39 | "-frerun-cse-after-loop", |
| 40 | "-frename-registers", |
| 41 | "-fno-strict-volatile-bitfields", |
| 42 | |
| 43 | // arm + arm64 |
| 44 | "-fno-align-jumps", |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 45 | |
| 46 | // arm |
| 47 | "-mthumb-interwork", |
| 48 | "-fno-builtin-sin", |
| 49 | "-fno-caller-saves", |
| 50 | "-fno-early-inlining", |
| 51 | "-fno-move-loop-invariants", |
| 52 | "-fno-partial-inlining", |
| 53 | "-fno-tree-copy-prop", |
| 54 | "-fno-tree-loop-optimize", |
| 55 | |
| 56 | // mips + mips64 |
| 57 | "-msynci", |
Dan Willemsen | 3bf6b47 | 2015-09-11 17:41:10 -0700 | [diff] [blame] | 58 | "-mno-synci", |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 59 | "-mno-fused-madd", |
| 60 | |
| 61 | // x86 + x86_64 |
| 62 | "-finline-limit=300", |
| 63 | "-fno-inline-functions-called-once", |
| 64 | "-mfpmath=sse", |
| 65 | "-mbionic", |
Dan Willemsen | e654045 | 2015-10-20 15:21:33 -0700 | [diff] [blame] | 66 | }) |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 67 | |
Jayant Chowdhary | 9677e8c | 2017-06-15 14:45:18 -0700 | [diff] [blame] | 68 | var ClangLibToolingUnknownCflags = []string{ |
Jayant Chowdhary | 20f5d13 | 2017-10-27 11:29:37 -0700 | [diff] [blame] | 69 | "-flto*", |
Jayant Chowdhary | 9677e8c | 2017-06-15 14:45:18 -0700 | [diff] [blame] | 70 | "-fsanitize*", |
| 71 | } |
| 72 | |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 73 | func init() { |
Colin Cross | b98c8b0 | 2016-07-29 13:44:28 -0700 | [diff] [blame] | 74 | pctx.StaticVariable("ClangExtraCflags", strings.Join([]string{ |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 75 | "-D__compiler_offsetof=__builtin_offsetof", |
| 76 | |
| 77 | // Help catch common 32/64-bit errors. |
| 78 | "-Werror=int-conversion", |
| 79 | |
Colin Cross | 74d1ec0 | 2015-04-28 13:30:13 -0700 | [diff] [blame] | 80 | // Disable overly aggressive warning for macros defined with a leading underscore |
| 81 | // This happens in AndroidConfig.h, which is included nearly everywhere. |
Dan Willemsen | 3bf6b47 | 2015-09-11 17:41:10 -0700 | [diff] [blame] | 82 | // TODO: can we remove this now? |
Colin Cross | 74d1ec0 | 2015-04-28 13:30:13 -0700 | [diff] [blame] | 83 | "-Wno-reserved-id-macro", |
| 84 | |
| 85 | // Disable overly aggressive warning for format strings. |
| 86 | // Bug: 20148343 |
| 87 | "-Wno-format-pedantic", |
| 88 | |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 89 | // Workaround for ccache with clang. |
| 90 | // See http://petereisentraut.blogspot.com/2011/05/ccache-and-clang.html. |
| 91 | "-Wno-unused-command-line-argument", |
| 92 | |
Dan Willemsen | e654045 | 2015-10-20 15:21:33 -0700 | [diff] [blame] | 93 | // Force clang to always output color diagnostics. Ninja will strip the ANSI |
| 94 | // color codes if it is not running in a terminal. |
| 95 | "-fcolor-diagnostics", |
Pirama Arumuga Nainar | b6572b1 | 2016-06-28 10:56:03 -0700 | [diff] [blame] | 96 | |
| 97 | // http://b/29823425 Disable -Wexpansion-to-defined for Clang update to r271374 |
| 98 | "-Wno-expansion-to-defined", |
Dan Willemsen | 253cab8 | 2017-03-27 16:53:38 -0700 | [diff] [blame] | 99 | |
Stephen Hines | 0ed7d24 | 2017-10-04 16:12:37 -0700 | [diff] [blame] | 100 | // http://b/68236239 Allow 0/NULL instead of using nullptr everywhere. |
| 101 | "-Wno-zero-as-null-pointer-constant", |
| 102 | |
Dan Willemsen | 253cab8 | 2017-03-27 16:53:38 -0700 | [diff] [blame] | 103 | // http://b/36463318 Clang executes with an absolute path, so clang-provided |
| 104 | // headers are now absolute. |
| 105 | "-fdebug-prefix-map=$$PWD/=", |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 106 | }, " ")) |
| 107 | |
Colin Cross | b98c8b0 | 2016-07-29 13:44:28 -0700 | [diff] [blame] | 108 | pctx.StaticVariable("ClangExtraCppflags", strings.Join([]string{ |
Dan Willemsen | ac5e1cb | 2016-01-12 16:22:40 -0800 | [diff] [blame] | 109 | // Disable -Winconsistent-missing-override until we can clean up the existing |
| 110 | // codebase for it. |
| 111 | "-Wno-inconsistent-missing-override", |
Pirama Arumuga Nainar | b6572b1 | 2016-06-28 10:56:03 -0700 | [diff] [blame] | 112 | |
| 113 | // Bug: http://b/29823425 Disable -Wnull-dereference until the |
| 114 | // new instances detected by this warning are fixed. |
| 115 | "-Wno-null-dereference", |
Josh Gao | e0b933b | 2017-04-26 20:26:14 -0700 | [diff] [blame] | 116 | |
| 117 | // Enable clang's thread-safety annotations in libcxx. |
| 118 | // Turn off -Wthread-safety-negative, to avoid breaking projects that use -Weverything. |
| 119 | "-D_LIBCPP_ENABLE_THREAD_SAFETY_ANNOTATIONS", |
| 120 | "-Wno-thread-safety-negative", |
Dan Willemsen | ac5e1cb | 2016-01-12 16:22:40 -0800 | [diff] [blame] | 121 | }, " ")) |
| 122 | |
Colin Cross | b98c8b0 | 2016-07-29 13:44:28 -0700 | [diff] [blame] | 123 | pctx.StaticVariable("ClangExtraTargetCflags", strings.Join([]string{ |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 124 | "-nostdlibinc", |
| 125 | }, " ")) |
Dan Willemsen | be03f34 | 2016-03-03 17:21:04 -0800 | [diff] [blame] | 126 | |
Colin Cross | b98c8b0 | 2016-07-29 13:44:28 -0700 | [diff] [blame] | 127 | pctx.StaticVariable("ClangExtraNoOverrideCflags", strings.Join([]string{ |
Dan Willemsen | be03f34 | 2016-03-03 17:21:04 -0800 | [diff] [blame] | 128 | "-Werror=address-of-temporary", |
Pirama Arumuga Nainar | b6572b1 | 2016-06-28 10:56:03 -0700 | [diff] [blame] | 129 | // Bug: http://b/29823425 Disable -Wnull-dereference until the |
| 130 | // new cases detected by this warning in Clang r271374 are |
| 131 | // fixed. |
| 132 | //"-Werror=null-dereference", |
Dan Willemsen | be03f34 | 2016-03-03 17:21:04 -0800 | [diff] [blame] | 133 | "-Werror=return-type", |
| 134 | }, " ")) |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 135 | } |
| 136 | |
Colin Cross | b98c8b0 | 2016-07-29 13:44:28 -0700 | [diff] [blame] | 137 | func ClangFilterUnknownCflags(cflags []string) []string { |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 138 | ret := make([]string, 0, len(cflags)) |
| 139 | for _, f := range cflags { |
Colin Cross | b98c8b0 | 2016-07-29 13:44:28 -0700 | [diff] [blame] | 140 | if !inListSorted(f, ClangUnknownCflags) { |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 141 | ret = append(ret, f) |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | return ret |
| 146 | } |
| 147 | |
| 148 | func inListSorted(s string, list []string) bool { |
| 149 | for _, l := range list { |
| 150 | if s == l { |
| 151 | return true |
| 152 | } else if s < l { |
| 153 | return false |
| 154 | } |
| 155 | } |
| 156 | return false |
| 157 | } |
Dan Willemsen | e654045 | 2015-10-20 15:21:33 -0700 | [diff] [blame] | 158 | |
| 159 | func sorted(list []string) []string { |
| 160 | sort.Strings(list) |
| 161 | return list |
| 162 | } |