blob: 198b7924147f313e73c12b066c7e4605af71864a [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
87 Debug, Release struct {
88 // list of module-specific flags that will be used for C and C++ compiles in debug or
89 // release builds
90 Cflags []string `android:"arch_variant"`
91 } `android:"arch_variant"`
92}
93
Colin Crossb916a382016-07-29 17:28:03 -070094func NewBaseCompiler() *baseCompiler {
95 return &baseCompiler{}
96}
97
Colin Cross4d9c2d12016-07-29 12:48:20 -070098type baseCompiler struct {
99 Properties BaseCompilerProperties
100}
101
102var _ compiler = (*baseCompiler)(nil)
103
104func (compiler *baseCompiler) appendCflags(flags []string) {
105 compiler.Properties.Cflags = append(compiler.Properties.Cflags, flags...)
106}
107
108func (compiler *baseCompiler) appendAsflags(flags []string) {
109 compiler.Properties.Asflags = append(compiler.Properties.Asflags, flags...)
110}
111
Colin Cross42742b82016-08-01 13:20:05 -0700112func (compiler *baseCompiler) compilerProps() []interface{} {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700113 return []interface{}{&compiler.Properties}
114}
115
Colin Cross42742b82016-08-01 13:20:05 -0700116func (compiler *baseCompiler) compilerInit(ctx BaseModuleContext) {}
Colin Cross4d9c2d12016-07-29 12:48:20 -0700117
Colin Cross42742b82016-08-01 13:20:05 -0700118func (compiler *baseCompiler) compilerDeps(ctx BaseModuleContext, deps Deps) Deps {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700119 deps.GeneratedSources = append(deps.GeneratedSources, compiler.Properties.Generated_sources...)
120 deps.GeneratedHeaders = append(deps.GeneratedHeaders, compiler.Properties.Generated_headers...)
121
122 return deps
123}
124
125// Create a Flags struct that collects the compile flags from global values,
126// per-target values, module type values, and per-module Blueprints properties
Colin Cross42742b82016-08-01 13:20:05 -0700127func (compiler *baseCompiler) compilerFlags(ctx ModuleContext, flags Flags) Flags {
Colin Crossb98c8b02016-07-29 13:44:28 -0700128 tc := ctx.toolchain()
Colin Cross4d9c2d12016-07-29 12:48:20 -0700129
130 CheckBadCompilerFlags(ctx, "cflags", compiler.Properties.Cflags)
131 CheckBadCompilerFlags(ctx, "cppflags", compiler.Properties.Cppflags)
132 CheckBadCompilerFlags(ctx, "conlyflags", compiler.Properties.Conlyflags)
133 CheckBadCompilerFlags(ctx, "asflags", compiler.Properties.Asflags)
134
Colin Cross4b963f82016-09-29 14:06:02 -0700135 esc := proptools.NinjaAndShellEscape
136
137 flags.CFlags = append(flags.CFlags, esc(compiler.Properties.Cflags)...)
138 flags.CppFlags = append(flags.CppFlags, esc(compiler.Properties.Cppflags)...)
139 flags.ConlyFlags = append(flags.ConlyFlags, esc(compiler.Properties.Conlyflags)...)
140 flags.AsFlags = append(flags.AsFlags, esc(compiler.Properties.Asflags)...)
141 flags.YaccFlags = append(flags.YaccFlags, esc(compiler.Properties.Yaccflags)...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700142
143 // Include dir cflags
144 rootIncludeDirs := android.PathsForSource(ctx, compiler.Properties.Include_dirs)
145 localIncludeDirs := android.PathsForModuleSrc(ctx, compiler.Properties.Local_include_dirs)
146 flags.GlobalFlags = append(flags.GlobalFlags,
147 includeDirsToFlags(localIncludeDirs),
148 includeDirsToFlags(rootIncludeDirs))
149
150 if !ctx.noDefaultCompilerFlags() {
151 if !ctx.sdk() || ctx.Host() {
152 flags.GlobalFlags = append(flags.GlobalFlags,
Colin Crossb98c8b02016-07-29 13:44:28 -0700153 "${config.CommonGlobalIncludes}",
Colin Cross1cfd89a2016-09-15 09:30:46 -0700154 "${config.CommonGlobalSystemIncludes}",
Colin Crossb98c8b02016-07-29 13:44:28 -0700155 tc.IncludeFlags(),
156 "${config.CommonNativehelperInclude}")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700157 }
158
159 flags.GlobalFlags = append(flags.GlobalFlags, []string{
160 "-I" + android.PathForModuleSrc(ctx).String(),
161 "-I" + android.PathForModuleOut(ctx).String(),
162 "-I" + android.PathForModuleGen(ctx).String(),
163 }...)
164 }
165
166 if ctx.sdk() {
167 // The NDK headers are installed to a common sysroot. While a more
168 // typical Soong approach would be to only make the headers for the
169 // library you're using available, we're trying to emulate the NDK
170 // behavior here, and the NDK always has all the NDK headers available.
171 flags.GlobalFlags = append(flags.GlobalFlags,
172 "-isystem "+getCurrentIncludePath(ctx).String(),
Colin Crossb98c8b02016-07-29 13:44:28 -0700173 "-isystem "+getCurrentIncludePath(ctx).Join(ctx, tc.ClangTriple()).String())
Colin Cross4d9c2d12016-07-29 12:48:20 -0700174
175 // Traditionally this has come from android/api-level.h, but with the
176 // libc headers unified it must be set by the build system since we
177 // don't have per-API level copies of that header now.
178 flags.GlobalFlags = append(flags.GlobalFlags,
179 "-D__ANDROID_API__="+ctx.sdkVersion())
180
181 // Until the full NDK has been migrated to using ndk_headers, we still
182 // need to add the legacy sysroot includes to get the full set of
183 // headers.
184 legacyIncludes := fmt.Sprintf(
185 "prebuilts/ndk/current/platforms/android-%s/arch-%s/usr/include",
186 ctx.sdkVersion(), ctx.Arch().ArchType.String())
187 flags.GlobalFlags = append(flags.GlobalFlags, "-isystem "+legacyIncludes)
188 }
189
190 instructionSet := compiler.Properties.Instruction_set
191 if flags.RequiredInstructionSet != "" {
192 instructionSet = flags.RequiredInstructionSet
193 }
Colin Crossb98c8b02016-07-29 13:44:28 -0700194 instructionSetFlags, err := tc.InstructionSetFlags(instructionSet)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700195 if flags.Clang {
Colin Crossb98c8b02016-07-29 13:44:28 -0700196 instructionSetFlags, err = tc.ClangInstructionSetFlags(instructionSet)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700197 }
198 if err != nil {
199 ctx.ModuleErrorf("%s", err)
200 }
201
202 CheckBadCompilerFlags(ctx, "release.cflags", compiler.Properties.Release.Cflags)
203
204 // TODO: debug
Colin Cross4b963f82016-09-29 14:06:02 -0700205 flags.CFlags = append(flags.CFlags, esc(compiler.Properties.Release.Cflags)...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700206
207 if flags.Clang {
208 CheckBadCompilerFlags(ctx, "clang_cflags", compiler.Properties.Clang_cflags)
209 CheckBadCompilerFlags(ctx, "clang_asflags", compiler.Properties.Clang_asflags)
210
Colin Crossb98c8b02016-07-29 13:44:28 -0700211 flags.CFlags = config.ClangFilterUnknownCflags(flags.CFlags)
Colin Cross4b963f82016-09-29 14:06:02 -0700212 flags.CFlags = append(flags.CFlags, esc(compiler.Properties.Clang_cflags)...)
213 flags.AsFlags = append(flags.AsFlags, esc(compiler.Properties.Clang_asflags)...)
Colin Crossb98c8b02016-07-29 13:44:28 -0700214 flags.CppFlags = config.ClangFilterUnknownCflags(flags.CppFlags)
215 flags.ConlyFlags = config.ClangFilterUnknownCflags(flags.ConlyFlags)
216 flags.LdFlags = config.ClangFilterUnknownCflags(flags.LdFlags)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700217
Colin Crossb98c8b02016-07-29 13:44:28 -0700218 target := "-target " + tc.ClangTriple()
Colin Cross4d9c2d12016-07-29 12:48:20 -0700219 var gccPrefix string
220 if !ctx.Darwin() {
Colin Crossb98c8b02016-07-29 13:44:28 -0700221 gccPrefix = "-B" + filepath.Join(tc.GccRoot(), tc.GccTriple(), "bin")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700222 }
223
224 flags.CFlags = append(flags.CFlags, target, gccPrefix)
225 flags.AsFlags = append(flags.AsFlags, target, gccPrefix)
226 flags.LdFlags = append(flags.LdFlags, target, gccPrefix)
227 }
228
Colin Crossb98c8b02016-07-29 13:44:28 -0700229 hod := "Host"
Colin Cross4d9c2d12016-07-29 12:48:20 -0700230 if ctx.Os().Class == android.Device {
Colin Crossb98c8b02016-07-29 13:44:28 -0700231 hod = "Device"
Colin Cross4d9c2d12016-07-29 12:48:20 -0700232 }
233
234 if !ctx.noDefaultCompilerFlags() {
235 flags.GlobalFlags = append(flags.GlobalFlags, instructionSetFlags)
Elliott Hughes5a0401a2016-10-07 13:12:58 -0700236 flags.ConlyFlags = append(flags.ConlyFlags, "${config.CommonGlobalConlyflags}")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700237
238 if flags.Clang {
Colin Crossb98c8b02016-07-29 13:44:28 -0700239 flags.AsFlags = append(flags.AsFlags, tc.ClangAsflags())
240 flags.CppFlags = append(flags.CppFlags, "${config.CommonClangGlobalCppflags}")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700241 flags.GlobalFlags = append(flags.GlobalFlags,
Colin Crossb98c8b02016-07-29 13:44:28 -0700242 tc.ClangCflags(),
243 "${config.CommonClangGlobalCflags}",
244 fmt.Sprintf("${config.%sClangGlobalCflags}", hod))
Colin Cross4d9c2d12016-07-29 12:48:20 -0700245 } else {
Colin Crossb98c8b02016-07-29 13:44:28 -0700246 flags.CppFlags = append(flags.CppFlags, "${config.CommonGlobalCppflags}")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700247 flags.GlobalFlags = append(flags.GlobalFlags,
Colin Crossb98c8b02016-07-29 13:44:28 -0700248 tc.Cflags(),
249 "${config.CommonGlobalCflags}",
250 fmt.Sprintf("${config.%sGlobalCflags}", hod))
Colin Cross4d9c2d12016-07-29 12:48:20 -0700251 }
252
253 if Bool(ctx.AConfig().ProductVariables.Brillo) {
254 flags.GlobalFlags = append(flags.GlobalFlags, "-D__BRILLO__")
255 }
256
257 if ctx.Device() {
258 if Bool(compiler.Properties.Rtti) {
259 flags.CppFlags = append(flags.CppFlags, "-frtti")
260 } else {
261 flags.CppFlags = append(flags.CppFlags, "-fno-rtti")
262 }
263 }
264
265 flags.AsFlags = append(flags.AsFlags, "-D__ASSEMBLY__")
266
267 if flags.Clang {
Colin Crossb98c8b02016-07-29 13:44:28 -0700268 flags.CppFlags = append(flags.CppFlags, tc.ClangCppflags())
Colin Cross4d9c2d12016-07-29 12:48:20 -0700269 } else {
Colin Crossb98c8b02016-07-29 13:44:28 -0700270 flags.CppFlags = append(flags.CppFlags, tc.Cppflags())
Colin Cross4d9c2d12016-07-29 12:48:20 -0700271 }
272 }
273
274 if flags.Clang {
Colin Crossb98c8b02016-07-29 13:44:28 -0700275 flags.GlobalFlags = append(flags.GlobalFlags, tc.ToolchainClangCflags())
Colin Cross4d9c2d12016-07-29 12:48:20 -0700276 } else {
Colin Crossb98c8b02016-07-29 13:44:28 -0700277 flags.GlobalFlags = append(flags.GlobalFlags, tc.ToolchainCflags())
Colin Cross4d9c2d12016-07-29 12:48:20 -0700278 }
279
280 if !ctx.sdk() {
281 if ctx.Host() && !flags.Clang {
282 // The host GCC doesn't support C++14 (and is deprecated, so likely
283 // never will). Build these modules with C++11.
284 flags.CppFlags = append(flags.CppFlags, "-std=gnu++11")
285 } else {
286 flags.CppFlags = append(flags.CppFlags, "-std=gnu++14")
287 }
288 }
289
290 // We can enforce some rules more strictly in the code we own. strict
291 // indicates if this is code that we can be stricter with. If we have
292 // rules that we want to apply to *our* code (but maybe can't for
293 // vendor/device specific things), we could extend this to be a ternary
294 // value.
295 strict := true
296 if strings.HasPrefix(android.PathForModuleSrc(ctx).String(), "external/") {
297 strict = false
298 }
299
300 // Can be used to make some annotations stricter for code we can fix
301 // (such as when we mark functions as deprecated).
302 if strict {
303 flags.CFlags = append(flags.CFlags, "-DANDROID_STRICT")
304 }
305
306 return flags
307}
308
309func ndkPathDeps(ctx ModuleContext) android.Paths {
310 if ctx.sdk() {
311 // The NDK sysroot timestamp file depends on all the NDK sysroot files
312 // (headers and libraries).
313 return android.Paths{getNdkSysrootTimestampFile(ctx)}
314 }
315 return nil
316}
317
318func (compiler *baseCompiler) compile(ctx ModuleContext, flags Flags, deps PathDeps) android.Paths {
319 pathDeps := deps.GeneratedHeaders
320 pathDeps = append(pathDeps, ndkPathDeps(ctx)...)
321 // Compile files listed in c.Properties.Srcs into objects
Colin Crossb916a382016-07-29 17:28:03 -0700322 objFiles := compileObjs(ctx, flags, "",
Colin Cross4d9c2d12016-07-29 12:48:20 -0700323 compiler.Properties.Srcs, compiler.Properties.Exclude_srcs,
324 deps.GeneratedSources, pathDeps)
325
326 if ctx.Failed() {
327 return nil
328 }
329
330 return objFiles
331}
332
333// Compile a list of source files into objects a specified subdirectory
Colin Crossb916a382016-07-29 17:28:03 -0700334func compileObjs(ctx android.ModuleContext, flags Flags,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700335 subdir string, srcFiles, excludes []string, extraSrcs, deps android.Paths) android.Paths {
336
337 buildFlags := flagsToBuilderFlags(flags)
338
339 inputFiles := ctx.ExpandSources(srcFiles, excludes)
340 inputFiles = append(inputFiles, extraSrcs...)
341 srcPaths, gendeps := genSources(ctx, inputFiles, buildFlags)
342
343 deps = append(deps, gendeps...)
344 deps = append(deps, flags.CFlagsDeps...)
345
346 return TransformSourceToObj(ctx, subdir, srcPaths, buildFlags, deps)
347}