blob: 8afd1bd215dd8437f7469ade6ab6be04676ac708 [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.
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).
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 Cross10d22312017-05-03 11:01:58 -0700128
129 // Stores the original list of source files before being cleared by library reuse
130 OriginalSrcs []string `blueprint:"mutated"`
Colin Cross4d9c2d12016-07-29 12:48:20 -0700131}
132
Colin Crossb916a382016-07-29 17:28:03 -0700133func NewBaseCompiler() *baseCompiler {
134 return &baseCompiler{}
135}
136
Colin Cross4d9c2d12016-07-29 12:48:20 -0700137type baseCompiler struct {
138 Properties BaseCompilerProperties
Colin Cross0c461f12016-10-20 16:11:43 -0700139 Proto ProtoProperties
Colin Cross2f336352016-10-26 10:03:47 -0700140 deps android.Paths
Fabien Sanglardd61f1f42017-01-10 16:21:22 -0800141 srcs android.Paths
142 flags builderFlags
Colin Cross4d9c2d12016-07-29 12:48:20 -0700143}
144
145var _ compiler = (*baseCompiler)(nil)
146
Fabien Sanglardd61f1f42017-01-10 16:21:22 -0800147type CompiledInterface interface {
148 Srcs() android.Paths
149}
150
151func (compiler *baseCompiler) Srcs() android.Paths {
152 return compiler.srcs
153}
154
Colin Cross4d9c2d12016-07-29 12:48:20 -0700155func (compiler *baseCompiler) appendCflags(flags []string) {
156 compiler.Properties.Cflags = append(compiler.Properties.Cflags, flags...)
157}
158
159func (compiler *baseCompiler) appendAsflags(flags []string) {
160 compiler.Properties.Asflags = append(compiler.Properties.Asflags, flags...)
161}
162
Colin Cross42742b82016-08-01 13:20:05 -0700163func (compiler *baseCompiler) compilerProps() []interface{} {
Colin Cross0feb1692016-11-03 14:38:52 -0700164 return []interface{}{&compiler.Properties, &compiler.Proto}
Colin Cross4d9c2d12016-07-29 12:48:20 -0700165}
166
Colin Cross42742b82016-08-01 13:20:05 -0700167func (compiler *baseCompiler) compilerInit(ctx BaseModuleContext) {}
Colin Cross4d9c2d12016-07-29 12:48:20 -0700168
Colin Cross37047f12016-12-13 17:06:13 -0800169func (compiler *baseCompiler) compilerDeps(ctx DepsContext, deps Deps) Deps {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700170 deps.GeneratedSources = append(deps.GeneratedSources, compiler.Properties.Generated_sources...)
171 deps.GeneratedHeaders = append(deps.GeneratedHeaders, compiler.Properties.Generated_headers...)
172
Colin Cross068e0fe2016-12-13 15:23:47 -0800173 android.ExtractSourcesDeps(ctx, compiler.Properties.Srcs)
174
Dan Willemsene1a3ce32016-11-02 20:44:08 -0700175 if compiler.hasSrcExt(".proto") {
Colin Cross0c461f12016-10-20 16:11:43 -0700176 deps = protoDeps(ctx, deps, &compiler.Proto)
177 }
178
Colin Cross4d9c2d12016-07-29 12:48:20 -0700179 return deps
180}
181
182// Create a Flags struct that collects the compile flags from global values,
183// per-target values, module type values, and per-module Blueprints properties
Colin Cross42742b82016-08-01 13:20:05 -0700184func (compiler *baseCompiler) compilerFlags(ctx ModuleContext, flags Flags) Flags {
Colin Crossb98c8b02016-07-29 13:44:28 -0700185 tc := ctx.toolchain()
Colin Cross4d9c2d12016-07-29 12:48:20 -0700186
187 CheckBadCompilerFlags(ctx, "cflags", compiler.Properties.Cflags)
188 CheckBadCompilerFlags(ctx, "cppflags", compiler.Properties.Cppflags)
189 CheckBadCompilerFlags(ctx, "conlyflags", compiler.Properties.Conlyflags)
190 CheckBadCompilerFlags(ctx, "asflags", compiler.Properties.Asflags)
191
Colin Cross4b963f82016-09-29 14:06:02 -0700192 esc := proptools.NinjaAndShellEscape
193
194 flags.CFlags = append(flags.CFlags, esc(compiler.Properties.Cflags)...)
195 flags.CppFlags = append(flags.CppFlags, esc(compiler.Properties.Cppflags)...)
196 flags.ConlyFlags = append(flags.ConlyFlags, esc(compiler.Properties.Conlyflags)...)
197 flags.AsFlags = append(flags.AsFlags, esc(compiler.Properties.Asflags)...)
Colin Cross91e90042016-12-02 17:13:24 -0800198 flags.YasmFlags = append(flags.YasmFlags, esc(compiler.Properties.Asflags)...)
Colin Cross4b963f82016-09-29 14:06:02 -0700199 flags.YaccFlags = append(flags.YaccFlags, esc(compiler.Properties.Yaccflags)...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700200
201 // Include dir cflags
Colin Cross4d9c2d12016-07-29 12:48:20 -0700202 localIncludeDirs := android.PathsForModuleSrc(ctx, compiler.Properties.Local_include_dirs)
Dan Willemsen273af7f2016-11-03 15:53:42 -0700203 if len(localIncludeDirs) > 0 {
Colin Crossdad8c952017-04-26 14:55:27 -0700204 f := includeDirsToFlags(localIncludeDirs)
205 flags.GlobalFlags = append(flags.GlobalFlags, f)
206 flags.YasmFlags = append(flags.YasmFlags, f)
Dan Willemsen273af7f2016-11-03 15:53:42 -0700207 }
208 rootIncludeDirs := android.PathsForSource(ctx, compiler.Properties.Include_dirs)
209 if len(rootIncludeDirs) > 0 {
Colin Crossdad8c952017-04-26 14:55:27 -0700210 f := includeDirsToFlags(rootIncludeDirs)
211 flags.GlobalFlags = append(flags.GlobalFlags, f)
212 flags.YasmFlags = append(flags.YasmFlags, f)
Dan Willemsen273af7f2016-11-03 15:53:42 -0700213 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700214
215 if !ctx.noDefaultCompilerFlags() {
Colin Crossc3199482017-03-30 15:03:04 -0700216 flags.GlobalFlags = append(flags.GlobalFlags, "-I"+android.PathForModuleSrc(ctx).String())
Colin Crossdad8c952017-04-26 14:55:27 -0700217 flags.YasmFlags = append(flags.YasmFlags, "-I"+android.PathForModuleSrc(ctx).String())
Colin Crossc3199482017-03-30 15:03:04 -0700218
Dan Willemsend2ede872016-11-18 14:54:24 -0800219 if !(ctx.sdk() || ctx.vndk()) || ctx.Host() {
Colin Crossc3199482017-03-30 15:03:04 -0700220 flags.SystemIncludeFlags = append(flags.SystemIncludeFlags,
Colin Crossb98c8b02016-07-29 13:44:28 -0700221 "${config.CommonGlobalIncludes}",
Colin Cross1cfd89a2016-09-15 09:30:46 -0700222 "${config.CommonGlobalSystemIncludes}",
Colin Crossb98c8b02016-07-29 13:44:28 -0700223 tc.IncludeFlags(),
224 "${config.CommonNativehelperInclude}")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700225 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700226 }
227
Dan Willemsenb916b802017-03-19 13:44:32 -0700228 if ctx.sdk() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700229 // The NDK headers are installed to a common sysroot. While a more
230 // typical Soong approach would be to only make the headers for the
231 // library you're using available, we're trying to emulate the NDK
232 // behavior here, and the NDK always has all the NDK headers available.
Colin Crossc3199482017-03-30 15:03:04 -0700233 flags.SystemIncludeFlags = append(flags.SystemIncludeFlags,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700234 "-isystem "+getCurrentIncludePath(ctx).String(),
Colin Crossb98c8b02016-07-29 13:44:28 -0700235 "-isystem "+getCurrentIncludePath(ctx).Join(ctx, tc.ClangTriple()).String())
Colin Cross4d9c2d12016-07-29 12:48:20 -0700236
237 // Traditionally this has come from android/api-level.h, but with the
238 // libc headers unified it must be set by the build system since we
239 // don't have per-API level copies of that header now.
Dan Albertebedf672016-11-08 15:06:22 -0800240 version := ctx.sdkVersion()
241 if version == "current" {
242 version = "__ANDROID_API_FUTURE__"
243 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700244 flags.GlobalFlags = append(flags.GlobalFlags,
Dan Albertebedf672016-11-08 15:06:22 -0800245 "-D__ANDROID_API__="+version)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700246
247 // Until the full NDK has been migrated to using ndk_headers, we still
248 // need to add the legacy sysroot includes to get the full set of
249 // headers.
250 legacyIncludes := fmt.Sprintf(
251 "prebuilts/ndk/current/platforms/android-%s/arch-%s/usr/include",
252 ctx.sdkVersion(), ctx.Arch().ArchType.String())
Colin Crossc3199482017-03-30 15:03:04 -0700253 flags.SystemIncludeFlags = append(flags.SystemIncludeFlags, "-isystem "+legacyIncludes)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700254 }
255
Dan Willemsenb916b802017-03-19 13:44:32 -0700256 if ctx.vndk() {
257 flags.GlobalFlags = append(flags.GlobalFlags,
258 "-D__ANDROID_API__=__ANDROID_API_FUTURE__", "-D__ANDROID_VNDK__")
259 }
260
Colin Cross4d9c2d12016-07-29 12:48:20 -0700261 instructionSet := compiler.Properties.Instruction_set
262 if flags.RequiredInstructionSet != "" {
263 instructionSet = flags.RequiredInstructionSet
264 }
Colin Crossb98c8b02016-07-29 13:44:28 -0700265 instructionSetFlags, err := tc.InstructionSetFlags(instructionSet)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700266 if flags.Clang {
Colin Crossb98c8b02016-07-29 13:44:28 -0700267 instructionSetFlags, err = tc.ClangInstructionSetFlags(instructionSet)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700268 }
269 if err != nil {
270 ctx.ModuleErrorf("%s", err)
271 }
272
273 CheckBadCompilerFlags(ctx, "release.cflags", compiler.Properties.Release.Cflags)
274
275 // TODO: debug
Colin Cross4b963f82016-09-29 14:06:02 -0700276 flags.CFlags = append(flags.CFlags, esc(compiler.Properties.Release.Cflags)...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700277
278 if flags.Clang {
279 CheckBadCompilerFlags(ctx, "clang_cflags", compiler.Properties.Clang_cflags)
280 CheckBadCompilerFlags(ctx, "clang_asflags", compiler.Properties.Clang_asflags)
281
Colin Crossb98c8b02016-07-29 13:44:28 -0700282 flags.CFlags = config.ClangFilterUnknownCflags(flags.CFlags)
Colin Cross4b963f82016-09-29 14:06:02 -0700283 flags.CFlags = append(flags.CFlags, esc(compiler.Properties.Clang_cflags)...)
284 flags.AsFlags = append(flags.AsFlags, esc(compiler.Properties.Clang_asflags)...)
Colin Crossb98c8b02016-07-29 13:44:28 -0700285 flags.CppFlags = config.ClangFilterUnknownCflags(flags.CppFlags)
286 flags.ConlyFlags = config.ClangFilterUnknownCflags(flags.ConlyFlags)
287 flags.LdFlags = config.ClangFilterUnknownCflags(flags.LdFlags)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700288
Colin Crossb98c8b02016-07-29 13:44:28 -0700289 target := "-target " + tc.ClangTriple()
Dan Willemsen300151b2017-03-13 12:40:30 -0700290 gccPrefix := "-B" + config.ToolPath(tc)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700291
292 flags.CFlags = append(flags.CFlags, target, gccPrefix)
293 flags.AsFlags = append(flags.AsFlags, target, gccPrefix)
294 flags.LdFlags = append(flags.LdFlags, target, gccPrefix)
295 }
296
Colin Crossb98c8b02016-07-29 13:44:28 -0700297 hod := "Host"
Colin Cross4d9c2d12016-07-29 12:48:20 -0700298 if ctx.Os().Class == android.Device {
Colin Crossb98c8b02016-07-29 13:44:28 -0700299 hod = "Device"
Colin Cross4d9c2d12016-07-29 12:48:20 -0700300 }
301
302 if !ctx.noDefaultCompilerFlags() {
303 flags.GlobalFlags = append(flags.GlobalFlags, instructionSetFlags)
Colin Crossb6688262016-11-22 12:32:47 -0800304 flags.ConlyFlags = append([]string{"${config.CommonGlobalConlyflags}"}, flags.ConlyFlags...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700305
306 if flags.Clang {
Colin Crossb98c8b02016-07-29 13:44:28 -0700307 flags.AsFlags = append(flags.AsFlags, tc.ClangAsflags())
Colin Crossb6688262016-11-22 12:32:47 -0800308 flags.CppFlags = append([]string{"${config.CommonClangGlobalCppflags}"}, flags.CppFlags...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700309 flags.GlobalFlags = append(flags.GlobalFlags,
Colin Crossb98c8b02016-07-29 13:44:28 -0700310 tc.ClangCflags(),
311 "${config.CommonClangGlobalCflags}",
312 fmt.Sprintf("${config.%sClangGlobalCflags}", hod))
Colin Cross4d9c2d12016-07-29 12:48:20 -0700313 } else {
Colin Crossb6688262016-11-22 12:32:47 -0800314 flags.CppFlags = append([]string{"${config.CommonGlobalCppflags}"}, flags.CppFlags...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700315 flags.GlobalFlags = append(flags.GlobalFlags,
Colin Crossb98c8b02016-07-29 13:44:28 -0700316 tc.Cflags(),
317 "${config.CommonGlobalCflags}",
318 fmt.Sprintf("${config.%sGlobalCflags}", hod))
Colin Cross4d9c2d12016-07-29 12:48:20 -0700319 }
320
321 if Bool(ctx.AConfig().ProductVariables.Brillo) {
322 flags.GlobalFlags = append(flags.GlobalFlags, "-D__BRILLO__")
323 }
324
325 if ctx.Device() {
326 if Bool(compiler.Properties.Rtti) {
327 flags.CppFlags = append(flags.CppFlags, "-frtti")
328 } else {
329 flags.CppFlags = append(flags.CppFlags, "-fno-rtti")
330 }
331 }
332
333 flags.AsFlags = append(flags.AsFlags, "-D__ASSEMBLY__")
334
335 if flags.Clang {
Colin Crossb98c8b02016-07-29 13:44:28 -0700336 flags.CppFlags = append(flags.CppFlags, tc.ClangCppflags())
Colin Cross4d9c2d12016-07-29 12:48:20 -0700337 } else {
Colin Crossb98c8b02016-07-29 13:44:28 -0700338 flags.CppFlags = append(flags.CppFlags, tc.Cppflags())
Colin Cross4d9c2d12016-07-29 12:48:20 -0700339 }
Colin Cross91e90042016-12-02 17:13:24 -0800340
341 flags.YasmFlags = append(flags.YasmFlags, tc.YasmFlags())
Colin Cross4d9c2d12016-07-29 12:48:20 -0700342 }
343
344 if flags.Clang {
Colin Crossb98c8b02016-07-29 13:44:28 -0700345 flags.GlobalFlags = append(flags.GlobalFlags, tc.ToolchainClangCflags())
Colin Cross4d9c2d12016-07-29 12:48:20 -0700346 } else {
Colin Crossb98c8b02016-07-29 13:44:28 -0700347 flags.GlobalFlags = append(flags.GlobalFlags, tc.ToolchainCflags())
Colin Cross4d9c2d12016-07-29 12:48:20 -0700348 }
349
350 if !ctx.sdk() {
Colin Cross6f6a4282016-10-17 14:19:06 -0700351 cStd := config.CStdVersion
Dan Albert043833c2017-02-03 16:13:38 -0800352 if compiler.Properties.C_std == "experimental" {
353 cStd = config.ExperimentalCStdVersion
354 } else if compiler.Properties.C_std != "" {
355 cStd = compiler.Properties.C_std
356 }
357
Colin Cross6f6a4282016-10-17 14:19:06 -0700358 cppStd := config.CppStdVersion
Dan Albert043833c2017-02-03 16:13:38 -0800359 if compiler.Properties.Cpp_std == "experimental" {
360 cppStd = config.ExperimentalCppStdVersion
361 } else if compiler.Properties.Cpp_std != "" {
362 cppStd = compiler.Properties.Cpp_std
363 }
Colin Cross6f6a4282016-10-17 14:19:06 -0700364
365 if !flags.Clang {
366 // GCC uses an invalid C++14 ABI (emits calls to
367 // __cxa_throw_bad_array_length, which is not a valid C++ RT ABI).
368 // http://b/25022512
369 cppStd = config.GccCppStdVersion
370 } else if ctx.Host() && !flags.Clang {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700371 // The host GCC doesn't support C++14 (and is deprecated, so likely
372 // never will). Build these modules with C++11.
Colin Cross6f6a4282016-10-17 14:19:06 -0700373 cppStd = config.GccCppStdVersion
Colin Cross4d9c2d12016-07-29 12:48:20 -0700374 }
Colin Cross6f6a4282016-10-17 14:19:06 -0700375
Colin Cross948f0cb2016-10-17 14:24:56 -0700376 if compiler.Properties.Gnu_extensions != nil && *compiler.Properties.Gnu_extensions == false {
377 cStd = gnuToCReplacer.Replace(cStd)
378 cppStd = gnuToCReplacer.Replace(cppStd)
379 }
380
Colin Cross6f6a4282016-10-17 14:19:06 -0700381 flags.ConlyFlags = append([]string{"-std=" + cStd}, flags.ConlyFlags...)
382 flags.CppFlags = append([]string{"-std=" + cppStd}, flags.CppFlags...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700383 }
384
385 // We can enforce some rules more strictly in the code we own. strict
386 // indicates if this is code that we can be stricter with. If we have
387 // rules that we want to apply to *our* code (but maybe can't for
388 // vendor/device specific things), we could extend this to be a ternary
389 // value.
390 strict := true
391 if strings.HasPrefix(android.PathForModuleSrc(ctx).String(), "external/") {
392 strict = false
393 }
394
395 // Can be used to make some annotations stricter for code we can fix
396 // (such as when we mark functions as deprecated).
397 if strict {
398 flags.CFlags = append(flags.CFlags, "-DANDROID_STRICT")
399 }
400
Dan Willemsene1a3ce32016-11-02 20:44:08 -0700401 if compiler.hasSrcExt(".proto") {
Colin Cross0c461f12016-10-20 16:11:43 -0700402 flags = protoFlags(ctx, flags, &compiler.Proto)
403 }
404
Dan Willemsene1a3ce32016-11-02 20:44:08 -0700405 if compiler.hasSrcExt(".y") || compiler.hasSrcExt(".yy") {
406 flags.GlobalFlags = append(flags.GlobalFlags,
407 "-I"+android.PathForModuleGen(ctx, "yacc", ctx.ModuleDir()).String())
408 }
409
Dan Willemsene1240db2016-11-03 14:28:51 -0700410 if compiler.hasSrcExt(".aidl") {
411 if len(compiler.Properties.Aidl.Local_include_dirs) > 0 {
412 localAidlIncludeDirs := android.PathsForModuleSrc(ctx, compiler.Properties.Aidl.Local_include_dirs)
413 flags.aidlFlags = append(flags.aidlFlags, includeDirsToFlags(localAidlIncludeDirs))
414 }
415 if len(compiler.Properties.Aidl.Include_dirs) > 0 {
416 rootAidlIncludeDirs := android.PathsForSource(ctx, compiler.Properties.Aidl.Include_dirs)
417 flags.aidlFlags = append(flags.aidlFlags, includeDirsToFlags(rootAidlIncludeDirs))
418 }
419
420 flags.GlobalFlags = append(flags.GlobalFlags,
421 "-I"+android.PathForModuleGen(ctx, "aidl").String())
422 }
423
Colin Cross4d9c2d12016-07-29 12:48:20 -0700424 return flags
425}
426
Dan Willemsene1a3ce32016-11-02 20:44:08 -0700427func (compiler *baseCompiler) hasSrcExt(ext string) bool {
Colin Cross0c461f12016-10-20 16:11:43 -0700428 for _, src := range compiler.Properties.Srcs {
Dan Willemsene1a3ce32016-11-02 20:44:08 -0700429 if filepath.Ext(src) == ext {
Colin Cross0c461f12016-10-20 16:11:43 -0700430 return true
431 }
432 }
Colin Cross10d22312017-05-03 11:01:58 -0700433 for _, src := range compiler.Properties.OriginalSrcs {
434 if filepath.Ext(src) == ext {
435 return true
436 }
437 }
Colin Cross0c461f12016-10-20 16:11:43 -0700438
439 return false
440}
441
Colin Cross948f0cb2016-10-17 14:24:56 -0700442var gnuToCReplacer = strings.NewReplacer("gnu", "c")
443
Colin Cross4d9c2d12016-07-29 12:48:20 -0700444func ndkPathDeps(ctx ModuleContext) android.Paths {
Dan Willemsenb916b802017-03-19 13:44:32 -0700445 if ctx.sdk() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700446 // The NDK sysroot timestamp file depends on all the NDK sysroot files
447 // (headers and libraries).
448 return android.Paths{getNdkSysrootTimestampFile(ctx)}
449 }
450 return nil
451}
452
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700453func (compiler *baseCompiler) compile(ctx ModuleContext, flags Flags, deps PathDeps) Objects {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700454 pathDeps := deps.GeneratedHeaders
455 pathDeps = append(pathDeps, ndkPathDeps(ctx)...)
Colin Cross2f336352016-10-26 10:03:47 -0700456
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700457 if ctx.vndk() {
458 compiler.Properties.Srcs = append(compiler.Properties.Srcs,
459 compiler.Properties.Target.Vendor.Srcs...)
460
461 compiler.Properties.Exclude_srcs = append(compiler.Properties.Exclude_srcs,
462 compiler.Properties.Target.Vendor.Exclude_srcs...)
463 }
464
Colin Cross2f336352016-10-26 10:03:47 -0700465 srcs := ctx.ExpandSources(compiler.Properties.Srcs, compiler.Properties.Exclude_srcs)
466 srcs = append(srcs, deps.GeneratedSources...)
467
468 buildFlags := flagsToBuilderFlags(flags)
469
470 srcs, genDeps := genSources(ctx, srcs, buildFlags)
471
472 pathDeps = append(pathDeps, genDeps...)
473 pathDeps = append(pathDeps, flags.CFlagsDeps...)
474
475 compiler.deps = pathDeps
476
Fabien Sanglardd61f1f42017-01-10 16:21:22 -0800477 // Save src, buildFlags and context
478 compiler.srcs = srcs
479
Colin Cross4d9c2d12016-07-29 12:48:20 -0700480 // Compile files listed in c.Properties.Srcs into objects
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700481 objs := compileObjs(ctx, buildFlags, "", srcs, compiler.deps)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700482
483 if ctx.Failed() {
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700484 return Objects{}
Colin Cross4d9c2d12016-07-29 12:48:20 -0700485 }
486
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700487 return objs
Colin Cross4d9c2d12016-07-29 12:48:20 -0700488}
489
490// Compile a list of source files into objects a specified subdirectory
Colin Cross2f336352016-10-26 10:03:47 -0700491func compileObjs(ctx android.ModuleContext, flags builderFlags,
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700492 subdir string, srcFiles, deps android.Paths) Objects {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700493
Colin Cross2f336352016-10-26 10:03:47 -0700494 return TransformSourceToObj(ctx, subdir, srcFiles, flags, deps)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700495}