blob: 0a2d7bd7d6017a2c00d072b0e2c3ab1a07f79d1d [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).
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 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
146 Static bool `android:"arch_variant"`
147 } `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 srcs android.Paths
162 flags builderFlags
Colin Cross4d9c2d12016-07-29 12:48:20 -0700163}
164
165var _ compiler = (*baseCompiler)(nil)
166
Fabien Sanglardd61f1f42017-01-10 16:21:22 -0800167type CompiledInterface interface {
168 Srcs() android.Paths
169}
170
171func (compiler *baseCompiler) Srcs() android.Paths {
172 return compiler.srcs
173}
174
Colin Cross4d9c2d12016-07-29 12:48:20 -0700175func (compiler *baseCompiler) appendCflags(flags []string) {
176 compiler.Properties.Cflags = append(compiler.Properties.Cflags, flags...)
177}
178
179func (compiler *baseCompiler) appendAsflags(flags []string) {
180 compiler.Properties.Asflags = append(compiler.Properties.Asflags, flags...)
181}
182
Colin Cross42742b82016-08-01 13:20:05 -0700183func (compiler *baseCompiler) compilerProps() []interface{} {
Colin Cross0feb1692016-11-03 14:38:52 -0700184 return []interface{}{&compiler.Properties, &compiler.Proto}
Colin Cross4d9c2d12016-07-29 12:48:20 -0700185}
186
Colin Cross42742b82016-08-01 13:20:05 -0700187func (compiler *baseCompiler) compilerInit(ctx BaseModuleContext) {}
Colin Cross4d9c2d12016-07-29 12:48:20 -0700188
Colin Cross37047f12016-12-13 17:06:13 -0800189func (compiler *baseCompiler) compilerDeps(ctx DepsContext, deps Deps) Deps {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700190 deps.GeneratedSources = append(deps.GeneratedSources, compiler.Properties.Generated_sources...)
191 deps.GeneratedHeaders = append(deps.GeneratedHeaders, compiler.Properties.Generated_headers...)
192
Colin Cross068e0fe2016-12-13 15:23:47 -0800193 android.ExtractSourcesDeps(ctx, compiler.Properties.Srcs)
194
Dan Willemsene1a3ce32016-11-02 20:44:08 -0700195 if compiler.hasSrcExt(".proto") {
Colin Cross38f794e2017-09-07 10:53:07 -0700196 deps = protoDeps(ctx, deps, &compiler.Proto, compiler.Properties.Proto.Static)
Colin Cross0c461f12016-10-20 16:11:43 -0700197 }
198
Colin Cross4d9c2d12016-07-29 12:48:20 -0700199 return deps
200}
201
202// Create a Flags struct that collects the compile flags from global values,
203// per-target values, module type values, and per-module Blueprints properties
Colin Cross42742b82016-08-01 13:20:05 -0700204func (compiler *baseCompiler) compilerFlags(ctx ModuleContext, flags Flags) Flags {
Colin Crossb98c8b02016-07-29 13:44:28 -0700205 tc := ctx.toolchain()
Colin Cross4d9c2d12016-07-29 12:48:20 -0700206
207 CheckBadCompilerFlags(ctx, "cflags", compiler.Properties.Cflags)
208 CheckBadCompilerFlags(ctx, "cppflags", compiler.Properties.Cppflags)
209 CheckBadCompilerFlags(ctx, "conlyflags", compiler.Properties.Conlyflags)
210 CheckBadCompilerFlags(ctx, "asflags", compiler.Properties.Asflags)
211
Colin Cross4b963f82016-09-29 14:06:02 -0700212 esc := proptools.NinjaAndShellEscape
213
214 flags.CFlags = append(flags.CFlags, esc(compiler.Properties.Cflags)...)
215 flags.CppFlags = append(flags.CppFlags, esc(compiler.Properties.Cppflags)...)
216 flags.ConlyFlags = append(flags.ConlyFlags, esc(compiler.Properties.Conlyflags)...)
217 flags.AsFlags = append(flags.AsFlags, esc(compiler.Properties.Asflags)...)
Colin Cross91e90042016-12-02 17:13:24 -0800218 flags.YasmFlags = append(flags.YasmFlags, esc(compiler.Properties.Asflags)...)
Colin Cross4b963f82016-09-29 14:06:02 -0700219 flags.YaccFlags = append(flags.YaccFlags, esc(compiler.Properties.Yaccflags)...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700220
221 // Include dir cflags
Colin Cross4d9c2d12016-07-29 12:48:20 -0700222 localIncludeDirs := android.PathsForModuleSrc(ctx, compiler.Properties.Local_include_dirs)
Dan Willemsen273af7f2016-11-03 15:53:42 -0700223 if len(localIncludeDirs) > 0 {
Colin Crossdad8c952017-04-26 14:55:27 -0700224 f := includeDirsToFlags(localIncludeDirs)
225 flags.GlobalFlags = append(flags.GlobalFlags, f)
226 flags.YasmFlags = append(flags.YasmFlags, f)
Dan Willemsen273af7f2016-11-03 15:53:42 -0700227 }
228 rootIncludeDirs := android.PathsForSource(ctx, compiler.Properties.Include_dirs)
229 if len(rootIncludeDirs) > 0 {
Colin Crossdad8c952017-04-26 14:55:27 -0700230 f := includeDirsToFlags(rootIncludeDirs)
231 flags.GlobalFlags = append(flags.GlobalFlags, f)
232 flags.YasmFlags = append(flags.YasmFlags, f)
Dan Willemsen273af7f2016-11-03 15:53:42 -0700233 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700234
235 if !ctx.noDefaultCompilerFlags() {
Colin Crossc3199482017-03-30 15:03:04 -0700236 flags.GlobalFlags = append(flags.GlobalFlags, "-I"+android.PathForModuleSrc(ctx).String())
Colin Crossdad8c952017-04-26 14:55:27 -0700237 flags.YasmFlags = append(flags.YasmFlags, "-I"+android.PathForModuleSrc(ctx).String())
Colin Crossc3199482017-03-30 15:03:04 -0700238
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700239 if !(ctx.useSdk() || ctx.useVndk()) || ctx.Host() {
Colin Crossc3199482017-03-30 15:03:04 -0700240 flags.SystemIncludeFlags = append(flags.SystemIncludeFlags,
Colin Crossb98c8b02016-07-29 13:44:28 -0700241 "${config.CommonGlobalIncludes}",
242 tc.IncludeFlags(),
243 "${config.CommonNativehelperInclude}")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700244 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700245 }
246
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700247 if ctx.useSdk() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700248 // The NDK headers are installed to a common sysroot. While a more
249 // typical Soong approach would be to only make the headers for the
250 // library you're using available, we're trying to emulate the NDK
251 // behavior here, and the NDK always has all the NDK headers available.
Colin Crossc3199482017-03-30 15:03:04 -0700252 flags.SystemIncludeFlags = append(flags.SystemIncludeFlags,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700253 "-isystem "+getCurrentIncludePath(ctx).String(),
Colin Crossb98c8b02016-07-29 13:44:28 -0700254 "-isystem "+getCurrentIncludePath(ctx).Join(ctx, tc.ClangTriple()).String())
Colin Cross4d9c2d12016-07-29 12:48:20 -0700255
256 // Traditionally this has come from android/api-level.h, but with the
257 // libc headers unified it must be set by the build system since we
258 // don't have per-API level copies of that header now.
Dan Albertebedf672016-11-08 15:06:22 -0800259 version := ctx.sdkVersion()
260 if version == "current" {
261 version = "__ANDROID_API_FUTURE__"
262 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700263 flags.GlobalFlags = append(flags.GlobalFlags,
Dan Albertebedf672016-11-08 15:06:22 -0800264 "-D__ANDROID_API__="+version)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700265
266 // Until the full NDK has been migrated to using ndk_headers, we still
267 // need to add the legacy sysroot includes to get the full set of
268 // headers.
269 legacyIncludes := fmt.Sprintf(
270 "prebuilts/ndk/current/platforms/android-%s/arch-%s/usr/include",
271 ctx.sdkVersion(), ctx.Arch().ArchType.String())
Colin Crossc3199482017-03-30 15:03:04 -0700272 flags.SystemIncludeFlags = append(flags.SystemIncludeFlags, "-isystem "+legacyIncludes)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700273 }
274
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700275 if ctx.useVndk() {
Dan Willemsenb916b802017-03-19 13:44:32 -0700276 flags.GlobalFlags = append(flags.GlobalFlags,
277 "-D__ANDROID_API__=__ANDROID_API_FUTURE__", "-D__ANDROID_VNDK__")
278 }
279
Dan Willemsen5922f3c2017-10-25 15:20:50 -0700280 instructionSet := proptools.String(compiler.Properties.Instruction_set)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700281 if flags.RequiredInstructionSet != "" {
282 instructionSet = flags.RequiredInstructionSet
283 }
Colin Crossb98c8b02016-07-29 13:44:28 -0700284 instructionSetFlags, err := tc.InstructionSetFlags(instructionSet)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700285 if flags.Clang {
Colin Crossb98c8b02016-07-29 13:44:28 -0700286 instructionSetFlags, err = tc.ClangInstructionSetFlags(instructionSet)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700287 }
288 if err != nil {
289 ctx.ModuleErrorf("%s", err)
290 }
291
292 CheckBadCompilerFlags(ctx, "release.cflags", compiler.Properties.Release.Cflags)
293
294 // TODO: debug
Colin Cross4b963f82016-09-29 14:06:02 -0700295 flags.CFlags = append(flags.CFlags, esc(compiler.Properties.Release.Cflags)...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700296
297 if flags.Clang {
298 CheckBadCompilerFlags(ctx, "clang_cflags", compiler.Properties.Clang_cflags)
299 CheckBadCompilerFlags(ctx, "clang_asflags", compiler.Properties.Clang_asflags)
300
Colin Crossb98c8b02016-07-29 13:44:28 -0700301 flags.CFlags = config.ClangFilterUnknownCflags(flags.CFlags)
Colin Cross4b963f82016-09-29 14:06:02 -0700302 flags.CFlags = append(flags.CFlags, esc(compiler.Properties.Clang_cflags)...)
303 flags.AsFlags = append(flags.AsFlags, esc(compiler.Properties.Clang_asflags)...)
Colin Crossb98c8b02016-07-29 13:44:28 -0700304 flags.CppFlags = config.ClangFilterUnknownCflags(flags.CppFlags)
305 flags.ConlyFlags = config.ClangFilterUnknownCflags(flags.ConlyFlags)
306 flags.LdFlags = config.ClangFilterUnknownCflags(flags.LdFlags)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700307
Colin Crossb98c8b02016-07-29 13:44:28 -0700308 target := "-target " + tc.ClangTriple()
Dan Willemsen300151b2017-03-13 12:40:30 -0700309 gccPrefix := "-B" + config.ToolPath(tc)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700310
311 flags.CFlags = append(flags.CFlags, target, gccPrefix)
312 flags.AsFlags = append(flags.AsFlags, target, gccPrefix)
313 flags.LdFlags = append(flags.LdFlags, target, gccPrefix)
314 }
315
Colin Crossb98c8b02016-07-29 13:44:28 -0700316 hod := "Host"
Colin Cross4d9c2d12016-07-29 12:48:20 -0700317 if ctx.Os().Class == android.Device {
Colin Crossb98c8b02016-07-29 13:44:28 -0700318 hod = "Device"
Colin Cross4d9c2d12016-07-29 12:48:20 -0700319 }
320
321 if !ctx.noDefaultCompilerFlags() {
322 flags.GlobalFlags = append(flags.GlobalFlags, instructionSetFlags)
Colin Crossb6688262016-11-22 12:32:47 -0800323 flags.ConlyFlags = append([]string{"${config.CommonGlobalConlyflags}"}, flags.ConlyFlags...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700324
325 if flags.Clang {
Colin Crossb98c8b02016-07-29 13:44:28 -0700326 flags.AsFlags = append(flags.AsFlags, tc.ClangAsflags())
Colin Crossb6688262016-11-22 12:32:47 -0800327 flags.CppFlags = append([]string{"${config.CommonClangGlobalCppflags}"}, flags.CppFlags...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700328 flags.GlobalFlags = append(flags.GlobalFlags,
Colin Crossb98c8b02016-07-29 13:44:28 -0700329 tc.ClangCflags(),
330 "${config.CommonClangGlobalCflags}",
331 fmt.Sprintf("${config.%sClangGlobalCflags}", hod))
Colin Cross4d9c2d12016-07-29 12:48:20 -0700332 } else {
Colin Crossb6688262016-11-22 12:32:47 -0800333 flags.CppFlags = append([]string{"${config.CommonGlobalCppflags}"}, flags.CppFlags...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700334 flags.GlobalFlags = append(flags.GlobalFlags,
Colin Crossb98c8b02016-07-29 13:44:28 -0700335 tc.Cflags(),
336 "${config.CommonGlobalCflags}",
337 fmt.Sprintf("${config.%sGlobalCflags}", hod))
Colin Cross4d9c2d12016-07-29 12:48:20 -0700338 }
339
340 if Bool(ctx.AConfig().ProductVariables.Brillo) {
341 flags.GlobalFlags = append(flags.GlobalFlags, "-D__BRILLO__")
342 }
343
344 if ctx.Device() {
345 if Bool(compiler.Properties.Rtti) {
346 flags.CppFlags = append(flags.CppFlags, "-frtti")
347 } else {
348 flags.CppFlags = append(flags.CppFlags, "-fno-rtti")
349 }
350 }
351
352 flags.AsFlags = append(flags.AsFlags, "-D__ASSEMBLY__")
353
354 if flags.Clang {
Colin Crossb98c8b02016-07-29 13:44:28 -0700355 flags.CppFlags = append(flags.CppFlags, tc.ClangCppflags())
Colin Cross4d9c2d12016-07-29 12:48:20 -0700356 } else {
Colin Crossb98c8b02016-07-29 13:44:28 -0700357 flags.CppFlags = append(flags.CppFlags, tc.Cppflags())
Colin Cross4d9c2d12016-07-29 12:48:20 -0700358 }
Colin Cross91e90042016-12-02 17:13:24 -0800359
360 flags.YasmFlags = append(flags.YasmFlags, tc.YasmFlags())
Colin Cross4d9c2d12016-07-29 12:48:20 -0700361 }
362
363 if flags.Clang {
Colin Crossb98c8b02016-07-29 13:44:28 -0700364 flags.GlobalFlags = append(flags.GlobalFlags, tc.ToolchainClangCflags())
Colin Cross4d9c2d12016-07-29 12:48:20 -0700365 } else {
Colin Crossb98c8b02016-07-29 13:44:28 -0700366 flags.GlobalFlags = append(flags.GlobalFlags, tc.ToolchainCflags())
Colin Cross4d9c2d12016-07-29 12:48:20 -0700367 }
368
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700369 if !ctx.useSdk() {
Colin Cross6f6a4282016-10-17 14:19:06 -0700370 cStd := config.CStdVersion
Dan Albert043833c2017-02-03 16:13:38 -0800371 if compiler.Properties.C_std == "experimental" {
372 cStd = config.ExperimentalCStdVersion
373 } else if compiler.Properties.C_std != "" {
374 cStd = compiler.Properties.C_std
375 }
376
Josh Gaof5720172017-06-23 14:56:50 -0700377 cppStd := compiler.Properties.Cpp_std
378 switch compiler.Properties.Cpp_std {
379 case "":
380 cppStd = config.CppStdVersion
381 case "experimental":
Dan Albert043833c2017-02-03 16:13:38 -0800382 cppStd = config.ExperimentalCppStdVersion
Josh Gaof5720172017-06-23 14:56:50 -0700383 case "c++17", "gnu++17":
384 // Map c++17 and gnu++17 to their 1z equivalents, until 17 is finalized.
385 cppStd = strings.Replace(compiler.Properties.Cpp_std, "17", "1z", 1)
Dan Albert043833c2017-02-03 16:13:38 -0800386 }
Colin Cross6f6a4282016-10-17 14:19:06 -0700387
388 if !flags.Clang {
389 // GCC uses an invalid C++14 ABI (emits calls to
390 // __cxa_throw_bad_array_length, which is not a valid C++ RT ABI).
391 // http://b/25022512
392 cppStd = config.GccCppStdVersion
393 } else if ctx.Host() && !flags.Clang {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700394 // The host GCC doesn't support C++14 (and is deprecated, so likely
395 // never will). Build these modules with C++11.
Colin Cross6f6a4282016-10-17 14:19:06 -0700396 cppStd = config.GccCppStdVersion
Colin Cross4d9c2d12016-07-29 12:48:20 -0700397 }
Colin Cross6f6a4282016-10-17 14:19:06 -0700398
Colin Cross948f0cb2016-10-17 14:24:56 -0700399 if compiler.Properties.Gnu_extensions != nil && *compiler.Properties.Gnu_extensions == false {
400 cStd = gnuToCReplacer.Replace(cStd)
401 cppStd = gnuToCReplacer.Replace(cppStd)
402 }
403
Colin Cross6f6a4282016-10-17 14:19:06 -0700404 flags.ConlyFlags = append([]string{"-std=" + cStd}, flags.ConlyFlags...)
405 flags.CppFlags = append([]string{"-std=" + cppStd}, flags.CppFlags...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700406 }
407
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700408 if ctx.useVndk() {
Jiyong Parka622de82017-08-10 13:33:27 +0900409 flags.CFlags = append(flags.CFlags, esc(compiler.Properties.Target.Vendor.Cflags)...)
410 }
411
Colin Cross4d9c2d12016-07-29 12:48:20 -0700412 // We can enforce some rules more strictly in the code we own. strict
413 // indicates if this is code that we can be stricter with. If we have
414 // rules that we want to apply to *our* code (but maybe can't for
415 // vendor/device specific things), we could extend this to be a ternary
416 // value.
417 strict := true
418 if strings.HasPrefix(android.PathForModuleSrc(ctx).String(), "external/") {
419 strict = false
420 }
421
422 // Can be used to make some annotations stricter for code we can fix
423 // (such as when we mark functions as deprecated).
424 if strict {
425 flags.CFlags = append(flags.CFlags, "-DANDROID_STRICT")
426 }
427
Dan Willemsene1a3ce32016-11-02 20:44:08 -0700428 if compiler.hasSrcExt(".proto") {
Colin Cross0c461f12016-10-20 16:11:43 -0700429 flags = protoFlags(ctx, flags, &compiler.Proto)
430 }
431
Dan Willemsene1a3ce32016-11-02 20:44:08 -0700432 if compiler.hasSrcExt(".y") || compiler.hasSrcExt(".yy") {
433 flags.GlobalFlags = append(flags.GlobalFlags,
434 "-I"+android.PathForModuleGen(ctx, "yacc", ctx.ModuleDir()).String())
435 }
436
Dan Willemsen4f1c3d42017-09-09 01:15:26 -0700437 if compiler.hasSrcExt(".mc") {
438 flags.GlobalFlags = append(flags.GlobalFlags,
439 "-I"+android.PathForModuleGen(ctx, "windmc", ctx.ModuleDir()).String())
440 }
441
Dan Willemsene1240db2016-11-03 14:28:51 -0700442 if compiler.hasSrcExt(".aidl") {
443 if len(compiler.Properties.Aidl.Local_include_dirs) > 0 {
444 localAidlIncludeDirs := android.PathsForModuleSrc(ctx, compiler.Properties.Aidl.Local_include_dirs)
445 flags.aidlFlags = append(flags.aidlFlags, includeDirsToFlags(localAidlIncludeDirs))
446 }
447 if len(compiler.Properties.Aidl.Include_dirs) > 0 {
448 rootAidlIncludeDirs := android.PathsForSource(ctx, compiler.Properties.Aidl.Include_dirs)
449 flags.aidlFlags = append(flags.aidlFlags, includeDirsToFlags(rootAidlIncludeDirs))
450 }
451
452 flags.GlobalFlags = append(flags.GlobalFlags,
453 "-I"+android.PathForModuleGen(ctx, "aidl").String())
454 }
455
Colin Cross2a252be2017-05-01 17:37:24 -0700456 if compiler.hasSrcExt(".rs") || compiler.hasSrcExt(".fs") {
457 flags = rsFlags(ctx, flags, &compiler.Properties)
458 }
459
Colin Cross4d9c2d12016-07-29 12:48:20 -0700460 return flags
461}
462
Dan Willemsene1a3ce32016-11-02 20:44:08 -0700463func (compiler *baseCompiler) hasSrcExt(ext string) bool {
Colin Cross0c461f12016-10-20 16:11:43 -0700464 for _, src := range compiler.Properties.Srcs {
Dan Willemsene1a3ce32016-11-02 20:44:08 -0700465 if filepath.Ext(src) == ext {
Colin Cross0c461f12016-10-20 16:11:43 -0700466 return true
467 }
468 }
Colin Cross10d22312017-05-03 11:01:58 -0700469 for _, src := range compiler.Properties.OriginalSrcs {
470 if filepath.Ext(src) == ext {
471 return true
472 }
473 }
Colin Cross0c461f12016-10-20 16:11:43 -0700474
475 return false
476}
477
Colin Cross948f0cb2016-10-17 14:24:56 -0700478var gnuToCReplacer = strings.NewReplacer("gnu", "c")
479
Colin Cross4d9c2d12016-07-29 12:48:20 -0700480func ndkPathDeps(ctx ModuleContext) android.Paths {
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700481 if ctx.useSdk() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700482 // The NDK sysroot timestamp file depends on all the NDK sysroot files
483 // (headers and libraries).
484 return android.Paths{getNdkSysrootTimestampFile(ctx)}
485 }
486 return nil
487}
488
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700489func (compiler *baseCompiler) compile(ctx ModuleContext, flags Flags, deps PathDeps) Objects {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700490 pathDeps := deps.GeneratedHeaders
491 pathDeps = append(pathDeps, ndkPathDeps(ctx)...)
Colin Cross2f336352016-10-26 10:03:47 -0700492
493 srcs := ctx.ExpandSources(compiler.Properties.Srcs, compiler.Properties.Exclude_srcs)
494 srcs = append(srcs, deps.GeneratedSources...)
495
496 buildFlags := flagsToBuilderFlags(flags)
497
498 srcs, genDeps := genSources(ctx, srcs, buildFlags)
499
500 pathDeps = append(pathDeps, genDeps...)
501 pathDeps = append(pathDeps, flags.CFlagsDeps...)
502
503 compiler.deps = pathDeps
504
Fabien Sanglardd61f1f42017-01-10 16:21:22 -0800505 // Save src, buildFlags and context
506 compiler.srcs = srcs
507
Colin Cross4d9c2d12016-07-29 12:48:20 -0700508 // Compile files listed in c.Properties.Srcs into objects
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700509 objs := compileObjs(ctx, buildFlags, "", srcs, compiler.deps)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700510
511 if ctx.Failed() {
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700512 return Objects{}
Colin Cross4d9c2d12016-07-29 12:48:20 -0700513 }
514
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700515 return objs
Colin Cross4d9c2d12016-07-29 12:48:20 -0700516}
517
518// Compile a list of source files into objects a specified subdirectory
Colin Cross2f336352016-10-26 10:03:47 -0700519func compileObjs(ctx android.ModuleContext, flags builderFlags,
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700520 subdir string, srcFiles, deps android.Paths) Objects {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700521
Colin Cross2f336352016-10-26 10:03:47 -0700522 return TransformSourceToObj(ctx, subdir, srcFiles, flags, deps)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700523}