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