Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 1 | // 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 | |
| 15 | package cc |
| 16 | |
| 17 | import ( |
| 18 | "fmt" |
| 19 | "path/filepath" |
| 20 | "strings" |
| 21 | |
| 22 | "android/soong/android" |
Colin Cross | b98c8b0 | 2016-07-29 13:44:28 -0700 | [diff] [blame] | 23 | "android/soong/cc/config" |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 24 | ) |
| 25 | |
| 26 | // This file contains the basic C/C++/assembly to .o compliation steps |
| 27 | |
| 28 | type 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 Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame^] | 92 | func NewBaseCompiler() *baseCompiler { |
| 93 | return &baseCompiler{} |
| 94 | } |
| 95 | |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 96 | type baseCompiler struct { |
| 97 | Properties BaseCompilerProperties |
| 98 | } |
| 99 | |
| 100 | var _ compiler = (*baseCompiler)(nil) |
| 101 | |
| 102 | func (compiler *baseCompiler) appendCflags(flags []string) { |
| 103 | compiler.Properties.Cflags = append(compiler.Properties.Cflags, flags...) |
| 104 | } |
| 105 | |
| 106 | func (compiler *baseCompiler) appendAsflags(flags []string) { |
| 107 | compiler.Properties.Asflags = append(compiler.Properties.Asflags, flags...) |
| 108 | } |
| 109 | |
Colin Cross | 42742b8 | 2016-08-01 13:20:05 -0700 | [diff] [blame] | 110 | func (compiler *baseCompiler) compilerProps() []interface{} { |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 111 | return []interface{}{&compiler.Properties} |
| 112 | } |
| 113 | |
Colin Cross | 42742b8 | 2016-08-01 13:20:05 -0700 | [diff] [blame] | 114 | func (compiler *baseCompiler) compilerInit(ctx BaseModuleContext) {} |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 115 | |
Colin Cross | 42742b8 | 2016-08-01 13:20:05 -0700 | [diff] [blame] | 116 | func (compiler *baseCompiler) compilerDeps(ctx BaseModuleContext, deps Deps) Deps { |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 117 | 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 Cross | 42742b8 | 2016-08-01 13:20:05 -0700 | [diff] [blame] | 125 | func (compiler *baseCompiler) compilerFlags(ctx ModuleContext, flags Flags) Flags { |
Colin Cross | b98c8b0 | 2016-07-29 13:44:28 -0700 | [diff] [blame] | 126 | tc := ctx.toolchain() |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 127 | |
| 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 Cross | b98c8b0 | 2016-07-29 13:44:28 -0700 | [diff] [blame] | 149 | "${config.CommonGlobalIncludes}", |
| 150 | tc.IncludeFlags(), |
| 151 | "${config.CommonNativehelperInclude}") |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | flags.GlobalFlags = append(flags.GlobalFlags, []string{ |
| 155 | "-I" + android.PathForModuleSrc(ctx).String(), |
| 156 | "-I" + android.PathForModuleOut(ctx).String(), |
| 157 | "-I" + android.PathForModuleGen(ctx).String(), |
| 158 | }...) |
| 159 | } |
| 160 | |
| 161 | if ctx.sdk() { |
| 162 | // The NDK headers are installed to a common sysroot. While a more |
| 163 | // typical Soong approach would be to only make the headers for the |
| 164 | // library you're using available, we're trying to emulate the NDK |
| 165 | // behavior here, and the NDK always has all the NDK headers available. |
| 166 | flags.GlobalFlags = append(flags.GlobalFlags, |
| 167 | "-isystem "+getCurrentIncludePath(ctx).String(), |
Colin Cross | b98c8b0 | 2016-07-29 13:44:28 -0700 | [diff] [blame] | 168 | "-isystem "+getCurrentIncludePath(ctx).Join(ctx, tc.ClangTriple()).String()) |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 169 | |
| 170 | // Traditionally this has come from android/api-level.h, but with the |
| 171 | // libc headers unified it must be set by the build system since we |
| 172 | // don't have per-API level copies of that header now. |
| 173 | flags.GlobalFlags = append(flags.GlobalFlags, |
| 174 | "-D__ANDROID_API__="+ctx.sdkVersion()) |
| 175 | |
| 176 | // Until the full NDK has been migrated to using ndk_headers, we still |
| 177 | // need to add the legacy sysroot includes to get the full set of |
| 178 | // headers. |
| 179 | legacyIncludes := fmt.Sprintf( |
| 180 | "prebuilts/ndk/current/platforms/android-%s/arch-%s/usr/include", |
| 181 | ctx.sdkVersion(), ctx.Arch().ArchType.String()) |
| 182 | flags.GlobalFlags = append(flags.GlobalFlags, "-isystem "+legacyIncludes) |
| 183 | } |
| 184 | |
| 185 | instructionSet := compiler.Properties.Instruction_set |
| 186 | if flags.RequiredInstructionSet != "" { |
| 187 | instructionSet = flags.RequiredInstructionSet |
| 188 | } |
Colin Cross | b98c8b0 | 2016-07-29 13:44:28 -0700 | [diff] [blame] | 189 | instructionSetFlags, err := tc.InstructionSetFlags(instructionSet) |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 190 | if flags.Clang { |
Colin Cross | b98c8b0 | 2016-07-29 13:44:28 -0700 | [diff] [blame] | 191 | instructionSetFlags, err = tc.ClangInstructionSetFlags(instructionSet) |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 192 | } |
| 193 | if err != nil { |
| 194 | ctx.ModuleErrorf("%s", err) |
| 195 | } |
| 196 | |
| 197 | CheckBadCompilerFlags(ctx, "release.cflags", compiler.Properties.Release.Cflags) |
| 198 | |
| 199 | // TODO: debug |
| 200 | flags.CFlags = append(flags.CFlags, compiler.Properties.Release.Cflags...) |
| 201 | |
| 202 | if flags.Clang { |
| 203 | CheckBadCompilerFlags(ctx, "clang_cflags", compiler.Properties.Clang_cflags) |
| 204 | CheckBadCompilerFlags(ctx, "clang_asflags", compiler.Properties.Clang_asflags) |
| 205 | |
Colin Cross | b98c8b0 | 2016-07-29 13:44:28 -0700 | [diff] [blame] | 206 | flags.CFlags = config.ClangFilterUnknownCflags(flags.CFlags) |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 207 | flags.CFlags = append(flags.CFlags, compiler.Properties.Clang_cflags...) |
| 208 | flags.AsFlags = append(flags.AsFlags, compiler.Properties.Clang_asflags...) |
Colin Cross | b98c8b0 | 2016-07-29 13:44:28 -0700 | [diff] [blame] | 209 | flags.CppFlags = config.ClangFilterUnknownCflags(flags.CppFlags) |
| 210 | flags.ConlyFlags = config.ClangFilterUnknownCflags(flags.ConlyFlags) |
| 211 | flags.LdFlags = config.ClangFilterUnknownCflags(flags.LdFlags) |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 212 | |
Colin Cross | b98c8b0 | 2016-07-29 13:44:28 -0700 | [diff] [blame] | 213 | target := "-target " + tc.ClangTriple() |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 214 | var gccPrefix string |
| 215 | if !ctx.Darwin() { |
Colin Cross | b98c8b0 | 2016-07-29 13:44:28 -0700 | [diff] [blame] | 216 | gccPrefix = "-B" + filepath.Join(tc.GccRoot(), tc.GccTriple(), "bin") |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | flags.CFlags = append(flags.CFlags, target, gccPrefix) |
| 220 | flags.AsFlags = append(flags.AsFlags, target, gccPrefix) |
| 221 | flags.LdFlags = append(flags.LdFlags, target, gccPrefix) |
| 222 | } |
| 223 | |
Colin Cross | b98c8b0 | 2016-07-29 13:44:28 -0700 | [diff] [blame] | 224 | hod := "Host" |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 225 | if ctx.Os().Class == android.Device { |
Colin Cross | b98c8b0 | 2016-07-29 13:44:28 -0700 | [diff] [blame] | 226 | hod = "Device" |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | if !ctx.noDefaultCompilerFlags() { |
| 230 | flags.GlobalFlags = append(flags.GlobalFlags, instructionSetFlags) |
| 231 | |
| 232 | if flags.Clang { |
Colin Cross | b98c8b0 | 2016-07-29 13:44:28 -0700 | [diff] [blame] | 233 | flags.AsFlags = append(flags.AsFlags, tc.ClangAsflags()) |
| 234 | flags.CppFlags = append(flags.CppFlags, "${config.CommonClangGlobalCppflags}") |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 235 | flags.GlobalFlags = append(flags.GlobalFlags, |
Colin Cross | b98c8b0 | 2016-07-29 13:44:28 -0700 | [diff] [blame] | 236 | tc.ClangCflags(), |
| 237 | "${config.CommonClangGlobalCflags}", |
| 238 | fmt.Sprintf("${config.%sClangGlobalCflags}", hod)) |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 239 | |
Colin Cross | b98c8b0 | 2016-07-29 13:44:28 -0700 | [diff] [blame] | 240 | flags.ConlyFlags = append(flags.ConlyFlags, "${config.ClangExtraConlyflags}") |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 241 | } else { |
Colin Cross | b98c8b0 | 2016-07-29 13:44:28 -0700 | [diff] [blame] | 242 | flags.CppFlags = append(flags.CppFlags, "${config.CommonGlobalCppflags}") |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 243 | flags.GlobalFlags = append(flags.GlobalFlags, |
Colin Cross | b98c8b0 | 2016-07-29 13:44:28 -0700 | [diff] [blame] | 244 | tc.Cflags(), |
| 245 | "${config.CommonGlobalCflags}", |
| 246 | fmt.Sprintf("${config.%sGlobalCflags}", hod)) |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | if Bool(ctx.AConfig().ProductVariables.Brillo) { |
| 250 | flags.GlobalFlags = append(flags.GlobalFlags, "-D__BRILLO__") |
| 251 | } |
| 252 | |
| 253 | if ctx.Device() { |
| 254 | if Bool(compiler.Properties.Rtti) { |
| 255 | flags.CppFlags = append(flags.CppFlags, "-frtti") |
| 256 | } else { |
| 257 | flags.CppFlags = append(flags.CppFlags, "-fno-rtti") |
| 258 | } |
| 259 | } |
| 260 | |
| 261 | flags.AsFlags = append(flags.AsFlags, "-D__ASSEMBLY__") |
| 262 | |
| 263 | if flags.Clang { |
Colin Cross | b98c8b0 | 2016-07-29 13:44:28 -0700 | [diff] [blame] | 264 | flags.CppFlags = append(flags.CppFlags, tc.ClangCppflags()) |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 265 | } else { |
Colin Cross | b98c8b0 | 2016-07-29 13:44:28 -0700 | [diff] [blame] | 266 | flags.CppFlags = append(flags.CppFlags, tc.Cppflags()) |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 267 | } |
| 268 | } |
| 269 | |
| 270 | if flags.Clang { |
Colin Cross | b98c8b0 | 2016-07-29 13:44:28 -0700 | [diff] [blame] | 271 | flags.GlobalFlags = append(flags.GlobalFlags, tc.ToolchainClangCflags()) |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 272 | } else { |
Colin Cross | b98c8b0 | 2016-07-29 13:44:28 -0700 | [diff] [blame] | 273 | flags.GlobalFlags = append(flags.GlobalFlags, tc.ToolchainCflags()) |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 274 | } |
| 275 | |
| 276 | if !ctx.sdk() { |
| 277 | if ctx.Host() && !flags.Clang { |
| 278 | // The host GCC doesn't support C++14 (and is deprecated, so likely |
| 279 | // never will). Build these modules with C++11. |
| 280 | flags.CppFlags = append(flags.CppFlags, "-std=gnu++11") |
| 281 | } else { |
| 282 | flags.CppFlags = append(flags.CppFlags, "-std=gnu++14") |
| 283 | } |
| 284 | } |
| 285 | |
| 286 | // We can enforce some rules more strictly in the code we own. strict |
| 287 | // indicates if this is code that we can be stricter with. If we have |
| 288 | // rules that we want to apply to *our* code (but maybe can't for |
| 289 | // vendor/device specific things), we could extend this to be a ternary |
| 290 | // value. |
| 291 | strict := true |
| 292 | if strings.HasPrefix(android.PathForModuleSrc(ctx).String(), "external/") { |
| 293 | strict = false |
| 294 | } |
| 295 | |
| 296 | // Can be used to make some annotations stricter for code we can fix |
| 297 | // (such as when we mark functions as deprecated). |
| 298 | if strict { |
| 299 | flags.CFlags = append(flags.CFlags, "-DANDROID_STRICT") |
| 300 | } |
| 301 | |
| 302 | return flags |
| 303 | } |
| 304 | |
| 305 | func ndkPathDeps(ctx ModuleContext) android.Paths { |
| 306 | if ctx.sdk() { |
| 307 | // The NDK sysroot timestamp file depends on all the NDK sysroot files |
| 308 | // (headers and libraries). |
| 309 | return android.Paths{getNdkSysrootTimestampFile(ctx)} |
| 310 | } |
| 311 | return nil |
| 312 | } |
| 313 | |
| 314 | func (compiler *baseCompiler) compile(ctx ModuleContext, flags Flags, deps PathDeps) android.Paths { |
| 315 | pathDeps := deps.GeneratedHeaders |
| 316 | pathDeps = append(pathDeps, ndkPathDeps(ctx)...) |
| 317 | // Compile files listed in c.Properties.Srcs into objects |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame^] | 318 | objFiles := compileObjs(ctx, flags, "", |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 319 | compiler.Properties.Srcs, compiler.Properties.Exclude_srcs, |
| 320 | deps.GeneratedSources, pathDeps) |
| 321 | |
| 322 | if ctx.Failed() { |
| 323 | return nil |
| 324 | } |
| 325 | |
| 326 | return objFiles |
| 327 | } |
| 328 | |
| 329 | // Compile a list of source files into objects a specified subdirectory |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame^] | 330 | func compileObjs(ctx android.ModuleContext, flags Flags, |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 331 | subdir string, srcFiles, excludes []string, extraSrcs, deps android.Paths) android.Paths { |
| 332 | |
| 333 | buildFlags := flagsToBuilderFlags(flags) |
| 334 | |
| 335 | inputFiles := ctx.ExpandSources(srcFiles, excludes) |
| 336 | inputFiles = append(inputFiles, extraSrcs...) |
| 337 | srcPaths, gendeps := genSources(ctx, inputFiles, buildFlags) |
| 338 | |
| 339 | deps = append(deps, gendeps...) |
| 340 | deps = append(deps, flags.CFlagsDeps...) |
| 341 | |
| 342 | return TransformSourceToObj(ctx, subdir, srcPaths, buildFlags, deps) |
| 343 | } |