blob: 0182491e07aae80d92efe0ad1b173df29e7ed6bb [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
92type baseCompiler struct {
93 Properties BaseCompilerProperties
94}
95
96var _ compiler = (*baseCompiler)(nil)
97
98func (compiler *baseCompiler) appendCflags(flags []string) {
99 compiler.Properties.Cflags = append(compiler.Properties.Cflags, flags...)
100}
101
102func (compiler *baseCompiler) appendAsflags(flags []string) {
103 compiler.Properties.Asflags = append(compiler.Properties.Asflags, flags...)
104}
105
Colin Cross42742b82016-08-01 13:20:05 -0700106func (compiler *baseCompiler) compilerProps() []interface{} {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700107 return []interface{}{&compiler.Properties}
108}
109
Colin Cross42742b82016-08-01 13:20:05 -0700110func (compiler *baseCompiler) compilerInit(ctx BaseModuleContext) {}
Colin Cross4d9c2d12016-07-29 12:48:20 -0700111
Colin Cross42742b82016-08-01 13:20:05 -0700112func (compiler *baseCompiler) compilerDeps(ctx BaseModuleContext, deps Deps) Deps {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700113 deps.GeneratedSources = append(deps.GeneratedSources, compiler.Properties.Generated_sources...)
114 deps.GeneratedHeaders = append(deps.GeneratedHeaders, compiler.Properties.Generated_headers...)
115
116 return deps
117}
118
119// Create a Flags struct that collects the compile flags from global values,
120// per-target values, module type values, and per-module Blueprints properties
Colin Cross42742b82016-08-01 13:20:05 -0700121func (compiler *baseCompiler) compilerFlags(ctx ModuleContext, flags Flags) Flags {
Colin Crossb98c8b02016-07-29 13:44:28 -0700122 tc := ctx.toolchain()
Colin Cross4d9c2d12016-07-29 12:48:20 -0700123
124 CheckBadCompilerFlags(ctx, "cflags", compiler.Properties.Cflags)
125 CheckBadCompilerFlags(ctx, "cppflags", compiler.Properties.Cppflags)
126 CheckBadCompilerFlags(ctx, "conlyflags", compiler.Properties.Conlyflags)
127 CheckBadCompilerFlags(ctx, "asflags", compiler.Properties.Asflags)
128
129 flags.CFlags = append(flags.CFlags, compiler.Properties.Cflags...)
130 flags.CppFlags = append(flags.CppFlags, compiler.Properties.Cppflags...)
131 flags.ConlyFlags = append(flags.ConlyFlags, compiler.Properties.Conlyflags...)
132 flags.AsFlags = append(flags.AsFlags, compiler.Properties.Asflags...)
133 flags.YaccFlags = append(flags.YaccFlags, compiler.Properties.Yaccflags...)
134
135 // Include dir cflags
136 rootIncludeDirs := android.PathsForSource(ctx, compiler.Properties.Include_dirs)
137 localIncludeDirs := android.PathsForModuleSrc(ctx, compiler.Properties.Local_include_dirs)
138 flags.GlobalFlags = append(flags.GlobalFlags,
139 includeDirsToFlags(localIncludeDirs),
140 includeDirsToFlags(rootIncludeDirs))
141
142 if !ctx.noDefaultCompilerFlags() {
143 if !ctx.sdk() || ctx.Host() {
144 flags.GlobalFlags = append(flags.GlobalFlags,
Colin Crossb98c8b02016-07-29 13:44:28 -0700145 "${config.CommonGlobalIncludes}",
146 tc.IncludeFlags(),
147 "${config.CommonNativehelperInclude}")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700148 }
149
150 flags.GlobalFlags = append(flags.GlobalFlags, []string{
151 "-I" + android.PathForModuleSrc(ctx).String(),
152 "-I" + android.PathForModuleOut(ctx).String(),
153 "-I" + android.PathForModuleGen(ctx).String(),
154 }...)
155 }
156
157 if ctx.sdk() {
158 // The NDK headers are installed to a common sysroot. While a more
159 // typical Soong approach would be to only make the headers for the
160 // library you're using available, we're trying to emulate the NDK
161 // behavior here, and the NDK always has all the NDK headers available.
162 flags.GlobalFlags = append(flags.GlobalFlags,
163 "-isystem "+getCurrentIncludePath(ctx).String(),
Colin Crossb98c8b02016-07-29 13:44:28 -0700164 "-isystem "+getCurrentIncludePath(ctx).Join(ctx, tc.ClangTriple()).String())
Colin Cross4d9c2d12016-07-29 12:48:20 -0700165
166 // Traditionally this has come from android/api-level.h, but with the
167 // libc headers unified it must be set by the build system since we
168 // don't have per-API level copies of that header now.
169 flags.GlobalFlags = append(flags.GlobalFlags,
170 "-D__ANDROID_API__="+ctx.sdkVersion())
171
172 // Until the full NDK has been migrated to using ndk_headers, we still
173 // need to add the legacy sysroot includes to get the full set of
174 // headers.
175 legacyIncludes := fmt.Sprintf(
176 "prebuilts/ndk/current/platforms/android-%s/arch-%s/usr/include",
177 ctx.sdkVersion(), ctx.Arch().ArchType.String())
178 flags.GlobalFlags = append(flags.GlobalFlags, "-isystem "+legacyIncludes)
179 }
180
181 instructionSet := compiler.Properties.Instruction_set
182 if flags.RequiredInstructionSet != "" {
183 instructionSet = flags.RequiredInstructionSet
184 }
Colin Crossb98c8b02016-07-29 13:44:28 -0700185 instructionSetFlags, err := tc.InstructionSetFlags(instructionSet)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700186 if flags.Clang {
Colin Crossb98c8b02016-07-29 13:44:28 -0700187 instructionSetFlags, err = tc.ClangInstructionSetFlags(instructionSet)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700188 }
189 if err != nil {
190 ctx.ModuleErrorf("%s", err)
191 }
192
193 CheckBadCompilerFlags(ctx, "release.cflags", compiler.Properties.Release.Cflags)
194
195 // TODO: debug
196 flags.CFlags = append(flags.CFlags, compiler.Properties.Release.Cflags...)
197
198 if flags.Clang {
199 CheckBadCompilerFlags(ctx, "clang_cflags", compiler.Properties.Clang_cflags)
200 CheckBadCompilerFlags(ctx, "clang_asflags", compiler.Properties.Clang_asflags)
201
Colin Crossb98c8b02016-07-29 13:44:28 -0700202 flags.CFlags = config.ClangFilterUnknownCflags(flags.CFlags)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700203 flags.CFlags = append(flags.CFlags, compiler.Properties.Clang_cflags...)
204 flags.AsFlags = append(flags.AsFlags, compiler.Properties.Clang_asflags...)
Colin Crossb98c8b02016-07-29 13:44:28 -0700205 flags.CppFlags = config.ClangFilterUnknownCflags(flags.CppFlags)
206 flags.ConlyFlags = config.ClangFilterUnknownCflags(flags.ConlyFlags)
207 flags.LdFlags = config.ClangFilterUnknownCflags(flags.LdFlags)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700208
Colin Crossb98c8b02016-07-29 13:44:28 -0700209 target := "-target " + tc.ClangTriple()
Colin Cross4d9c2d12016-07-29 12:48:20 -0700210 var gccPrefix string
211 if !ctx.Darwin() {
Colin Crossb98c8b02016-07-29 13:44:28 -0700212 gccPrefix = "-B" + filepath.Join(tc.GccRoot(), tc.GccTriple(), "bin")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700213 }
214
215 flags.CFlags = append(flags.CFlags, target, gccPrefix)
216 flags.AsFlags = append(flags.AsFlags, target, gccPrefix)
217 flags.LdFlags = append(flags.LdFlags, target, gccPrefix)
218 }
219
Colin Crossb98c8b02016-07-29 13:44:28 -0700220 hod := "Host"
Colin Cross4d9c2d12016-07-29 12:48:20 -0700221 if ctx.Os().Class == android.Device {
Colin Crossb98c8b02016-07-29 13:44:28 -0700222 hod = "Device"
Colin Cross4d9c2d12016-07-29 12:48:20 -0700223 }
224
225 if !ctx.noDefaultCompilerFlags() {
226 flags.GlobalFlags = append(flags.GlobalFlags, instructionSetFlags)
227
228 if flags.Clang {
Colin Crossb98c8b02016-07-29 13:44:28 -0700229 flags.AsFlags = append(flags.AsFlags, tc.ClangAsflags())
230 flags.CppFlags = append(flags.CppFlags, "${config.CommonClangGlobalCppflags}")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700231 flags.GlobalFlags = append(flags.GlobalFlags,
Colin Crossb98c8b02016-07-29 13:44:28 -0700232 tc.ClangCflags(),
233 "${config.CommonClangGlobalCflags}",
234 fmt.Sprintf("${config.%sClangGlobalCflags}", hod))
Colin Cross4d9c2d12016-07-29 12:48:20 -0700235
Colin Crossb98c8b02016-07-29 13:44:28 -0700236 flags.ConlyFlags = append(flags.ConlyFlags, "${config.ClangExtraConlyflags}")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700237 } else {
Colin Crossb98c8b02016-07-29 13:44:28 -0700238 flags.CppFlags = append(flags.CppFlags, "${config.CommonGlobalCppflags}")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700239 flags.GlobalFlags = append(flags.GlobalFlags,
Colin Crossb98c8b02016-07-29 13:44:28 -0700240 tc.Cflags(),
241 "${config.CommonGlobalCflags}",
242 fmt.Sprintf("${config.%sGlobalCflags}", hod))
Colin Cross4d9c2d12016-07-29 12:48:20 -0700243 }
244
245 if Bool(ctx.AConfig().ProductVariables.Brillo) {
246 flags.GlobalFlags = append(flags.GlobalFlags, "-D__BRILLO__")
247 }
248
249 if ctx.Device() {
250 if Bool(compiler.Properties.Rtti) {
251 flags.CppFlags = append(flags.CppFlags, "-frtti")
252 } else {
253 flags.CppFlags = append(flags.CppFlags, "-fno-rtti")
254 }
255 }
256
257 flags.AsFlags = append(flags.AsFlags, "-D__ASSEMBLY__")
258
259 if flags.Clang {
Colin Crossb98c8b02016-07-29 13:44:28 -0700260 flags.CppFlags = append(flags.CppFlags, tc.ClangCppflags())
Colin Cross4d9c2d12016-07-29 12:48:20 -0700261 } else {
Colin Crossb98c8b02016-07-29 13:44:28 -0700262 flags.CppFlags = append(flags.CppFlags, tc.Cppflags())
Colin Cross4d9c2d12016-07-29 12:48:20 -0700263 }
264 }
265
266 if flags.Clang {
Colin Crossb98c8b02016-07-29 13:44:28 -0700267 flags.GlobalFlags = append(flags.GlobalFlags, tc.ToolchainClangCflags())
Colin Cross4d9c2d12016-07-29 12:48:20 -0700268 } else {
Colin Crossb98c8b02016-07-29 13:44:28 -0700269 flags.GlobalFlags = append(flags.GlobalFlags, tc.ToolchainCflags())
Colin Cross4d9c2d12016-07-29 12:48:20 -0700270 }
271
272 if !ctx.sdk() {
273 if ctx.Host() && !flags.Clang {
274 // The host GCC doesn't support C++14 (and is deprecated, so likely
275 // never will). Build these modules with C++11.
276 flags.CppFlags = append(flags.CppFlags, "-std=gnu++11")
277 } else {
278 flags.CppFlags = append(flags.CppFlags, "-std=gnu++14")
279 }
280 }
281
282 // We can enforce some rules more strictly in the code we own. strict
283 // indicates if this is code that we can be stricter with. If we have
284 // rules that we want to apply to *our* code (but maybe can't for
285 // vendor/device specific things), we could extend this to be a ternary
286 // value.
287 strict := true
288 if strings.HasPrefix(android.PathForModuleSrc(ctx).String(), "external/") {
289 strict = false
290 }
291
292 // Can be used to make some annotations stricter for code we can fix
293 // (such as when we mark functions as deprecated).
294 if strict {
295 flags.CFlags = append(flags.CFlags, "-DANDROID_STRICT")
296 }
297
298 return flags
299}
300
301func ndkPathDeps(ctx ModuleContext) android.Paths {
302 if ctx.sdk() {
303 // The NDK sysroot timestamp file depends on all the NDK sysroot files
304 // (headers and libraries).
305 return android.Paths{getNdkSysrootTimestampFile(ctx)}
306 }
307 return nil
308}
309
310func (compiler *baseCompiler) compile(ctx ModuleContext, flags Flags, deps PathDeps) android.Paths {
311 pathDeps := deps.GeneratedHeaders
312 pathDeps = append(pathDeps, ndkPathDeps(ctx)...)
313 // Compile files listed in c.Properties.Srcs into objects
314 objFiles := compiler.compileObjs(ctx, flags, "",
315 compiler.Properties.Srcs, compiler.Properties.Exclude_srcs,
316 deps.GeneratedSources, pathDeps)
317
318 if ctx.Failed() {
319 return nil
320 }
321
322 return objFiles
323}
324
325// Compile a list of source files into objects a specified subdirectory
326func (compiler *baseCompiler) compileObjs(ctx android.ModuleContext, flags Flags,
327 subdir string, srcFiles, excludes []string, extraSrcs, deps android.Paths) android.Paths {
328
329 buildFlags := flagsToBuilderFlags(flags)
330
331 inputFiles := ctx.ExpandSources(srcFiles, excludes)
332 inputFiles = append(inputFiles, extraSrcs...)
333 srcPaths, gendeps := genSources(ctx, inputFiles, buildFlags)
334
335 deps = append(deps, gendeps...)
336 deps = append(deps, flags.CFlagsDeps...)
337
338 return TransformSourceToObj(ctx, subdir, srcPaths, buildFlags, deps)
339}