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