blob: 37dc7449cd5281005c4f1e65d0e56da7ebdbc734 [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
22 "android/soong/android"
Colin Crossb98c8b02016-07-29 13:44:28 -070023 "android/soong/cc/config"
Colin Cross4d9c2d12016-07-29 12:48:20 -070024)
25
26// This file contains the basic C/C++/assembly to .o compliation steps
27
28type BaseCompilerProperties struct {
29 // list of source files used to compile the C/C++ module. May be .c, .cpp, or .S files.
30 Srcs []string `android:"arch_variant"`
31
32 // list of source files that should not be used to build the C/C++ module.
33 // This is most useful in the arch/multilib variants to remove non-common files
34 Exclude_srcs []string `android:"arch_variant"`
35
36 // list of module-specific flags that will be used for C and C++ compiles.
37 Cflags []string `android:"arch_variant"`
38
39 // list of module-specific flags that will be used for C++ compiles
40 Cppflags []string `android:"arch_variant"`
41
42 // list of module-specific flags that will be used for C compiles
43 Conlyflags []string `android:"arch_variant"`
44
45 // list of module-specific flags that will be used for .S compiles
46 Asflags []string `android:"arch_variant"`
47
48 // list of module-specific flags that will be used for C and C++ compiles when
49 // compiling with clang
50 Clang_cflags []string `android:"arch_variant"`
51
52 // list of module-specific flags that will be used for .S compiles when
53 // compiling with clang
54 Clang_asflags []string `android:"arch_variant"`
55
56 // list of module-specific flags that will be used for .y and .yy compiles
57 Yaccflags []string
58
59 // the instruction set architecture to use to compile the C/C++
60 // module.
61 Instruction_set string `android:"arch_variant"`
62
63 // list of directories relative to the root of the source tree that will
64 // be added to the include path using -I.
65 // If possible, don't use this. If adding paths from the current directory use
66 // local_include_dirs, if adding paths from other modules use export_include_dirs in
67 // that module.
68 Include_dirs []string `android:"arch_variant"`
69
70 // list of directories relative to the Blueprints file that will
71 // be added to the include path using -I
72 Local_include_dirs []string `android:"arch_variant"`
73
74 // list of generated sources to compile. These are the names of gensrcs or
75 // genrule modules.
76 Generated_sources []string `android:"arch_variant"`
77
78 // list of generated headers to add to the include path. These are the names
79 // of genrule modules.
80 Generated_headers []string `android:"arch_variant"`
81
82 // pass -frtti instead of -fno-rtti
83 Rtti *bool
84
85 Debug, Release struct {
86 // list of module-specific flags that will be used for C and C++ compiles in debug or
87 // release builds
88 Cflags []string `android:"arch_variant"`
89 } `android:"arch_variant"`
90}
91
Colin Crossb916a382016-07-29 17:28:03 -070092func NewBaseCompiler() *baseCompiler {
93 return &baseCompiler{}
94}
95
Colin Cross4d9c2d12016-07-29 12:48:20 -070096type baseCompiler struct {
97 Properties BaseCompilerProperties
98}
99
100var _ compiler = (*baseCompiler)(nil)
101
102func (compiler *baseCompiler) appendCflags(flags []string) {
103 compiler.Properties.Cflags = append(compiler.Properties.Cflags, flags...)
104}
105
106func (compiler *baseCompiler) appendAsflags(flags []string) {
107 compiler.Properties.Asflags = append(compiler.Properties.Asflags, flags...)
108}
109
Colin Cross42742b82016-08-01 13:20:05 -0700110func (compiler *baseCompiler) compilerProps() []interface{} {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700111 return []interface{}{&compiler.Properties}
112}
113
Colin Cross42742b82016-08-01 13:20:05 -0700114func (compiler *baseCompiler) compilerInit(ctx BaseModuleContext) {}
Colin Cross4d9c2d12016-07-29 12:48:20 -0700115
Colin Cross42742b82016-08-01 13:20:05 -0700116func (compiler *baseCompiler) compilerDeps(ctx BaseModuleContext, deps Deps) Deps {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700117 deps.GeneratedSources = append(deps.GeneratedSources, compiler.Properties.Generated_sources...)
118 deps.GeneratedHeaders = append(deps.GeneratedHeaders, compiler.Properties.Generated_headers...)
119
120 return deps
121}
122
123// Create a Flags struct that collects the compile flags from global values,
124// per-target values, module type values, and per-module Blueprints properties
Colin Cross42742b82016-08-01 13:20:05 -0700125func (compiler *baseCompiler) compilerFlags(ctx ModuleContext, flags Flags) Flags {
Colin Crossb98c8b02016-07-29 13:44:28 -0700126 tc := ctx.toolchain()
Colin Cross4d9c2d12016-07-29 12:48:20 -0700127
128 CheckBadCompilerFlags(ctx, "cflags", compiler.Properties.Cflags)
129 CheckBadCompilerFlags(ctx, "cppflags", compiler.Properties.Cppflags)
130 CheckBadCompilerFlags(ctx, "conlyflags", compiler.Properties.Conlyflags)
131 CheckBadCompilerFlags(ctx, "asflags", compiler.Properties.Asflags)
132
133 flags.CFlags = append(flags.CFlags, compiler.Properties.Cflags...)
134 flags.CppFlags = append(flags.CppFlags, compiler.Properties.Cppflags...)
135 flags.ConlyFlags = append(flags.ConlyFlags, compiler.Properties.Conlyflags...)
136 flags.AsFlags = append(flags.AsFlags, compiler.Properties.Asflags...)
137 flags.YaccFlags = append(flags.YaccFlags, compiler.Properties.Yaccflags...)
138
139 // Include dir cflags
140 rootIncludeDirs := android.PathsForSource(ctx, compiler.Properties.Include_dirs)
141 localIncludeDirs := android.PathsForModuleSrc(ctx, compiler.Properties.Local_include_dirs)
142 flags.GlobalFlags = append(flags.GlobalFlags,
143 includeDirsToFlags(localIncludeDirs),
144 includeDirsToFlags(rootIncludeDirs))
145
146 if !ctx.noDefaultCompilerFlags() {
147 if !ctx.sdk() || ctx.Host() {
148 flags.GlobalFlags = append(flags.GlobalFlags,
Colin Crossb98c8b02016-07-29 13:44:28 -0700149 "${config.CommonGlobalIncludes}",
Colin Cross1cfd89a2016-09-15 09:30:46 -0700150 "${config.CommonGlobalSystemIncludes}",
Colin Crossb98c8b02016-07-29 13:44:28 -0700151 tc.IncludeFlags(),
152 "${config.CommonNativehelperInclude}")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700153 }
154
155 flags.GlobalFlags = append(flags.GlobalFlags, []string{
156 "-I" + android.PathForModuleSrc(ctx).String(),
157 "-I" + android.PathForModuleOut(ctx).String(),
158 "-I" + android.PathForModuleGen(ctx).String(),
159 }...)
160 }
161
162 if ctx.sdk() {
163 // The NDK headers are installed to a common sysroot. While a more
164 // typical Soong approach would be to only make the headers for the
165 // library you're using available, we're trying to emulate the NDK
166 // behavior here, and the NDK always has all the NDK headers available.
167 flags.GlobalFlags = append(flags.GlobalFlags,
168 "-isystem "+getCurrentIncludePath(ctx).String(),
Colin Crossb98c8b02016-07-29 13:44:28 -0700169 "-isystem "+getCurrentIncludePath(ctx).Join(ctx, tc.ClangTriple()).String())
Colin Cross4d9c2d12016-07-29 12:48:20 -0700170
171 // Traditionally this has come from android/api-level.h, but with the
172 // libc headers unified it must be set by the build system since we
173 // don't have per-API level copies of that header now.
174 flags.GlobalFlags = append(flags.GlobalFlags,
175 "-D__ANDROID_API__="+ctx.sdkVersion())
176
177 // Until the full NDK has been migrated to using ndk_headers, we still
178 // need to add the legacy sysroot includes to get the full set of
179 // headers.
180 legacyIncludes := fmt.Sprintf(
181 "prebuilts/ndk/current/platforms/android-%s/arch-%s/usr/include",
182 ctx.sdkVersion(), ctx.Arch().ArchType.String())
183 flags.GlobalFlags = append(flags.GlobalFlags, "-isystem "+legacyIncludes)
184 }
185
186 instructionSet := compiler.Properties.Instruction_set
187 if flags.RequiredInstructionSet != "" {
188 instructionSet = flags.RequiredInstructionSet
189 }
Colin Crossb98c8b02016-07-29 13:44:28 -0700190 instructionSetFlags, err := tc.InstructionSetFlags(instructionSet)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700191 if flags.Clang {
Colin Crossb98c8b02016-07-29 13:44:28 -0700192 instructionSetFlags, err = tc.ClangInstructionSetFlags(instructionSet)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700193 }
194 if err != nil {
195 ctx.ModuleErrorf("%s", err)
196 }
197
198 CheckBadCompilerFlags(ctx, "release.cflags", compiler.Properties.Release.Cflags)
199
200 // TODO: debug
201 flags.CFlags = append(flags.CFlags, compiler.Properties.Release.Cflags...)
202
203 if flags.Clang {
204 CheckBadCompilerFlags(ctx, "clang_cflags", compiler.Properties.Clang_cflags)
205 CheckBadCompilerFlags(ctx, "clang_asflags", compiler.Properties.Clang_asflags)
206
Colin Crossb98c8b02016-07-29 13:44:28 -0700207 flags.CFlags = config.ClangFilterUnknownCflags(flags.CFlags)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700208 flags.CFlags = append(flags.CFlags, compiler.Properties.Clang_cflags...)
209 flags.AsFlags = append(flags.AsFlags, compiler.Properties.Clang_asflags...)
Colin Crossb98c8b02016-07-29 13:44:28 -0700210 flags.CppFlags = config.ClangFilterUnknownCflags(flags.CppFlags)
211 flags.ConlyFlags = config.ClangFilterUnknownCflags(flags.ConlyFlags)
212 flags.LdFlags = config.ClangFilterUnknownCflags(flags.LdFlags)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700213
Colin Crossb98c8b02016-07-29 13:44:28 -0700214 target := "-target " + tc.ClangTriple()
Colin Cross4d9c2d12016-07-29 12:48:20 -0700215 var gccPrefix string
216 if !ctx.Darwin() {
Colin Crossb98c8b02016-07-29 13:44:28 -0700217 gccPrefix = "-B" + filepath.Join(tc.GccRoot(), tc.GccTriple(), "bin")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700218 }
219
220 flags.CFlags = append(flags.CFlags, target, gccPrefix)
221 flags.AsFlags = append(flags.AsFlags, target, gccPrefix)
222 flags.LdFlags = append(flags.LdFlags, target, gccPrefix)
223 }
224
Colin Crossb98c8b02016-07-29 13:44:28 -0700225 hod := "Host"
Colin Cross4d9c2d12016-07-29 12:48:20 -0700226 if ctx.Os().Class == android.Device {
Colin Crossb98c8b02016-07-29 13:44:28 -0700227 hod = "Device"
Colin Cross4d9c2d12016-07-29 12:48:20 -0700228 }
229
230 if !ctx.noDefaultCompilerFlags() {
231 flags.GlobalFlags = append(flags.GlobalFlags, instructionSetFlags)
232
233 if flags.Clang {
Colin Crossb98c8b02016-07-29 13:44:28 -0700234 flags.AsFlags = append(flags.AsFlags, tc.ClangAsflags())
235 flags.CppFlags = append(flags.CppFlags, "${config.CommonClangGlobalCppflags}")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700236 flags.GlobalFlags = append(flags.GlobalFlags,
Colin Crossb98c8b02016-07-29 13:44:28 -0700237 tc.ClangCflags(),
238 "${config.CommonClangGlobalCflags}",
239 fmt.Sprintf("${config.%sClangGlobalCflags}", hod))
Colin Cross4d9c2d12016-07-29 12:48:20 -0700240
Colin Crossb98c8b02016-07-29 13:44:28 -0700241 flags.ConlyFlags = append(flags.ConlyFlags, "${config.ClangExtraConlyflags}")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700242 } else {
Colin Crossb98c8b02016-07-29 13:44:28 -0700243 flags.CppFlags = append(flags.CppFlags, "${config.CommonGlobalCppflags}")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700244 flags.GlobalFlags = append(flags.GlobalFlags,
Colin Crossb98c8b02016-07-29 13:44:28 -0700245 tc.Cflags(),
246 "${config.CommonGlobalCflags}",
247 fmt.Sprintf("${config.%sGlobalCflags}", hod))
Colin Cross4d9c2d12016-07-29 12:48:20 -0700248 }
249
250 if Bool(ctx.AConfig().ProductVariables.Brillo) {
251 flags.GlobalFlags = append(flags.GlobalFlags, "-D__BRILLO__")
252 }
253
254 if ctx.Device() {
255 if Bool(compiler.Properties.Rtti) {
256 flags.CppFlags = append(flags.CppFlags, "-frtti")
257 } else {
258 flags.CppFlags = append(flags.CppFlags, "-fno-rtti")
259 }
260 }
261
262 flags.AsFlags = append(flags.AsFlags, "-D__ASSEMBLY__")
263
264 if flags.Clang {
Colin Crossb98c8b02016-07-29 13:44:28 -0700265 flags.CppFlags = append(flags.CppFlags, tc.ClangCppflags())
Colin Cross4d9c2d12016-07-29 12:48:20 -0700266 } else {
Colin Crossb98c8b02016-07-29 13:44:28 -0700267 flags.CppFlags = append(flags.CppFlags, tc.Cppflags())
Colin Cross4d9c2d12016-07-29 12:48:20 -0700268 }
269 }
270
271 if flags.Clang {
Colin Crossb98c8b02016-07-29 13:44:28 -0700272 flags.GlobalFlags = append(flags.GlobalFlags, tc.ToolchainClangCflags())
Colin Cross4d9c2d12016-07-29 12:48:20 -0700273 } else {
Colin Crossb98c8b02016-07-29 13:44:28 -0700274 flags.GlobalFlags = append(flags.GlobalFlags, tc.ToolchainCflags())
Colin Cross4d9c2d12016-07-29 12:48:20 -0700275 }
276
277 if !ctx.sdk() {
278 if ctx.Host() && !flags.Clang {
279 // The host GCC doesn't support C++14 (and is deprecated, so likely
280 // never will). Build these modules with C++11.
281 flags.CppFlags = append(flags.CppFlags, "-std=gnu++11")
282 } else {
283 flags.CppFlags = append(flags.CppFlags, "-std=gnu++14")
284 }
285 }
286
287 // We can enforce some rules more strictly in the code we own. strict
288 // indicates if this is code that we can be stricter with. If we have
289 // rules that we want to apply to *our* code (but maybe can't for
290 // vendor/device specific things), we could extend this to be a ternary
291 // value.
292 strict := true
293 if strings.HasPrefix(android.PathForModuleSrc(ctx).String(), "external/") {
294 strict = false
295 }
296
297 // Can be used to make some annotations stricter for code we can fix
298 // (such as when we mark functions as deprecated).
299 if strict {
300 flags.CFlags = append(flags.CFlags, "-DANDROID_STRICT")
301 }
302
303 return flags
304}
305
306func ndkPathDeps(ctx ModuleContext) android.Paths {
307 if ctx.sdk() {
308 // The NDK sysroot timestamp file depends on all the NDK sysroot files
309 // (headers and libraries).
310 return android.Paths{getNdkSysrootTimestampFile(ctx)}
311 }
312 return nil
313}
314
315func (compiler *baseCompiler) compile(ctx ModuleContext, flags Flags, deps PathDeps) android.Paths {
316 pathDeps := deps.GeneratedHeaders
317 pathDeps = append(pathDeps, ndkPathDeps(ctx)...)
318 // Compile files listed in c.Properties.Srcs into objects
Colin Crossb916a382016-07-29 17:28:03 -0700319 objFiles := compileObjs(ctx, flags, "",
Colin Cross4d9c2d12016-07-29 12:48:20 -0700320 compiler.Properties.Srcs, compiler.Properties.Exclude_srcs,
321 deps.GeneratedSources, pathDeps)
322
323 if ctx.Failed() {
324 return nil
325 }
326
327 return objFiles
328}
329
330// Compile a list of source files into objects a specified subdirectory
Colin Crossb916a382016-07-29 17:28:03 -0700331func compileObjs(ctx android.ModuleContext, flags Flags,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700332 subdir string, srcFiles, excludes []string, extraSrcs, deps android.Paths) android.Paths {
333
334 buildFlags := flagsToBuilderFlags(flags)
335
336 inputFiles := ctx.ExpandSources(srcFiles, excludes)
337 inputFiles = append(inputFiles, extraSrcs...)
338 srcPaths, gendeps := genSources(ctx, inputFiles, buildFlags)
339
340 deps = append(deps, gendeps...)
341 deps = append(deps, flags.CFlagsDeps...)
342
343 return TransformSourceToObj(ctx, subdir, srcPaths, buildFlags, deps)
344}