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" |
| 23 | ) |
| 24 | |
| 25 | // This file contains the basic C/C++/assembly to .o compliation steps |
| 26 | |
| 27 | type BaseCompilerProperties struct { |
| 28 | // list of source files used to compile the C/C++ module. May be .c, .cpp, or .S files. |
| 29 | Srcs []string `android:"arch_variant"` |
| 30 | |
| 31 | // list of source files that should not be used to build the C/C++ module. |
| 32 | // This is most useful in the arch/multilib variants to remove non-common files |
| 33 | Exclude_srcs []string `android:"arch_variant"` |
| 34 | |
| 35 | // list of module-specific flags that will be used for C and C++ compiles. |
| 36 | Cflags []string `android:"arch_variant"` |
| 37 | |
| 38 | // list of module-specific flags that will be used for C++ compiles |
| 39 | Cppflags []string `android:"arch_variant"` |
| 40 | |
| 41 | // list of module-specific flags that will be used for C compiles |
| 42 | Conlyflags []string `android:"arch_variant"` |
| 43 | |
| 44 | // list of module-specific flags that will be used for .S compiles |
| 45 | Asflags []string `android:"arch_variant"` |
| 46 | |
| 47 | // list of module-specific flags that will be used for C and C++ compiles when |
| 48 | // compiling with clang |
| 49 | Clang_cflags []string `android:"arch_variant"` |
| 50 | |
| 51 | // list of module-specific flags that will be used for .S compiles when |
| 52 | // compiling with clang |
| 53 | Clang_asflags []string `android:"arch_variant"` |
| 54 | |
| 55 | // list of module-specific flags that will be used for .y and .yy compiles |
| 56 | Yaccflags []string |
| 57 | |
| 58 | // the instruction set architecture to use to compile the C/C++ |
| 59 | // module. |
| 60 | Instruction_set string `android:"arch_variant"` |
| 61 | |
| 62 | // list of directories relative to the root of the source tree that will |
| 63 | // be added to the include path using -I. |
| 64 | // If possible, don't use this. If adding paths from the current directory use |
| 65 | // local_include_dirs, if adding paths from other modules use export_include_dirs in |
| 66 | // that module. |
| 67 | Include_dirs []string `android:"arch_variant"` |
| 68 | |
| 69 | // list of directories relative to the Blueprints file that will |
| 70 | // be added to the include path using -I |
| 71 | Local_include_dirs []string `android:"arch_variant"` |
| 72 | |
| 73 | // list of generated sources to compile. These are the names of gensrcs or |
| 74 | // genrule modules. |
| 75 | Generated_sources []string `android:"arch_variant"` |
| 76 | |
| 77 | // list of generated headers to add to the include path. These are the names |
| 78 | // of genrule modules. |
| 79 | Generated_headers []string `android:"arch_variant"` |
| 80 | |
| 81 | // pass -frtti instead of -fno-rtti |
| 82 | Rtti *bool |
| 83 | |
| 84 | Debug, Release struct { |
| 85 | // list of module-specific flags that will be used for C and C++ compiles in debug or |
| 86 | // release builds |
| 87 | Cflags []string `android:"arch_variant"` |
| 88 | } `android:"arch_variant"` |
| 89 | } |
| 90 | |
| 91 | type baseCompiler struct { |
| 92 | Properties BaseCompilerProperties |
| 93 | } |
| 94 | |
| 95 | var _ compiler = (*baseCompiler)(nil) |
| 96 | |
| 97 | func (compiler *baseCompiler) appendCflags(flags []string) { |
| 98 | compiler.Properties.Cflags = append(compiler.Properties.Cflags, flags...) |
| 99 | } |
| 100 | |
| 101 | func (compiler *baseCompiler) appendAsflags(flags []string) { |
| 102 | compiler.Properties.Asflags = append(compiler.Properties.Asflags, flags...) |
| 103 | } |
| 104 | |
| 105 | func (compiler *baseCompiler) props() []interface{} { |
| 106 | return []interface{}{&compiler.Properties} |
| 107 | } |
| 108 | |
| 109 | func (compiler *baseCompiler) begin(ctx BaseModuleContext) {} |
| 110 | |
| 111 | func (compiler *baseCompiler) deps(ctx BaseModuleContext, deps Deps) Deps { |
| 112 | deps.GeneratedSources = append(deps.GeneratedSources, compiler.Properties.Generated_sources...) |
| 113 | deps.GeneratedHeaders = append(deps.GeneratedHeaders, compiler.Properties.Generated_headers...) |
| 114 | |
| 115 | return deps |
| 116 | } |
| 117 | |
| 118 | // Create a Flags struct that collects the compile flags from global values, |
| 119 | // per-target values, module type values, and per-module Blueprints properties |
| 120 | func (compiler *baseCompiler) flags(ctx ModuleContext, flags Flags) Flags { |
| 121 | toolchain := ctx.toolchain() |
| 122 | |
| 123 | CheckBadCompilerFlags(ctx, "cflags", compiler.Properties.Cflags) |
| 124 | CheckBadCompilerFlags(ctx, "cppflags", compiler.Properties.Cppflags) |
| 125 | CheckBadCompilerFlags(ctx, "conlyflags", compiler.Properties.Conlyflags) |
| 126 | CheckBadCompilerFlags(ctx, "asflags", compiler.Properties.Asflags) |
| 127 | |
| 128 | flags.CFlags = append(flags.CFlags, compiler.Properties.Cflags...) |
| 129 | flags.CppFlags = append(flags.CppFlags, compiler.Properties.Cppflags...) |
| 130 | flags.ConlyFlags = append(flags.ConlyFlags, compiler.Properties.Conlyflags...) |
| 131 | flags.AsFlags = append(flags.AsFlags, compiler.Properties.Asflags...) |
| 132 | flags.YaccFlags = append(flags.YaccFlags, compiler.Properties.Yaccflags...) |
| 133 | |
| 134 | // Include dir cflags |
| 135 | rootIncludeDirs := android.PathsForSource(ctx, compiler.Properties.Include_dirs) |
| 136 | localIncludeDirs := android.PathsForModuleSrc(ctx, compiler.Properties.Local_include_dirs) |
| 137 | flags.GlobalFlags = append(flags.GlobalFlags, |
| 138 | includeDirsToFlags(localIncludeDirs), |
| 139 | includeDirsToFlags(rootIncludeDirs)) |
| 140 | |
| 141 | if !ctx.noDefaultCompilerFlags() { |
| 142 | if !ctx.sdk() || ctx.Host() { |
| 143 | flags.GlobalFlags = append(flags.GlobalFlags, |
| 144 | "${commonGlobalIncludes}", |
| 145 | toolchain.IncludeFlags(), |
| 146 | "${commonNativehelperInclude}") |
| 147 | } |
| 148 | |
| 149 | flags.GlobalFlags = append(flags.GlobalFlags, []string{ |
| 150 | "-I" + android.PathForModuleSrc(ctx).String(), |
| 151 | "-I" + android.PathForModuleOut(ctx).String(), |
| 152 | "-I" + android.PathForModuleGen(ctx).String(), |
| 153 | }...) |
| 154 | } |
| 155 | |
| 156 | if ctx.sdk() { |
| 157 | // The NDK headers are installed to a common sysroot. While a more |
| 158 | // typical Soong approach would be to only make the headers for the |
| 159 | // library you're using available, we're trying to emulate the NDK |
| 160 | // behavior here, and the NDK always has all the NDK headers available. |
| 161 | flags.GlobalFlags = append(flags.GlobalFlags, |
| 162 | "-isystem "+getCurrentIncludePath(ctx).String(), |
| 163 | "-isystem "+getCurrentIncludePath(ctx).Join(ctx, toolchain.ClangTriple()).String()) |
| 164 | |
| 165 | // Traditionally this has come from android/api-level.h, but with the |
| 166 | // libc headers unified it must be set by the build system since we |
| 167 | // don't have per-API level copies of that header now. |
| 168 | flags.GlobalFlags = append(flags.GlobalFlags, |
| 169 | "-D__ANDROID_API__="+ctx.sdkVersion()) |
| 170 | |
| 171 | // Until the full NDK has been migrated to using ndk_headers, we still |
| 172 | // need to add the legacy sysroot includes to get the full set of |
| 173 | // headers. |
| 174 | legacyIncludes := fmt.Sprintf( |
| 175 | "prebuilts/ndk/current/platforms/android-%s/arch-%s/usr/include", |
| 176 | ctx.sdkVersion(), ctx.Arch().ArchType.String()) |
| 177 | flags.GlobalFlags = append(flags.GlobalFlags, "-isystem "+legacyIncludes) |
| 178 | } |
| 179 | |
| 180 | instructionSet := compiler.Properties.Instruction_set |
| 181 | if flags.RequiredInstructionSet != "" { |
| 182 | instructionSet = flags.RequiredInstructionSet |
| 183 | } |
| 184 | instructionSetFlags, err := toolchain.InstructionSetFlags(instructionSet) |
| 185 | if flags.Clang { |
| 186 | instructionSetFlags, err = toolchain.ClangInstructionSetFlags(instructionSet) |
| 187 | } |
| 188 | if err != nil { |
| 189 | ctx.ModuleErrorf("%s", err) |
| 190 | } |
| 191 | |
| 192 | CheckBadCompilerFlags(ctx, "release.cflags", compiler.Properties.Release.Cflags) |
| 193 | |
| 194 | // TODO: debug |
| 195 | flags.CFlags = append(flags.CFlags, compiler.Properties.Release.Cflags...) |
| 196 | |
| 197 | if flags.Clang { |
| 198 | CheckBadCompilerFlags(ctx, "clang_cflags", compiler.Properties.Clang_cflags) |
| 199 | CheckBadCompilerFlags(ctx, "clang_asflags", compiler.Properties.Clang_asflags) |
| 200 | |
| 201 | flags.CFlags = clangFilterUnknownCflags(flags.CFlags) |
| 202 | flags.CFlags = append(flags.CFlags, compiler.Properties.Clang_cflags...) |
| 203 | flags.AsFlags = append(flags.AsFlags, compiler.Properties.Clang_asflags...) |
| 204 | flags.CppFlags = clangFilterUnknownCflags(flags.CppFlags) |
| 205 | flags.ConlyFlags = clangFilterUnknownCflags(flags.ConlyFlags) |
| 206 | flags.LdFlags = clangFilterUnknownCflags(flags.LdFlags) |
| 207 | |
| 208 | target := "-target " + toolchain.ClangTriple() |
| 209 | var gccPrefix string |
| 210 | if !ctx.Darwin() { |
| 211 | gccPrefix = "-B" + filepath.Join(toolchain.GccRoot(), toolchain.GccTriple(), "bin") |
| 212 | } |
| 213 | |
| 214 | flags.CFlags = append(flags.CFlags, target, gccPrefix) |
| 215 | flags.AsFlags = append(flags.AsFlags, target, gccPrefix) |
| 216 | flags.LdFlags = append(flags.LdFlags, target, gccPrefix) |
| 217 | } |
| 218 | |
| 219 | hod := "host" |
| 220 | if ctx.Os().Class == android.Device { |
| 221 | hod = "device" |
| 222 | } |
| 223 | |
| 224 | if !ctx.noDefaultCompilerFlags() { |
| 225 | flags.GlobalFlags = append(flags.GlobalFlags, instructionSetFlags) |
| 226 | |
| 227 | if flags.Clang { |
| 228 | flags.AsFlags = append(flags.AsFlags, toolchain.ClangAsflags()) |
| 229 | flags.CppFlags = append(flags.CppFlags, "${commonClangGlobalCppflags}") |
| 230 | flags.GlobalFlags = append(flags.GlobalFlags, |
| 231 | toolchain.ClangCflags(), |
| 232 | "${commonClangGlobalCflags}", |
| 233 | fmt.Sprintf("${%sClangGlobalCflags}", hod)) |
| 234 | |
| 235 | flags.ConlyFlags = append(flags.ConlyFlags, "${clangExtraConlyflags}") |
| 236 | } else { |
| 237 | flags.CppFlags = append(flags.CppFlags, "${commonGlobalCppflags}") |
| 238 | flags.GlobalFlags = append(flags.GlobalFlags, |
| 239 | toolchain.Cflags(), |
| 240 | "${commonGlobalCflags}", |
| 241 | fmt.Sprintf("${%sGlobalCflags}", hod)) |
| 242 | } |
| 243 | |
| 244 | if Bool(ctx.AConfig().ProductVariables.Brillo) { |
| 245 | flags.GlobalFlags = append(flags.GlobalFlags, "-D__BRILLO__") |
| 246 | } |
| 247 | |
| 248 | if ctx.Device() { |
| 249 | if Bool(compiler.Properties.Rtti) { |
| 250 | flags.CppFlags = append(flags.CppFlags, "-frtti") |
| 251 | } else { |
| 252 | flags.CppFlags = append(flags.CppFlags, "-fno-rtti") |
| 253 | } |
| 254 | } |
| 255 | |
| 256 | flags.AsFlags = append(flags.AsFlags, "-D__ASSEMBLY__") |
| 257 | |
| 258 | if flags.Clang { |
| 259 | flags.CppFlags = append(flags.CppFlags, toolchain.ClangCppflags()) |
| 260 | } else { |
| 261 | flags.CppFlags = append(flags.CppFlags, toolchain.Cppflags()) |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | if flags.Clang { |
| 266 | flags.GlobalFlags = append(flags.GlobalFlags, toolchain.ToolchainClangCflags()) |
| 267 | } else { |
| 268 | flags.GlobalFlags = append(flags.GlobalFlags, toolchain.ToolchainCflags()) |
| 269 | } |
| 270 | |
| 271 | if !ctx.sdk() { |
| 272 | if ctx.Host() && !flags.Clang { |
| 273 | // The host GCC doesn't support C++14 (and is deprecated, so likely |
| 274 | // never will). Build these modules with C++11. |
| 275 | flags.CppFlags = append(flags.CppFlags, "-std=gnu++11") |
| 276 | } else { |
| 277 | flags.CppFlags = append(flags.CppFlags, "-std=gnu++14") |
| 278 | } |
| 279 | } |
| 280 | |
| 281 | // We can enforce some rules more strictly in the code we own. strict |
| 282 | // indicates if this is code that we can be stricter with. If we have |
| 283 | // rules that we want to apply to *our* code (but maybe can't for |
| 284 | // vendor/device specific things), we could extend this to be a ternary |
| 285 | // value. |
| 286 | strict := true |
| 287 | if strings.HasPrefix(android.PathForModuleSrc(ctx).String(), "external/") { |
| 288 | strict = false |
| 289 | } |
| 290 | |
| 291 | // Can be used to make some annotations stricter for code we can fix |
| 292 | // (such as when we mark functions as deprecated). |
| 293 | if strict { |
| 294 | flags.CFlags = append(flags.CFlags, "-DANDROID_STRICT") |
| 295 | } |
| 296 | |
| 297 | return flags |
| 298 | } |
| 299 | |
| 300 | func ndkPathDeps(ctx ModuleContext) android.Paths { |
| 301 | if ctx.sdk() { |
| 302 | // The NDK sysroot timestamp file depends on all the NDK sysroot files |
| 303 | // (headers and libraries). |
| 304 | return android.Paths{getNdkSysrootTimestampFile(ctx)} |
| 305 | } |
| 306 | return nil |
| 307 | } |
| 308 | |
| 309 | func (compiler *baseCompiler) compile(ctx ModuleContext, flags Flags, deps PathDeps) android.Paths { |
| 310 | pathDeps := deps.GeneratedHeaders |
| 311 | pathDeps = append(pathDeps, ndkPathDeps(ctx)...) |
| 312 | // Compile files listed in c.Properties.Srcs into objects |
| 313 | objFiles := compiler.compileObjs(ctx, flags, "", |
| 314 | compiler.Properties.Srcs, compiler.Properties.Exclude_srcs, |
| 315 | deps.GeneratedSources, pathDeps) |
| 316 | |
| 317 | if ctx.Failed() { |
| 318 | return nil |
| 319 | } |
| 320 | |
| 321 | return objFiles |
| 322 | } |
| 323 | |
| 324 | // Compile a list of source files into objects a specified subdirectory |
| 325 | func (compiler *baseCompiler) compileObjs(ctx android.ModuleContext, flags Flags, |
| 326 | subdir string, srcFiles, excludes []string, extraSrcs, deps android.Paths) android.Paths { |
| 327 | |
| 328 | buildFlags := flagsToBuilderFlags(flags) |
| 329 | |
| 330 | inputFiles := ctx.ExpandSources(srcFiles, excludes) |
| 331 | inputFiles = append(inputFiles, extraSrcs...) |
| 332 | srcPaths, gendeps := genSources(ctx, inputFiles, buildFlags) |
| 333 | |
| 334 | deps = append(deps, gendeps...) |
| 335 | deps = append(deps, flags.CFlagsDeps...) |
| 336 | |
| 337 | return TransformSourceToObj(ctx, subdir, srcPaths, buildFlags, deps) |
| 338 | } |