• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
15package config
16
17import (
18	"sort"
19	"strings"
20)
21
22// Cflags that should be filtered out when compiling with clang
23var ClangUnknownCflags = sorted([]string{
24	"-finline-functions",
25	"-finline-limit=64",
26	"-fno-canonical-system-headers",
27	"-Wno-clobbered",
28	"-fno-devirtualize",
29	"-fno-tree-sra",
30	"-fprefetch-loop-arrays",
31	"-funswitch-loops",
32	"-Werror=unused-but-set-parameter",
33	"-Werror=unused-but-set-variable",
34	"-Wmaybe-uninitialized",
35	"-Wno-error=clobbered",
36	"-Wno-error=maybe-uninitialized",
37	"-Wno-error=unused-but-set-parameter",
38	"-Wno-error=unused-but-set-variable",
39	"-Wno-extended-offsetof",
40	"-Wno-free-nonheap-object",
41	"-Wno-literal-suffix",
42	"-Wno-maybe-uninitialized",
43	"-Wno-old-style-declaration",
44	"-Wno-psabi",
45	"-Wno-unused-but-set-parameter",
46	"-Wno-unused-but-set-variable",
47	"-Wno-unused-local-typedefs",
48	"-Wunused-but-set-parameter",
49	"-Wunused-but-set-variable",
50	"-fdiagnostics-color",
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",
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",
73	"-mno-synci",
74	"-mno-fused-madd",
75
76	// x86 + x86_64
77	"-finline-limit=300",
78	"-fno-inline-functions-called-once",
79	"-mfpmath=sse",
80	"-mbionic",
81
82	// windows
83	"--enable-stdcall-fixup",
84})
85
86// Ldflags that should be filtered out when linking with clang lld
87var ClangUnknownLldflags = sorted([]string{
88	"-fuse-ld=gold",
89	"-Wl,--fix-cortex-a8",
90	"-Wl,--no-fix-cortex-a8",
91	"-Wl,-m,aarch64_elf64_le_vec",
92})
93
94var ClangLibToolingUnknownCflags = sorted([]string{})
95
96func init() {
97	pctx.StaticVariable("ClangExtraCflags", strings.Join([]string{
98		"-D__compiler_offsetof=__builtin_offsetof",
99
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
104		// -Wimplicit-fallthrough is not enabled by -Wall.
105		"-Wimplicit-fallthrough",
106
107		// Help catch common 32/64-bit errors.
108		"-Werror=int-conversion",
109
110		// Disable overly aggressive warning for macros defined with a leading underscore
111		// This happens in AndroidConfig.h, which is included nearly everywhere.
112		// TODO: can we remove this now?
113		"-Wno-reserved-id-macro",
114
115		// Disable overly aggressive warning for format strings.
116		// Bug: 20148343
117		"-Wno-format-pedantic",
118
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
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",
126
127		// http://b/68236239 Allow 0/NULL instead of using nullptr everywhere.
128		"-Wno-zero-as-null-pointer-constant",
129
130		// Warnings from clang-7.0
131		"-Wno-sign-compare",
132
133		// Warnings from clang-8.0
134		"-Wno-defaulted-function-deleted",
135
136		// Disable -Winconsistent-missing-override until we can clean up the existing
137		// codebase for it.
138		"-Wno-inconsistent-missing-override",
139	}, " "))
140
141	pctx.StaticVariable("ClangExtraCppflags", strings.Join([]string{
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",
146
147		// libc++'s math.h has an #include_next outside of system_headers.
148		"-Wno-gnu-include-next",
149	}, " "))
150
151	pctx.StaticVariable("ClangExtraTargetCflags", strings.Join([]string{
152		"-nostdlibinc",
153	}, " "))
154
155	pctx.StaticVariable("ClangExtraNoOverrideCflags", strings.Join([]string{
156		"-Werror=address-of-temporary",
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",
161		"-Werror=return-type",
162
163		// http://b/72331526 Disable -Wtautological-* until the instances detected by these
164		// new warnings are fixed.
165		"-Wno-tautological-constant-compare",
166		"-Wno-tautological-type-limit-compare",
167		"-Wno-tautological-unsigned-enum-zero-compare",
168		"-Wno-tautological-unsigned-zero-compare",
169
170		// Disable c++98-specific warning since Android is not concerned with C++98
171		// compatibility.
172		"-Wno-c++98-compat-extra-semi",
173
174		// Disable this warning because we don't care about behavior with older compilers.
175		"-Wno-return-std-move-in-c++11",
176	}, " "))
177
178	// Extra cflags for projects under external/ directory to disable warnings that are infeasible
179	// to fix in all the external projects and their upstream repos.
180	pctx.StaticVariable("ClangExtraExternalCflags", strings.Join([]string{
181		"-Wno-enum-compare",
182		"-Wno-enum-compare-switch",
183
184		// http://b/72331524 Allow null pointer arithmetic until the instances detected by
185		// this new warning are fixed.
186		"-Wno-null-pointer-arithmetic",
187
188		// Bug: http://b/29823425 Disable -Wnull-dereference until the
189		// new instances detected by this warning are fixed.
190		"-Wno-null-dereference",
191	}, " "))
192}
193
194func ClangFilterUnknownCflags(cflags []string) []string {
195	ret := make([]string, 0, len(cflags))
196	for _, f := range cflags {
197		if !inListSorted(f, ClangUnknownCflags) {
198			ret = append(ret, f)
199		}
200	}
201
202	return ret
203}
204
205func ClangFilterUnknownLldflags(lldflags []string) []string {
206	ret := make([]string, 0, len(lldflags))
207	for _, f := range lldflags {
208		if !inListSorted(f, ClangUnknownLldflags) {
209			ret = append(ret, f)
210		}
211	}
212
213	return ret
214}
215
216func inListSorted(s string, list []string) bool {
217	for _, l := range list {
218		if s == l {
219			return true
220		} else if s < l {
221			return false
222		}
223	}
224	return false
225}
226
227func sorted(list []string) []string {
228	sort.Strings(list)
229	return list
230}
231