blob: 32d98e4a8ddb32cb7e4206ed7fabf9899d46570f [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 Cross2f336352016-10-26 10:03:47 -0700103 deps android.Paths
Colin Cross4d9c2d12016-07-29 12:48:20 -0700104}
105
106var _ compiler = (*baseCompiler)(nil)
107
108func (compiler *baseCompiler) appendCflags(flags []string) {
109 compiler.Properties.Cflags = append(compiler.Properties.Cflags, flags...)
110}
111
112func (compiler *baseCompiler) appendAsflags(flags []string) {
113 compiler.Properties.Asflags = append(compiler.Properties.Asflags, flags...)
114}
115
Colin Cross42742b82016-08-01 13:20:05 -0700116func (compiler *baseCompiler) compilerProps() []interface{} {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700117 return []interface{}{&compiler.Properties}
118}
119
Colin Cross42742b82016-08-01 13:20:05 -0700120func (compiler *baseCompiler) compilerInit(ctx BaseModuleContext) {}
Colin Cross4d9c2d12016-07-29 12:48:20 -0700121
Colin Cross42742b82016-08-01 13:20:05 -0700122func (compiler *baseCompiler) compilerDeps(ctx BaseModuleContext, deps Deps) Deps {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700123 deps.GeneratedSources = append(deps.GeneratedSources, compiler.Properties.Generated_sources...)
124 deps.GeneratedHeaders = append(deps.GeneratedHeaders, compiler.Properties.Generated_headers...)
125
126 return deps
127}
128
129// Create a Flags struct that collects the compile flags from global values,
130// per-target values, module type values, and per-module Blueprints properties
Colin Cross42742b82016-08-01 13:20:05 -0700131func (compiler *baseCompiler) compilerFlags(ctx ModuleContext, flags Flags) Flags {
Colin Crossb98c8b02016-07-29 13:44:28 -0700132 tc := ctx.toolchain()
Colin Cross4d9c2d12016-07-29 12:48:20 -0700133
134 CheckBadCompilerFlags(ctx, "cflags", compiler.Properties.Cflags)
135 CheckBadCompilerFlags(ctx, "cppflags", compiler.Properties.Cppflags)
136 CheckBadCompilerFlags(ctx, "conlyflags", compiler.Properties.Conlyflags)
137 CheckBadCompilerFlags(ctx, "asflags", compiler.Properties.Asflags)
138
Colin Cross4b963f82016-09-29 14:06:02 -0700139 esc := proptools.NinjaAndShellEscape
140
141 flags.CFlags = append(flags.CFlags, esc(compiler.Properties.Cflags)...)
142 flags.CppFlags = append(flags.CppFlags, esc(compiler.Properties.Cppflags)...)
143 flags.ConlyFlags = append(flags.ConlyFlags, esc(compiler.Properties.Conlyflags)...)
144 flags.AsFlags = append(flags.AsFlags, esc(compiler.Properties.Asflags)...)
145 flags.YaccFlags = append(flags.YaccFlags, esc(compiler.Properties.Yaccflags)...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700146
147 // Include dir cflags
148 rootIncludeDirs := android.PathsForSource(ctx, compiler.Properties.Include_dirs)
149 localIncludeDirs := android.PathsForModuleSrc(ctx, compiler.Properties.Local_include_dirs)
150 flags.GlobalFlags = append(flags.GlobalFlags,
151 includeDirsToFlags(localIncludeDirs),
152 includeDirsToFlags(rootIncludeDirs))
153
154 if !ctx.noDefaultCompilerFlags() {
155 if !ctx.sdk() || ctx.Host() {
156 flags.GlobalFlags = append(flags.GlobalFlags,
Colin Crossb98c8b02016-07-29 13:44:28 -0700157 "${config.CommonGlobalIncludes}",
Colin Cross1cfd89a2016-09-15 09:30:46 -0700158 "${config.CommonGlobalSystemIncludes}",
Colin Crossb98c8b02016-07-29 13:44:28 -0700159 tc.IncludeFlags(),
160 "${config.CommonNativehelperInclude}")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700161 }
162
163 flags.GlobalFlags = append(flags.GlobalFlags, []string{
164 "-I" + android.PathForModuleSrc(ctx).String(),
165 "-I" + android.PathForModuleOut(ctx).String(),
166 "-I" + android.PathForModuleGen(ctx).String(),
167 }...)
168 }
169
170 if ctx.sdk() {
171 // The NDK headers are installed to a common sysroot. While a more
172 // typical Soong approach would be to only make the headers for the
173 // library you're using available, we're trying to emulate the NDK
174 // behavior here, and the NDK always has all the NDK headers available.
175 flags.GlobalFlags = append(flags.GlobalFlags,
176 "-isystem "+getCurrentIncludePath(ctx).String(),
Colin Crossb98c8b02016-07-29 13:44:28 -0700177 "-isystem "+getCurrentIncludePath(ctx).Join(ctx, tc.ClangTriple()).String())
Colin Cross4d9c2d12016-07-29 12:48:20 -0700178
179 // Traditionally this has come from android/api-level.h, but with the
180 // libc headers unified it must be set by the build system since we
181 // don't have per-API level copies of that header now.
182 flags.GlobalFlags = append(flags.GlobalFlags,
183 "-D__ANDROID_API__="+ctx.sdkVersion())
184
185 // Until the full NDK has been migrated to using ndk_headers, we still
186 // need to add the legacy sysroot includes to get the full set of
187 // headers.
188 legacyIncludes := fmt.Sprintf(
189 "prebuilts/ndk/current/platforms/android-%s/arch-%s/usr/include",
190 ctx.sdkVersion(), ctx.Arch().ArchType.String())
191 flags.GlobalFlags = append(flags.GlobalFlags, "-isystem "+legacyIncludes)
192 }
193
194 instructionSet := compiler.Properties.Instruction_set
195 if flags.RequiredInstructionSet != "" {
196 instructionSet = flags.RequiredInstructionSet
197 }
Colin Crossb98c8b02016-07-29 13:44:28 -0700198 instructionSetFlags, err := tc.InstructionSetFlags(instructionSet)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700199 if flags.Clang {
Colin Crossb98c8b02016-07-29 13:44:28 -0700200 instructionSetFlags, err = tc.ClangInstructionSetFlags(instructionSet)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700201 }
202 if err != nil {
203 ctx.ModuleErrorf("%s", err)
204 }
205
206 CheckBadCompilerFlags(ctx, "release.cflags", compiler.Properties.Release.Cflags)
207
208 // TODO: debug
Colin Cross4b963f82016-09-29 14:06:02 -0700209 flags.CFlags = append(flags.CFlags, esc(compiler.Properties.Release.Cflags)...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700210
211 if flags.Clang {
212 CheckBadCompilerFlags(ctx, "clang_cflags", compiler.Properties.Clang_cflags)
213 CheckBadCompilerFlags(ctx, "clang_asflags", compiler.Properties.Clang_asflags)
214
Colin Crossb98c8b02016-07-29 13:44:28 -0700215 flags.CFlags = config.ClangFilterUnknownCflags(flags.CFlags)
Colin Cross4b963f82016-09-29 14:06:02 -0700216 flags.CFlags = append(flags.CFlags, esc(compiler.Properties.Clang_cflags)...)
217 flags.AsFlags = append(flags.AsFlags, esc(compiler.Properties.Clang_asflags)...)
Colin Crossb98c8b02016-07-29 13:44:28 -0700218 flags.CppFlags = config.ClangFilterUnknownCflags(flags.CppFlags)
219 flags.ConlyFlags = config.ClangFilterUnknownCflags(flags.ConlyFlags)
220 flags.LdFlags = config.ClangFilterUnknownCflags(flags.LdFlags)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700221
Colin Crossb98c8b02016-07-29 13:44:28 -0700222 target := "-target " + tc.ClangTriple()
Colin Cross4d9c2d12016-07-29 12:48:20 -0700223 var gccPrefix string
224 if !ctx.Darwin() {
Colin Crossb98c8b02016-07-29 13:44:28 -0700225 gccPrefix = "-B" + filepath.Join(tc.GccRoot(), tc.GccTriple(), "bin")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700226 }
227
228 flags.CFlags = append(flags.CFlags, target, gccPrefix)
229 flags.AsFlags = append(flags.AsFlags, target, gccPrefix)
230 flags.LdFlags = append(flags.LdFlags, target, gccPrefix)
231 }
232
Colin Crossb98c8b02016-07-29 13:44:28 -0700233 hod := "Host"
Colin Cross4d9c2d12016-07-29 12:48:20 -0700234 if ctx.Os().Class == android.Device {
Colin Crossb98c8b02016-07-29 13:44:28 -0700235 hod = "Device"
Colin Cross4d9c2d12016-07-29 12:48:20 -0700236 }
237
238 if !ctx.noDefaultCompilerFlags() {
239 flags.GlobalFlags = append(flags.GlobalFlags, instructionSetFlags)
Elliott Hughes5a0401a2016-10-07 13:12:58 -0700240 flags.ConlyFlags = append(flags.ConlyFlags, "${config.CommonGlobalConlyflags}")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700241
242 if flags.Clang {
Colin Crossb98c8b02016-07-29 13:44:28 -0700243 flags.AsFlags = append(flags.AsFlags, tc.ClangAsflags())
244 flags.CppFlags = append(flags.CppFlags, "${config.CommonClangGlobalCppflags}")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700245 flags.GlobalFlags = append(flags.GlobalFlags,
Colin Crossb98c8b02016-07-29 13:44:28 -0700246 tc.ClangCflags(),
247 "${config.CommonClangGlobalCflags}",
248 fmt.Sprintf("${config.%sClangGlobalCflags}", hod))
Colin Cross4d9c2d12016-07-29 12:48:20 -0700249 } else {
Colin Crossb98c8b02016-07-29 13:44:28 -0700250 flags.CppFlags = append(flags.CppFlags, "${config.CommonGlobalCppflags}")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700251 flags.GlobalFlags = append(flags.GlobalFlags,
Colin Crossb98c8b02016-07-29 13:44:28 -0700252 tc.Cflags(),
253 "${config.CommonGlobalCflags}",
254 fmt.Sprintf("${config.%sGlobalCflags}", hod))
Colin Cross4d9c2d12016-07-29 12:48:20 -0700255 }
256
257 if Bool(ctx.AConfig().ProductVariables.Brillo) {
258 flags.GlobalFlags = append(flags.GlobalFlags, "-D__BRILLO__")
259 }
260
261 if ctx.Device() {
262 if Bool(compiler.Properties.Rtti) {
263 flags.CppFlags = append(flags.CppFlags, "-frtti")
264 } else {
265 flags.CppFlags = append(flags.CppFlags, "-fno-rtti")
266 }
267 }
268
269 flags.AsFlags = append(flags.AsFlags, "-D__ASSEMBLY__")
270
271 if flags.Clang {
Colin Crossb98c8b02016-07-29 13:44:28 -0700272 flags.CppFlags = append(flags.CppFlags, tc.ClangCppflags())
Colin Cross4d9c2d12016-07-29 12:48:20 -0700273 } else {
Colin Crossb98c8b02016-07-29 13:44:28 -0700274 flags.CppFlags = append(flags.CppFlags, tc.Cppflags())
Colin Cross4d9c2d12016-07-29 12:48:20 -0700275 }
276 }
277
278 if flags.Clang {
Colin Crossb98c8b02016-07-29 13:44:28 -0700279 flags.GlobalFlags = append(flags.GlobalFlags, tc.ToolchainClangCflags())
Colin Cross4d9c2d12016-07-29 12:48:20 -0700280 } else {
Colin Crossb98c8b02016-07-29 13:44:28 -0700281 flags.GlobalFlags = append(flags.GlobalFlags, tc.ToolchainCflags())
Colin Cross4d9c2d12016-07-29 12:48:20 -0700282 }
283
284 if !ctx.sdk() {
Colin Cross6f6a4282016-10-17 14:19:06 -0700285 cStd := config.CStdVersion
286 cppStd := config.CppStdVersion
287
288 if !flags.Clang {
289 // GCC uses an invalid C++14 ABI (emits calls to
290 // __cxa_throw_bad_array_length, which is not a valid C++ RT ABI).
291 // http://b/25022512
292 cppStd = config.GccCppStdVersion
293 } else if ctx.Host() && !flags.Clang {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700294 // The host GCC doesn't support C++14 (and is deprecated, so likely
295 // never will). Build these modules with C++11.
Colin Cross6f6a4282016-10-17 14:19:06 -0700296 cppStd = config.GccCppStdVersion
Colin Cross4d9c2d12016-07-29 12:48:20 -0700297 }
Colin Cross6f6a4282016-10-17 14:19:06 -0700298
Colin Cross948f0cb2016-10-17 14:24:56 -0700299 if compiler.Properties.Gnu_extensions != nil && *compiler.Properties.Gnu_extensions == false {
300 cStd = gnuToCReplacer.Replace(cStd)
301 cppStd = gnuToCReplacer.Replace(cppStd)
302 }
303
Colin Cross6f6a4282016-10-17 14:19:06 -0700304 flags.ConlyFlags = append([]string{"-std=" + cStd}, flags.ConlyFlags...)
305 flags.CppFlags = append([]string{"-std=" + cppStd}, flags.CppFlags...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700306 }
307
308 // We can enforce some rules more strictly in the code we own. strict
309 // indicates if this is code that we can be stricter with. If we have
310 // rules that we want to apply to *our* code (but maybe can't for
311 // vendor/device specific things), we could extend this to be a ternary
312 // value.
313 strict := true
314 if strings.HasPrefix(android.PathForModuleSrc(ctx).String(), "external/") {
315 strict = false
316 }
317
318 // Can be used to make some annotations stricter for code we can fix
319 // (such as when we mark functions as deprecated).
320 if strict {
321 flags.CFlags = append(flags.CFlags, "-DANDROID_STRICT")
322 }
323
324 return flags
325}
326
Colin Cross948f0cb2016-10-17 14:24:56 -0700327var gnuToCReplacer = strings.NewReplacer("gnu", "c")
328
Colin Cross4d9c2d12016-07-29 12:48:20 -0700329func ndkPathDeps(ctx ModuleContext) android.Paths {
330 if ctx.sdk() {
331 // The NDK sysroot timestamp file depends on all the NDK sysroot files
332 // (headers and libraries).
333 return android.Paths{getNdkSysrootTimestampFile(ctx)}
334 }
335 return nil
336}
337
338func (compiler *baseCompiler) compile(ctx ModuleContext, flags Flags, deps PathDeps) android.Paths {
339 pathDeps := deps.GeneratedHeaders
340 pathDeps = append(pathDeps, ndkPathDeps(ctx)...)
Colin Cross2f336352016-10-26 10:03:47 -0700341
342 srcs := ctx.ExpandSources(compiler.Properties.Srcs, compiler.Properties.Exclude_srcs)
343 srcs = append(srcs, deps.GeneratedSources...)
344
345 buildFlags := flagsToBuilderFlags(flags)
346
347 srcs, genDeps := genSources(ctx, srcs, buildFlags)
348
349 pathDeps = append(pathDeps, genDeps...)
350 pathDeps = append(pathDeps, flags.CFlagsDeps...)
351
352 compiler.deps = pathDeps
353
Colin Cross4d9c2d12016-07-29 12:48:20 -0700354 // Compile files listed in c.Properties.Srcs into objects
Colin Cross2f336352016-10-26 10:03:47 -0700355 objFiles := compileObjs(ctx, buildFlags, "", srcs, compiler.deps)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700356
357 if ctx.Failed() {
358 return nil
359 }
360
361 return objFiles
362}
363
364// Compile a list of source files into objects a specified subdirectory
Colin Cross2f336352016-10-26 10:03:47 -0700365func compileObjs(ctx android.ModuleContext, flags builderFlags,
366 subdir string, srcFiles, deps android.Paths) android.Paths {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700367
Colin Cross2f336352016-10-26 10:03:47 -0700368 return TransformSourceToObj(ctx, subdir, srcFiles, flags, deps)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700369}