blob: fe81bd0a47e56d88a2c09aafb64505d681f9859e [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
Ivan Lozanod094d402019-11-26 10:32:50 -080030var (
31 allowedManualInterfacePaths = []string{"vendor/", "hardware/"}
32)
33
Colin Cross4d9c2d12016-07-29 12:48:20 -070034// This file contains the basic C/C++/assembly to .o compliation steps
35
36type BaseCompilerProperties struct {
37 // 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 -080038 // srcs may reference the outputs of other modules that produce source files like genrule
39 // or filegroup using the syntax ":module".
Colin Cross27b922f2019-03-04 22:35:41 -080040 Srcs []string `android:"path,arch_variant"`
Colin Cross4d9c2d12016-07-29 12:48:20 -070041
42 // list of source files that should not be used to build the C/C++ module.
43 // This is most useful in the arch/multilib variants to remove non-common files
Colin Cross27b922f2019-03-04 22:35:41 -080044 Exclude_srcs []string `android:"path,arch_variant"`
Colin Cross4d9c2d12016-07-29 12:48:20 -070045
46 // list of module-specific flags that will be used for C and C++ compiles.
47 Cflags []string `android:"arch_variant"`
48
49 // list of module-specific flags that will be used for C++ compiles
50 Cppflags []string `android:"arch_variant"`
51
52 // list of module-specific flags that will be used for C compiles
53 Conlyflags []string `android:"arch_variant"`
54
55 // list of module-specific flags that will be used for .S compiles
56 Asflags []string `android:"arch_variant"`
57
58 // list of module-specific flags that will be used for C and C++ compiles when
59 // compiling with clang
60 Clang_cflags []string `android:"arch_variant"`
61
62 // list of module-specific flags that will be used for .S compiles when
63 // compiling with clang
64 Clang_asflags []string `android:"arch_variant"`
65
Colin Cross4d9c2d12016-07-29 12:48:20 -070066 // the instruction set architecture to use to compile the C/C++
67 // module.
Dan Willemsen5922f3c2017-10-25 15:20:50 -070068 Instruction_set *string `android:"arch_variant"`
Colin Cross4d9c2d12016-07-29 12:48:20 -070069
70 // list of directories relative to the root of the source tree that will
71 // be added to the include path using -I.
72 // If possible, don't use this. If adding paths from the current directory use
73 // local_include_dirs, if adding paths from other modules use export_include_dirs in
74 // that module.
Colin Crossccf01e72017-04-26 17:01:07 -070075 Include_dirs []string `android:"arch_variant,variant_prepend"`
Colin Cross4d9c2d12016-07-29 12:48:20 -070076
77 // list of directories relative to the Blueprints file that will
78 // be added to the include path using -I
Dan Willemsen59339a22018-07-22 21:18:45 -070079 Local_include_dirs []string `android:"arch_variant,variant_prepend"`
Colin Cross4d9c2d12016-07-29 12:48:20 -070080
Dan Albert899c23e2018-12-06 11:04:03 -080081 // Add the directory containing the Android.bp file to the list of include
82 // directories. Defaults to true.
83 Include_build_directory *bool
84
Colin Cross4d9c2d12016-07-29 12:48:20 -070085 // list of generated sources to compile. These are the names of gensrcs or
86 // genrule modules.
87 Generated_sources []string `android:"arch_variant"`
88
89 // list of generated headers to add to the include path. These are the names
90 // of genrule modules.
91 Generated_headers []string `android:"arch_variant"`
92
93 // pass -frtti instead of -fno-rtti
94 Rtti *bool
95
Dan Albert043833c2017-02-03 16:13:38 -080096 // C standard version to use. Can be a specific version (such as "gnu11"),
97 // "experimental" (which will use draft versions like C1x when available),
98 // or the empty string (which will use the default).
Nan Zhang0007d812017-11-07 10:57:05 -080099 C_std *string
Dan Albert043833c2017-02-03 16:13:38 -0800100
101 // C++ standard version to use. Can be a specific version (such as
102 // "gnu++11"), "experimental" (which will use draft versions like C++1z when
103 // available), or the empty string (which will use the default).
Nan Zhang0007d812017-11-07 10:57:05 -0800104 Cpp_std *string
Dan Albert043833c2017-02-03 16:13:38 -0800105
Colin Cross948f0cb2016-10-17 14:24:56 -0700106 // if set to false, use -std=c++* instead of -std=gnu++*
107 Gnu_extensions *bool
108
Dan Willemsen4e0aa232019-04-10 22:59:54 -0700109 Yacc *YaccProperties
110
Dan Willemsene1240db2016-11-03 14:28:51 -0700111 Aidl struct {
112 // list of directories that will be added to the aidl include paths.
113 Include_dirs []string
114
115 // list of directories relative to the Blueprints file that will
116 // be added to the aidl include paths.
117 Local_include_dirs []string
Martijn Coeneneab15642018-03-09 09:29:59 +0100118
119 // whether to generate traces (for systrace) for this interface
120 Generate_traces *bool
Dan Willemsene1240db2016-11-03 14:28:51 -0700121 }
122
Colin Cross2a252be2017-05-01 17:37:24 -0700123 Renderscript struct {
124 // list of directories that will be added to the llvm-rs-cc include paths
125 Include_dirs []string
126
127 // list of flags that will be passed to llvm-rs-cc
128 Flags []string
129
130 // Renderscript API level to target
131 Target_api *string
132 }
133
Colin Cross4d9c2d12016-07-29 12:48:20 -0700134 Debug, Release struct {
135 // list of module-specific flags that will be used for C and C++ compiles in debug or
136 // release builds
137 Cflags []string `android:"arch_variant"`
138 } `android:"arch_variant"`
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700139
140 Target struct {
141 Vendor struct {
142 // list of source files that should only be used in the
143 // vendor variant of the C/C++ module.
Colin Cross27b922f2019-03-04 22:35:41 -0800144 Srcs []string `android:"path"`
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700145
146 // list of source files that should not be used to
147 // build the vendor variant of the C/C++ module.
Colin Cross27b922f2019-03-04 22:35:41 -0800148 Exclude_srcs []string `android:"path"`
Jiyong Parka622de82017-08-10 13:33:27 +0900149
150 // List of additional cflags that should be used to build the vendor
151 // variant of the C/C++ module.
152 Cflags []string
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700153 }
Jiyong Parkf9332f12018-02-01 00:54:12 +0900154 Recovery struct {
155 // list of source files that should only be used in the
156 // recovery variant of the C/C++ module.
Colin Cross27b922f2019-03-04 22:35:41 -0800157 Srcs []string `android:"path"`
Jiyong Parkf9332f12018-02-01 00:54:12 +0900158
159 // list of source files that should not be used to
160 // build the recovery variant of the C/C++ module.
Colin Cross27b922f2019-03-04 22:35:41 -0800161 Exclude_srcs []string `android:"path"`
Jiyong Parkf9332f12018-02-01 00:54:12 +0900162
163 // List of additional cflags that should be used to build the recovery
164 // variant of the C/C++ module.
165 Cflags []string
166 }
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700167 }
Colin Cross10d22312017-05-03 11:01:58 -0700168
Colin Cross38f794e2017-09-07 10:53:07 -0700169 Proto struct {
170 // Link statically against the protobuf runtime
Nan Zhang0007d812017-11-07 10:57:05 -0800171 Static *bool `android:"arch_variant"`
Colin Cross38f794e2017-09-07 10:53:07 -0700172 } `android:"arch_variant"`
173
Colin Cross10d22312017-05-03 11:01:58 -0700174 // Stores the original list of source files before being cleared by library reuse
175 OriginalSrcs []string `blueprint:"mutated"`
Pirama Arumuga Nainarfadb7b52017-12-19 23:08:09 -0800176
177 // Build and link with OpenMP
178 Openmp *bool `android:"arch_variant"`
Jooyung Han68e511e2020-03-02 17:44:33 +0900179
180 // Adds __ANDROID_APEX_<APEX_MODULE_NAME>__ macro defined for apex variants in addition to __ANDROID_APEX__
181 Use_apex_name_macro *bool
Colin Cross4d9c2d12016-07-29 12:48:20 -0700182}
183
Colin Crossb916a382016-07-29 17:28:03 -0700184func NewBaseCompiler() *baseCompiler {
185 return &baseCompiler{}
186}
187
Colin Cross4d9c2d12016-07-29 12:48:20 -0700188type baseCompiler struct {
189 Properties BaseCompilerProperties
Colin Cross38f794e2017-09-07 10:53:07 -0700190 Proto android.ProtoProperties
Pirama Arumuga Nainarf231b192018-01-23 10:49:04 -0800191 cFlagsDeps android.Paths
Pirama Arumuga Nainar70ba5a32017-12-19 15:11:01 -0800192 pathDeps android.Paths
Fabien Sanglardd61f1f42017-01-10 16:21:22 -0800193 flags builderFlags
Colin Crossf18e1102017-11-16 14:33:08 -0800194
195 // Sources that were passed to the C/C++ compiler
196 srcs android.Paths
197
198 // Sources that were passed in the Android.bp file, including generated sources generated by
199 // other modules and filegroups. May include source files that have not yet been translated to
200 // C/C++ (.aidl, .proto, etc.)
201 srcsBeforeGen android.Paths
Colin Cross4d9c2d12016-07-29 12:48:20 -0700202}
203
204var _ compiler = (*baseCompiler)(nil)
205
Fabien Sanglardd61f1f42017-01-10 16:21:22 -0800206type CompiledInterface interface {
207 Srcs() android.Paths
208}
209
210func (compiler *baseCompiler) Srcs() android.Paths {
Nan Zhange42777a2018-03-27 16:19:42 -0700211 return append(android.Paths{}, compiler.srcs...)
Fabien Sanglardd61f1f42017-01-10 16:21:22 -0800212}
213
Colin Cross4d9c2d12016-07-29 12:48:20 -0700214func (compiler *baseCompiler) appendCflags(flags []string) {
215 compiler.Properties.Cflags = append(compiler.Properties.Cflags, flags...)
216}
217
218func (compiler *baseCompiler) appendAsflags(flags []string) {
219 compiler.Properties.Asflags = append(compiler.Properties.Asflags, flags...)
220}
221
Colin Cross42742b82016-08-01 13:20:05 -0700222func (compiler *baseCompiler) compilerProps() []interface{} {
Colin Cross0feb1692016-11-03 14:38:52 -0700223 return []interface{}{&compiler.Properties, &compiler.Proto}
Colin Cross4d9c2d12016-07-29 12:48:20 -0700224}
225
Colin Cross42742b82016-08-01 13:20:05 -0700226func (compiler *baseCompiler) compilerInit(ctx BaseModuleContext) {}
Colin Cross4d9c2d12016-07-29 12:48:20 -0700227
Colin Cross37047f12016-12-13 17:06:13 -0800228func (compiler *baseCompiler) compilerDeps(ctx DepsContext, deps Deps) Deps {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700229 deps.GeneratedSources = append(deps.GeneratedSources, compiler.Properties.Generated_sources...)
230 deps.GeneratedHeaders = append(deps.GeneratedHeaders, compiler.Properties.Generated_headers...)
231
Colin Crossfe17f6f2019-03-28 19:30:56 -0700232 android.ProtoDeps(ctx, &compiler.Proto)
Dan Willemsene1a3ce32016-11-02 20:44:08 -0700233 if compiler.hasSrcExt(".proto") {
Nan Zhang0007d812017-11-07 10:57:05 -0800234 deps = protoDeps(ctx, deps, &compiler.Proto, Bool(compiler.Properties.Proto.Static))
Colin Cross0c461f12016-10-20 16:11:43 -0700235 }
236
Pirama Arumuga Nainarfadb7b52017-12-19 23:08:09 -0800237 if Bool(compiler.Properties.Openmp) {
238 deps.StaticLibs = append(deps.StaticLibs, "libomp")
239 }
240
Colin Cross4d9c2d12016-07-29 12:48:20 -0700241 return deps
242}
243
Chih-Hung Hsieh64a38dc2017-11-14 14:09:14 -0800244// Return true if the module is in the WarningAllowedProjects.
245func warningsAreAllowed(subdir string) bool {
246 subdir += "/"
Jaewoong Jung3aff5782020-02-11 07:54:35 -0800247 return android.HasAnyPrefix(subdir, config.WarningAllowedProjects)
Chih-Hung Hsieh64a38dc2017-11-14 14:09:14 -0800248}
249
Colin Cross571cccf2019-02-04 11:22:08 -0800250func addToModuleList(ctx ModuleContext, key android.OnceKey, module string) {
251 getNamedMapForConfig(ctx.Config(), key).Store(module, true)
Chih-Hung Hsieh64a38dc2017-11-14 14:09:14 -0800252}
253
Colin Cross4d9c2d12016-07-29 12:48:20 -0700254// Create a Flags struct that collects the compile flags from global values,
255// per-target values, module type values, and per-module Blueprints properties
Colin Crossf18e1102017-11-16 14:33:08 -0800256func (compiler *baseCompiler) compilerFlags(ctx ModuleContext, flags Flags, deps PathDeps) Flags {
Colin Crossb98c8b02016-07-29 13:44:28 -0700257 tc := ctx.toolchain()
Yi Kong950e0ba2019-10-08 02:27:17 -0700258 modulePath := android.PathForModuleSrc(ctx).String()
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
Colin Cross4af21ed2019-11-04 09:37:55 -0800272 flags.Local.CFlags = append(flags.Local.CFlags, esc(compiler.Properties.Cflags)...)
273 flags.Local.CppFlags = append(flags.Local.CppFlags, esc(compiler.Properties.Cppflags)...)
274 flags.Local.ConlyFlags = append(flags.Local.ConlyFlags, esc(compiler.Properties.Conlyflags)...)
275 flags.Local.AsFlags = append(flags.Local.AsFlags, esc(compiler.Properties.Asflags)...)
276 flags.Local.YasmFlags = append(flags.Local.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)
Colin Cross4af21ed2019-11-04 09:37:55 -0800284 flags.Local.CommonFlags = append(flags.Local.CommonFlags, f)
285 flags.Local.YasmFlags = append(flags.Local.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)
Colin Cross4af21ed2019-11-04 09:37:55 -0800290 flags.Local.CommonFlags = append(flags.Local.CommonFlags, f)
291 flags.Local.YasmFlags = append(flags.Local.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 {
Yi Kong950e0ba2019-10-08 02:27:17 -0700296 flags.Local.CommonFlags = append(flags.Local.CommonFlags, "-I"+modulePath)
297 flags.Local.YasmFlags = append(flags.Local.YasmFlags, "-I"+modulePath)
Dan Albert899c23e2018-12-06 11:04:03 -0800298 }
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 }
317
Jooyung Han61c41542020-03-07 03:45:53 +0900318 if ctx.canUseSdk() {
319 sdkVersion := ctx.sdkVersion()
320 if sdkVersion == "" || sdkVersion == "current" {
321 if ctx.isForPlatform() {
322 sdkVersion = strconv.Itoa(android.FutureApiLevel)
323 } else {
324 sdkVersion = strconv.Itoa(ctx.apexSdkVersion())
325 }
326 }
327 flags.Global.CommonFlags = append(flags.Global.CommonFlags, "-D__ANDROID_SDK_VERSION__="+sdkVersion)
328 }
329
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700330 if ctx.useVndk() {
Colin Cross4af21ed2019-11-04 09:37:55 -0800331 flags.Global.CommonFlags = append(flags.Global.CommonFlags, "-D__ANDROID_VNDK__")
Dan Willemsenb916b802017-03-19 13:44:32 -0700332 }
333
Jiyong Parkd54aee52018-06-09 01:32:54 +0900334 if ctx.inRecovery() {
Colin Cross4af21ed2019-11-04 09:37:55 -0800335 flags.Global.CommonFlags = append(flags.Global.CommonFlags, "-D__ANDROID_RECOVERY__")
Jiyong Parkd54aee52018-06-09 01:32:54 +0900336 }
337
Jiyong Park58e364a2019-01-19 19:24:06 +0900338 if ctx.apexName() != "" {
Jooyung Han68e511e2020-03-02 17:44:33 +0900339 flags.Global.CommonFlags = append(flags.Global.CommonFlags, "-D__ANDROID_APEX__")
340 if Bool(compiler.Properties.Use_apex_name_macro) {
341 flags.Global.CommonFlags = append(flags.Global.CommonFlags, "-D__ANDROID_APEX_"+makeDefineString(ctx.apexName())+"__")
342 }
Jiyong Park58e364a2019-01-19 19:24:06 +0900343 }
344
Nan Zhang0007d812017-11-07 10:57:05 -0800345 instructionSet := String(compiler.Properties.Instruction_set)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700346 if flags.RequiredInstructionSet != "" {
347 instructionSet = flags.RequiredInstructionSet
348 }
Dan Willemsen8536d6b2018-10-07 20:54:34 -0700349 instructionSetFlags, err := tc.ClangInstructionSetFlags(instructionSet)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700350 if err != nil {
351 ctx.ModuleErrorf("%s", err)
352 }
353
354 CheckBadCompilerFlags(ctx, "release.cflags", compiler.Properties.Release.Cflags)
355
356 // TODO: debug
Colin Cross4af21ed2019-11-04 09:37:55 -0800357 flags.Local.CFlags = append(flags.Local.CFlags, esc(compiler.Properties.Release.Cflags)...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700358
Dan Willemsen8536d6b2018-10-07 20:54:34 -0700359 CheckBadCompilerFlags(ctx, "clang_cflags", compiler.Properties.Clang_cflags)
360 CheckBadCompilerFlags(ctx, "clang_asflags", compiler.Properties.Clang_asflags)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700361
Colin Cross4af21ed2019-11-04 09:37:55 -0800362 flags.Local.CFlags = config.ClangFilterUnknownCflags(flags.Local.CFlags)
363 flags.Local.CFlags = append(flags.Local.CFlags, esc(compiler.Properties.Clang_cflags)...)
364 flags.Local.AsFlags = append(flags.Local.AsFlags, esc(compiler.Properties.Clang_asflags)...)
365 flags.Local.CppFlags = config.ClangFilterUnknownCflags(flags.Local.CppFlags)
366 flags.Local.ConlyFlags = config.ClangFilterUnknownCflags(flags.Local.ConlyFlags)
367 flags.Local.LdFlags = config.ClangFilterUnknownCflags(flags.Local.LdFlags)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700368
Dan Willemsen8536d6b2018-10-07 20:54:34 -0700369 target := "-target " + tc.ClangTriple()
Peter Collingbourneb0e61432019-07-22 16:36:06 -0700370 if ctx.Os().Class == android.Device {
371 version := ctx.sdkVersion()
372 if version == "" || version == "current" {
373 target += strconv.Itoa(android.FutureApiLevel)
374 } else {
375 target += version
376 }
377 }
378
Dan Willemsen8536d6b2018-10-07 20:54:34 -0700379 gccPrefix := "-B" + config.ToolPath(tc)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700380
Colin Cross4af21ed2019-11-04 09:37:55 -0800381 flags.Global.CFlags = append(flags.Global.CFlags, target, gccPrefix)
382 flags.Global.AsFlags = append(flags.Global.AsFlags, target, gccPrefix)
383 flags.Global.LdFlags = append(flags.Global.LdFlags, target, gccPrefix)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700384
Colin Crossb98c8b02016-07-29 13:44:28 -0700385 hod := "Host"
Colin Cross4d9c2d12016-07-29 12:48:20 -0700386 if ctx.Os().Class == android.Device {
Colin Crossb98c8b02016-07-29 13:44:28 -0700387 hod = "Device"
Colin Cross4d9c2d12016-07-29 12:48:20 -0700388 }
389
Colin Cross4af21ed2019-11-04 09:37:55 -0800390 flags.Global.CommonFlags = append(flags.Global.CommonFlags, instructionSetFlags)
391 flags.Global.ConlyFlags = append([]string{"${config.CommonGlobalConlyflags}"}, flags.Global.ConlyFlags...)
392 flags.Global.CppFlags = append([]string{fmt.Sprintf("${config.%sGlobalCppflags}", hod)}, flags.Global.CppFlags...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700393
Colin Cross4af21ed2019-11-04 09:37:55 -0800394 flags.Global.AsFlags = append(flags.Global.AsFlags, tc.ClangAsflags())
395 flags.Global.CppFlags = append([]string{"${config.CommonClangGlobalCppflags}"}, flags.Global.CppFlags...)
396 flags.Global.CommonFlags = append(flags.Global.CommonFlags,
Dan Willemsen8536d6b2018-10-07 20:54:34 -0700397 tc.ClangCflags(),
398 "${config.CommonClangGlobalCflags}",
399 fmt.Sprintf("${config.%sClangGlobalCflags}", hod))
Colin Cross4d9c2d12016-07-29 12:48:20 -0700400
Yi Kong950e0ba2019-10-08 02:27:17 -0700401 if isThirdParty(modulePath) {
Colin Cross4af21ed2019-11-04 09:37:55 -0800402 flags.Global.CommonFlags = append([]string{"${config.ClangExternalCflags}"}, flags.Global.CommonFlags...)
Yi Kongcc80f8d2018-06-06 14:42:44 -0700403 }
404
Dan Willemsen10db3842018-10-21 18:42:46 -0700405 if tc.Bionic() {
Colin Cross87dd9632017-11-03 13:31:05 -0700406 if Bool(compiler.Properties.Rtti) {
Colin Cross4af21ed2019-11-04 09:37:55 -0800407 flags.Local.CppFlags = append(flags.Local.CppFlags, "-frtti")
Colin Cross87dd9632017-11-03 13:31:05 -0700408 } else {
Colin Cross4af21ed2019-11-04 09:37:55 -0800409 flags.Local.CppFlags = append(flags.Local.CppFlags, "-fno-rtti")
Colin Cross87dd9632017-11-03 13:31:05 -0700410 }
411 }
412
Colin Cross4af21ed2019-11-04 09:37:55 -0800413 flags.Global.AsFlags = append(flags.Global.AsFlags, "-D__ASSEMBLY__")
Colin Cross87dd9632017-11-03 13:31:05 -0700414
Colin Cross4af21ed2019-11-04 09:37:55 -0800415 flags.Global.CppFlags = append(flags.Global.CppFlags, tc.ClangCppflags())
Colin Cross87dd9632017-11-03 13:31:05 -0700416
Colin Cross4af21ed2019-11-04 09:37:55 -0800417 flags.Global.YasmFlags = append(flags.Global.YasmFlags, tc.YasmFlags())
Colin Cross87dd9632017-11-03 13:31:05 -0700418
Colin Cross4af21ed2019-11-04 09:37:55 -0800419 flags.Global.CommonFlags = append(flags.Global.CommonFlags, tc.ToolchainClangCflags())
Colin Cross4d9c2d12016-07-29 12:48:20 -0700420
Elliott Hughes5789ca92018-02-16 17:15:19 -0800421 cStd := config.CStdVersion
422 if String(compiler.Properties.C_std) == "experimental" {
423 cStd = config.ExperimentalCStdVersion
424 } else if String(compiler.Properties.C_std) != "" {
425 cStd = String(compiler.Properties.C_std)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700426 }
427
Elliott Hughes5789ca92018-02-16 17:15:19 -0800428 cppStd := String(compiler.Properties.Cpp_std)
429 switch String(compiler.Properties.Cpp_std) {
430 case "":
431 cppStd = config.CppStdVersion
432 case "experimental":
433 cppStd = config.ExperimentalCppStdVersion
Elliott Hughes5789ca92018-02-16 17:15:19 -0800434 }
435
Elliott Hughes5789ca92018-02-16 17:15:19 -0800436 if compiler.Properties.Gnu_extensions != nil && *compiler.Properties.Gnu_extensions == false {
437 cStd = gnuToCReplacer.Replace(cStd)
438 cppStd = gnuToCReplacer.Replace(cppStd)
439 }
440
Colin Cross4af21ed2019-11-04 09:37:55 -0800441 flags.Local.ConlyFlags = append([]string{"-std=" + cStd}, flags.Local.ConlyFlags...)
442 flags.Local.CppFlags = append([]string{"-std=" + cppStd}, flags.Local.CppFlags...)
Elliott Hughes5789ca92018-02-16 17:15:19 -0800443
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700444 if ctx.useVndk() {
Colin Cross4af21ed2019-11-04 09:37:55 -0800445 flags.Local.CFlags = append(flags.Local.CFlags, esc(compiler.Properties.Target.Vendor.Cflags)...)
Jiyong Parka622de82017-08-10 13:33:27 +0900446 }
447
Jiyong Park4a2dcb52018-05-24 16:44:14 +0900448 if ctx.inRecovery() {
Colin Cross4af21ed2019-11-04 09:37:55 -0800449 flags.Local.CFlags = append(flags.Local.CFlags, esc(compiler.Properties.Target.Recovery.Cflags)...)
Jiyong Park4a2dcb52018-05-24 16:44:14 +0900450 }
451
Colin Cross4d9c2d12016-07-29 12:48:20 -0700452 // We can enforce some rules more strictly in the code we own. strict
453 // indicates if this is code that we can be stricter with. If we have
454 // rules that we want to apply to *our* code (but maybe can't for
455 // vendor/device specific things), we could extend this to be a ternary
456 // value.
457 strict := true
Yi Kong950e0ba2019-10-08 02:27:17 -0700458 if strings.HasPrefix(modulePath, "external/") {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700459 strict = false
460 }
461
462 // Can be used to make some annotations stricter for code we can fix
463 // (such as when we mark functions as deprecated).
464 if strict {
Colin Cross4af21ed2019-11-04 09:37:55 -0800465 flags.Global.CFlags = append(flags.Global.CFlags, "-DANDROID_STRICT")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700466 }
467
Dan Willemsene1a3ce32016-11-02 20:44:08 -0700468 if compiler.hasSrcExt(".proto") {
Colin Cross0c461f12016-10-20 16:11:43 -0700469 flags = protoFlags(ctx, flags, &compiler.Proto)
470 }
471
Dan Willemsene1a3ce32016-11-02 20:44:08 -0700472 if compiler.hasSrcExt(".y") || compiler.hasSrcExt(".yy") {
Colin Cross4af21ed2019-11-04 09:37:55 -0800473 flags.Local.CommonFlags = append(flags.Local.CommonFlags,
Dan Willemsene1a3ce32016-11-02 20:44:08 -0700474 "-I"+android.PathForModuleGen(ctx, "yacc", ctx.ModuleDir()).String())
475 }
476
Dan Willemsen4f1c3d42017-09-09 01:15:26 -0700477 if compiler.hasSrcExt(".mc") {
Colin Cross4af21ed2019-11-04 09:37:55 -0800478 flags.Local.CommonFlags = append(flags.Local.CommonFlags,
Dan Willemsen4f1c3d42017-09-09 01:15:26 -0700479 "-I"+android.PathForModuleGen(ctx, "windmc", ctx.ModuleDir()).String())
480 }
481
Dan Willemsene1240db2016-11-03 14:28:51 -0700482 if compiler.hasSrcExt(".aidl") {
483 if len(compiler.Properties.Aidl.Local_include_dirs) > 0 {
484 localAidlIncludeDirs := android.PathsForModuleSrc(ctx, compiler.Properties.Aidl.Local_include_dirs)
485 flags.aidlFlags = append(flags.aidlFlags, includeDirsToFlags(localAidlIncludeDirs))
486 }
487 if len(compiler.Properties.Aidl.Include_dirs) > 0 {
488 rootAidlIncludeDirs := android.PathsForSource(ctx, compiler.Properties.Aidl.Include_dirs)
489 flags.aidlFlags = append(flags.aidlFlags, includeDirsToFlags(rootAidlIncludeDirs))
490 }
491
Martijn Coeneneab15642018-03-09 09:29:59 +0100492 if Bool(compiler.Properties.Aidl.Generate_traces) {
493 flags.aidlFlags = append(flags.aidlFlags, "-t")
494 }
495
Colin Cross4af21ed2019-11-04 09:37:55 -0800496 flags.Local.CommonFlags = append(flags.Local.CommonFlags,
Dan Willemsene1240db2016-11-03 14:28:51 -0700497 "-I"+android.PathForModuleGen(ctx, "aidl").String())
498 }
499
Jeff Vander Stoepd6126272019-07-15 19:08:37 -0700500 if compiler.hasSrcExt(".rscript") || compiler.hasSrcExt(".fs") {
Colin Cross2a252be2017-05-01 17:37:24 -0700501 flags = rsFlags(ctx, flags, &compiler.Properties)
502 }
503
Inseob Kim21f26902018-09-06 00:55:20 +0900504 if compiler.hasSrcExt(".sysprop") {
Colin Cross4af21ed2019-11-04 09:37:55 -0800505 flags.Local.CommonFlags = append(flags.Local.CommonFlags,
Inseob Kim21f26902018-09-06 00:55:20 +0900506 "-I"+android.PathForModuleGen(ctx, "sysprop", "include").String())
507 }
508
Chih-Hung Hsieh64a38dc2017-11-14 14:09:14 -0800509 if len(compiler.Properties.Srcs) > 0 {
510 module := ctx.ModuleDir() + "/Android.bp:" + ctx.ModuleName()
Colin Cross4af21ed2019-11-04 09:37:55 -0800511 if inList("-Wno-error", flags.Local.CFlags) || inList("-Wno-error", flags.Local.CppFlags) {
Colin Cross571cccf2019-02-04 11:22:08 -0800512 addToModuleList(ctx, modulesUsingWnoErrorKey, module)
Colin Cross4af21ed2019-11-04 09:37:55 -0800513 } else if !inList("-Werror", flags.Local.CFlags) && !inList("-Werror", flags.Local.CppFlags) {
Chih-Hung Hsieh64a38dc2017-11-14 14:09:14 -0800514 if warningsAreAllowed(ctx.ModuleDir()) {
Colin Cross571cccf2019-02-04 11:22:08 -0800515 addToModuleList(ctx, modulesAddedWallKey, module)
Colin Cross4af21ed2019-11-04 09:37:55 -0800516 flags.Local.CFlags = append([]string{"-Wall"}, flags.Local.CFlags...)
Chih-Hung Hsieh64a38dc2017-11-14 14:09:14 -0800517 } else {
Colin Cross4af21ed2019-11-04 09:37:55 -0800518 flags.Local.CFlags = append([]string{"-Wall", "-Werror"}, flags.Local.CFlags...)
Chih-Hung Hsieh64a38dc2017-11-14 14:09:14 -0800519 }
520 }
521 }
522
Pirama Arumuga Nainarfadb7b52017-12-19 23:08:09 -0800523 if Bool(compiler.Properties.Openmp) {
Colin Cross4af21ed2019-11-04 09:37:55 -0800524 flags.Local.CFlags = append(flags.Local.CFlags, "-fopenmp")
Pirama Arumuga Nainarfadb7b52017-12-19 23:08:09 -0800525 }
526
Ivan Lozanod094d402019-11-26 10:32:50 -0800527 // Exclude directories from manual binder interface whitelisting.
528 //TODO(b/145621474): Move this check into IInterface.h when clang-tidy no longer uses absolute paths.
Jaewoong Jung3aff5782020-02-11 07:54:35 -0800529 if android.HasAnyPrefix(ctx.ModuleDir(), allowedManualInterfacePaths) {
Ivan Lozanod094d402019-11-26 10:32:50 -0800530 flags.Local.CFlags = append(flags.Local.CFlags, "-DDO_NOT_CHECK_MANUAL_BINDER_INTERFACES")
531 }
532
Colin Cross4d9c2d12016-07-29 12:48:20 -0700533 return flags
534}
535
Dan Willemsene1a3ce32016-11-02 20:44:08 -0700536func (compiler *baseCompiler) hasSrcExt(ext string) bool {
Colin Crossf18e1102017-11-16 14:33:08 -0800537 for _, src := range compiler.srcsBeforeGen {
538 if src.Ext() == ext {
539 return true
540 }
541 }
Colin Cross0c461f12016-10-20 16:11:43 -0700542 for _, src := range compiler.Properties.Srcs {
Dan Willemsene1a3ce32016-11-02 20:44:08 -0700543 if filepath.Ext(src) == ext {
Colin Cross0c461f12016-10-20 16:11:43 -0700544 return true
545 }
546 }
Colin Cross10d22312017-05-03 11:01:58 -0700547 for _, src := range compiler.Properties.OriginalSrcs {
548 if filepath.Ext(src) == ext {
549 return true
550 }
551 }
Colin Cross0c461f12016-10-20 16:11:43 -0700552
553 return false
554}
555
Jooyung Han77988572019-10-18 16:26:16 +0900556// makeDefineString transforms a name of an APEX module into a value to be used as value for C define
557// For example, com.android.foo => COM_ANDROID_FOO
558func makeDefineString(name string) string {
559 return strings.ReplaceAll(strings.ToUpper(name), ".", "_")
560}
561
Colin Cross948f0cb2016-10-17 14:24:56 -0700562var gnuToCReplacer = strings.NewReplacer("gnu", "c")
563
Colin Cross4d9c2d12016-07-29 12:48:20 -0700564func ndkPathDeps(ctx ModuleContext) android.Paths {
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700565 if ctx.useSdk() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700566 // The NDK sysroot timestamp file depends on all the NDK sysroot files
567 // (headers and libraries).
Dan Albert6ab43d82017-12-13 15:05:04 -0800568 return android.Paths{getNdkBaseTimestampFile(ctx)}
Colin Cross4d9c2d12016-07-29 12:48:20 -0700569 }
570 return nil
571}
572
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700573func (compiler *baseCompiler) compile(ctx ModuleContext, flags Flags, deps PathDeps) Objects {
Inseob Kimd110f872019-12-06 13:15:38 +0900574 pathDeps := deps.GeneratedDeps
Colin Cross4d9c2d12016-07-29 12:48:20 -0700575 pathDeps = append(pathDeps, ndkPathDeps(ctx)...)
Colin Cross2f336352016-10-26 10:03:47 -0700576
Colin Cross2f336352016-10-26 10:03:47 -0700577 buildFlags := flagsToBuilderFlags(flags)
578
Colin Crossf18e1102017-11-16 14:33:08 -0800579 srcs := append(android.Paths(nil), compiler.srcsBeforeGen...)
580
Colin Cross2f336352016-10-26 10:03:47 -0700581 srcs, genDeps := genSources(ctx, srcs, buildFlags)
Pirama Arumuga Nainarf231b192018-01-23 10:49:04 -0800582 pathDeps = append(pathDeps, genDeps...)
Colin Cross2f336352016-10-26 10:03:47 -0700583
Pirama Arumuga Nainar70ba5a32017-12-19 15:11:01 -0800584 compiler.pathDeps = pathDeps
Pirama Arumuga Nainarf231b192018-01-23 10:49:04 -0800585 compiler.cFlagsDeps = flags.CFlagsDeps
Colin Cross2f336352016-10-26 10:03:47 -0700586
Fabien Sanglardd61f1f42017-01-10 16:21:22 -0800587 // Save src, buildFlags and context
588 compiler.srcs = srcs
589
Colin Cross4d9c2d12016-07-29 12:48:20 -0700590 // Compile files listed in c.Properties.Srcs into objects
Pirama Arumuga Nainarf231b192018-01-23 10:49:04 -0800591 objs := compileObjs(ctx, buildFlags, "", srcs, pathDeps, compiler.cFlagsDeps)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700592
593 if ctx.Failed() {
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700594 return Objects{}
Colin Cross4d9c2d12016-07-29 12:48:20 -0700595 }
596
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700597 return objs
Colin Cross4d9c2d12016-07-29 12:48:20 -0700598}
599
600// Compile a list of source files into objects a specified subdirectory
Colin Cross2f336352016-10-26 10:03:47 -0700601func compileObjs(ctx android.ModuleContext, flags builderFlags,
Pirama Arumuga Nainarf231b192018-01-23 10:49:04 -0800602 subdir string, srcFiles, pathDeps android.Paths, cFlagsDeps android.Paths) Objects {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700603
Pirama Arumuga Nainarf231b192018-01-23 10:49:04 -0800604 return TransformSourceToObj(ctx, subdir, srcFiles, flags, pathDeps, cFlagsDeps)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700605}
Yi Kong950e0ba2019-10-08 02:27:17 -0700606
607var thirdPartyDirPrefixExceptions = []*regexp.Regexp{
608 regexp.MustCompile("^vendor/[^/]*google[^/]*/"),
609 regexp.MustCompile("^hardware/google/"),
610 regexp.MustCompile("^hardware/interfaces/"),
611 regexp.MustCompile("^hardware/libhardware[^/]*/"),
612 regexp.MustCompile("^hardware/ril/"),
613}
614
615func isThirdParty(path string) bool {
616 thirdPartyDirPrefixes := []string{"external/", "vendor/", "hardware/"}
617
Jaewoong Jung3aff5782020-02-11 07:54:35 -0800618 if android.HasAnyPrefix(path, thirdPartyDirPrefixes) {
619 for _, prefix := range thirdPartyDirPrefixExceptions {
620 if prefix.MatchString(path) {
621 return false
Yi Kong950e0ba2019-10-08 02:27:17 -0700622 }
Yi Kong950e0ba2019-10-08 02:27:17 -0700623 }
624 }
Yi Kong950e0ba2019-10-08 02:27:17 -0700625 return true
626}