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 |
Dan Albert | be96168 | 2015-03-18 23:38:50 -0700 | [diff] [blame^] | 149 | Export_include_dirs []string `android:"arch_variant"` |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 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 | 21b9a24 | 2015-03-24 14:15:58 -0700 | [diff] [blame] | 230 | // Modify the ccFlags |
| 231 | Flags(common.AndroidModuleContext, CCFlags) CCFlags |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 232 | |
Colin Cross | 21b9a24 | 2015-03-24 14:15:58 -0700 | [diff] [blame] | 233 | // Return list of dependency names for use in AndroidDynamicDependencies and in depsToPaths |
| 234 | DepNames(common.AndroidBaseContext, CCDeps) CCDeps |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 235 | |
| 236 | // Compile objects into final module |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 237 | compileModule(common.AndroidModuleContext, CCFlags, CCDeps, []string) |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 238 | |
Dan Albert | c403f7c | 2015-03-18 14:01:18 -0700 | [diff] [blame] | 239 | // Install the built module. |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 240 | installModule(common.AndroidModuleContext, CCFlags) |
Dan Albert | c403f7c | 2015-03-18 14:01:18 -0700 | [diff] [blame] | 241 | |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 242 | // Return the output file (.o, .a or .so) for use by other modules |
| 243 | outputFile() string |
| 244 | } |
| 245 | |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 246 | type CCDeps struct { |
| 247 | StaticLibs, SharedLibs, LateStaticLibs, WholeStaticLibs, ObjFiles, IncludeDirs []string |
Colin Cross | c472d57 | 2015-03-17 15:06:21 -0700 | [diff] [blame] | 248 | |
Colin Cross | 21b9a24 | 2015-03-24 14:15:58 -0700 | [diff] [blame] | 249 | WholeStaticLibObjFiles []string |
| 250 | |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 251 | CrtBegin, CrtEnd string |
Colin Cross | c472d57 | 2015-03-17 15:06:21 -0700 | [diff] [blame] | 252 | } |
| 253 | |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 254 | type CCFlags struct { |
| 255 | GlobalFlags []string |
| 256 | AsFlags []string |
| 257 | CFlags []string |
| 258 | ConlyFlags []string |
| 259 | CppFlags []string |
| 260 | LdFlags []string |
| 261 | LdLibs []string |
| 262 | IncludeDirs []string |
| 263 | Nocrt bool |
| 264 | Toolchain Toolchain |
| 265 | Clang bool |
Colin Cross | c472d57 | 2015-03-17 15:06:21 -0700 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | // ccBase contains the properties and members used by all C/C++ module types, and implements |
| 269 | // the blueprint.Module interface. It expects to be embedded into an outer specialization struct, |
| 270 | // and uses a ccModuleType interface to that struct to create the build steps. |
| 271 | type ccBase struct { |
| 272 | common.AndroidModuleBase |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 273 | module CCModuleType |
Colin Cross | c472d57 | 2015-03-17 15:06:21 -0700 | [diff] [blame] | 274 | |
| 275 | properties ccProperties |
| 276 | unused unusedProperties |
| 277 | |
| 278 | installPath string |
| 279 | } |
| 280 | |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 281 | func newCCBase(base *ccBase, module CCModuleType, hod common.HostOrDeviceSupported, |
Colin Cross | c472d57 | 2015-03-17 15:06:21 -0700 | [diff] [blame] | 282 | multilib common.Multilib, props ...interface{}) (blueprint.Module, []interface{}) { |
| 283 | |
| 284 | base.module = module |
| 285 | |
| 286 | props = append(props, &base.properties, &base.unused) |
| 287 | |
Colin Cross | 5049f02 | 2015-03-18 13:28:46 -0700 | [diff] [blame] | 288 | return common.InitAndroidArchModule(module, hod, multilib, props...) |
Colin Cross | c472d57 | 2015-03-17 15:06:21 -0700 | [diff] [blame] | 289 | } |
| 290 | |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 291 | func (c *ccBase) GenerateAndroidBuildActions(ctx common.AndroidModuleContext) { |
| 292 | toolchain := c.findToolchain(ctx) |
| 293 | if ctx.Failed() { |
| 294 | return |
| 295 | } |
| 296 | |
Colin Cross | 21b9a24 | 2015-03-24 14:15:58 -0700 | [diff] [blame] | 297 | flags := c.collectFlags(ctx, toolchain) |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 298 | if ctx.Failed() { |
| 299 | return |
| 300 | } |
| 301 | |
Colin Cross | 21b9a24 | 2015-03-24 14:15:58 -0700 | [diff] [blame] | 302 | depNames := c.module.DepNames(ctx, CCDeps{}) |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 303 | if ctx.Failed() { |
| 304 | return |
| 305 | } |
| 306 | |
Colin Cross | 21b9a24 | 2015-03-24 14:15:58 -0700 | [diff] [blame] | 307 | deps := c.depsToPaths(ctx, depNames) |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 308 | if ctx.Failed() { |
| 309 | return |
| 310 | } |
| 311 | |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 312 | flags.IncludeDirs = append(flags.IncludeDirs, deps.IncludeDirs...) |
Colin Cross | ed9f868 | 2015-03-18 17:17:35 -0700 | [diff] [blame] | 313 | |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 314 | objFiles := c.compileObjs(ctx, flags, deps) |
| 315 | if ctx.Failed() { |
| 316 | return |
| 317 | } |
| 318 | |
Colin Cross | 5049f02 | 2015-03-18 13:28:46 -0700 | [diff] [blame] | 319 | generatedObjFiles := c.compileGeneratedObjs(ctx, flags, deps) |
| 320 | if ctx.Failed() { |
| 321 | return |
| 322 | } |
| 323 | |
| 324 | objFiles = append(objFiles, generatedObjFiles...) |
| 325 | |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 326 | c.ccModuleType().compileModule(ctx, flags, deps, objFiles) |
| 327 | if ctx.Failed() { |
| 328 | return |
| 329 | } |
Dan Albert | c403f7c | 2015-03-18 14:01:18 -0700 | [diff] [blame] | 330 | |
| 331 | c.ccModuleType().installModule(ctx, flags) |
| 332 | if ctx.Failed() { |
| 333 | return |
| 334 | } |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 335 | } |
| 336 | |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 337 | func (c *ccBase) ccModuleType() CCModuleType { |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 338 | return c.module |
| 339 | } |
| 340 | |
| 341 | var _ common.AndroidDynamicDepender = (*ccBase)(nil) |
| 342 | |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 343 | func (c *ccBase) findToolchain(ctx common.AndroidModuleContext) Toolchain { |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 344 | arch := ctx.Arch() |
| 345 | factory := toolchainFactories[arch.HostOrDevice][arch.ArchType] |
| 346 | if factory == nil { |
| 347 | panic(fmt.Sprintf("Toolchain not found for %s arch %q", |
| 348 | arch.HostOrDevice.String(), arch.String())) |
| 349 | } |
| 350 | return factory(arch.ArchVariant, arch.CpuVariant) |
| 351 | } |
| 352 | |
Dan Albert | be96168 | 2015-03-18 23:38:50 -0700 | [diff] [blame^] | 353 | func addNdkStlDepNames(ctx common.AndroidBaseContext, stl string, depNames CCDeps) CCDeps { |
| 354 | if stl == "ndk_system" { |
| 355 | // TODO: Make a system STL prebuilt for the NDK. |
| 356 | // The system STL doesn't have a prebuilt (it uses the system's libstdc++), but it does have |
| 357 | // its own includes. The includes are handled in ccBase.Flags(). |
| 358 | return depNames |
| 359 | } |
| 360 | |
| 361 | if strings.HasSuffix(stl, "_static") { |
| 362 | depNames.StaticLibs = append(depNames.StaticLibs, stl) |
| 363 | } else { |
| 364 | depNames.SharedLibs = append(depNames.SharedLibs, stl) |
| 365 | } |
| 366 | |
| 367 | return depNames |
| 368 | } |
| 369 | |
Colin Cross | 21b9a24 | 2015-03-24 14:15:58 -0700 | [diff] [blame] | 370 | func (c *ccBase) DepNames(ctx common.AndroidBaseContext, depNames CCDeps) CCDeps { |
| 371 | depNames.WholeStaticLibs = append(depNames.WholeStaticLibs, c.properties.Whole_static_libs...) |
| 372 | depNames.StaticLibs = append(depNames.StaticLibs, c.properties.Static_libs...) |
| 373 | depNames.SharedLibs = append(depNames.SharedLibs, c.properties.Shared_libs...) |
| 374 | |
| 375 | stl := c.stl(ctx) |
| 376 | if ctx.Failed() { |
| 377 | return depNames |
| 378 | } |
| 379 | |
| 380 | switch stl { |
| 381 | case "libc++", "libstdc++": |
| 382 | depNames.SharedLibs = append(depNames.SharedLibs, stl) |
| 383 | case "libc++_static": |
| 384 | depNames.StaticLibs = append(depNames.StaticLibs, stl) |
| 385 | case "stlport": |
| 386 | depNames.SharedLibs = append(depNames.SharedLibs, "libstdc++", "libstlport") |
| 387 | case "stlport_static": |
| 388 | depNames.StaticLibs = append(depNames.StaticLibs, "libstdc++", "libstlport_static") |
Dan Albert | be96168 | 2015-03-18 23:38:50 -0700 | [diff] [blame^] | 389 | case "": |
| 390 | // None or error. |
| 391 | default: |
| 392 | if !strings.HasPrefix(stl, "ndk_") { |
| 393 | panic("unexpected case") |
| 394 | } |
| 395 | |
| 396 | depNames = addNdkStlDepNames(ctx, stl, depNames) |
Colin Cross | 21b9a24 | 2015-03-24 14:15:58 -0700 | [diff] [blame] | 397 | } |
| 398 | |
| 399 | return depNames |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 400 | } |
| 401 | |
| 402 | func (c *ccBase) AndroidDynamicDependencies(ctx common.AndroidDynamicDependerModuleContext) []string { |
Colin Cross | 21b9a24 | 2015-03-24 14:15:58 -0700 | [diff] [blame] | 403 | depNames := CCDeps{} |
| 404 | depNames = c.module.DepNames(ctx, depNames) |
| 405 | staticLibs := depNames.WholeStaticLibs |
| 406 | staticLibs = append(staticLibs, depNames.StaticLibs...) |
| 407 | staticLibs = append(staticLibs, depNames.LateStaticLibs...) |
| 408 | ctx.AddVariationDependencies([]blueprint.Variation{{"link", "static"}}, staticLibs...) |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 409 | |
Colin Cross | 21b9a24 | 2015-03-24 14:15:58 -0700 | [diff] [blame] | 410 | ctx.AddVariationDependencies([]blueprint.Variation{{"link", "shared"}}, depNames.SharedLibs...) |
| 411 | |
| 412 | ret := append([]string(nil), depNames.ObjFiles...) |
| 413 | if depNames.CrtBegin != "" { |
| 414 | ret = append(ret, depNames.CrtBegin) |
| 415 | } |
| 416 | if depNames.CrtEnd != "" { |
| 417 | ret = append(ret, depNames.CrtEnd) |
| 418 | } |
| 419 | |
| 420 | return ret |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 421 | } |
| 422 | |
| 423 | // Create a ccFlags struct that collects the compile flags from global values, |
| 424 | // per-target values, module type values, and per-module Blueprints properties |
Colin Cross | 21b9a24 | 2015-03-24 14:15:58 -0700 | [diff] [blame] | 425 | func (c *ccBase) collectFlags(ctx common.AndroidModuleContext, toolchain Toolchain) CCFlags { |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 426 | flags := CCFlags{ |
| 427 | CFlags: c.properties.Cflags, |
| 428 | CppFlags: c.properties.Cppflags, |
| 429 | ConlyFlags: c.properties.Conlyflags, |
| 430 | LdFlags: c.properties.Ldflags, |
| 431 | AsFlags: c.properties.Asflags, |
| 432 | Nocrt: c.properties.Nocrt, |
| 433 | Toolchain: toolchain, |
| 434 | Clang: c.properties.Clang, |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 435 | } |
Tim Kilbourn | 1a9bf26 | 2015-03-18 12:28:32 -0700 | [diff] [blame] | 436 | instructionSet := c.properties.Instruction_set |
| 437 | instructionSetFlags, err := toolchain.InstructionSetFlags(instructionSet) |
| 438 | if err != nil { |
| 439 | ctx.ModuleErrorf("%s", err) |
| 440 | } |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 441 | |
Colin Cross | af19a29 | 2015-03-18 12:07:10 -0700 | [diff] [blame] | 442 | // TODO: debug |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 443 | flags.CFlags = append(flags.CFlags, c.properties.Release.Cflags...) |
Colin Cross | af19a29 | 2015-03-18 12:07:10 -0700 | [diff] [blame] | 444 | |
Colin Cross | f6566ed | 2015-03-24 11:13:38 -0700 | [diff] [blame] | 445 | if ctx.Host() { |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 446 | // TODO: allow per-module clang disable for host |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 447 | flags.Clang = true |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 448 | } |
| 449 | |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 450 | if flags.Clang { |
| 451 | flags.CFlags = clangFilterUnknownCflags(flags.CFlags) |
| 452 | flags.CFlags = append(flags.CFlags, c.properties.Clang_cflags...) |
| 453 | flags.AsFlags = append(flags.AsFlags, c.properties.Clang_asflags...) |
| 454 | flags.CppFlags = clangFilterUnknownCflags(flags.CppFlags) |
| 455 | flags.ConlyFlags = clangFilterUnknownCflags(flags.ConlyFlags) |
| 456 | flags.LdFlags = clangFilterUnknownCflags(flags.LdFlags) |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 457 | |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 458 | flags.CFlags = append(flags.CFlags, "${clangExtraCflags}") |
| 459 | flags.ConlyFlags = append(flags.ConlyFlags, "${clangExtraConlyflags}") |
Colin Cross | f6566ed | 2015-03-24 11:13:38 -0700 | [diff] [blame] | 460 | if ctx.Device() { |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 461 | flags.CFlags = append(flags.CFlags, "${clangExtraTargetCflags}") |
Colin Cross | bdd7b1c | 2015-03-16 16:21:20 -0700 | [diff] [blame] | 462 | } |
| 463 | |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 464 | target := "-target " + toolchain.ClangTriple() |
| 465 | gccPrefix := "-B" + filepath.Join(toolchain.GccRoot(), toolchain.GccTriple(), "bin") |
| 466 | |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 467 | flags.CFlags = append(flags.CFlags, target, gccPrefix) |
| 468 | flags.AsFlags = append(flags.AsFlags, target, gccPrefix) |
| 469 | flags.LdFlags = append(flags.LdFlags, target, gccPrefix) |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 470 | |
Colin Cross | f6566ed | 2015-03-24 11:13:38 -0700 | [diff] [blame] | 471 | if ctx.Host() { |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 472 | gccToolchain := "--gcc-toolchain=" + toolchain.GccRoot() |
| 473 | sysroot := "--sysroot=" + filepath.Join(toolchain.GccRoot(), "sysroot") |
| 474 | |
| 475 | // TODO: also need more -B, -L flags to make host builds hermetic |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 476 | flags.CFlags = append(flags.CFlags, gccToolchain, sysroot) |
| 477 | flags.AsFlags = append(flags.AsFlags, gccToolchain, sysroot) |
| 478 | flags.LdFlags = append(flags.LdFlags, gccToolchain, sysroot) |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 479 | } |
| 480 | } |
| 481 | |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 482 | flags.IncludeDirs = pathtools.PrefixPaths(c.properties.Include_dirs, ctx.Config().(Config).SrcDir()) |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 483 | localIncludeDirs := pathtools.PrefixPaths(c.properties.Local_include_dirs, common.ModuleSrcDir(ctx)) |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 484 | flags.IncludeDirs = append(flags.IncludeDirs, localIncludeDirs...) |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 485 | |
| 486 | if !c.properties.No_default_compiler_flags { |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 487 | flags.IncludeDirs = append(flags.IncludeDirs, []string{ |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 488 | common.ModuleSrcDir(ctx), |
| 489 | common.ModuleOutDir(ctx), |
| 490 | common.ModuleGenDir(ctx), |
| 491 | }...) |
| 492 | |
Colin Cross | efd8e48 | 2015-03-18 17:17:35 -0700 | [diff] [blame] | 493 | if c.properties.Sdk_version == "" { |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 494 | flags.IncludeDirs = append(flags.IncludeDirs, "${SrcDir}/libnativehelper/include/nativehelper") |
Colin Cross | efd8e48 | 2015-03-18 17:17:35 -0700 | [diff] [blame] | 495 | } |
| 496 | |
Colin Cross | f6566ed | 2015-03-24 11:13:38 -0700 | [diff] [blame] | 497 | if ctx.Device() && !c.properties.Allow_undefined_symbols { |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 498 | flags.LdFlags = append(flags.LdFlags, "-Wl,--no-undefined") |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 499 | } |
| 500 | |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 501 | if flags.Clang { |
| 502 | flags.CppFlags = append(flags.CppFlags, "${commonClangGlobalCppflags}") |
| 503 | flags.GlobalFlags = []string{ |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 504 | "${commonGlobalIncludes}", |
| 505 | toolchain.IncludeFlags(), |
Tim Kilbourn | 1a9bf26 | 2015-03-18 12:28:32 -0700 | [diff] [blame] | 506 | instructionSetFlags, |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 507 | toolchain.ClangCflags(), |
| 508 | "${commonClangGlobalCflags}", |
Colin Cross | f6566ed | 2015-03-24 11:13:38 -0700 | [diff] [blame] | 509 | fmt.Sprintf("${%sClangGlobalCflags}", ctx.Arch().HostOrDevice), |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 510 | } |
| 511 | } else { |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 512 | flags.CppFlags = append(flags.CppFlags, "${commonGlobalCppflags}") |
| 513 | flags.GlobalFlags = []string{ |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 514 | "${commonGlobalIncludes}", |
| 515 | toolchain.IncludeFlags(), |
Tim Kilbourn | 1a9bf26 | 2015-03-18 12:28:32 -0700 | [diff] [blame] | 516 | instructionSetFlags, |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 517 | toolchain.Cflags(), |
| 518 | "${commonGlobalCflags}", |
Colin Cross | f6566ed | 2015-03-24 11:13:38 -0700 | [diff] [blame] | 519 | fmt.Sprintf("${%sGlobalCflags}", ctx.Arch().HostOrDevice), |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 520 | } |
| 521 | } |
| 522 | |
Colin Cross | f6566ed | 2015-03-24 11:13:38 -0700 | [diff] [blame] | 523 | if ctx.Host() { |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 524 | flags.LdFlags = append(flags.LdFlags, c.properties.Host_ldlibs...) |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 525 | } |
| 526 | |
Colin Cross | f6566ed | 2015-03-24 11:13:38 -0700 | [diff] [blame] | 527 | if ctx.Device() { |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 528 | if c.properties.Rtti { |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 529 | flags.CppFlags = append(flags.CppFlags, "-frtti") |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 530 | } else { |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 531 | flags.CppFlags = append(flags.CppFlags, "-fno-rtti") |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 532 | } |
| 533 | } |
| 534 | |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 535 | flags.AsFlags = append(flags.AsFlags, "-D__ASSEMBLY__") |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 536 | |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 537 | if flags.Clang { |
| 538 | flags.CppFlags = append(flags.CppFlags, toolchain.ClangCppflags()) |
| 539 | flags.LdFlags = append(flags.LdFlags, toolchain.ClangLdflags()) |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 540 | } else { |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 541 | flags.CppFlags = append(flags.CppFlags, toolchain.Cppflags()) |
| 542 | flags.LdFlags = append(flags.LdFlags, toolchain.Ldflags()) |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 543 | } |
| 544 | } |
| 545 | |
Colin Cross | 21b9a24 | 2015-03-24 14:15:58 -0700 | [diff] [blame] | 546 | flags = c.ccModuleType().Flags(ctx, flags) |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 547 | |
| 548 | // Optimization to reduce size of build.ninja |
| 549 | // 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] | 550 | ctx.Variable(pctx, "cflags", strings.Join(flags.CFlags, " ")) |
| 551 | ctx.Variable(pctx, "cppflags", strings.Join(flags.CppFlags, " ")) |
| 552 | ctx.Variable(pctx, "asflags", strings.Join(flags.AsFlags, " ")) |
| 553 | flags.CFlags = []string{"$cflags"} |
| 554 | flags.CppFlags = []string{"$cppflags"} |
| 555 | flags.AsFlags = []string{"$asflags"} |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 556 | |
| 557 | return flags |
| 558 | } |
| 559 | |
Colin Cross | 21b9a24 | 2015-03-24 14:15:58 -0700 | [diff] [blame] | 560 | func (c *ccBase) stl(ctx common.AndroidBaseContext) string { |
Dan Albert | be96168 | 2015-03-18 23:38:50 -0700 | [diff] [blame^] | 561 | if c.properties.Sdk_version != "" { |
| 562 | stl := c.properties.Stl |
| 563 | if stl == "" { |
| 564 | return "ndk_system" |
| 565 | } |
| 566 | return "ndk_lib" + stl |
| 567 | } |
| 568 | |
Colin Cross | 21b9a24 | 2015-03-24 14:15:58 -0700 | [diff] [blame] | 569 | switch c.properties.Stl { |
| 570 | case "libc++", "libc++_static", |
| 571 | "stlport", "stlport_static", |
| 572 | "libstdc++": |
| 573 | return c.properties.Stl |
| 574 | case "none": |
| 575 | return "" |
| 576 | case "": |
| 577 | return "libc++" // TODO: mingw needs libstdc++ |
Colin Cross | 21b9a24 | 2015-03-24 14:15:58 -0700 | [diff] [blame] | 578 | default: |
| 579 | ctx.ModuleErrorf("stl: %q is not a supported STL", c.properties.Stl) |
| 580 | return "" |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 581 | } |
Colin Cross | 21b9a24 | 2015-03-24 14:15:58 -0700 | [diff] [blame] | 582 | } |
| 583 | |
| 584 | func (c *ccBase) Flags(ctx common.AndroidModuleContext, flags CCFlags) CCFlags { |
| 585 | stl := c.stl(ctx) |
| 586 | if ctx.Failed() { |
| 587 | return flags |
| 588 | } |
| 589 | |
| 590 | switch stl { |
| 591 | case "libc++", "libc++_static": |
| 592 | flags.CFlags = append(flags.CFlags, "-D_USING_LIBCXX") |
| 593 | flags.IncludeDirs = append(flags.IncludeDirs, "${SrcDir}/external/libcxx/include") |
| 594 | if ctx.Host() { |
| 595 | flags.CppFlags = append(flags.CppFlags, "-nostdinc++") |
| 596 | flags.LdFlags = append(flags.LdFlags, "-nodefaultlibs") |
| 597 | flags.LdLibs = append(flags.LdLibs, "-lc", "-lm", "-lpthread") |
| 598 | } |
| 599 | case "stlport", "stlport_static": |
| 600 | if ctx.Device() { |
| 601 | flags.IncludeDirs = append(flags.IncludeDirs, |
| 602 | "${SrcDir}/external/stlport/stlport", |
| 603 | "${SrcDir}/bionic/libstdc++/include", |
| 604 | "${SrcDir}/bionic") |
| 605 | } |
Colin Cross | 21b9a24 | 2015-03-24 14:15:58 -0700 | [diff] [blame] | 606 | case "libstdc++": |
| 607 | // Using bionic's basic libstdc++. Not actually an STL. Only around until the |
| 608 | // tree is in good enough shape to not need it. |
| 609 | // Host builds will use GNU libstdc++. |
| 610 | if ctx.Device() { |
| 611 | flags.IncludeDirs = append(flags.IncludeDirs, "${SrcDir}/bionic/libstdc++/include") |
| 612 | } |
Dan Albert | be96168 | 2015-03-18 23:38:50 -0700 | [diff] [blame^] | 613 | case "ndk_system": |
| 614 | ndkSrcRoot := ctx.Config().(Config).SrcDir() + "/prebuilts/ndk/current/sources/" |
| 615 | flags.IncludeDirs = append(flags.IncludeDirs, ndkSrcRoot + "cxx-stl/system/include") |
| 616 | case "ndk_c++_shared", "ndk_c++_static": |
| 617 | // TODO(danalbert): This really shouldn't be here... |
| 618 | flags.CppFlags = append(flags.CppFlags, "-std=c++11") |
Colin Cross | 21b9a24 | 2015-03-24 14:15:58 -0700 | [diff] [blame] | 619 | case "": |
Dan Albert | be96168 | 2015-03-18 23:38:50 -0700 | [diff] [blame^] | 620 | // None or error. |
Colin Cross | 21b9a24 | 2015-03-24 14:15:58 -0700 | [diff] [blame] | 621 | if ctx.Host() { |
| 622 | flags.CppFlags = append(flags.CppFlags, "-nostdinc++") |
| 623 | flags.LdFlags = append(flags.LdFlags, "-nodefaultlibs") |
| 624 | flags.LdLibs = append(flags.LdLibs, "-lc", "-lm") |
| 625 | } |
Colin Cross | 21b9a24 | 2015-03-24 14:15:58 -0700 | [diff] [blame] | 626 | } |
| 627 | |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 628 | return flags |
| 629 | } |
| 630 | |
| 631 | // Compile a list of source files into objects a specified subdirectory |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 632 | func (c *ccBase) customCompileObjs(ctx common.AndroidModuleContext, flags CCFlags, |
| 633 | deps CCDeps, subdir string, srcFiles []string) []string { |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 634 | |
| 635 | srcFiles = pathtools.PrefixPaths(srcFiles, common.ModuleSrcDir(ctx)) |
| 636 | srcFiles = common.ExpandGlobs(ctx, srcFiles) |
| 637 | |
| 638 | return TransformSourceToObj(ctx, subdir, srcFiles, ccFlagsToBuilderFlags(flags)) |
| 639 | } |
| 640 | |
| 641 | // Compile files listed in c.properties.Srcs into objects |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 642 | func (c *ccBase) compileObjs(ctx common.AndroidModuleContext, flags CCFlags, |
| 643 | deps CCDeps) []string { |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 644 | |
| 645 | if c.properties.SkipCompileObjs { |
| 646 | return nil |
| 647 | } |
| 648 | |
| 649 | return c.customCompileObjs(ctx, flags, deps, "", c.properties.Srcs) |
| 650 | } |
| 651 | |
Colin Cross | 5049f02 | 2015-03-18 13:28:46 -0700 | [diff] [blame] | 652 | // Compile generated source files from dependencies |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 653 | func (c *ccBase) compileGeneratedObjs(ctx common.AndroidModuleContext, flags CCFlags, |
| 654 | deps CCDeps) []string { |
Colin Cross | 5049f02 | 2015-03-18 13:28:46 -0700 | [diff] [blame] | 655 | var srcs []string |
| 656 | |
| 657 | if c.properties.SkipCompileObjs { |
| 658 | return nil |
| 659 | } |
| 660 | |
| 661 | ctx.VisitDirectDeps(func(module blueprint.Module) { |
| 662 | if gen, ok := module.(genrule.SourceFileGenerator); ok { |
| 663 | srcs = append(srcs, gen.GeneratedSourceFiles()...) |
| 664 | } |
| 665 | }) |
| 666 | |
| 667 | if len(srcs) == 0 { |
| 668 | return nil |
| 669 | } |
| 670 | |
| 671 | return TransformSourceToObj(ctx, "", srcs, ccFlagsToBuilderFlags(flags)) |
| 672 | } |
| 673 | |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 674 | func (c *ccBase) outputFile() string { |
| 675 | return "" |
| 676 | } |
| 677 | |
Colin Cross | 21b9a24 | 2015-03-24 14:15:58 -0700 | [diff] [blame] | 678 | func (c *ccBase) depsToPathsFromList(ctx common.AndroidModuleContext, |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 679 | names []string) (modules []common.AndroidModule, |
| 680 | outputFiles []string, exportedIncludeDirs []string) { |
| 681 | |
| 682 | for _, n := range names { |
| 683 | found := false |
| 684 | ctx.VisitDirectDeps(func(m blueprint.Module) { |
| 685 | otherName := ctx.OtherModuleName(m) |
| 686 | if otherName != n { |
| 687 | return |
| 688 | } |
| 689 | |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 690 | if a, ok := m.(CCModuleType); ok { |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 691 | if a.Disabled() { |
| 692 | // If a cc_library host+device module depends on a library that exists as both |
| 693 | // cc_library_shared and cc_library_host_shared, it will end up with two |
| 694 | // dependencies with the same name, one of which is marked disabled for each |
| 695 | // of host and device. Ignore the disabled one. |
| 696 | return |
| 697 | } |
| 698 | if a.HostOrDevice() != ctx.Arch().HostOrDevice { |
| 699 | ctx.ModuleErrorf("host/device mismatch between %q and %q", ctx.ModuleName(), |
| 700 | otherName) |
| 701 | return |
| 702 | } |
| 703 | |
| 704 | if outputFile := a.outputFile(); outputFile != "" { |
| 705 | if found { |
| 706 | ctx.ModuleErrorf("multiple modules satisified dependency on %q", otherName) |
| 707 | return |
| 708 | } |
| 709 | outputFiles = append(outputFiles, outputFile) |
| 710 | modules = append(modules, a) |
| 711 | if i, ok := a.(ccExportedIncludeDirsProducer); ok { |
| 712 | exportedIncludeDirs = append(exportedIncludeDirs, i.exportedIncludeDirs()...) |
| 713 | } |
| 714 | found = true |
| 715 | } else { |
| 716 | ctx.ModuleErrorf("module %q missing output file", otherName) |
| 717 | return |
| 718 | } |
| 719 | } else { |
| 720 | ctx.ModuleErrorf("module %q not an android module", otherName) |
| 721 | return |
| 722 | } |
| 723 | }) |
| 724 | if !found { |
| 725 | ctx.ModuleErrorf("unsatisified dependency on %q", n) |
| 726 | } |
| 727 | } |
| 728 | |
| 729 | return modules, outputFiles, exportedIncludeDirs |
| 730 | } |
| 731 | |
Colin Cross | 21b9a24 | 2015-03-24 14:15:58 -0700 | [diff] [blame] | 732 | // Convert depenedency names to paths. Takes a CCDeps containing names and returns a CCDeps |
| 733 | // containing paths |
| 734 | func (c *ccBase) depsToPaths(ctx common.AndroidModuleContext, depNames CCDeps) CCDeps { |
| 735 | var depPaths CCDeps |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 736 | var newIncludeDirs []string |
| 737 | |
Colin Cross | 21b9a24 | 2015-03-24 14:15:58 -0700 | [diff] [blame] | 738 | var wholeStaticLibModules []common.AndroidModule |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 739 | |
Colin Cross | 21b9a24 | 2015-03-24 14:15:58 -0700 | [diff] [blame] | 740 | wholeStaticLibModules, depPaths.WholeStaticLibs, newIncludeDirs = |
| 741 | c.depsToPathsFromList(ctx, depNames.WholeStaticLibs) |
| 742 | depPaths.IncludeDirs = append(depPaths.IncludeDirs, newIncludeDirs...) |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 743 | |
Colin Cross | 21b9a24 | 2015-03-24 14:15:58 -0700 | [diff] [blame] | 744 | for _, m := range wholeStaticLibModules { |
| 745 | if staticLib, ok := m.(ccLibraryInterface); ok && staticLib.static() { |
| 746 | depPaths.WholeStaticLibObjFiles = |
| 747 | append(depPaths.WholeStaticLibObjFiles, staticLib.allObjFiles()...) |
| 748 | } else { |
| 749 | ctx.ModuleErrorf("module %q not a static library", ctx.OtherModuleName(m)) |
| 750 | } |
| 751 | } |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 752 | |
Colin Cross | 21b9a24 | 2015-03-24 14:15:58 -0700 | [diff] [blame] | 753 | _, depPaths.StaticLibs, newIncludeDirs = c.depsToPathsFromList(ctx, depNames.StaticLibs) |
| 754 | depPaths.IncludeDirs = append(depPaths.IncludeDirs, newIncludeDirs...) |
| 755 | |
| 756 | _, depPaths.LateStaticLibs, newIncludeDirs = c.depsToPathsFromList(ctx, depNames.LateStaticLibs) |
| 757 | depPaths.IncludeDirs = append(depPaths.IncludeDirs, newIncludeDirs...) |
| 758 | |
| 759 | _, depPaths.SharedLibs, newIncludeDirs = c.depsToPathsFromList(ctx, depNames.SharedLibs) |
| 760 | depPaths.IncludeDirs = append(depPaths.IncludeDirs, newIncludeDirs...) |
| 761 | |
| 762 | ctx.VisitDirectDeps(func(m blueprint.Module) { |
| 763 | if obj, ok := m.(*ccObject); ok { |
| 764 | otherName := ctx.OtherModuleName(m) |
| 765 | if otherName == depNames.CrtBegin { |
| 766 | if !c.properties.Nocrt { |
| 767 | depPaths.CrtBegin = obj.outputFile() |
| 768 | } |
| 769 | } else if otherName == depNames.CrtEnd { |
| 770 | if !c.properties.Nocrt { |
| 771 | depPaths.CrtEnd = obj.outputFile() |
| 772 | } |
| 773 | } else { |
| 774 | depPaths.ObjFiles = append(depPaths.ObjFiles, obj.outputFile()) |
| 775 | } |
| 776 | } |
| 777 | }) |
| 778 | |
| 779 | return depPaths |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 780 | } |
| 781 | |
| 782 | // ccDynamic contains the properties and members used by shared libraries and dynamic executables |
| 783 | type ccDynamic struct { |
| 784 | ccBase |
| 785 | } |
| 786 | |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 787 | func newCCDynamic(dynamic *ccDynamic, module CCModuleType, hod common.HostOrDeviceSupported, |
Colin Cross | c472d57 | 2015-03-17 15:06:21 -0700 | [diff] [blame] | 788 | multilib common.Multilib, props ...interface{}) (blueprint.Module, []interface{}) { |
| 789 | |
| 790 | dynamic.properties.System_shared_libs = []string{defaultSystemSharedLibraries} |
| 791 | |
| 792 | return newCCBase(&dynamic.ccBase, module, hod, multilib, props...) |
| 793 | } |
| 794 | |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 795 | const defaultSystemSharedLibraries = "__default__" |
| 796 | |
Colin Cross | f6566ed | 2015-03-24 11:13:38 -0700 | [diff] [blame] | 797 | func (c *ccDynamic) systemSharedLibs(ctx common.AndroidBaseContext) []string { |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 798 | if len(c.properties.System_shared_libs) == 1 && |
| 799 | c.properties.System_shared_libs[0] == defaultSystemSharedLibraries { |
| 800 | |
Colin Cross | f6566ed | 2015-03-24 11:13:38 -0700 | [diff] [blame] | 801 | if ctx.Host() { |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 802 | return []string{} |
Dan Albert | be96168 | 2015-03-18 23:38:50 -0700 | [diff] [blame^] | 803 | } else if c.properties.Sdk_version != "" { |
| 804 | version := c.properties.Sdk_version |
| 805 | libs := []string{ |
| 806 | "ndk_libc." + version, |
| 807 | "ndk_libm." + version, |
| 808 | } |
| 809 | |
| 810 | if c.properties.Sdk_version != "" && c.stl(ctx) == "ndk_system" { |
| 811 | libs = append([]string{"libstdc++"}, libs...) |
| 812 | } |
| 813 | return libs |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 814 | } else { |
| 815 | return []string{"libc", "libm"} |
| 816 | } |
| 817 | } |
| 818 | return c.properties.System_shared_libs |
| 819 | } |
| 820 | |
Colin Cross | 21b9a24 | 2015-03-24 14:15:58 -0700 | [diff] [blame] | 821 | func (c *ccDynamic) DepNames(ctx common.AndroidBaseContext, depNames CCDeps) CCDeps { |
| 822 | depNames = c.ccBase.DepNames(ctx, depNames) |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 823 | |
Colin Cross | f6566ed | 2015-03-24 11:13:38 -0700 | [diff] [blame] | 824 | if ctx.Device() { |
Colin Cross | 21b9a24 | 2015-03-24 14:15:58 -0700 | [diff] [blame] | 825 | depNames.StaticLibs = append(depNames.StaticLibs, "libcompiler_rt-extras") |
Colin Cross | 77b00fa | 2015-03-16 16:15:49 -0700 | [diff] [blame] | 826 | // libgcc and libatomic have to be last on the command line |
Colin Cross | 21b9a24 | 2015-03-24 14:15:58 -0700 | [diff] [blame] | 827 | depNames.LateStaticLibs = append(depNames.LateStaticLibs, "libgcov", "libatomic", "libgcc") |
| 828 | depNames.SharedLibs = append(depNames.SharedLibs, c.systemSharedLibs(ctx)...) |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 829 | } |
| 830 | |
Colin Cross | 21b9a24 | 2015-03-24 14:15:58 -0700 | [diff] [blame] | 831 | return depNames |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 832 | } |
| 833 | |
| 834 | type ccExportedIncludeDirsProducer interface { |
| 835 | exportedIncludeDirs() []string |
| 836 | } |
| 837 | |
| 838 | // |
| 839 | // Combined static+shared libraries |
| 840 | // |
| 841 | |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 842 | type CCLibrary struct { |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 843 | ccDynamic |
| 844 | |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 845 | primary *CCLibrary |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 846 | primaryObjFiles []string |
| 847 | objFiles []string |
| 848 | exportIncludeDirs []string |
| 849 | out string |
| 850 | |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 851 | LibraryProperties struct { |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 852 | BuildStatic bool `blueprint:"mutated"` |
| 853 | BuildShared bool `blueprint:"mutated"` |
| 854 | IsShared bool `blueprint:"mutated"` |
| 855 | IsStatic bool `blueprint:"mutated"` |
| 856 | |
| 857 | Static struct { |
| 858 | Srcs []string `android:"arch_variant"` |
| 859 | Cflags []string `android:"arch_variant"` |
| 860 | } `android:"arch_variant"` |
| 861 | Shared struct { |
| 862 | Srcs []string `android:"arch_variant"` |
| 863 | Cflags []string `android:"arch_variant"` |
| 864 | } `android:"arch_variant"` |
| 865 | } |
| 866 | } |
| 867 | |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 868 | type ccLibraryInterface interface { |
| 869 | ccLibrary() *CCLibrary |
| 870 | static() bool |
| 871 | shared() bool |
| 872 | allObjFiles() []string |
Colin Cross | c472d57 | 2015-03-17 15:06:21 -0700 | [diff] [blame] | 873 | } |
| 874 | |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 875 | func (c *CCLibrary) ccLibrary() *CCLibrary { |
| 876 | return c |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 877 | } |
| 878 | |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 879 | func (c *CCLibrary) static() bool { |
| 880 | return c.LibraryProperties.IsStatic |
| 881 | } |
| 882 | |
| 883 | func (c *CCLibrary) shared() bool { |
| 884 | return c.LibraryProperties.IsShared |
| 885 | } |
| 886 | |
| 887 | func NewCCLibrary(library *CCLibrary, module CCModuleType, |
| 888 | hod common.HostOrDeviceSupported) (blueprint.Module, []interface{}) { |
| 889 | |
| 890 | return newCCDynamic(&library.ccDynamic, module, hod, common.MultilibBoth, |
| 891 | &library.LibraryProperties) |
| 892 | } |
| 893 | |
| 894 | func CCLibraryFactory() (blueprint.Module, []interface{}) { |
| 895 | module := &CCLibrary{} |
| 896 | |
| 897 | module.LibraryProperties.BuildShared = true |
| 898 | module.LibraryProperties.BuildStatic = true |
| 899 | |
| 900 | return NewCCLibrary(module, module, common.HostAndDeviceSupported) |
| 901 | } |
| 902 | |
Colin Cross | 21b9a24 | 2015-03-24 14:15:58 -0700 | [diff] [blame] | 903 | func (c *CCLibrary) DepNames(ctx common.AndroidBaseContext, depNames CCDeps) CCDeps { |
| 904 | if c.shared() { |
| 905 | depNames = c.ccDynamic.DepNames(ctx, depNames) |
Colin Cross | f6566ed | 2015-03-24 11:13:38 -0700 | [diff] [blame] | 906 | if ctx.Device() { |
Colin Cross | 21b9a24 | 2015-03-24 14:15:58 -0700 | [diff] [blame] | 907 | depNames.CrtBegin = "crtbegin_so" |
| 908 | depNames.CrtEnd = "crtend_so" |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 909 | } |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 910 | } else { |
Colin Cross | 21b9a24 | 2015-03-24 14:15:58 -0700 | [diff] [blame] | 911 | depNames = c.ccBase.DepNames(ctx, depNames) |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 912 | } |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 913 | |
Colin Cross | 21b9a24 | 2015-03-24 14:15:58 -0700 | [diff] [blame] | 914 | return depNames |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 915 | } |
| 916 | |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 917 | func (c *CCLibrary) outputFile() string { |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 918 | return c.out |
| 919 | } |
| 920 | |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 921 | func (c *CCLibrary) allObjFiles() []string { |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 922 | return c.objFiles |
| 923 | } |
| 924 | |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 925 | func (c *CCLibrary) exportedIncludeDirs() []string { |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 926 | return c.exportIncludeDirs |
| 927 | } |
| 928 | |
Colin Cross | 21b9a24 | 2015-03-24 14:15:58 -0700 | [diff] [blame] | 929 | func (c *CCLibrary) Flags(ctx common.AndroidModuleContext, flags CCFlags) CCFlags { |
| 930 | flags = c.ccDynamic.Flags(ctx, flags) |
| 931 | |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 932 | flags.CFlags = append(flags.CFlags, "-fPIC") |
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 | if c.LibraryProperties.IsShared { |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 935 | libName := ctx.ModuleName() |
| 936 | // GCC for Android assumes that -shared means -Bsymbolic, use -Wl,-shared instead |
| 937 | sharedFlag := "-Wl,-shared" |
Colin Cross | f6566ed | 2015-03-24 11:13:38 -0700 | [diff] [blame] | 938 | if c.properties.Clang || ctx.Host() { |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 939 | sharedFlag = "-shared" |
| 940 | } |
Colin Cross | f6566ed | 2015-03-24 11:13:38 -0700 | [diff] [blame] | 941 | if ctx.Device() { |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 942 | flags.LdFlags = append(flags.LdFlags, "-nostdlib") |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 943 | } |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 944 | |
| 945 | flags.LdFlags = append(flags.LdFlags, |
| 946 | "-Wl,--gc-sections", |
| 947 | sharedFlag, |
| 948 | "-Wl,-soname,"+libName+sharedLibraryExtension, |
| 949 | ) |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 950 | } |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 951 | |
| 952 | return flags |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 953 | } |
| 954 | |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 955 | func (c *CCLibrary) compileStaticLibrary(ctx common.AndroidModuleContext, |
| 956 | flags CCFlags, deps CCDeps, objFiles []string) { |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 957 | |
| 958 | staticFlags := flags |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 959 | staticFlags.CFlags = append(staticFlags.CFlags, c.LibraryProperties.Static.Cflags...) |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 960 | objFilesStatic := c.customCompileObjs(ctx, staticFlags, deps, common.DeviceStaticLibrary, |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 961 | c.LibraryProperties.Static.Srcs) |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 962 | |
| 963 | objFiles = append(objFiles, objFilesStatic...) |
Colin Cross | 21b9a24 | 2015-03-24 14:15:58 -0700 | [diff] [blame] | 964 | objFiles = append(objFiles, deps.WholeStaticLibObjFiles...) |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 965 | |
| 966 | outputFile := filepath.Join(common.ModuleOutDir(ctx), ctx.ModuleName()+staticLibraryExtension) |
| 967 | |
| 968 | TransformObjToStaticLib(ctx, objFiles, ccFlagsToBuilderFlags(flags), outputFile) |
| 969 | |
| 970 | c.objFiles = objFiles |
| 971 | c.out = outputFile |
| 972 | c.exportIncludeDirs = pathtools.PrefixPaths(c.properties.Export_include_dirs, |
| 973 | common.ModuleSrcDir(ctx)) |
| 974 | |
| 975 | ctx.CheckbuildFile(outputFile) |
| 976 | } |
| 977 | |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 978 | func (c *CCLibrary) compileSharedLibrary(ctx common.AndroidModuleContext, |
| 979 | flags CCFlags, deps CCDeps, objFiles []string) { |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 980 | |
| 981 | sharedFlags := flags |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 982 | sharedFlags.CFlags = append(sharedFlags.CFlags, c.LibraryProperties.Shared.Cflags...) |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 983 | objFilesShared := c.customCompileObjs(ctx, sharedFlags, deps, common.DeviceSharedLibrary, |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 984 | c.LibraryProperties.Shared.Srcs) |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 985 | |
| 986 | objFiles = append(objFiles, objFilesShared...) |
| 987 | |
| 988 | outputFile := filepath.Join(common.ModuleOutDir(ctx), ctx.ModuleName()+sharedLibraryExtension) |
| 989 | |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 990 | TransformObjToDynamicBinary(ctx, objFiles, deps.SharedLibs, deps.StaticLibs, |
| 991 | deps.LateStaticLibs, deps.WholeStaticLibs, deps.CrtBegin, deps.CrtEnd, |
Colin Cross | 77b00fa | 2015-03-16 16:15:49 -0700 | [diff] [blame] | 992 | ccFlagsToBuilderFlags(flags), outputFile) |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 993 | |
| 994 | c.out = outputFile |
| 995 | c.exportIncludeDirs = pathtools.PrefixPaths(c.properties.Export_include_dirs, |
| 996 | common.ModuleSrcDir(ctx)) |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 997 | } |
| 998 | |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 999 | func (c *CCLibrary) compileModule(ctx common.AndroidModuleContext, |
| 1000 | flags CCFlags, deps CCDeps, objFiles []string) { |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 1001 | |
| 1002 | // Reuse the object files from the matching static library if it exists |
| 1003 | if c.primary == c { |
| 1004 | c.primaryObjFiles = objFiles |
| 1005 | } else { |
| 1006 | objFiles = append([]string(nil), c.primary.primaryObjFiles...) |
| 1007 | } |
| 1008 | |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 1009 | if c.LibraryProperties.IsStatic { |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 1010 | c.compileStaticLibrary(ctx, flags, deps, objFiles) |
| 1011 | } else { |
| 1012 | c.compileSharedLibrary(ctx, flags, deps, objFiles) |
| 1013 | } |
| 1014 | } |
| 1015 | |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 1016 | func (c *CCLibrary) installStaticLibrary(ctx common.AndroidModuleContext, flags CCFlags) { |
Dan Albert | c403f7c | 2015-03-18 14:01:18 -0700 | [diff] [blame] | 1017 | // Static libraries do not get installed. |
| 1018 | } |
| 1019 | |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 1020 | func (c *CCLibrary) installSharedLibrary(ctx common.AndroidModuleContext, flags CCFlags) { |
Dan Albert | c403f7c | 2015-03-18 14:01:18 -0700 | [diff] [blame] | 1021 | installDir := "lib" |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 1022 | if flags.Toolchain.Is64Bit() { |
Dan Albert | c403f7c | 2015-03-18 14:01:18 -0700 | [diff] [blame] | 1023 | installDir = "lib64" |
| 1024 | } |
| 1025 | |
| 1026 | ctx.InstallFile(installDir, c.out) |
| 1027 | } |
| 1028 | |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 1029 | func (c *CCLibrary) installModule(ctx common.AndroidModuleContext, flags CCFlags) { |
| 1030 | if c.LibraryProperties.IsStatic { |
Dan Albert | c403f7c | 2015-03-18 14:01:18 -0700 | [diff] [blame] | 1031 | c.installStaticLibrary(ctx, flags) |
| 1032 | } else { |
| 1033 | c.installSharedLibrary(ctx, flags) |
| 1034 | } |
| 1035 | } |
| 1036 | |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 1037 | // |
| 1038 | // Objects (for crt*.o) |
| 1039 | // |
| 1040 | |
| 1041 | type ccObject struct { |
| 1042 | ccBase |
| 1043 | out string |
| 1044 | } |
| 1045 | |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 1046 | func CCObjectFactory() (blueprint.Module, []interface{}) { |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 1047 | module := &ccObject{} |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 1048 | |
Colin Cross | c472d57 | 2015-03-17 15:06:21 -0700 | [diff] [blame] | 1049 | return newCCBase(&module.ccBase, module, common.DeviceSupported, common.MultilibBoth) |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 1050 | } |
| 1051 | |
| 1052 | func (*ccObject) AndroidDynamicDependencies(ctx common.AndroidDynamicDependerModuleContext) []string { |
| 1053 | // object files can't have any dynamic dependencies |
| 1054 | return nil |
| 1055 | } |
| 1056 | |
Colin Cross | 21b9a24 | 2015-03-24 14:15:58 -0700 | [diff] [blame] | 1057 | func (*ccObject) DepNames(ctx common.AndroidBaseContext, depNames CCDeps) CCDeps { |
| 1058 | // object files can't have any dynamic dependencies |
| 1059 | return CCDeps{} |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 1060 | } |
| 1061 | |
| 1062 | func (c *ccObject) compileModule(ctx common.AndroidModuleContext, |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 1063 | flags CCFlags, deps CCDeps, objFiles []string) { |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 1064 | |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 1065 | objFiles = append(objFiles, deps.ObjFiles...) |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 1066 | |
| 1067 | var outputFile string |
| 1068 | if len(objFiles) == 1 { |
| 1069 | outputFile = objFiles[0] |
| 1070 | } else { |
| 1071 | outputFile = filepath.Join(common.ModuleOutDir(ctx), ctx.ModuleName()+".o") |
| 1072 | TransformObjsToObj(ctx, objFiles, ccFlagsToBuilderFlags(flags), outputFile) |
| 1073 | } |
| 1074 | |
| 1075 | c.out = outputFile |
| 1076 | |
| 1077 | ctx.CheckbuildFile(outputFile) |
| 1078 | } |
| 1079 | |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 1080 | func (c *ccObject) installModule(ctx common.AndroidModuleContext, flags CCFlags) { |
Dan Albert | c403f7c | 2015-03-18 14:01:18 -0700 | [diff] [blame] | 1081 | // Object files do not get installed. |
| 1082 | } |
| 1083 | |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 1084 | func (c *ccObject) outputFile() string { |
| 1085 | return c.out |
| 1086 | } |
| 1087 | |
| 1088 | // |
| 1089 | // Executables |
| 1090 | // |
| 1091 | |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 1092 | type CCBinary struct { |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 1093 | ccDynamic |
Dan Albert | c403f7c | 2015-03-18 14:01:18 -0700 | [diff] [blame] | 1094 | out string |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 1095 | BinaryProperties struct { |
| 1096 | // static_executable: compile executable with -static |
| 1097 | Static_executable bool |
| 1098 | |
| 1099 | // stem: set the name of the output |
| 1100 | Stem string `android:"arch_variant"` |
| 1101 | |
| 1102 | // prefix_symbols: if set, add an extra objcopy --prefix-symbols= step |
| 1103 | Prefix_symbols string |
| 1104 | } |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 1105 | } |
| 1106 | |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 1107 | func (c *CCBinary) getStem(ctx common.AndroidModuleContext) string { |
| 1108 | if c.BinaryProperties.Stem != "" { |
| 1109 | return c.BinaryProperties.Stem |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 1110 | } |
| 1111 | return ctx.ModuleName() |
| 1112 | } |
| 1113 | |
Colin Cross | 21b9a24 | 2015-03-24 14:15:58 -0700 | [diff] [blame] | 1114 | func (c *CCBinary) DepNames(ctx common.AndroidBaseContext, depNames CCDeps) CCDeps { |
| 1115 | depNames = c.ccDynamic.DepNames(ctx, depNames) |
Colin Cross | f6566ed | 2015-03-24 11:13:38 -0700 | [diff] [blame] | 1116 | if ctx.Device() { |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 1117 | if c.BinaryProperties.Static_executable { |
Colin Cross | 21b9a24 | 2015-03-24 14:15:58 -0700 | [diff] [blame] | 1118 | depNames.CrtBegin = "crtbegin_static" |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 1119 | } else { |
Colin Cross | 21b9a24 | 2015-03-24 14:15:58 -0700 | [diff] [blame] | 1120 | depNames.CrtBegin = "crtbegin_dynamic" |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 1121 | } |
Colin Cross | 21b9a24 | 2015-03-24 14:15:58 -0700 | [diff] [blame] | 1122 | depNames.CrtEnd = "crtend_android" |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 1123 | } |
Colin Cross | 21b9a24 | 2015-03-24 14:15:58 -0700 | [diff] [blame] | 1124 | return depNames |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 1125 | } |
| 1126 | |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 1127 | func NewCCBinary(binary *CCBinary, module CCModuleType, |
| 1128 | hod common.HostOrDeviceSupported) (blueprint.Module, []interface{}) { |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 1129 | |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 1130 | return newCCDynamic(&binary.ccDynamic, module, hod, common.MultilibFirst, |
| 1131 | &binary.BinaryProperties) |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 1132 | } |
| 1133 | |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 1134 | func CCBinaryFactory() (blueprint.Module, []interface{}) { |
| 1135 | module := &CCBinary{} |
| 1136 | |
| 1137 | return NewCCBinary(module, module, common.HostAndDeviceSupported) |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 1138 | } |
| 1139 | |
Colin Cross | 21b9a24 | 2015-03-24 14:15:58 -0700 | [diff] [blame] | 1140 | func (c *CCBinary) Flags(ctx common.AndroidModuleContext, flags CCFlags) CCFlags { |
| 1141 | flags = c.ccDynamic.Flags(ctx, flags) |
| 1142 | |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 1143 | flags.CFlags = append(flags.CFlags, "-fpie") |
| 1144 | |
Colin Cross | f6566ed | 2015-03-24 11:13:38 -0700 | [diff] [blame] | 1145 | if ctx.Device() { |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 1146 | linker := "/system/bin/linker" |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 1147 | if flags.Toolchain.Is64Bit() { |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 1148 | linker = "/system/bin/linker64" |
| 1149 | } |
| 1150 | |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 1151 | flags.LdFlags = append(flags.LdFlags, |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 1152 | "-nostdlib", |
| 1153 | "-Bdynamic", |
| 1154 | fmt.Sprintf("-Wl,-dynamic-linker,%s", linker), |
| 1155 | "-Wl,--gc-sections", |
| 1156 | "-Wl,-z,nocopyreloc", |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 1157 | ) |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 1158 | } |
| 1159 | |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 1160 | return flags |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 1161 | } |
| 1162 | |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 1163 | func (c *CCBinary) compileModule(ctx common.AndroidModuleContext, |
| 1164 | flags CCFlags, deps CCDeps, objFiles []string) { |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 1165 | |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 1166 | if !c.BinaryProperties.Static_executable && inList("libc", c.properties.Static_libs) { |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 1167 | ctx.ModuleErrorf("statically linking libc to dynamic executable, please remove libc\n" + |
| 1168 | "from static libs or set static_executable: true") |
| 1169 | } |
| 1170 | |
| 1171 | outputFile := filepath.Join(common.ModuleOutDir(ctx), c.getStem(ctx)) |
Dan Albert | c403f7c | 2015-03-18 14:01:18 -0700 | [diff] [blame] | 1172 | c.out = outputFile |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 1173 | |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 1174 | TransformObjToDynamicBinary(ctx, objFiles, deps.SharedLibs, deps.StaticLibs, |
| 1175 | deps.LateStaticLibs, deps.WholeStaticLibs, deps.CrtBegin, deps.CrtEnd, |
Colin Cross | 77b00fa | 2015-03-16 16:15:49 -0700 | [diff] [blame] | 1176 | ccFlagsToBuilderFlags(flags), outputFile) |
Dan Albert | c403f7c | 2015-03-18 14:01:18 -0700 | [diff] [blame] | 1177 | } |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 1178 | |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 1179 | func (c *CCBinary) installModule(ctx common.AndroidModuleContext, flags CCFlags) { |
Dan Albert | c403f7c | 2015-03-18 14:01:18 -0700 | [diff] [blame] | 1180 | ctx.InstallFile("bin", c.out) |
| 1181 | } |
| 1182 | |
| 1183 | type ccTest struct { |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 1184 | CCBinary |
Colin Cross | 6b29069 | 2015-03-19 14:05:33 -0700 | [diff] [blame] | 1185 | |
| 1186 | testProperties struct { |
| 1187 | // test_per_src: Create a separate test for each source file. Useful when there is |
| 1188 | // global state that can not be torn down and reset between each test suite. |
| 1189 | Test_per_src bool |
| 1190 | } |
Dan Albert | c403f7c | 2015-03-18 14:01:18 -0700 | [diff] [blame] | 1191 | } |
| 1192 | |
Colin Cross | 21b9a24 | 2015-03-24 14:15:58 -0700 | [diff] [blame] | 1193 | func (c *ccTest) Flags(ctx common.AndroidModuleContext, flags CCFlags) CCFlags { |
| 1194 | flags = c.CCBinary.Flags(ctx, flags) |
Dan Albert | c403f7c | 2015-03-18 14:01:18 -0700 | [diff] [blame] | 1195 | |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 1196 | flags.CFlags = append(flags.CFlags, "-DGTEST_HAS_STD_STRING") |
Colin Cross | f6566ed | 2015-03-24 11:13:38 -0700 | [diff] [blame] | 1197 | if ctx.Host() { |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 1198 | flags.CFlags = append(flags.CFlags, "-O0", "-g") |
| 1199 | flags.LdLibs = append(flags.LdLibs, "-lpthread") |
Dan Albert | c403f7c | 2015-03-18 14:01:18 -0700 | [diff] [blame] | 1200 | } |
| 1201 | |
| 1202 | // TODO(danalbert): Make gtest export its dependencies. |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 1203 | flags.IncludeDirs = append(flags.IncludeDirs, |
Tim Kilbourn | 5ccc730 | 2015-03-19 10:02:21 -0700 | [diff] [blame] | 1204 | filepath.Join(ctx.Config().(Config).SrcDir(), "external/gtest/include")) |
Dan Albert | c403f7c | 2015-03-18 14:01:18 -0700 | [diff] [blame] | 1205 | |
Colin Cross | 21b9a24 | 2015-03-24 14:15:58 -0700 | [diff] [blame] | 1206 | return flags |
Dan Albert | c403f7c | 2015-03-18 14:01:18 -0700 | [diff] [blame] | 1207 | } |
| 1208 | |
Colin Cross | 21b9a24 | 2015-03-24 14:15:58 -0700 | [diff] [blame] | 1209 | func (c *ccTest) DepNames(ctx common.AndroidBaseContext, depNames CCDeps) CCDeps { |
| 1210 | depNames = c.CCBinary.DepNames(ctx, depNames) |
| 1211 | depNames.StaticLibs = append(depNames.StaticLibs, "libgtest", "libgtest_main") |
| 1212 | return depNames |
Dan Albert | c403f7c | 2015-03-18 14:01:18 -0700 | [diff] [blame] | 1213 | } |
| 1214 | |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 1215 | func (c *ccTest) installModule(ctx common.AndroidModuleContext, flags CCFlags) { |
Colin Cross | f6566ed | 2015-03-24 11:13:38 -0700 | [diff] [blame] | 1216 | if ctx.Device() { |
Tim Kilbourn | 5ccc730 | 2015-03-19 10:02:21 -0700 | [diff] [blame] | 1217 | ctx.InstallFile("../data/nativetest/"+ctx.ModuleName(), c.out) |
Dan Albert | c403f7c | 2015-03-18 14:01:18 -0700 | [diff] [blame] | 1218 | } else { |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 1219 | c.CCBinary.installModule(ctx, flags) |
Dan Albert | c403f7c | 2015-03-18 14:01:18 -0700 | [diff] [blame] | 1220 | } |
| 1221 | } |
| 1222 | |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 1223 | func CCTestFactory() (blueprint.Module, []interface{}) { |
Dan Albert | c403f7c | 2015-03-18 14:01:18 -0700 | [diff] [blame] | 1224 | module := &ccTest{} |
| 1225 | return newCCDynamic(&module.ccDynamic, module, common.HostAndDeviceSupported, |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 1226 | common.MultilibFirst, &module.BinaryProperties, &module.testProperties) |
Colin Cross | 6b29069 | 2015-03-19 14:05:33 -0700 | [diff] [blame] | 1227 | } |
| 1228 | |
| 1229 | func TestPerSrcMutator(mctx blueprint.EarlyMutatorContext) { |
| 1230 | if test, ok := mctx.Module().(*ccTest); ok { |
| 1231 | if test.testProperties.Test_per_src { |
| 1232 | testNames := make([]string, len(test.properties.Srcs)) |
| 1233 | for i, src := range test.properties.Srcs { |
| 1234 | testNames[i] = strings.TrimSuffix(src, filepath.Ext(src)) |
| 1235 | } |
| 1236 | tests := mctx.CreateLocalVariations(testNames...) |
| 1237 | for i, src := range test.properties.Srcs { |
| 1238 | tests[i].(*ccTest).properties.Srcs = []string{src} |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 1239 | tests[i].(*ccTest).BinaryProperties.Stem = testNames[i] |
Colin Cross | 6b29069 | 2015-03-19 14:05:33 -0700 | [diff] [blame] | 1240 | } |
| 1241 | } |
| 1242 | } |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 1243 | } |
| 1244 | |
| 1245 | // |
| 1246 | // Static library |
| 1247 | // |
| 1248 | |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 1249 | func CCLibraryStaticFactory() (blueprint.Module, []interface{}) { |
| 1250 | module := &CCLibrary{} |
| 1251 | module.LibraryProperties.BuildStatic = true |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 1252 | |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 1253 | return NewCCLibrary(module, module, common.HostAndDeviceSupported) |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 1254 | } |
| 1255 | |
| 1256 | // |
| 1257 | // Shared libraries |
| 1258 | // |
| 1259 | |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 1260 | func CCLibrarySharedFactory() (blueprint.Module, []interface{}) { |
| 1261 | module := &CCLibrary{} |
| 1262 | module.LibraryProperties.BuildShared = true |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 1263 | |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 1264 | return NewCCLibrary(module, module, common.HostAndDeviceSupported) |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 1265 | } |
| 1266 | |
| 1267 | // |
| 1268 | // Host static library |
| 1269 | // |
| 1270 | |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 1271 | func CCLibraryHostStaticFactory() (blueprint.Module, []interface{}) { |
| 1272 | module := &CCLibrary{} |
| 1273 | module.LibraryProperties.BuildStatic = true |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 1274 | |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 1275 | return NewCCLibrary(module, module, common.HostSupported) |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 1276 | } |
| 1277 | |
| 1278 | // |
| 1279 | // Host Shared libraries |
| 1280 | // |
| 1281 | |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 1282 | func CCLibraryHostSharedFactory() (blueprint.Module, []interface{}) { |
| 1283 | module := &CCLibrary{} |
| 1284 | module.LibraryProperties.BuildShared = true |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 1285 | |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 1286 | return NewCCLibrary(module, module, common.HostSupported) |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 1287 | } |
| 1288 | |
| 1289 | // |
| 1290 | // Host Binaries |
| 1291 | // |
| 1292 | |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 1293 | func CCBinaryHostFactory() (blueprint.Module, []interface{}) { |
| 1294 | module := &CCBinary{} |
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 NewCCBinary(module, module, common.HostSupported) |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 1297 | } |
| 1298 | |
| 1299 | // |
| 1300 | // Device libraries shipped with gcc |
| 1301 | // |
| 1302 | |
| 1303 | type toolchainLibrary struct { |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 1304 | CCLibrary |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 1305 | } |
| 1306 | |
| 1307 | func (*toolchainLibrary) AndroidDynamicDependencies(ctx common.AndroidDynamicDependerModuleContext) []string { |
| 1308 | // toolchain libraries can't have any dependencies |
| 1309 | return nil |
| 1310 | } |
| 1311 | |
Colin Cross | 21b9a24 | 2015-03-24 14:15:58 -0700 | [diff] [blame] | 1312 | func (*toolchainLibrary) DepNames(ctx common.AndroidBaseContext, depNames CCDeps) CCDeps { |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 1313 | // toolchain libraries can't have any dependencies |
Colin Cross | 21b9a24 | 2015-03-24 14:15:58 -0700 | [diff] [blame] | 1314 | return CCDeps{} |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 1315 | } |
| 1316 | |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 1317 | func ToolchainLibraryFactory() (blueprint.Module, []interface{}) { |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 1318 | module := &toolchainLibrary{} |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 1319 | |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 1320 | module.LibraryProperties.BuildStatic = true |
| 1321 | |
Colin Cross | 21b9a24 | 2015-03-24 14:15:58 -0700 | [diff] [blame] | 1322 | return newCCBase(&module.ccBase, module, common.DeviceSupported, common.MultilibBoth, |
| 1323 | &module.LibraryProperties) |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 1324 | } |
| 1325 | |
| 1326 | func (c *toolchainLibrary) compileModule(ctx common.AndroidModuleContext, |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 1327 | flags CCFlags, deps CCDeps, objFiles []string) { |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 1328 | |
| 1329 | libName := ctx.ModuleName() + staticLibraryExtension |
| 1330 | outputFile := filepath.Join(common.ModuleOutDir(ctx), libName) |
| 1331 | |
| 1332 | CopyGccLib(ctx, libName, ccFlagsToBuilderFlags(flags), outputFile) |
| 1333 | |
| 1334 | c.out = outputFile |
| 1335 | |
| 1336 | ctx.CheckbuildFile(outputFile) |
| 1337 | } |
| 1338 | |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 1339 | func (c *toolchainLibrary) installModule(ctx common.AndroidModuleContext, flags CCFlags) { |
Dan Albert | c403f7c | 2015-03-18 14:01:18 -0700 | [diff] [blame] | 1340 | // Toolchain libraries do not get installed. |
| 1341 | } |
| 1342 | |
Dan Albert | be96168 | 2015-03-18 23:38:50 -0700 | [diff] [blame^] | 1343 | // NDK prebuilt libraries. |
| 1344 | // |
| 1345 | // These differ from regular prebuilts in that they aren't stripped and usually aren't installed |
| 1346 | // either (with the exception of the shared STLs, which are installed to the app's directory rather |
| 1347 | // than to the system image). |
| 1348 | |
| 1349 | func getNdkLibDir(ctx common.AndroidModuleContext, toolchain Toolchain, version string) string { |
| 1350 | return fmt.Sprintf("%s/prebuilts/ndk/current/platforms/android-%s/arch-%s/usr/lib", |
| 1351 | ctx.Config().(Config).SrcDir(), version, toolchain.Name()) |
| 1352 | } |
| 1353 | |
| 1354 | type ndkPrebuiltLibrary struct { |
| 1355 | CCLibrary |
| 1356 | } |
| 1357 | |
| 1358 | func (*ndkPrebuiltLibrary) AndroidDynamicDependencies( |
| 1359 | ctx common.AndroidDynamicDependerModuleContext) []string { |
| 1360 | |
| 1361 | // NDK libraries can't have any dependencies |
| 1362 | return nil |
| 1363 | } |
| 1364 | |
| 1365 | func (*ndkPrebuiltLibrary) DepNames(ctx common.AndroidBaseContext, depNames CCDeps) CCDeps { |
| 1366 | // NDK libraries can't have any dependencies |
| 1367 | return CCDeps{} |
| 1368 | } |
| 1369 | |
| 1370 | func NdkPrebuiltLibraryFactory() (blueprint.Module, []interface{}) { |
| 1371 | module := &ndkPrebuiltLibrary{} |
| 1372 | module.LibraryProperties.BuildShared = true |
| 1373 | return NewCCLibrary(&module.CCLibrary, module, common.DeviceSupported) |
| 1374 | } |
| 1375 | |
| 1376 | func (c *ndkPrebuiltLibrary) compileModule(ctx common.AndroidModuleContext, flags CCFlags, |
| 1377 | deps CCDeps, objFiles []string) { |
| 1378 | // A null build step, but it sets up the output path. |
| 1379 | if !strings.HasPrefix(ctx.ModuleName(), "ndk_lib") { |
| 1380 | ctx.ModuleErrorf("NDK prebuilts must have an ndk_lib prefixed name") |
| 1381 | } |
| 1382 | |
| 1383 | c.exportIncludeDirs = pathtools.PrefixPaths(c.properties.Export_include_dirs, |
| 1384 | common.ModuleSrcDir(ctx)) |
| 1385 | |
| 1386 | // NDK prebuilt libraries are named like: ndk_LIBNAME.SDK_VERSION. |
| 1387 | // We want to translate to just LIBNAME. |
| 1388 | libName := strings.Split(strings.TrimPrefix(ctx.ModuleName(), "ndk_"), ".")[0] |
| 1389 | libDir := getNdkLibDir(ctx, flags.Toolchain, c.properties.Sdk_version) |
| 1390 | c.out = filepath.Join(libDir, libName + sharedLibraryExtension) |
| 1391 | } |
| 1392 | |
| 1393 | func (c *ndkPrebuiltLibrary) installModule(ctx common.AndroidModuleContext, flags CCFlags) { |
| 1394 | // Toolchain libraries do not get installed. |
| 1395 | } |
| 1396 | |
| 1397 | // The NDK STLs are slightly different from the prebuilt system libraries: |
| 1398 | // * Are not specific to each platform version. |
| 1399 | // * The libraries are not in a predictable location for each STL. |
| 1400 | |
| 1401 | type ndkPrebuiltStl struct { |
| 1402 | ndkPrebuiltLibrary |
| 1403 | } |
| 1404 | |
| 1405 | type ndkPrebuiltStaticStl struct { |
| 1406 | ndkPrebuiltStl |
| 1407 | } |
| 1408 | |
| 1409 | type ndkPrebuiltSharedStl struct { |
| 1410 | ndkPrebuiltStl |
| 1411 | } |
| 1412 | |
| 1413 | func NdkPrebuiltSharedStlFactory() (blueprint.Module, []interface{}) { |
| 1414 | module := &ndkPrebuiltSharedStl{} |
| 1415 | module.LibraryProperties.BuildShared = true |
| 1416 | return NewCCLibrary(&module.CCLibrary, module, common.DeviceSupported) |
| 1417 | } |
| 1418 | |
| 1419 | func NdkPrebuiltStaticStlFactory() (blueprint.Module, []interface{}) { |
| 1420 | module := &ndkPrebuiltStaticStl{} |
| 1421 | module.LibraryProperties.BuildStatic = true |
| 1422 | return NewCCLibrary(&module.CCLibrary, module, common.DeviceSupported) |
| 1423 | } |
| 1424 | |
| 1425 | func getNdkStlLibDir(ctx common.AndroidModuleContext, toolchain Toolchain, stl string) string { |
| 1426 | gccVersion := toolchain.GccVersion() |
| 1427 | var libDir string |
| 1428 | switch stl { |
| 1429 | case "libstlport": |
| 1430 | libDir = "cxx-stl/stlport/libs" |
| 1431 | case "libc++": |
| 1432 | libDir = "cxx-stl/llvm-libc++/libs" |
| 1433 | case "libgnustl": |
| 1434 | libDir = fmt.Sprintf("cxx-stl/gnu-libstdc++/%s/libs", gccVersion) |
| 1435 | } |
| 1436 | |
| 1437 | if libDir != "" { |
| 1438 | ndkSrcRoot := ctx.Config().(Config).SrcDir() + "/prebuilts/ndk/current/sources" |
| 1439 | return fmt.Sprintf("%s/%s/%s", ndkSrcRoot, libDir, ctx.Arch().Abi) |
| 1440 | } |
| 1441 | |
| 1442 | ctx.ModuleErrorf("Unknown NDK STL: %s", stl) |
| 1443 | return "" |
| 1444 | } |
| 1445 | |
| 1446 | func (c *ndkPrebuiltStl) compileModule(ctx common.AndroidModuleContext, flags CCFlags, |
| 1447 | deps CCDeps, objFiles []string) { |
| 1448 | // A null build step, but it sets up the output path. |
| 1449 | if !strings.HasPrefix(ctx.ModuleName(), "ndk_lib") { |
| 1450 | ctx.ModuleErrorf("NDK prebuilts must have an ndk_lib prefixed name") |
| 1451 | } |
| 1452 | |
| 1453 | c.exportIncludeDirs = pathtools.PrefixPaths(c.properties.Export_include_dirs, |
| 1454 | common.ModuleSrcDir(ctx)) |
| 1455 | |
| 1456 | libName := strings.TrimPrefix(ctx.ModuleName(), "ndk_") |
| 1457 | libExt := sharedLibraryExtension |
| 1458 | if c.LibraryProperties.BuildStatic { |
| 1459 | libExt = staticLibraryExtension |
| 1460 | } |
| 1461 | |
| 1462 | stlName := strings.TrimSuffix(libName, "_shared") |
| 1463 | stlName = strings.TrimSuffix(stlName, "_static") |
| 1464 | libDir := getNdkStlLibDir(ctx, flags.Toolchain, stlName) |
| 1465 | c.out = libDir + "/" + libName + libExt |
| 1466 | } |
| 1467 | |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 1468 | func LinkageMutator(mctx blueprint.EarlyMutatorContext) { |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 1469 | if c, ok := mctx.Module().(ccLibraryInterface); ok { |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 1470 | var modules []blueprint.Module |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 1471 | if c.ccLibrary().LibraryProperties.BuildStatic && c.ccLibrary().LibraryProperties.BuildShared { |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 1472 | modules = mctx.CreateLocalVariations("static", "shared") |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 1473 | modules[0].(ccLibraryInterface).ccLibrary().LibraryProperties.IsStatic = true |
| 1474 | modules[1].(ccLibraryInterface).ccLibrary().LibraryProperties.IsShared = true |
| 1475 | } else if c.ccLibrary().LibraryProperties.BuildStatic { |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 1476 | modules = mctx.CreateLocalVariations("static") |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 1477 | modules[0].(ccLibraryInterface).ccLibrary().LibraryProperties.IsStatic = true |
| 1478 | } else if c.ccLibrary().LibraryProperties.BuildShared { |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 1479 | modules = mctx.CreateLocalVariations("shared") |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 1480 | modules[0].(ccLibraryInterface).ccLibrary().LibraryProperties.IsShared = true |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 1481 | } else { |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 1482 | panic(fmt.Errorf("ccLibrary %q not static or shared", mctx.ModuleName())) |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 1483 | } |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 1484 | primary := modules[0].(ccLibraryInterface).ccLibrary() |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 1485 | for _, m := range modules { |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 1486 | m.(ccLibraryInterface).ccLibrary().primary = primary |
| 1487 | if m.(ccLibraryInterface).ccLibrary() != primary { |
| 1488 | m.(ccLibraryInterface).ccLibrary().properties.SkipCompileObjs = true |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 1489 | } |
| 1490 | } |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 1491 | } |
| 1492 | } |