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