Colin Cross | d00350c | 2017-11-17 10:55:38 -0800 | [diff] [blame] | 1 | // Copyright 2017 Google Inc. All rights reserved. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
Colin Cross | b98c8b0 | 2016-07-29 13:44:28 -0700 | [diff] [blame] | 15 | package config |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 16 | |
| 17 | import ( |
| 18 | "sort" |
| 19 | "strings" |
| 20 | ) |
| 21 | |
| 22 | // Cflags that should be filtered out when compiling with clang |
Colin Cross | b98c8b0 | 2016-07-29 13:44:28 -0700 | [diff] [blame] | 23 | var ClangUnknownCflags = sorted([]string{ |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 24 | "-finline-functions", |
| 25 | "-finline-limit=64", |
| 26 | "-fno-canonical-system-headers", |
Dan Willemsen | 3bf6b47 | 2015-09-11 17:41:10 -0700 | [diff] [blame] | 27 | "-Wno-clobbered", |
| 28 | "-fno-devirtualize", |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 29 | "-fno-tree-sra", |
Colin Cross | a360e8b | 2015-03-16 16:22:28 -0700 | [diff] [blame] | 30 | "-fprefetch-loop-arrays", |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 31 | "-funswitch-loops", |
Dan Willemsen | e8c5237 | 2016-05-19 16:57:11 -0700 | [diff] [blame] | 32 | "-Werror=unused-but-set-parameter", |
| 33 | "-Werror=unused-but-set-variable", |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 34 | "-Wmaybe-uninitialized", |
Dan Willemsen | 3bf6b47 | 2015-09-11 17:41:10 -0700 | [diff] [blame] | 35 | "-Wno-error=clobbered", |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 36 | "-Wno-error=maybe-uninitialized", |
Colin Cross | 74d1ec0 | 2015-04-28 13:30:13 -0700 | [diff] [blame] | 37 | "-Wno-error=unused-but-set-parameter", |
| 38 | "-Wno-error=unused-but-set-variable", |
Chih-Hung Hsieh | 3ede294 | 2018-01-10 14:30:44 -0800 | [diff] [blame] | 39 | "-Wno-extended-offsetof", |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 40 | "-Wno-free-nonheap-object", |
| 41 | "-Wno-literal-suffix", |
| 42 | "-Wno-maybe-uninitialized", |
| 43 | "-Wno-old-style-declaration", |
| 44 | "-Wno-psabi", |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 45 | "-Wno-unused-but-set-parameter", |
Colin Cross | 74d1ec0 | 2015-04-28 13:30:13 -0700 | [diff] [blame] | 46 | "-Wno-unused-but-set-variable", |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 47 | "-Wno-unused-local-typedefs", |
Colin Cross | 62ec5f4 | 2015-03-18 17:20:28 -0700 | [diff] [blame] | 48 | "-Wunused-but-set-parameter", |
Colin Cross | 74d1ec0 | 2015-04-28 13:30:13 -0700 | [diff] [blame] | 49 | "-Wunused-but-set-variable", |
Dan Willemsen | e654045 | 2015-10-20 15:21:33 -0700 | [diff] [blame] | 50 | "-fdiagnostics-color", |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 51 | |
| 52 | // arm + arm64 + mips + mips64 |
| 53 | "-fgcse-after-reload", |
| 54 | "-frerun-cse-after-loop", |
| 55 | "-frename-registers", |
| 56 | "-fno-strict-volatile-bitfields", |
| 57 | |
| 58 | // arm + arm64 |
| 59 | "-fno-align-jumps", |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 60 | |
| 61 | // arm |
| 62 | "-mthumb-interwork", |
| 63 | "-fno-builtin-sin", |
| 64 | "-fno-caller-saves", |
| 65 | "-fno-early-inlining", |
| 66 | "-fno-move-loop-invariants", |
| 67 | "-fno-partial-inlining", |
| 68 | "-fno-tree-copy-prop", |
| 69 | "-fno-tree-loop-optimize", |
| 70 | |
| 71 | // mips + mips64 |
| 72 | "-msynci", |
Dan Willemsen | 3bf6b47 | 2015-09-11 17:41:10 -0700 | [diff] [blame] | 73 | "-mno-synci", |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 74 | "-mno-fused-madd", |
| 75 | |
| 76 | // x86 + x86_64 |
| 77 | "-finline-limit=300", |
| 78 | "-fno-inline-functions-called-once", |
| 79 | "-mfpmath=sse", |
| 80 | "-mbionic", |
Dan Willemsen | 01f388c | 2017-11-30 13:31:26 -0800 | [diff] [blame] | 81 | |
| 82 | // windows |
| 83 | "--enable-stdcall-fixup", |
Dan Willemsen | e654045 | 2015-10-20 15:21:33 -0700 | [diff] [blame] | 84 | }) |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 85 | |
Chih-Hung Hsieh | 02b4da5 | 2018-04-03 11:33:34 -0700 | [diff] [blame] | 86 | // Ldflags that should be filtered out when linking with clang lld |
| 87 | var ClangUnknownLldflags = sorted([]string{ |
| 88 | "-fuse-ld=gold", |
Chih-Hung Hsieh | 02b4da5 | 2018-04-03 11:33:34 -0700 | [diff] [blame] | 89 | "-Wl,--fix-cortex-a8", |
| 90 | "-Wl,--no-fix-cortex-a8", |
| 91 | "-Wl,-m,aarch64_elf64_le_vec", |
| 92 | }) |
| 93 | |
Chih-Hung Hsieh | 1017b37 | 2018-12-06 12:12:41 -0800 | [diff] [blame] | 94 | var ClangLibToolingUnknownCflags = sorted([]string{}) |
Jayant Chowdhary | 9677e8c | 2017-06-15 14:45:18 -0700 | [diff] [blame] | 95 | |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 96 | func init() { |
Colin Cross | b98c8b0 | 2016-07-29 13:44:28 -0700 | [diff] [blame] | 97 | pctx.StaticVariable("ClangExtraCflags", strings.Join([]string{ |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 98 | "-D__compiler_offsetof=__builtin_offsetof", |
| 99 | |
Yi Kong | a32b5e6 | 2019-03-19 20:05:05 -0700 | [diff] [blame] | 100 | // Emit address-significance table which allows linker to perform safe ICF. Clang does |
| 101 | // not emit the table by default on Android since NDK still uses GNU binutils. |
| 102 | "-faddrsig", |
| 103 | |
Chih-Hung Hsieh | d602244 | 2018-10-19 09:41:19 -0700 | [diff] [blame] | 104 | // -Wimplicit-fallthrough is not enabled by -Wall. |
Chih-Hung Hsieh | bd783f1 | 2018-08-14 13:20:38 -0700 | [diff] [blame] | 105 | "-Wimplicit-fallthrough", |
Chih-Hung Hsieh | bd783f1 | 2018-08-14 13:20:38 -0700 | [diff] [blame] | 106 | |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 107 | // Help catch common 32/64-bit errors. |
| 108 | "-Werror=int-conversion", |
| 109 | |
Colin Cross | 74d1ec0 | 2015-04-28 13:30:13 -0700 | [diff] [blame] | 110 | // Disable overly aggressive warning for macros defined with a leading underscore |
| 111 | // This happens in AndroidConfig.h, which is included nearly everywhere. |
Dan Willemsen | 3bf6b47 | 2015-09-11 17:41:10 -0700 | [diff] [blame] | 112 | // TODO: can we remove this now? |
Colin Cross | 74d1ec0 | 2015-04-28 13:30:13 -0700 | [diff] [blame] | 113 | "-Wno-reserved-id-macro", |
| 114 | |
| 115 | // Disable overly aggressive warning for format strings. |
| 116 | // Bug: 20148343 |
| 117 | "-Wno-format-pedantic", |
| 118 | |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 119 | // Workaround for ccache with clang. |
| 120 | // See http://petereisentraut.blogspot.com/2011/05/ccache-and-clang.html. |
| 121 | "-Wno-unused-command-line-argument", |
| 122 | |
Dan Willemsen | e654045 | 2015-10-20 15:21:33 -0700 | [diff] [blame] | 123 | // Force clang to always output color diagnostics. Ninja will strip the ANSI |
| 124 | // color codes if it is not running in a terminal. |
| 125 | "-fcolor-diagnostics", |
Pirama Arumuga Nainar | b6572b1 | 2016-06-28 10:56:03 -0700 | [diff] [blame] | 126 | |
Stephen Hines | 0ed7d24 | 2017-10-04 16:12:37 -0700 | [diff] [blame] | 127 | // http://b/68236239 Allow 0/NULL instead of using nullptr everywhere. |
| 128 | "-Wno-zero-as-null-pointer-constant", |
Chih-Hung Hsieh | 3ede294 | 2018-01-10 14:30:44 -0800 | [diff] [blame] | 129 | |
| 130 | // Warnings from clang-7.0 |
Chih-Hung Hsieh | 3ede294 | 2018-01-10 14:30:44 -0800 | [diff] [blame] | 131 | "-Wno-sign-compare", |
Yi Kong | 53ed59e | 2018-10-30 15:31:38 -0700 | [diff] [blame] | 132 | |
| 133 | // Warnings from clang-8.0 |
| 134 | "-Wno-defaulted-function-deleted", |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 135 | |
Dan Willemsen | ac5e1cb | 2016-01-12 16:22:40 -0800 | [diff] [blame] | 136 | // Disable -Winconsistent-missing-override until we can clean up the existing |
| 137 | // codebase for it. |
| 138 | "-Wno-inconsistent-missing-override", |
Yi Kong | 4d048d5 | 2019-03-27 18:26:22 -0700 | [diff] [blame^] | 139 | }, " ")) |
Pirama Arumuga Nainar | b6572b1 | 2016-06-28 10:56:03 -0700 | [diff] [blame] | 140 | |
Yi Kong | 4d048d5 | 2019-03-27 18:26:22 -0700 | [diff] [blame^] | 141 | pctx.StaticVariable("ClangExtraCppflags", strings.Join([]string{ |
Josh Gao | e0b933b | 2017-04-26 20:26:14 -0700 | [diff] [blame] | 142 | // Enable clang's thread-safety annotations in libcxx. |
| 143 | // Turn off -Wthread-safety-negative, to avoid breaking projects that use -Weverything. |
| 144 | "-D_LIBCPP_ENABLE_THREAD_SAFETY_ANNOTATIONS", |
| 145 | "-Wno-thread-safety-negative", |
Dan Albert | f2ceea7 | 2018-02-07 17:24:42 -0800 | [diff] [blame] | 146 | |
| 147 | // libc++'s math.h has an #include_next outside of system_headers. |
| 148 | "-Wno-gnu-include-next", |
Dan Willemsen | ac5e1cb | 2016-01-12 16:22:40 -0800 | [diff] [blame] | 149 | }, " ")) |
| 150 | |
Colin Cross | b98c8b0 | 2016-07-29 13:44:28 -0700 | [diff] [blame] | 151 | pctx.StaticVariable("ClangExtraTargetCflags", strings.Join([]string{ |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 152 | "-nostdlibinc", |
| 153 | }, " ")) |
Dan Willemsen | be03f34 | 2016-03-03 17:21:04 -0800 | [diff] [blame] | 154 | |
Colin Cross | b98c8b0 | 2016-07-29 13:44:28 -0700 | [diff] [blame] | 155 | pctx.StaticVariable("ClangExtraNoOverrideCflags", strings.Join([]string{ |
Dan Willemsen | be03f34 | 2016-03-03 17:21:04 -0800 | [diff] [blame] | 156 | "-Werror=address-of-temporary", |
Pirama Arumuga Nainar | b6572b1 | 2016-06-28 10:56:03 -0700 | [diff] [blame] | 157 | // Bug: http://b/29823425 Disable -Wnull-dereference until the |
| 158 | // new cases detected by this warning in Clang r271374 are |
| 159 | // fixed. |
| 160 | //"-Werror=null-dereference", |
Dan Willemsen | be03f34 | 2016-03-03 17:21:04 -0800 | [diff] [blame] | 161 | "-Werror=return-type", |
Yi Kong | 599a603 | 2017-12-06 19:56:34 -0800 | [diff] [blame] | 162 | |
| 163 | // http://b/72331526 Disable -Wtautological-* until the instances detected by these |
| 164 | // new warnings are fixed. |
Stephen Hines | a42e0a0 | 2018-02-06 14:49:42 -0800 | [diff] [blame] | 165 | "-Wno-tautological-constant-compare", |
Chih-Hung Hsieh | 3ede294 | 2018-01-10 14:30:44 -0800 | [diff] [blame] | 166 | "-Wno-tautological-type-limit-compare", |
| 167 | "-Wno-tautological-unsigned-enum-zero-compare", |
| 168 | "-Wno-tautological-unsigned-zero-compare", |
Yi Kong | 599a603 | 2017-12-06 19:56:34 -0800 | [diff] [blame] | 169 | |
Yi Kong | c729b50 | 2018-06-29 08:19:46 +0000 | [diff] [blame] | 170 | // http://b/72330874 Disable -Wenum-compare until the instances detected by this new |
| 171 | // warning are fixed. |
| 172 | "-Wno-enum-compare", |
| 173 | "-Wno-enum-compare-switch", |
| 174 | |
Kevin Rocard | a9ccbb7 | 2018-06-05 21:43:46 +0000 | [diff] [blame] | 175 | // Disable c++98-specific warning since Android is not concerned with C++98 |
| 176 | // compatibility. |
| 177 | "-Wno-c++98-compat-extra-semi", |
Stephen Hines | c91ab9e | 2018-09-20 18:01:39 -0700 | [diff] [blame] | 178 | |
Stephen Hines | c91ab9e | 2018-09-20 18:01:39 -0700 | [diff] [blame] | 179 | // Disable this warning because we don't care about behavior with older compilers. |
| 180 | "-Wno-return-std-move-in-c++11", |
Dan Willemsen | be03f34 | 2016-03-03 17:21:04 -0800 | [diff] [blame] | 181 | }, " ")) |
Yi Kong | cc80f8d | 2018-06-06 14:42:44 -0700 | [diff] [blame] | 182 | |
Yi Kong | c729b50 | 2018-06-29 08:19:46 +0000 | [diff] [blame] | 183 | // Extra cflags for projects under external/ directory |
Yi Kong | cc80f8d | 2018-06-06 14:42:44 -0700 | [diff] [blame] | 184 | pctx.StaticVariable("ClangExtraExternalCflags", strings.Join([]string{ |
Yi Kong | c729b50 | 2018-06-29 08:19:46 +0000 | [diff] [blame] | 185 | // TODO(yikong): Move -Wno flags here |
Yi Kong | c9f6db5 | 2018-12-11 18:34:49 -0800 | [diff] [blame] | 186 | |
| 187 | // http://b/72331524 Allow null pointer arithmetic until the instances detected by |
| 188 | // this new warning are fixed. |
| 189 | "-Wno-null-pointer-arithmetic", |
Yi Kong | fae5dac | 2018-12-17 17:18:37 -0800 | [diff] [blame] | 190 | |
| 191 | // Bug: http://b/29823425 Disable -Wnull-dereference until the |
| 192 | // new instances detected by this warning are fixed. |
| 193 | "-Wno-null-dereference", |
Yi Kong | cc80f8d | 2018-06-06 14:42:44 -0700 | [diff] [blame] | 194 | }, " ")) |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 195 | } |
| 196 | |
Colin Cross | b98c8b0 | 2016-07-29 13:44:28 -0700 | [diff] [blame] | 197 | func ClangFilterUnknownCflags(cflags []string) []string { |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 198 | ret := make([]string, 0, len(cflags)) |
| 199 | for _, f := range cflags { |
Colin Cross | b98c8b0 | 2016-07-29 13:44:28 -0700 | [diff] [blame] | 200 | if !inListSorted(f, ClangUnknownCflags) { |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 201 | ret = append(ret, f) |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | return ret |
| 206 | } |
| 207 | |
Chih-Hung Hsieh | 02b4da5 | 2018-04-03 11:33:34 -0700 | [diff] [blame] | 208 | func ClangFilterUnknownLldflags(lldflags []string) []string { |
| 209 | ret := make([]string, 0, len(lldflags)) |
| 210 | for _, f := range lldflags { |
| 211 | if !inListSorted(f, ClangUnknownLldflags) { |
| 212 | ret = append(ret, f) |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | return ret |
| 217 | } |
| 218 | |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 219 | func inListSorted(s string, list []string) bool { |
| 220 | for _, l := range list { |
| 221 | if s == l { |
| 222 | return true |
| 223 | } else if s < l { |
| 224 | return false |
| 225 | } |
| 226 | } |
| 227 | return false |
| 228 | } |
Dan Willemsen | e654045 | 2015-10-20 15:21:33 -0700 | [diff] [blame] | 229 | |
| 230 | func sorted(list []string) []string { |
| 231 | sort.Strings(list) |
| 232 | return list |
| 233 | } |