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