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