blob: 7e8e8b8ecab21de349081f41c937b70512a5c176 [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
109 }
110
Colin Cross2a252be2017-05-01 17:37:24 -0700111 Renderscript struct {
112 // list of directories that will be added to the llvm-rs-cc include paths
113 Include_dirs []string
114
115 // list of flags that will be passed to llvm-rs-cc
116 Flags []string
117
118 // Renderscript API level to target
119 Target_api *string
120 }
121
Colin Cross4d9c2d12016-07-29 12:48:20 -0700122 Debug, Release struct {
123 // list of module-specific flags that will be used for C and C++ compiles in debug or
124 // release builds
125 Cflags []string `android:"arch_variant"`
126 } `android:"arch_variant"`
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700127
128 Target struct {
129 Vendor struct {
130 // list of source files that should only be used in the
131 // vendor variant of the C/C++ module.
132 Srcs []string
133
134 // list of source files that should not be used to
135 // build the vendor variant of the C/C++ module.
136 Exclude_srcs []string
Jiyong Parka622de82017-08-10 13:33:27 +0900137
138 // List of additional cflags that should be used to build the vendor
139 // variant of the C/C++ module.
140 Cflags []string
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700141 }
142 }
Colin Cross10d22312017-05-03 11:01:58 -0700143
Colin Cross38f794e2017-09-07 10:53:07 -0700144 Proto struct {
145 // Link statically against the protobuf runtime
Nan Zhang0007d812017-11-07 10:57:05 -0800146 Static *bool `android:"arch_variant"`
Colin Cross38f794e2017-09-07 10:53:07 -0700147 } `android:"arch_variant"`
148
Colin Cross10d22312017-05-03 11:01:58 -0700149 // Stores the original list of source files before being cleared by library reuse
150 OriginalSrcs []string `blueprint:"mutated"`
Pirama Arumuga Nainarfadb7b52017-12-19 23:08:09 -0800151
152 // Build and link with OpenMP
153 Openmp *bool `android:"arch_variant"`
Colin Cross4d9c2d12016-07-29 12:48:20 -0700154}
155
Colin Crossb916a382016-07-29 17:28:03 -0700156func NewBaseCompiler() *baseCompiler {
157 return &baseCompiler{}
158}
159
Colin Cross4d9c2d12016-07-29 12:48:20 -0700160type baseCompiler struct {
161 Properties BaseCompilerProperties
Colin Cross38f794e2017-09-07 10:53:07 -0700162 Proto android.ProtoProperties
Pirama Arumuga Nainar70ba5a32017-12-19 15:11:01 -0800163 genDeps android.Paths
164 pathDeps android.Paths
Fabien Sanglardd61f1f42017-01-10 16:21:22 -0800165 flags builderFlags
Colin Crossf18e1102017-11-16 14:33:08 -0800166
167 // Sources that were passed to the C/C++ compiler
168 srcs android.Paths
169
170 // Sources that were passed in the Android.bp file, including generated sources generated by
171 // other modules and filegroups. May include source files that have not yet been translated to
172 // C/C++ (.aidl, .proto, etc.)
173 srcsBeforeGen android.Paths
Colin Cross4d9c2d12016-07-29 12:48:20 -0700174}
175
176var _ compiler = (*baseCompiler)(nil)
177
Fabien Sanglardd61f1f42017-01-10 16:21:22 -0800178type CompiledInterface interface {
179 Srcs() android.Paths
180}
181
182func (compiler *baseCompiler) Srcs() android.Paths {
183 return compiler.srcs
184}
185
Colin Cross4d9c2d12016-07-29 12:48:20 -0700186func (compiler *baseCompiler) appendCflags(flags []string) {
187 compiler.Properties.Cflags = append(compiler.Properties.Cflags, flags...)
188}
189
190func (compiler *baseCompiler) appendAsflags(flags []string) {
191 compiler.Properties.Asflags = append(compiler.Properties.Asflags, flags...)
192}
193
Colin Cross42742b82016-08-01 13:20:05 -0700194func (compiler *baseCompiler) compilerProps() []interface{} {
Colin Cross0feb1692016-11-03 14:38:52 -0700195 return []interface{}{&compiler.Properties, &compiler.Proto}
Colin Cross4d9c2d12016-07-29 12:48:20 -0700196}
197
Colin Cross42742b82016-08-01 13:20:05 -0700198func (compiler *baseCompiler) compilerInit(ctx BaseModuleContext) {}
Colin Cross4d9c2d12016-07-29 12:48:20 -0700199
Colin Cross37047f12016-12-13 17:06:13 -0800200func (compiler *baseCompiler) compilerDeps(ctx DepsContext, deps Deps) Deps {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700201 deps.GeneratedSources = append(deps.GeneratedSources, compiler.Properties.Generated_sources...)
202 deps.GeneratedHeaders = append(deps.GeneratedHeaders, compiler.Properties.Generated_headers...)
203
Colin Cross068e0fe2016-12-13 15:23:47 -0800204 android.ExtractSourcesDeps(ctx, compiler.Properties.Srcs)
205
Dan Willemsene1a3ce32016-11-02 20:44:08 -0700206 if compiler.hasSrcExt(".proto") {
Nan Zhang0007d812017-11-07 10:57:05 -0800207 deps = protoDeps(ctx, deps, &compiler.Proto, Bool(compiler.Properties.Proto.Static))
Colin Cross0c461f12016-10-20 16:11:43 -0700208 }
209
Pirama Arumuga Nainarfadb7b52017-12-19 23:08:09 -0800210 if Bool(compiler.Properties.Openmp) {
211 deps.StaticLibs = append(deps.StaticLibs, "libomp")
212 }
213
Colin Cross4d9c2d12016-07-29 12:48:20 -0700214 return deps
215}
216
Chih-Hung Hsieh64a38dc2017-11-14 14:09:14 -0800217// Return true if the module is in the WarningAllowedProjects.
218func warningsAreAllowed(subdir string) bool {
219 subdir += "/"
220 for _, prefix := range config.WarningAllowedProjects {
221 if strings.HasPrefix(subdir, prefix) {
222 return true
223 }
224 }
225 return false
226}
227
228func addToModuleList(ctx ModuleContext, list string, module string) {
Colin Cross6510f912017-11-29 00:27:14 -0800229 getWallWerrorMap(ctx.Config(), list).Store(module, true)
Chih-Hung Hsieh64a38dc2017-11-14 14:09:14 -0800230}
231
Colin Cross4d9c2d12016-07-29 12:48:20 -0700232// Create a Flags struct that collects the compile flags from global values,
233// per-target values, module type values, and per-module Blueprints properties
Colin Crossf18e1102017-11-16 14:33:08 -0800234func (compiler *baseCompiler) compilerFlags(ctx ModuleContext, flags Flags, deps PathDeps) Flags {
Colin Crossb98c8b02016-07-29 13:44:28 -0700235 tc := ctx.toolchain()
Colin Cross4d9c2d12016-07-29 12:48:20 -0700236
Colin Crossf18e1102017-11-16 14:33:08 -0800237 compiler.srcsBeforeGen = ctx.ExpandSources(compiler.Properties.Srcs, compiler.Properties.Exclude_srcs)
238 compiler.srcsBeforeGen = append(compiler.srcsBeforeGen, deps.GeneratedSources...)
239
Colin Cross4d9c2d12016-07-29 12:48:20 -0700240 CheckBadCompilerFlags(ctx, "cflags", compiler.Properties.Cflags)
241 CheckBadCompilerFlags(ctx, "cppflags", compiler.Properties.Cppflags)
242 CheckBadCompilerFlags(ctx, "conlyflags", compiler.Properties.Conlyflags)
243 CheckBadCompilerFlags(ctx, "asflags", compiler.Properties.Asflags)
244
Colin Cross4b963f82016-09-29 14:06:02 -0700245 esc := proptools.NinjaAndShellEscape
246
247 flags.CFlags = append(flags.CFlags, esc(compiler.Properties.Cflags)...)
248 flags.CppFlags = append(flags.CppFlags, esc(compiler.Properties.Cppflags)...)
249 flags.ConlyFlags = append(flags.ConlyFlags, esc(compiler.Properties.Conlyflags)...)
250 flags.AsFlags = append(flags.AsFlags, esc(compiler.Properties.Asflags)...)
Colin Cross91e90042016-12-02 17:13:24 -0800251 flags.YasmFlags = append(flags.YasmFlags, esc(compiler.Properties.Asflags)...)
Colin Cross4b963f82016-09-29 14:06:02 -0700252 flags.YaccFlags = append(flags.YaccFlags, esc(compiler.Properties.Yaccflags)...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700253
254 // Include dir cflags
Colin Cross4d9c2d12016-07-29 12:48:20 -0700255 localIncludeDirs := android.PathsForModuleSrc(ctx, compiler.Properties.Local_include_dirs)
Dan Willemsen273af7f2016-11-03 15:53:42 -0700256 if len(localIncludeDirs) > 0 {
Colin Crossdad8c952017-04-26 14:55:27 -0700257 f := includeDirsToFlags(localIncludeDirs)
258 flags.GlobalFlags = append(flags.GlobalFlags, f)
259 flags.YasmFlags = append(flags.YasmFlags, f)
Dan Willemsen273af7f2016-11-03 15:53:42 -0700260 }
261 rootIncludeDirs := android.PathsForSource(ctx, compiler.Properties.Include_dirs)
262 if len(rootIncludeDirs) > 0 {
Colin Crossdad8c952017-04-26 14:55:27 -0700263 f := includeDirsToFlags(rootIncludeDirs)
264 flags.GlobalFlags = append(flags.GlobalFlags, f)
265 flags.YasmFlags = append(flags.YasmFlags, f)
Dan Willemsen273af7f2016-11-03 15:53:42 -0700266 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700267
Colin Cross87dd9632017-11-03 13:31:05 -0700268 flags.GlobalFlags = append(flags.GlobalFlags, "-I"+android.PathForModuleSrc(ctx).String())
269 flags.YasmFlags = append(flags.YasmFlags, "-I"+android.PathForModuleSrc(ctx).String())
Colin Crossc3199482017-03-30 15:03:04 -0700270
Colin Cross87dd9632017-11-03 13:31:05 -0700271 if !(ctx.useSdk() || ctx.useVndk()) || ctx.Host() {
272 flags.SystemIncludeFlags = append(flags.SystemIncludeFlags,
273 "${config.CommonGlobalIncludes}",
274 tc.IncludeFlags(),
275 "${config.CommonNativehelperInclude}")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700276 }
277
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700278 if ctx.useSdk() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700279 // The NDK headers are installed to a common sysroot. While a more
280 // typical Soong approach would be to only make the headers for the
281 // library you're using available, we're trying to emulate the NDK
282 // behavior here, and the NDK always has all the NDK headers available.
Colin Crossc3199482017-03-30 15:03:04 -0700283 flags.SystemIncludeFlags = append(flags.SystemIncludeFlags,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700284 "-isystem "+getCurrentIncludePath(ctx).String(),
Colin Crossb98c8b02016-07-29 13:44:28 -0700285 "-isystem "+getCurrentIncludePath(ctx).Join(ctx, tc.ClangTriple()).String())
Colin Cross4d9c2d12016-07-29 12:48:20 -0700286
287 // Traditionally this has come from android/api-level.h, but with the
288 // libc headers unified it must be set by the build system since we
289 // don't have per-API level copies of that header now.
Dan Albertebedf672016-11-08 15:06:22 -0800290 version := ctx.sdkVersion()
291 if version == "current" {
292 version = "__ANDROID_API_FUTURE__"
293 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700294 flags.GlobalFlags = append(flags.GlobalFlags,
Dan Albertebedf672016-11-08 15:06:22 -0800295 "-D__ANDROID_API__="+version)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700296
297 // Until the full NDK has been migrated to using ndk_headers, we still
298 // need to add the legacy sysroot includes to get the full set of
299 // headers.
300 legacyIncludes := fmt.Sprintf(
301 "prebuilts/ndk/current/platforms/android-%s/arch-%s/usr/include",
302 ctx.sdkVersion(), ctx.Arch().ArchType.String())
Colin Crossc3199482017-03-30 15:03:04 -0700303 flags.SystemIncludeFlags = append(flags.SystemIncludeFlags, "-isystem "+legacyIncludes)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700304 }
305
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700306 if ctx.useVndk() {
Dan Willemsenb916b802017-03-19 13:44:32 -0700307 flags.GlobalFlags = append(flags.GlobalFlags,
308 "-D__ANDROID_API__=__ANDROID_API_FUTURE__", "-D__ANDROID_VNDK__")
309 }
310
Nan Zhang0007d812017-11-07 10:57:05 -0800311 instructionSet := String(compiler.Properties.Instruction_set)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700312 if flags.RequiredInstructionSet != "" {
313 instructionSet = flags.RequiredInstructionSet
314 }
Colin Crossb98c8b02016-07-29 13:44:28 -0700315 instructionSetFlags, err := tc.InstructionSetFlags(instructionSet)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700316 if flags.Clang {
Colin Crossb98c8b02016-07-29 13:44:28 -0700317 instructionSetFlags, err = tc.ClangInstructionSetFlags(instructionSet)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700318 }
319 if err != nil {
320 ctx.ModuleErrorf("%s", err)
321 }
322
323 CheckBadCompilerFlags(ctx, "release.cflags", compiler.Properties.Release.Cflags)
324
325 // TODO: debug
Colin Cross4b963f82016-09-29 14:06:02 -0700326 flags.CFlags = append(flags.CFlags, esc(compiler.Properties.Release.Cflags)...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700327
328 if flags.Clang {
329 CheckBadCompilerFlags(ctx, "clang_cflags", compiler.Properties.Clang_cflags)
330 CheckBadCompilerFlags(ctx, "clang_asflags", compiler.Properties.Clang_asflags)
331
Colin Crossb98c8b02016-07-29 13:44:28 -0700332 flags.CFlags = config.ClangFilterUnknownCflags(flags.CFlags)
Colin Cross4b963f82016-09-29 14:06:02 -0700333 flags.CFlags = append(flags.CFlags, esc(compiler.Properties.Clang_cflags)...)
334 flags.AsFlags = append(flags.AsFlags, esc(compiler.Properties.Clang_asflags)...)
Colin Crossb98c8b02016-07-29 13:44:28 -0700335 flags.CppFlags = config.ClangFilterUnknownCflags(flags.CppFlags)
336 flags.ConlyFlags = config.ClangFilterUnknownCflags(flags.ConlyFlags)
337 flags.LdFlags = config.ClangFilterUnknownCflags(flags.LdFlags)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700338
Colin Crossb98c8b02016-07-29 13:44:28 -0700339 target := "-target " + tc.ClangTriple()
Dan Willemsen300151b2017-03-13 12:40:30 -0700340 gccPrefix := "-B" + config.ToolPath(tc)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700341
342 flags.CFlags = append(flags.CFlags, target, gccPrefix)
343 flags.AsFlags = append(flags.AsFlags, target, gccPrefix)
344 flags.LdFlags = append(flags.LdFlags, target, gccPrefix)
345 }
346
Colin Crossb98c8b02016-07-29 13:44:28 -0700347 hod := "Host"
Colin Cross4d9c2d12016-07-29 12:48:20 -0700348 if ctx.Os().Class == android.Device {
Colin Crossb98c8b02016-07-29 13:44:28 -0700349 hod = "Device"
Colin Cross4d9c2d12016-07-29 12:48:20 -0700350 }
351
Colin Cross87dd9632017-11-03 13:31:05 -0700352 flags.GlobalFlags = append(flags.GlobalFlags, instructionSetFlags)
353 flags.ConlyFlags = append([]string{"${config.CommonGlobalConlyflags}"}, flags.ConlyFlags...)
Colin Cross26f14502017-11-06 13:59:48 -0800354 flags.CppFlags = append([]string{fmt.Sprintf("${config.%sGlobalCppflags}", hod)}, flags.CppFlags...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700355
Colin Cross87dd9632017-11-03 13:31:05 -0700356 if flags.Clang {
357 flags.AsFlags = append(flags.AsFlags, tc.ClangAsflags())
358 flags.CppFlags = append([]string{"${config.CommonClangGlobalCppflags}"}, flags.CppFlags...)
359 flags.GlobalFlags = append(flags.GlobalFlags,
360 tc.ClangCflags(),
361 "${config.CommonClangGlobalCflags}",
362 fmt.Sprintf("${config.%sClangGlobalCflags}", hod))
363 } else {
364 flags.CppFlags = append([]string{"${config.CommonGlobalCppflags}"}, flags.CppFlags...)
365 flags.GlobalFlags = append(flags.GlobalFlags,
366 tc.Cflags(),
367 "${config.CommonGlobalCflags}",
368 fmt.Sprintf("${config.%sGlobalCflags}", hod))
Colin Cross4d9c2d12016-07-29 12:48:20 -0700369 }
370
Colin Cross6510f912017-11-29 00:27:14 -0800371 if Bool(ctx.Config().ProductVariables.Brillo) {
Colin Cross87dd9632017-11-03 13:31:05 -0700372 flags.GlobalFlags = append(flags.GlobalFlags, "-D__BRILLO__")
373 }
374
375 if ctx.Device() {
376 if Bool(compiler.Properties.Rtti) {
377 flags.CppFlags = append(flags.CppFlags, "-frtti")
378 } else {
379 flags.CppFlags = append(flags.CppFlags, "-fno-rtti")
380 }
381 }
382
383 flags.AsFlags = append(flags.AsFlags, "-D__ASSEMBLY__")
384
385 if flags.Clang {
386 flags.CppFlags = append(flags.CppFlags, tc.ClangCppflags())
387 } else {
388 flags.CppFlags = append(flags.CppFlags, tc.Cppflags())
389 }
390
391 flags.YasmFlags = append(flags.YasmFlags, tc.YasmFlags())
392
Colin Cross4d9c2d12016-07-29 12:48:20 -0700393 if flags.Clang {
Colin Crossb98c8b02016-07-29 13:44:28 -0700394 flags.GlobalFlags = append(flags.GlobalFlags, tc.ToolchainClangCflags())
Colin Cross4d9c2d12016-07-29 12:48:20 -0700395 } else {
Colin Crossb98c8b02016-07-29 13:44:28 -0700396 flags.GlobalFlags = append(flags.GlobalFlags, tc.ToolchainCflags())
Colin Cross4d9c2d12016-07-29 12:48:20 -0700397 }
398
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700399 if !ctx.useSdk() {
Colin Cross6f6a4282016-10-17 14:19:06 -0700400 cStd := config.CStdVersion
Nan Zhang0007d812017-11-07 10:57:05 -0800401 if String(compiler.Properties.C_std) == "experimental" {
Dan Albert043833c2017-02-03 16:13:38 -0800402 cStd = config.ExperimentalCStdVersion
Nan Zhang0007d812017-11-07 10:57:05 -0800403 } else if String(compiler.Properties.C_std) != "" {
404 cStd = String(compiler.Properties.C_std)
Dan Albert043833c2017-02-03 16:13:38 -0800405 }
406
Nan Zhang0007d812017-11-07 10:57:05 -0800407 cppStd := String(compiler.Properties.Cpp_std)
408 switch String(compiler.Properties.Cpp_std) {
Josh Gaof5720172017-06-23 14:56:50 -0700409 case "":
410 cppStd = config.CppStdVersion
411 case "experimental":
Dan Albert043833c2017-02-03 16:13:38 -0800412 cppStd = config.ExperimentalCppStdVersion
Josh Gaof5720172017-06-23 14:56:50 -0700413 case "c++17", "gnu++17":
414 // Map c++17 and gnu++17 to their 1z equivalents, until 17 is finalized.
Nan Zhang0007d812017-11-07 10:57:05 -0800415 cppStd = strings.Replace(String(compiler.Properties.Cpp_std), "17", "1z", 1)
Dan Albert043833c2017-02-03 16:13:38 -0800416 }
Colin Cross6f6a4282016-10-17 14:19:06 -0700417
418 if !flags.Clang {
419 // GCC uses an invalid C++14 ABI (emits calls to
420 // __cxa_throw_bad_array_length, which is not a valid C++ RT ABI).
421 // http://b/25022512
422 cppStd = config.GccCppStdVersion
423 } else if ctx.Host() && !flags.Clang {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700424 // The host GCC doesn't support C++14 (and is deprecated, so likely
425 // never will). Build these modules with C++11.
Colin Cross6f6a4282016-10-17 14:19:06 -0700426 cppStd = config.GccCppStdVersion
Colin Cross4d9c2d12016-07-29 12:48:20 -0700427 }
Colin Cross6f6a4282016-10-17 14:19:06 -0700428
Colin Cross948f0cb2016-10-17 14:24:56 -0700429 if compiler.Properties.Gnu_extensions != nil && *compiler.Properties.Gnu_extensions == false {
430 cStd = gnuToCReplacer.Replace(cStd)
431 cppStd = gnuToCReplacer.Replace(cppStd)
432 }
433
Colin Cross6f6a4282016-10-17 14:19:06 -0700434 flags.ConlyFlags = append([]string{"-std=" + cStd}, flags.ConlyFlags...)
435 flags.CppFlags = append([]string{"-std=" + cppStd}, flags.CppFlags...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700436 }
437
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700438 if ctx.useVndk() {
Jiyong Parka622de82017-08-10 13:33:27 +0900439 flags.CFlags = append(flags.CFlags, esc(compiler.Properties.Target.Vendor.Cflags)...)
440 }
441
Colin Cross4d9c2d12016-07-29 12:48:20 -0700442 // We can enforce some rules more strictly in the code we own. strict
443 // indicates if this is code that we can be stricter with. If we have
444 // rules that we want to apply to *our* code (but maybe can't for
445 // vendor/device specific things), we could extend this to be a ternary
446 // value.
447 strict := true
448 if strings.HasPrefix(android.PathForModuleSrc(ctx).String(), "external/") {
449 strict = false
450 }
451
452 // Can be used to make some annotations stricter for code we can fix
453 // (such as when we mark functions as deprecated).
454 if strict {
455 flags.CFlags = append(flags.CFlags, "-DANDROID_STRICT")
456 }
457
Dan Willemsene1a3ce32016-11-02 20:44:08 -0700458 if compiler.hasSrcExt(".proto") {
Colin Cross0c461f12016-10-20 16:11:43 -0700459 flags = protoFlags(ctx, flags, &compiler.Proto)
460 }
461
Dan Willemsene1a3ce32016-11-02 20:44:08 -0700462 if compiler.hasSrcExt(".y") || compiler.hasSrcExt(".yy") {
463 flags.GlobalFlags = append(flags.GlobalFlags,
464 "-I"+android.PathForModuleGen(ctx, "yacc", ctx.ModuleDir()).String())
465 }
466
Dan Willemsen4f1c3d42017-09-09 01:15:26 -0700467 if compiler.hasSrcExt(".mc") {
468 flags.GlobalFlags = append(flags.GlobalFlags,
469 "-I"+android.PathForModuleGen(ctx, "windmc", ctx.ModuleDir()).String())
470 }
471
Dan Willemsene1240db2016-11-03 14:28:51 -0700472 if compiler.hasSrcExt(".aidl") {
473 if len(compiler.Properties.Aidl.Local_include_dirs) > 0 {
474 localAidlIncludeDirs := android.PathsForModuleSrc(ctx, compiler.Properties.Aidl.Local_include_dirs)
475 flags.aidlFlags = append(flags.aidlFlags, includeDirsToFlags(localAidlIncludeDirs))
476 }
477 if len(compiler.Properties.Aidl.Include_dirs) > 0 {
478 rootAidlIncludeDirs := android.PathsForSource(ctx, compiler.Properties.Aidl.Include_dirs)
479 flags.aidlFlags = append(flags.aidlFlags, includeDirsToFlags(rootAidlIncludeDirs))
480 }
481
482 flags.GlobalFlags = append(flags.GlobalFlags,
483 "-I"+android.PathForModuleGen(ctx, "aidl").String())
484 }
485
Colin Cross2a252be2017-05-01 17:37:24 -0700486 if compiler.hasSrcExt(".rs") || compiler.hasSrcExt(".fs") {
487 flags = rsFlags(ctx, flags, &compiler.Properties)
488 }
489
Chih-Hung Hsieh64a38dc2017-11-14 14:09:14 -0800490 if len(compiler.Properties.Srcs) > 0 {
491 module := ctx.ModuleDir() + "/Android.bp:" + ctx.ModuleName()
492 if inList("-Wno-error", flags.CFlags) || inList("-Wno-error", flags.CppFlags) {
493 addToModuleList(ctx, modulesUsingWnoError, module)
494 } else if !inList("-Werror", flags.CFlags) && !inList("-Werror", flags.CppFlags) {
495 if warningsAreAllowed(ctx.ModuleDir()) {
496 addToModuleList(ctx, modulesAddedWall, module)
497 flags.CFlags = append([]string{"-Wall"}, flags.CFlags...)
498 } else {
Chih-Hung Hsieh64a38dc2017-11-14 14:09:14 -0800499 flags.CFlags = append([]string{"-Wall", "-Werror"}, flags.CFlags...)
500 }
501 }
502 }
503
Pirama Arumuga Nainarfadb7b52017-12-19 23:08:09 -0800504 if Bool(compiler.Properties.Openmp) {
505 flags.CFlags = append(flags.CFlags, "-fopenmp")
506 }
507
Colin Cross4d9c2d12016-07-29 12:48:20 -0700508 return flags
509}
510
Dan Willemsene1a3ce32016-11-02 20:44:08 -0700511func (compiler *baseCompiler) hasSrcExt(ext string) bool {
Colin Crossf18e1102017-11-16 14:33:08 -0800512 for _, src := range compiler.srcsBeforeGen {
513 if src.Ext() == ext {
514 return true
515 }
516 }
Colin Cross0c461f12016-10-20 16:11:43 -0700517 for _, src := range compiler.Properties.Srcs {
Dan Willemsene1a3ce32016-11-02 20:44:08 -0700518 if filepath.Ext(src) == ext {
Colin Cross0c461f12016-10-20 16:11:43 -0700519 return true
520 }
521 }
Colin Cross10d22312017-05-03 11:01:58 -0700522 for _, src := range compiler.Properties.OriginalSrcs {
523 if filepath.Ext(src) == ext {
524 return true
525 }
526 }
Colin Cross0c461f12016-10-20 16:11:43 -0700527
528 return false
529}
530
Colin Cross948f0cb2016-10-17 14:24:56 -0700531var gnuToCReplacer = strings.NewReplacer("gnu", "c")
532
Colin Cross4d9c2d12016-07-29 12:48:20 -0700533func ndkPathDeps(ctx ModuleContext) android.Paths {
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700534 if ctx.useSdk() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700535 // The NDK sysroot timestamp file depends on all the NDK sysroot files
536 // (headers and libraries).
Dan Albert6ab43d82017-12-13 15:05:04 -0800537 return android.Paths{getNdkBaseTimestampFile(ctx)}
Colin Cross4d9c2d12016-07-29 12:48:20 -0700538 }
539 return nil
540}
541
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700542func (compiler *baseCompiler) compile(ctx ModuleContext, flags Flags, deps PathDeps) Objects {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700543 pathDeps := deps.GeneratedHeaders
544 pathDeps = append(pathDeps, ndkPathDeps(ctx)...)
Colin Cross2f336352016-10-26 10:03:47 -0700545
Colin Cross2f336352016-10-26 10:03:47 -0700546 buildFlags := flagsToBuilderFlags(flags)
547
Colin Crossf18e1102017-11-16 14:33:08 -0800548 srcs := append(android.Paths(nil), compiler.srcsBeforeGen...)
549
Colin Cross2f336352016-10-26 10:03:47 -0700550 srcs, genDeps := genSources(ctx, srcs, buildFlags)
Colin Cross2f336352016-10-26 10:03:47 -0700551 pathDeps = append(pathDeps, flags.CFlagsDeps...)
552
Pirama Arumuga Nainar70ba5a32017-12-19 15:11:01 -0800553 compiler.pathDeps = pathDeps
554 compiler.genDeps = genDeps
Colin Cross2f336352016-10-26 10:03:47 -0700555
Fabien Sanglardd61f1f42017-01-10 16:21:22 -0800556 // Save src, buildFlags and context
557 compiler.srcs = srcs
558
Colin Cross4d9c2d12016-07-29 12:48:20 -0700559 // Compile files listed in c.Properties.Srcs into objects
Pirama Arumuga Nainar70ba5a32017-12-19 15:11:01 -0800560 objs := compileObjs(ctx, buildFlags, "", srcs, pathDeps, genDeps)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700561
562 if ctx.Failed() {
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700563 return Objects{}
Colin Cross4d9c2d12016-07-29 12:48:20 -0700564 }
565
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700566 return objs
Colin Cross4d9c2d12016-07-29 12:48:20 -0700567}
568
569// Compile a list of source files into objects a specified subdirectory
Colin Cross2f336352016-10-26 10:03:47 -0700570func compileObjs(ctx android.ModuleContext, flags builderFlags,
Pirama Arumuga Nainar70ba5a32017-12-19 15:11:01 -0800571 subdir string, srcFiles, pathDeps android.Paths, genDeps android.Paths) Objects {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700572
Pirama Arumuga Nainar70ba5a32017-12-19 15:11:01 -0800573 return TransformSourceToObj(ctx, subdir, srcFiles, flags, pathDeps, genDeps)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700574}