blob: 63d2adee0341643f4322fa934987952cd2d9b4a7 [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
Dan Willemsen59339a22018-07-22 21:18:45 -070076 Local_include_dirs []string `android:"arch_variant,variant_prepend"`
Colin Cross4d9c2d12016-07-29 12:48:20 -070077
Dan Albert899c23e2018-12-06 11:04:03 -080078 // Add the directory containing the Android.bp file to the list of include
79 // directories. Defaults to true.
80 Include_build_directory *bool
81
Colin Cross4d9c2d12016-07-29 12:48:20 -070082 // list of generated sources to compile. These are the names of gensrcs or
83 // genrule modules.
84 Generated_sources []string `android:"arch_variant"`
85
86 // list of generated headers to add to the include path. These are the names
87 // of genrule modules.
88 Generated_headers []string `android:"arch_variant"`
89
90 // pass -frtti instead of -fno-rtti
91 Rtti *bool
92
Dan Albert043833c2017-02-03 16:13:38 -080093 // C standard version to use. Can be a specific version (such as "gnu11"),
94 // "experimental" (which will use draft versions like C1x when available),
95 // or the empty string (which will use the default).
Nan Zhang0007d812017-11-07 10:57:05 -080096 C_std *string
Dan Albert043833c2017-02-03 16:13:38 -080097
98 // C++ standard version to use. Can be a specific version (such as
99 // "gnu++11"), "experimental" (which will use draft versions like C++1z when
100 // available), or the empty string (which will use the default).
Nan Zhang0007d812017-11-07 10:57:05 -0800101 Cpp_std *string
Dan Albert043833c2017-02-03 16:13:38 -0800102
Colin Cross948f0cb2016-10-17 14:24:56 -0700103 // if set to false, use -std=c++* instead of -std=gnu++*
104 Gnu_extensions *bool
105
Dan Willemsene1240db2016-11-03 14:28:51 -0700106 Aidl struct {
107 // list of directories that will be added to the aidl include paths.
108 Include_dirs []string
109
110 // list of directories relative to the Blueprints file that will
111 // be added to the aidl include paths.
112 Local_include_dirs []string
Martijn Coeneneab15642018-03-09 09:29:59 +0100113
114 // whether to generate traces (for systrace) for this interface
115 Generate_traces *bool
Dan Willemsene1240db2016-11-03 14:28:51 -0700116 }
117
Colin Cross2a252be2017-05-01 17:37:24 -0700118 Renderscript struct {
119 // list of directories that will be added to the llvm-rs-cc include paths
120 Include_dirs []string
121
122 // list of flags that will be passed to llvm-rs-cc
123 Flags []string
124
125 // Renderscript API level to target
126 Target_api *string
127 }
128
Colin Cross4d9c2d12016-07-29 12:48:20 -0700129 Debug, Release struct {
130 // list of module-specific flags that will be used for C and C++ compiles in debug or
131 // release builds
132 Cflags []string `android:"arch_variant"`
133 } `android:"arch_variant"`
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700134
135 Target struct {
136 Vendor struct {
137 // list of source files that should only be used in the
138 // vendor variant of the C/C++ module.
139 Srcs []string
140
141 // list of source files that should not be used to
142 // build the vendor variant of the C/C++ module.
143 Exclude_srcs []string
Jiyong Parka622de82017-08-10 13:33:27 +0900144
145 // List of additional cflags that should be used to build the vendor
146 // variant of the C/C++ module.
147 Cflags []string
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700148 }
Jiyong Parkf9332f12018-02-01 00:54:12 +0900149 Recovery struct {
150 // list of source files that should only be used in the
151 // recovery variant of the C/C++ module.
152 Srcs []string
153
154 // list of source files that should not be used to
155 // build the recovery variant of the C/C++ module.
156 Exclude_srcs []string
157
158 // List of additional cflags that should be used to build the recovery
159 // variant of the C/C++ module.
160 Cflags []string
161 }
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700162 }
Colin Cross10d22312017-05-03 11:01:58 -0700163
Colin Cross38f794e2017-09-07 10:53:07 -0700164 Proto struct {
165 // Link statically against the protobuf runtime
Nan Zhang0007d812017-11-07 10:57:05 -0800166 Static *bool `android:"arch_variant"`
Colin Cross38f794e2017-09-07 10:53:07 -0700167 } `android:"arch_variant"`
168
Colin Cross10d22312017-05-03 11:01:58 -0700169 // Stores the original list of source files before being cleared by library reuse
170 OriginalSrcs []string `blueprint:"mutated"`
Pirama Arumuga Nainarfadb7b52017-12-19 23:08:09 -0800171
172 // Build and link with OpenMP
173 Openmp *bool `android:"arch_variant"`
Colin Cross4d9c2d12016-07-29 12:48:20 -0700174}
175
Colin Crossb916a382016-07-29 17:28:03 -0700176func NewBaseCompiler() *baseCompiler {
177 return &baseCompiler{}
178}
179
Colin Cross4d9c2d12016-07-29 12:48:20 -0700180type baseCompiler struct {
181 Properties BaseCompilerProperties
Colin Cross38f794e2017-09-07 10:53:07 -0700182 Proto android.ProtoProperties
Pirama Arumuga Nainarf231b192018-01-23 10:49:04 -0800183 cFlagsDeps android.Paths
Pirama Arumuga Nainar70ba5a32017-12-19 15:11:01 -0800184 pathDeps android.Paths
Fabien Sanglardd61f1f42017-01-10 16:21:22 -0800185 flags builderFlags
Colin Crossf18e1102017-11-16 14:33:08 -0800186
187 // Sources that were passed to the C/C++ compiler
188 srcs android.Paths
189
190 // Sources that were passed in the Android.bp file, including generated sources generated by
191 // other modules and filegroups. May include source files that have not yet been translated to
192 // C/C++ (.aidl, .proto, etc.)
193 srcsBeforeGen android.Paths
Colin Cross4d9c2d12016-07-29 12:48:20 -0700194}
195
196var _ compiler = (*baseCompiler)(nil)
197
Fabien Sanglardd61f1f42017-01-10 16:21:22 -0800198type CompiledInterface interface {
199 Srcs() android.Paths
200}
201
202func (compiler *baseCompiler) Srcs() android.Paths {
Nan Zhange42777a2018-03-27 16:19:42 -0700203 return append(android.Paths{}, compiler.srcs...)
Fabien Sanglardd61f1f42017-01-10 16:21:22 -0800204}
205
Colin Cross4d9c2d12016-07-29 12:48:20 -0700206func (compiler *baseCompiler) appendCflags(flags []string) {
207 compiler.Properties.Cflags = append(compiler.Properties.Cflags, flags...)
208}
209
210func (compiler *baseCompiler) appendAsflags(flags []string) {
211 compiler.Properties.Asflags = append(compiler.Properties.Asflags, flags...)
212}
213
Colin Cross42742b82016-08-01 13:20:05 -0700214func (compiler *baseCompiler) compilerProps() []interface{} {
Colin Cross0feb1692016-11-03 14:38:52 -0700215 return []interface{}{&compiler.Properties, &compiler.Proto}
Colin Cross4d9c2d12016-07-29 12:48:20 -0700216}
217
Colin Cross42742b82016-08-01 13:20:05 -0700218func (compiler *baseCompiler) compilerInit(ctx BaseModuleContext) {}
Colin Cross4d9c2d12016-07-29 12:48:20 -0700219
Colin Cross37047f12016-12-13 17:06:13 -0800220func (compiler *baseCompiler) compilerDeps(ctx DepsContext, deps Deps) Deps {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700221 deps.GeneratedSources = append(deps.GeneratedSources, compiler.Properties.Generated_sources...)
222 deps.GeneratedHeaders = append(deps.GeneratedHeaders, compiler.Properties.Generated_headers...)
223
Colin Cross068e0fe2016-12-13 15:23:47 -0800224 android.ExtractSourcesDeps(ctx, compiler.Properties.Srcs)
Nan Zhang27e284d2018-02-09 21:03:53 +0000225 android.ExtractSourcesDeps(ctx, compiler.Properties.Exclude_srcs)
Colin Cross068e0fe2016-12-13 15:23:47 -0800226
Dan Willemsene1a3ce32016-11-02 20:44:08 -0700227 if compiler.hasSrcExt(".proto") {
Nan Zhang0007d812017-11-07 10:57:05 -0800228 deps = protoDeps(ctx, deps, &compiler.Proto, Bool(compiler.Properties.Proto.Static))
Colin Cross0c461f12016-10-20 16:11:43 -0700229 }
230
Inseob Kim21f26902018-09-06 00:55:20 +0900231 if compiler.hasSrcExt(".sysprop") {
232 deps.SharedLibs = append(deps.SharedLibs, "libbase")
233 }
234
Pirama Arumuga Nainarfadb7b52017-12-19 23:08:09 -0800235 if Bool(compiler.Properties.Openmp) {
236 deps.StaticLibs = append(deps.StaticLibs, "libomp")
237 }
238
Colin Cross4d9c2d12016-07-29 12:48:20 -0700239 return deps
240}
241
Chih-Hung Hsieh64a38dc2017-11-14 14:09:14 -0800242// Return true if the module is in the WarningAllowedProjects.
243func warningsAreAllowed(subdir string) bool {
244 subdir += "/"
245 for _, prefix := range config.WarningAllowedProjects {
246 if strings.HasPrefix(subdir, prefix) {
247 return true
248 }
249 }
250 return false
251}
252
253func addToModuleList(ctx ModuleContext, list string, module string) {
Pirama Arumuga Nainar28316d42018-01-29 09:18:45 -0800254 getNamedMapForConfig(ctx.Config(), list).Store(module, true)
Chih-Hung Hsieh64a38dc2017-11-14 14:09:14 -0800255}
256
Colin Cross4d9c2d12016-07-29 12:48:20 -0700257// Create a Flags struct that collects the compile flags from global values,
258// per-target values, module type values, and per-module Blueprints properties
Colin Crossf18e1102017-11-16 14:33:08 -0800259func (compiler *baseCompiler) compilerFlags(ctx ModuleContext, flags Flags, deps PathDeps) Flags {
Colin Crossb98c8b02016-07-29 13:44:28 -0700260 tc := ctx.toolchain()
Colin Cross4d9c2d12016-07-29 12:48:20 -0700261
Colin Crossf18e1102017-11-16 14:33:08 -0800262 compiler.srcsBeforeGen = ctx.ExpandSources(compiler.Properties.Srcs, compiler.Properties.Exclude_srcs)
263 compiler.srcsBeforeGen = append(compiler.srcsBeforeGen, deps.GeneratedSources...)
264
Colin Cross4d9c2d12016-07-29 12:48:20 -0700265 CheckBadCompilerFlags(ctx, "cflags", compiler.Properties.Cflags)
266 CheckBadCompilerFlags(ctx, "cppflags", compiler.Properties.Cppflags)
267 CheckBadCompilerFlags(ctx, "conlyflags", compiler.Properties.Conlyflags)
268 CheckBadCompilerFlags(ctx, "asflags", compiler.Properties.Asflags)
Jiyong Park4a2dcb52018-05-24 16:44:14 +0900269 CheckBadCompilerFlags(ctx, "vendor.cflags", compiler.Properties.Target.Vendor.Cflags)
270 CheckBadCompilerFlags(ctx, "recovery.cflags", compiler.Properties.Target.Recovery.Cflags)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700271
Colin Cross4b963f82016-09-29 14:06:02 -0700272 esc := proptools.NinjaAndShellEscape
273
274 flags.CFlags = append(flags.CFlags, esc(compiler.Properties.Cflags)...)
275 flags.CppFlags = append(flags.CppFlags, esc(compiler.Properties.Cppflags)...)
276 flags.ConlyFlags = append(flags.ConlyFlags, esc(compiler.Properties.Conlyflags)...)
277 flags.AsFlags = append(flags.AsFlags, esc(compiler.Properties.Asflags)...)
Colin Cross91e90042016-12-02 17:13:24 -0800278 flags.YasmFlags = append(flags.YasmFlags, esc(compiler.Properties.Asflags)...)
Colin Cross4b963f82016-09-29 14:06:02 -0700279 flags.YaccFlags = append(flags.YaccFlags, esc(compiler.Properties.Yaccflags)...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700280
281 // Include dir cflags
Colin Cross4d9c2d12016-07-29 12:48:20 -0700282 localIncludeDirs := android.PathsForModuleSrc(ctx, compiler.Properties.Local_include_dirs)
Dan Willemsen273af7f2016-11-03 15:53:42 -0700283 if len(localIncludeDirs) > 0 {
Colin Crossdad8c952017-04-26 14:55:27 -0700284 f := includeDirsToFlags(localIncludeDirs)
285 flags.GlobalFlags = append(flags.GlobalFlags, f)
286 flags.YasmFlags = append(flags.YasmFlags, f)
Dan Willemsen273af7f2016-11-03 15:53:42 -0700287 }
288 rootIncludeDirs := android.PathsForSource(ctx, compiler.Properties.Include_dirs)
289 if len(rootIncludeDirs) > 0 {
Colin Crossdad8c952017-04-26 14:55:27 -0700290 f := includeDirsToFlags(rootIncludeDirs)
291 flags.GlobalFlags = append(flags.GlobalFlags, f)
292 flags.YasmFlags = append(flags.YasmFlags, f)
Dan Willemsen273af7f2016-11-03 15:53:42 -0700293 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700294
Dan Albert899c23e2018-12-06 11:04:03 -0800295 if compiler.Properties.Include_build_directory == nil ||
296 *compiler.Properties.Include_build_directory {
297 flags.GlobalFlags = append(flags.GlobalFlags, "-I"+android.PathForModuleSrc(ctx).String())
298 flags.YasmFlags = append(flags.YasmFlags, "-I"+android.PathForModuleSrc(ctx).String())
299 }
Colin Crossc3199482017-03-30 15:03:04 -0700300
Colin Cross87dd9632017-11-03 13:31:05 -0700301 if !(ctx.useSdk() || ctx.useVndk()) || ctx.Host() {
302 flags.SystemIncludeFlags = append(flags.SystemIncludeFlags,
303 "${config.CommonGlobalIncludes}",
304 tc.IncludeFlags(),
305 "${config.CommonNativehelperInclude}")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700306 }
307
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700308 if ctx.useSdk() {
Dan Albertfac114b2018-11-14 21:22:00 -0800309 // TODO: Switch to --sysroot.
Colin Cross4d9c2d12016-07-29 12:48:20 -0700310 // The NDK headers are installed to a common sysroot. While a more
311 // typical Soong approach would be to only make the headers for the
312 // library you're using available, we're trying to emulate the NDK
313 // behavior here, and the NDK always has all the NDK headers available.
Colin Crossc3199482017-03-30 15:03:04 -0700314 flags.SystemIncludeFlags = append(flags.SystemIncludeFlags,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700315 "-isystem "+getCurrentIncludePath(ctx).String(),
Chih-Hung Hsieh1e7d1bf2018-03-15 18:44:57 -0700316 "-isystem "+getCurrentIncludePath(ctx).Join(ctx, config.NDKTriple(tc)).String())
Colin Cross4d9c2d12016-07-29 12:48:20 -0700317
Dan Albertfac114b2018-11-14 21:22:00 -0800318 // TODO: Migrate to API suffixed triple?
Colin Cross4d9c2d12016-07-29 12:48:20 -0700319 // Traditionally this has come from android/api-level.h, but with the
320 // libc headers unified it must be set by the build system since we
321 // don't have per-API level copies of that header now.
Dan Albertebedf672016-11-08 15:06:22 -0800322 version := ctx.sdkVersion()
323 if version == "current" {
324 version = "__ANDROID_API_FUTURE__"
325 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700326 flags.GlobalFlags = append(flags.GlobalFlags,
Dan Albertebedf672016-11-08 15:06:22 -0800327 "-D__ANDROID_API__="+version)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700328 }
329
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700330 if ctx.useVndk() {
Justin Yun732aa6a2018-03-23 17:43:47 +0900331 // sdkVersion() returns VNDK version for vendor modules.
332 version := ctx.sdkVersion()
333 if version == "current" {
334 version = "__ANDROID_API_FUTURE__"
335 }
Dan Willemsenb916b802017-03-19 13:44:32 -0700336 flags.GlobalFlags = append(flags.GlobalFlags,
Justin Yun732aa6a2018-03-23 17:43:47 +0900337 "-D__ANDROID_API__="+version, "-D__ANDROID_VNDK__")
Dan Willemsenb916b802017-03-19 13:44:32 -0700338 }
339
Jiyong Parkd54aee52018-06-09 01:32:54 +0900340 if ctx.inRecovery() {
341 flags.GlobalFlags = append(flags.GlobalFlags, "-D__ANDROID_RECOVERY__")
342 }
343
Nan Zhang0007d812017-11-07 10:57:05 -0800344 instructionSet := String(compiler.Properties.Instruction_set)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700345 if flags.RequiredInstructionSet != "" {
346 instructionSet = flags.RequiredInstructionSet
347 }
Dan Willemsen8536d6b2018-10-07 20:54:34 -0700348 instructionSetFlags, err := tc.ClangInstructionSetFlags(instructionSet)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700349 if err != nil {
350 ctx.ModuleErrorf("%s", err)
351 }
352
353 CheckBadCompilerFlags(ctx, "release.cflags", compiler.Properties.Release.Cflags)
354
355 // TODO: debug
Colin Cross4b963f82016-09-29 14:06:02 -0700356 flags.CFlags = append(flags.CFlags, esc(compiler.Properties.Release.Cflags)...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700357
Dan Willemsen8536d6b2018-10-07 20:54:34 -0700358 CheckBadCompilerFlags(ctx, "clang_cflags", compiler.Properties.Clang_cflags)
359 CheckBadCompilerFlags(ctx, "clang_asflags", compiler.Properties.Clang_asflags)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700360
Dan Willemsen8536d6b2018-10-07 20:54:34 -0700361 flags.CFlags = config.ClangFilterUnknownCflags(flags.CFlags)
362 flags.CFlags = append(flags.CFlags, esc(compiler.Properties.Clang_cflags)...)
363 flags.AsFlags = append(flags.AsFlags, esc(compiler.Properties.Clang_asflags)...)
364 flags.CppFlags = config.ClangFilterUnknownCflags(flags.CppFlags)
365 flags.ConlyFlags = config.ClangFilterUnknownCflags(flags.ConlyFlags)
366 flags.LdFlags = config.ClangFilterUnknownCflags(flags.LdFlags)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700367
Dan Willemsen8536d6b2018-10-07 20:54:34 -0700368 target := "-target " + tc.ClangTriple()
369 gccPrefix := "-B" + config.ToolPath(tc)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700370
Dan Willemsen8536d6b2018-10-07 20:54:34 -0700371 flags.CFlags = append(flags.CFlags, target, gccPrefix)
372 flags.AsFlags = append(flags.AsFlags, target, gccPrefix)
373 flags.LdFlags = append(flags.LdFlags, target, gccPrefix)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700374
Colin Crossb98c8b02016-07-29 13:44:28 -0700375 hod := "Host"
Colin Cross4d9c2d12016-07-29 12:48:20 -0700376 if ctx.Os().Class == android.Device {
Colin Crossb98c8b02016-07-29 13:44:28 -0700377 hod = "Device"
Colin Cross4d9c2d12016-07-29 12:48:20 -0700378 }
379
Colin Cross87dd9632017-11-03 13:31:05 -0700380 flags.GlobalFlags = append(flags.GlobalFlags, instructionSetFlags)
381 flags.ConlyFlags = append([]string{"${config.CommonGlobalConlyflags}"}, flags.ConlyFlags...)
Colin Cross26f14502017-11-06 13:59:48 -0800382 flags.CppFlags = append([]string{fmt.Sprintf("${config.%sGlobalCppflags}", hod)}, flags.CppFlags...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700383
Dan Willemsen8536d6b2018-10-07 20:54:34 -0700384 flags.AsFlags = append(flags.AsFlags, tc.ClangAsflags())
385 flags.CppFlags = append([]string{"${config.CommonClangGlobalCppflags}"}, flags.CppFlags...)
386 flags.GlobalFlags = append(flags.GlobalFlags,
387 tc.ClangCflags(),
388 "${config.CommonClangGlobalCflags}",
389 fmt.Sprintf("${config.%sClangGlobalCflags}", hod))
Colin Cross4d9c2d12016-07-29 12:48:20 -0700390
Dan Willemsen8536d6b2018-10-07 20:54:34 -0700391 if strings.HasPrefix(android.PathForModuleSrc(ctx).String(), "external/") {
392 flags.GlobalFlags = append([]string{"${config.ClangExternalCflags}"}, flags.GlobalFlags...)
Yi Kongcc80f8d2018-06-06 14:42:44 -0700393 }
394
Dan Willemsen10db3842018-10-21 18:42:46 -0700395 if tc.Bionic() {
Colin Cross87dd9632017-11-03 13:31:05 -0700396 if Bool(compiler.Properties.Rtti) {
397 flags.CppFlags = append(flags.CppFlags, "-frtti")
398 } else {
399 flags.CppFlags = append(flags.CppFlags, "-fno-rtti")
400 }
401 }
402
403 flags.AsFlags = append(flags.AsFlags, "-D__ASSEMBLY__")
404
Dan Willemsen8536d6b2018-10-07 20:54:34 -0700405 flags.CppFlags = append(flags.CppFlags, tc.ClangCppflags())
Colin Cross87dd9632017-11-03 13:31:05 -0700406
407 flags.YasmFlags = append(flags.YasmFlags, tc.YasmFlags())
408
Dan Willemsen8536d6b2018-10-07 20:54:34 -0700409 flags.GlobalFlags = append(flags.GlobalFlags, tc.ToolchainClangCflags())
Colin Cross4d9c2d12016-07-29 12:48:20 -0700410
Elliott Hughes5789ca92018-02-16 17:15:19 -0800411 cStd := config.CStdVersion
412 if String(compiler.Properties.C_std) == "experimental" {
413 cStd = config.ExperimentalCStdVersion
414 } else if String(compiler.Properties.C_std) != "" {
415 cStd = String(compiler.Properties.C_std)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700416 }
417
Elliott Hughes5789ca92018-02-16 17:15:19 -0800418 cppStd := String(compiler.Properties.Cpp_std)
419 switch String(compiler.Properties.Cpp_std) {
420 case "":
421 cppStd = config.CppStdVersion
422 case "experimental":
423 cppStd = config.ExperimentalCppStdVersion
Elliott Hughes5789ca92018-02-16 17:15:19 -0800424 }
425
Elliott Hughes5789ca92018-02-16 17:15:19 -0800426 if compiler.Properties.Gnu_extensions != nil && *compiler.Properties.Gnu_extensions == false {
427 cStd = gnuToCReplacer.Replace(cStd)
428 cppStd = gnuToCReplacer.Replace(cppStd)
429 }
430
431 flags.ConlyFlags = append([]string{"-std=" + cStd}, flags.ConlyFlags...)
432 flags.CppFlags = append([]string{"-std=" + cppStd}, flags.CppFlags...)
433
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700434 if ctx.useVndk() {
Jiyong Parka622de82017-08-10 13:33:27 +0900435 flags.CFlags = append(flags.CFlags, esc(compiler.Properties.Target.Vendor.Cflags)...)
436 }
437
Jiyong Park4a2dcb52018-05-24 16:44:14 +0900438 if ctx.inRecovery() {
439 flags.CFlags = append(flags.CFlags, esc(compiler.Properties.Target.Recovery.Cflags)...)
440 }
441
Colin Cross4d9c2d12016-07-29 12:48:20 -0700442 // We can enforce some rules more strictly in the code we own. strict
443 // indicates if this is code that we can be stricter with. If we have
444 // rules that we want to apply to *our* code (but maybe can't for
445 // vendor/device specific things), we could extend this to be a ternary
446 // value.
447 strict := true
448 if strings.HasPrefix(android.PathForModuleSrc(ctx).String(), "external/") {
449 strict = false
450 }
451
452 // Can be used to make some annotations stricter for code we can fix
453 // (such as when we mark functions as deprecated).
454 if strict {
455 flags.CFlags = append(flags.CFlags, "-DANDROID_STRICT")
456 }
457
Dan Willemsene1a3ce32016-11-02 20:44:08 -0700458 if compiler.hasSrcExt(".proto") {
Colin Cross0c461f12016-10-20 16:11:43 -0700459 flags = protoFlags(ctx, flags, &compiler.Proto)
460 }
461
Dan Willemsene1a3ce32016-11-02 20:44:08 -0700462 if compiler.hasSrcExt(".y") || compiler.hasSrcExt(".yy") {
463 flags.GlobalFlags = append(flags.GlobalFlags,
464 "-I"+android.PathForModuleGen(ctx, "yacc", ctx.ModuleDir()).String())
465 }
466
Dan Willemsen4f1c3d42017-09-09 01:15:26 -0700467 if compiler.hasSrcExt(".mc") {
468 flags.GlobalFlags = append(flags.GlobalFlags,
469 "-I"+android.PathForModuleGen(ctx, "windmc", ctx.ModuleDir()).String())
470 }
471
Dan Willemsene1240db2016-11-03 14:28:51 -0700472 if compiler.hasSrcExt(".aidl") {
473 if len(compiler.Properties.Aidl.Local_include_dirs) > 0 {
474 localAidlIncludeDirs := android.PathsForModuleSrc(ctx, compiler.Properties.Aidl.Local_include_dirs)
475 flags.aidlFlags = append(flags.aidlFlags, includeDirsToFlags(localAidlIncludeDirs))
476 }
477 if len(compiler.Properties.Aidl.Include_dirs) > 0 {
478 rootAidlIncludeDirs := android.PathsForSource(ctx, compiler.Properties.Aidl.Include_dirs)
479 flags.aidlFlags = append(flags.aidlFlags, includeDirsToFlags(rootAidlIncludeDirs))
480 }
481
Martijn Coeneneab15642018-03-09 09:29:59 +0100482 if Bool(compiler.Properties.Aidl.Generate_traces) {
483 flags.aidlFlags = append(flags.aidlFlags, "-t")
484 }
485
Dan Willemsene1240db2016-11-03 14:28:51 -0700486 flags.GlobalFlags = append(flags.GlobalFlags,
487 "-I"+android.PathForModuleGen(ctx, "aidl").String())
488 }
489
Colin Cross2a252be2017-05-01 17:37:24 -0700490 if compiler.hasSrcExt(".rs") || compiler.hasSrcExt(".fs") {
491 flags = rsFlags(ctx, flags, &compiler.Properties)
492 }
493
Inseob Kim21f26902018-09-06 00:55:20 +0900494 if compiler.hasSrcExt(".sysprop") {
495 flags.GlobalFlags = append(flags.GlobalFlags,
496 "-I"+android.PathForModuleGen(ctx, "sysprop", "include").String())
497 }
498
Chih-Hung Hsieh64a38dc2017-11-14 14:09:14 -0800499 if len(compiler.Properties.Srcs) > 0 {
500 module := ctx.ModuleDir() + "/Android.bp:" + ctx.ModuleName()
501 if inList("-Wno-error", flags.CFlags) || inList("-Wno-error", flags.CppFlags) {
502 addToModuleList(ctx, modulesUsingWnoError, module)
503 } else if !inList("-Werror", flags.CFlags) && !inList("-Werror", flags.CppFlags) {
504 if warningsAreAllowed(ctx.ModuleDir()) {
505 addToModuleList(ctx, modulesAddedWall, module)
506 flags.CFlags = append([]string{"-Wall"}, flags.CFlags...)
507 } else {
Chih-Hung Hsieh64a38dc2017-11-14 14:09:14 -0800508 flags.CFlags = append([]string{"-Wall", "-Werror"}, flags.CFlags...)
509 }
510 }
511 }
512
Pirama Arumuga Nainarfadb7b52017-12-19 23:08:09 -0800513 if Bool(compiler.Properties.Openmp) {
514 flags.CFlags = append(flags.CFlags, "-fopenmp")
515 }
516
Colin Cross4d9c2d12016-07-29 12:48:20 -0700517 return flags
518}
519
Dan Willemsene1a3ce32016-11-02 20:44:08 -0700520func (compiler *baseCompiler) hasSrcExt(ext string) bool {
Colin Crossf18e1102017-11-16 14:33:08 -0800521 for _, src := range compiler.srcsBeforeGen {
522 if src.Ext() == ext {
523 return true
524 }
525 }
Colin Cross0c461f12016-10-20 16:11:43 -0700526 for _, src := range compiler.Properties.Srcs {
Dan Willemsene1a3ce32016-11-02 20:44:08 -0700527 if filepath.Ext(src) == ext {
Colin Cross0c461f12016-10-20 16:11:43 -0700528 return true
529 }
530 }
Colin Cross10d22312017-05-03 11:01:58 -0700531 for _, src := range compiler.Properties.OriginalSrcs {
532 if filepath.Ext(src) == ext {
533 return true
534 }
535 }
Colin Cross0c461f12016-10-20 16:11:43 -0700536
537 return false
538}
539
Colin Cross948f0cb2016-10-17 14:24:56 -0700540var gnuToCReplacer = strings.NewReplacer("gnu", "c")
541
Colin Cross4d9c2d12016-07-29 12:48:20 -0700542func ndkPathDeps(ctx ModuleContext) android.Paths {
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700543 if ctx.useSdk() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700544 // The NDK sysroot timestamp file depends on all the NDK sysroot files
545 // (headers and libraries).
Dan Albert6ab43d82017-12-13 15:05:04 -0800546 return android.Paths{getNdkBaseTimestampFile(ctx)}
Colin Cross4d9c2d12016-07-29 12:48:20 -0700547 }
548 return nil
549}
550
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700551func (compiler *baseCompiler) compile(ctx ModuleContext, flags Flags, deps PathDeps) Objects {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700552 pathDeps := deps.GeneratedHeaders
553 pathDeps = append(pathDeps, ndkPathDeps(ctx)...)
Colin Cross2f336352016-10-26 10:03:47 -0700554
Colin Cross2f336352016-10-26 10:03:47 -0700555 buildFlags := flagsToBuilderFlags(flags)
556
Colin Crossf18e1102017-11-16 14:33:08 -0800557 srcs := append(android.Paths(nil), compiler.srcsBeforeGen...)
558
Colin Cross2f336352016-10-26 10:03:47 -0700559 srcs, genDeps := genSources(ctx, srcs, buildFlags)
Pirama Arumuga Nainarf231b192018-01-23 10:49:04 -0800560 pathDeps = append(pathDeps, genDeps...)
Colin Cross2f336352016-10-26 10:03:47 -0700561
Pirama Arumuga Nainar70ba5a32017-12-19 15:11:01 -0800562 compiler.pathDeps = pathDeps
Pirama Arumuga Nainarf231b192018-01-23 10:49:04 -0800563 compiler.cFlagsDeps = flags.CFlagsDeps
Colin Cross2f336352016-10-26 10:03:47 -0700564
Fabien Sanglardd61f1f42017-01-10 16:21:22 -0800565 // Save src, buildFlags and context
566 compiler.srcs = srcs
567
Colin Cross4d9c2d12016-07-29 12:48:20 -0700568 // Compile files listed in c.Properties.Srcs into objects
Pirama Arumuga Nainarf231b192018-01-23 10:49:04 -0800569 objs := compileObjs(ctx, buildFlags, "", srcs, pathDeps, compiler.cFlagsDeps)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700570
571 if ctx.Failed() {
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700572 return Objects{}
Colin Cross4d9c2d12016-07-29 12:48:20 -0700573 }
574
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700575 return objs
Colin Cross4d9c2d12016-07-29 12:48:20 -0700576}
577
578// Compile a list of source files into objects a specified subdirectory
Colin Cross2f336352016-10-26 10:03:47 -0700579func compileObjs(ctx android.ModuleContext, flags builderFlags,
Pirama Arumuga Nainarf231b192018-01-23 10:49:04 -0800580 subdir string, srcFiles, pathDeps android.Paths, cFlagsDeps android.Paths) Objects {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700581
Pirama Arumuga Nainarf231b192018-01-23 10:49:04 -0800582 return TransformSourceToObj(ctx, subdir, srcFiles, flags, pathDeps, cFlagsDeps)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700583}