blob: 8d034c9e03459311e3c82d4599384f95e0cb779a [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 Cross4d9c2d12016-07-29 12:48:20 -070034 Srcs []string `android:"arch_variant"`
35
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
38 Exclude_srcs []string `android:"arch_variant"`
39
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
60 // list of module-specific flags that will be used for .y and .yy compiles
61 Yaccflags []string
62
63 // the instruction set architecture to use to compile the C/C++
64 // module.
Dan Willemsen5922f3c2017-10-25 15:20:50 -070065 Instruction_set *string `android:"arch_variant"`
Colin Cross4d9c2d12016-07-29 12:48:20 -070066
67 // list of directories relative to the root of the source tree that will
68 // be added to the include path using -I.
69 // If possible, don't use this. If adding paths from the current directory use
70 // local_include_dirs, if adding paths from other modules use export_include_dirs in
71 // that module.
Colin Crossccf01e72017-04-26 17:01:07 -070072 Include_dirs []string `android:"arch_variant,variant_prepend"`
Colin Cross4d9c2d12016-07-29 12:48:20 -070073
74 // list of directories relative to the Blueprints file that will
75 // be added to the include path using -I
Colin Crossccf01e72017-04-26 17:01:07 -070076 Local_include_dirs []string `android:"arch_variant,variant_prepend",`
Colin Cross4d9c2d12016-07-29 12:48:20 -070077
78 // list of generated sources to compile. These are the names of gensrcs or
79 // genrule modules.
80 Generated_sources []string `android:"arch_variant"`
81
82 // list of generated headers to add to the include path. These are the names
83 // of genrule modules.
84 Generated_headers []string `android:"arch_variant"`
85
86 // pass -frtti instead of -fno-rtti
87 Rtti *bool
88
Dan Albert043833c2017-02-03 16:13:38 -080089 // C standard version to use. Can be a specific version (such as "gnu11"),
90 // "experimental" (which will use draft versions like C1x when available),
91 // or the empty string (which will use the default).
Nan Zhang0007d812017-11-07 10:57:05 -080092 C_std *string
Dan Albert043833c2017-02-03 16:13:38 -080093
94 // C++ standard version to use. Can be a specific version (such as
95 // "gnu++11"), "experimental" (which will use draft versions like C++1z when
96 // available), or the empty string (which will use the default).
Nan Zhang0007d812017-11-07 10:57:05 -080097 Cpp_std *string
Dan Albert043833c2017-02-03 16:13:38 -080098
Colin Cross948f0cb2016-10-17 14:24:56 -070099 // if set to false, use -std=c++* instead of -std=gnu++*
100 Gnu_extensions *bool
101
Dan Willemsene1240db2016-11-03 14:28:51 -0700102 Aidl struct {
103 // list of directories that will be added to the aidl include paths.
104 Include_dirs []string
105
106 // list of directories relative to the Blueprints file that will
107 // be added to the aidl include paths.
108 Local_include_dirs []string
Martijn Coeneneab15642018-03-09 09:29:59 +0100109
110 // whether to generate traces (for systrace) for this interface
111 Generate_traces *bool
Dan Willemsene1240db2016-11-03 14:28:51 -0700112 }
113
Colin Cross2a252be2017-05-01 17:37:24 -0700114 Renderscript struct {
115 // list of directories that will be added to the llvm-rs-cc include paths
116 Include_dirs []string
117
118 // list of flags that will be passed to llvm-rs-cc
119 Flags []string
120
121 // Renderscript API level to target
122 Target_api *string
123 }
124
Colin Cross4d9c2d12016-07-29 12:48:20 -0700125 Debug, Release struct {
126 // list of module-specific flags that will be used for C and C++ compiles in debug or
127 // release builds
128 Cflags []string `android:"arch_variant"`
129 } `android:"arch_variant"`
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700130
131 Target struct {
132 Vendor struct {
133 // list of source files that should only be used in the
134 // vendor variant of the C/C++ module.
135 Srcs []string
136
137 // list of source files that should not be used to
138 // build the vendor variant of the C/C++ module.
139 Exclude_srcs []string
Jiyong Parka622de82017-08-10 13:33:27 +0900140
141 // List of additional cflags that should be used to build the vendor
142 // variant of the C/C++ module.
143 Cflags []string
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700144 }
Jiyong Parkf9332f12018-02-01 00:54:12 +0900145 Recovery struct {
146 // list of source files that should only be used in the
147 // recovery variant of the C/C++ module.
148 Srcs []string
149
150 // list of source files that should not be used to
151 // build the recovery variant of the C/C++ module.
152 Exclude_srcs []string
153
154 // List of additional cflags that should be used to build the recovery
155 // variant of the C/C++ module.
156 Cflags []string
157 }
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700158 }
Colin Cross10d22312017-05-03 11:01:58 -0700159
Colin Cross38f794e2017-09-07 10:53:07 -0700160 Proto struct {
161 // Link statically against the protobuf runtime
Nan Zhang0007d812017-11-07 10:57:05 -0800162 Static *bool `android:"arch_variant"`
Colin Cross38f794e2017-09-07 10:53:07 -0700163 } `android:"arch_variant"`
164
Colin Cross10d22312017-05-03 11:01:58 -0700165 // Stores the original list of source files before being cleared by library reuse
166 OriginalSrcs []string `blueprint:"mutated"`
Pirama Arumuga Nainarfadb7b52017-12-19 23:08:09 -0800167
168 // Build and link with OpenMP
169 Openmp *bool `android:"arch_variant"`
Colin Cross4d9c2d12016-07-29 12:48:20 -0700170}
171
Colin Crossb916a382016-07-29 17:28:03 -0700172func NewBaseCompiler() *baseCompiler {
173 return &baseCompiler{}
174}
175
Colin Cross4d9c2d12016-07-29 12:48:20 -0700176type baseCompiler struct {
177 Properties BaseCompilerProperties
Colin Cross38f794e2017-09-07 10:53:07 -0700178 Proto android.ProtoProperties
Pirama Arumuga Nainarf231b192018-01-23 10:49:04 -0800179 cFlagsDeps android.Paths
Pirama Arumuga Nainar70ba5a32017-12-19 15:11:01 -0800180 pathDeps android.Paths
Fabien Sanglardd61f1f42017-01-10 16:21:22 -0800181 flags builderFlags
Colin Crossf18e1102017-11-16 14:33:08 -0800182
183 // Sources that were passed to the C/C++ compiler
184 srcs android.Paths
185
186 // Sources that were passed in the Android.bp file, including generated sources generated by
187 // other modules and filegroups. May include source files that have not yet been translated to
188 // C/C++ (.aidl, .proto, etc.)
189 srcsBeforeGen android.Paths
Colin Cross4d9c2d12016-07-29 12:48:20 -0700190}
191
192var _ compiler = (*baseCompiler)(nil)
193
Fabien Sanglardd61f1f42017-01-10 16:21:22 -0800194type CompiledInterface interface {
195 Srcs() android.Paths
196}
197
198func (compiler *baseCompiler) Srcs() android.Paths {
Nan Zhange42777a2018-03-27 16:19:42 -0700199 return append(android.Paths{}, compiler.srcs...)
Fabien Sanglardd61f1f42017-01-10 16:21:22 -0800200}
201
Colin Cross4d9c2d12016-07-29 12:48:20 -0700202func (compiler *baseCompiler) appendCflags(flags []string) {
203 compiler.Properties.Cflags = append(compiler.Properties.Cflags, flags...)
204}
205
206func (compiler *baseCompiler) appendAsflags(flags []string) {
207 compiler.Properties.Asflags = append(compiler.Properties.Asflags, flags...)
208}
209
Colin Cross42742b82016-08-01 13:20:05 -0700210func (compiler *baseCompiler) compilerProps() []interface{} {
Colin Cross0feb1692016-11-03 14:38:52 -0700211 return []interface{}{&compiler.Properties, &compiler.Proto}
Colin Cross4d9c2d12016-07-29 12:48:20 -0700212}
213
Colin Cross42742b82016-08-01 13:20:05 -0700214func (compiler *baseCompiler) compilerInit(ctx BaseModuleContext) {}
Colin Cross4d9c2d12016-07-29 12:48:20 -0700215
Colin Cross37047f12016-12-13 17:06:13 -0800216func (compiler *baseCompiler) compilerDeps(ctx DepsContext, deps Deps) Deps {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700217 deps.GeneratedSources = append(deps.GeneratedSources, compiler.Properties.Generated_sources...)
218 deps.GeneratedHeaders = append(deps.GeneratedHeaders, compiler.Properties.Generated_headers...)
219
Colin Cross068e0fe2016-12-13 15:23:47 -0800220 android.ExtractSourcesDeps(ctx, compiler.Properties.Srcs)
Nan Zhang27e284d2018-02-09 21:03:53 +0000221 android.ExtractSourcesDeps(ctx, compiler.Properties.Exclude_srcs)
Colin Cross068e0fe2016-12-13 15:23:47 -0800222
Dan Willemsene1a3ce32016-11-02 20:44:08 -0700223 if compiler.hasSrcExt(".proto") {
Nan Zhang0007d812017-11-07 10:57:05 -0800224 deps = protoDeps(ctx, deps, &compiler.Proto, Bool(compiler.Properties.Proto.Static))
Colin Cross0c461f12016-10-20 16:11:43 -0700225 }
226
Pirama Arumuga Nainarfadb7b52017-12-19 23:08:09 -0800227 if Bool(compiler.Properties.Openmp) {
228 deps.StaticLibs = append(deps.StaticLibs, "libomp")
229 }
230
Colin Cross4d9c2d12016-07-29 12:48:20 -0700231 return deps
232}
233
Chih-Hung Hsieh64a38dc2017-11-14 14:09:14 -0800234// Return true if the module is in the WarningAllowedProjects.
235func warningsAreAllowed(subdir string) bool {
236 subdir += "/"
237 for _, prefix := range config.WarningAllowedProjects {
238 if strings.HasPrefix(subdir, prefix) {
239 return true
240 }
241 }
242 return false
243}
244
245func addToModuleList(ctx ModuleContext, list string, module string) {
Pirama Arumuga Nainar28316d42018-01-29 09:18:45 -0800246 getNamedMapForConfig(ctx.Config(), list).Store(module, true)
Chih-Hung Hsieh64a38dc2017-11-14 14:09:14 -0800247}
248
Colin Cross4d9c2d12016-07-29 12:48:20 -0700249// Create a Flags struct that collects the compile flags from global values,
250// per-target values, module type values, and per-module Blueprints properties
Colin Crossf18e1102017-11-16 14:33:08 -0800251func (compiler *baseCompiler) compilerFlags(ctx ModuleContext, flags Flags, deps PathDeps) Flags {
Colin Crossb98c8b02016-07-29 13:44:28 -0700252 tc := ctx.toolchain()
Colin Cross4d9c2d12016-07-29 12:48:20 -0700253
Colin Crossf18e1102017-11-16 14:33:08 -0800254 compiler.srcsBeforeGen = ctx.ExpandSources(compiler.Properties.Srcs, compiler.Properties.Exclude_srcs)
255 compiler.srcsBeforeGen = append(compiler.srcsBeforeGen, deps.GeneratedSources...)
256
Colin Cross4d9c2d12016-07-29 12:48:20 -0700257 CheckBadCompilerFlags(ctx, "cflags", compiler.Properties.Cflags)
258 CheckBadCompilerFlags(ctx, "cppflags", compiler.Properties.Cppflags)
259 CheckBadCompilerFlags(ctx, "conlyflags", compiler.Properties.Conlyflags)
260 CheckBadCompilerFlags(ctx, "asflags", compiler.Properties.Asflags)
Jiyong Park4a2dcb52018-05-24 16:44:14 +0900261 CheckBadCompilerFlags(ctx, "vendor.cflags", compiler.Properties.Target.Vendor.Cflags)
262 CheckBadCompilerFlags(ctx, "recovery.cflags", compiler.Properties.Target.Recovery.Cflags)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700263
Colin Cross4b963f82016-09-29 14:06:02 -0700264 esc := proptools.NinjaAndShellEscape
265
266 flags.CFlags = append(flags.CFlags, esc(compiler.Properties.Cflags)...)
267 flags.CppFlags = append(flags.CppFlags, esc(compiler.Properties.Cppflags)...)
268 flags.ConlyFlags = append(flags.ConlyFlags, esc(compiler.Properties.Conlyflags)...)
269 flags.AsFlags = append(flags.AsFlags, esc(compiler.Properties.Asflags)...)
Colin Cross91e90042016-12-02 17:13:24 -0800270 flags.YasmFlags = append(flags.YasmFlags, esc(compiler.Properties.Asflags)...)
Colin Cross4b963f82016-09-29 14:06:02 -0700271 flags.YaccFlags = append(flags.YaccFlags, esc(compiler.Properties.Yaccflags)...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700272
273 // Include dir cflags
Colin Cross4d9c2d12016-07-29 12:48:20 -0700274 localIncludeDirs := android.PathsForModuleSrc(ctx, compiler.Properties.Local_include_dirs)
Dan Willemsen273af7f2016-11-03 15:53:42 -0700275 if len(localIncludeDirs) > 0 {
Colin Crossdad8c952017-04-26 14:55:27 -0700276 f := includeDirsToFlags(localIncludeDirs)
277 flags.GlobalFlags = append(flags.GlobalFlags, f)
278 flags.YasmFlags = append(flags.YasmFlags, f)
Dan Willemsen273af7f2016-11-03 15:53:42 -0700279 }
280 rootIncludeDirs := android.PathsForSource(ctx, compiler.Properties.Include_dirs)
281 if len(rootIncludeDirs) > 0 {
Colin Crossdad8c952017-04-26 14:55:27 -0700282 f := includeDirsToFlags(rootIncludeDirs)
283 flags.GlobalFlags = append(flags.GlobalFlags, f)
284 flags.YasmFlags = append(flags.YasmFlags, f)
Dan Willemsen273af7f2016-11-03 15:53:42 -0700285 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700286
Colin Cross87dd9632017-11-03 13:31:05 -0700287 flags.GlobalFlags = append(flags.GlobalFlags, "-I"+android.PathForModuleSrc(ctx).String())
288 flags.YasmFlags = append(flags.YasmFlags, "-I"+android.PathForModuleSrc(ctx).String())
Colin Crossc3199482017-03-30 15:03:04 -0700289
Colin Cross87dd9632017-11-03 13:31:05 -0700290 if !(ctx.useSdk() || ctx.useVndk()) || ctx.Host() {
291 flags.SystemIncludeFlags = append(flags.SystemIncludeFlags,
292 "${config.CommonGlobalIncludes}",
293 tc.IncludeFlags(),
294 "${config.CommonNativehelperInclude}")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700295 }
296
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700297 if ctx.useSdk() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700298 // The NDK headers are installed to a common sysroot. While a more
299 // typical Soong approach would be to only make the headers for the
300 // library you're using available, we're trying to emulate the NDK
301 // behavior here, and the NDK always has all the NDK headers available.
Colin Crossc3199482017-03-30 15:03:04 -0700302 flags.SystemIncludeFlags = append(flags.SystemIncludeFlags,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700303 "-isystem "+getCurrentIncludePath(ctx).String(),
Chih-Hung Hsieh1e7d1bf2018-03-15 18:44:57 -0700304 "-isystem "+getCurrentIncludePath(ctx).Join(ctx, config.NDKTriple(tc)).String())
Colin Cross4d9c2d12016-07-29 12:48:20 -0700305
306 // Traditionally this has come from android/api-level.h, but with the
307 // libc headers unified it must be set by the build system since we
308 // don't have per-API level copies of that header now.
Dan Albertebedf672016-11-08 15:06:22 -0800309 version := ctx.sdkVersion()
310 if version == "current" {
311 version = "__ANDROID_API_FUTURE__"
312 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700313 flags.GlobalFlags = append(flags.GlobalFlags,
Dan Albertebedf672016-11-08 15:06:22 -0800314 "-D__ANDROID_API__="+version)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700315
316 // Until the full NDK has been migrated to using ndk_headers, we still
317 // need to add the legacy sysroot includes to get the full set of
318 // headers.
319 legacyIncludes := fmt.Sprintf(
320 "prebuilts/ndk/current/platforms/android-%s/arch-%s/usr/include",
321 ctx.sdkVersion(), ctx.Arch().ArchType.String())
Colin Crossc3199482017-03-30 15:03:04 -0700322 flags.SystemIncludeFlags = append(flags.SystemIncludeFlags, "-isystem "+legacyIncludes)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700323 }
324
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700325 if ctx.useVndk() {
Justin Yun732aa6a2018-03-23 17:43:47 +0900326 // sdkVersion() returns VNDK version for vendor modules.
327 version := ctx.sdkVersion()
328 if version == "current" {
329 version = "__ANDROID_API_FUTURE__"
330 }
Dan Willemsenb916b802017-03-19 13:44:32 -0700331 flags.GlobalFlags = append(flags.GlobalFlags,
Justin Yun732aa6a2018-03-23 17:43:47 +0900332 "-D__ANDROID_API__="+version, "-D__ANDROID_VNDK__")
Dan Willemsenb916b802017-03-19 13:44:32 -0700333 }
334
Jiyong Parkd54aee52018-06-09 01:32:54 +0900335 if ctx.inRecovery() {
336 flags.GlobalFlags = append(flags.GlobalFlags, "-D__ANDROID_RECOVERY__")
337 }
338
Nan Zhang0007d812017-11-07 10:57:05 -0800339 instructionSet := String(compiler.Properties.Instruction_set)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700340 if flags.RequiredInstructionSet != "" {
341 instructionSet = flags.RequiredInstructionSet
342 }
Colin Crossb98c8b02016-07-29 13:44:28 -0700343 instructionSetFlags, err := tc.InstructionSetFlags(instructionSet)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700344 if flags.Clang {
Colin Crossb98c8b02016-07-29 13:44:28 -0700345 instructionSetFlags, err = tc.ClangInstructionSetFlags(instructionSet)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700346 }
347 if err != nil {
348 ctx.ModuleErrorf("%s", err)
349 }
350
351 CheckBadCompilerFlags(ctx, "release.cflags", compiler.Properties.Release.Cflags)
352
353 // TODO: debug
Colin Cross4b963f82016-09-29 14:06:02 -0700354 flags.CFlags = append(flags.CFlags, esc(compiler.Properties.Release.Cflags)...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700355
356 if flags.Clang {
357 CheckBadCompilerFlags(ctx, "clang_cflags", compiler.Properties.Clang_cflags)
358 CheckBadCompilerFlags(ctx, "clang_asflags", compiler.Properties.Clang_asflags)
359
Colin Crossb98c8b02016-07-29 13:44:28 -0700360 flags.CFlags = config.ClangFilterUnknownCflags(flags.CFlags)
Colin Cross4b963f82016-09-29 14:06:02 -0700361 flags.CFlags = append(flags.CFlags, esc(compiler.Properties.Clang_cflags)...)
362 flags.AsFlags = append(flags.AsFlags, esc(compiler.Properties.Clang_asflags)...)
Colin Crossb98c8b02016-07-29 13:44:28 -0700363 flags.CppFlags = config.ClangFilterUnknownCflags(flags.CppFlags)
364 flags.ConlyFlags = config.ClangFilterUnknownCflags(flags.ConlyFlags)
365 flags.LdFlags = config.ClangFilterUnknownCflags(flags.LdFlags)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700366
Colin Crossb98c8b02016-07-29 13:44:28 -0700367 target := "-target " + tc.ClangTriple()
Dan Willemsen300151b2017-03-13 12:40:30 -0700368 gccPrefix := "-B" + config.ToolPath(tc)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700369
370 flags.CFlags = append(flags.CFlags, target, gccPrefix)
371 flags.AsFlags = append(flags.AsFlags, target, gccPrefix)
372 flags.LdFlags = append(flags.LdFlags, target, gccPrefix)
373 }
374
Colin Crossb98c8b02016-07-29 13:44:28 -0700375 hod := "Host"
Colin Cross4d9c2d12016-07-29 12:48:20 -0700376 if ctx.Os().Class == android.Device {
Colin Crossb98c8b02016-07-29 13:44:28 -0700377 hod = "Device"
Colin Cross4d9c2d12016-07-29 12:48:20 -0700378 }
379
Colin Cross87dd9632017-11-03 13:31:05 -0700380 flags.GlobalFlags = append(flags.GlobalFlags, instructionSetFlags)
381 flags.ConlyFlags = append([]string{"${config.CommonGlobalConlyflags}"}, flags.ConlyFlags...)
Colin Cross26f14502017-11-06 13:59:48 -0800382 flags.CppFlags = append([]string{fmt.Sprintf("${config.%sGlobalCppflags}", hod)}, flags.CppFlags...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700383
Colin Cross87dd9632017-11-03 13:31:05 -0700384 if flags.Clang {
385 flags.AsFlags = append(flags.AsFlags, tc.ClangAsflags())
386 flags.CppFlags = append([]string{"${config.CommonClangGlobalCppflags}"}, flags.CppFlags...)
387 flags.GlobalFlags = append(flags.GlobalFlags,
388 tc.ClangCflags(),
389 "${config.CommonClangGlobalCflags}",
390 fmt.Sprintf("${config.%sClangGlobalCflags}", hod))
391 } else {
392 flags.CppFlags = append([]string{"${config.CommonGlobalCppflags}"}, flags.CppFlags...)
393 flags.GlobalFlags = append(flags.GlobalFlags,
394 tc.Cflags(),
395 "${config.CommonGlobalCflags}",
396 fmt.Sprintf("${config.%sGlobalCflags}", hod))
Colin Cross4d9c2d12016-07-29 12:48:20 -0700397 }
398
Yi Kongcc80f8d2018-06-06 14:42:44 -0700399 if flags.Clang {
400 if strings.HasPrefix(android.PathForModuleSrc(ctx).String(), "external/") {
401 flags.GlobalFlags = append([]string{"${config.ClangExternalCflags}"}, flags.GlobalFlags...)
402 }
403 }
404
Colin Cross87dd9632017-11-03 13:31:05 -0700405 if ctx.Device() {
406 if Bool(compiler.Properties.Rtti) {
407 flags.CppFlags = append(flags.CppFlags, "-frtti")
408 } else {
409 flags.CppFlags = append(flags.CppFlags, "-fno-rtti")
410 }
411 }
412
413 flags.AsFlags = append(flags.AsFlags, "-D__ASSEMBLY__")
414
415 if flags.Clang {
416 flags.CppFlags = append(flags.CppFlags, tc.ClangCppflags())
417 } else {
418 flags.CppFlags = append(flags.CppFlags, tc.Cppflags())
419 }
420
421 flags.YasmFlags = append(flags.YasmFlags, tc.YasmFlags())
422
Colin Cross4d9c2d12016-07-29 12:48:20 -0700423 if flags.Clang {
Colin Crossb98c8b02016-07-29 13:44:28 -0700424 flags.GlobalFlags = append(flags.GlobalFlags, tc.ToolchainClangCflags())
Colin Cross4d9c2d12016-07-29 12:48:20 -0700425 } else {
Colin Crossb98c8b02016-07-29 13:44:28 -0700426 flags.GlobalFlags = append(flags.GlobalFlags, tc.ToolchainCflags())
Colin Cross4d9c2d12016-07-29 12:48:20 -0700427 }
428
Elliott Hughes5789ca92018-02-16 17:15:19 -0800429 cStd := config.CStdVersion
430 if String(compiler.Properties.C_std) == "experimental" {
431 cStd = config.ExperimentalCStdVersion
432 } else if String(compiler.Properties.C_std) != "" {
433 cStd = String(compiler.Properties.C_std)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700434 }
435
Elliott Hughes5789ca92018-02-16 17:15:19 -0800436 cppStd := String(compiler.Properties.Cpp_std)
437 switch String(compiler.Properties.Cpp_std) {
438 case "":
439 cppStd = config.CppStdVersion
440 case "experimental":
441 cppStd = config.ExperimentalCppStdVersion
442 case "c++17", "gnu++17":
443 // Map c++17 and gnu++17 to their 1z equivalents, until 17 is finalized.
444 cppStd = strings.Replace(String(compiler.Properties.Cpp_std), "17", "1z", 1)
445 }
446
447 if !flags.Clang {
448 // GCC uses an invalid C++14 ABI (emits calls to
449 // __cxa_throw_bad_array_length, which is not a valid C++ RT ABI).
450 // http://b/25022512
451 // The host GCC doesn't support C++14 (and is deprecated, so likely
452 // never will).
453 // Build these modules with C++11.
454 cppStd = config.GccCppStdVersion
455 }
456
457 if compiler.Properties.Gnu_extensions != nil && *compiler.Properties.Gnu_extensions == false {
458 cStd = gnuToCReplacer.Replace(cStd)
459 cppStd = gnuToCReplacer.Replace(cppStd)
460 }
461
462 flags.ConlyFlags = append([]string{"-std=" + cStd}, flags.ConlyFlags...)
463 flags.CppFlags = append([]string{"-std=" + cppStd}, flags.CppFlags...)
464
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700465 if ctx.useVndk() {
Jiyong Parka622de82017-08-10 13:33:27 +0900466 flags.CFlags = append(flags.CFlags, esc(compiler.Properties.Target.Vendor.Cflags)...)
467 }
468
Jiyong Park4a2dcb52018-05-24 16:44:14 +0900469 if ctx.inRecovery() {
470 flags.CFlags = append(flags.CFlags, esc(compiler.Properties.Target.Recovery.Cflags)...)
471 }
472
Colin Cross4d9c2d12016-07-29 12:48:20 -0700473 // We can enforce some rules more strictly in the code we own. strict
474 // indicates if this is code that we can be stricter with. If we have
475 // rules that we want to apply to *our* code (but maybe can't for
476 // vendor/device specific things), we could extend this to be a ternary
477 // value.
478 strict := true
479 if strings.HasPrefix(android.PathForModuleSrc(ctx).String(), "external/") {
480 strict = false
481 }
482
483 // Can be used to make some annotations stricter for code we can fix
484 // (such as when we mark functions as deprecated).
485 if strict {
486 flags.CFlags = append(flags.CFlags, "-DANDROID_STRICT")
487 }
488
Dan Willemsene1a3ce32016-11-02 20:44:08 -0700489 if compiler.hasSrcExt(".proto") {
Colin Cross0c461f12016-10-20 16:11:43 -0700490 flags = protoFlags(ctx, flags, &compiler.Proto)
491 }
492
Dan Willemsene1a3ce32016-11-02 20:44:08 -0700493 if compiler.hasSrcExt(".y") || compiler.hasSrcExt(".yy") {
494 flags.GlobalFlags = append(flags.GlobalFlags,
495 "-I"+android.PathForModuleGen(ctx, "yacc", ctx.ModuleDir()).String())
496 }
497
Dan Willemsen4f1c3d42017-09-09 01:15:26 -0700498 if compiler.hasSrcExt(".mc") {
499 flags.GlobalFlags = append(flags.GlobalFlags,
500 "-I"+android.PathForModuleGen(ctx, "windmc", ctx.ModuleDir()).String())
501 }
502
Dan Willemsene1240db2016-11-03 14:28:51 -0700503 if compiler.hasSrcExt(".aidl") {
504 if len(compiler.Properties.Aidl.Local_include_dirs) > 0 {
505 localAidlIncludeDirs := android.PathsForModuleSrc(ctx, compiler.Properties.Aidl.Local_include_dirs)
506 flags.aidlFlags = append(flags.aidlFlags, includeDirsToFlags(localAidlIncludeDirs))
507 }
508 if len(compiler.Properties.Aidl.Include_dirs) > 0 {
509 rootAidlIncludeDirs := android.PathsForSource(ctx, compiler.Properties.Aidl.Include_dirs)
510 flags.aidlFlags = append(flags.aidlFlags, includeDirsToFlags(rootAidlIncludeDirs))
511 }
512
Martijn Coeneneab15642018-03-09 09:29:59 +0100513 if Bool(compiler.Properties.Aidl.Generate_traces) {
514 flags.aidlFlags = append(flags.aidlFlags, "-t")
515 }
516
Dan Willemsene1240db2016-11-03 14:28:51 -0700517 flags.GlobalFlags = append(flags.GlobalFlags,
518 "-I"+android.PathForModuleGen(ctx, "aidl").String())
519 }
520
Colin Cross2a252be2017-05-01 17:37:24 -0700521 if compiler.hasSrcExt(".rs") || compiler.hasSrcExt(".fs") {
522 flags = rsFlags(ctx, flags, &compiler.Properties)
523 }
524
Chih-Hung Hsieh64a38dc2017-11-14 14:09:14 -0800525 if len(compiler.Properties.Srcs) > 0 {
526 module := ctx.ModuleDir() + "/Android.bp:" + ctx.ModuleName()
527 if inList("-Wno-error", flags.CFlags) || inList("-Wno-error", flags.CppFlags) {
528 addToModuleList(ctx, modulesUsingWnoError, module)
529 } else if !inList("-Werror", flags.CFlags) && !inList("-Werror", flags.CppFlags) {
530 if warningsAreAllowed(ctx.ModuleDir()) {
531 addToModuleList(ctx, modulesAddedWall, module)
532 flags.CFlags = append([]string{"-Wall"}, flags.CFlags...)
533 } else {
Chih-Hung Hsieh64a38dc2017-11-14 14:09:14 -0800534 flags.CFlags = append([]string{"-Wall", "-Werror"}, flags.CFlags...)
535 }
536 }
537 }
538
Pirama Arumuga Nainarfadb7b52017-12-19 23:08:09 -0800539 if Bool(compiler.Properties.Openmp) {
540 flags.CFlags = append(flags.CFlags, "-fopenmp")
541 }
542
Colin Cross4d9c2d12016-07-29 12:48:20 -0700543 return flags
544}
545
Dan Willemsene1a3ce32016-11-02 20:44:08 -0700546func (compiler *baseCompiler) hasSrcExt(ext string) bool {
Colin Crossf18e1102017-11-16 14:33:08 -0800547 for _, src := range compiler.srcsBeforeGen {
548 if src.Ext() == ext {
549 return true
550 }
551 }
Colin Cross0c461f12016-10-20 16:11:43 -0700552 for _, src := range compiler.Properties.Srcs {
Dan Willemsene1a3ce32016-11-02 20:44:08 -0700553 if filepath.Ext(src) == ext {
Colin Cross0c461f12016-10-20 16:11:43 -0700554 return true
555 }
556 }
Colin Cross10d22312017-05-03 11:01:58 -0700557 for _, src := range compiler.Properties.OriginalSrcs {
558 if filepath.Ext(src) == ext {
559 return true
560 }
561 }
Colin Cross0c461f12016-10-20 16:11:43 -0700562
563 return false
564}
565
Colin Cross948f0cb2016-10-17 14:24:56 -0700566var gnuToCReplacer = strings.NewReplacer("gnu", "c")
567
Colin Cross4d9c2d12016-07-29 12:48:20 -0700568func ndkPathDeps(ctx ModuleContext) android.Paths {
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700569 if ctx.useSdk() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700570 // The NDK sysroot timestamp file depends on all the NDK sysroot files
571 // (headers and libraries).
Dan Albert6ab43d82017-12-13 15:05:04 -0800572 return android.Paths{getNdkBaseTimestampFile(ctx)}
Colin Cross4d9c2d12016-07-29 12:48:20 -0700573 }
574 return nil
575}
576
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700577func (compiler *baseCompiler) compile(ctx ModuleContext, flags Flags, deps PathDeps) Objects {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700578 pathDeps := deps.GeneratedHeaders
579 pathDeps = append(pathDeps, ndkPathDeps(ctx)...)
Colin Cross2f336352016-10-26 10:03:47 -0700580
Colin Cross2f336352016-10-26 10:03:47 -0700581 buildFlags := flagsToBuilderFlags(flags)
582
Colin Crossf18e1102017-11-16 14:33:08 -0800583 srcs := append(android.Paths(nil), compiler.srcsBeforeGen...)
584
Colin Cross2f336352016-10-26 10:03:47 -0700585 srcs, genDeps := genSources(ctx, srcs, buildFlags)
Pirama Arumuga Nainarf231b192018-01-23 10:49:04 -0800586 pathDeps = append(pathDeps, genDeps...)
Colin Cross2f336352016-10-26 10:03:47 -0700587
Pirama Arumuga Nainar70ba5a32017-12-19 15:11:01 -0800588 compiler.pathDeps = pathDeps
Pirama Arumuga Nainarf231b192018-01-23 10:49:04 -0800589 compiler.cFlagsDeps = flags.CFlagsDeps
Colin Cross2f336352016-10-26 10:03:47 -0700590
Fabien Sanglardd61f1f42017-01-10 16:21:22 -0800591 // Save src, buildFlags and context
592 compiler.srcs = srcs
593
Colin Cross4d9c2d12016-07-29 12:48:20 -0700594 // Compile files listed in c.Properties.Srcs into objects
Pirama Arumuga Nainarf231b192018-01-23 10:49:04 -0800595 objs := compileObjs(ctx, buildFlags, "", srcs, pathDeps, compiler.cFlagsDeps)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700596
597 if ctx.Failed() {
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700598 return Objects{}
Colin Cross4d9c2d12016-07-29 12:48:20 -0700599 }
600
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700601 return objs
Colin Cross4d9c2d12016-07-29 12:48:20 -0700602}
603
604// Compile a list of source files into objects a specified subdirectory
Colin Cross2f336352016-10-26 10:03:47 -0700605func compileObjs(ctx android.ModuleContext, flags builderFlags,
Pirama Arumuga Nainarf231b192018-01-23 10:49:04 -0800606 subdir string, srcFiles, pathDeps android.Paths, cFlagsDeps android.Paths) Objects {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700607
Pirama Arumuga Nainarf231b192018-01-23 10:49:04 -0800608 return TransformSourceToObj(ctx, subdir, srcFiles, flags, pathDeps, cFlagsDeps)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700609}