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 | |
Colin Cross | 4b963f8 | 2016-09-29 14:06:02 -0700 | [diff] [blame] | 22 | "github.com/google/blueprint/proptools" |
| 23 | |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 24 | "android/soong/android" |
Colin Cross | b98c8b0 | 2016-07-29 13:44:28 -0700 | [diff] [blame] | 25 | "android/soong/cc/config" |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 26 | ) |
| 27 | |
| 28 | // This file contains the basic C/C++/assembly to .o compliation steps |
| 29 | |
| 30 | type 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 | |
Colin Cross | 948f0cb | 2016-10-17 14:24:56 -0700 | [diff] [blame] | 87 | // if set to false, use -std=c++* instead of -std=gnu++* |
| 88 | Gnu_extensions *bool |
| 89 | |
Dan Willemsen | e1240db | 2016-11-03 14:28:51 -0700 | [diff] [blame] | 90 | Aidl struct { |
| 91 | // list of directories that will be added to the aidl include paths. |
| 92 | Include_dirs []string |
| 93 | |
| 94 | // list of directories relative to the Blueprints file that will |
| 95 | // be added to the aidl include paths. |
| 96 | Local_include_dirs []string |
| 97 | } |
| 98 | |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 99 | Debug, Release struct { |
| 100 | // list of module-specific flags that will be used for C and C++ compiles in debug or |
| 101 | // release builds |
| 102 | Cflags []string `android:"arch_variant"` |
| 103 | } `android:"arch_variant"` |
| 104 | } |
| 105 | |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 106 | func NewBaseCompiler() *baseCompiler { |
| 107 | return &baseCompiler{} |
| 108 | } |
| 109 | |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 110 | type baseCompiler struct { |
| 111 | Properties BaseCompilerProperties |
Colin Cross | 0c461f1 | 2016-10-20 16:11:43 -0700 | [diff] [blame] | 112 | Proto ProtoProperties |
Colin Cross | 2f33635 | 2016-10-26 10:03:47 -0700 | [diff] [blame] | 113 | deps android.Paths |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | var _ compiler = (*baseCompiler)(nil) |
| 117 | |
| 118 | func (compiler *baseCompiler) appendCflags(flags []string) { |
| 119 | compiler.Properties.Cflags = append(compiler.Properties.Cflags, flags...) |
| 120 | } |
| 121 | |
| 122 | func (compiler *baseCompiler) appendAsflags(flags []string) { |
| 123 | compiler.Properties.Asflags = append(compiler.Properties.Asflags, flags...) |
| 124 | } |
| 125 | |
Colin Cross | 42742b8 | 2016-08-01 13:20:05 -0700 | [diff] [blame] | 126 | func (compiler *baseCompiler) compilerProps() []interface{} { |
Colin Cross | 0feb169 | 2016-11-03 14:38:52 -0700 | [diff] [blame] | 127 | return []interface{}{&compiler.Properties, &compiler.Proto} |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 128 | } |
| 129 | |
Colin Cross | 42742b8 | 2016-08-01 13:20:05 -0700 | [diff] [blame] | 130 | func (compiler *baseCompiler) compilerInit(ctx BaseModuleContext) {} |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 131 | |
Colin Cross | 42742b8 | 2016-08-01 13:20:05 -0700 | [diff] [blame] | 132 | func (compiler *baseCompiler) compilerDeps(ctx BaseModuleContext, deps Deps) Deps { |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 133 | deps.GeneratedSources = append(deps.GeneratedSources, compiler.Properties.Generated_sources...) |
| 134 | deps.GeneratedHeaders = append(deps.GeneratedHeaders, compiler.Properties.Generated_headers...) |
| 135 | |
Dan Willemsen | e1a3ce3 | 2016-11-02 20:44:08 -0700 | [diff] [blame] | 136 | if compiler.hasSrcExt(".proto") { |
Colin Cross | 0c461f1 | 2016-10-20 16:11:43 -0700 | [diff] [blame] | 137 | deps = protoDeps(ctx, deps, &compiler.Proto) |
| 138 | } |
| 139 | |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 140 | return deps |
| 141 | } |
| 142 | |
| 143 | // Create a Flags struct that collects the compile flags from global values, |
| 144 | // per-target values, module type values, and per-module Blueprints properties |
Colin Cross | 42742b8 | 2016-08-01 13:20:05 -0700 | [diff] [blame] | 145 | func (compiler *baseCompiler) compilerFlags(ctx ModuleContext, flags Flags) Flags { |
Colin Cross | b98c8b0 | 2016-07-29 13:44:28 -0700 | [diff] [blame] | 146 | tc := ctx.toolchain() |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 147 | |
| 148 | CheckBadCompilerFlags(ctx, "cflags", compiler.Properties.Cflags) |
| 149 | CheckBadCompilerFlags(ctx, "cppflags", compiler.Properties.Cppflags) |
| 150 | CheckBadCompilerFlags(ctx, "conlyflags", compiler.Properties.Conlyflags) |
| 151 | CheckBadCompilerFlags(ctx, "asflags", compiler.Properties.Asflags) |
| 152 | |
Colin Cross | 4b963f8 | 2016-09-29 14:06:02 -0700 | [diff] [blame] | 153 | esc := proptools.NinjaAndShellEscape |
| 154 | |
| 155 | flags.CFlags = append(flags.CFlags, esc(compiler.Properties.Cflags)...) |
| 156 | flags.CppFlags = append(flags.CppFlags, esc(compiler.Properties.Cppflags)...) |
| 157 | flags.ConlyFlags = append(flags.ConlyFlags, esc(compiler.Properties.Conlyflags)...) |
| 158 | flags.AsFlags = append(flags.AsFlags, esc(compiler.Properties.Asflags)...) |
Colin Cross | 91e9004 | 2016-12-02 17:13:24 -0800 | [diff] [blame] | 159 | flags.YasmFlags = append(flags.YasmFlags, esc(compiler.Properties.Asflags)...) |
Colin Cross | 4b963f8 | 2016-09-29 14:06:02 -0700 | [diff] [blame] | 160 | flags.YaccFlags = append(flags.YaccFlags, esc(compiler.Properties.Yaccflags)...) |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 161 | |
| 162 | // Include dir cflags |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 163 | localIncludeDirs := android.PathsForModuleSrc(ctx, compiler.Properties.Local_include_dirs) |
Dan Willemsen | 273af7f | 2016-11-03 15:53:42 -0700 | [diff] [blame] | 164 | if len(localIncludeDirs) > 0 { |
| 165 | flags.GlobalFlags = append(flags.GlobalFlags, includeDirsToFlags(localIncludeDirs)) |
| 166 | } |
| 167 | rootIncludeDirs := android.PathsForSource(ctx, compiler.Properties.Include_dirs) |
| 168 | if len(rootIncludeDirs) > 0 { |
| 169 | flags.GlobalFlags = append(flags.GlobalFlags, includeDirsToFlags(rootIncludeDirs)) |
| 170 | } |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 171 | |
| 172 | if !ctx.noDefaultCompilerFlags() { |
Dan Willemsen | d2ede87 | 2016-11-18 14:54:24 -0800 | [diff] [blame] | 173 | if !(ctx.sdk() || ctx.vndk()) || ctx.Host() { |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 174 | flags.GlobalFlags = append(flags.GlobalFlags, |
Colin Cross | b98c8b0 | 2016-07-29 13:44:28 -0700 | [diff] [blame] | 175 | "${config.CommonGlobalIncludes}", |
Colin Cross | 1cfd89a | 2016-09-15 09:30:46 -0700 | [diff] [blame] | 176 | "${config.CommonGlobalSystemIncludes}", |
Colin Cross | b98c8b0 | 2016-07-29 13:44:28 -0700 | [diff] [blame] | 177 | tc.IncludeFlags(), |
| 178 | "${config.CommonNativehelperInclude}") |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 179 | } |
| 180 | |
Colin Cross | 0c461f1 | 2016-10-20 16:11:43 -0700 | [diff] [blame] | 181 | flags.GlobalFlags = append(flags.GlobalFlags, "-I"+android.PathForModuleSrc(ctx).String()) |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 182 | } |
| 183 | |
Dan Willemsen | d2ede87 | 2016-11-18 14:54:24 -0800 | [diff] [blame] | 184 | if ctx.sdk() || ctx.vndk() { |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 185 | // The NDK headers are installed to a common sysroot. While a more |
| 186 | // typical Soong approach would be to only make the headers for the |
| 187 | // library you're using available, we're trying to emulate the NDK |
| 188 | // behavior here, and the NDK always has all the NDK headers available. |
| 189 | flags.GlobalFlags = append(flags.GlobalFlags, |
| 190 | "-isystem "+getCurrentIncludePath(ctx).String(), |
Colin Cross | b98c8b0 | 2016-07-29 13:44:28 -0700 | [diff] [blame] | 191 | "-isystem "+getCurrentIncludePath(ctx).Join(ctx, tc.ClangTriple()).String()) |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 192 | |
| 193 | // Traditionally this has come from android/api-level.h, but with the |
| 194 | // libc headers unified it must be set by the build system since we |
| 195 | // don't have per-API level copies of that header now. |
Dan Albert | ebedf67 | 2016-11-08 15:06:22 -0800 | [diff] [blame] | 196 | version := ctx.sdkVersion() |
| 197 | if version == "current" { |
| 198 | version = "__ANDROID_API_FUTURE__" |
| 199 | } |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 200 | flags.GlobalFlags = append(flags.GlobalFlags, |
Dan Albert | ebedf67 | 2016-11-08 15:06:22 -0800 | [diff] [blame] | 201 | "-D__ANDROID_API__="+version) |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 202 | |
| 203 | // Until the full NDK has been migrated to using ndk_headers, we still |
| 204 | // need to add the legacy sysroot includes to get the full set of |
| 205 | // headers. |
| 206 | legacyIncludes := fmt.Sprintf( |
| 207 | "prebuilts/ndk/current/platforms/android-%s/arch-%s/usr/include", |
| 208 | ctx.sdkVersion(), ctx.Arch().ArchType.String()) |
| 209 | flags.GlobalFlags = append(flags.GlobalFlags, "-isystem "+legacyIncludes) |
| 210 | } |
| 211 | |
| 212 | instructionSet := compiler.Properties.Instruction_set |
| 213 | if flags.RequiredInstructionSet != "" { |
| 214 | instructionSet = flags.RequiredInstructionSet |
| 215 | } |
Colin Cross | b98c8b0 | 2016-07-29 13:44:28 -0700 | [diff] [blame] | 216 | instructionSetFlags, err := tc.InstructionSetFlags(instructionSet) |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 217 | if flags.Clang { |
Colin Cross | b98c8b0 | 2016-07-29 13:44:28 -0700 | [diff] [blame] | 218 | instructionSetFlags, err = tc.ClangInstructionSetFlags(instructionSet) |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 219 | } |
| 220 | if err != nil { |
| 221 | ctx.ModuleErrorf("%s", err) |
| 222 | } |
| 223 | |
| 224 | CheckBadCompilerFlags(ctx, "release.cflags", compiler.Properties.Release.Cflags) |
| 225 | |
| 226 | // TODO: debug |
Colin Cross | 4b963f8 | 2016-09-29 14:06:02 -0700 | [diff] [blame] | 227 | flags.CFlags = append(flags.CFlags, esc(compiler.Properties.Release.Cflags)...) |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 228 | |
| 229 | if flags.Clang { |
| 230 | CheckBadCompilerFlags(ctx, "clang_cflags", compiler.Properties.Clang_cflags) |
| 231 | CheckBadCompilerFlags(ctx, "clang_asflags", compiler.Properties.Clang_asflags) |
| 232 | |
Colin Cross | b98c8b0 | 2016-07-29 13:44:28 -0700 | [diff] [blame] | 233 | flags.CFlags = config.ClangFilterUnknownCflags(flags.CFlags) |
Colin Cross | 4b963f8 | 2016-09-29 14:06:02 -0700 | [diff] [blame] | 234 | flags.CFlags = append(flags.CFlags, esc(compiler.Properties.Clang_cflags)...) |
| 235 | flags.AsFlags = append(flags.AsFlags, esc(compiler.Properties.Clang_asflags)...) |
Colin Cross | b98c8b0 | 2016-07-29 13:44:28 -0700 | [diff] [blame] | 236 | flags.CppFlags = config.ClangFilterUnknownCflags(flags.CppFlags) |
| 237 | flags.ConlyFlags = config.ClangFilterUnknownCflags(flags.ConlyFlags) |
| 238 | flags.LdFlags = config.ClangFilterUnknownCflags(flags.LdFlags) |
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 | target := "-target " + tc.ClangTriple() |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 241 | var gccPrefix string |
| 242 | if !ctx.Darwin() { |
Colin Cross | b98c8b0 | 2016-07-29 13:44:28 -0700 | [diff] [blame] | 243 | gccPrefix = "-B" + filepath.Join(tc.GccRoot(), tc.GccTriple(), "bin") |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 244 | } |
| 245 | |
| 246 | flags.CFlags = append(flags.CFlags, target, gccPrefix) |
| 247 | flags.AsFlags = append(flags.AsFlags, target, gccPrefix) |
| 248 | flags.LdFlags = append(flags.LdFlags, target, gccPrefix) |
| 249 | } |
| 250 | |
Colin Cross | b98c8b0 | 2016-07-29 13:44:28 -0700 | [diff] [blame] | 251 | hod := "Host" |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 252 | if ctx.Os().Class == android.Device { |
Colin Cross | b98c8b0 | 2016-07-29 13:44:28 -0700 | [diff] [blame] | 253 | hod = "Device" |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | if !ctx.noDefaultCompilerFlags() { |
| 257 | flags.GlobalFlags = append(flags.GlobalFlags, instructionSetFlags) |
Colin Cross | b668826 | 2016-11-22 12:32:47 -0800 | [diff] [blame] | 258 | flags.ConlyFlags = append([]string{"${config.CommonGlobalConlyflags}"}, flags.ConlyFlags...) |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 259 | |
| 260 | if flags.Clang { |
Colin Cross | b98c8b0 | 2016-07-29 13:44:28 -0700 | [diff] [blame] | 261 | flags.AsFlags = append(flags.AsFlags, tc.ClangAsflags()) |
Colin Cross | b668826 | 2016-11-22 12:32:47 -0800 | [diff] [blame] | 262 | flags.CppFlags = append([]string{"${config.CommonClangGlobalCppflags}"}, flags.CppFlags...) |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 263 | flags.GlobalFlags = append(flags.GlobalFlags, |
Colin Cross | b98c8b0 | 2016-07-29 13:44:28 -0700 | [diff] [blame] | 264 | tc.ClangCflags(), |
| 265 | "${config.CommonClangGlobalCflags}", |
| 266 | fmt.Sprintf("${config.%sClangGlobalCflags}", hod)) |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 267 | } else { |
Colin Cross | b668826 | 2016-11-22 12:32:47 -0800 | [diff] [blame] | 268 | flags.CppFlags = append([]string{"${config.CommonGlobalCppflags}"}, flags.CppFlags...) |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 269 | flags.GlobalFlags = append(flags.GlobalFlags, |
Colin Cross | b98c8b0 | 2016-07-29 13:44:28 -0700 | [diff] [blame] | 270 | tc.Cflags(), |
| 271 | "${config.CommonGlobalCflags}", |
| 272 | fmt.Sprintf("${config.%sGlobalCflags}", hod)) |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | if Bool(ctx.AConfig().ProductVariables.Brillo) { |
| 276 | flags.GlobalFlags = append(flags.GlobalFlags, "-D__BRILLO__") |
| 277 | } |
| 278 | |
| 279 | if ctx.Device() { |
| 280 | if Bool(compiler.Properties.Rtti) { |
| 281 | flags.CppFlags = append(flags.CppFlags, "-frtti") |
| 282 | } else { |
| 283 | flags.CppFlags = append(flags.CppFlags, "-fno-rtti") |
| 284 | } |
| 285 | } |
| 286 | |
| 287 | flags.AsFlags = append(flags.AsFlags, "-D__ASSEMBLY__") |
| 288 | |
| 289 | if flags.Clang { |
Colin Cross | b98c8b0 | 2016-07-29 13:44:28 -0700 | [diff] [blame] | 290 | flags.CppFlags = append(flags.CppFlags, tc.ClangCppflags()) |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 291 | } else { |
Colin Cross | b98c8b0 | 2016-07-29 13:44:28 -0700 | [diff] [blame] | 292 | flags.CppFlags = append(flags.CppFlags, tc.Cppflags()) |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 293 | } |
Colin Cross | 91e9004 | 2016-12-02 17:13:24 -0800 | [diff] [blame] | 294 | |
| 295 | flags.YasmFlags = append(flags.YasmFlags, tc.YasmFlags()) |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 296 | } |
| 297 | |
| 298 | if flags.Clang { |
Colin Cross | b98c8b0 | 2016-07-29 13:44:28 -0700 | [diff] [blame] | 299 | flags.GlobalFlags = append(flags.GlobalFlags, tc.ToolchainClangCflags()) |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 300 | } else { |
Colin Cross | b98c8b0 | 2016-07-29 13:44:28 -0700 | [diff] [blame] | 301 | flags.GlobalFlags = append(flags.GlobalFlags, tc.ToolchainCflags()) |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 302 | } |
| 303 | |
| 304 | if !ctx.sdk() { |
Colin Cross | 6f6a428 | 2016-10-17 14:19:06 -0700 | [diff] [blame] | 305 | cStd := config.CStdVersion |
| 306 | cppStd := config.CppStdVersion |
| 307 | |
| 308 | if !flags.Clang { |
| 309 | // GCC uses an invalid C++14 ABI (emits calls to |
| 310 | // __cxa_throw_bad_array_length, which is not a valid C++ RT ABI). |
| 311 | // http://b/25022512 |
| 312 | cppStd = config.GccCppStdVersion |
| 313 | } else if ctx.Host() && !flags.Clang { |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 314 | // The host GCC doesn't support C++14 (and is deprecated, so likely |
| 315 | // never will). Build these modules with C++11. |
Colin Cross | 6f6a428 | 2016-10-17 14:19:06 -0700 | [diff] [blame] | 316 | cppStd = config.GccCppStdVersion |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 317 | } |
Colin Cross | 6f6a428 | 2016-10-17 14:19:06 -0700 | [diff] [blame] | 318 | |
Colin Cross | 948f0cb | 2016-10-17 14:24:56 -0700 | [diff] [blame] | 319 | if compiler.Properties.Gnu_extensions != nil && *compiler.Properties.Gnu_extensions == false { |
| 320 | cStd = gnuToCReplacer.Replace(cStd) |
| 321 | cppStd = gnuToCReplacer.Replace(cppStd) |
| 322 | } |
| 323 | |
Colin Cross | 6f6a428 | 2016-10-17 14:19:06 -0700 | [diff] [blame] | 324 | flags.ConlyFlags = append([]string{"-std=" + cStd}, flags.ConlyFlags...) |
| 325 | flags.CppFlags = append([]string{"-std=" + cppStd}, flags.CppFlags...) |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 326 | } |
| 327 | |
| 328 | // We can enforce some rules more strictly in the code we own. strict |
| 329 | // indicates if this is code that we can be stricter with. If we have |
| 330 | // rules that we want to apply to *our* code (but maybe can't for |
| 331 | // vendor/device specific things), we could extend this to be a ternary |
| 332 | // value. |
| 333 | strict := true |
| 334 | if strings.HasPrefix(android.PathForModuleSrc(ctx).String(), "external/") { |
| 335 | strict = false |
| 336 | } |
| 337 | |
| 338 | // Can be used to make some annotations stricter for code we can fix |
| 339 | // (such as when we mark functions as deprecated). |
| 340 | if strict { |
| 341 | flags.CFlags = append(flags.CFlags, "-DANDROID_STRICT") |
| 342 | } |
| 343 | |
Dan Willemsen | e1a3ce3 | 2016-11-02 20:44:08 -0700 | [diff] [blame] | 344 | if compiler.hasSrcExt(".proto") { |
Colin Cross | 0c461f1 | 2016-10-20 16:11:43 -0700 | [diff] [blame] | 345 | flags = protoFlags(ctx, flags, &compiler.Proto) |
| 346 | } |
| 347 | |
Dan Willemsen | e1a3ce3 | 2016-11-02 20:44:08 -0700 | [diff] [blame] | 348 | if compiler.hasSrcExt(".y") || compiler.hasSrcExt(".yy") { |
| 349 | flags.GlobalFlags = append(flags.GlobalFlags, |
| 350 | "-I"+android.PathForModuleGen(ctx, "yacc", ctx.ModuleDir()).String()) |
| 351 | } |
| 352 | |
Dan Willemsen | e1240db | 2016-11-03 14:28:51 -0700 | [diff] [blame] | 353 | if compiler.hasSrcExt(".aidl") { |
| 354 | if len(compiler.Properties.Aidl.Local_include_dirs) > 0 { |
| 355 | localAidlIncludeDirs := android.PathsForModuleSrc(ctx, compiler.Properties.Aidl.Local_include_dirs) |
| 356 | flags.aidlFlags = append(flags.aidlFlags, includeDirsToFlags(localAidlIncludeDirs)) |
| 357 | } |
| 358 | if len(compiler.Properties.Aidl.Include_dirs) > 0 { |
| 359 | rootAidlIncludeDirs := android.PathsForSource(ctx, compiler.Properties.Aidl.Include_dirs) |
| 360 | flags.aidlFlags = append(flags.aidlFlags, includeDirsToFlags(rootAidlIncludeDirs)) |
| 361 | } |
| 362 | |
| 363 | flags.GlobalFlags = append(flags.GlobalFlags, |
| 364 | "-I"+android.PathForModuleGen(ctx, "aidl").String()) |
| 365 | } |
| 366 | |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 367 | return flags |
| 368 | } |
| 369 | |
Dan Willemsen | e1a3ce3 | 2016-11-02 20:44:08 -0700 | [diff] [blame] | 370 | func (compiler *baseCompiler) hasSrcExt(ext string) bool { |
Colin Cross | 0c461f1 | 2016-10-20 16:11:43 -0700 | [diff] [blame] | 371 | for _, src := range compiler.Properties.Srcs { |
Dan Willemsen | e1a3ce3 | 2016-11-02 20:44:08 -0700 | [diff] [blame] | 372 | if filepath.Ext(src) == ext { |
Colin Cross | 0c461f1 | 2016-10-20 16:11:43 -0700 | [diff] [blame] | 373 | return true |
| 374 | } |
| 375 | } |
| 376 | |
| 377 | return false |
| 378 | } |
| 379 | |
Colin Cross | 948f0cb | 2016-10-17 14:24:56 -0700 | [diff] [blame] | 380 | var gnuToCReplacer = strings.NewReplacer("gnu", "c") |
| 381 | |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 382 | func ndkPathDeps(ctx ModuleContext) android.Paths { |
Dan Willemsen | d2ede87 | 2016-11-18 14:54:24 -0800 | [diff] [blame] | 383 | if ctx.sdk() || ctx.vndk() { |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 384 | // The NDK sysroot timestamp file depends on all the NDK sysroot files |
| 385 | // (headers and libraries). |
| 386 | return android.Paths{getNdkSysrootTimestampFile(ctx)} |
| 387 | } |
| 388 | return nil |
| 389 | } |
| 390 | |
Dan Willemsen | 5cb580f | 2016-09-26 17:33:01 -0700 | [diff] [blame] | 391 | func (compiler *baseCompiler) compile(ctx ModuleContext, flags Flags, deps PathDeps) Objects { |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 392 | pathDeps := deps.GeneratedHeaders |
| 393 | pathDeps = append(pathDeps, ndkPathDeps(ctx)...) |
Colin Cross | 2f33635 | 2016-10-26 10:03:47 -0700 | [diff] [blame] | 394 | |
| 395 | srcs := ctx.ExpandSources(compiler.Properties.Srcs, compiler.Properties.Exclude_srcs) |
| 396 | srcs = append(srcs, deps.GeneratedSources...) |
| 397 | |
| 398 | buildFlags := flagsToBuilderFlags(flags) |
| 399 | |
| 400 | srcs, genDeps := genSources(ctx, srcs, buildFlags) |
| 401 | |
| 402 | pathDeps = append(pathDeps, genDeps...) |
| 403 | pathDeps = append(pathDeps, flags.CFlagsDeps...) |
| 404 | |
| 405 | compiler.deps = pathDeps |
| 406 | |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 407 | // Compile files listed in c.Properties.Srcs into objects |
Dan Willemsen | 5cb580f | 2016-09-26 17:33:01 -0700 | [diff] [blame] | 408 | objs := compileObjs(ctx, buildFlags, "", srcs, compiler.deps) |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 409 | |
| 410 | if ctx.Failed() { |
Dan Willemsen | 5cb580f | 2016-09-26 17:33:01 -0700 | [diff] [blame] | 411 | return Objects{} |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 412 | } |
| 413 | |
Dan Willemsen | 5cb580f | 2016-09-26 17:33:01 -0700 | [diff] [blame] | 414 | return objs |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 415 | } |
| 416 | |
| 417 | // Compile a list of source files into objects a specified subdirectory |
Colin Cross | 2f33635 | 2016-10-26 10:03:47 -0700 | [diff] [blame] | 418 | func compileObjs(ctx android.ModuleContext, flags builderFlags, |
Dan Willemsen | 5cb580f | 2016-09-26 17:33:01 -0700 | [diff] [blame] | 419 | subdir string, srcFiles, deps android.Paths) Objects { |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 420 | |
Colin Cross | 2f33635 | 2016-10-26 10:03:47 -0700 | [diff] [blame] | 421 | return TransformSourceToObj(ctx, subdir, srcFiles, flags, deps) |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 422 | } |