blob: 671861b0be2a6e34e273586b7d83b10108647dec [file] [log] [blame]
Colin Cross4d9c2d12016-07-29 12:48:20 -07001// Copyright 2016 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 cc
16
17import (
18 "fmt"
19 "path/filepath"
Yi Kong950e0ba2019-10-08 02:27:17 -070020 "regexp"
Peter Collingbourneb0e61432019-07-22 16:36:06 -070021 "strconv"
Colin Cross4d9c2d12016-07-29 12:48:20 -070022 "strings"
23
Colin Cross4b963f82016-09-29 14:06:02 -070024 "github.com/google/blueprint/proptools"
25
Colin Cross4d9c2d12016-07-29 12:48:20 -070026 "android/soong/android"
Colin Crossb98c8b02016-07-29 13:44:28 -070027 "android/soong/cc/config"
Colin Cross4d9c2d12016-07-29 12:48:20 -070028)
29
30// This file contains the basic C/C++/assembly to .o compliation steps
31
32type BaseCompilerProperties struct {
33 // list of source files used to compile the C/C++ module. May be .c, .cpp, or .S files.
Colin Cross068e0fe2016-12-13 15:23:47 -080034 // srcs may reference the outputs of other modules that produce source files like genrule
35 // or filegroup using the syntax ":module".
Colin Cross27b922f2019-03-04 22:35:41 -080036 Srcs []string `android:"path,arch_variant"`
Colin Cross4d9c2d12016-07-29 12:48:20 -070037
38 // list of source files that should not be used to build the C/C++ module.
39 // This is most useful in the arch/multilib variants to remove non-common files
Colin Cross27b922f2019-03-04 22:35:41 -080040 Exclude_srcs []string `android:"path,arch_variant"`
Colin Cross4d9c2d12016-07-29 12:48:20 -070041
42 // list of module-specific flags that will be used for C and C++ compiles.
43 Cflags []string `android:"arch_variant"`
44
45 // list of module-specific flags that will be used for C++ compiles
46 Cppflags []string `android:"arch_variant"`
47
48 // list of module-specific flags that will be used for C compiles
49 Conlyflags []string `android:"arch_variant"`
50
51 // list of module-specific flags that will be used for .S compiles
52 Asflags []string `android:"arch_variant"`
53
54 // list of module-specific flags that will be used for C and C++ compiles when
55 // compiling with clang
56 Clang_cflags []string `android:"arch_variant"`
57
58 // list of module-specific flags that will be used for .S compiles when
59 // compiling with clang
60 Clang_asflags []string `android:"arch_variant"`
61
Colin Cross4d9c2d12016-07-29 12:48:20 -070062 // the instruction set architecture to use to compile the C/C++
63 // module.
Dan Willemsen5922f3c2017-10-25 15:20:50 -070064 Instruction_set *string `android:"arch_variant"`
Colin Cross4d9c2d12016-07-29 12:48:20 -070065
66 // list of directories relative to the root of the source tree that will
67 // be added to the include path using -I.
68 // If possible, don't use this. If adding paths from the current directory use
69 // local_include_dirs, if adding paths from other modules use export_include_dirs in
70 // that module.
Colin Crossccf01e72017-04-26 17:01:07 -070071 Include_dirs []string `android:"arch_variant,variant_prepend"`
Colin Cross4d9c2d12016-07-29 12:48:20 -070072
73 // list of directories relative to the Blueprints file that will
74 // be added to the include path using -I
Dan Willemsen59339a22018-07-22 21:18:45 -070075 Local_include_dirs []string `android:"arch_variant,variant_prepend"`
Colin Cross4d9c2d12016-07-29 12:48:20 -070076
Dan Albert899c23e2018-12-06 11:04:03 -080077 // Add the directory containing the Android.bp file to the list of include
78 // directories. Defaults to true.
79 Include_build_directory *bool
80
Colin Cross4d9c2d12016-07-29 12:48:20 -070081 // list of generated sources to compile. These are the names of gensrcs or
82 // genrule modules.
83 Generated_sources []string `android:"arch_variant"`
84
85 // list of generated headers to add to the include path. These are the names
86 // of genrule modules.
87 Generated_headers []string `android:"arch_variant"`
88
89 // pass -frtti instead of -fno-rtti
90 Rtti *bool
91
Dan Albert043833c2017-02-03 16:13:38 -080092 // C standard version to use. Can be a specific version (such as "gnu11"),
93 // "experimental" (which will use draft versions like C1x when available),
94 // or the empty string (which will use the default).
Nan Zhang0007d812017-11-07 10:57:05 -080095 C_std *string
Dan Albert043833c2017-02-03 16:13:38 -080096
97 // C++ standard version to use. Can be a specific version (such as
98 // "gnu++11"), "experimental" (which will use draft versions like C++1z when
99 // available), or the empty string (which will use the default).
Nan Zhang0007d812017-11-07 10:57:05 -0800100 Cpp_std *string
Dan Albert043833c2017-02-03 16:13:38 -0800101
Colin Cross948f0cb2016-10-17 14:24:56 -0700102 // if set to false, use -std=c++* instead of -std=gnu++*
103 Gnu_extensions *bool
104
Dan Willemsen4e0aa232019-04-10 22:59:54 -0700105 Yacc *YaccProperties
106
Dan Willemsene1240db2016-11-03 14:28:51 -0700107 Aidl struct {
108 // list of directories that will be added to the aidl include paths.
109 Include_dirs []string
110
111 // list of directories relative to the Blueprints file that will
112 // be added to the aidl include paths.
113 Local_include_dirs []string
Martijn Coeneneab15642018-03-09 09:29:59 +0100114
115 // whether to generate traces (for systrace) for this interface
116 Generate_traces *bool
Dan Willemsene1240db2016-11-03 14:28:51 -0700117 }
118
Colin Cross2a252be2017-05-01 17:37:24 -0700119 Renderscript struct {
120 // list of directories that will be added to the llvm-rs-cc include paths
121 Include_dirs []string
122
123 // list of flags that will be passed to llvm-rs-cc
124 Flags []string
125
126 // Renderscript API level to target
127 Target_api *string
128 }
129
Colin Cross4d9c2d12016-07-29 12:48:20 -0700130 Debug, Release struct {
131 // list of module-specific flags that will be used for C and C++ compiles in debug or
132 // release builds
133 Cflags []string `android:"arch_variant"`
134 } `android:"arch_variant"`
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700135
136 Target struct {
137 Vendor struct {
138 // list of source files that should only be used in the
139 // vendor variant of the C/C++ module.
Colin Cross27b922f2019-03-04 22:35:41 -0800140 Srcs []string `android:"path"`
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700141
142 // list of source files that should not be used to
143 // build the vendor variant of the C/C++ module.
Colin Cross27b922f2019-03-04 22:35:41 -0800144 Exclude_srcs []string `android:"path"`
Jiyong Parka622de82017-08-10 13:33:27 +0900145
146 // List of additional cflags that should be used to build the vendor
147 // variant of the C/C++ module.
148 Cflags []string
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700149 }
Jiyong Parkf9332f12018-02-01 00:54:12 +0900150 Recovery struct {
151 // list of source files that should only be used in the
152 // recovery variant of the C/C++ module.
Colin Cross27b922f2019-03-04 22:35:41 -0800153 Srcs []string `android:"path"`
Jiyong Parkf9332f12018-02-01 00:54:12 +0900154
155 // list of source files that should not be used to
156 // build the recovery variant of the C/C++ module.
Colin Cross27b922f2019-03-04 22:35:41 -0800157 Exclude_srcs []string `android:"path"`
Jiyong Parkf9332f12018-02-01 00:54:12 +0900158
159 // List of additional cflags that should be used to build the recovery
160 // variant of the C/C++ module.
161 Cflags []string
162 }
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700163 }
Colin Cross10d22312017-05-03 11:01:58 -0700164
Colin Cross38f794e2017-09-07 10:53:07 -0700165 Proto struct {
166 // Link statically against the protobuf runtime
Nan Zhang0007d812017-11-07 10:57:05 -0800167 Static *bool `android:"arch_variant"`
Colin Cross38f794e2017-09-07 10:53:07 -0700168 } `android:"arch_variant"`
169
Colin Cross10d22312017-05-03 11:01:58 -0700170 // Stores the original list of source files before being cleared by library reuse
171 OriginalSrcs []string `blueprint:"mutated"`
Pirama Arumuga Nainarfadb7b52017-12-19 23:08:09 -0800172
173 // Build and link with OpenMP
174 Openmp *bool `android:"arch_variant"`
Colin Cross4d9c2d12016-07-29 12:48:20 -0700175}
176
Colin Crossb916a382016-07-29 17:28:03 -0700177func NewBaseCompiler() *baseCompiler {
178 return &baseCompiler{}
179}
180
Colin Cross4d9c2d12016-07-29 12:48:20 -0700181type baseCompiler struct {
182 Properties BaseCompilerProperties
Colin Cross38f794e2017-09-07 10:53:07 -0700183 Proto android.ProtoProperties
Pirama Arumuga Nainarf231b192018-01-23 10:49:04 -0800184 cFlagsDeps android.Paths
Pirama Arumuga Nainar70ba5a32017-12-19 15:11:01 -0800185 pathDeps android.Paths
Fabien Sanglardd61f1f42017-01-10 16:21:22 -0800186 flags builderFlags
Colin Crossf18e1102017-11-16 14:33:08 -0800187
188 // Sources that were passed to the C/C++ compiler
189 srcs android.Paths
190
191 // Sources that were passed in the Android.bp file, including generated sources generated by
192 // other modules and filegroups. May include source files that have not yet been translated to
193 // C/C++ (.aidl, .proto, etc.)
194 srcsBeforeGen android.Paths
Colin Cross4d9c2d12016-07-29 12:48:20 -0700195}
196
197var _ compiler = (*baseCompiler)(nil)
198
Fabien Sanglardd61f1f42017-01-10 16:21:22 -0800199type CompiledInterface interface {
200 Srcs() android.Paths
201}
202
203func (compiler *baseCompiler) Srcs() android.Paths {
Nan Zhange42777a2018-03-27 16:19:42 -0700204 return append(android.Paths{}, compiler.srcs...)
Fabien Sanglardd61f1f42017-01-10 16:21:22 -0800205}
206
Colin Cross4d9c2d12016-07-29 12:48:20 -0700207func (compiler *baseCompiler) appendCflags(flags []string) {
208 compiler.Properties.Cflags = append(compiler.Properties.Cflags, flags...)
209}
210
211func (compiler *baseCompiler) appendAsflags(flags []string) {
212 compiler.Properties.Asflags = append(compiler.Properties.Asflags, flags...)
213}
214
Colin Cross42742b82016-08-01 13:20:05 -0700215func (compiler *baseCompiler) compilerProps() []interface{} {
Colin Cross0feb1692016-11-03 14:38:52 -0700216 return []interface{}{&compiler.Properties, &compiler.Proto}
Colin Cross4d9c2d12016-07-29 12:48:20 -0700217}
218
Colin Cross42742b82016-08-01 13:20:05 -0700219func (compiler *baseCompiler) compilerInit(ctx BaseModuleContext) {}
Colin Cross4d9c2d12016-07-29 12:48:20 -0700220
Colin Cross37047f12016-12-13 17:06:13 -0800221func (compiler *baseCompiler) compilerDeps(ctx DepsContext, deps Deps) Deps {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700222 deps.GeneratedSources = append(deps.GeneratedSources, compiler.Properties.Generated_sources...)
223 deps.GeneratedHeaders = append(deps.GeneratedHeaders, compiler.Properties.Generated_headers...)
224
Colin Crossfe17f6f2019-03-28 19:30:56 -0700225 android.ProtoDeps(ctx, &compiler.Proto)
Dan Willemsene1a3ce32016-11-02 20:44:08 -0700226 if compiler.hasSrcExt(".proto") {
Nan Zhang0007d812017-11-07 10:57:05 -0800227 deps = protoDeps(ctx, deps, &compiler.Proto, Bool(compiler.Properties.Proto.Static))
Colin Cross0c461f12016-10-20 16:11:43 -0700228 }
229
Pirama Arumuga Nainarfadb7b52017-12-19 23:08:09 -0800230 if Bool(compiler.Properties.Openmp) {
231 deps.StaticLibs = append(deps.StaticLibs, "libomp")
232 }
233
Colin Cross4d9c2d12016-07-29 12:48:20 -0700234 return deps
235}
236
Chih-Hung Hsieh64a38dc2017-11-14 14:09:14 -0800237// Return true if the module is in the WarningAllowedProjects.
238func warningsAreAllowed(subdir string) bool {
239 subdir += "/"
240 for _, prefix := range config.WarningAllowedProjects {
241 if strings.HasPrefix(subdir, prefix) {
242 return true
243 }
244 }
245 return false
246}
247
Colin Cross571cccf2019-02-04 11:22:08 -0800248func addToModuleList(ctx ModuleContext, key android.OnceKey, module string) {
249 getNamedMapForConfig(ctx.Config(), key).Store(module, true)
Chih-Hung Hsieh64a38dc2017-11-14 14:09:14 -0800250}
251
Colin Cross4d9c2d12016-07-29 12:48:20 -0700252// Create a Flags struct that collects the compile flags from global values,
253// per-target values, module type values, and per-module Blueprints properties
Colin Crossf18e1102017-11-16 14:33:08 -0800254func (compiler *baseCompiler) compilerFlags(ctx ModuleContext, flags Flags, deps PathDeps) Flags {
Colin Crossb98c8b02016-07-29 13:44:28 -0700255 tc := ctx.toolchain()
Yi Kong950e0ba2019-10-08 02:27:17 -0700256 modulePath := android.PathForModuleSrc(ctx).String()
Colin Cross4d9c2d12016-07-29 12:48:20 -0700257
Colin Cross8a497952019-03-05 22:25:09 -0800258 compiler.srcsBeforeGen = android.PathsForModuleSrcExcludes(ctx, compiler.Properties.Srcs, compiler.Properties.Exclude_srcs)
Colin Crossf18e1102017-11-16 14:33:08 -0800259 compiler.srcsBeforeGen = append(compiler.srcsBeforeGen, deps.GeneratedSources...)
260
Colin Cross4d9c2d12016-07-29 12:48:20 -0700261 CheckBadCompilerFlags(ctx, "cflags", compiler.Properties.Cflags)
262 CheckBadCompilerFlags(ctx, "cppflags", compiler.Properties.Cppflags)
263 CheckBadCompilerFlags(ctx, "conlyflags", compiler.Properties.Conlyflags)
264 CheckBadCompilerFlags(ctx, "asflags", compiler.Properties.Asflags)
Jiyong Park4a2dcb52018-05-24 16:44:14 +0900265 CheckBadCompilerFlags(ctx, "vendor.cflags", compiler.Properties.Target.Vendor.Cflags)
266 CheckBadCompilerFlags(ctx, "recovery.cflags", compiler.Properties.Target.Recovery.Cflags)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700267
Colin Cross0b9f31f2019-02-28 11:00:01 -0800268 esc := proptools.NinjaAndShellEscapeList
Colin Cross4b963f82016-09-29 14:06:02 -0700269
Colin Cross4af21ed2019-11-04 09:37:55 -0800270 flags.Local.CFlags = append(flags.Local.CFlags, esc(compiler.Properties.Cflags)...)
271 flags.Local.CppFlags = append(flags.Local.CppFlags, esc(compiler.Properties.Cppflags)...)
272 flags.Local.ConlyFlags = append(flags.Local.ConlyFlags, esc(compiler.Properties.Conlyflags)...)
273 flags.Local.AsFlags = append(flags.Local.AsFlags, esc(compiler.Properties.Asflags)...)
274 flags.Local.YasmFlags = append(flags.Local.YasmFlags, esc(compiler.Properties.Asflags)...)
Dan Willemsen4e0aa232019-04-10 22:59:54 -0700275
276 flags.Yacc = compiler.Properties.Yacc
Colin Cross4d9c2d12016-07-29 12:48:20 -0700277
278 // Include dir cflags
Colin Cross4d9c2d12016-07-29 12:48:20 -0700279 localIncludeDirs := android.PathsForModuleSrc(ctx, compiler.Properties.Local_include_dirs)
Dan Willemsen273af7f2016-11-03 15:53:42 -0700280 if len(localIncludeDirs) > 0 {
Colin Crossdad8c952017-04-26 14:55:27 -0700281 f := includeDirsToFlags(localIncludeDirs)
Colin Cross4af21ed2019-11-04 09:37:55 -0800282 flags.Local.CommonFlags = append(flags.Local.CommonFlags, f)
283 flags.Local.YasmFlags = append(flags.Local.YasmFlags, f)
Dan Willemsen273af7f2016-11-03 15:53:42 -0700284 }
285 rootIncludeDirs := android.PathsForSource(ctx, compiler.Properties.Include_dirs)
286 if len(rootIncludeDirs) > 0 {
Colin Crossdad8c952017-04-26 14:55:27 -0700287 f := includeDirsToFlags(rootIncludeDirs)
Colin Cross4af21ed2019-11-04 09:37:55 -0800288 flags.Local.CommonFlags = append(flags.Local.CommonFlags, f)
289 flags.Local.YasmFlags = append(flags.Local.YasmFlags, f)
Dan Willemsen273af7f2016-11-03 15:53:42 -0700290 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700291
Dan Albert899c23e2018-12-06 11:04:03 -0800292 if compiler.Properties.Include_build_directory == nil ||
293 *compiler.Properties.Include_build_directory {
Yi Kong950e0ba2019-10-08 02:27:17 -0700294 flags.Local.CommonFlags = append(flags.Local.CommonFlags, "-I"+modulePath)
295 flags.Local.YasmFlags = append(flags.Local.YasmFlags, "-I"+modulePath)
Dan Albert899c23e2018-12-06 11:04:03 -0800296 }
Colin Crossc3199482017-03-30 15:03:04 -0700297
Colin Cross87dd9632017-11-03 13:31:05 -0700298 if !(ctx.useSdk() || ctx.useVndk()) || ctx.Host() {
299 flags.SystemIncludeFlags = append(flags.SystemIncludeFlags,
300 "${config.CommonGlobalIncludes}",
301 tc.IncludeFlags(),
302 "${config.CommonNativehelperInclude}")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700303 }
304
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700305 if ctx.useSdk() {
Dan Albertfac114b2018-11-14 21:22:00 -0800306 // TODO: Switch to --sysroot.
Colin Cross4d9c2d12016-07-29 12:48:20 -0700307 // The NDK headers are installed to a common sysroot. While a more
308 // typical Soong approach would be to only make the headers for the
309 // library you're using available, we're trying to emulate the NDK
310 // behavior here, and the NDK always has all the NDK headers available.
Colin Crossc3199482017-03-30 15:03:04 -0700311 flags.SystemIncludeFlags = append(flags.SystemIncludeFlags,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700312 "-isystem "+getCurrentIncludePath(ctx).String(),
Chih-Hung Hsieh1e7d1bf2018-03-15 18:44:57 -0700313 "-isystem "+getCurrentIncludePath(ctx).Join(ctx, config.NDKTriple(tc)).String())
Colin Cross4d9c2d12016-07-29 12:48:20 -0700314 }
315
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700316 if ctx.useVndk() {
Colin Cross4af21ed2019-11-04 09:37:55 -0800317 flags.Global.CommonFlags = append(flags.Global.CommonFlags, "-D__ANDROID_VNDK__")
Dan Willemsenb916b802017-03-19 13:44:32 -0700318 }
319
Jiyong Parkd54aee52018-06-09 01:32:54 +0900320 if ctx.inRecovery() {
Colin Cross4af21ed2019-11-04 09:37:55 -0800321 flags.Global.CommonFlags = append(flags.Global.CommonFlags, "-D__ANDROID_RECOVERY__")
Jiyong Parkd54aee52018-06-09 01:32:54 +0900322 }
323
Jiyong Park58e364a2019-01-19 19:24:06 +0900324 if ctx.apexName() != "" {
Colin Cross4af21ed2019-11-04 09:37:55 -0800325 flags.Global.CommonFlags = append(flags.Global.CommonFlags,
326 "-D__ANDROID_APEX__",
327 "-D__ANDROID_APEX_"+makeDefineString(ctx.apexName())+"__")
Jiyong Park58e364a2019-01-19 19:24:06 +0900328 }
329
Nan Zhang0007d812017-11-07 10:57:05 -0800330 instructionSet := String(compiler.Properties.Instruction_set)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700331 if flags.RequiredInstructionSet != "" {
332 instructionSet = flags.RequiredInstructionSet
333 }
Dan Willemsen8536d6b2018-10-07 20:54:34 -0700334 instructionSetFlags, err := tc.ClangInstructionSetFlags(instructionSet)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700335 if err != nil {
336 ctx.ModuleErrorf("%s", err)
337 }
338
339 CheckBadCompilerFlags(ctx, "release.cflags", compiler.Properties.Release.Cflags)
340
341 // TODO: debug
Colin Cross4af21ed2019-11-04 09:37:55 -0800342 flags.Local.CFlags = append(flags.Local.CFlags, esc(compiler.Properties.Release.Cflags)...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700343
Dan Willemsen8536d6b2018-10-07 20:54:34 -0700344 CheckBadCompilerFlags(ctx, "clang_cflags", compiler.Properties.Clang_cflags)
345 CheckBadCompilerFlags(ctx, "clang_asflags", compiler.Properties.Clang_asflags)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700346
Colin Cross4af21ed2019-11-04 09:37:55 -0800347 flags.Local.CFlags = config.ClangFilterUnknownCflags(flags.Local.CFlags)
348 flags.Local.CFlags = append(flags.Local.CFlags, esc(compiler.Properties.Clang_cflags)...)
349 flags.Local.AsFlags = append(flags.Local.AsFlags, esc(compiler.Properties.Clang_asflags)...)
350 flags.Local.CppFlags = config.ClangFilterUnknownCflags(flags.Local.CppFlags)
351 flags.Local.ConlyFlags = config.ClangFilterUnknownCflags(flags.Local.ConlyFlags)
352 flags.Local.LdFlags = config.ClangFilterUnknownCflags(flags.Local.LdFlags)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700353
Dan Willemsen8536d6b2018-10-07 20:54:34 -0700354 target := "-target " + tc.ClangTriple()
Peter Collingbourneb0e61432019-07-22 16:36:06 -0700355 if ctx.Os().Class == android.Device {
356 version := ctx.sdkVersion()
357 if version == "" || version == "current" {
358 target += strconv.Itoa(android.FutureApiLevel)
359 } else {
360 target += version
361 }
362 }
363
Dan Willemsen8536d6b2018-10-07 20:54:34 -0700364 gccPrefix := "-B" + config.ToolPath(tc)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700365
Colin Cross4af21ed2019-11-04 09:37:55 -0800366 flags.Global.CFlags = append(flags.Global.CFlags, target, gccPrefix)
367 flags.Global.AsFlags = append(flags.Global.AsFlags, target, gccPrefix)
368 flags.Global.LdFlags = append(flags.Global.LdFlags, target, gccPrefix)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700369
Colin Crossb98c8b02016-07-29 13:44:28 -0700370 hod := "Host"
Colin Cross4d9c2d12016-07-29 12:48:20 -0700371 if ctx.Os().Class == android.Device {
Colin Crossb98c8b02016-07-29 13:44:28 -0700372 hod = "Device"
Colin Cross4d9c2d12016-07-29 12:48:20 -0700373 }
374
Colin Cross4af21ed2019-11-04 09:37:55 -0800375 flags.Global.CommonFlags = append(flags.Global.CommonFlags, instructionSetFlags)
376 flags.Global.ConlyFlags = append([]string{"${config.CommonGlobalConlyflags}"}, flags.Global.ConlyFlags...)
377 flags.Global.CppFlags = append([]string{fmt.Sprintf("${config.%sGlobalCppflags}", hod)}, flags.Global.CppFlags...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700378
Colin Cross4af21ed2019-11-04 09:37:55 -0800379 flags.Global.AsFlags = append(flags.Global.AsFlags, tc.ClangAsflags())
380 flags.Global.CppFlags = append([]string{"${config.CommonClangGlobalCppflags}"}, flags.Global.CppFlags...)
381 flags.Global.CommonFlags = append(flags.Global.CommonFlags,
Dan Willemsen8536d6b2018-10-07 20:54:34 -0700382 tc.ClangCflags(),
383 "${config.CommonClangGlobalCflags}",
384 fmt.Sprintf("${config.%sClangGlobalCflags}", hod))
Colin Cross4d9c2d12016-07-29 12:48:20 -0700385
Yi Kong950e0ba2019-10-08 02:27:17 -0700386 if isThirdParty(modulePath) {
Colin Cross4af21ed2019-11-04 09:37:55 -0800387 flags.Global.CommonFlags = append([]string{"${config.ClangExternalCflags}"}, flags.Global.CommonFlags...)
Yi Kongcc80f8d2018-06-06 14:42:44 -0700388 }
389
Dan Willemsen10db3842018-10-21 18:42:46 -0700390 if tc.Bionic() {
Colin Cross87dd9632017-11-03 13:31:05 -0700391 if Bool(compiler.Properties.Rtti) {
Colin Cross4af21ed2019-11-04 09:37:55 -0800392 flags.Local.CppFlags = append(flags.Local.CppFlags, "-frtti")
Colin Cross87dd9632017-11-03 13:31:05 -0700393 } else {
Colin Cross4af21ed2019-11-04 09:37:55 -0800394 flags.Local.CppFlags = append(flags.Local.CppFlags, "-fno-rtti")
Colin Cross87dd9632017-11-03 13:31:05 -0700395 }
396 }
397
Colin Cross4af21ed2019-11-04 09:37:55 -0800398 flags.Global.AsFlags = append(flags.Global.AsFlags, "-D__ASSEMBLY__")
Colin Cross87dd9632017-11-03 13:31:05 -0700399
Colin Cross4af21ed2019-11-04 09:37:55 -0800400 flags.Global.CppFlags = append(flags.Global.CppFlags, tc.ClangCppflags())
Colin Cross87dd9632017-11-03 13:31:05 -0700401
Colin Cross4af21ed2019-11-04 09:37:55 -0800402 flags.Global.YasmFlags = append(flags.Global.YasmFlags, tc.YasmFlags())
Colin Cross87dd9632017-11-03 13:31:05 -0700403
Colin Cross4af21ed2019-11-04 09:37:55 -0800404 flags.Global.CommonFlags = append(flags.Global.CommonFlags, tc.ToolchainClangCflags())
Colin Cross4d9c2d12016-07-29 12:48:20 -0700405
Elliott Hughes5789ca92018-02-16 17:15:19 -0800406 cStd := config.CStdVersion
407 if String(compiler.Properties.C_std) == "experimental" {
408 cStd = config.ExperimentalCStdVersion
409 } else if String(compiler.Properties.C_std) != "" {
410 cStd = String(compiler.Properties.C_std)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700411 }
412
Elliott Hughes5789ca92018-02-16 17:15:19 -0800413 cppStd := String(compiler.Properties.Cpp_std)
414 switch String(compiler.Properties.Cpp_std) {
415 case "":
416 cppStd = config.CppStdVersion
417 case "experimental":
418 cppStd = config.ExperimentalCppStdVersion
Elliott Hughes5789ca92018-02-16 17:15:19 -0800419 }
420
Elliott Hughes5789ca92018-02-16 17:15:19 -0800421 if compiler.Properties.Gnu_extensions != nil && *compiler.Properties.Gnu_extensions == false {
422 cStd = gnuToCReplacer.Replace(cStd)
423 cppStd = gnuToCReplacer.Replace(cppStd)
424 }
425
Colin Cross4af21ed2019-11-04 09:37:55 -0800426 flags.Local.ConlyFlags = append([]string{"-std=" + cStd}, flags.Local.ConlyFlags...)
427 flags.Local.CppFlags = append([]string{"-std=" + cppStd}, flags.Local.CppFlags...)
Elliott Hughes5789ca92018-02-16 17:15:19 -0800428
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700429 if ctx.useVndk() {
Colin Cross4af21ed2019-11-04 09:37:55 -0800430 flags.Local.CFlags = append(flags.Local.CFlags, esc(compiler.Properties.Target.Vendor.Cflags)...)
Jiyong Parka622de82017-08-10 13:33:27 +0900431 }
432
Jiyong Park4a2dcb52018-05-24 16:44:14 +0900433 if ctx.inRecovery() {
Colin Cross4af21ed2019-11-04 09:37:55 -0800434 flags.Local.CFlags = append(flags.Local.CFlags, esc(compiler.Properties.Target.Recovery.Cflags)...)
Jiyong Park4a2dcb52018-05-24 16:44:14 +0900435 }
436
Colin Cross4d9c2d12016-07-29 12:48:20 -0700437 // We can enforce some rules more strictly in the code we own. strict
438 // indicates if this is code that we can be stricter with. If we have
439 // rules that we want to apply to *our* code (but maybe can't for
440 // vendor/device specific things), we could extend this to be a ternary
441 // value.
442 strict := true
Yi Kong950e0ba2019-10-08 02:27:17 -0700443 if strings.HasPrefix(modulePath, "external/") {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700444 strict = false
445 }
446
447 // Can be used to make some annotations stricter for code we can fix
448 // (such as when we mark functions as deprecated).
449 if strict {
Colin Cross4af21ed2019-11-04 09:37:55 -0800450 flags.Global.CFlags = append(flags.Global.CFlags, "-DANDROID_STRICT")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700451 }
452
Dan Willemsene1a3ce32016-11-02 20:44:08 -0700453 if compiler.hasSrcExt(".proto") {
Colin Cross0c461f12016-10-20 16:11:43 -0700454 flags = protoFlags(ctx, flags, &compiler.Proto)
455 }
456
Dan Willemsene1a3ce32016-11-02 20:44:08 -0700457 if compiler.hasSrcExt(".y") || compiler.hasSrcExt(".yy") {
Colin Cross4af21ed2019-11-04 09:37:55 -0800458 flags.Local.CommonFlags = append(flags.Local.CommonFlags,
Dan Willemsene1a3ce32016-11-02 20:44:08 -0700459 "-I"+android.PathForModuleGen(ctx, "yacc", ctx.ModuleDir()).String())
460 }
461
Dan Willemsen4f1c3d42017-09-09 01:15:26 -0700462 if compiler.hasSrcExt(".mc") {
Colin Cross4af21ed2019-11-04 09:37:55 -0800463 flags.Local.CommonFlags = append(flags.Local.CommonFlags,
Dan Willemsen4f1c3d42017-09-09 01:15:26 -0700464 "-I"+android.PathForModuleGen(ctx, "windmc", ctx.ModuleDir()).String())
465 }
466
Dan Willemsene1240db2016-11-03 14:28:51 -0700467 if compiler.hasSrcExt(".aidl") {
468 if len(compiler.Properties.Aidl.Local_include_dirs) > 0 {
469 localAidlIncludeDirs := android.PathsForModuleSrc(ctx, compiler.Properties.Aidl.Local_include_dirs)
470 flags.aidlFlags = append(flags.aidlFlags, includeDirsToFlags(localAidlIncludeDirs))
471 }
472 if len(compiler.Properties.Aidl.Include_dirs) > 0 {
473 rootAidlIncludeDirs := android.PathsForSource(ctx, compiler.Properties.Aidl.Include_dirs)
474 flags.aidlFlags = append(flags.aidlFlags, includeDirsToFlags(rootAidlIncludeDirs))
475 }
476
Martijn Coeneneab15642018-03-09 09:29:59 +0100477 if Bool(compiler.Properties.Aidl.Generate_traces) {
478 flags.aidlFlags = append(flags.aidlFlags, "-t")
479 }
480
Colin Cross4af21ed2019-11-04 09:37:55 -0800481 flags.Local.CommonFlags = append(flags.Local.CommonFlags,
Dan Willemsene1240db2016-11-03 14:28:51 -0700482 "-I"+android.PathForModuleGen(ctx, "aidl").String())
483 }
484
Jeff Vander Stoepd6126272019-07-15 19:08:37 -0700485 if compiler.hasSrcExt(".rscript") || compiler.hasSrcExt(".fs") {
Colin Cross2a252be2017-05-01 17:37:24 -0700486 flags = rsFlags(ctx, flags, &compiler.Properties)
487 }
488
Inseob Kim21f26902018-09-06 00:55:20 +0900489 if compiler.hasSrcExt(".sysprop") {
Colin Cross4af21ed2019-11-04 09:37:55 -0800490 flags.Local.CommonFlags = append(flags.Local.CommonFlags,
Inseob Kim21f26902018-09-06 00:55:20 +0900491 "-I"+android.PathForModuleGen(ctx, "sysprop", "include").String())
492 }
493
Chih-Hung Hsieh64a38dc2017-11-14 14:09:14 -0800494 if len(compiler.Properties.Srcs) > 0 {
495 module := ctx.ModuleDir() + "/Android.bp:" + ctx.ModuleName()
Colin Cross4af21ed2019-11-04 09:37:55 -0800496 if inList("-Wno-error", flags.Local.CFlags) || inList("-Wno-error", flags.Local.CppFlags) {
Colin Cross571cccf2019-02-04 11:22:08 -0800497 addToModuleList(ctx, modulesUsingWnoErrorKey, module)
Colin Cross4af21ed2019-11-04 09:37:55 -0800498 } else if !inList("-Werror", flags.Local.CFlags) && !inList("-Werror", flags.Local.CppFlags) {
Chih-Hung Hsieh64a38dc2017-11-14 14:09:14 -0800499 if warningsAreAllowed(ctx.ModuleDir()) {
Colin Cross571cccf2019-02-04 11:22:08 -0800500 addToModuleList(ctx, modulesAddedWallKey, module)
Colin Cross4af21ed2019-11-04 09:37:55 -0800501 flags.Local.CFlags = append([]string{"-Wall"}, flags.Local.CFlags...)
Chih-Hung Hsieh64a38dc2017-11-14 14:09:14 -0800502 } else {
Colin Cross4af21ed2019-11-04 09:37:55 -0800503 flags.Local.CFlags = append([]string{"-Wall", "-Werror"}, flags.Local.CFlags...)
Chih-Hung Hsieh64a38dc2017-11-14 14:09:14 -0800504 }
505 }
506 }
507
Pirama Arumuga Nainarfadb7b52017-12-19 23:08:09 -0800508 if Bool(compiler.Properties.Openmp) {
Colin Cross4af21ed2019-11-04 09:37:55 -0800509 flags.Local.CFlags = append(flags.Local.CFlags, "-fopenmp")
Pirama Arumuga Nainarfadb7b52017-12-19 23:08:09 -0800510 }
511
Colin Cross4d9c2d12016-07-29 12:48:20 -0700512 return flags
513}
514
Dan Willemsene1a3ce32016-11-02 20:44:08 -0700515func (compiler *baseCompiler) hasSrcExt(ext string) bool {
Colin Crossf18e1102017-11-16 14:33:08 -0800516 for _, src := range compiler.srcsBeforeGen {
517 if src.Ext() == ext {
518 return true
519 }
520 }
Colin Cross0c461f12016-10-20 16:11:43 -0700521 for _, src := range compiler.Properties.Srcs {
Dan Willemsene1a3ce32016-11-02 20:44:08 -0700522 if filepath.Ext(src) == ext {
Colin Cross0c461f12016-10-20 16:11:43 -0700523 return true
524 }
525 }
Colin Cross10d22312017-05-03 11:01:58 -0700526 for _, src := range compiler.Properties.OriginalSrcs {
527 if filepath.Ext(src) == ext {
528 return true
529 }
530 }
Colin Cross0c461f12016-10-20 16:11:43 -0700531
532 return false
533}
534
Jooyung Han77988572019-10-18 16:26:16 +0900535// makeDefineString transforms a name of an APEX module into a value to be used as value for C define
536// For example, com.android.foo => COM_ANDROID_FOO
537func makeDefineString(name string) string {
538 return strings.ReplaceAll(strings.ToUpper(name), ".", "_")
539}
540
Colin Cross948f0cb2016-10-17 14:24:56 -0700541var gnuToCReplacer = strings.NewReplacer("gnu", "c")
542
Colin Cross4d9c2d12016-07-29 12:48:20 -0700543func ndkPathDeps(ctx ModuleContext) android.Paths {
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700544 if ctx.useSdk() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700545 // The NDK sysroot timestamp file depends on all the NDK sysroot files
546 // (headers and libraries).
Dan Albert6ab43d82017-12-13 15:05:04 -0800547 return android.Paths{getNdkBaseTimestampFile(ctx)}
Colin Cross4d9c2d12016-07-29 12:48:20 -0700548 }
549 return nil
550}
551
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700552func (compiler *baseCompiler) compile(ctx ModuleContext, flags Flags, deps PathDeps) Objects {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700553 pathDeps := deps.GeneratedHeaders
554 pathDeps = append(pathDeps, ndkPathDeps(ctx)...)
Colin Cross2f336352016-10-26 10:03:47 -0700555
Colin Cross2f336352016-10-26 10:03:47 -0700556 buildFlags := flagsToBuilderFlags(flags)
557
Colin Crossf18e1102017-11-16 14:33:08 -0800558 srcs := append(android.Paths(nil), compiler.srcsBeforeGen...)
559
Colin Cross2f336352016-10-26 10:03:47 -0700560 srcs, genDeps := genSources(ctx, srcs, buildFlags)
Pirama Arumuga Nainarf231b192018-01-23 10:49:04 -0800561 pathDeps = append(pathDeps, genDeps...)
Colin Cross2f336352016-10-26 10:03:47 -0700562
Pirama Arumuga Nainar70ba5a32017-12-19 15:11:01 -0800563 compiler.pathDeps = pathDeps
Pirama Arumuga Nainarf231b192018-01-23 10:49:04 -0800564 compiler.cFlagsDeps = flags.CFlagsDeps
Colin Cross2f336352016-10-26 10:03:47 -0700565
Fabien Sanglardd61f1f42017-01-10 16:21:22 -0800566 // Save src, buildFlags and context
567 compiler.srcs = srcs
568
Colin Cross4d9c2d12016-07-29 12:48:20 -0700569 // Compile files listed in c.Properties.Srcs into objects
Pirama Arumuga Nainarf231b192018-01-23 10:49:04 -0800570 objs := compileObjs(ctx, buildFlags, "", srcs, pathDeps, compiler.cFlagsDeps)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700571
572 if ctx.Failed() {
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700573 return Objects{}
Colin Cross4d9c2d12016-07-29 12:48:20 -0700574 }
575
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700576 return objs
Colin Cross4d9c2d12016-07-29 12:48:20 -0700577}
578
579// Compile a list of source files into objects a specified subdirectory
Colin Cross2f336352016-10-26 10:03:47 -0700580func compileObjs(ctx android.ModuleContext, flags builderFlags,
Pirama Arumuga Nainarf231b192018-01-23 10:49:04 -0800581 subdir string, srcFiles, pathDeps android.Paths, cFlagsDeps android.Paths) Objects {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700582
Pirama Arumuga Nainarf231b192018-01-23 10:49:04 -0800583 return TransformSourceToObj(ctx, subdir, srcFiles, flags, pathDeps, cFlagsDeps)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700584}
Yi Kong950e0ba2019-10-08 02:27:17 -0700585
586var thirdPartyDirPrefixExceptions = []*regexp.Regexp{
587 regexp.MustCompile("^vendor/[^/]*google[^/]*/"),
588 regexp.MustCompile("^hardware/google/"),
589 regexp.MustCompile("^hardware/interfaces/"),
590 regexp.MustCompile("^hardware/libhardware[^/]*/"),
591 regexp.MustCompile("^hardware/ril/"),
592}
593
594func isThirdParty(path string) bool {
595 thirdPartyDirPrefixes := []string{"external/", "vendor/", "hardware/"}
596
597 for _, prefix := range thirdPartyDirPrefixes {
598 if strings.HasPrefix(path, prefix) {
599 for _, prefix := range thirdPartyDirPrefixExceptions {
600 if prefix.MatchString(path) {
601 return false
602 }
603 }
604 break
605 }
606 }
607
608 return true
609}