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