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