blob: aed4480e0c55791deff07ab88d5c8ecbb5dc50c7 [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.
65 Instruction_set string `android:"arch_variant"`
66
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.
72 Include_dirs []string `android:"arch_variant"`
73
74 // list of directories relative to the Blueprints file that will
75 // be added to the include path using -I
76 Local_include_dirs []string `android:"arch_variant"`
77
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).
92 C_std string
93
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).
97 Cpp_std string
98
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 Cross4d9c2d12016-07-29 12:48:20 -0700111 Debug, Release struct {
112 // list of module-specific flags that will be used for C and C++ compiles in debug or
113 // release builds
114 Cflags []string `android:"arch_variant"`
115 } `android:"arch_variant"`
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700116
117 Target struct {
118 Vendor struct {
119 // list of source files that should only be used in the
120 // vendor variant of the C/C++ module.
121 Srcs []string
122
123 // list of source files that should not be used to
124 // build the vendor variant of the C/C++ module.
125 Exclude_srcs []string
126 }
127 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700128}
129
Colin Crossb916a382016-07-29 17:28:03 -0700130func NewBaseCompiler() *baseCompiler {
131 return &baseCompiler{}
132}
133
Colin Cross4d9c2d12016-07-29 12:48:20 -0700134type baseCompiler struct {
135 Properties BaseCompilerProperties
Colin Cross0c461f12016-10-20 16:11:43 -0700136 Proto ProtoProperties
Colin Cross2f336352016-10-26 10:03:47 -0700137 deps android.Paths
Fabien Sanglardd61f1f42017-01-10 16:21:22 -0800138 srcs android.Paths
139 flags builderFlags
Colin Cross4d9c2d12016-07-29 12:48:20 -0700140}
141
142var _ compiler = (*baseCompiler)(nil)
143
Fabien Sanglardd61f1f42017-01-10 16:21:22 -0800144type CompiledInterface interface {
145 Srcs() android.Paths
146}
147
148func (compiler *baseCompiler) Srcs() android.Paths {
149 return compiler.srcs
150}
151
Colin Cross4d9c2d12016-07-29 12:48:20 -0700152func (compiler *baseCompiler) appendCflags(flags []string) {
153 compiler.Properties.Cflags = append(compiler.Properties.Cflags, flags...)
154}
155
156func (compiler *baseCompiler) appendAsflags(flags []string) {
157 compiler.Properties.Asflags = append(compiler.Properties.Asflags, flags...)
158}
159
Colin Cross42742b82016-08-01 13:20:05 -0700160func (compiler *baseCompiler) compilerProps() []interface{} {
Colin Cross0feb1692016-11-03 14:38:52 -0700161 return []interface{}{&compiler.Properties, &compiler.Proto}
Colin Cross4d9c2d12016-07-29 12:48:20 -0700162}
163
Colin Cross42742b82016-08-01 13:20:05 -0700164func (compiler *baseCompiler) compilerInit(ctx BaseModuleContext) {}
Colin Cross4d9c2d12016-07-29 12:48:20 -0700165
Colin Cross37047f12016-12-13 17:06:13 -0800166func (compiler *baseCompiler) compilerDeps(ctx DepsContext, deps Deps) Deps {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700167 deps.GeneratedSources = append(deps.GeneratedSources, compiler.Properties.Generated_sources...)
168 deps.GeneratedHeaders = append(deps.GeneratedHeaders, compiler.Properties.Generated_headers...)
169
Colin Cross068e0fe2016-12-13 15:23:47 -0800170 android.ExtractSourcesDeps(ctx, compiler.Properties.Srcs)
171
Dan Willemsene1a3ce32016-11-02 20:44:08 -0700172 if compiler.hasSrcExt(".proto") {
Colin Cross0c461f12016-10-20 16:11:43 -0700173 deps = protoDeps(ctx, deps, &compiler.Proto)
174 }
175
Colin Cross4d9c2d12016-07-29 12:48:20 -0700176 return deps
177}
178
179// Create a Flags struct that collects the compile flags from global values,
180// per-target values, module type values, and per-module Blueprints properties
Colin Cross42742b82016-08-01 13:20:05 -0700181func (compiler *baseCompiler) compilerFlags(ctx ModuleContext, flags Flags) Flags {
Colin Crossb98c8b02016-07-29 13:44:28 -0700182 tc := ctx.toolchain()
Colin Cross4d9c2d12016-07-29 12:48:20 -0700183
184 CheckBadCompilerFlags(ctx, "cflags", compiler.Properties.Cflags)
185 CheckBadCompilerFlags(ctx, "cppflags", compiler.Properties.Cppflags)
186 CheckBadCompilerFlags(ctx, "conlyflags", compiler.Properties.Conlyflags)
187 CheckBadCompilerFlags(ctx, "asflags", compiler.Properties.Asflags)
188
Colin Cross4b963f82016-09-29 14:06:02 -0700189 esc := proptools.NinjaAndShellEscape
190
191 flags.CFlags = append(flags.CFlags, esc(compiler.Properties.Cflags)...)
192 flags.CppFlags = append(flags.CppFlags, esc(compiler.Properties.Cppflags)...)
193 flags.ConlyFlags = append(flags.ConlyFlags, esc(compiler.Properties.Conlyflags)...)
194 flags.AsFlags = append(flags.AsFlags, esc(compiler.Properties.Asflags)...)
Colin Cross91e90042016-12-02 17:13:24 -0800195 flags.YasmFlags = append(flags.YasmFlags, esc(compiler.Properties.Asflags)...)
Colin Cross4b963f82016-09-29 14:06:02 -0700196 flags.YaccFlags = append(flags.YaccFlags, esc(compiler.Properties.Yaccflags)...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700197
198 // Include dir cflags
Colin Cross4d9c2d12016-07-29 12:48:20 -0700199 localIncludeDirs := android.PathsForModuleSrc(ctx, compiler.Properties.Local_include_dirs)
Dan Willemsen273af7f2016-11-03 15:53:42 -0700200 if len(localIncludeDirs) > 0 {
201 flags.GlobalFlags = append(flags.GlobalFlags, includeDirsToFlags(localIncludeDirs))
202 }
203 rootIncludeDirs := android.PathsForSource(ctx, compiler.Properties.Include_dirs)
204 if len(rootIncludeDirs) > 0 {
205 flags.GlobalFlags = append(flags.GlobalFlags, includeDirsToFlags(rootIncludeDirs))
206 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700207
208 if !ctx.noDefaultCompilerFlags() {
Colin Crossc3199482017-03-30 15:03:04 -0700209 flags.GlobalFlags = append(flags.GlobalFlags, "-I"+android.PathForModuleSrc(ctx).String())
210
Dan Willemsend2ede872016-11-18 14:54:24 -0800211 if !(ctx.sdk() || ctx.vndk()) || ctx.Host() {
Colin Crossc3199482017-03-30 15:03:04 -0700212 flags.SystemIncludeFlags = append(flags.SystemIncludeFlags,
Colin Crossb98c8b02016-07-29 13:44:28 -0700213 "${config.CommonGlobalIncludes}",
Colin Cross1cfd89a2016-09-15 09:30:46 -0700214 "${config.CommonGlobalSystemIncludes}",
Colin Crossb98c8b02016-07-29 13:44:28 -0700215 tc.IncludeFlags(),
216 "${config.CommonNativehelperInclude}")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700217 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700218 }
219
Dan Willemsenb916b802017-03-19 13:44:32 -0700220 if ctx.sdk() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700221 // The NDK headers are installed to a common sysroot. While a more
222 // typical Soong approach would be to only make the headers for the
223 // library you're using available, we're trying to emulate the NDK
224 // behavior here, and the NDK always has all the NDK headers available.
Colin Crossc3199482017-03-30 15:03:04 -0700225 flags.SystemIncludeFlags = append(flags.SystemIncludeFlags,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700226 "-isystem "+getCurrentIncludePath(ctx).String(),
Colin Crossb98c8b02016-07-29 13:44:28 -0700227 "-isystem "+getCurrentIncludePath(ctx).Join(ctx, tc.ClangTriple()).String())
Colin Cross4d9c2d12016-07-29 12:48:20 -0700228
229 // Traditionally this has come from android/api-level.h, but with the
230 // libc headers unified it must be set by the build system since we
231 // don't have per-API level copies of that header now.
Dan Albertebedf672016-11-08 15:06:22 -0800232 version := ctx.sdkVersion()
233 if version == "current" {
234 version = "__ANDROID_API_FUTURE__"
235 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700236 flags.GlobalFlags = append(flags.GlobalFlags,
Dan Albertebedf672016-11-08 15:06:22 -0800237 "-D__ANDROID_API__="+version)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700238
239 // Until the full NDK has been migrated to using ndk_headers, we still
240 // need to add the legacy sysroot includes to get the full set of
241 // headers.
242 legacyIncludes := fmt.Sprintf(
243 "prebuilts/ndk/current/platforms/android-%s/arch-%s/usr/include",
244 ctx.sdkVersion(), ctx.Arch().ArchType.String())
Colin Crossc3199482017-03-30 15:03:04 -0700245 flags.SystemIncludeFlags = append(flags.SystemIncludeFlags, "-isystem "+legacyIncludes)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700246 }
247
Dan Willemsenb916b802017-03-19 13:44:32 -0700248 if ctx.vndk() {
249 flags.GlobalFlags = append(flags.GlobalFlags,
250 "-D__ANDROID_API__=__ANDROID_API_FUTURE__", "-D__ANDROID_VNDK__")
251 }
252
Colin Cross4d9c2d12016-07-29 12:48:20 -0700253 instructionSet := compiler.Properties.Instruction_set
254 if flags.RequiredInstructionSet != "" {
255 instructionSet = flags.RequiredInstructionSet
256 }
Colin Crossb98c8b02016-07-29 13:44:28 -0700257 instructionSetFlags, err := tc.InstructionSetFlags(instructionSet)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700258 if flags.Clang {
Colin Crossb98c8b02016-07-29 13:44:28 -0700259 instructionSetFlags, err = tc.ClangInstructionSetFlags(instructionSet)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700260 }
261 if err != nil {
262 ctx.ModuleErrorf("%s", err)
263 }
264
265 CheckBadCompilerFlags(ctx, "release.cflags", compiler.Properties.Release.Cflags)
266
267 // TODO: debug
Colin Cross4b963f82016-09-29 14:06:02 -0700268 flags.CFlags = append(flags.CFlags, esc(compiler.Properties.Release.Cflags)...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700269
270 if flags.Clang {
271 CheckBadCompilerFlags(ctx, "clang_cflags", compiler.Properties.Clang_cflags)
272 CheckBadCompilerFlags(ctx, "clang_asflags", compiler.Properties.Clang_asflags)
273
Colin Crossb98c8b02016-07-29 13:44:28 -0700274 flags.CFlags = config.ClangFilterUnknownCflags(flags.CFlags)
Colin Cross4b963f82016-09-29 14:06:02 -0700275 flags.CFlags = append(flags.CFlags, esc(compiler.Properties.Clang_cflags)...)
276 flags.AsFlags = append(flags.AsFlags, esc(compiler.Properties.Clang_asflags)...)
Colin Crossb98c8b02016-07-29 13:44:28 -0700277 flags.CppFlags = config.ClangFilterUnknownCflags(flags.CppFlags)
278 flags.ConlyFlags = config.ClangFilterUnknownCflags(flags.ConlyFlags)
279 flags.LdFlags = config.ClangFilterUnknownCflags(flags.LdFlags)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700280
Colin Crossb98c8b02016-07-29 13:44:28 -0700281 target := "-target " + tc.ClangTriple()
Dan Willemsen300151b2017-03-13 12:40:30 -0700282 gccPrefix := "-B" + config.ToolPath(tc)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700283
284 flags.CFlags = append(flags.CFlags, target, gccPrefix)
285 flags.AsFlags = append(flags.AsFlags, target, gccPrefix)
286 flags.LdFlags = append(flags.LdFlags, target, gccPrefix)
287 }
288
Colin Crossb98c8b02016-07-29 13:44:28 -0700289 hod := "Host"
Colin Cross4d9c2d12016-07-29 12:48:20 -0700290 if ctx.Os().Class == android.Device {
Colin Crossb98c8b02016-07-29 13:44:28 -0700291 hod = "Device"
Colin Cross4d9c2d12016-07-29 12:48:20 -0700292 }
293
294 if !ctx.noDefaultCompilerFlags() {
295 flags.GlobalFlags = append(flags.GlobalFlags, instructionSetFlags)
Colin Crossb6688262016-11-22 12:32:47 -0800296 flags.ConlyFlags = append([]string{"${config.CommonGlobalConlyflags}"}, flags.ConlyFlags...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700297
298 if flags.Clang {
Colin Crossb98c8b02016-07-29 13:44:28 -0700299 flags.AsFlags = append(flags.AsFlags, tc.ClangAsflags())
Colin Crossb6688262016-11-22 12:32:47 -0800300 flags.CppFlags = append([]string{"${config.CommonClangGlobalCppflags}"}, flags.CppFlags...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700301 flags.GlobalFlags = append(flags.GlobalFlags,
Colin Crossb98c8b02016-07-29 13:44:28 -0700302 tc.ClangCflags(),
303 "${config.CommonClangGlobalCflags}",
304 fmt.Sprintf("${config.%sClangGlobalCflags}", hod))
Colin Cross4d9c2d12016-07-29 12:48:20 -0700305 } else {
Colin Crossb6688262016-11-22 12:32:47 -0800306 flags.CppFlags = append([]string{"${config.CommonGlobalCppflags}"}, flags.CppFlags...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700307 flags.GlobalFlags = append(flags.GlobalFlags,
Colin Crossb98c8b02016-07-29 13:44:28 -0700308 tc.Cflags(),
309 "${config.CommonGlobalCflags}",
310 fmt.Sprintf("${config.%sGlobalCflags}", hod))
Colin Cross4d9c2d12016-07-29 12:48:20 -0700311 }
312
313 if Bool(ctx.AConfig().ProductVariables.Brillo) {
314 flags.GlobalFlags = append(flags.GlobalFlags, "-D__BRILLO__")
315 }
316
317 if ctx.Device() {
318 if Bool(compiler.Properties.Rtti) {
319 flags.CppFlags = append(flags.CppFlags, "-frtti")
320 } else {
321 flags.CppFlags = append(flags.CppFlags, "-fno-rtti")
322 }
323 }
324
325 flags.AsFlags = append(flags.AsFlags, "-D__ASSEMBLY__")
326
327 if flags.Clang {
Colin Crossb98c8b02016-07-29 13:44:28 -0700328 flags.CppFlags = append(flags.CppFlags, tc.ClangCppflags())
Colin Cross4d9c2d12016-07-29 12:48:20 -0700329 } else {
Colin Crossb98c8b02016-07-29 13:44:28 -0700330 flags.CppFlags = append(flags.CppFlags, tc.Cppflags())
Colin Cross4d9c2d12016-07-29 12:48:20 -0700331 }
Colin Cross91e90042016-12-02 17:13:24 -0800332
333 flags.YasmFlags = append(flags.YasmFlags, tc.YasmFlags())
Colin Cross4d9c2d12016-07-29 12:48:20 -0700334 }
335
336 if flags.Clang {
Colin Crossb98c8b02016-07-29 13:44:28 -0700337 flags.GlobalFlags = append(flags.GlobalFlags, tc.ToolchainClangCflags())
Colin Cross4d9c2d12016-07-29 12:48:20 -0700338 } else {
Colin Crossb98c8b02016-07-29 13:44:28 -0700339 flags.GlobalFlags = append(flags.GlobalFlags, tc.ToolchainCflags())
Colin Cross4d9c2d12016-07-29 12:48:20 -0700340 }
341
342 if !ctx.sdk() {
Colin Cross6f6a4282016-10-17 14:19:06 -0700343 cStd := config.CStdVersion
Dan Albert043833c2017-02-03 16:13:38 -0800344 if compiler.Properties.C_std == "experimental" {
345 cStd = config.ExperimentalCStdVersion
346 } else if compiler.Properties.C_std != "" {
347 cStd = compiler.Properties.C_std
348 }
349
Colin Cross6f6a4282016-10-17 14:19:06 -0700350 cppStd := config.CppStdVersion
Dan Albert043833c2017-02-03 16:13:38 -0800351 if compiler.Properties.Cpp_std == "experimental" {
352 cppStd = config.ExperimentalCppStdVersion
353 } else if compiler.Properties.Cpp_std != "" {
354 cppStd = compiler.Properties.Cpp_std
355 }
Colin Cross6f6a4282016-10-17 14:19:06 -0700356
357 if !flags.Clang {
358 // GCC uses an invalid C++14 ABI (emits calls to
359 // __cxa_throw_bad_array_length, which is not a valid C++ RT ABI).
360 // http://b/25022512
361 cppStd = config.GccCppStdVersion
362 } else if ctx.Host() && !flags.Clang {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700363 // The host GCC doesn't support C++14 (and is deprecated, so likely
364 // never will). Build these modules with C++11.
Colin Cross6f6a4282016-10-17 14:19:06 -0700365 cppStd = config.GccCppStdVersion
Colin Cross4d9c2d12016-07-29 12:48:20 -0700366 }
Colin Cross6f6a4282016-10-17 14:19:06 -0700367
Colin Cross948f0cb2016-10-17 14:24:56 -0700368 if compiler.Properties.Gnu_extensions != nil && *compiler.Properties.Gnu_extensions == false {
369 cStd = gnuToCReplacer.Replace(cStd)
370 cppStd = gnuToCReplacer.Replace(cppStd)
371 }
372
Colin Cross6f6a4282016-10-17 14:19:06 -0700373 flags.ConlyFlags = append([]string{"-std=" + cStd}, flags.ConlyFlags...)
374 flags.CppFlags = append([]string{"-std=" + cppStd}, flags.CppFlags...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700375 }
376
377 // We can enforce some rules more strictly in the code we own. strict
378 // indicates if this is code that we can be stricter with. If we have
379 // rules that we want to apply to *our* code (but maybe can't for
380 // vendor/device specific things), we could extend this to be a ternary
381 // value.
382 strict := true
383 if strings.HasPrefix(android.PathForModuleSrc(ctx).String(), "external/") {
384 strict = false
385 }
386
387 // Can be used to make some annotations stricter for code we can fix
388 // (such as when we mark functions as deprecated).
389 if strict {
390 flags.CFlags = append(flags.CFlags, "-DANDROID_STRICT")
391 }
392
Dan Willemsene1a3ce32016-11-02 20:44:08 -0700393 if compiler.hasSrcExt(".proto") {
Colin Cross0c461f12016-10-20 16:11:43 -0700394 flags = protoFlags(ctx, flags, &compiler.Proto)
395 }
396
Dan Willemsene1a3ce32016-11-02 20:44:08 -0700397 if compiler.hasSrcExt(".y") || compiler.hasSrcExt(".yy") {
398 flags.GlobalFlags = append(flags.GlobalFlags,
399 "-I"+android.PathForModuleGen(ctx, "yacc", ctx.ModuleDir()).String())
400 }
401
Dan Willemsene1240db2016-11-03 14:28:51 -0700402 if compiler.hasSrcExt(".aidl") {
403 if len(compiler.Properties.Aidl.Local_include_dirs) > 0 {
404 localAidlIncludeDirs := android.PathsForModuleSrc(ctx, compiler.Properties.Aidl.Local_include_dirs)
405 flags.aidlFlags = append(flags.aidlFlags, includeDirsToFlags(localAidlIncludeDirs))
406 }
407 if len(compiler.Properties.Aidl.Include_dirs) > 0 {
408 rootAidlIncludeDirs := android.PathsForSource(ctx, compiler.Properties.Aidl.Include_dirs)
409 flags.aidlFlags = append(flags.aidlFlags, includeDirsToFlags(rootAidlIncludeDirs))
410 }
411
412 flags.GlobalFlags = append(flags.GlobalFlags,
413 "-I"+android.PathForModuleGen(ctx, "aidl").String())
414 }
415
Colin Cross4d9c2d12016-07-29 12:48:20 -0700416 return flags
417}
418
Dan Willemsene1a3ce32016-11-02 20:44:08 -0700419func (compiler *baseCompiler) hasSrcExt(ext string) bool {
Colin Cross0c461f12016-10-20 16:11:43 -0700420 for _, src := range compiler.Properties.Srcs {
Dan Willemsene1a3ce32016-11-02 20:44:08 -0700421 if filepath.Ext(src) == ext {
Colin Cross0c461f12016-10-20 16:11:43 -0700422 return true
423 }
424 }
425
426 return false
427}
428
Colin Cross948f0cb2016-10-17 14:24:56 -0700429var gnuToCReplacer = strings.NewReplacer("gnu", "c")
430
Colin Cross4d9c2d12016-07-29 12:48:20 -0700431func ndkPathDeps(ctx ModuleContext) android.Paths {
Dan Willemsenb916b802017-03-19 13:44:32 -0700432 if ctx.sdk() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700433 // The NDK sysroot timestamp file depends on all the NDK sysroot files
434 // (headers and libraries).
435 return android.Paths{getNdkSysrootTimestampFile(ctx)}
436 }
437 return nil
438}
439
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700440func (compiler *baseCompiler) compile(ctx ModuleContext, flags Flags, deps PathDeps) Objects {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700441 pathDeps := deps.GeneratedHeaders
442 pathDeps = append(pathDeps, ndkPathDeps(ctx)...)
Colin Cross2f336352016-10-26 10:03:47 -0700443
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700444 if ctx.vndk() {
445 compiler.Properties.Srcs = append(compiler.Properties.Srcs,
446 compiler.Properties.Target.Vendor.Srcs...)
447
448 compiler.Properties.Exclude_srcs = append(compiler.Properties.Exclude_srcs,
449 compiler.Properties.Target.Vendor.Exclude_srcs...)
450 }
451
Colin Cross2f336352016-10-26 10:03:47 -0700452 srcs := ctx.ExpandSources(compiler.Properties.Srcs, compiler.Properties.Exclude_srcs)
453 srcs = append(srcs, deps.GeneratedSources...)
454
455 buildFlags := flagsToBuilderFlags(flags)
456
457 srcs, genDeps := genSources(ctx, srcs, buildFlags)
458
459 pathDeps = append(pathDeps, genDeps...)
460 pathDeps = append(pathDeps, flags.CFlagsDeps...)
461
462 compiler.deps = pathDeps
463
Fabien Sanglardd61f1f42017-01-10 16:21:22 -0800464 // Save src, buildFlags and context
465 compiler.srcs = srcs
466
Colin Cross4d9c2d12016-07-29 12:48:20 -0700467 // Compile files listed in c.Properties.Srcs into objects
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700468 objs := compileObjs(ctx, buildFlags, "", srcs, compiler.deps)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700469
470 if ctx.Failed() {
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700471 return Objects{}
Colin Cross4d9c2d12016-07-29 12:48:20 -0700472 }
473
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700474 return objs
Colin Cross4d9c2d12016-07-29 12:48:20 -0700475}
476
477// Compile a list of source files into objects a specified subdirectory
Colin Cross2f336352016-10-26 10:03:47 -0700478func compileObjs(ctx android.ModuleContext, flags builderFlags,
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700479 subdir string, srcFiles, deps android.Paths) Objects {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700480
Colin Cross2f336352016-10-26 10:03:47 -0700481 return TransformSourceToObj(ctx, subdir, srcFiles, flags, deps)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700482}