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