blob: bb40a5bd2df0d170b1ccc8f80940531de15684f6 [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"
Peter Collingbourneb0e61432019-07-22 16:36:06 -070020 "strconv"
Colin Cross4d9c2d12016-07-29 12:48:20 -070021 "strings"
22
Colin Cross4b963f82016-09-29 14:06:02 -070023 "github.com/google/blueprint/proptools"
24
Colin Cross4d9c2d12016-07-29 12:48:20 -070025 "android/soong/android"
Colin Crossb98c8b02016-07-29 13:44:28 -070026 "android/soong/cc/config"
Colin Cross4d9c2d12016-07-29 12:48:20 -070027)
28
29// This file contains the basic C/C++/assembly to .o compliation steps
30
31type BaseCompilerProperties struct {
32 // 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 -080033 // srcs may reference the outputs of other modules that produce source files like genrule
34 // or filegroup using the syntax ":module".
Colin Cross27b922f2019-03-04 22:35:41 -080035 Srcs []string `android:"path,arch_variant"`
Colin Cross4d9c2d12016-07-29 12:48:20 -070036
37 // list of source files that should not be used to build the C/C++ module.
38 // This is most useful in the arch/multilib variants to remove non-common files
Colin Cross27b922f2019-03-04 22:35:41 -080039 Exclude_srcs []string `android:"path,arch_variant"`
Colin Cross4d9c2d12016-07-29 12:48:20 -070040
41 // list of module-specific flags that will be used for C and C++ compiles.
42 Cflags []string `android:"arch_variant"`
43
44 // list of module-specific flags that will be used for C++ compiles
45 Cppflags []string `android:"arch_variant"`
46
47 // list of module-specific flags that will be used for C compiles
48 Conlyflags []string `android:"arch_variant"`
49
50 // list of module-specific flags that will be used for .S compiles
51 Asflags []string `android:"arch_variant"`
52
53 // list of module-specific flags that will be used for C and C++ compiles when
54 // compiling with clang
55 Clang_cflags []string `android:"arch_variant"`
56
57 // list of module-specific flags that will be used for .S compiles when
58 // compiling with clang
59 Clang_asflags []string `android:"arch_variant"`
60
Colin Cross4d9c2d12016-07-29 12:48:20 -070061 // the instruction set architecture to use to compile the C/C++
62 // module.
Dan Willemsen5922f3c2017-10-25 15:20:50 -070063 Instruction_set *string `android:"arch_variant"`
Colin Cross4d9c2d12016-07-29 12:48:20 -070064
65 // list of directories relative to the root of the source tree that will
66 // be added to the include path using -I.
67 // If possible, don't use this. If adding paths from the current directory use
68 // local_include_dirs, if adding paths from other modules use export_include_dirs in
69 // that module.
Colin Crossccf01e72017-04-26 17:01:07 -070070 Include_dirs []string `android:"arch_variant,variant_prepend"`
Colin Cross4d9c2d12016-07-29 12:48:20 -070071
72 // list of directories relative to the Blueprints file that will
73 // be added to the include path using -I
Dan Willemsen59339a22018-07-22 21:18:45 -070074 Local_include_dirs []string `android:"arch_variant,variant_prepend"`
Colin Cross4d9c2d12016-07-29 12:48:20 -070075
Dan Albert899c23e2018-12-06 11:04:03 -080076 // Add the directory containing the Android.bp file to the list of include
77 // directories. Defaults to true.
78 Include_build_directory *bool
79
Colin Cross4d9c2d12016-07-29 12:48:20 -070080 // list of generated sources to compile. These are the names of gensrcs or
81 // genrule modules.
82 Generated_sources []string `android:"arch_variant"`
83
84 // list of generated headers to add to the include path. These are the names
85 // of genrule modules.
86 Generated_headers []string `android:"arch_variant"`
87
88 // pass -frtti instead of -fno-rtti
89 Rtti *bool
90
Dan Albert043833c2017-02-03 16:13:38 -080091 // C standard version to use. Can be a specific version (such as "gnu11"),
92 // "experimental" (which will use draft versions like C1x when available),
93 // or the empty string (which will use the default).
Nan Zhang0007d812017-11-07 10:57:05 -080094 C_std *string
Dan Albert043833c2017-02-03 16:13:38 -080095
96 // C++ standard version to use. Can be a specific version (such as
97 // "gnu++11"), "experimental" (which will use draft versions like C++1z when
98 // available), or the empty string (which will use the default).
Nan Zhang0007d812017-11-07 10:57:05 -080099 Cpp_std *string
Dan Albert043833c2017-02-03 16:13:38 -0800100
Colin Cross948f0cb2016-10-17 14:24:56 -0700101 // if set to false, use -std=c++* instead of -std=gnu++*
102 Gnu_extensions *bool
103
Dan Willemsen4e0aa232019-04-10 22:59:54 -0700104 Yacc *YaccProperties
105
Dan Willemsene1240db2016-11-03 14:28:51 -0700106 Aidl struct {
107 // list of directories that will be added to the aidl include paths.
108 Include_dirs []string
109
110 // list of directories relative to the Blueprints file that will
111 // be added to the aidl include paths.
112 Local_include_dirs []string
Martijn Coeneneab15642018-03-09 09:29:59 +0100113
114 // whether to generate traces (for systrace) for this interface
115 Generate_traces *bool
Dan Willemsene1240db2016-11-03 14:28:51 -0700116 }
117
Colin Cross2a252be2017-05-01 17:37:24 -0700118 Renderscript struct {
119 // list of directories that will be added to the llvm-rs-cc include paths
120 Include_dirs []string
121
122 // list of flags that will be passed to llvm-rs-cc
123 Flags []string
124
125 // Renderscript API level to target
126 Target_api *string
127 }
128
Colin Cross4d9c2d12016-07-29 12:48:20 -0700129 Debug, Release struct {
130 // list of module-specific flags that will be used for C and C++ compiles in debug or
131 // release builds
132 Cflags []string `android:"arch_variant"`
133 } `android:"arch_variant"`
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700134
135 Target struct {
136 Vendor struct {
137 // list of source files that should only be used in the
138 // vendor variant of the C/C++ module.
Colin Cross27b922f2019-03-04 22:35:41 -0800139 Srcs []string `android:"path"`
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700140
141 // list of source files that should not be used to
142 // build the vendor variant of the C/C++ module.
Colin Cross27b922f2019-03-04 22:35:41 -0800143 Exclude_srcs []string `android:"path"`
Jiyong Parka622de82017-08-10 13:33:27 +0900144
145 // List of additional cflags that should be used to build the vendor
146 // variant of the C/C++ module.
147 Cflags []string
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700148 }
Jiyong Parkf9332f12018-02-01 00:54:12 +0900149 Recovery struct {
150 // list of source files that should only be used in the
151 // recovery variant of the C/C++ module.
Colin Cross27b922f2019-03-04 22:35:41 -0800152 Srcs []string `android:"path"`
Jiyong Parkf9332f12018-02-01 00:54:12 +0900153
154 // list of source files that should not be used to
155 // build the recovery variant of the C/C++ module.
Colin Cross27b922f2019-03-04 22:35:41 -0800156 Exclude_srcs []string `android:"path"`
Jiyong Parkf9332f12018-02-01 00:54:12 +0900157
158 // List of additional cflags that should be used to build the recovery
159 // variant of the C/C++ module.
160 Cflags []string
161 }
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700162 }
Colin Cross10d22312017-05-03 11:01:58 -0700163
Colin Cross38f794e2017-09-07 10:53:07 -0700164 Proto struct {
165 // Link statically against the protobuf runtime
Nan Zhang0007d812017-11-07 10:57:05 -0800166 Static *bool `android:"arch_variant"`
Colin Cross38f794e2017-09-07 10:53:07 -0700167 } `android:"arch_variant"`
168
Colin Cross10d22312017-05-03 11:01:58 -0700169 // Stores the original list of source files before being cleared by library reuse
170 OriginalSrcs []string `blueprint:"mutated"`
Pirama Arumuga Nainarfadb7b52017-12-19 23:08:09 -0800171
172 // Build and link with OpenMP
173 Openmp *bool `android:"arch_variant"`
Colin Cross4d9c2d12016-07-29 12:48:20 -0700174}
175
Colin Crossb916a382016-07-29 17:28:03 -0700176func NewBaseCompiler() *baseCompiler {
177 return &baseCompiler{}
178}
179
Colin Cross4d9c2d12016-07-29 12:48:20 -0700180type baseCompiler struct {
181 Properties BaseCompilerProperties
Colin Cross38f794e2017-09-07 10:53:07 -0700182 Proto android.ProtoProperties
Pirama Arumuga Nainarf231b192018-01-23 10:49:04 -0800183 cFlagsDeps android.Paths
Pirama Arumuga Nainar70ba5a32017-12-19 15:11:01 -0800184 pathDeps android.Paths
Fabien Sanglardd61f1f42017-01-10 16:21:22 -0800185 flags builderFlags
Colin Crossf18e1102017-11-16 14:33:08 -0800186
187 // Sources that were passed to the C/C++ compiler
188 srcs android.Paths
189
190 // Sources that were passed in the Android.bp file, including generated sources generated by
191 // other modules and filegroups. May include source files that have not yet been translated to
192 // C/C++ (.aidl, .proto, etc.)
193 srcsBeforeGen android.Paths
Colin Cross4d9c2d12016-07-29 12:48:20 -0700194}
195
196var _ compiler = (*baseCompiler)(nil)
197
Fabien Sanglardd61f1f42017-01-10 16:21:22 -0800198type CompiledInterface interface {
199 Srcs() android.Paths
200}
201
202func (compiler *baseCompiler) Srcs() android.Paths {
Nan Zhange42777a2018-03-27 16:19:42 -0700203 return append(android.Paths{}, compiler.srcs...)
Fabien Sanglardd61f1f42017-01-10 16:21:22 -0800204}
205
Colin Cross4d9c2d12016-07-29 12:48:20 -0700206func (compiler *baseCompiler) appendCflags(flags []string) {
207 compiler.Properties.Cflags = append(compiler.Properties.Cflags, flags...)
208}
209
210func (compiler *baseCompiler) appendAsflags(flags []string) {
211 compiler.Properties.Asflags = append(compiler.Properties.Asflags, flags...)
212}
213
Colin Cross42742b82016-08-01 13:20:05 -0700214func (compiler *baseCompiler) compilerProps() []interface{} {
Colin Cross0feb1692016-11-03 14:38:52 -0700215 return []interface{}{&compiler.Properties, &compiler.Proto}
Colin Cross4d9c2d12016-07-29 12:48:20 -0700216}
217
Colin Cross42742b82016-08-01 13:20:05 -0700218func (compiler *baseCompiler) compilerInit(ctx BaseModuleContext) {}
Colin Cross4d9c2d12016-07-29 12:48:20 -0700219
Colin Cross37047f12016-12-13 17:06:13 -0800220func (compiler *baseCompiler) compilerDeps(ctx DepsContext, deps Deps) Deps {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700221 deps.GeneratedSources = append(deps.GeneratedSources, compiler.Properties.Generated_sources...)
222 deps.GeneratedHeaders = append(deps.GeneratedHeaders, compiler.Properties.Generated_headers...)
223
Colin Crossfe17f6f2019-03-28 19:30:56 -0700224 android.ProtoDeps(ctx, &compiler.Proto)
Dan Willemsene1a3ce32016-11-02 20:44:08 -0700225 if compiler.hasSrcExt(".proto") {
Nan Zhang0007d812017-11-07 10:57:05 -0800226 deps = protoDeps(ctx, deps, &compiler.Proto, Bool(compiler.Properties.Proto.Static))
Colin Cross0c461f12016-10-20 16:11:43 -0700227 }
228
Pirama Arumuga Nainarfadb7b52017-12-19 23:08:09 -0800229 if Bool(compiler.Properties.Openmp) {
230 deps.StaticLibs = append(deps.StaticLibs, "libomp")
231 }
232
Colin Cross4d9c2d12016-07-29 12:48:20 -0700233 return deps
234}
235
Chih-Hung Hsieh64a38dc2017-11-14 14:09:14 -0800236// Return true if the module is in the WarningAllowedProjects.
237func warningsAreAllowed(subdir string) bool {
238 subdir += "/"
239 for _, prefix := range config.WarningAllowedProjects {
240 if strings.HasPrefix(subdir, prefix) {
241 return true
242 }
243 }
244 return false
245}
246
Colin Cross571cccf2019-02-04 11:22:08 -0800247func addToModuleList(ctx ModuleContext, key android.OnceKey, module string) {
248 getNamedMapForConfig(ctx.Config(), key).Store(module, true)
Chih-Hung Hsieh64a38dc2017-11-14 14:09:14 -0800249}
250
Colin Cross4d9c2d12016-07-29 12:48:20 -0700251// Create a Flags struct that collects the compile flags from global values,
252// per-target values, module type values, and per-module Blueprints properties
Colin Crossf18e1102017-11-16 14:33:08 -0800253func (compiler *baseCompiler) compilerFlags(ctx ModuleContext, flags Flags, deps PathDeps) Flags {
Colin Crossb98c8b02016-07-29 13:44:28 -0700254 tc := ctx.toolchain()
Colin Cross4d9c2d12016-07-29 12:48:20 -0700255
Colin Cross8a497952019-03-05 22:25:09 -0800256 compiler.srcsBeforeGen = android.PathsForModuleSrcExcludes(ctx, compiler.Properties.Srcs, compiler.Properties.Exclude_srcs)
Colin Crossf18e1102017-11-16 14:33:08 -0800257 compiler.srcsBeforeGen = append(compiler.srcsBeforeGen, deps.GeneratedSources...)
258
Colin Cross4d9c2d12016-07-29 12:48:20 -0700259 CheckBadCompilerFlags(ctx, "cflags", compiler.Properties.Cflags)
260 CheckBadCompilerFlags(ctx, "cppflags", compiler.Properties.Cppflags)
261 CheckBadCompilerFlags(ctx, "conlyflags", compiler.Properties.Conlyflags)
262 CheckBadCompilerFlags(ctx, "asflags", compiler.Properties.Asflags)
Jiyong Park4a2dcb52018-05-24 16:44:14 +0900263 CheckBadCompilerFlags(ctx, "vendor.cflags", compiler.Properties.Target.Vendor.Cflags)
264 CheckBadCompilerFlags(ctx, "recovery.cflags", compiler.Properties.Target.Recovery.Cflags)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700265
Colin Cross0b9f31f2019-02-28 11:00:01 -0800266 esc := proptools.NinjaAndShellEscapeList
Colin Cross4b963f82016-09-29 14:06:02 -0700267
Colin Cross4af21ed2019-11-04 09:37:55 -0800268 flags.Local.CFlags = append(flags.Local.CFlags, esc(compiler.Properties.Cflags)...)
269 flags.Local.CppFlags = append(flags.Local.CppFlags, esc(compiler.Properties.Cppflags)...)
270 flags.Local.ConlyFlags = append(flags.Local.ConlyFlags, esc(compiler.Properties.Conlyflags)...)
271 flags.Local.AsFlags = append(flags.Local.AsFlags, esc(compiler.Properties.Asflags)...)
272 flags.Local.YasmFlags = append(flags.Local.YasmFlags, esc(compiler.Properties.Asflags)...)
Dan Willemsen4e0aa232019-04-10 22:59:54 -0700273
274 flags.Yacc = compiler.Properties.Yacc
Colin Cross4d9c2d12016-07-29 12:48:20 -0700275
276 // Include dir cflags
Colin Cross4d9c2d12016-07-29 12:48:20 -0700277 localIncludeDirs := android.PathsForModuleSrc(ctx, compiler.Properties.Local_include_dirs)
Dan Willemsen273af7f2016-11-03 15:53:42 -0700278 if len(localIncludeDirs) > 0 {
Colin Crossdad8c952017-04-26 14:55:27 -0700279 f := includeDirsToFlags(localIncludeDirs)
Colin Cross4af21ed2019-11-04 09:37:55 -0800280 flags.Local.CommonFlags = append(flags.Local.CommonFlags, f)
281 flags.Local.YasmFlags = append(flags.Local.YasmFlags, f)
Dan Willemsen273af7f2016-11-03 15:53:42 -0700282 }
283 rootIncludeDirs := android.PathsForSource(ctx, compiler.Properties.Include_dirs)
284 if len(rootIncludeDirs) > 0 {
Colin Crossdad8c952017-04-26 14:55:27 -0700285 f := includeDirsToFlags(rootIncludeDirs)
Colin Cross4af21ed2019-11-04 09:37:55 -0800286 flags.Local.CommonFlags = append(flags.Local.CommonFlags, f)
287 flags.Local.YasmFlags = append(flags.Local.YasmFlags, f)
Dan Willemsen273af7f2016-11-03 15:53:42 -0700288 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700289
Dan Albert899c23e2018-12-06 11:04:03 -0800290 if compiler.Properties.Include_build_directory == nil ||
291 *compiler.Properties.Include_build_directory {
Colin Cross4af21ed2019-11-04 09:37:55 -0800292 flags.Local.CommonFlags = append(flags.Local.CommonFlags, "-I"+android.PathForModuleSrc(ctx).String())
293 flags.Local.YasmFlags = append(flags.Local.YasmFlags, "-I"+android.PathForModuleSrc(ctx).String())
Dan Albert899c23e2018-12-06 11:04:03 -0800294 }
Colin Crossc3199482017-03-30 15:03:04 -0700295
Colin Cross87dd9632017-11-03 13:31:05 -0700296 if !(ctx.useSdk() || ctx.useVndk()) || ctx.Host() {
297 flags.SystemIncludeFlags = append(flags.SystemIncludeFlags,
298 "${config.CommonGlobalIncludes}",
299 tc.IncludeFlags(),
300 "${config.CommonNativehelperInclude}")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700301 }
302
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700303 if ctx.useSdk() {
Dan Albertfac114b2018-11-14 21:22:00 -0800304 // TODO: Switch to --sysroot.
Colin Cross4d9c2d12016-07-29 12:48:20 -0700305 // The NDK headers are installed to a common sysroot. While a more
306 // typical Soong approach would be to only make the headers for the
307 // library you're using available, we're trying to emulate the NDK
308 // behavior here, and the NDK always has all the NDK headers available.
Colin Crossc3199482017-03-30 15:03:04 -0700309 flags.SystemIncludeFlags = append(flags.SystemIncludeFlags,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700310 "-isystem "+getCurrentIncludePath(ctx).String(),
Chih-Hung Hsieh1e7d1bf2018-03-15 18:44:57 -0700311 "-isystem "+getCurrentIncludePath(ctx).Join(ctx, config.NDKTriple(tc)).String())
Colin Cross4d9c2d12016-07-29 12:48:20 -0700312 }
313
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700314 if ctx.useVndk() {
Colin Cross4af21ed2019-11-04 09:37:55 -0800315 flags.Global.CommonFlags = append(flags.Global.CommonFlags, "-D__ANDROID_VNDK__")
Dan Willemsenb916b802017-03-19 13:44:32 -0700316 }
317
Jiyong Parkd54aee52018-06-09 01:32:54 +0900318 if ctx.inRecovery() {
Colin Cross4af21ed2019-11-04 09:37:55 -0800319 flags.Global.CommonFlags = append(flags.Global.CommonFlags, "-D__ANDROID_RECOVERY__")
Jiyong Parkd54aee52018-06-09 01:32:54 +0900320 }
321
Jiyong Park58e364a2019-01-19 19:24:06 +0900322 if ctx.apexName() != "" {
Colin Cross4af21ed2019-11-04 09:37:55 -0800323 flags.Global.CommonFlags = append(flags.Global.CommonFlags,
324 "-D__ANDROID_APEX__",
325 "-D__ANDROID_APEX_"+makeDefineString(ctx.apexName())+"__")
Jiyong Park58e364a2019-01-19 19:24:06 +0900326 }
327
Nan Zhang0007d812017-11-07 10:57:05 -0800328 instructionSet := String(compiler.Properties.Instruction_set)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700329 if flags.RequiredInstructionSet != "" {
330 instructionSet = flags.RequiredInstructionSet
331 }
Dan Willemsen8536d6b2018-10-07 20:54:34 -0700332 instructionSetFlags, err := tc.ClangInstructionSetFlags(instructionSet)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700333 if err != nil {
334 ctx.ModuleErrorf("%s", err)
335 }
336
337 CheckBadCompilerFlags(ctx, "release.cflags", compiler.Properties.Release.Cflags)
338
339 // TODO: debug
Colin Cross4af21ed2019-11-04 09:37:55 -0800340 flags.Local.CFlags = append(flags.Local.CFlags, esc(compiler.Properties.Release.Cflags)...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700341
Dan Willemsen8536d6b2018-10-07 20:54:34 -0700342 CheckBadCompilerFlags(ctx, "clang_cflags", compiler.Properties.Clang_cflags)
343 CheckBadCompilerFlags(ctx, "clang_asflags", compiler.Properties.Clang_asflags)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700344
Colin Cross4af21ed2019-11-04 09:37:55 -0800345 flags.Local.CFlags = config.ClangFilterUnknownCflags(flags.Local.CFlags)
346 flags.Local.CFlags = append(flags.Local.CFlags, esc(compiler.Properties.Clang_cflags)...)
347 flags.Local.AsFlags = append(flags.Local.AsFlags, esc(compiler.Properties.Clang_asflags)...)
348 flags.Local.CppFlags = config.ClangFilterUnknownCflags(flags.Local.CppFlags)
349 flags.Local.ConlyFlags = config.ClangFilterUnknownCflags(flags.Local.ConlyFlags)
350 flags.Local.LdFlags = config.ClangFilterUnknownCflags(flags.Local.LdFlags)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700351
Dan Willemsen8536d6b2018-10-07 20:54:34 -0700352 target := "-target " + tc.ClangTriple()
Peter Collingbourneb0e61432019-07-22 16:36:06 -0700353 if ctx.Os().Class == android.Device {
354 version := ctx.sdkVersion()
355 if version == "" || version == "current" {
356 target += strconv.Itoa(android.FutureApiLevel)
357 } else {
358 target += version
359 }
360 }
361
Dan Willemsen8536d6b2018-10-07 20:54:34 -0700362 gccPrefix := "-B" + config.ToolPath(tc)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700363
Colin Cross4af21ed2019-11-04 09:37:55 -0800364 flags.Global.CFlags = append(flags.Global.CFlags, target, gccPrefix)
365 flags.Global.AsFlags = append(flags.Global.AsFlags, target, gccPrefix)
366 flags.Global.LdFlags = append(flags.Global.LdFlags, target, gccPrefix)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700367
Colin Crossb98c8b02016-07-29 13:44:28 -0700368 hod := "Host"
Colin Cross4d9c2d12016-07-29 12:48:20 -0700369 if ctx.Os().Class == android.Device {
Colin Crossb98c8b02016-07-29 13:44:28 -0700370 hod = "Device"
Colin Cross4d9c2d12016-07-29 12:48:20 -0700371 }
372
Colin Cross4af21ed2019-11-04 09:37:55 -0800373 flags.Global.CommonFlags = append(flags.Global.CommonFlags, instructionSetFlags)
374 flags.Global.ConlyFlags = append([]string{"${config.CommonGlobalConlyflags}"}, flags.Global.ConlyFlags...)
375 flags.Global.CppFlags = append([]string{fmt.Sprintf("${config.%sGlobalCppflags}", hod)}, flags.Global.CppFlags...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700376
Colin Cross4af21ed2019-11-04 09:37:55 -0800377 flags.Global.AsFlags = append(flags.Global.AsFlags, tc.ClangAsflags())
378 flags.Global.CppFlags = append([]string{"${config.CommonClangGlobalCppflags}"}, flags.Global.CppFlags...)
379 flags.Global.CommonFlags = append(flags.Global.CommonFlags,
Dan Willemsen8536d6b2018-10-07 20:54:34 -0700380 tc.ClangCflags(),
381 "${config.CommonClangGlobalCflags}",
382 fmt.Sprintf("${config.%sClangGlobalCflags}", hod))
Colin Cross4d9c2d12016-07-29 12:48:20 -0700383
Dan Willemsen8536d6b2018-10-07 20:54:34 -0700384 if strings.HasPrefix(android.PathForModuleSrc(ctx).String(), "external/") {
Colin Cross4af21ed2019-11-04 09:37:55 -0800385 flags.Global.CommonFlags = append([]string{"${config.ClangExternalCflags}"}, flags.Global.CommonFlags...)
Yi Kongcc80f8d2018-06-06 14:42:44 -0700386 }
387
Dan Willemsen10db3842018-10-21 18:42:46 -0700388 if tc.Bionic() {
Colin Cross87dd9632017-11-03 13:31:05 -0700389 if Bool(compiler.Properties.Rtti) {
Colin Cross4af21ed2019-11-04 09:37:55 -0800390 flags.Local.CppFlags = append(flags.Local.CppFlags, "-frtti")
Colin Cross87dd9632017-11-03 13:31:05 -0700391 } else {
Colin Cross4af21ed2019-11-04 09:37:55 -0800392 flags.Local.CppFlags = append(flags.Local.CppFlags, "-fno-rtti")
Colin Cross87dd9632017-11-03 13:31:05 -0700393 }
394 }
395
Colin Cross4af21ed2019-11-04 09:37:55 -0800396 flags.Global.AsFlags = append(flags.Global.AsFlags, "-D__ASSEMBLY__")
Colin Cross87dd9632017-11-03 13:31:05 -0700397
Colin Cross4af21ed2019-11-04 09:37:55 -0800398 flags.Global.CppFlags = append(flags.Global.CppFlags, tc.ClangCppflags())
Colin Cross87dd9632017-11-03 13:31:05 -0700399
Colin Cross4af21ed2019-11-04 09:37:55 -0800400 flags.Global.YasmFlags = append(flags.Global.YasmFlags, tc.YasmFlags())
Colin Cross87dd9632017-11-03 13:31:05 -0700401
Colin Cross4af21ed2019-11-04 09:37:55 -0800402 flags.Global.CommonFlags = append(flags.Global.CommonFlags, tc.ToolchainClangCflags())
Colin Cross4d9c2d12016-07-29 12:48:20 -0700403
Elliott Hughes5789ca92018-02-16 17:15:19 -0800404 cStd := config.CStdVersion
405 if String(compiler.Properties.C_std) == "experimental" {
406 cStd = config.ExperimentalCStdVersion
407 } else if String(compiler.Properties.C_std) != "" {
408 cStd = String(compiler.Properties.C_std)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700409 }
410
Elliott Hughes5789ca92018-02-16 17:15:19 -0800411 cppStd := String(compiler.Properties.Cpp_std)
412 switch String(compiler.Properties.Cpp_std) {
413 case "":
414 cppStd = config.CppStdVersion
415 case "experimental":
416 cppStd = config.ExperimentalCppStdVersion
Elliott Hughes5789ca92018-02-16 17:15:19 -0800417 }
418
Elliott Hughes5789ca92018-02-16 17:15:19 -0800419 if compiler.Properties.Gnu_extensions != nil && *compiler.Properties.Gnu_extensions == false {
420 cStd = gnuToCReplacer.Replace(cStd)
421 cppStd = gnuToCReplacer.Replace(cppStd)
422 }
423
Colin Cross4af21ed2019-11-04 09:37:55 -0800424 flags.Local.ConlyFlags = append([]string{"-std=" + cStd}, flags.Local.ConlyFlags...)
425 flags.Local.CppFlags = append([]string{"-std=" + cppStd}, flags.Local.CppFlags...)
Elliott Hughes5789ca92018-02-16 17:15:19 -0800426
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700427 if ctx.useVndk() {
Colin Cross4af21ed2019-11-04 09:37:55 -0800428 flags.Local.CFlags = append(flags.Local.CFlags, esc(compiler.Properties.Target.Vendor.Cflags)...)
Jiyong Parka622de82017-08-10 13:33:27 +0900429 }
430
Jiyong Park4a2dcb52018-05-24 16:44:14 +0900431 if ctx.inRecovery() {
Colin Cross4af21ed2019-11-04 09:37:55 -0800432 flags.Local.CFlags = append(flags.Local.CFlags, esc(compiler.Properties.Target.Recovery.Cflags)...)
Jiyong Park4a2dcb52018-05-24 16:44:14 +0900433 }
434
Colin Cross4d9c2d12016-07-29 12:48:20 -0700435 // We can enforce some rules more strictly in the code we own. strict
436 // indicates if this is code that we can be stricter with. If we have
437 // rules that we want to apply to *our* code (but maybe can't for
438 // vendor/device specific things), we could extend this to be a ternary
439 // value.
440 strict := true
441 if strings.HasPrefix(android.PathForModuleSrc(ctx).String(), "external/") {
442 strict = false
443 }
444
445 // Can be used to make some annotations stricter for code we can fix
446 // (such as when we mark functions as deprecated).
447 if strict {
Colin Cross4af21ed2019-11-04 09:37:55 -0800448 flags.Global.CFlags = append(flags.Global.CFlags, "-DANDROID_STRICT")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700449 }
450
Dan Willemsene1a3ce32016-11-02 20:44:08 -0700451 if compiler.hasSrcExt(".proto") {
Colin Cross0c461f12016-10-20 16:11:43 -0700452 flags = protoFlags(ctx, flags, &compiler.Proto)
453 }
454
Dan Willemsene1a3ce32016-11-02 20:44:08 -0700455 if compiler.hasSrcExt(".y") || compiler.hasSrcExt(".yy") {
Colin Cross4af21ed2019-11-04 09:37:55 -0800456 flags.Local.CommonFlags = append(flags.Local.CommonFlags,
Dan Willemsene1a3ce32016-11-02 20:44:08 -0700457 "-I"+android.PathForModuleGen(ctx, "yacc", ctx.ModuleDir()).String())
458 }
459
Dan Willemsen4f1c3d42017-09-09 01:15:26 -0700460 if compiler.hasSrcExt(".mc") {
Colin Cross4af21ed2019-11-04 09:37:55 -0800461 flags.Local.CommonFlags = append(flags.Local.CommonFlags,
Dan Willemsen4f1c3d42017-09-09 01:15:26 -0700462 "-I"+android.PathForModuleGen(ctx, "windmc", ctx.ModuleDir()).String())
463 }
464
Dan Willemsene1240db2016-11-03 14:28:51 -0700465 if compiler.hasSrcExt(".aidl") {
466 if len(compiler.Properties.Aidl.Local_include_dirs) > 0 {
467 localAidlIncludeDirs := android.PathsForModuleSrc(ctx, compiler.Properties.Aidl.Local_include_dirs)
468 flags.aidlFlags = append(flags.aidlFlags, includeDirsToFlags(localAidlIncludeDirs))
469 }
470 if len(compiler.Properties.Aidl.Include_dirs) > 0 {
471 rootAidlIncludeDirs := android.PathsForSource(ctx, compiler.Properties.Aidl.Include_dirs)
472 flags.aidlFlags = append(flags.aidlFlags, includeDirsToFlags(rootAidlIncludeDirs))
473 }
474
Martijn Coeneneab15642018-03-09 09:29:59 +0100475 if Bool(compiler.Properties.Aidl.Generate_traces) {
476 flags.aidlFlags = append(flags.aidlFlags, "-t")
477 }
478
Colin Cross4af21ed2019-11-04 09:37:55 -0800479 flags.Local.CommonFlags = append(flags.Local.CommonFlags,
Dan Willemsene1240db2016-11-03 14:28:51 -0700480 "-I"+android.PathForModuleGen(ctx, "aidl").String())
481 }
482
Jeff Vander Stoepd6126272019-07-15 19:08:37 -0700483 if compiler.hasSrcExt(".rscript") || compiler.hasSrcExt(".fs") {
Colin Cross2a252be2017-05-01 17:37:24 -0700484 flags = rsFlags(ctx, flags, &compiler.Properties)
485 }
486
Inseob Kim21f26902018-09-06 00:55:20 +0900487 if compiler.hasSrcExt(".sysprop") {
Colin Cross4af21ed2019-11-04 09:37:55 -0800488 flags.Local.CommonFlags = append(flags.Local.CommonFlags,
Inseob Kim21f26902018-09-06 00:55:20 +0900489 "-I"+android.PathForModuleGen(ctx, "sysprop", "include").String())
490 }
491
Chih-Hung Hsieh64a38dc2017-11-14 14:09:14 -0800492 if len(compiler.Properties.Srcs) > 0 {
493 module := ctx.ModuleDir() + "/Android.bp:" + ctx.ModuleName()
Colin Cross4af21ed2019-11-04 09:37:55 -0800494 if inList("-Wno-error", flags.Local.CFlags) || inList("-Wno-error", flags.Local.CppFlags) {
Colin Cross571cccf2019-02-04 11:22:08 -0800495 addToModuleList(ctx, modulesUsingWnoErrorKey, module)
Colin Cross4af21ed2019-11-04 09:37:55 -0800496 } else if !inList("-Werror", flags.Local.CFlags) && !inList("-Werror", flags.Local.CppFlags) {
Chih-Hung Hsieh64a38dc2017-11-14 14:09:14 -0800497 if warningsAreAllowed(ctx.ModuleDir()) {
Colin Cross571cccf2019-02-04 11:22:08 -0800498 addToModuleList(ctx, modulesAddedWallKey, module)
Colin Cross4af21ed2019-11-04 09:37:55 -0800499 flags.Local.CFlags = append([]string{"-Wall"}, flags.Local.CFlags...)
Chih-Hung Hsieh64a38dc2017-11-14 14:09:14 -0800500 } else {
Colin Cross4af21ed2019-11-04 09:37:55 -0800501 flags.Local.CFlags = append([]string{"-Wall", "-Werror"}, flags.Local.CFlags...)
Chih-Hung Hsieh64a38dc2017-11-14 14:09:14 -0800502 }
503 }
504 }
505
Pirama Arumuga Nainarfadb7b52017-12-19 23:08:09 -0800506 if Bool(compiler.Properties.Openmp) {
Colin Cross4af21ed2019-11-04 09:37:55 -0800507 flags.Local.CFlags = append(flags.Local.CFlags, "-fopenmp")
Pirama Arumuga Nainarfadb7b52017-12-19 23:08:09 -0800508 }
509
Colin Cross4d9c2d12016-07-29 12:48:20 -0700510 return flags
511}
512
Dan Willemsene1a3ce32016-11-02 20:44:08 -0700513func (compiler *baseCompiler) hasSrcExt(ext string) bool {
Colin Crossf18e1102017-11-16 14:33:08 -0800514 for _, src := range compiler.srcsBeforeGen {
515 if src.Ext() == ext {
516 return true
517 }
518 }
Colin Cross0c461f12016-10-20 16:11:43 -0700519 for _, src := range compiler.Properties.Srcs {
Dan Willemsene1a3ce32016-11-02 20:44:08 -0700520 if filepath.Ext(src) == ext {
Colin Cross0c461f12016-10-20 16:11:43 -0700521 return true
522 }
523 }
Colin Cross10d22312017-05-03 11:01:58 -0700524 for _, src := range compiler.Properties.OriginalSrcs {
525 if filepath.Ext(src) == ext {
526 return true
527 }
528 }
Colin Cross0c461f12016-10-20 16:11:43 -0700529
530 return false
531}
532
Jooyung Han77988572019-10-18 16:26:16 +0900533// makeDefineString transforms a name of an APEX module into a value to be used as value for C define
534// For example, com.android.foo => COM_ANDROID_FOO
535func makeDefineString(name string) string {
536 return strings.ReplaceAll(strings.ToUpper(name), ".", "_")
537}
538
Colin Cross948f0cb2016-10-17 14:24:56 -0700539var gnuToCReplacer = strings.NewReplacer("gnu", "c")
540
Colin Cross4d9c2d12016-07-29 12:48:20 -0700541func ndkPathDeps(ctx ModuleContext) android.Paths {
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700542 if ctx.useSdk() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700543 // The NDK sysroot timestamp file depends on all the NDK sysroot files
544 // (headers and libraries).
Dan Albert6ab43d82017-12-13 15:05:04 -0800545 return android.Paths{getNdkBaseTimestampFile(ctx)}
Colin Cross4d9c2d12016-07-29 12:48:20 -0700546 }
547 return nil
548}
549
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700550func (compiler *baseCompiler) compile(ctx ModuleContext, flags Flags, deps PathDeps) Objects {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700551 pathDeps := deps.GeneratedHeaders
552 pathDeps = append(pathDeps, ndkPathDeps(ctx)...)
Colin Cross2f336352016-10-26 10:03:47 -0700553
Colin Cross2f336352016-10-26 10:03:47 -0700554 buildFlags := flagsToBuilderFlags(flags)
555
Colin Crossf18e1102017-11-16 14:33:08 -0800556 srcs := append(android.Paths(nil), compiler.srcsBeforeGen...)
557
Colin Cross2f336352016-10-26 10:03:47 -0700558 srcs, genDeps := genSources(ctx, srcs, buildFlags)
Pirama Arumuga Nainarf231b192018-01-23 10:49:04 -0800559 pathDeps = append(pathDeps, genDeps...)
Colin Cross2f336352016-10-26 10:03:47 -0700560
Pirama Arumuga Nainar70ba5a32017-12-19 15:11:01 -0800561 compiler.pathDeps = pathDeps
Pirama Arumuga Nainarf231b192018-01-23 10:49:04 -0800562 compiler.cFlagsDeps = flags.CFlagsDeps
Colin Cross2f336352016-10-26 10:03:47 -0700563
Fabien Sanglardd61f1f42017-01-10 16:21:22 -0800564 // Save src, buildFlags and context
565 compiler.srcs = srcs
566
Colin Cross4d9c2d12016-07-29 12:48:20 -0700567 // Compile files listed in c.Properties.Srcs into objects
Pirama Arumuga Nainarf231b192018-01-23 10:49:04 -0800568 objs := compileObjs(ctx, buildFlags, "", srcs, pathDeps, compiler.cFlagsDeps)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700569
570 if ctx.Failed() {
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700571 return Objects{}
Colin Cross4d9c2d12016-07-29 12:48:20 -0700572 }
573
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700574 return objs
Colin Cross4d9c2d12016-07-29 12:48:20 -0700575}
576
577// Compile a list of source files into objects a specified subdirectory
Colin Cross2f336352016-10-26 10:03:47 -0700578func compileObjs(ctx android.ModuleContext, flags builderFlags,
Pirama Arumuga Nainarf231b192018-01-23 10:49:04 -0800579 subdir string, srcFiles, pathDeps android.Paths, cFlagsDeps android.Paths) Objects {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700580
Pirama Arumuga Nainarf231b192018-01-23 10:49:04 -0800581 return TransformSourceToObj(ctx, subdir, srcFiles, flags, pathDeps, cFlagsDeps)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700582}