blob: 3ff6b1bf75de3df85fca7fe4f1749e5e3263c90d [file] [log] [blame]
Colin Crossb98c8b02016-07-29 13:44:28 -07001package config
Colin Cross3f40fa42015-01-30 17:27:36 -08002
3import (
4 "sort"
5 "strings"
6)
7
8// Cflags that should be filtered out when compiling with clang
Colin Crossb98c8b02016-07-29 13:44:28 -07009var ClangUnknownCflags = sorted([]string{
Colin Cross3f40fa42015-01-30 17:27:36 -080010 "-finline-functions",
11 "-finline-limit=64",
12 "-fno-canonical-system-headers",
Dan Willemsen3bf6b472015-09-11 17:41:10 -070013 "-Wno-clobbered",
14 "-fno-devirtualize",
Colin Cross3f40fa42015-01-30 17:27:36 -080015 "-fno-tree-sra",
Colin Crossa360e8b2015-03-16 16:22:28 -070016 "-fprefetch-loop-arrays",
Colin Cross3f40fa42015-01-30 17:27:36 -080017 "-funswitch-loops",
Dan Willemsene8c52372016-05-19 16:57:11 -070018 "-Werror=unused-but-set-parameter",
19 "-Werror=unused-but-set-variable",
Colin Cross3f40fa42015-01-30 17:27:36 -080020 "-Wmaybe-uninitialized",
Dan Willemsen3bf6b472015-09-11 17:41:10 -070021 "-Wno-error=clobbered",
Colin Cross3f40fa42015-01-30 17:27:36 -080022 "-Wno-error=maybe-uninitialized",
Colin Cross74d1ec02015-04-28 13:30:13 -070023 "-Wno-error=unused-but-set-parameter",
24 "-Wno-error=unused-but-set-variable",
Colin Cross3f40fa42015-01-30 17:27:36 -080025 "-Wno-free-nonheap-object",
26 "-Wno-literal-suffix",
27 "-Wno-maybe-uninitialized",
28 "-Wno-old-style-declaration",
29 "-Wno-psabi",
Colin Cross3f40fa42015-01-30 17:27:36 -080030 "-Wno-unused-but-set-parameter",
Colin Cross74d1ec02015-04-28 13:30:13 -070031 "-Wno-unused-but-set-variable",
Colin Cross3f40fa42015-01-30 17:27:36 -080032 "-Wno-unused-local-typedefs",
Colin Cross62ec5f42015-03-18 17:20:28 -070033 "-Wunused-but-set-parameter",
Colin Cross74d1ec02015-04-28 13:30:13 -070034 "-Wunused-but-set-variable",
Dan Willemsene6540452015-10-20 15:21:33 -070035 "-fdiagnostics-color",
Colin Cross3f40fa42015-01-30 17:27:36 -080036
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 Cross3f40fa42015-01-30 17:27:36 -080045
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 Willemsen3bf6b472015-09-11 17:41:10 -070058 "-mno-synci",
Colin Cross3f40fa42015-01-30 17:27:36 -080059 "-mno-fused-madd",
60
61 // x86 + x86_64
62 "-finline-limit=300",
63 "-fno-inline-functions-called-once",
64 "-mfpmath=sse",
65 "-mbionic",
Dan Willemsene6540452015-10-20 15:21:33 -070066})
Colin Cross3f40fa42015-01-30 17:27:36 -080067
68func init() {
Colin Crossb98c8b02016-07-29 13:44:28 -070069 pctx.StaticVariable("ClangExtraCflags", strings.Join([]string{
Colin Cross3f40fa42015-01-30 17:27:36 -080070 "-D__compiler_offsetof=__builtin_offsetof",
71
72 // Help catch common 32/64-bit errors.
73 "-Werror=int-conversion",
74
Colin Cross74d1ec02015-04-28 13:30:13 -070075 // Disable overly aggressive warning for macros defined with a leading underscore
76 // This happens in AndroidConfig.h, which is included nearly everywhere.
Dan Willemsen3bf6b472015-09-11 17:41:10 -070077 // TODO: can we remove this now?
Colin Cross74d1ec02015-04-28 13:30:13 -070078 "-Wno-reserved-id-macro",
79
80 // Disable overly aggressive warning for format strings.
81 // Bug: 20148343
82 "-Wno-format-pedantic",
83
Colin Cross3f40fa42015-01-30 17:27:36 -080084 // Workaround for ccache with clang.
85 // See http://petereisentraut.blogspot.com/2011/05/ccache-and-clang.html.
86 "-Wno-unused-command-line-argument",
87
Dan Willemsene6540452015-10-20 15:21:33 -070088 // Force clang to always output color diagnostics. Ninja will strip the ANSI
89 // color codes if it is not running in a terminal.
90 "-fcolor-diagnostics",
Pirama Arumuga Nainarb6572b12016-06-28 10:56:03 -070091
92 // http://b/29823425 Disable -Wexpansion-to-defined for Clang update to r271374
93 "-Wno-expansion-to-defined",
Dan Willemsen253cab82017-03-27 16:53:38 -070094
95 // http://b/36463318 Clang executes with an absolute path, so clang-provided
96 // headers are now absolute.
97 "-fdebug-prefix-map=$$PWD/=",
Colin Cross3f40fa42015-01-30 17:27:36 -080098 }, " "))
99
Colin Crossb98c8b02016-07-29 13:44:28 -0700100 pctx.StaticVariable("ClangExtraCppflags", strings.Join([]string{
Dan Willemsenac5e1cb2016-01-12 16:22:40 -0800101 // Disable -Winconsistent-missing-override until we can clean up the existing
102 // codebase for it.
103 "-Wno-inconsistent-missing-override",
Pirama Arumuga Nainarb6572b12016-06-28 10:56:03 -0700104
105 // Bug: http://b/29823425 Disable -Wnull-dereference until the
106 // new instances detected by this warning are fixed.
107 "-Wno-null-dereference",
Josh Gaoe0b933b2017-04-26 20:26:14 -0700108
109 // Enable clang's thread-safety annotations in libcxx.
110 // Turn off -Wthread-safety-negative, to avoid breaking projects that use -Weverything.
111 "-D_LIBCPP_ENABLE_THREAD_SAFETY_ANNOTATIONS",
112 "-Wno-thread-safety-negative",
Dan Willemsenac5e1cb2016-01-12 16:22:40 -0800113 }, " "))
114
Colin Crossb98c8b02016-07-29 13:44:28 -0700115 pctx.StaticVariable("ClangExtraTargetCflags", strings.Join([]string{
Colin Cross3f40fa42015-01-30 17:27:36 -0800116 "-nostdlibinc",
117 }, " "))
Dan Willemsenbe03f342016-03-03 17:21:04 -0800118
Colin Crossb98c8b02016-07-29 13:44:28 -0700119 pctx.StaticVariable("ClangExtraNoOverrideCflags", strings.Join([]string{
Dan Willemsenbe03f342016-03-03 17:21:04 -0800120 "-Werror=address-of-temporary",
Pirama Arumuga Nainarb6572b12016-06-28 10:56:03 -0700121 // Bug: http://b/29823425 Disable -Wnull-dereference until the
122 // new cases detected by this warning in Clang r271374 are
123 // fixed.
124 //"-Werror=null-dereference",
Dan Willemsenbe03f342016-03-03 17:21:04 -0800125 "-Werror=return-type",
126 }, " "))
Colin Cross3f40fa42015-01-30 17:27:36 -0800127}
128
Colin Crossb98c8b02016-07-29 13:44:28 -0700129func ClangFilterUnknownCflags(cflags []string) []string {
Colin Cross3f40fa42015-01-30 17:27:36 -0800130 ret := make([]string, 0, len(cflags))
131 for _, f := range cflags {
Colin Crossb98c8b02016-07-29 13:44:28 -0700132 if !inListSorted(f, ClangUnknownCflags) {
Colin Cross3f40fa42015-01-30 17:27:36 -0800133 ret = append(ret, f)
134 }
135 }
136
137 return ret
138}
139
140func inListSorted(s string, list []string) bool {
141 for _, l := range list {
142 if s == l {
143 return true
144 } else if s < l {
145 return false
146 }
147 }
148 return false
149}
Dan Willemsene6540452015-10-20 15:21:33 -0700150
151func sorted(list []string) []string {
152 sort.Strings(list)
153 return list
154}