blob: 7667ae7abaaf53f7b110e996e352929fb3f691ce [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"
20 "strings"
21
Colin Cross4b963f82016-09-29 14:06:02 -070022 "github.com/google/blueprint/proptools"
23
Colin Cross4d9c2d12016-07-29 12:48:20 -070024 "android/soong/android"
Colin Crossb98c8b02016-07-29 13:44:28 -070025 "android/soong/cc/config"
Colin Cross4d9c2d12016-07-29 12:48:20 -070026)
27
28// This file contains the basic C/C++/assembly to .o compliation steps
29
30type BaseCompilerProperties struct {
31 // 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 -080032 // srcs may reference the outputs of other modules that produce source files like genrule
33 // or filegroup using the syntax ":module".
Colin Cross27b922f2019-03-04 22:35:41 -080034 Srcs []string `android:"path,arch_variant"`
Colin Cross4d9c2d12016-07-29 12:48:20 -070035
36 // list of source files that should not be used to build the C/C++ module.
37 // This is most useful in the arch/multilib variants to remove non-common files
Colin Cross27b922f2019-03-04 22:35:41 -080038 Exclude_srcs []string `android:"path,arch_variant"`
Colin Cross4d9c2d12016-07-29 12:48:20 -070039
40 // list of module-specific flags that will be used for C and C++ compiles.
41 Cflags []string `android:"arch_variant"`
42
43 // list of module-specific flags that will be used for C++ compiles
44 Cppflags []string `android:"arch_variant"`
45
46 // list of module-specific flags that will be used for C compiles
47 Conlyflags []string `android:"arch_variant"`
48
49 // list of module-specific flags that will be used for .S compiles
50 Asflags []string `android:"arch_variant"`
51
52 // list of module-specific flags that will be used for C and C++ compiles when
53 // compiling with clang
54 Clang_cflags []string `android:"arch_variant"`
55
56 // list of module-specific flags that will be used for .S compiles when
57 // compiling with clang
58 Clang_asflags []string `android:"arch_variant"`
59
Colin Cross4d9c2d12016-07-29 12:48:20 -070060 // the instruction set architecture to use to compile the C/C++
61 // module.
Dan Willemsen5922f3c2017-10-25 15:20:50 -070062 Instruction_set *string `android:"arch_variant"`
Colin Cross4d9c2d12016-07-29 12:48:20 -070063
64 // list of directories relative to the root of the source tree that will
65 // be added to the include path using -I.
66 // If possible, don't use this. If adding paths from the current directory use
67 // local_include_dirs, if adding paths from other modules use export_include_dirs in
68 // that module.
Colin Crossccf01e72017-04-26 17:01:07 -070069 Include_dirs []string `android:"arch_variant,variant_prepend"`
Colin Cross4d9c2d12016-07-29 12:48:20 -070070
71 // list of directories relative to the Blueprints file that will
72 // be added to the include path using -I
Dan Willemsen59339a22018-07-22 21:18:45 -070073 Local_include_dirs []string `android:"arch_variant,variant_prepend"`
Colin Cross4d9c2d12016-07-29 12:48:20 -070074
Dan Albert899c23e2018-12-06 11:04:03 -080075 // Add the directory containing the Android.bp file to the list of include
76 // directories. Defaults to true.
77 Include_build_directory *bool
78
Colin Cross4d9c2d12016-07-29 12:48:20 -070079 // list of generated sources to compile. These are the names of gensrcs or
80 // genrule modules.
81 Generated_sources []string `android:"arch_variant"`
82
83 // list of generated headers to add to the include path. These are the names
84 // of genrule modules.
85 Generated_headers []string `android:"arch_variant"`
86
87 // pass -frtti instead of -fno-rtti
88 Rtti *bool
89
Dan Albert043833c2017-02-03 16:13:38 -080090 // C standard version to use. Can be a specific version (such as "gnu11"),
91 // "experimental" (which will use draft versions like C1x when available),
92 // or the empty string (which will use the default).
Nan Zhang0007d812017-11-07 10:57:05 -080093 C_std *string
Dan Albert043833c2017-02-03 16:13:38 -080094
95 // C++ standard version to use. Can be a specific version (such as
96 // "gnu++11"), "experimental" (which will use draft versions like C++1z when
97 // available), or the empty string (which will use the default).
Nan Zhang0007d812017-11-07 10:57:05 -080098 Cpp_std *string
Dan Albert043833c2017-02-03 16:13:38 -080099
Colin Cross948f0cb2016-10-17 14:24:56 -0700100 // if set to false, use -std=c++* instead of -std=gnu++*
101 Gnu_extensions *bool
102
Dan Willemsen4e0aa232019-04-10 22:59:54 -0700103 Yacc *YaccProperties
104
Dan Willemsene1240db2016-11-03 14:28:51 -0700105 Aidl struct {
106 // list of directories that will be added to the aidl include paths.
107 Include_dirs []string
108
109 // list of directories relative to the Blueprints file that will
110 // be added to the aidl include paths.
111 Local_include_dirs []string
Martijn Coeneneab15642018-03-09 09:29:59 +0100112
113 // whether to generate traces (for systrace) for this interface
114 Generate_traces *bool
Dan Willemsene1240db2016-11-03 14:28:51 -0700115 }
116
Colin Cross2a252be2017-05-01 17:37:24 -0700117 Renderscript struct {
118 // list of directories that will be added to the llvm-rs-cc include paths
119 Include_dirs []string
120
121 // list of flags that will be passed to llvm-rs-cc
122 Flags []string
123
124 // Renderscript API level to target
125 Target_api *string
126 }
127
Colin Cross4d9c2d12016-07-29 12:48:20 -0700128 Debug, Release struct {
129 // list of module-specific flags that will be used for C and C++ compiles in debug or
130 // release builds
131 Cflags []string `android:"arch_variant"`
132 } `android:"arch_variant"`
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700133
134 Target struct {
135 Vendor struct {
136 // list of source files that should only be used in the
137 // vendor variant of the C/C++ module.
Colin Cross27b922f2019-03-04 22:35:41 -0800138 Srcs []string `android:"path"`
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700139
140 // list of source files that should not be used to
141 // build the vendor variant of the C/C++ module.
Colin Cross27b922f2019-03-04 22:35:41 -0800142 Exclude_srcs []string `android:"path"`
Jiyong Parka622de82017-08-10 13:33:27 +0900143
144 // List of additional cflags that should be used to build the vendor
145 // variant of the C/C++ module.
146 Cflags []string
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700147 }
Jiyong Parkf9332f12018-02-01 00:54:12 +0900148 Recovery struct {
149 // list of source files that should only be used in the
150 // recovery variant of the C/C++ module.
Colin Cross27b922f2019-03-04 22:35:41 -0800151 Srcs []string `android:"path"`
Jiyong Parkf9332f12018-02-01 00:54:12 +0900152
153 // list of source files that should not be used to
154 // build the recovery variant of the C/C++ module.
Colin Cross27b922f2019-03-04 22:35:41 -0800155 Exclude_srcs []string `android:"path"`
Jiyong Parkf9332f12018-02-01 00:54:12 +0900156
157 // List of additional cflags that should be used to build the recovery
158 // variant of the C/C++ module.
159 Cflags []string
160 }
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700161 }
Colin Cross10d22312017-05-03 11:01:58 -0700162
Colin Cross38f794e2017-09-07 10:53:07 -0700163 Proto struct {
164 // Link statically against the protobuf runtime
Nan Zhang0007d812017-11-07 10:57:05 -0800165 Static *bool `android:"arch_variant"`
Colin Cross38f794e2017-09-07 10:53:07 -0700166 } `android:"arch_variant"`
167
Colin Cross10d22312017-05-03 11:01:58 -0700168 // Stores the original list of source files before being cleared by library reuse
169 OriginalSrcs []string `blueprint:"mutated"`
Pirama Arumuga Nainarfadb7b52017-12-19 23:08:09 -0800170
171 // Build and link with OpenMP
172 Openmp *bool `android:"arch_variant"`
Colin Cross4d9c2d12016-07-29 12:48:20 -0700173}
174
Colin Crossb916a382016-07-29 17:28:03 -0700175func NewBaseCompiler() *baseCompiler {
176 return &baseCompiler{}
177}
178
Colin Cross4d9c2d12016-07-29 12:48:20 -0700179type baseCompiler struct {
180 Properties BaseCompilerProperties
Colin Cross38f794e2017-09-07 10:53:07 -0700181 Proto android.ProtoProperties
Pirama Arumuga Nainarf231b192018-01-23 10:49:04 -0800182 cFlagsDeps android.Paths
Pirama Arumuga Nainar70ba5a32017-12-19 15:11:01 -0800183 pathDeps android.Paths
Fabien Sanglardd61f1f42017-01-10 16:21:22 -0800184 flags builderFlags
Colin Crossf18e1102017-11-16 14:33:08 -0800185
186 // Sources that were passed to the C/C++ compiler
187 srcs android.Paths
188
189 // Sources that were passed in the Android.bp file, including generated sources generated by
190 // other modules and filegroups. May include source files that have not yet been translated to
191 // C/C++ (.aidl, .proto, etc.)
192 srcsBeforeGen android.Paths
Colin Cross4d9c2d12016-07-29 12:48:20 -0700193}
194
195var _ compiler = (*baseCompiler)(nil)
196
Fabien Sanglardd61f1f42017-01-10 16:21:22 -0800197type CompiledInterface interface {
198 Srcs() android.Paths
199}
200
201func (compiler *baseCompiler) Srcs() android.Paths {
Nan Zhange42777a2018-03-27 16:19:42 -0700202 return append(android.Paths{}, compiler.srcs...)
Fabien Sanglardd61f1f42017-01-10 16:21:22 -0800203}
204
Colin Cross4d9c2d12016-07-29 12:48:20 -0700205func (compiler *baseCompiler) appendCflags(flags []string) {
206 compiler.Properties.Cflags = append(compiler.Properties.Cflags, flags...)
207}
208
209func (compiler *baseCompiler) appendAsflags(flags []string) {
210 compiler.Properties.Asflags = append(compiler.Properties.Asflags, flags...)
211}
212
Colin Cross42742b82016-08-01 13:20:05 -0700213func (compiler *baseCompiler) compilerProps() []interface{} {
Colin Cross0feb1692016-11-03 14:38:52 -0700214 return []interface{}{&compiler.Properties, &compiler.Proto}
Colin Cross4d9c2d12016-07-29 12:48:20 -0700215}
216
Colin Cross42742b82016-08-01 13:20:05 -0700217func (compiler *baseCompiler) compilerInit(ctx BaseModuleContext) {}
Colin Cross4d9c2d12016-07-29 12:48:20 -0700218
Colin Cross37047f12016-12-13 17:06:13 -0800219func (compiler *baseCompiler) compilerDeps(ctx DepsContext, deps Deps) Deps {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700220 deps.GeneratedSources = append(deps.GeneratedSources, compiler.Properties.Generated_sources...)
221 deps.GeneratedHeaders = append(deps.GeneratedHeaders, compiler.Properties.Generated_headers...)
222
Colin Crossfe17f6f2019-03-28 19:30:56 -0700223 android.ProtoDeps(ctx, &compiler.Proto)
Dan Willemsene1a3ce32016-11-02 20:44:08 -0700224 if compiler.hasSrcExt(".proto") {
Nan Zhang0007d812017-11-07 10:57:05 -0800225 deps = protoDeps(ctx, deps, &compiler.Proto, Bool(compiler.Properties.Proto.Static))
Colin Cross0c461f12016-10-20 16:11:43 -0700226 }
227
Inseob Kim21f26902018-09-06 00:55:20 +0900228 if compiler.hasSrcExt(".sysprop") {
Inseob Kim1f959762019-03-27 17:20:37 +0900229 deps.HeaderLibs = append(deps.HeaderLibs, "libbase_headers")
230 deps.SharedLibs = append(deps.SharedLibs, "liblog")
Inseob Kim21f26902018-09-06 00:55:20 +0900231 }
232
Pirama Arumuga Nainarfadb7b52017-12-19 23:08:09 -0800233 if Bool(compiler.Properties.Openmp) {
234 deps.StaticLibs = append(deps.StaticLibs, "libomp")
235 }
236
Colin Cross4d9c2d12016-07-29 12:48:20 -0700237 return deps
238}
239
Chih-Hung Hsieh64a38dc2017-11-14 14:09:14 -0800240// Return true if the module is in the WarningAllowedProjects.
241func warningsAreAllowed(subdir string) bool {
242 subdir += "/"
243 for _, prefix := range config.WarningAllowedProjects {
244 if strings.HasPrefix(subdir, prefix) {
245 return true
246 }
247 }
248 return false
249}
250
Colin Cross571cccf2019-02-04 11:22:08 -0800251func addToModuleList(ctx ModuleContext, key android.OnceKey, module string) {
252 getNamedMapForConfig(ctx.Config(), key).Store(module, true)
Chih-Hung Hsieh64a38dc2017-11-14 14:09:14 -0800253}
254
Colin Cross4d9c2d12016-07-29 12:48:20 -0700255// Create a Flags struct that collects the compile flags from global values,
256// per-target values, module type values, and per-module Blueprints properties
Colin Crossf18e1102017-11-16 14:33:08 -0800257func (compiler *baseCompiler) compilerFlags(ctx ModuleContext, flags Flags, deps PathDeps) Flags {
Colin Crossb98c8b02016-07-29 13:44:28 -0700258 tc := ctx.toolchain()
Colin Cross4d9c2d12016-07-29 12:48:20 -0700259
Colin Cross8a497952019-03-05 22:25:09 -0800260 compiler.srcsBeforeGen = android.PathsForModuleSrcExcludes(ctx, compiler.Properties.Srcs, compiler.Properties.Exclude_srcs)
Colin Crossf18e1102017-11-16 14:33:08 -0800261 compiler.srcsBeforeGen = append(compiler.srcsBeforeGen, deps.GeneratedSources...)
262
Colin Cross4d9c2d12016-07-29 12:48:20 -0700263 CheckBadCompilerFlags(ctx, "cflags", compiler.Properties.Cflags)
264 CheckBadCompilerFlags(ctx, "cppflags", compiler.Properties.Cppflags)
265 CheckBadCompilerFlags(ctx, "conlyflags", compiler.Properties.Conlyflags)
266 CheckBadCompilerFlags(ctx, "asflags", compiler.Properties.Asflags)
Jiyong Park4a2dcb52018-05-24 16:44:14 +0900267 CheckBadCompilerFlags(ctx, "vendor.cflags", compiler.Properties.Target.Vendor.Cflags)
268 CheckBadCompilerFlags(ctx, "recovery.cflags", compiler.Properties.Target.Recovery.Cflags)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700269
Colin Cross0b9f31f2019-02-28 11:00:01 -0800270 esc := proptools.NinjaAndShellEscapeList
Colin Cross4b963f82016-09-29 14:06:02 -0700271
272 flags.CFlags = append(flags.CFlags, esc(compiler.Properties.Cflags)...)
273 flags.CppFlags = append(flags.CppFlags, esc(compiler.Properties.Cppflags)...)
274 flags.ConlyFlags = append(flags.ConlyFlags, esc(compiler.Properties.Conlyflags)...)
275 flags.AsFlags = append(flags.AsFlags, esc(compiler.Properties.Asflags)...)
Colin Cross91e90042016-12-02 17:13:24 -0800276 flags.YasmFlags = append(flags.YasmFlags, esc(compiler.Properties.Asflags)...)
Dan Willemsen4e0aa232019-04-10 22:59:54 -0700277
278 flags.Yacc = compiler.Properties.Yacc
Colin Cross4d9c2d12016-07-29 12:48:20 -0700279
280 // Include dir cflags
Colin Cross4d9c2d12016-07-29 12:48:20 -0700281 localIncludeDirs := android.PathsForModuleSrc(ctx, compiler.Properties.Local_include_dirs)
Dan Willemsen273af7f2016-11-03 15:53:42 -0700282 if len(localIncludeDirs) > 0 {
Colin Crossdad8c952017-04-26 14:55:27 -0700283 f := includeDirsToFlags(localIncludeDirs)
284 flags.GlobalFlags = append(flags.GlobalFlags, f)
285 flags.YasmFlags = append(flags.YasmFlags, f)
Dan Willemsen273af7f2016-11-03 15:53:42 -0700286 }
287 rootIncludeDirs := android.PathsForSource(ctx, compiler.Properties.Include_dirs)
288 if len(rootIncludeDirs) > 0 {
Colin Crossdad8c952017-04-26 14:55:27 -0700289 f := includeDirsToFlags(rootIncludeDirs)
290 flags.GlobalFlags = append(flags.GlobalFlags, f)
291 flags.YasmFlags = append(flags.YasmFlags, f)
Dan Willemsen273af7f2016-11-03 15:53:42 -0700292 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700293
Dan Albert899c23e2018-12-06 11:04:03 -0800294 if compiler.Properties.Include_build_directory == nil ||
295 *compiler.Properties.Include_build_directory {
296 flags.GlobalFlags = append(flags.GlobalFlags, "-I"+android.PathForModuleSrc(ctx).String())
297 flags.YasmFlags = append(flags.YasmFlags, "-I"+android.PathForModuleSrc(ctx).String())
298 }
Colin Crossc3199482017-03-30 15:03:04 -0700299
Colin Cross87dd9632017-11-03 13:31:05 -0700300 if !(ctx.useSdk() || ctx.useVndk()) || ctx.Host() {
301 flags.SystemIncludeFlags = append(flags.SystemIncludeFlags,
302 "${config.CommonGlobalIncludes}",
303 tc.IncludeFlags(),
304 "${config.CommonNativehelperInclude}")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700305 }
306
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700307 if ctx.useSdk() {
Dan Albertfac114b2018-11-14 21:22:00 -0800308 // TODO: Switch to --sysroot.
Colin Cross4d9c2d12016-07-29 12:48:20 -0700309 // The NDK headers are installed to a common sysroot. While a more
310 // typical Soong approach would be to only make the headers for the
311 // library you're using available, we're trying to emulate the NDK
312 // behavior here, and the NDK always has all the NDK headers available.
Colin Crossc3199482017-03-30 15:03:04 -0700313 flags.SystemIncludeFlags = append(flags.SystemIncludeFlags,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700314 "-isystem "+getCurrentIncludePath(ctx).String(),
Chih-Hung Hsieh1e7d1bf2018-03-15 18:44:57 -0700315 "-isystem "+getCurrentIncludePath(ctx).Join(ctx, config.NDKTriple(tc)).String())
Colin Cross4d9c2d12016-07-29 12:48:20 -0700316
Dan Albertfac114b2018-11-14 21:22:00 -0800317 // TODO: Migrate to API suffixed triple?
Colin Cross4d9c2d12016-07-29 12:48:20 -0700318 // Traditionally this has come from android/api-level.h, but with the
319 // libc headers unified it must be set by the build system since we
320 // don't have per-API level copies of that header now.
Dan Albertebedf672016-11-08 15:06:22 -0800321 version := ctx.sdkVersion()
322 if version == "current" {
323 version = "__ANDROID_API_FUTURE__"
324 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700325 flags.GlobalFlags = append(flags.GlobalFlags,
Dan Albertebedf672016-11-08 15:06:22 -0800326 "-D__ANDROID_API__="+version)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700327 }
328
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700329 if ctx.useVndk() {
Justin Yun732aa6a2018-03-23 17:43:47 +0900330 // sdkVersion() returns VNDK version for vendor modules.
331 version := ctx.sdkVersion()
332 if version == "current" {
333 version = "__ANDROID_API_FUTURE__"
334 }
Dan Willemsenb916b802017-03-19 13:44:32 -0700335 flags.GlobalFlags = append(flags.GlobalFlags,
Justin Yun732aa6a2018-03-23 17:43:47 +0900336 "-D__ANDROID_API__="+version, "-D__ANDROID_VNDK__")
Dan Willemsenb916b802017-03-19 13:44:32 -0700337 }
338
Jiyong Parkd54aee52018-06-09 01:32:54 +0900339 if ctx.inRecovery() {
340 flags.GlobalFlags = append(flags.GlobalFlags, "-D__ANDROID_RECOVERY__")
341 }
342
Jiyong Park58e364a2019-01-19 19:24:06 +0900343 if ctx.apexName() != "" {
344 flags.GlobalFlags = append(flags.GlobalFlags, "-D__ANDROID_APEX__="+ctx.apexName())
345 }
346
Nan Zhang0007d812017-11-07 10:57:05 -0800347 instructionSet := String(compiler.Properties.Instruction_set)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700348 if flags.RequiredInstructionSet != "" {
349 instructionSet = flags.RequiredInstructionSet
350 }
Dan Willemsen8536d6b2018-10-07 20:54:34 -0700351 instructionSetFlags, err := tc.ClangInstructionSetFlags(instructionSet)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700352 if err != nil {
353 ctx.ModuleErrorf("%s", err)
354 }
355
356 CheckBadCompilerFlags(ctx, "release.cflags", compiler.Properties.Release.Cflags)
357
358 // TODO: debug
Colin Cross4b963f82016-09-29 14:06:02 -0700359 flags.CFlags = append(flags.CFlags, esc(compiler.Properties.Release.Cflags)...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700360
Dan Willemsen8536d6b2018-10-07 20:54:34 -0700361 CheckBadCompilerFlags(ctx, "clang_cflags", compiler.Properties.Clang_cflags)
362 CheckBadCompilerFlags(ctx, "clang_asflags", compiler.Properties.Clang_asflags)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700363
Dan Willemsen8536d6b2018-10-07 20:54:34 -0700364 flags.CFlags = config.ClangFilterUnknownCflags(flags.CFlags)
365 flags.CFlags = append(flags.CFlags, esc(compiler.Properties.Clang_cflags)...)
366 flags.AsFlags = append(flags.AsFlags, esc(compiler.Properties.Clang_asflags)...)
367 flags.CppFlags = config.ClangFilterUnknownCflags(flags.CppFlags)
368 flags.ConlyFlags = config.ClangFilterUnknownCflags(flags.ConlyFlags)
369 flags.LdFlags = config.ClangFilterUnknownCflags(flags.LdFlags)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700370
Dan Willemsen8536d6b2018-10-07 20:54:34 -0700371 target := "-target " + tc.ClangTriple()
372 gccPrefix := "-B" + config.ToolPath(tc)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700373
Dan Willemsen8536d6b2018-10-07 20:54:34 -0700374 flags.CFlags = append(flags.CFlags, target, gccPrefix)
375 flags.AsFlags = append(flags.AsFlags, target, gccPrefix)
376 flags.LdFlags = append(flags.LdFlags, target, gccPrefix)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700377
Colin Crossb98c8b02016-07-29 13:44:28 -0700378 hod := "Host"
Colin Cross4d9c2d12016-07-29 12:48:20 -0700379 if ctx.Os().Class == android.Device {
Colin Crossb98c8b02016-07-29 13:44:28 -0700380 hod = "Device"
Colin Cross4d9c2d12016-07-29 12:48:20 -0700381 }
382
Colin Cross87dd9632017-11-03 13:31:05 -0700383 flags.GlobalFlags = append(flags.GlobalFlags, instructionSetFlags)
384 flags.ConlyFlags = append([]string{"${config.CommonGlobalConlyflags}"}, flags.ConlyFlags...)
Colin Cross26f14502017-11-06 13:59:48 -0800385 flags.CppFlags = append([]string{fmt.Sprintf("${config.%sGlobalCppflags}", hod)}, flags.CppFlags...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700386
Dan Willemsen8536d6b2018-10-07 20:54:34 -0700387 flags.AsFlags = append(flags.AsFlags, tc.ClangAsflags())
388 flags.CppFlags = append([]string{"${config.CommonClangGlobalCppflags}"}, flags.CppFlags...)
389 flags.GlobalFlags = append(flags.GlobalFlags,
390 tc.ClangCflags(),
391 "${config.CommonClangGlobalCflags}",
392 fmt.Sprintf("${config.%sClangGlobalCflags}", hod))
Colin Cross4d9c2d12016-07-29 12:48:20 -0700393
Dan Willemsen8536d6b2018-10-07 20:54:34 -0700394 if strings.HasPrefix(android.PathForModuleSrc(ctx).String(), "external/") {
395 flags.GlobalFlags = append([]string{"${config.ClangExternalCflags}"}, flags.GlobalFlags...)
Yi Kongcc80f8d2018-06-06 14:42:44 -0700396 }
397
Dan Willemsen10db3842018-10-21 18:42:46 -0700398 if tc.Bionic() {
Colin Cross87dd9632017-11-03 13:31:05 -0700399 if Bool(compiler.Properties.Rtti) {
400 flags.CppFlags = append(flags.CppFlags, "-frtti")
401 } else {
402 flags.CppFlags = append(flags.CppFlags, "-fno-rtti")
403 }
404 }
405
406 flags.AsFlags = append(flags.AsFlags, "-D__ASSEMBLY__")
407
Dan Willemsen8536d6b2018-10-07 20:54:34 -0700408 flags.CppFlags = append(flags.CppFlags, tc.ClangCppflags())
Colin Cross87dd9632017-11-03 13:31:05 -0700409
410 flags.YasmFlags = append(flags.YasmFlags, tc.YasmFlags())
411
Dan Willemsen8536d6b2018-10-07 20:54:34 -0700412 flags.GlobalFlags = append(flags.GlobalFlags, tc.ToolchainClangCflags())
Colin Cross4d9c2d12016-07-29 12:48:20 -0700413
Elliott Hughes5789ca92018-02-16 17:15:19 -0800414 cStd := config.CStdVersion
415 if String(compiler.Properties.C_std) == "experimental" {
416 cStd = config.ExperimentalCStdVersion
417 } else if String(compiler.Properties.C_std) != "" {
418 cStd = String(compiler.Properties.C_std)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700419 }
420
Elliott Hughes5789ca92018-02-16 17:15:19 -0800421 cppStd := String(compiler.Properties.Cpp_std)
422 switch String(compiler.Properties.Cpp_std) {
423 case "":
424 cppStd = config.CppStdVersion
425 case "experimental":
426 cppStd = config.ExperimentalCppStdVersion
Elliott Hughes5789ca92018-02-16 17:15:19 -0800427 }
428
Elliott Hughes5789ca92018-02-16 17:15:19 -0800429 if compiler.Properties.Gnu_extensions != nil && *compiler.Properties.Gnu_extensions == false {
430 cStd = gnuToCReplacer.Replace(cStd)
431 cppStd = gnuToCReplacer.Replace(cppStd)
432 }
433
434 flags.ConlyFlags = append([]string{"-std=" + cStd}, flags.ConlyFlags...)
435 flags.CppFlags = append([]string{"-std=" + cppStd}, flags.CppFlags...)
436
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700437 if ctx.useVndk() {
Jiyong Parka622de82017-08-10 13:33:27 +0900438 flags.CFlags = append(flags.CFlags, esc(compiler.Properties.Target.Vendor.Cflags)...)
439 }
440
Jiyong Park4a2dcb52018-05-24 16:44:14 +0900441 if ctx.inRecovery() {
442 flags.CFlags = append(flags.CFlags, esc(compiler.Properties.Target.Recovery.Cflags)...)
443 }
444
Colin Cross4d9c2d12016-07-29 12:48:20 -0700445 // We can enforce some rules more strictly in the code we own. strict
446 // indicates if this is code that we can be stricter with. If we have
447 // rules that we want to apply to *our* code (but maybe can't for
448 // vendor/device specific things), we could extend this to be a ternary
449 // value.
450 strict := true
451 if strings.HasPrefix(android.PathForModuleSrc(ctx).String(), "external/") {
452 strict = false
453 }
454
455 // Can be used to make some annotations stricter for code we can fix
456 // (such as when we mark functions as deprecated).
457 if strict {
458 flags.CFlags = append(flags.CFlags, "-DANDROID_STRICT")
459 }
460
Dan Willemsene1a3ce32016-11-02 20:44:08 -0700461 if compiler.hasSrcExt(".proto") {
Colin Cross0c461f12016-10-20 16:11:43 -0700462 flags = protoFlags(ctx, flags, &compiler.Proto)
463 }
464
Dan Willemsene1a3ce32016-11-02 20:44:08 -0700465 if compiler.hasSrcExt(".y") || compiler.hasSrcExt(".yy") {
466 flags.GlobalFlags = append(flags.GlobalFlags,
467 "-I"+android.PathForModuleGen(ctx, "yacc", ctx.ModuleDir()).String())
468 }
469
Dan Willemsen4f1c3d42017-09-09 01:15:26 -0700470 if compiler.hasSrcExt(".mc") {
471 flags.GlobalFlags = append(flags.GlobalFlags,
472 "-I"+android.PathForModuleGen(ctx, "windmc", ctx.ModuleDir()).String())
473 }
474
Dan Willemsene1240db2016-11-03 14:28:51 -0700475 if compiler.hasSrcExt(".aidl") {
476 if len(compiler.Properties.Aidl.Local_include_dirs) > 0 {
477 localAidlIncludeDirs := android.PathsForModuleSrc(ctx, compiler.Properties.Aidl.Local_include_dirs)
478 flags.aidlFlags = append(flags.aidlFlags, includeDirsToFlags(localAidlIncludeDirs))
479 }
480 if len(compiler.Properties.Aidl.Include_dirs) > 0 {
481 rootAidlIncludeDirs := android.PathsForSource(ctx, compiler.Properties.Aidl.Include_dirs)
482 flags.aidlFlags = append(flags.aidlFlags, includeDirsToFlags(rootAidlIncludeDirs))
483 }
484
Martijn Coeneneab15642018-03-09 09:29:59 +0100485 if Bool(compiler.Properties.Aidl.Generate_traces) {
486 flags.aidlFlags = append(flags.aidlFlags, "-t")
487 }
488
Dan Willemsene1240db2016-11-03 14:28:51 -0700489 flags.GlobalFlags = append(flags.GlobalFlags,
490 "-I"+android.PathForModuleGen(ctx, "aidl").String())
491 }
492
Colin Cross2a252be2017-05-01 17:37:24 -0700493 if compiler.hasSrcExt(".rs") || compiler.hasSrcExt(".fs") {
494 flags = rsFlags(ctx, flags, &compiler.Properties)
495 }
496
Inseob Kim21f26902018-09-06 00:55:20 +0900497 if compiler.hasSrcExt(".sysprop") {
498 flags.GlobalFlags = append(flags.GlobalFlags,
499 "-I"+android.PathForModuleGen(ctx, "sysprop", "include").String())
500 }
501
Chih-Hung Hsieh64a38dc2017-11-14 14:09:14 -0800502 if len(compiler.Properties.Srcs) > 0 {
503 module := ctx.ModuleDir() + "/Android.bp:" + ctx.ModuleName()
504 if inList("-Wno-error", flags.CFlags) || inList("-Wno-error", flags.CppFlags) {
Colin Cross571cccf2019-02-04 11:22:08 -0800505 addToModuleList(ctx, modulesUsingWnoErrorKey, module)
Chih-Hung Hsieh64a38dc2017-11-14 14:09:14 -0800506 } else if !inList("-Werror", flags.CFlags) && !inList("-Werror", flags.CppFlags) {
507 if warningsAreAllowed(ctx.ModuleDir()) {
Colin Cross571cccf2019-02-04 11:22:08 -0800508 addToModuleList(ctx, modulesAddedWallKey, module)
Chih-Hung Hsieh64a38dc2017-11-14 14:09:14 -0800509 flags.CFlags = append([]string{"-Wall"}, flags.CFlags...)
510 } else {
Chih-Hung Hsieh64a38dc2017-11-14 14:09:14 -0800511 flags.CFlags = append([]string{"-Wall", "-Werror"}, flags.CFlags...)
512 }
513 }
514 }
515
Pirama Arumuga Nainarfadb7b52017-12-19 23:08:09 -0800516 if Bool(compiler.Properties.Openmp) {
517 flags.CFlags = append(flags.CFlags, "-fopenmp")
518 }
519
Colin Cross4d9c2d12016-07-29 12:48:20 -0700520 return flags
521}
522
Dan Willemsene1a3ce32016-11-02 20:44:08 -0700523func (compiler *baseCompiler) hasSrcExt(ext string) bool {
Colin Crossf18e1102017-11-16 14:33:08 -0800524 for _, src := range compiler.srcsBeforeGen {
525 if src.Ext() == ext {
526 return true
527 }
528 }
Colin Cross0c461f12016-10-20 16:11:43 -0700529 for _, src := range compiler.Properties.Srcs {
Dan Willemsene1a3ce32016-11-02 20:44:08 -0700530 if filepath.Ext(src) == ext {
Colin Cross0c461f12016-10-20 16:11:43 -0700531 return true
532 }
533 }
Colin Cross10d22312017-05-03 11:01:58 -0700534 for _, src := range compiler.Properties.OriginalSrcs {
535 if filepath.Ext(src) == ext {
536 return true
537 }
538 }
Colin Cross0c461f12016-10-20 16:11:43 -0700539
540 return false
541}
542
Colin Cross948f0cb2016-10-17 14:24:56 -0700543var gnuToCReplacer = strings.NewReplacer("gnu", "c")
544
Colin Cross4d9c2d12016-07-29 12:48:20 -0700545func ndkPathDeps(ctx ModuleContext) android.Paths {
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700546 if ctx.useSdk() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700547 // The NDK sysroot timestamp file depends on all the NDK sysroot files
548 // (headers and libraries).
Dan Albert6ab43d82017-12-13 15:05:04 -0800549 return android.Paths{getNdkBaseTimestampFile(ctx)}
Colin Cross4d9c2d12016-07-29 12:48:20 -0700550 }
551 return nil
552}
553
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700554func (compiler *baseCompiler) compile(ctx ModuleContext, flags Flags, deps PathDeps) Objects {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700555 pathDeps := deps.GeneratedHeaders
556 pathDeps = append(pathDeps, ndkPathDeps(ctx)...)
Colin Cross2f336352016-10-26 10:03:47 -0700557
Colin Cross2f336352016-10-26 10:03:47 -0700558 buildFlags := flagsToBuilderFlags(flags)
559
Colin Crossf18e1102017-11-16 14:33:08 -0800560 srcs := append(android.Paths(nil), compiler.srcsBeforeGen...)
561
Colin Cross2f336352016-10-26 10:03:47 -0700562 srcs, genDeps := genSources(ctx, srcs, buildFlags)
Pirama Arumuga Nainarf231b192018-01-23 10:49:04 -0800563 pathDeps = append(pathDeps, genDeps...)
Colin Cross2f336352016-10-26 10:03:47 -0700564
Pirama Arumuga Nainar70ba5a32017-12-19 15:11:01 -0800565 compiler.pathDeps = pathDeps
Pirama Arumuga Nainarf231b192018-01-23 10:49:04 -0800566 compiler.cFlagsDeps = flags.CFlagsDeps
Colin Cross2f336352016-10-26 10:03:47 -0700567
Fabien Sanglardd61f1f42017-01-10 16:21:22 -0800568 // Save src, buildFlags and context
569 compiler.srcs = srcs
570
Colin Cross4d9c2d12016-07-29 12:48:20 -0700571 // Compile files listed in c.Properties.Srcs into objects
Pirama Arumuga Nainarf231b192018-01-23 10:49:04 -0800572 objs := compileObjs(ctx, buildFlags, "", srcs, pathDeps, compiler.cFlagsDeps)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700573
574 if ctx.Failed() {
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700575 return Objects{}
Colin Cross4d9c2d12016-07-29 12:48:20 -0700576 }
577
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700578 return objs
Colin Cross4d9c2d12016-07-29 12:48:20 -0700579}
580
581// Compile a list of source files into objects a specified subdirectory
Colin Cross2f336352016-10-26 10:03:47 -0700582func compileObjs(ctx android.ModuleContext, flags builderFlags,
Pirama Arumuga Nainarf231b192018-01-23 10:49:04 -0800583 subdir string, srcFiles, pathDeps android.Paths, cFlagsDeps android.Paths) Objects {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700584
Pirama Arumuga Nainarf231b192018-01-23 10:49:04 -0800585 return TransformSourceToObj(ctx, subdir, srcFiles, flags, pathDeps, cFlagsDeps)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700586}