Colin Cross | 5049f02 | 2015-03-18 13:28:46 -0700 | [diff] [blame] | 1 | // Copyright 2015 Google Inc. All rights reserved. |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 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 | // This file contains the module types for compiling C/C++ for Android, and converts the properties |
| 18 | // into the flags and filenames necessary to pass to the compiler. The final creation of the rules |
| 19 | // is handled in builder.go |
| 20 | |
| 21 | import ( |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 22 | "fmt" |
| 23 | "path/filepath" |
| 24 | "strings" |
| 25 | |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 26 | "github.com/google/blueprint" |
| 27 | "github.com/google/blueprint/pathtools" |
| 28 | |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 29 | "android/soong/common" |
Colin Cross | 5049f02 | 2015-03-18 13:28:46 -0700 | [diff] [blame] | 30 | "android/soong/genrule" |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 31 | ) |
| 32 | |
| 33 | type Config interface { |
| 34 | SrcDir() string |
| 35 | PrebuiltOS() string |
| 36 | } |
| 37 | |
| 38 | var ( |
| 39 | HostPrebuiltTag = pctx.VariableConfigMethod("HostPrebuiltTag", Config.PrebuiltOS) |
| 40 | SrcDir = pctx.VariableConfigMethod("SrcDir", Config.SrcDir) |
| 41 | |
| 42 | LibcRoot = pctx.StaticVariable("LibcRoot", "${SrcDir}/bionic/libc") |
| 43 | LibmRoot = pctx.StaticVariable("LibmRoot", "${SrcDir}/bionic/libm") |
| 44 | ) |
| 45 | |
| 46 | // Flags used by lots of devices. Putting them in package static variables will save bytes in |
| 47 | // build.ninja so they aren't repeated for every file |
| 48 | var ( |
| 49 | commonGlobalCflags = []string{ |
| 50 | "-DANDROID", |
| 51 | "-fmessage-length=0", |
| 52 | "-W", |
| 53 | "-Wall", |
| 54 | "-Wno-unused", |
| 55 | "-Winit-self", |
| 56 | "-Wpointer-arith", |
| 57 | |
| 58 | // COMMON_RELEASE_CFLAGS |
| 59 | "-DNDEBUG", |
| 60 | "-UDEBUG", |
| 61 | } |
| 62 | |
| 63 | deviceGlobalCflags = []string{ |
| 64 | // TARGET_ERROR_FLAGS |
| 65 | "-Werror=return-type", |
| 66 | "-Werror=non-virtual-dtor", |
| 67 | "-Werror=address", |
| 68 | "-Werror=sequence-point", |
| 69 | } |
| 70 | |
| 71 | hostGlobalCflags = []string{} |
| 72 | |
| 73 | commonGlobalCppflags = []string{ |
| 74 | "-Wsign-promo", |
| 75 | "-std=gnu++11", |
| 76 | } |
| 77 | ) |
| 78 | |
| 79 | func init() { |
| 80 | pctx.StaticVariable("commonGlobalCflags", strings.Join(commonGlobalCflags, " ")) |
| 81 | pctx.StaticVariable("deviceGlobalCflags", strings.Join(deviceGlobalCflags, " ")) |
| 82 | pctx.StaticVariable("hostGlobalCflags", strings.Join(hostGlobalCflags, " ")) |
| 83 | |
| 84 | pctx.StaticVariable("commonGlobalCppflags", strings.Join(commonGlobalCppflags, " ")) |
| 85 | |
| 86 | pctx.StaticVariable("commonClangGlobalCflags", |
| 87 | strings.Join(clangFilterUnknownCflags(commonGlobalCflags), " ")) |
| 88 | pctx.StaticVariable("deviceClangGlobalCflags", |
| 89 | strings.Join(clangFilterUnknownCflags(deviceGlobalCflags), " ")) |
| 90 | pctx.StaticVariable("hostClangGlobalCflags", |
| 91 | strings.Join(clangFilterUnknownCflags(hostGlobalCflags), " ")) |
Tim Kilbourn | f294814 | 2015-03-11 12:03:03 -0700 | [diff] [blame] | 92 | pctx.StaticVariable("commonClangGlobalCppflags", |
| 93 | strings.Join(clangFilterUnknownCflags(commonGlobalCppflags), " ")) |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 94 | |
| 95 | // Everything in this list is a crime against abstraction and dependency tracking. |
| 96 | // Do not add anything to this list. |
| 97 | pctx.StaticVariable("commonGlobalIncludes", strings.Join([]string{ |
| 98 | "-isystem ${SrcDir}/system/core/include", |
| 99 | "-isystem ${SrcDir}/hardware/libhardware/include", |
| 100 | "-isystem ${SrcDir}/hardware/libhardware_legacy/include", |
| 101 | "-isystem ${SrcDir}/hardware/ril/include", |
| 102 | "-isystem ${SrcDir}/libnativehelper/include", |
| 103 | "-isystem ${SrcDir}/frameworks/native/include", |
| 104 | "-isystem ${SrcDir}/frameworks/native/opengl/include", |
| 105 | "-isystem ${SrcDir}/frameworks/av/include", |
| 106 | "-isystem ${SrcDir}/frameworks/base/include", |
| 107 | }, " ")) |
| 108 | |
| 109 | pctx.StaticVariable("clangPath", "${SrcDir}/prebuilts/clang/${HostPrebuiltTag}/host/3.6/bin/") |
| 110 | } |
| 111 | |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 112 | // ccProperties describes properties used to compile all C or C++ modules |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 113 | type ccProperties struct { |
| 114 | // srcs: list of source files used to compile the C/C++ module. May be .c, .cpp, or .S files. |
| 115 | Srcs []string `android:"arch_variant,arch_subtract"` |
| 116 | |
| 117 | // cflags: list of module-specific flags that will be used for C and C++ compiles. |
| 118 | Cflags []string `android:"arch_variant"` |
| 119 | |
| 120 | // cppflags: list of module-specific flags that will be used for C++ compiles |
| 121 | Cppflags []string `android:"arch_variant"` |
| 122 | |
| 123 | // conlyflags: list of module-specific flags that will be used for C compiles |
| 124 | Conlyflags []string `android:"arch_variant"` |
| 125 | |
| 126 | // asflags: list of module-specific flags that will be used for .S compiles |
| 127 | Asflags []string `android:"arch_variant"` |
| 128 | |
| 129 | // ldflags: list of module-specific flags that will be used for all link steps |
| 130 | Ldflags []string `android:"arch_variant"` |
| 131 | |
Tim Kilbourn | 1a9bf26 | 2015-03-18 12:28:32 -0700 | [diff] [blame] | 132 | // instruction_set: the instruction set architecture to use to compile the C/C++ |
| 133 | // module. |
| 134 | Instruction_set string `android:"arch_variant"` |
| 135 | |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 136 | // include_dirs: list of directories relative to the root of the source tree that will |
| 137 | // be added to the include path using -I. |
| 138 | // If possible, don't use this. If adding paths from the current directory use |
| 139 | // local_include_dirs, if adding paths from other modules use export_include_dirs in |
| 140 | // that module. |
| 141 | Include_dirs []string `android:"arch_variant"` |
| 142 | |
| 143 | // local_include_dirs: list of directories relative to the Blueprints file that will |
| 144 | // be added to the include path using -I |
| 145 | Local_include_dirs []string `android:"arch_variant"` |
| 146 | |
| 147 | // export_include_dirs: list of directories relative to the Blueprints file that will |
| 148 | // be added to the include path using -I for any module that links against this module |
| 149 | Export_include_dirs []string |
| 150 | |
| 151 | // clang_cflags: list of module-specific flags that will be used for C and C++ compiles when |
| 152 | // compiling with clang |
| 153 | Clang_cflags []string `android:"arch_variant"` |
| 154 | |
| 155 | // clang_asflags: list of module-specific flags that will be used for .S compiles when |
| 156 | // compiling with clang |
| 157 | Clang_asflags []string `android:"arch_variant"` |
| 158 | |
| 159 | // system_shared_libs: list of system libraries that will be dynamically linked to |
| 160 | // shared library and executable modules. If unset, generally defaults to libc |
| 161 | // and libm. Set to [] to prevent linking against libc and libm. |
| 162 | System_shared_libs []string |
| 163 | |
| 164 | // whole_static_libs: list of modules whose object files should be linked into this module |
| 165 | // in their entirety. For static library modules, all of the .o files from the intermediate |
| 166 | // directory of the dependency will be linked into this modules .a file. For a shared library, |
| 167 | // the dependency's .a file will be linked into this module using -Wl,--whole-archive. |
| 168 | Whole_static_libs []string `android:"arch_variant"` |
| 169 | |
| 170 | // static_libs: list of modules that should be statically linked into this module. |
| 171 | Static_libs []string `android:"arch_variant"` |
| 172 | |
| 173 | // shared_libs: list of modules that should be dynamically linked into this module. |
| 174 | Shared_libs []string `android:"arch_variant"` |
| 175 | |
| 176 | // allow_undefined_symbols: allow the module to contain undefined symbols. By default, |
| 177 | // modules cannot contain undefined symbols that are not satisified by their immediate |
| 178 | // dependencies. Set this flag to true to remove --no-undefined from the linker flags. |
| 179 | // This flag should only be necessary for compiling low-level libraries like libc. |
| 180 | Allow_undefined_symbols bool |
| 181 | |
| 182 | // nocrt: don't link in crt_begin and crt_end. This flag should only be necessary for |
| 183 | // compiling crt or libc. |
| 184 | Nocrt bool `android:"arch_variant"` |
| 185 | |
| 186 | // no_default_compiler_flags: don't insert default compiler flags into asflags, cflags, |
| 187 | // cppflags, conlyflags, ldflags, or include_dirs |
| 188 | No_default_compiler_flags bool |
| 189 | |
| 190 | // clang: compile module with clang instead of gcc |
| 191 | Clang bool `android:"arch_variant"` |
| 192 | |
| 193 | // rtti: pass -frtti instead of -fno-rtti |
| 194 | Rtti bool |
| 195 | |
| 196 | // host_ldlibs: -l arguments to pass to linker for host-provided shared libraries |
| 197 | Host_ldlibs []string `android:"arch_variant"` |
| 198 | |
| 199 | // stl: select the STL library to use. Possible values are "libc++", "libc++_static", |
| 200 | // "stlport", "stlport_static", "ndk", "libstdc++", or "none". Leave blank to select the |
| 201 | // default |
| 202 | Stl string |
| 203 | |
| 204 | // Set for combined shared/static libraries to prevent compiling object files a second time |
| 205 | SkipCompileObjs bool `blueprint:"mutated"` |
Colin Cross | af19a29 | 2015-03-18 12:07:10 -0700 | [diff] [blame] | 206 | |
| 207 | Debug struct { |
| 208 | Cflags []string `android:"arch_variant"` |
| 209 | } `android:"arch_variant"` |
| 210 | Release struct { |
| 211 | Cflags []string `android:"arch_variant"` |
| 212 | } `android:"arch_variant"` |
Colin Cross | efd8e48 | 2015-03-18 17:17:35 -0700 | [diff] [blame] | 213 | |
| 214 | // Minimum sdk version supported when compiling against the ndk |
| 215 | Sdk_version string |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | type unusedProperties struct { |
| 219 | Asan bool |
| 220 | Native_coverage bool |
| 221 | Strip string |
| 222 | Tags []string |
| 223 | Required []string |
| 224 | } |
| 225 | |
| 226 | // Building C/C++ code is handled by objects that satisfy this interface via composition |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 227 | type CCModuleType interface { |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 228 | common.AndroidModule |
| 229 | |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 230 | // Modify the ccFlags that are specific to this _type_ of module |
| 231 | ModuleTypeFlags(common.AndroidModuleContext, CCFlags) CCFlags |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 232 | |
| 233 | // Create a ccDeps struct that collects the module dependency info. Can also |
| 234 | // modify ccFlags in order to add dependency include directories, etc. |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 235 | collectDeps(common.AndroidModuleContext, CCFlags) (CCDeps, CCFlags) |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 236 | |
| 237 | // Compile objects into final module |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 238 | compileModule(common.AndroidModuleContext, CCFlags, CCDeps, []string) |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 239 | |
Dan Albert | c403f7c | 2015-03-18 14:01:18 -0700 | [diff] [blame] | 240 | // Install the built module. |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 241 | installModule(common.AndroidModuleContext, CCFlags) |
Dan Albert | c403f7c | 2015-03-18 14:01:18 -0700 | [diff] [blame] | 242 | |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 243 | // Return the output file (.o, .a or .so) for use by other modules |
| 244 | outputFile() string |
| 245 | } |
| 246 | |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 247 | type CCDeps struct { |
| 248 | StaticLibs, SharedLibs, LateStaticLibs, WholeStaticLibs, ObjFiles, IncludeDirs []string |
Colin Cross | c472d57 | 2015-03-17 15:06:21 -0700 | [diff] [blame] | 249 | |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 250 | CrtBegin, CrtEnd string |
Colin Cross | c472d57 | 2015-03-17 15:06:21 -0700 | [diff] [blame] | 251 | } |
| 252 | |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 253 | type CCFlags struct { |
| 254 | GlobalFlags []string |
| 255 | AsFlags []string |
| 256 | CFlags []string |
| 257 | ConlyFlags []string |
| 258 | CppFlags []string |
| 259 | LdFlags []string |
| 260 | LdLibs []string |
| 261 | IncludeDirs []string |
| 262 | Nocrt bool |
| 263 | Toolchain Toolchain |
| 264 | Clang bool |
Colin Cross | c472d57 | 2015-03-17 15:06:21 -0700 | [diff] [blame] | 265 | |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 266 | ExtraStaticLibs []string |
| 267 | ExtraSharedLibs []string |
Colin Cross | c472d57 | 2015-03-17 15:06:21 -0700 | [diff] [blame] | 268 | } |
| 269 | |
| 270 | // ccBase contains the properties and members used by all C/C++ module types, and implements |
| 271 | // the blueprint.Module interface. It expects to be embedded into an outer specialization struct, |
| 272 | // and uses a ccModuleType interface to that struct to create the build steps. |
| 273 | type ccBase struct { |
| 274 | common.AndroidModuleBase |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 275 | module CCModuleType |
Colin Cross | c472d57 | 2015-03-17 15:06:21 -0700 | [diff] [blame] | 276 | |
| 277 | properties ccProperties |
| 278 | unused unusedProperties |
| 279 | |
| 280 | installPath string |
| 281 | } |
| 282 | |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 283 | func newCCBase(base *ccBase, module CCModuleType, hod common.HostOrDeviceSupported, |
Colin Cross | c472d57 | 2015-03-17 15:06:21 -0700 | [diff] [blame] | 284 | multilib common.Multilib, props ...interface{}) (blueprint.Module, []interface{}) { |
| 285 | |
| 286 | base.module = module |
| 287 | |
| 288 | props = append(props, &base.properties, &base.unused) |
| 289 | |
Colin Cross | 5049f02 | 2015-03-18 13:28:46 -0700 | [diff] [blame] | 290 | return common.InitAndroidArchModule(module, hod, multilib, props...) |
Colin Cross | c472d57 | 2015-03-17 15:06:21 -0700 | [diff] [blame] | 291 | } |
| 292 | |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 293 | func (c *ccBase) GenerateAndroidBuildActions(ctx common.AndroidModuleContext) { |
| 294 | toolchain := c.findToolchain(ctx) |
| 295 | if ctx.Failed() { |
| 296 | return |
| 297 | } |
| 298 | |
| 299 | flags := c.flags(ctx, toolchain) |
| 300 | if ctx.Failed() { |
| 301 | return |
| 302 | } |
| 303 | |
| 304 | flags = c.addStlFlags(ctx, flags) |
| 305 | if ctx.Failed() { |
| 306 | return |
| 307 | } |
| 308 | |
| 309 | deps, flags := c.ccModuleType().collectDeps(ctx, flags) |
| 310 | if ctx.Failed() { |
| 311 | return |
| 312 | } |
| 313 | |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 314 | flags.IncludeDirs = append(flags.IncludeDirs, deps.IncludeDirs...) |
Colin Cross | ed9f868 | 2015-03-18 17:17:35 -0700 | [diff] [blame] | 315 | |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 316 | objFiles := c.compileObjs(ctx, flags, deps) |
| 317 | if ctx.Failed() { |
| 318 | return |
| 319 | } |
| 320 | |
Colin Cross | 5049f02 | 2015-03-18 13:28:46 -0700 | [diff] [blame] | 321 | generatedObjFiles := c.compileGeneratedObjs(ctx, flags, deps) |
| 322 | if ctx.Failed() { |
| 323 | return |
| 324 | } |
| 325 | |
| 326 | objFiles = append(objFiles, generatedObjFiles...) |
| 327 | |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 328 | c.ccModuleType().compileModule(ctx, flags, deps, objFiles) |
| 329 | if ctx.Failed() { |
| 330 | return |
| 331 | } |
Dan Albert | c403f7c | 2015-03-18 14:01:18 -0700 | [diff] [blame] | 332 | |
| 333 | c.ccModuleType().installModule(ctx, flags) |
| 334 | if ctx.Failed() { |
| 335 | return |
| 336 | } |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 337 | } |
| 338 | |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 339 | func (c *ccBase) ccModuleType() CCModuleType { |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 340 | return c.module |
| 341 | } |
| 342 | |
| 343 | var _ common.AndroidDynamicDepender = (*ccBase)(nil) |
| 344 | |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 345 | func (c *ccBase) findToolchain(ctx common.AndroidModuleContext) Toolchain { |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 346 | arch := ctx.Arch() |
| 347 | factory := toolchainFactories[arch.HostOrDevice][arch.ArchType] |
| 348 | if factory == nil { |
| 349 | panic(fmt.Sprintf("Toolchain not found for %s arch %q", |
| 350 | arch.HostOrDevice.String(), arch.String())) |
| 351 | } |
| 352 | return factory(arch.ArchVariant, arch.CpuVariant) |
| 353 | } |
| 354 | |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 355 | func (c *ccBase) ModuleTypeFlags(ctx common.AndroidModuleContext, flags CCFlags) CCFlags { |
| 356 | return flags |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 357 | } |
| 358 | |
| 359 | func (c *ccBase) AndroidDynamicDependencies(ctx common.AndroidDynamicDependerModuleContext) []string { |
| 360 | ctx.AddVariationDependencies([]blueprint.Variation{{"link", "static"}}, c.properties.Whole_static_libs...) |
| 361 | ctx.AddVariationDependencies([]blueprint.Variation{{"link", "static"}}, c.properties.Static_libs...) |
| 362 | ctx.AddVariationDependencies([]blueprint.Variation{{"link", "shared"}}, c.properties.Shared_libs...) |
| 363 | |
| 364 | return nil |
| 365 | } |
| 366 | |
| 367 | // Create a ccFlags struct that collects the compile flags from global values, |
| 368 | // per-target values, module type values, and per-module Blueprints properties |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 369 | func (c *ccBase) flags(ctx common.AndroidModuleContext, toolchain Toolchain) CCFlags { |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 370 | flags := CCFlags{ |
| 371 | CFlags: c.properties.Cflags, |
| 372 | CppFlags: c.properties.Cppflags, |
| 373 | ConlyFlags: c.properties.Conlyflags, |
| 374 | LdFlags: c.properties.Ldflags, |
| 375 | AsFlags: c.properties.Asflags, |
| 376 | Nocrt: c.properties.Nocrt, |
| 377 | Toolchain: toolchain, |
| 378 | Clang: c.properties.Clang, |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 379 | } |
Tim Kilbourn | 1a9bf26 | 2015-03-18 12:28:32 -0700 | [diff] [blame] | 380 | instructionSet := c.properties.Instruction_set |
| 381 | instructionSetFlags, err := toolchain.InstructionSetFlags(instructionSet) |
| 382 | if err != nil { |
| 383 | ctx.ModuleErrorf("%s", err) |
| 384 | } |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 385 | |
Colin Cross | af19a29 | 2015-03-18 12:07:10 -0700 | [diff] [blame] | 386 | // TODO: debug |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 387 | flags.CFlags = append(flags.CFlags, c.properties.Release.Cflags...) |
Colin Cross | af19a29 | 2015-03-18 12:07:10 -0700 | [diff] [blame] | 388 | |
Colin Cross | f6566ed | 2015-03-24 11:13:38 -0700 | [diff] [blame^] | 389 | if ctx.Host() { |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 390 | // TODO: allow per-module clang disable for host |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 391 | flags.Clang = true |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 392 | } |
| 393 | |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 394 | if flags.Clang { |
| 395 | flags.CFlags = clangFilterUnknownCflags(flags.CFlags) |
| 396 | flags.CFlags = append(flags.CFlags, c.properties.Clang_cflags...) |
| 397 | flags.AsFlags = append(flags.AsFlags, c.properties.Clang_asflags...) |
| 398 | flags.CppFlags = clangFilterUnknownCflags(flags.CppFlags) |
| 399 | flags.ConlyFlags = clangFilterUnknownCflags(flags.ConlyFlags) |
| 400 | flags.LdFlags = clangFilterUnknownCflags(flags.LdFlags) |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 401 | |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 402 | flags.CFlags = append(flags.CFlags, "${clangExtraCflags}") |
| 403 | flags.ConlyFlags = append(flags.ConlyFlags, "${clangExtraConlyflags}") |
Colin Cross | f6566ed | 2015-03-24 11:13:38 -0700 | [diff] [blame^] | 404 | if ctx.Device() { |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 405 | flags.CFlags = append(flags.CFlags, "${clangExtraTargetCflags}") |
Colin Cross | bdd7b1c | 2015-03-16 16:21:20 -0700 | [diff] [blame] | 406 | } |
| 407 | |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 408 | target := "-target " + toolchain.ClangTriple() |
| 409 | gccPrefix := "-B" + filepath.Join(toolchain.GccRoot(), toolchain.GccTriple(), "bin") |
| 410 | |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 411 | flags.CFlags = append(flags.CFlags, target, gccPrefix) |
| 412 | flags.AsFlags = append(flags.AsFlags, target, gccPrefix) |
| 413 | flags.LdFlags = append(flags.LdFlags, target, gccPrefix) |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 414 | |
Colin Cross | f6566ed | 2015-03-24 11:13:38 -0700 | [diff] [blame^] | 415 | if ctx.Host() { |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 416 | gccToolchain := "--gcc-toolchain=" + toolchain.GccRoot() |
| 417 | sysroot := "--sysroot=" + filepath.Join(toolchain.GccRoot(), "sysroot") |
| 418 | |
| 419 | // TODO: also need more -B, -L flags to make host builds hermetic |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 420 | flags.CFlags = append(flags.CFlags, gccToolchain, sysroot) |
| 421 | flags.AsFlags = append(flags.AsFlags, gccToolchain, sysroot) |
| 422 | flags.LdFlags = append(flags.LdFlags, gccToolchain, sysroot) |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 423 | } |
| 424 | } |
| 425 | |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 426 | flags.IncludeDirs = pathtools.PrefixPaths(c.properties.Include_dirs, ctx.Config().(Config).SrcDir()) |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 427 | localIncludeDirs := pathtools.PrefixPaths(c.properties.Local_include_dirs, common.ModuleSrcDir(ctx)) |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 428 | flags.IncludeDirs = append(flags.IncludeDirs, localIncludeDirs...) |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 429 | |
| 430 | if !c.properties.No_default_compiler_flags { |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 431 | flags.IncludeDirs = append(flags.IncludeDirs, []string{ |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 432 | common.ModuleSrcDir(ctx), |
| 433 | common.ModuleOutDir(ctx), |
| 434 | common.ModuleGenDir(ctx), |
| 435 | }...) |
| 436 | |
Colin Cross | efd8e48 | 2015-03-18 17:17:35 -0700 | [diff] [blame] | 437 | if c.properties.Sdk_version == "" { |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 438 | flags.IncludeDirs = append(flags.IncludeDirs, "${SrcDir}/libnativehelper/include/nativehelper") |
Colin Cross | efd8e48 | 2015-03-18 17:17:35 -0700 | [diff] [blame] | 439 | } |
| 440 | |
Colin Cross | f6566ed | 2015-03-24 11:13:38 -0700 | [diff] [blame^] | 441 | if ctx.Device() && !c.properties.Allow_undefined_symbols { |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 442 | flags.LdFlags = append(flags.LdFlags, "-Wl,--no-undefined") |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 443 | } |
| 444 | |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 445 | if flags.Clang { |
| 446 | flags.CppFlags = append(flags.CppFlags, "${commonClangGlobalCppflags}") |
| 447 | flags.GlobalFlags = []string{ |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 448 | "${commonGlobalIncludes}", |
| 449 | toolchain.IncludeFlags(), |
Tim Kilbourn | 1a9bf26 | 2015-03-18 12:28:32 -0700 | [diff] [blame] | 450 | instructionSetFlags, |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 451 | toolchain.ClangCflags(), |
| 452 | "${commonClangGlobalCflags}", |
Colin Cross | f6566ed | 2015-03-24 11:13:38 -0700 | [diff] [blame^] | 453 | fmt.Sprintf("${%sClangGlobalCflags}", ctx.Arch().HostOrDevice), |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 454 | } |
| 455 | } else { |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 456 | flags.CppFlags = append(flags.CppFlags, "${commonGlobalCppflags}") |
| 457 | flags.GlobalFlags = []string{ |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 458 | "${commonGlobalIncludes}", |
| 459 | toolchain.IncludeFlags(), |
Tim Kilbourn | 1a9bf26 | 2015-03-18 12:28:32 -0700 | [diff] [blame] | 460 | instructionSetFlags, |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 461 | toolchain.Cflags(), |
| 462 | "${commonGlobalCflags}", |
Colin Cross | f6566ed | 2015-03-24 11:13:38 -0700 | [diff] [blame^] | 463 | fmt.Sprintf("${%sGlobalCflags}", ctx.Arch().HostOrDevice), |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 464 | } |
| 465 | } |
| 466 | |
Colin Cross | f6566ed | 2015-03-24 11:13:38 -0700 | [diff] [blame^] | 467 | if ctx.Host() { |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 468 | flags.LdFlags = append(flags.LdFlags, c.properties.Host_ldlibs...) |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 469 | } |
| 470 | |
Colin Cross | f6566ed | 2015-03-24 11:13:38 -0700 | [diff] [blame^] | 471 | if ctx.Device() { |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 472 | if c.properties.Rtti { |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 473 | flags.CppFlags = append(flags.CppFlags, "-frtti") |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 474 | } else { |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 475 | flags.CppFlags = append(flags.CppFlags, "-fno-rtti") |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 476 | } |
| 477 | } |
| 478 | |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 479 | flags.AsFlags = append(flags.AsFlags, "-D__ASSEMBLY__") |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 480 | |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 481 | if flags.Clang { |
| 482 | flags.CppFlags = append(flags.CppFlags, toolchain.ClangCppflags()) |
| 483 | flags.LdFlags = append(flags.LdFlags, toolchain.ClangLdflags()) |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 484 | } else { |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 485 | flags.CppFlags = append(flags.CppFlags, toolchain.Cppflags()) |
| 486 | flags.LdFlags = append(flags.LdFlags, toolchain.Ldflags()) |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 487 | } |
| 488 | } |
| 489 | |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 490 | flags = c.ccModuleType().ModuleTypeFlags(ctx, flags) |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 491 | |
| 492 | // Optimization to reduce size of build.ninja |
| 493 | // Replace the long list of flags for each file with a module-local variable |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 494 | ctx.Variable(pctx, "cflags", strings.Join(flags.CFlags, " ")) |
| 495 | ctx.Variable(pctx, "cppflags", strings.Join(flags.CppFlags, " ")) |
| 496 | ctx.Variable(pctx, "asflags", strings.Join(flags.AsFlags, " ")) |
| 497 | flags.CFlags = []string{"$cflags"} |
| 498 | flags.CppFlags = []string{"$cppflags"} |
| 499 | flags.AsFlags = []string{"$asflags"} |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 500 | |
| 501 | return flags |
| 502 | } |
| 503 | |
| 504 | // Modify ccFlags structs with STL library info |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 505 | func (c *ccBase) addStlFlags(ctx common.AndroidModuleContext, flags CCFlags) CCFlags { |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 506 | if !c.properties.No_default_compiler_flags { |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 507 | stl := "libc++" // TODO: mingw needs libstdc++ |
| 508 | if c.properties.Stl != "" { |
| 509 | stl = c.properties.Stl |
| 510 | } |
| 511 | |
| 512 | stlStatic := false |
| 513 | if strings.HasSuffix(stl, "_static") { |
| 514 | stlStatic = true |
| 515 | } |
| 516 | |
| 517 | switch stl { |
| 518 | case "libc++", "libc++_static": |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 519 | flags.CFlags = append(flags.CFlags, "-D_USING_LIBCXX") |
| 520 | flags.IncludeDirs = append(flags.IncludeDirs, "${SrcDir}/external/libcxx/include") |
Colin Cross | f6566ed | 2015-03-24 11:13:38 -0700 | [diff] [blame^] | 521 | if ctx.Host() { |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 522 | flags.CppFlags = append(flags.CppFlags, "-nostdinc++") |
| 523 | flags.LdFlags = append(flags.LdFlags, "-nodefaultlibs") |
| 524 | flags.LdLibs = append(flags.LdLibs, "-lc", "-lm", "-lpthread") |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 525 | } |
| 526 | if stlStatic { |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 527 | flags.ExtraStaticLibs = append(flags.ExtraStaticLibs, "libc++_static") |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 528 | } else { |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 529 | flags.ExtraSharedLibs = append(flags.ExtraSharedLibs, "libc++") |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 530 | } |
| 531 | case "stlport", "stlport_static": |
Colin Cross | f6566ed | 2015-03-24 11:13:38 -0700 | [diff] [blame^] | 532 | if ctx.Device() { |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 533 | flags.IncludeDirs = append(flags.IncludeDirs, |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 534 | "${SrcDir}/external/stlport/stlport", |
| 535 | "${SrcDir}/bionic/libstdc++/include", |
| 536 | "${SrcDir}/bionic") |
| 537 | if stlStatic { |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 538 | flags.ExtraStaticLibs = append(flags.ExtraStaticLibs, "libstdc++", "libstlport_static") |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 539 | } else { |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 540 | flags.ExtraSharedLibs = append(flags.ExtraSharedLibs, "libstdc++", "libstlport") |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 541 | } |
| 542 | } |
| 543 | case "ndk": |
| 544 | panic("TODO") |
| 545 | case "libstdc++": |
| 546 | // Using bionic's basic libstdc++. Not actually an STL. Only around until the |
| 547 | // tree is in good enough shape to not need it. |
| 548 | // Host builds will use GNU libstdc++. |
Colin Cross | f6566ed | 2015-03-24 11:13:38 -0700 | [diff] [blame^] | 549 | if ctx.Device() { |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 550 | flags.IncludeDirs = append(flags.IncludeDirs, "${SrcDir}/bionic/libstdc++/include") |
| 551 | flags.ExtraSharedLibs = append(flags.ExtraSharedLibs, "libstdc++") |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 552 | } |
| 553 | case "none": |
Colin Cross | f6566ed | 2015-03-24 11:13:38 -0700 | [diff] [blame^] | 554 | if ctx.Host() { |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 555 | flags.CppFlags = append(flags.CppFlags, "-nostdinc++") |
| 556 | flags.LdFlags = append(flags.LdFlags, "-nodefaultlibs") |
| 557 | flags.LdLibs = append(flags.LdLibs, "-lc", "-lm") |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 558 | } |
| 559 | default: |
| 560 | ctx.ModuleErrorf("stl: %q is not a supported STL", stl) |
| 561 | } |
| 562 | |
| 563 | } |
| 564 | return flags |
| 565 | } |
| 566 | |
| 567 | // Compile a list of source files into objects a specified subdirectory |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 568 | func (c *ccBase) customCompileObjs(ctx common.AndroidModuleContext, flags CCFlags, |
| 569 | deps CCDeps, subdir string, srcFiles []string) []string { |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 570 | |
| 571 | srcFiles = pathtools.PrefixPaths(srcFiles, common.ModuleSrcDir(ctx)) |
| 572 | srcFiles = common.ExpandGlobs(ctx, srcFiles) |
| 573 | |
| 574 | return TransformSourceToObj(ctx, subdir, srcFiles, ccFlagsToBuilderFlags(flags)) |
| 575 | } |
| 576 | |
| 577 | // Compile files listed in c.properties.Srcs into objects |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 578 | func (c *ccBase) compileObjs(ctx common.AndroidModuleContext, flags CCFlags, |
| 579 | deps CCDeps) []string { |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 580 | |
| 581 | if c.properties.SkipCompileObjs { |
| 582 | return nil |
| 583 | } |
| 584 | |
| 585 | return c.customCompileObjs(ctx, flags, deps, "", c.properties.Srcs) |
| 586 | } |
| 587 | |
Colin Cross | 5049f02 | 2015-03-18 13:28:46 -0700 | [diff] [blame] | 588 | // Compile generated source files from dependencies |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 589 | func (c *ccBase) compileGeneratedObjs(ctx common.AndroidModuleContext, flags CCFlags, |
| 590 | deps CCDeps) []string { |
Colin Cross | 5049f02 | 2015-03-18 13:28:46 -0700 | [diff] [blame] | 591 | var srcs []string |
| 592 | |
| 593 | if c.properties.SkipCompileObjs { |
| 594 | return nil |
| 595 | } |
| 596 | |
| 597 | ctx.VisitDirectDeps(func(module blueprint.Module) { |
| 598 | if gen, ok := module.(genrule.SourceFileGenerator); ok { |
| 599 | srcs = append(srcs, gen.GeneratedSourceFiles()...) |
| 600 | } |
| 601 | }) |
| 602 | |
| 603 | if len(srcs) == 0 { |
| 604 | return nil |
| 605 | } |
| 606 | |
| 607 | return TransformSourceToObj(ctx, "", srcs, ccFlagsToBuilderFlags(flags)) |
| 608 | } |
| 609 | |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 610 | func (c *ccBase) outputFile() string { |
| 611 | return "" |
| 612 | } |
| 613 | |
| 614 | func (c *ccBase) collectDepsFromList(ctx common.AndroidModuleContext, |
| 615 | names []string) (modules []common.AndroidModule, |
| 616 | outputFiles []string, exportedIncludeDirs []string) { |
| 617 | |
| 618 | for _, n := range names { |
| 619 | found := false |
| 620 | ctx.VisitDirectDeps(func(m blueprint.Module) { |
| 621 | otherName := ctx.OtherModuleName(m) |
| 622 | if otherName != n { |
| 623 | return |
| 624 | } |
| 625 | |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 626 | if a, ok := m.(CCModuleType); ok { |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 627 | if a.Disabled() { |
| 628 | // If a cc_library host+device module depends on a library that exists as both |
| 629 | // cc_library_shared and cc_library_host_shared, it will end up with two |
| 630 | // dependencies with the same name, one of which is marked disabled for each |
| 631 | // of host and device. Ignore the disabled one. |
| 632 | return |
| 633 | } |
| 634 | if a.HostOrDevice() != ctx.Arch().HostOrDevice { |
| 635 | ctx.ModuleErrorf("host/device mismatch between %q and %q", ctx.ModuleName(), |
| 636 | otherName) |
| 637 | return |
| 638 | } |
| 639 | |
| 640 | if outputFile := a.outputFile(); outputFile != "" { |
| 641 | if found { |
| 642 | ctx.ModuleErrorf("multiple modules satisified dependency on %q", otherName) |
| 643 | return |
| 644 | } |
| 645 | outputFiles = append(outputFiles, outputFile) |
| 646 | modules = append(modules, a) |
| 647 | if i, ok := a.(ccExportedIncludeDirsProducer); ok { |
| 648 | exportedIncludeDirs = append(exportedIncludeDirs, i.exportedIncludeDirs()...) |
| 649 | } |
| 650 | found = true |
| 651 | } else { |
| 652 | ctx.ModuleErrorf("module %q missing output file", otherName) |
| 653 | return |
| 654 | } |
| 655 | } else { |
| 656 | ctx.ModuleErrorf("module %q not an android module", otherName) |
| 657 | return |
| 658 | } |
| 659 | }) |
| 660 | if !found { |
| 661 | ctx.ModuleErrorf("unsatisified dependency on %q", n) |
| 662 | } |
| 663 | } |
| 664 | |
| 665 | return modules, outputFiles, exportedIncludeDirs |
| 666 | } |
| 667 | |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 668 | func (c *ccBase) collectDeps(ctx common.AndroidModuleContext, flags CCFlags) (CCDeps, CCFlags) { |
| 669 | var deps CCDeps |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 670 | var newIncludeDirs []string |
| 671 | |
| 672 | wholeStaticLibNames := c.properties.Whole_static_libs |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 673 | _, deps.WholeStaticLibs, newIncludeDirs = c.collectDepsFromList(ctx, wholeStaticLibNames) |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 674 | |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 675 | deps.IncludeDirs = append(deps.IncludeDirs, newIncludeDirs...) |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 676 | |
| 677 | staticLibNames := c.properties.Static_libs |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 678 | staticLibNames = append(staticLibNames, flags.ExtraStaticLibs...) |
| 679 | _, deps.StaticLibs, newIncludeDirs = c.collectDepsFromList(ctx, staticLibNames) |
| 680 | deps.IncludeDirs = append(deps.IncludeDirs, newIncludeDirs...) |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 681 | |
| 682 | return deps, flags |
| 683 | } |
| 684 | |
| 685 | // ccDynamic contains the properties and members used by shared libraries and dynamic executables |
| 686 | type ccDynamic struct { |
| 687 | ccBase |
| 688 | } |
| 689 | |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 690 | func newCCDynamic(dynamic *ccDynamic, module CCModuleType, hod common.HostOrDeviceSupported, |
Colin Cross | c472d57 | 2015-03-17 15:06:21 -0700 | [diff] [blame] | 691 | multilib common.Multilib, props ...interface{}) (blueprint.Module, []interface{}) { |
| 692 | |
| 693 | dynamic.properties.System_shared_libs = []string{defaultSystemSharedLibraries} |
| 694 | |
| 695 | return newCCBase(&dynamic.ccBase, module, hod, multilib, props...) |
| 696 | } |
| 697 | |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 698 | const defaultSystemSharedLibraries = "__default__" |
| 699 | |
Colin Cross | f6566ed | 2015-03-24 11:13:38 -0700 | [diff] [blame^] | 700 | func (c *ccDynamic) systemSharedLibs(ctx common.AndroidBaseContext) []string { |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 701 | if len(c.properties.System_shared_libs) == 1 && |
| 702 | c.properties.System_shared_libs[0] == defaultSystemSharedLibraries { |
| 703 | |
Colin Cross | f6566ed | 2015-03-24 11:13:38 -0700 | [diff] [blame^] | 704 | if ctx.Host() { |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 705 | return []string{} |
| 706 | } else { |
| 707 | return []string{"libc", "libm"} |
| 708 | } |
| 709 | } |
| 710 | return c.properties.System_shared_libs |
| 711 | } |
| 712 | |
| 713 | var ( |
| 714 | stlSharedLibs = []string{"libc++", "libstlport", "libstdc++"} |
| 715 | stlSharedHostLibs = []string{"libc++"} |
| 716 | stlStaticLibs = []string{"libc++_static", "libstlport_static", "libstdc++"} |
| 717 | stlStaticHostLibs = []string{"libc++_static"} |
| 718 | ) |
| 719 | |
| 720 | func (c *ccDynamic) AndroidDynamicDependencies(ctx common.AndroidDynamicDependerModuleContext) []string { |
| 721 | deps := c.ccBase.AndroidDynamicDependencies(ctx) |
| 722 | |
Colin Cross | f6566ed | 2015-03-24 11:13:38 -0700 | [diff] [blame^] | 723 | if ctx.Device() { |
| 724 | ctx.AddVariationDependencies([]blueprint.Variation{{"link", "shared"}}, c.systemSharedLibs(ctx)...) |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 725 | ctx.AddVariationDependencies([]blueprint.Variation{{"link", "static"}}, |
| 726 | "libcompiler_rt-extras", |
Colin Cross | 77b00fa | 2015-03-16 16:15:49 -0700 | [diff] [blame] | 727 | "libgcov", |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 728 | "libatomic", |
| 729 | "libgcc") |
| 730 | |
| 731 | if c.properties.Stl != "none" { |
| 732 | ctx.AddVariationDependencies([]blueprint.Variation{{"link", "shared"}}, stlSharedLibs...) |
| 733 | ctx.AddVariationDependencies([]blueprint.Variation{{"link", "static"}}, stlStaticLibs...) |
| 734 | } |
| 735 | } else { |
| 736 | if c.properties.Stl != "none" { |
| 737 | ctx.AddVariationDependencies([]blueprint.Variation{{"link", "shared"}}, stlSharedHostLibs...) |
| 738 | ctx.AddVariationDependencies([]blueprint.Variation{{"link", "static"}}, stlStaticHostLibs...) |
| 739 | } |
| 740 | } |
| 741 | |
| 742 | return deps |
| 743 | } |
| 744 | |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 745 | func (c *ccDynamic) collectDeps(ctx common.AndroidModuleContext, flags CCFlags) (CCDeps, CCFlags) { |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 746 | var newIncludeDirs []string |
| 747 | |
| 748 | deps, flags := c.ccBase.collectDeps(ctx, flags) |
| 749 | |
Colin Cross | f6566ed | 2015-03-24 11:13:38 -0700 | [diff] [blame^] | 750 | systemSharedLibs := c.systemSharedLibs(ctx) |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 751 | sharedLibNames := make([]string, 0, len(c.properties.Shared_libs)+len(systemSharedLibs)+ |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 752 | len(flags.ExtraSharedLibs)) |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 753 | sharedLibNames = append(sharedLibNames, c.properties.Shared_libs...) |
| 754 | sharedLibNames = append(sharedLibNames, systemSharedLibs...) |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 755 | sharedLibNames = append(sharedLibNames, flags.ExtraSharedLibs...) |
| 756 | _, deps.SharedLibs, newIncludeDirs = c.collectDepsFromList(ctx, sharedLibNames) |
| 757 | deps.IncludeDirs = append(deps.IncludeDirs, newIncludeDirs...) |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 758 | |
Colin Cross | f6566ed | 2015-03-24 11:13:38 -0700 | [diff] [blame^] | 759 | if ctx.Device() { |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 760 | var staticLibs []string |
Colin Cross | 77b00fa | 2015-03-16 16:15:49 -0700 | [diff] [blame] | 761 | staticLibNames := []string{"libcompiler_rt-extras"} |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 762 | _, staticLibs, newIncludeDirs = c.collectDepsFromList(ctx, staticLibNames) |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 763 | deps.StaticLibs = append(deps.StaticLibs, staticLibs...) |
| 764 | deps.IncludeDirs = append(deps.IncludeDirs, newIncludeDirs...) |
Colin Cross | 77b00fa | 2015-03-16 16:15:49 -0700 | [diff] [blame] | 765 | |
| 766 | // libgcc and libatomic have to be last on the command line |
| 767 | staticLibNames = []string{"libgcov", "libatomic", "libgcc"} |
| 768 | _, staticLibs, newIncludeDirs = c.collectDepsFromList(ctx, staticLibNames) |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 769 | deps.LateStaticLibs = append(deps.LateStaticLibs, staticLibs...) |
| 770 | deps.IncludeDirs = append(deps.IncludeDirs, newIncludeDirs...) |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 771 | } |
| 772 | |
| 773 | ctx.VisitDirectDeps(func(m blueprint.Module) { |
| 774 | if obj, ok := m.(*ccObject); ok { |
| 775 | otherName := ctx.OtherModuleName(m) |
| 776 | if strings.HasPrefix(otherName, "crtbegin") { |
| 777 | if !c.properties.Nocrt { |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 778 | deps.CrtBegin = obj.outputFile() |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 779 | } |
| 780 | } else if strings.HasPrefix(otherName, "crtend") { |
| 781 | if !c.properties.Nocrt { |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 782 | deps.CrtEnd = obj.outputFile() |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 783 | } |
| 784 | } else { |
| 785 | ctx.ModuleErrorf("object module type only support for crtbegin and crtend, found %q", |
| 786 | ctx.OtherModuleName(m)) |
| 787 | } |
| 788 | } |
| 789 | }) |
| 790 | |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 791 | return deps, flags |
| 792 | } |
| 793 | |
| 794 | type ccExportedIncludeDirsProducer interface { |
| 795 | exportedIncludeDirs() []string |
| 796 | } |
| 797 | |
| 798 | // |
| 799 | // Combined static+shared libraries |
| 800 | // |
| 801 | |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 802 | type CCLibrary struct { |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 803 | ccDynamic |
| 804 | |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 805 | primary *CCLibrary |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 806 | primaryObjFiles []string |
| 807 | objFiles []string |
| 808 | exportIncludeDirs []string |
| 809 | out string |
| 810 | |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 811 | LibraryProperties struct { |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 812 | BuildStatic bool `blueprint:"mutated"` |
| 813 | BuildShared bool `blueprint:"mutated"` |
| 814 | IsShared bool `blueprint:"mutated"` |
| 815 | IsStatic bool `blueprint:"mutated"` |
| 816 | |
| 817 | Static struct { |
| 818 | Srcs []string `android:"arch_variant"` |
| 819 | Cflags []string `android:"arch_variant"` |
| 820 | } `android:"arch_variant"` |
| 821 | Shared struct { |
| 822 | Srcs []string `android:"arch_variant"` |
| 823 | Cflags []string `android:"arch_variant"` |
| 824 | } `android:"arch_variant"` |
| 825 | } |
| 826 | } |
| 827 | |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 828 | type ccLibraryInterface interface { |
| 829 | ccLibrary() *CCLibrary |
| 830 | static() bool |
| 831 | shared() bool |
| 832 | allObjFiles() []string |
Colin Cross | c472d57 | 2015-03-17 15:06:21 -0700 | [diff] [blame] | 833 | } |
| 834 | |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 835 | func (c *CCLibrary) ccLibrary() *CCLibrary { |
| 836 | return c |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 837 | } |
| 838 | |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 839 | func (c *CCLibrary) static() bool { |
| 840 | return c.LibraryProperties.IsStatic |
| 841 | } |
| 842 | |
| 843 | func (c *CCLibrary) shared() bool { |
| 844 | return c.LibraryProperties.IsShared |
| 845 | } |
| 846 | |
| 847 | func NewCCLibrary(library *CCLibrary, module CCModuleType, |
| 848 | hod common.HostOrDeviceSupported) (blueprint.Module, []interface{}) { |
| 849 | |
| 850 | return newCCDynamic(&library.ccDynamic, module, hod, common.MultilibBoth, |
| 851 | &library.LibraryProperties) |
| 852 | } |
| 853 | |
| 854 | func CCLibraryFactory() (blueprint.Module, []interface{}) { |
| 855 | module := &CCLibrary{} |
| 856 | |
| 857 | module.LibraryProperties.BuildShared = true |
| 858 | module.LibraryProperties.BuildStatic = true |
| 859 | |
| 860 | return NewCCLibrary(module, module, common.HostAndDeviceSupported) |
| 861 | } |
| 862 | |
| 863 | func (c *CCLibrary) AndroidDynamicDependencies(ctx common.AndroidDynamicDependerModuleContext) []string { |
| 864 | if c.LibraryProperties.IsShared { |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 865 | deps := c.ccDynamic.AndroidDynamicDependencies(ctx) |
Colin Cross | f6566ed | 2015-03-24 11:13:38 -0700 | [diff] [blame^] | 866 | if ctx.Device() { |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 867 | deps = append(deps, "crtbegin_so", "crtend_so") |
| 868 | } |
| 869 | return deps |
| 870 | } else { |
| 871 | return c.ccBase.AndroidDynamicDependencies(ctx) |
| 872 | } |
| 873 | } |
| 874 | |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 875 | func (c *CCLibrary) collectDeps(ctx common.AndroidModuleContext, flags CCFlags) (CCDeps, CCFlags) { |
| 876 | if c.LibraryProperties.IsStatic { |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 877 | deps, flags := c.ccBase.collectDeps(ctx, flags) |
| 878 | wholeStaticLibNames := c.properties.Whole_static_libs |
| 879 | wholeStaticLibs, _, _ := c.collectDepsFromList(ctx, wholeStaticLibNames) |
| 880 | |
| 881 | for _, m := range wholeStaticLibs { |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 882 | if staticLib, ok := m.(ccLibraryInterface); ok && staticLib.static() { |
| 883 | deps.ObjFiles = append(deps.ObjFiles, staticLib.allObjFiles()...) |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 884 | } else { |
| 885 | ctx.ModuleErrorf("module %q not a static library", ctx.OtherModuleName(m)) |
| 886 | } |
| 887 | } |
| 888 | |
Colin Cross | ed9f868 | 2015-03-18 17:17:35 -0700 | [diff] [blame] | 889 | // Collect exported includes from shared lib dependencies |
| 890 | sharedLibNames := c.properties.Shared_libs |
| 891 | _, _, newIncludeDirs := c.collectDepsFromList(ctx, sharedLibNames) |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 892 | deps.IncludeDirs = append(deps.IncludeDirs, newIncludeDirs...) |
Colin Cross | ed9f868 | 2015-03-18 17:17:35 -0700 | [diff] [blame] | 893 | |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 894 | return deps, flags |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 895 | } else if c.LibraryProperties.IsShared { |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 896 | return c.ccDynamic.collectDeps(ctx, flags) |
| 897 | } else { |
| 898 | panic("Not shared or static") |
| 899 | } |
| 900 | } |
| 901 | |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 902 | func (c *CCLibrary) outputFile() string { |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 903 | return c.out |
| 904 | } |
| 905 | |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 906 | func (c *CCLibrary) allObjFiles() []string { |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 907 | return c.objFiles |
| 908 | } |
| 909 | |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 910 | func (c *CCLibrary) exportedIncludeDirs() []string { |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 911 | return c.exportIncludeDirs |
| 912 | } |
| 913 | |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 914 | func (c *CCLibrary) ModuleTypeFlags(ctx common.AndroidModuleContext, flags CCFlags) CCFlags { |
| 915 | flags.CFlags = append(flags.CFlags, "-fPIC") |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 916 | |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 917 | if c.LibraryProperties.IsShared { |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 918 | libName := ctx.ModuleName() |
| 919 | // GCC for Android assumes that -shared means -Bsymbolic, use -Wl,-shared instead |
| 920 | sharedFlag := "-Wl,-shared" |
Colin Cross | f6566ed | 2015-03-24 11:13:38 -0700 | [diff] [blame^] | 921 | if c.properties.Clang || ctx.Host() { |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 922 | sharedFlag = "-shared" |
| 923 | } |
Colin Cross | f6566ed | 2015-03-24 11:13:38 -0700 | [diff] [blame^] | 924 | if ctx.Device() { |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 925 | flags.LdFlags = append(flags.LdFlags, "-nostdlib") |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 926 | } |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 927 | |
| 928 | flags.LdFlags = append(flags.LdFlags, |
| 929 | "-Wl,--gc-sections", |
| 930 | sharedFlag, |
| 931 | "-Wl,-soname,"+libName+sharedLibraryExtension, |
| 932 | ) |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 933 | } |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 934 | |
| 935 | return flags |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 936 | } |
| 937 | |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 938 | func (c *CCLibrary) compileStaticLibrary(ctx common.AndroidModuleContext, |
| 939 | flags CCFlags, deps CCDeps, objFiles []string) { |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 940 | |
| 941 | staticFlags := flags |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 942 | staticFlags.CFlags = append(staticFlags.CFlags, c.LibraryProperties.Static.Cflags...) |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 943 | objFilesStatic := c.customCompileObjs(ctx, staticFlags, deps, common.DeviceStaticLibrary, |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 944 | c.LibraryProperties.Static.Srcs) |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 945 | |
| 946 | objFiles = append(objFiles, objFilesStatic...) |
| 947 | |
| 948 | var includeDirs []string |
| 949 | |
| 950 | wholeStaticLibNames := c.properties.Whole_static_libs |
| 951 | wholeStaticLibs, _, newIncludeDirs := c.collectDepsFromList(ctx, wholeStaticLibNames) |
| 952 | includeDirs = append(includeDirs, newIncludeDirs...) |
| 953 | |
| 954 | for _, m := range wholeStaticLibs { |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 955 | if staticLib, ok := m.(ccLibraryInterface); ok && staticLib.static() { |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 956 | objFiles = append(objFiles, staticLib.allObjFiles()...) |
| 957 | } else { |
| 958 | ctx.ModuleErrorf("module %q not a static library", ctx.OtherModuleName(m)) |
| 959 | } |
| 960 | } |
| 961 | |
| 962 | staticLibNames := c.properties.Static_libs |
| 963 | _, _, newIncludeDirs = c.collectDepsFromList(ctx, staticLibNames) |
| 964 | includeDirs = append(includeDirs, newIncludeDirs...) |
| 965 | |
| 966 | ctx.VisitDirectDeps(func(m blueprint.Module) { |
| 967 | if obj, ok := m.(*ccObject); ok { |
| 968 | otherName := ctx.OtherModuleName(m) |
| 969 | if !strings.HasPrefix(otherName, "crtbegin") && !strings.HasPrefix(otherName, "crtend") { |
| 970 | objFiles = append(objFiles, obj.outputFile()) |
| 971 | } |
| 972 | } |
| 973 | }) |
| 974 | |
| 975 | outputFile := filepath.Join(common.ModuleOutDir(ctx), ctx.ModuleName()+staticLibraryExtension) |
| 976 | |
| 977 | TransformObjToStaticLib(ctx, objFiles, ccFlagsToBuilderFlags(flags), outputFile) |
| 978 | |
| 979 | c.objFiles = objFiles |
| 980 | c.out = outputFile |
| 981 | c.exportIncludeDirs = pathtools.PrefixPaths(c.properties.Export_include_dirs, |
| 982 | common.ModuleSrcDir(ctx)) |
| 983 | |
| 984 | ctx.CheckbuildFile(outputFile) |
| 985 | } |
| 986 | |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 987 | func (c *CCLibrary) compileSharedLibrary(ctx common.AndroidModuleContext, |
| 988 | flags CCFlags, deps CCDeps, objFiles []string) { |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 989 | |
| 990 | sharedFlags := flags |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 991 | sharedFlags.CFlags = append(sharedFlags.CFlags, c.LibraryProperties.Shared.Cflags...) |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 992 | objFilesShared := c.customCompileObjs(ctx, sharedFlags, deps, common.DeviceSharedLibrary, |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 993 | c.LibraryProperties.Shared.Srcs) |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 994 | |
| 995 | objFiles = append(objFiles, objFilesShared...) |
| 996 | |
| 997 | outputFile := filepath.Join(common.ModuleOutDir(ctx), ctx.ModuleName()+sharedLibraryExtension) |
| 998 | |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 999 | TransformObjToDynamicBinary(ctx, objFiles, deps.SharedLibs, deps.StaticLibs, |
| 1000 | deps.LateStaticLibs, deps.WholeStaticLibs, deps.CrtBegin, deps.CrtEnd, |
Colin Cross | 77b00fa | 2015-03-16 16:15:49 -0700 | [diff] [blame] | 1001 | ccFlagsToBuilderFlags(flags), outputFile) |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 1002 | |
| 1003 | c.out = outputFile |
| 1004 | c.exportIncludeDirs = pathtools.PrefixPaths(c.properties.Export_include_dirs, |
| 1005 | common.ModuleSrcDir(ctx)) |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 1006 | } |
| 1007 | |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 1008 | func (c *CCLibrary) compileModule(ctx common.AndroidModuleContext, |
| 1009 | flags CCFlags, deps CCDeps, objFiles []string) { |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 1010 | |
| 1011 | // Reuse the object files from the matching static library if it exists |
| 1012 | if c.primary == c { |
| 1013 | c.primaryObjFiles = objFiles |
| 1014 | } else { |
| 1015 | objFiles = append([]string(nil), c.primary.primaryObjFiles...) |
| 1016 | } |
| 1017 | |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 1018 | if c.LibraryProperties.IsStatic { |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 1019 | c.compileStaticLibrary(ctx, flags, deps, objFiles) |
| 1020 | } else { |
| 1021 | c.compileSharedLibrary(ctx, flags, deps, objFiles) |
| 1022 | } |
| 1023 | } |
| 1024 | |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 1025 | func (c *CCLibrary) installStaticLibrary(ctx common.AndroidModuleContext, flags CCFlags) { |
Dan Albert | c403f7c | 2015-03-18 14:01:18 -0700 | [diff] [blame] | 1026 | // Static libraries do not get installed. |
| 1027 | } |
| 1028 | |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 1029 | func (c *CCLibrary) installSharedLibrary(ctx common.AndroidModuleContext, flags CCFlags) { |
Dan Albert | c403f7c | 2015-03-18 14:01:18 -0700 | [diff] [blame] | 1030 | installDir := "lib" |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 1031 | if flags.Toolchain.Is64Bit() { |
Dan Albert | c403f7c | 2015-03-18 14:01:18 -0700 | [diff] [blame] | 1032 | installDir = "lib64" |
| 1033 | } |
| 1034 | |
| 1035 | ctx.InstallFile(installDir, c.out) |
| 1036 | } |
| 1037 | |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 1038 | func (c *CCLibrary) installModule(ctx common.AndroidModuleContext, flags CCFlags) { |
| 1039 | if c.LibraryProperties.IsStatic { |
Dan Albert | c403f7c | 2015-03-18 14:01:18 -0700 | [diff] [blame] | 1040 | c.installStaticLibrary(ctx, flags) |
| 1041 | } else { |
| 1042 | c.installSharedLibrary(ctx, flags) |
| 1043 | } |
| 1044 | } |
| 1045 | |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 1046 | // |
| 1047 | // Objects (for crt*.o) |
| 1048 | // |
| 1049 | |
| 1050 | type ccObject struct { |
| 1051 | ccBase |
| 1052 | out string |
| 1053 | } |
| 1054 | |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 1055 | func CCObjectFactory() (blueprint.Module, []interface{}) { |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 1056 | module := &ccObject{} |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 1057 | |
Colin Cross | c472d57 | 2015-03-17 15:06:21 -0700 | [diff] [blame] | 1058 | return newCCBase(&module.ccBase, module, common.DeviceSupported, common.MultilibBoth) |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 1059 | } |
| 1060 | |
| 1061 | func (*ccObject) AndroidDynamicDependencies(ctx common.AndroidDynamicDependerModuleContext) []string { |
| 1062 | // object files can't have any dynamic dependencies |
| 1063 | return nil |
| 1064 | } |
| 1065 | |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 1066 | func (c *ccObject) collectDeps(ctx common.AndroidModuleContext, flags CCFlags) (CCDeps, CCFlags) { |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 1067 | deps, flags := c.ccBase.collectDeps(ctx, flags) |
| 1068 | ctx.VisitDirectDeps(func(m blueprint.Module) { |
| 1069 | if obj, ok := m.(*ccObject); ok { |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 1070 | deps.ObjFiles = append(deps.ObjFiles, obj.outputFile()) |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 1071 | } else { |
| 1072 | ctx.ModuleErrorf("Unknown module type for dependency %q", ctx.OtherModuleName(m)) |
| 1073 | } |
| 1074 | }) |
| 1075 | |
| 1076 | return deps, flags |
| 1077 | } |
| 1078 | |
| 1079 | func (c *ccObject) compileModule(ctx common.AndroidModuleContext, |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 1080 | flags CCFlags, deps CCDeps, objFiles []string) { |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 1081 | |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 1082 | objFiles = append(objFiles, deps.ObjFiles...) |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 1083 | |
| 1084 | var outputFile string |
| 1085 | if len(objFiles) == 1 { |
| 1086 | outputFile = objFiles[0] |
| 1087 | } else { |
| 1088 | outputFile = filepath.Join(common.ModuleOutDir(ctx), ctx.ModuleName()+".o") |
| 1089 | TransformObjsToObj(ctx, objFiles, ccFlagsToBuilderFlags(flags), outputFile) |
| 1090 | } |
| 1091 | |
| 1092 | c.out = outputFile |
| 1093 | |
| 1094 | ctx.CheckbuildFile(outputFile) |
| 1095 | } |
| 1096 | |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 1097 | func (c *ccObject) installModule(ctx common.AndroidModuleContext, flags CCFlags) { |
Dan Albert | c403f7c | 2015-03-18 14:01:18 -0700 | [diff] [blame] | 1098 | // Object files do not get installed. |
| 1099 | } |
| 1100 | |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 1101 | func (c *ccObject) outputFile() string { |
| 1102 | return c.out |
| 1103 | } |
| 1104 | |
| 1105 | // |
| 1106 | // Executables |
| 1107 | // |
| 1108 | |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 1109 | type CCBinary struct { |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 1110 | ccDynamic |
Dan Albert | c403f7c | 2015-03-18 14:01:18 -0700 | [diff] [blame] | 1111 | out string |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 1112 | BinaryProperties struct { |
| 1113 | // static_executable: compile executable with -static |
| 1114 | Static_executable bool |
| 1115 | |
| 1116 | // stem: set the name of the output |
| 1117 | Stem string `android:"arch_variant"` |
| 1118 | |
| 1119 | // prefix_symbols: if set, add an extra objcopy --prefix-symbols= step |
| 1120 | Prefix_symbols string |
| 1121 | } |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 1122 | } |
| 1123 | |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 1124 | func (c *CCBinary) getStem(ctx common.AndroidModuleContext) string { |
| 1125 | if c.BinaryProperties.Stem != "" { |
| 1126 | return c.BinaryProperties.Stem |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 1127 | } |
| 1128 | return ctx.ModuleName() |
| 1129 | } |
| 1130 | |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 1131 | func (c *CCBinary) AndroidDynamicDependencies(ctx common.AndroidDynamicDependerModuleContext) []string { |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 1132 | deps := c.ccDynamic.AndroidDynamicDependencies(ctx) |
Colin Cross | f6566ed | 2015-03-24 11:13:38 -0700 | [diff] [blame^] | 1133 | if ctx.Device() { |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 1134 | if c.BinaryProperties.Static_executable { |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 1135 | deps = append(deps, "crtbegin_static", "crtend_android") |
| 1136 | } else { |
| 1137 | deps = append(deps, "crtbegin_dynamic", "crtend_android") |
| 1138 | } |
| 1139 | } |
| 1140 | return deps |
| 1141 | } |
| 1142 | |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 1143 | func NewCCBinary(binary *CCBinary, module CCModuleType, |
| 1144 | hod common.HostOrDeviceSupported) (blueprint.Module, []interface{}) { |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 1145 | |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 1146 | return newCCDynamic(&binary.ccDynamic, module, hod, common.MultilibFirst, |
| 1147 | &binary.BinaryProperties) |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 1148 | } |
| 1149 | |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 1150 | func CCBinaryFactory() (blueprint.Module, []interface{}) { |
| 1151 | module := &CCBinary{} |
| 1152 | |
| 1153 | return NewCCBinary(module, module, common.HostAndDeviceSupported) |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 1154 | } |
| 1155 | |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 1156 | func (c *CCBinary) ModuleTypeFlags(ctx common.AndroidModuleContext, flags CCFlags) CCFlags { |
| 1157 | flags.CFlags = append(flags.CFlags, "-fpie") |
| 1158 | |
Colin Cross | f6566ed | 2015-03-24 11:13:38 -0700 | [diff] [blame^] | 1159 | if ctx.Device() { |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 1160 | linker := "/system/bin/linker" |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 1161 | if flags.Toolchain.Is64Bit() { |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 1162 | linker = "/system/bin/linker64" |
| 1163 | } |
| 1164 | |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 1165 | flags.LdFlags = append(flags.LdFlags, |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 1166 | "-nostdlib", |
| 1167 | "-Bdynamic", |
| 1168 | fmt.Sprintf("-Wl,-dynamic-linker,%s", linker), |
| 1169 | "-Wl,--gc-sections", |
| 1170 | "-Wl,-z,nocopyreloc", |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 1171 | ) |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 1172 | } |
| 1173 | |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 1174 | return flags |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 1175 | } |
| 1176 | |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 1177 | func (c *CCBinary) compileModule(ctx common.AndroidModuleContext, |
| 1178 | flags CCFlags, deps CCDeps, objFiles []string) { |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 1179 | |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 1180 | if !c.BinaryProperties.Static_executable && inList("libc", c.properties.Static_libs) { |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 1181 | ctx.ModuleErrorf("statically linking libc to dynamic executable, please remove libc\n" + |
| 1182 | "from static libs or set static_executable: true") |
| 1183 | } |
| 1184 | |
| 1185 | outputFile := filepath.Join(common.ModuleOutDir(ctx), c.getStem(ctx)) |
Dan Albert | c403f7c | 2015-03-18 14:01:18 -0700 | [diff] [blame] | 1186 | c.out = outputFile |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 1187 | |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 1188 | TransformObjToDynamicBinary(ctx, objFiles, deps.SharedLibs, deps.StaticLibs, |
| 1189 | deps.LateStaticLibs, deps.WholeStaticLibs, deps.CrtBegin, deps.CrtEnd, |
Colin Cross | 77b00fa | 2015-03-16 16:15:49 -0700 | [diff] [blame] | 1190 | ccFlagsToBuilderFlags(flags), outputFile) |
Dan Albert | c403f7c | 2015-03-18 14:01:18 -0700 | [diff] [blame] | 1191 | } |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 1192 | |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 1193 | func (c *CCBinary) installModule(ctx common.AndroidModuleContext, flags CCFlags) { |
Dan Albert | c403f7c | 2015-03-18 14:01:18 -0700 | [diff] [blame] | 1194 | ctx.InstallFile("bin", c.out) |
| 1195 | } |
| 1196 | |
| 1197 | type ccTest struct { |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 1198 | CCBinary |
Colin Cross | 6b29069 | 2015-03-19 14:05:33 -0700 | [diff] [blame] | 1199 | |
| 1200 | testProperties struct { |
| 1201 | // test_per_src: Create a separate test for each source file. Useful when there is |
| 1202 | // global state that can not be torn down and reset between each test suite. |
| 1203 | Test_per_src bool |
| 1204 | } |
Dan Albert | c403f7c | 2015-03-18 14:01:18 -0700 | [diff] [blame] | 1205 | } |
| 1206 | |
| 1207 | var ( |
| 1208 | gtestLibs = []string{"libgtest", "libgtest_main"} |
| 1209 | ) |
| 1210 | |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 1211 | func (c *ccTest) collectDeps(ctx common.AndroidModuleContext, flags CCFlags) (CCDeps, CCFlags) { |
| 1212 | deps, flags := c.CCBinary.collectDeps(ctx, flags) |
Dan Albert | c403f7c | 2015-03-18 14:01:18 -0700 | [diff] [blame] | 1213 | |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 1214 | flags.CFlags = append(flags.CFlags, "-DGTEST_HAS_STD_STRING") |
Colin Cross | f6566ed | 2015-03-24 11:13:38 -0700 | [diff] [blame^] | 1215 | if ctx.Host() { |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 1216 | flags.CFlags = append(flags.CFlags, "-O0", "-g") |
| 1217 | flags.LdLibs = append(flags.LdLibs, "-lpthread") |
Dan Albert | c403f7c | 2015-03-18 14:01:18 -0700 | [diff] [blame] | 1218 | } |
| 1219 | |
| 1220 | // TODO(danalbert): Make gtest export its dependencies. |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 1221 | flags.IncludeDirs = append(flags.IncludeDirs, |
Tim Kilbourn | 5ccc730 | 2015-03-19 10:02:21 -0700 | [diff] [blame] | 1222 | filepath.Join(ctx.Config().(Config).SrcDir(), "external/gtest/include")) |
Dan Albert | c403f7c | 2015-03-18 14:01:18 -0700 | [diff] [blame] | 1223 | |
| 1224 | _, staticLibs, _ := c.collectDepsFromList(ctx, gtestLibs) |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 1225 | deps.StaticLibs = append(deps.StaticLibs, staticLibs...) |
Dan Albert | c403f7c | 2015-03-18 14:01:18 -0700 | [diff] [blame] | 1226 | |
| 1227 | return deps, flags |
| 1228 | } |
| 1229 | |
| 1230 | func (c *ccTest) AndroidDynamicDependencies(ctx common.AndroidDynamicDependerModuleContext) []string { |
| 1231 | ctx.AddVariationDependencies([]blueprint.Variation{{"link", "static"}}, gtestLibs...) |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 1232 | deps := c.CCBinary.AndroidDynamicDependencies(ctx) |
Dan Albert | c403f7c | 2015-03-18 14:01:18 -0700 | [diff] [blame] | 1233 | return append(deps, gtestLibs...) |
| 1234 | } |
| 1235 | |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 1236 | func (c *ccTest) installModule(ctx common.AndroidModuleContext, flags CCFlags) { |
Colin Cross | f6566ed | 2015-03-24 11:13:38 -0700 | [diff] [blame^] | 1237 | if ctx.Device() { |
Tim Kilbourn | 5ccc730 | 2015-03-19 10:02:21 -0700 | [diff] [blame] | 1238 | ctx.InstallFile("../data/nativetest/"+ctx.ModuleName(), c.out) |
Dan Albert | c403f7c | 2015-03-18 14:01:18 -0700 | [diff] [blame] | 1239 | } else { |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 1240 | c.CCBinary.installModule(ctx, flags) |
Dan Albert | c403f7c | 2015-03-18 14:01:18 -0700 | [diff] [blame] | 1241 | } |
| 1242 | } |
| 1243 | |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 1244 | func CCTestFactory() (blueprint.Module, []interface{}) { |
Dan Albert | c403f7c | 2015-03-18 14:01:18 -0700 | [diff] [blame] | 1245 | module := &ccTest{} |
| 1246 | return newCCDynamic(&module.ccDynamic, module, common.HostAndDeviceSupported, |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 1247 | common.MultilibFirst, &module.BinaryProperties, &module.testProperties) |
Colin Cross | 6b29069 | 2015-03-19 14:05:33 -0700 | [diff] [blame] | 1248 | } |
| 1249 | |
| 1250 | func TestPerSrcMutator(mctx blueprint.EarlyMutatorContext) { |
| 1251 | if test, ok := mctx.Module().(*ccTest); ok { |
| 1252 | if test.testProperties.Test_per_src { |
| 1253 | testNames := make([]string, len(test.properties.Srcs)) |
| 1254 | for i, src := range test.properties.Srcs { |
| 1255 | testNames[i] = strings.TrimSuffix(src, filepath.Ext(src)) |
| 1256 | } |
| 1257 | tests := mctx.CreateLocalVariations(testNames...) |
| 1258 | for i, src := range test.properties.Srcs { |
| 1259 | tests[i].(*ccTest).properties.Srcs = []string{src} |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 1260 | tests[i].(*ccTest).BinaryProperties.Stem = testNames[i] |
Colin Cross | 6b29069 | 2015-03-19 14:05:33 -0700 | [diff] [blame] | 1261 | } |
| 1262 | } |
| 1263 | } |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 1264 | } |
| 1265 | |
| 1266 | // |
| 1267 | // Static library |
| 1268 | // |
| 1269 | |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 1270 | func CCLibraryStaticFactory() (blueprint.Module, []interface{}) { |
| 1271 | module := &CCLibrary{} |
| 1272 | module.LibraryProperties.BuildStatic = true |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 1273 | |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 1274 | return NewCCLibrary(module, module, common.HostAndDeviceSupported) |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 1275 | } |
| 1276 | |
| 1277 | // |
| 1278 | // Shared libraries |
| 1279 | // |
| 1280 | |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 1281 | func CCLibrarySharedFactory() (blueprint.Module, []interface{}) { |
| 1282 | module := &CCLibrary{} |
| 1283 | module.LibraryProperties.BuildShared = true |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 1284 | |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 1285 | return NewCCLibrary(module, module, common.HostAndDeviceSupported) |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 1286 | } |
| 1287 | |
| 1288 | // |
| 1289 | // Host static library |
| 1290 | // |
| 1291 | |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 1292 | func CCLibraryHostStaticFactory() (blueprint.Module, []interface{}) { |
| 1293 | module := &CCLibrary{} |
| 1294 | module.LibraryProperties.BuildStatic = true |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 1295 | |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 1296 | return NewCCLibrary(module, module, common.HostSupported) |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 1297 | } |
| 1298 | |
| 1299 | // |
| 1300 | // Host Shared libraries |
| 1301 | // |
| 1302 | |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 1303 | func CCLibraryHostSharedFactory() (blueprint.Module, []interface{}) { |
| 1304 | module := &CCLibrary{} |
| 1305 | module.LibraryProperties.BuildShared = true |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 1306 | |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 1307 | return NewCCLibrary(module, module, common.HostSupported) |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 1308 | } |
| 1309 | |
| 1310 | // |
| 1311 | // Host Binaries |
| 1312 | // |
| 1313 | |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 1314 | func CCBinaryHostFactory() (blueprint.Module, []interface{}) { |
| 1315 | module := &CCBinary{} |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 1316 | |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 1317 | return NewCCBinary(module, module, common.HostSupported) |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 1318 | } |
| 1319 | |
| 1320 | // |
| 1321 | // Device libraries shipped with gcc |
| 1322 | // |
| 1323 | |
| 1324 | type toolchainLibrary struct { |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 1325 | CCLibrary |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 1326 | } |
| 1327 | |
| 1328 | func (*toolchainLibrary) AndroidDynamicDependencies(ctx common.AndroidDynamicDependerModuleContext) []string { |
| 1329 | // toolchain libraries can't have any dependencies |
| 1330 | return nil |
| 1331 | } |
| 1332 | |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 1333 | func (*toolchainLibrary) collectDeps(ctx common.AndroidModuleContext, flags CCFlags) (CCDeps, CCFlags) { |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 1334 | // toolchain libraries can't have any dependencies |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 1335 | return CCDeps{}, flags |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 1336 | } |
| 1337 | |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 1338 | func ToolchainLibraryFactory() (blueprint.Module, []interface{}) { |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 1339 | module := &toolchainLibrary{} |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 1340 | |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 1341 | module.LibraryProperties.BuildStatic = true |
| 1342 | |
Colin Cross | c472d57 | 2015-03-17 15:06:21 -0700 | [diff] [blame] | 1343 | return newCCBase(&module.ccBase, module, common.DeviceSupported, common.MultilibBoth) |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 1344 | } |
| 1345 | |
| 1346 | func (c *toolchainLibrary) compileModule(ctx common.AndroidModuleContext, |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 1347 | flags CCFlags, deps CCDeps, objFiles []string) { |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 1348 | |
| 1349 | libName := ctx.ModuleName() + staticLibraryExtension |
| 1350 | outputFile := filepath.Join(common.ModuleOutDir(ctx), libName) |
| 1351 | |
| 1352 | CopyGccLib(ctx, libName, ccFlagsToBuilderFlags(flags), outputFile) |
| 1353 | |
| 1354 | c.out = outputFile |
| 1355 | |
| 1356 | ctx.CheckbuildFile(outputFile) |
| 1357 | } |
| 1358 | |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 1359 | func (c *toolchainLibrary) installModule(ctx common.AndroidModuleContext, flags CCFlags) { |
Dan Albert | c403f7c | 2015-03-18 14:01:18 -0700 | [diff] [blame] | 1360 | // Toolchain libraries do not get installed. |
| 1361 | } |
| 1362 | |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 1363 | func LinkageMutator(mctx blueprint.EarlyMutatorContext) { |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 1364 | if c, ok := mctx.Module().(ccLibraryInterface); ok { |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 1365 | var modules []blueprint.Module |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 1366 | if c.ccLibrary().LibraryProperties.BuildStatic && c.ccLibrary().LibraryProperties.BuildShared { |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 1367 | modules = mctx.CreateLocalVariations("static", "shared") |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 1368 | modules[0].(ccLibraryInterface).ccLibrary().LibraryProperties.IsStatic = true |
| 1369 | modules[1].(ccLibraryInterface).ccLibrary().LibraryProperties.IsShared = true |
| 1370 | } else if c.ccLibrary().LibraryProperties.BuildStatic { |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 1371 | modules = mctx.CreateLocalVariations("static") |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 1372 | modules[0].(ccLibraryInterface).ccLibrary().LibraryProperties.IsStatic = true |
| 1373 | } else if c.ccLibrary().LibraryProperties.BuildShared { |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 1374 | modules = mctx.CreateLocalVariations("shared") |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 1375 | modules[0].(ccLibraryInterface).ccLibrary().LibraryProperties.IsShared = true |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 1376 | } else { |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 1377 | panic(fmt.Errorf("ccLibrary %q not static or shared", mctx.ModuleName())) |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 1378 | } |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 1379 | primary := modules[0].(ccLibraryInterface).ccLibrary() |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 1380 | for _, m := range modules { |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 1381 | m.(ccLibraryInterface).ccLibrary().primary = primary |
| 1382 | if m.(ccLibraryInterface).ccLibrary() != primary { |
| 1383 | m.(ccLibraryInterface).ccLibrary().properties.SkipCompileObjs = true |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 1384 | } |
| 1385 | } |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 1386 | } |
| 1387 | } |