blob: c1040dd0c9c2e09e068f791122cb7663ee8d1c81 [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.
32 Srcs []string `android:"arch_variant"`
33
34 // list of source files that should not be used to build the C/C++ module.
35 // This is most useful in the arch/multilib variants to remove non-common files
36 Exclude_srcs []string `android:"arch_variant"`
37
38 // list of module-specific flags that will be used for C and C++ compiles.
39 Cflags []string `android:"arch_variant"`
40
41 // list of module-specific flags that will be used for C++ compiles
42 Cppflags []string `android:"arch_variant"`
43
44 // list of module-specific flags that will be used for C compiles
45 Conlyflags []string `android:"arch_variant"`
46
47 // list of module-specific flags that will be used for .S compiles
48 Asflags []string `android:"arch_variant"`
49
50 // list of module-specific flags that will be used for C and C++ compiles when
51 // compiling with clang
52 Clang_cflags []string `android:"arch_variant"`
53
54 // list of module-specific flags that will be used for .S compiles when
55 // compiling with clang
56 Clang_asflags []string `android:"arch_variant"`
57
58 // list of module-specific flags that will be used for .y and .yy compiles
59 Yaccflags []string
60
61 // the instruction set architecture to use to compile the C/C++
62 // module.
63 Instruction_set string `android:"arch_variant"`
64
65 // list of directories relative to the root of the source tree that will
66 // be added to the include path using -I.
67 // If possible, don't use this. If adding paths from the current directory use
68 // local_include_dirs, if adding paths from other modules use export_include_dirs in
69 // that module.
70 Include_dirs []string `android:"arch_variant"`
71
72 // list of directories relative to the Blueprints file that will
73 // be added to the include path using -I
74 Local_include_dirs []string `android:"arch_variant"`
75
76 // list of generated sources to compile. These are the names of gensrcs or
77 // genrule modules.
78 Generated_sources []string `android:"arch_variant"`
79
80 // list of generated headers to add to the include path. These are the names
81 // of genrule modules.
82 Generated_headers []string `android:"arch_variant"`
83
84 // pass -frtti instead of -fno-rtti
85 Rtti *bool
86
Colin Cross948f0cb2016-10-17 14:24:56 -070087 // if set to false, use -std=c++* instead of -std=gnu++*
88 Gnu_extensions *bool
89
Colin Cross4d9c2d12016-07-29 12:48:20 -070090 Debug, Release struct {
91 // list of module-specific flags that will be used for C and C++ compiles in debug or
92 // release builds
93 Cflags []string `android:"arch_variant"`
94 } `android:"arch_variant"`
95}
96
Colin Crossb916a382016-07-29 17:28:03 -070097func NewBaseCompiler() *baseCompiler {
98 return &baseCompiler{}
99}
100
Colin Cross4d9c2d12016-07-29 12:48:20 -0700101type baseCompiler struct {
102 Properties BaseCompilerProperties
Colin Cross0c461f12016-10-20 16:11:43 -0700103 Proto ProtoProperties
Colin Cross2f336352016-10-26 10:03:47 -0700104 deps android.Paths
Colin Cross4d9c2d12016-07-29 12:48:20 -0700105}
106
107var _ compiler = (*baseCompiler)(nil)
108
109func (compiler *baseCompiler) appendCflags(flags []string) {
110 compiler.Properties.Cflags = append(compiler.Properties.Cflags, flags...)
111}
112
113func (compiler *baseCompiler) appendAsflags(flags []string) {
114 compiler.Properties.Asflags = append(compiler.Properties.Asflags, flags...)
115}
116
Colin Cross42742b82016-08-01 13:20:05 -0700117func (compiler *baseCompiler) compilerProps() []interface{} {
Colin Cross0feb1692016-11-03 14:38:52 -0700118 return []interface{}{&compiler.Properties, &compiler.Proto}
Colin Cross4d9c2d12016-07-29 12:48:20 -0700119}
120
Colin Cross42742b82016-08-01 13:20:05 -0700121func (compiler *baseCompiler) compilerInit(ctx BaseModuleContext) {}
Colin Cross4d9c2d12016-07-29 12:48:20 -0700122
Colin Cross42742b82016-08-01 13:20:05 -0700123func (compiler *baseCompiler) compilerDeps(ctx BaseModuleContext, deps Deps) Deps {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700124 deps.GeneratedSources = append(deps.GeneratedSources, compiler.Properties.Generated_sources...)
125 deps.GeneratedHeaders = append(deps.GeneratedHeaders, compiler.Properties.Generated_headers...)
126
Dan Willemsene1a3ce32016-11-02 20:44:08 -0700127 if compiler.hasSrcExt(".proto") {
Colin Cross0c461f12016-10-20 16:11:43 -0700128 deps = protoDeps(ctx, deps, &compiler.Proto)
129 }
130
Colin Cross4d9c2d12016-07-29 12:48:20 -0700131 return deps
132}
133
134// Create a Flags struct that collects the compile flags from global values,
135// per-target values, module type values, and per-module Blueprints properties
Colin Cross42742b82016-08-01 13:20:05 -0700136func (compiler *baseCompiler) compilerFlags(ctx ModuleContext, flags Flags) Flags {
Colin Crossb98c8b02016-07-29 13:44:28 -0700137 tc := ctx.toolchain()
Colin Cross4d9c2d12016-07-29 12:48:20 -0700138
139 CheckBadCompilerFlags(ctx, "cflags", compiler.Properties.Cflags)
140 CheckBadCompilerFlags(ctx, "cppflags", compiler.Properties.Cppflags)
141 CheckBadCompilerFlags(ctx, "conlyflags", compiler.Properties.Conlyflags)
142 CheckBadCompilerFlags(ctx, "asflags", compiler.Properties.Asflags)
143
Colin Cross4b963f82016-09-29 14:06:02 -0700144 esc := proptools.NinjaAndShellEscape
145
146 flags.CFlags = append(flags.CFlags, esc(compiler.Properties.Cflags)...)
147 flags.CppFlags = append(flags.CppFlags, esc(compiler.Properties.Cppflags)...)
148 flags.ConlyFlags = append(flags.ConlyFlags, esc(compiler.Properties.Conlyflags)...)
149 flags.AsFlags = append(flags.AsFlags, esc(compiler.Properties.Asflags)...)
150 flags.YaccFlags = append(flags.YaccFlags, esc(compiler.Properties.Yaccflags)...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700151
152 // Include dir cflags
Colin Cross4d9c2d12016-07-29 12:48:20 -0700153 localIncludeDirs := android.PathsForModuleSrc(ctx, compiler.Properties.Local_include_dirs)
Dan Willemsen273af7f2016-11-03 15:53:42 -0700154 if len(localIncludeDirs) > 0 {
155 flags.GlobalFlags = append(flags.GlobalFlags, includeDirsToFlags(localIncludeDirs))
156 }
157 rootIncludeDirs := android.PathsForSource(ctx, compiler.Properties.Include_dirs)
158 if len(rootIncludeDirs) > 0 {
159 flags.GlobalFlags = append(flags.GlobalFlags, includeDirsToFlags(rootIncludeDirs))
160 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700161
162 if !ctx.noDefaultCompilerFlags() {
163 if !ctx.sdk() || ctx.Host() {
164 flags.GlobalFlags = append(flags.GlobalFlags,
Colin Crossb98c8b02016-07-29 13:44:28 -0700165 "${config.CommonGlobalIncludes}",
Colin Cross1cfd89a2016-09-15 09:30:46 -0700166 "${config.CommonGlobalSystemIncludes}",
Colin Crossb98c8b02016-07-29 13:44:28 -0700167 tc.IncludeFlags(),
168 "${config.CommonNativehelperInclude}")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700169 }
170
Colin Cross0c461f12016-10-20 16:11:43 -0700171 flags.GlobalFlags = append(flags.GlobalFlags, "-I"+android.PathForModuleSrc(ctx).String())
Colin Cross4d9c2d12016-07-29 12:48:20 -0700172 }
173
174 if ctx.sdk() {
175 // The NDK headers are installed to a common sysroot. While a more
176 // typical Soong approach would be to only make the headers for the
177 // library you're using available, we're trying to emulate the NDK
178 // behavior here, and the NDK always has all the NDK headers available.
179 flags.GlobalFlags = append(flags.GlobalFlags,
180 "-isystem "+getCurrentIncludePath(ctx).String(),
Colin Crossb98c8b02016-07-29 13:44:28 -0700181 "-isystem "+getCurrentIncludePath(ctx).Join(ctx, tc.ClangTriple()).String())
Colin Cross4d9c2d12016-07-29 12:48:20 -0700182
183 // Traditionally this has come from android/api-level.h, but with the
184 // libc headers unified it must be set by the build system since we
185 // don't have per-API level copies of that header now.
186 flags.GlobalFlags = append(flags.GlobalFlags,
187 "-D__ANDROID_API__="+ctx.sdkVersion())
188
189 // Until the full NDK has been migrated to using ndk_headers, we still
190 // need to add the legacy sysroot includes to get the full set of
191 // headers.
192 legacyIncludes := fmt.Sprintf(
193 "prebuilts/ndk/current/platforms/android-%s/arch-%s/usr/include",
194 ctx.sdkVersion(), ctx.Arch().ArchType.String())
195 flags.GlobalFlags = append(flags.GlobalFlags, "-isystem "+legacyIncludes)
196 }
197
198 instructionSet := compiler.Properties.Instruction_set
199 if flags.RequiredInstructionSet != "" {
200 instructionSet = flags.RequiredInstructionSet
201 }
Colin Crossb98c8b02016-07-29 13:44:28 -0700202 instructionSetFlags, err := tc.InstructionSetFlags(instructionSet)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700203 if flags.Clang {
Colin Crossb98c8b02016-07-29 13:44:28 -0700204 instructionSetFlags, err = tc.ClangInstructionSetFlags(instructionSet)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700205 }
206 if err != nil {
207 ctx.ModuleErrorf("%s", err)
208 }
209
210 CheckBadCompilerFlags(ctx, "release.cflags", compiler.Properties.Release.Cflags)
211
212 // TODO: debug
Colin Cross4b963f82016-09-29 14:06:02 -0700213 flags.CFlags = append(flags.CFlags, esc(compiler.Properties.Release.Cflags)...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700214
215 if flags.Clang {
216 CheckBadCompilerFlags(ctx, "clang_cflags", compiler.Properties.Clang_cflags)
217 CheckBadCompilerFlags(ctx, "clang_asflags", compiler.Properties.Clang_asflags)
218
Colin Crossb98c8b02016-07-29 13:44:28 -0700219 flags.CFlags = config.ClangFilterUnknownCflags(flags.CFlags)
Colin Cross4b963f82016-09-29 14:06:02 -0700220 flags.CFlags = append(flags.CFlags, esc(compiler.Properties.Clang_cflags)...)
221 flags.AsFlags = append(flags.AsFlags, esc(compiler.Properties.Clang_asflags)...)
Colin Crossb98c8b02016-07-29 13:44:28 -0700222 flags.CppFlags = config.ClangFilterUnknownCflags(flags.CppFlags)
223 flags.ConlyFlags = config.ClangFilterUnknownCflags(flags.ConlyFlags)
224 flags.LdFlags = config.ClangFilterUnknownCflags(flags.LdFlags)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700225
Colin Crossb98c8b02016-07-29 13:44:28 -0700226 target := "-target " + tc.ClangTriple()
Colin Cross4d9c2d12016-07-29 12:48:20 -0700227 var gccPrefix string
228 if !ctx.Darwin() {
Colin Crossb98c8b02016-07-29 13:44:28 -0700229 gccPrefix = "-B" + filepath.Join(tc.GccRoot(), tc.GccTriple(), "bin")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700230 }
231
232 flags.CFlags = append(flags.CFlags, target, gccPrefix)
233 flags.AsFlags = append(flags.AsFlags, target, gccPrefix)
234 flags.LdFlags = append(flags.LdFlags, target, gccPrefix)
235 }
236
Colin Crossb98c8b02016-07-29 13:44:28 -0700237 hod := "Host"
Colin Cross4d9c2d12016-07-29 12:48:20 -0700238 if ctx.Os().Class == android.Device {
Colin Crossb98c8b02016-07-29 13:44:28 -0700239 hod = "Device"
Colin Cross4d9c2d12016-07-29 12:48:20 -0700240 }
241
242 if !ctx.noDefaultCompilerFlags() {
243 flags.GlobalFlags = append(flags.GlobalFlags, instructionSetFlags)
Elliott Hughes5a0401a2016-10-07 13:12:58 -0700244 flags.ConlyFlags = append(flags.ConlyFlags, "${config.CommonGlobalConlyflags}")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700245
246 if flags.Clang {
Colin Crossb98c8b02016-07-29 13:44:28 -0700247 flags.AsFlags = append(flags.AsFlags, tc.ClangAsflags())
248 flags.CppFlags = append(flags.CppFlags, "${config.CommonClangGlobalCppflags}")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700249 flags.GlobalFlags = append(flags.GlobalFlags,
Colin Crossb98c8b02016-07-29 13:44:28 -0700250 tc.ClangCflags(),
251 "${config.CommonClangGlobalCflags}",
252 fmt.Sprintf("${config.%sClangGlobalCflags}", hod))
Colin Cross4d9c2d12016-07-29 12:48:20 -0700253 } else {
Colin Crossb98c8b02016-07-29 13:44:28 -0700254 flags.CppFlags = append(flags.CppFlags, "${config.CommonGlobalCppflags}")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700255 flags.GlobalFlags = append(flags.GlobalFlags,
Colin Crossb98c8b02016-07-29 13:44:28 -0700256 tc.Cflags(),
257 "${config.CommonGlobalCflags}",
258 fmt.Sprintf("${config.%sGlobalCflags}", hod))
Colin Cross4d9c2d12016-07-29 12:48:20 -0700259 }
260
261 if Bool(ctx.AConfig().ProductVariables.Brillo) {
262 flags.GlobalFlags = append(flags.GlobalFlags, "-D__BRILLO__")
263 }
264
265 if ctx.Device() {
266 if Bool(compiler.Properties.Rtti) {
267 flags.CppFlags = append(flags.CppFlags, "-frtti")
268 } else {
269 flags.CppFlags = append(flags.CppFlags, "-fno-rtti")
270 }
271 }
272
273 flags.AsFlags = append(flags.AsFlags, "-D__ASSEMBLY__")
274
275 if flags.Clang {
Colin Crossb98c8b02016-07-29 13:44:28 -0700276 flags.CppFlags = append(flags.CppFlags, tc.ClangCppflags())
Colin Cross4d9c2d12016-07-29 12:48:20 -0700277 } else {
Colin Crossb98c8b02016-07-29 13:44:28 -0700278 flags.CppFlags = append(flags.CppFlags, tc.Cppflags())
Colin Cross4d9c2d12016-07-29 12:48:20 -0700279 }
280 }
281
282 if flags.Clang {
Colin Crossb98c8b02016-07-29 13:44:28 -0700283 flags.GlobalFlags = append(flags.GlobalFlags, tc.ToolchainClangCflags())
Colin Cross4d9c2d12016-07-29 12:48:20 -0700284 } else {
Colin Crossb98c8b02016-07-29 13:44:28 -0700285 flags.GlobalFlags = append(flags.GlobalFlags, tc.ToolchainCflags())
Colin Cross4d9c2d12016-07-29 12:48:20 -0700286 }
287
288 if !ctx.sdk() {
Colin Cross6f6a4282016-10-17 14:19:06 -0700289 cStd := config.CStdVersion
290 cppStd := config.CppStdVersion
291
292 if !flags.Clang {
293 // GCC uses an invalid C++14 ABI (emits calls to
294 // __cxa_throw_bad_array_length, which is not a valid C++ RT ABI).
295 // http://b/25022512
296 cppStd = config.GccCppStdVersion
297 } else if ctx.Host() && !flags.Clang {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700298 // The host GCC doesn't support C++14 (and is deprecated, so likely
299 // never will). Build these modules with C++11.
Colin Cross6f6a4282016-10-17 14:19:06 -0700300 cppStd = config.GccCppStdVersion
Colin Cross4d9c2d12016-07-29 12:48:20 -0700301 }
Colin Cross6f6a4282016-10-17 14:19:06 -0700302
Colin Cross948f0cb2016-10-17 14:24:56 -0700303 if compiler.Properties.Gnu_extensions != nil && *compiler.Properties.Gnu_extensions == false {
304 cStd = gnuToCReplacer.Replace(cStd)
305 cppStd = gnuToCReplacer.Replace(cppStd)
306 }
307
Colin Cross6f6a4282016-10-17 14:19:06 -0700308 flags.ConlyFlags = append([]string{"-std=" + cStd}, flags.ConlyFlags...)
309 flags.CppFlags = append([]string{"-std=" + cppStd}, flags.CppFlags...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700310 }
311
312 // We can enforce some rules more strictly in the code we own. strict
313 // indicates if this is code that we can be stricter with. If we have
314 // rules that we want to apply to *our* code (but maybe can't for
315 // vendor/device specific things), we could extend this to be a ternary
316 // value.
317 strict := true
318 if strings.HasPrefix(android.PathForModuleSrc(ctx).String(), "external/") {
319 strict = false
320 }
321
322 // Can be used to make some annotations stricter for code we can fix
323 // (such as when we mark functions as deprecated).
324 if strict {
325 flags.CFlags = append(flags.CFlags, "-DANDROID_STRICT")
326 }
327
Dan Willemsene1a3ce32016-11-02 20:44:08 -0700328 if compiler.hasSrcExt(".proto") {
Colin Cross0c461f12016-10-20 16:11:43 -0700329 flags = protoFlags(ctx, flags, &compiler.Proto)
330 }
331
Dan Willemsene1a3ce32016-11-02 20:44:08 -0700332 if compiler.hasSrcExt(".y") || compiler.hasSrcExt(".yy") {
333 flags.GlobalFlags = append(flags.GlobalFlags,
334 "-I"+android.PathForModuleGen(ctx, "yacc", ctx.ModuleDir()).String())
335 }
336
Colin Cross4d9c2d12016-07-29 12:48:20 -0700337 return flags
338}
339
Dan Willemsene1a3ce32016-11-02 20:44:08 -0700340func (compiler *baseCompiler) hasSrcExt(ext string) bool {
Colin Cross0c461f12016-10-20 16:11:43 -0700341 for _, src := range compiler.Properties.Srcs {
Dan Willemsene1a3ce32016-11-02 20:44:08 -0700342 if filepath.Ext(src) == ext {
Colin Cross0c461f12016-10-20 16:11:43 -0700343 return true
344 }
345 }
346
347 return false
348}
349
Colin Cross948f0cb2016-10-17 14:24:56 -0700350var gnuToCReplacer = strings.NewReplacer("gnu", "c")
351
Colin Cross4d9c2d12016-07-29 12:48:20 -0700352func ndkPathDeps(ctx ModuleContext) android.Paths {
353 if ctx.sdk() {
354 // The NDK sysroot timestamp file depends on all the NDK sysroot files
355 // (headers and libraries).
356 return android.Paths{getNdkSysrootTimestampFile(ctx)}
357 }
358 return nil
359}
360
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700361func (compiler *baseCompiler) compile(ctx ModuleContext, flags Flags, deps PathDeps) Objects {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700362 pathDeps := deps.GeneratedHeaders
363 pathDeps = append(pathDeps, ndkPathDeps(ctx)...)
Colin Cross2f336352016-10-26 10:03:47 -0700364
365 srcs := ctx.ExpandSources(compiler.Properties.Srcs, compiler.Properties.Exclude_srcs)
366 srcs = append(srcs, deps.GeneratedSources...)
367
368 buildFlags := flagsToBuilderFlags(flags)
369
370 srcs, genDeps := genSources(ctx, srcs, buildFlags)
371
372 pathDeps = append(pathDeps, genDeps...)
373 pathDeps = append(pathDeps, flags.CFlagsDeps...)
374
375 compiler.deps = pathDeps
376
Colin Cross4d9c2d12016-07-29 12:48:20 -0700377 // Compile files listed in c.Properties.Srcs into objects
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700378 objs := compileObjs(ctx, buildFlags, "", srcs, compiler.deps)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700379
380 if ctx.Failed() {
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700381 return Objects{}
Colin Cross4d9c2d12016-07-29 12:48:20 -0700382 }
383
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700384 return objs
Colin Cross4d9c2d12016-07-29 12:48:20 -0700385}
386
387// Compile a list of source files into objects a specified subdirectory
Colin Cross2f336352016-10-26 10:03:47 -0700388func compileObjs(ctx android.ModuleContext, flags builderFlags,
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700389 subdir string, srcFiles, deps android.Paths) Objects {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700390
Colin Cross2f336352016-10-26 10:03:47 -0700391 return TransformSourceToObj(ctx, subdir, srcFiles, flags, deps)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700392}