blob: ca68a00a5586398a9cbfe08e16549b035952942e [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"`
Colin Cross4d9c2d12016-07-29 12:48:20 -0700151}
152
Colin Crossb916a382016-07-29 17:28:03 -0700153func NewBaseCompiler() *baseCompiler {
154 return &baseCompiler{}
155}
156
Colin Cross4d9c2d12016-07-29 12:48:20 -0700157type baseCompiler struct {
158 Properties BaseCompilerProperties
Colin Cross38f794e2017-09-07 10:53:07 -0700159 Proto android.ProtoProperties
Colin Cross2f336352016-10-26 10:03:47 -0700160 deps android.Paths
Fabien Sanglardd61f1f42017-01-10 16:21:22 -0800161 flags builderFlags
Colin Crossf18e1102017-11-16 14:33:08 -0800162
163 // Sources that were passed to the C/C++ compiler
164 srcs android.Paths
165
166 // Sources that were passed in the Android.bp file, including generated sources generated by
167 // other modules and filegroups. May include source files that have not yet been translated to
168 // C/C++ (.aidl, .proto, etc.)
169 srcsBeforeGen android.Paths
Colin Cross4d9c2d12016-07-29 12:48:20 -0700170}
171
172var _ compiler = (*baseCompiler)(nil)
173
Fabien Sanglardd61f1f42017-01-10 16:21:22 -0800174type CompiledInterface interface {
175 Srcs() android.Paths
176}
177
178func (compiler *baseCompiler) Srcs() android.Paths {
179 return compiler.srcs
180}
181
Colin Cross4d9c2d12016-07-29 12:48:20 -0700182func (compiler *baseCompiler) appendCflags(flags []string) {
183 compiler.Properties.Cflags = append(compiler.Properties.Cflags, flags...)
184}
185
186func (compiler *baseCompiler) appendAsflags(flags []string) {
187 compiler.Properties.Asflags = append(compiler.Properties.Asflags, flags...)
188}
189
Colin Cross42742b82016-08-01 13:20:05 -0700190func (compiler *baseCompiler) compilerProps() []interface{} {
Colin Cross0feb1692016-11-03 14:38:52 -0700191 return []interface{}{&compiler.Properties, &compiler.Proto}
Colin Cross4d9c2d12016-07-29 12:48:20 -0700192}
193
Colin Cross42742b82016-08-01 13:20:05 -0700194func (compiler *baseCompiler) compilerInit(ctx BaseModuleContext) {}
Colin Cross4d9c2d12016-07-29 12:48:20 -0700195
Colin Cross37047f12016-12-13 17:06:13 -0800196func (compiler *baseCompiler) compilerDeps(ctx DepsContext, deps Deps) Deps {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700197 deps.GeneratedSources = append(deps.GeneratedSources, compiler.Properties.Generated_sources...)
198 deps.GeneratedHeaders = append(deps.GeneratedHeaders, compiler.Properties.Generated_headers...)
199
Colin Cross068e0fe2016-12-13 15:23:47 -0800200 android.ExtractSourcesDeps(ctx, compiler.Properties.Srcs)
201
Dan Willemsene1a3ce32016-11-02 20:44:08 -0700202 if compiler.hasSrcExt(".proto") {
Nan Zhang0007d812017-11-07 10:57:05 -0800203 deps = protoDeps(ctx, deps, &compiler.Proto, Bool(compiler.Properties.Proto.Static))
Colin Cross0c461f12016-10-20 16:11:43 -0700204 }
205
Colin Cross4d9c2d12016-07-29 12:48:20 -0700206 return deps
207}
208
209// Create a Flags struct that collects the compile flags from global values,
210// per-target values, module type values, and per-module Blueprints properties
Colin Crossf18e1102017-11-16 14:33:08 -0800211func (compiler *baseCompiler) compilerFlags(ctx ModuleContext, flags Flags, deps PathDeps) Flags {
Colin Crossb98c8b02016-07-29 13:44:28 -0700212 tc := ctx.toolchain()
Colin Cross4d9c2d12016-07-29 12:48:20 -0700213
Colin Crossf18e1102017-11-16 14:33:08 -0800214 compiler.srcsBeforeGen = ctx.ExpandSources(compiler.Properties.Srcs, compiler.Properties.Exclude_srcs)
215 compiler.srcsBeforeGen = append(compiler.srcsBeforeGen, deps.GeneratedSources...)
216
Colin Cross4d9c2d12016-07-29 12:48:20 -0700217 CheckBadCompilerFlags(ctx, "cflags", compiler.Properties.Cflags)
218 CheckBadCompilerFlags(ctx, "cppflags", compiler.Properties.Cppflags)
219 CheckBadCompilerFlags(ctx, "conlyflags", compiler.Properties.Conlyflags)
220 CheckBadCompilerFlags(ctx, "asflags", compiler.Properties.Asflags)
221
Colin Cross4b963f82016-09-29 14:06:02 -0700222 esc := proptools.NinjaAndShellEscape
223
224 flags.CFlags = append(flags.CFlags, esc(compiler.Properties.Cflags)...)
225 flags.CppFlags = append(flags.CppFlags, esc(compiler.Properties.Cppflags)...)
226 flags.ConlyFlags = append(flags.ConlyFlags, esc(compiler.Properties.Conlyflags)...)
227 flags.AsFlags = append(flags.AsFlags, esc(compiler.Properties.Asflags)...)
Colin Cross91e90042016-12-02 17:13:24 -0800228 flags.YasmFlags = append(flags.YasmFlags, esc(compiler.Properties.Asflags)...)
Colin Cross4b963f82016-09-29 14:06:02 -0700229 flags.YaccFlags = append(flags.YaccFlags, esc(compiler.Properties.Yaccflags)...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700230
231 // Include dir cflags
Colin Cross4d9c2d12016-07-29 12:48:20 -0700232 localIncludeDirs := android.PathsForModuleSrc(ctx, compiler.Properties.Local_include_dirs)
Dan Willemsen273af7f2016-11-03 15:53:42 -0700233 if len(localIncludeDirs) > 0 {
Colin Crossdad8c952017-04-26 14:55:27 -0700234 f := includeDirsToFlags(localIncludeDirs)
235 flags.GlobalFlags = append(flags.GlobalFlags, f)
236 flags.YasmFlags = append(flags.YasmFlags, f)
Dan Willemsen273af7f2016-11-03 15:53:42 -0700237 }
238 rootIncludeDirs := android.PathsForSource(ctx, compiler.Properties.Include_dirs)
239 if len(rootIncludeDirs) > 0 {
Colin Crossdad8c952017-04-26 14:55:27 -0700240 f := includeDirsToFlags(rootIncludeDirs)
241 flags.GlobalFlags = append(flags.GlobalFlags, f)
242 flags.YasmFlags = append(flags.YasmFlags, f)
Dan Willemsen273af7f2016-11-03 15:53:42 -0700243 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700244
Colin Cross87dd9632017-11-03 13:31:05 -0700245 flags.GlobalFlags = append(flags.GlobalFlags, "-I"+android.PathForModuleSrc(ctx).String())
246 flags.YasmFlags = append(flags.YasmFlags, "-I"+android.PathForModuleSrc(ctx).String())
Colin Crossc3199482017-03-30 15:03:04 -0700247
Colin Cross87dd9632017-11-03 13:31:05 -0700248 if !(ctx.useSdk() || ctx.useVndk()) || ctx.Host() {
249 flags.SystemIncludeFlags = append(flags.SystemIncludeFlags,
250 "${config.CommonGlobalIncludes}",
251 tc.IncludeFlags(),
252 "${config.CommonNativehelperInclude}")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700253 }
254
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700255 if ctx.useSdk() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700256 // The NDK headers are installed to a common sysroot. While a more
257 // typical Soong approach would be to only make the headers for the
258 // library you're using available, we're trying to emulate the NDK
259 // behavior here, and the NDK always has all the NDK headers available.
Colin Crossc3199482017-03-30 15:03:04 -0700260 flags.SystemIncludeFlags = append(flags.SystemIncludeFlags,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700261 "-isystem "+getCurrentIncludePath(ctx).String(),
Colin Crossb98c8b02016-07-29 13:44:28 -0700262 "-isystem "+getCurrentIncludePath(ctx).Join(ctx, tc.ClangTriple()).String())
Colin Cross4d9c2d12016-07-29 12:48:20 -0700263
264 // Traditionally this has come from android/api-level.h, but with the
265 // libc headers unified it must be set by the build system since we
266 // don't have per-API level copies of that header now.
Dan Albertebedf672016-11-08 15:06:22 -0800267 version := ctx.sdkVersion()
268 if version == "current" {
269 version = "__ANDROID_API_FUTURE__"
270 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700271 flags.GlobalFlags = append(flags.GlobalFlags,
Dan Albertebedf672016-11-08 15:06:22 -0800272 "-D__ANDROID_API__="+version)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700273
274 // Until the full NDK has been migrated to using ndk_headers, we still
275 // need to add the legacy sysroot includes to get the full set of
276 // headers.
277 legacyIncludes := fmt.Sprintf(
278 "prebuilts/ndk/current/platforms/android-%s/arch-%s/usr/include",
279 ctx.sdkVersion(), ctx.Arch().ArchType.String())
Colin Crossc3199482017-03-30 15:03:04 -0700280 flags.SystemIncludeFlags = append(flags.SystemIncludeFlags, "-isystem "+legacyIncludes)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700281 }
282
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700283 if ctx.useVndk() {
Dan Willemsenb916b802017-03-19 13:44:32 -0700284 flags.GlobalFlags = append(flags.GlobalFlags,
285 "-D__ANDROID_API__=__ANDROID_API_FUTURE__", "-D__ANDROID_VNDK__")
286 }
287
Nan Zhang0007d812017-11-07 10:57:05 -0800288 instructionSet := String(compiler.Properties.Instruction_set)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700289 if flags.RequiredInstructionSet != "" {
290 instructionSet = flags.RequiredInstructionSet
291 }
Colin Crossb98c8b02016-07-29 13:44:28 -0700292 instructionSetFlags, err := tc.InstructionSetFlags(instructionSet)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700293 if flags.Clang {
Colin Crossb98c8b02016-07-29 13:44:28 -0700294 instructionSetFlags, err = tc.ClangInstructionSetFlags(instructionSet)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700295 }
296 if err != nil {
297 ctx.ModuleErrorf("%s", err)
298 }
299
300 CheckBadCompilerFlags(ctx, "release.cflags", compiler.Properties.Release.Cflags)
301
302 // TODO: debug
Colin Cross4b963f82016-09-29 14:06:02 -0700303 flags.CFlags = append(flags.CFlags, esc(compiler.Properties.Release.Cflags)...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700304
305 if flags.Clang {
306 CheckBadCompilerFlags(ctx, "clang_cflags", compiler.Properties.Clang_cflags)
307 CheckBadCompilerFlags(ctx, "clang_asflags", compiler.Properties.Clang_asflags)
308
Colin Crossb98c8b02016-07-29 13:44:28 -0700309 flags.CFlags = config.ClangFilterUnknownCflags(flags.CFlags)
Colin Cross4b963f82016-09-29 14:06:02 -0700310 flags.CFlags = append(flags.CFlags, esc(compiler.Properties.Clang_cflags)...)
311 flags.AsFlags = append(flags.AsFlags, esc(compiler.Properties.Clang_asflags)...)
Colin Crossb98c8b02016-07-29 13:44:28 -0700312 flags.CppFlags = config.ClangFilterUnknownCflags(flags.CppFlags)
313 flags.ConlyFlags = config.ClangFilterUnknownCflags(flags.ConlyFlags)
314 flags.LdFlags = config.ClangFilterUnknownCflags(flags.LdFlags)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700315
Colin Crossb98c8b02016-07-29 13:44:28 -0700316 target := "-target " + tc.ClangTriple()
Dan Willemsen300151b2017-03-13 12:40:30 -0700317 gccPrefix := "-B" + config.ToolPath(tc)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700318
319 flags.CFlags = append(flags.CFlags, target, gccPrefix)
320 flags.AsFlags = append(flags.AsFlags, target, gccPrefix)
321 flags.LdFlags = append(flags.LdFlags, target, gccPrefix)
322 }
323
Colin Crossb98c8b02016-07-29 13:44:28 -0700324 hod := "Host"
Colin Cross4d9c2d12016-07-29 12:48:20 -0700325 if ctx.Os().Class == android.Device {
Colin Crossb98c8b02016-07-29 13:44:28 -0700326 hod = "Device"
Colin Cross4d9c2d12016-07-29 12:48:20 -0700327 }
328
Colin Cross87dd9632017-11-03 13:31:05 -0700329 flags.GlobalFlags = append(flags.GlobalFlags, instructionSetFlags)
330 flags.ConlyFlags = append([]string{"${config.CommonGlobalConlyflags}"}, flags.ConlyFlags...)
Colin Cross26f14502017-11-06 13:59:48 -0800331 flags.CppFlags = append([]string{fmt.Sprintf("${config.%sGlobalCppflags}", hod)}, flags.CppFlags...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700332
Colin Cross87dd9632017-11-03 13:31:05 -0700333 if flags.Clang {
334 flags.AsFlags = append(flags.AsFlags, tc.ClangAsflags())
335 flags.CppFlags = append([]string{"${config.CommonClangGlobalCppflags}"}, flags.CppFlags...)
336 flags.GlobalFlags = append(flags.GlobalFlags,
337 tc.ClangCflags(),
338 "${config.CommonClangGlobalCflags}",
339 fmt.Sprintf("${config.%sClangGlobalCflags}", hod))
340 } else {
341 flags.CppFlags = append([]string{"${config.CommonGlobalCppflags}"}, flags.CppFlags...)
342 flags.GlobalFlags = append(flags.GlobalFlags,
343 tc.Cflags(),
344 "${config.CommonGlobalCflags}",
345 fmt.Sprintf("${config.%sGlobalCflags}", hod))
Colin Cross4d9c2d12016-07-29 12:48:20 -0700346 }
347
Colin Cross87dd9632017-11-03 13:31:05 -0700348 if Bool(ctx.AConfig().ProductVariables.Brillo) {
349 flags.GlobalFlags = append(flags.GlobalFlags, "-D__BRILLO__")
350 }
351
352 if ctx.Device() {
353 if Bool(compiler.Properties.Rtti) {
354 flags.CppFlags = append(flags.CppFlags, "-frtti")
355 } else {
356 flags.CppFlags = append(flags.CppFlags, "-fno-rtti")
357 }
358 }
359
360 flags.AsFlags = append(flags.AsFlags, "-D__ASSEMBLY__")
361
362 if flags.Clang {
363 flags.CppFlags = append(flags.CppFlags, tc.ClangCppflags())
364 } else {
365 flags.CppFlags = append(flags.CppFlags, tc.Cppflags())
366 }
367
368 flags.YasmFlags = append(flags.YasmFlags, tc.YasmFlags())
369
Colin Cross4d9c2d12016-07-29 12:48:20 -0700370 if flags.Clang {
Colin Crossb98c8b02016-07-29 13:44:28 -0700371 flags.GlobalFlags = append(flags.GlobalFlags, tc.ToolchainClangCflags())
Colin Cross4d9c2d12016-07-29 12:48:20 -0700372 } else {
Colin Crossb98c8b02016-07-29 13:44:28 -0700373 flags.GlobalFlags = append(flags.GlobalFlags, tc.ToolchainCflags())
Colin Cross4d9c2d12016-07-29 12:48:20 -0700374 }
375
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700376 if !ctx.useSdk() {
Colin Cross6f6a4282016-10-17 14:19:06 -0700377 cStd := config.CStdVersion
Nan Zhang0007d812017-11-07 10:57:05 -0800378 if String(compiler.Properties.C_std) == "experimental" {
Dan Albert043833c2017-02-03 16:13:38 -0800379 cStd = config.ExperimentalCStdVersion
Nan Zhang0007d812017-11-07 10:57:05 -0800380 } else if String(compiler.Properties.C_std) != "" {
381 cStd = String(compiler.Properties.C_std)
Dan Albert043833c2017-02-03 16:13:38 -0800382 }
383
Nan Zhang0007d812017-11-07 10:57:05 -0800384 cppStd := String(compiler.Properties.Cpp_std)
385 switch String(compiler.Properties.Cpp_std) {
Josh Gaof5720172017-06-23 14:56:50 -0700386 case "":
387 cppStd = config.CppStdVersion
388 case "experimental":
Dan Albert043833c2017-02-03 16:13:38 -0800389 cppStd = config.ExperimentalCppStdVersion
Josh Gaof5720172017-06-23 14:56:50 -0700390 case "c++17", "gnu++17":
391 // Map c++17 and gnu++17 to their 1z equivalents, until 17 is finalized.
Nan Zhang0007d812017-11-07 10:57:05 -0800392 cppStd = strings.Replace(String(compiler.Properties.Cpp_std), "17", "1z", 1)
Dan Albert043833c2017-02-03 16:13:38 -0800393 }
Colin Cross6f6a4282016-10-17 14:19:06 -0700394
395 if !flags.Clang {
396 // GCC uses an invalid C++14 ABI (emits calls to
397 // __cxa_throw_bad_array_length, which is not a valid C++ RT ABI).
398 // http://b/25022512
399 cppStd = config.GccCppStdVersion
400 } else if ctx.Host() && !flags.Clang {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700401 // The host GCC doesn't support C++14 (and is deprecated, so likely
402 // never will). Build these modules with C++11.
Colin Cross6f6a4282016-10-17 14:19:06 -0700403 cppStd = config.GccCppStdVersion
Colin Cross4d9c2d12016-07-29 12:48:20 -0700404 }
Colin Cross6f6a4282016-10-17 14:19:06 -0700405
Colin Cross948f0cb2016-10-17 14:24:56 -0700406 if compiler.Properties.Gnu_extensions != nil && *compiler.Properties.Gnu_extensions == false {
407 cStd = gnuToCReplacer.Replace(cStd)
408 cppStd = gnuToCReplacer.Replace(cppStd)
409 }
410
Colin Cross6f6a4282016-10-17 14:19:06 -0700411 flags.ConlyFlags = append([]string{"-std=" + cStd}, flags.ConlyFlags...)
412 flags.CppFlags = append([]string{"-std=" + cppStd}, flags.CppFlags...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700413 }
414
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700415 if ctx.useVndk() {
Jiyong Parka622de82017-08-10 13:33:27 +0900416 flags.CFlags = append(flags.CFlags, esc(compiler.Properties.Target.Vendor.Cflags)...)
417 }
418
Colin Cross4d9c2d12016-07-29 12:48:20 -0700419 // We can enforce some rules more strictly in the code we own. strict
420 // indicates if this is code that we can be stricter with. If we have
421 // rules that we want to apply to *our* code (but maybe can't for
422 // vendor/device specific things), we could extend this to be a ternary
423 // value.
424 strict := true
425 if strings.HasPrefix(android.PathForModuleSrc(ctx).String(), "external/") {
426 strict = false
427 }
428
429 // Can be used to make some annotations stricter for code we can fix
430 // (such as when we mark functions as deprecated).
431 if strict {
432 flags.CFlags = append(flags.CFlags, "-DANDROID_STRICT")
433 }
434
Dan Willemsene1a3ce32016-11-02 20:44:08 -0700435 if compiler.hasSrcExt(".proto") {
Colin Cross0c461f12016-10-20 16:11:43 -0700436 flags = protoFlags(ctx, flags, &compiler.Proto)
437 }
438
Dan Willemsene1a3ce32016-11-02 20:44:08 -0700439 if compiler.hasSrcExt(".y") || compiler.hasSrcExt(".yy") {
440 flags.GlobalFlags = append(flags.GlobalFlags,
441 "-I"+android.PathForModuleGen(ctx, "yacc", ctx.ModuleDir()).String())
442 }
443
Dan Willemsen4f1c3d42017-09-09 01:15:26 -0700444 if compiler.hasSrcExt(".mc") {
445 flags.GlobalFlags = append(flags.GlobalFlags,
446 "-I"+android.PathForModuleGen(ctx, "windmc", ctx.ModuleDir()).String())
447 }
448
Dan Willemsene1240db2016-11-03 14:28:51 -0700449 if compiler.hasSrcExt(".aidl") {
450 if len(compiler.Properties.Aidl.Local_include_dirs) > 0 {
451 localAidlIncludeDirs := android.PathsForModuleSrc(ctx, compiler.Properties.Aidl.Local_include_dirs)
452 flags.aidlFlags = append(flags.aidlFlags, includeDirsToFlags(localAidlIncludeDirs))
453 }
454 if len(compiler.Properties.Aidl.Include_dirs) > 0 {
455 rootAidlIncludeDirs := android.PathsForSource(ctx, compiler.Properties.Aidl.Include_dirs)
456 flags.aidlFlags = append(flags.aidlFlags, includeDirsToFlags(rootAidlIncludeDirs))
457 }
458
459 flags.GlobalFlags = append(flags.GlobalFlags,
460 "-I"+android.PathForModuleGen(ctx, "aidl").String())
461 }
462
Colin Cross2a252be2017-05-01 17:37:24 -0700463 if compiler.hasSrcExt(".rs") || compiler.hasSrcExt(".fs") {
464 flags = rsFlags(ctx, flags, &compiler.Properties)
465 }
466
Colin Cross4d9c2d12016-07-29 12:48:20 -0700467 return flags
468}
469
Dan Willemsene1a3ce32016-11-02 20:44:08 -0700470func (compiler *baseCompiler) hasSrcExt(ext string) bool {
Colin Crossf18e1102017-11-16 14:33:08 -0800471 for _, src := range compiler.srcsBeforeGen {
472 if src.Ext() == ext {
473 return true
474 }
475 }
Colin Cross0c461f12016-10-20 16:11:43 -0700476 for _, src := range compiler.Properties.Srcs {
Dan Willemsene1a3ce32016-11-02 20:44:08 -0700477 if filepath.Ext(src) == ext {
Colin Cross0c461f12016-10-20 16:11:43 -0700478 return true
479 }
480 }
Colin Cross10d22312017-05-03 11:01:58 -0700481 for _, src := range compiler.Properties.OriginalSrcs {
482 if filepath.Ext(src) == ext {
483 return true
484 }
485 }
Colin Cross0c461f12016-10-20 16:11:43 -0700486
487 return false
488}
489
Colin Cross948f0cb2016-10-17 14:24:56 -0700490var gnuToCReplacer = strings.NewReplacer("gnu", "c")
491
Colin Cross4d9c2d12016-07-29 12:48:20 -0700492func ndkPathDeps(ctx ModuleContext) android.Paths {
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700493 if ctx.useSdk() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700494 // The NDK sysroot timestamp file depends on all the NDK sysroot files
495 // (headers and libraries).
496 return android.Paths{getNdkSysrootTimestampFile(ctx)}
497 }
498 return nil
499}
500
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700501func (compiler *baseCompiler) compile(ctx ModuleContext, flags Flags, deps PathDeps) Objects {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700502 pathDeps := deps.GeneratedHeaders
503 pathDeps = append(pathDeps, ndkPathDeps(ctx)...)
Colin Cross2f336352016-10-26 10:03:47 -0700504
Colin Cross2f336352016-10-26 10:03:47 -0700505 buildFlags := flagsToBuilderFlags(flags)
506
Colin Crossf18e1102017-11-16 14:33:08 -0800507 srcs := append(android.Paths(nil), compiler.srcsBeforeGen...)
508
Colin Cross2f336352016-10-26 10:03:47 -0700509 srcs, genDeps := genSources(ctx, srcs, buildFlags)
510
511 pathDeps = append(pathDeps, genDeps...)
512 pathDeps = append(pathDeps, flags.CFlagsDeps...)
513
514 compiler.deps = pathDeps
515
Fabien Sanglardd61f1f42017-01-10 16:21:22 -0800516 // Save src, buildFlags and context
517 compiler.srcs = srcs
518
Colin Cross4d9c2d12016-07-29 12:48:20 -0700519 // Compile files listed in c.Properties.Srcs into objects
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700520 objs := compileObjs(ctx, buildFlags, "", srcs, compiler.deps)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700521
522 if ctx.Failed() {
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700523 return Objects{}
Colin Cross4d9c2d12016-07-29 12:48:20 -0700524 }
525
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700526 return objs
Colin Cross4d9c2d12016-07-29 12:48:20 -0700527}
528
529// Compile a list of source files into objects a specified subdirectory
Colin Cross2f336352016-10-26 10:03:47 -0700530func compileObjs(ctx android.ModuleContext, flags builderFlags,
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700531 subdir string, srcFiles, deps android.Paths) Objects {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700532
Colin Cross2f336352016-10-26 10:03:47 -0700533 return TransformSourceToObj(ctx, subdir, srcFiles, flags, deps)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700534}