Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 1 | // Copyright 2015 Google Inc. All rights reserved. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package common |
| 16 | |
| 17 | import ( |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 18 | "path/filepath" |
Colin Cross | 0af4b84 | 2015-04-30 16:36:18 -0700 | [diff] [blame] | 19 | "runtime" |
Colin Cross | f6566ed | 2015-03-24 11:13:38 -0700 | [diff] [blame] | 20 | |
Colin Cross | 8f101b4 | 2015-06-17 15:09:06 -0700 | [diff] [blame^] | 21 | "android/soong/glob" |
| 22 | |
Colin Cross | f6566ed | 2015-03-24 11:13:38 -0700 | [diff] [blame] | 23 | "github.com/google/blueprint" |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 24 | ) |
| 25 | |
| 26 | var ( |
| 27 | DeviceSharedLibrary = "shared_library" |
| 28 | DeviceStaticLibrary = "static_library" |
| 29 | DeviceExecutable = "executable" |
| 30 | HostSharedLibrary = "host_shared_library" |
| 31 | HostStaticLibrary = "host_static_library" |
| 32 | HostExecutable = "host_executable" |
| 33 | ) |
| 34 | |
Colin Cross | f6566ed | 2015-03-24 11:13:38 -0700 | [diff] [blame] | 35 | type androidBaseContext interface { |
| 36 | Arch() Arch |
Colin Cross | d3ba039 | 2015-05-07 14:11:29 -0700 | [diff] [blame] | 37 | HostOrDevice() HostOrDevice |
Colin Cross | f6566ed | 2015-03-24 11:13:38 -0700 | [diff] [blame] | 38 | Host() bool |
| 39 | Device() bool |
Colin Cross | 0af4b84 | 2015-04-30 16:36:18 -0700 | [diff] [blame] | 40 | Darwin() bool |
Colin Cross | f6566ed | 2015-03-24 11:13:38 -0700 | [diff] [blame] | 41 | Debug() bool |
Colin Cross | 1332b00 | 2015-04-07 17:11:30 -0700 | [diff] [blame] | 42 | AConfig() Config |
Colin Cross | f6566ed | 2015-03-24 11:13:38 -0700 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | type AndroidBaseContext interface { |
| 46 | blueprint.BaseModuleContext |
| 47 | androidBaseContext |
| 48 | } |
| 49 | |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 50 | type AndroidModuleContext interface { |
| 51 | blueprint.ModuleContext |
Colin Cross | f6566ed | 2015-03-24 11:13:38 -0700 | [diff] [blame] | 52 | androidBaseContext |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 53 | |
Colin Cross | 8f101b4 | 2015-06-17 15:09:06 -0700 | [diff] [blame^] | 54 | ExpandSources(srcFiles []string) []string |
| 55 | Glob(globPattern string, excludes []string) []string |
| 56 | |
Colin Cross | 35cec12 | 2015-04-02 14:37:16 -0700 | [diff] [blame] | 57 | InstallFile(installPath, srcPath string, deps ...string) string |
| 58 | InstallFileName(installPath, name, srcPath string, deps ...string) string |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 59 | CheckbuildFile(srcPath string) |
| 60 | } |
| 61 | |
| 62 | type AndroidModule interface { |
| 63 | blueprint.Module |
| 64 | |
| 65 | GenerateAndroidBuildActions(AndroidModuleContext) |
| 66 | |
| 67 | base() *AndroidModuleBase |
| 68 | Disabled() bool |
| 69 | HostOrDevice() HostOrDevice |
| 70 | } |
| 71 | |
| 72 | type AndroidDynamicDepender interface { |
| 73 | AndroidDynamicDependencies(ctx AndroidDynamicDependerModuleContext) []string |
| 74 | } |
| 75 | |
| 76 | type AndroidDynamicDependerModuleContext interface { |
| 77 | blueprint.DynamicDependerModuleContext |
Colin Cross | f6566ed | 2015-03-24 11:13:38 -0700 | [diff] [blame] | 78 | androidBaseContext |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | type commonProperties struct { |
Colin Cross | c77f9d1 | 2015-04-02 13:54:39 -0700 | [diff] [blame] | 82 | Name string |
| 83 | Deps []string |
| 84 | Tags []string |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 85 | |
| 86 | // disabled: don't emit any build rules for this module |
| 87 | Disabled bool `android:"arch_variant"` |
| 88 | |
| 89 | // multilib: control whether this module compiles for 32-bit, 64-bit, or both. Possible values |
| 90 | // are "32" (compile for 32-bit only), "64" (compile for 64-bit only), "both" (compile for both |
| 91 | // architectures), or "first" (compile for 64-bit on a 64-bit platform, and 32-bit on a 32-bit |
| 92 | // platform |
| 93 | Compile_multilib string |
| 94 | |
Colin Cross | d3ba039 | 2015-05-07 14:11:29 -0700 | [diff] [blame] | 95 | // Set by HostOrDeviceMutator |
| 96 | CompileHostOrDevice HostOrDevice `blueprint:"mutated"` |
| 97 | |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 98 | // Set by ArchMutator |
| 99 | CompileArch Arch `blueprint:"mutated"` |
| 100 | |
| 101 | // Set by InitAndroidModule |
| 102 | HostOrDeviceSupported HostOrDeviceSupported `blueprint:"mutated"` |
| 103 | } |
| 104 | |
| 105 | type hostAndDeviceProperties struct { |
| 106 | Host_supported bool |
| 107 | Device_supported bool |
| 108 | } |
| 109 | |
Colin Cross | c472d57 | 2015-03-17 15:06:21 -0700 | [diff] [blame] | 110 | type Multilib string |
| 111 | |
| 112 | const ( |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 113 | MultilibBoth Multilib = "both" |
| 114 | MultilibFirst Multilib = "first" |
| 115 | MultilibCommon Multilib = "common" |
Colin Cross | c472d57 | 2015-03-17 15:06:21 -0700 | [diff] [blame] | 116 | ) |
| 117 | |
Colin Cross | 5049f02 | 2015-03-18 13:28:46 -0700 | [diff] [blame] | 118 | func InitAndroidModule(m AndroidModule, |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 119 | propertyStructs ...interface{}) (blueprint.Module, []interface{}) { |
| 120 | |
| 121 | base := m.base() |
| 122 | base.module = m |
Colin Cross | 28d7659 | 2015-03-26 16:14:04 -0700 | [diff] [blame] | 123 | base.extendedProperties = make(map[string]struct{}) |
Colin Cross | 5049f02 | 2015-03-18 13:28:46 -0700 | [diff] [blame] | 124 | |
| 125 | propertyStructs = append(propertyStructs, &base.commonProperties) |
| 126 | |
| 127 | return m, propertyStructs |
| 128 | } |
| 129 | |
| 130 | func InitAndroidArchModule(m AndroidModule, hod HostOrDeviceSupported, defaultMultilib Multilib, |
| 131 | propertyStructs ...interface{}) (blueprint.Module, []interface{}) { |
| 132 | |
| 133 | _, propertyStructs = InitAndroidModule(m, propertyStructs...) |
| 134 | |
| 135 | base := m.base() |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 136 | base.commonProperties.HostOrDeviceSupported = hod |
| 137 | |
| 138 | if hod == HostAndDeviceSupported { |
| 139 | // Default to module to device supported, host not supported, can override in module |
| 140 | // properties |
| 141 | base.hostAndDeviceProperties.Device_supported = true |
| 142 | propertyStructs = append(propertyStructs, &base.hostAndDeviceProperties) |
| 143 | } |
| 144 | |
| 145 | return InitArchModule(m, defaultMultilib, propertyStructs...) |
| 146 | } |
| 147 | |
| 148 | // A AndroidModuleBase object contains the properties that are common to all Android |
| 149 | // modules. It should be included as an anonymous field in every module |
| 150 | // struct definition. InitAndroidModule should then be called from the module's |
| 151 | // factory function, and the return values from InitAndroidModule should be |
| 152 | // returned from the factory function. |
| 153 | // |
| 154 | // The AndroidModuleBase type is responsible for implementing the |
| 155 | // GenerateBuildActions method to support the blueprint.Module interface. This |
| 156 | // method will then call the module's GenerateAndroidBuildActions method once |
| 157 | // for each build variant that is to be built. GenerateAndroidBuildActions is |
| 158 | // passed a AndroidModuleContext rather than the usual blueprint.ModuleContext. |
| 159 | // AndroidModuleContext exposes extra functionality specific to the Android build |
| 160 | // system including details about the particular build variant that is to be |
| 161 | // generated. |
| 162 | // |
| 163 | // For example: |
| 164 | // |
| 165 | // import ( |
| 166 | // "android/soong/common" |
Colin Cross | 70b4059 | 2015-03-23 12:57:34 -0700 | [diff] [blame] | 167 | // "github.com/google/blueprint" |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 168 | // ) |
| 169 | // |
| 170 | // type myModule struct { |
| 171 | // common.AndroidModuleBase |
| 172 | // properties struct { |
| 173 | // MyProperty string |
| 174 | // } |
| 175 | // } |
| 176 | // |
| 177 | // func NewMyModule() (blueprint.Module, []interface{}) { |
| 178 | // m := &myModule{} |
| 179 | // return common.InitAndroidModule(m, &m.properties) |
| 180 | // } |
| 181 | // |
| 182 | // func (m *myModule) GenerateAndroidBuildActions(ctx common.AndroidModuleContext) { |
| 183 | // // Get the CPU architecture for the current build variant. |
| 184 | // variantArch := ctx.Arch() |
| 185 | // |
| 186 | // // ... |
| 187 | // } |
| 188 | type AndroidModuleBase struct { |
| 189 | // Putting the curiously recurring thing pointing to the thing that contains |
| 190 | // the thing pattern to good use. |
| 191 | module AndroidModule |
| 192 | |
| 193 | commonProperties commonProperties |
| 194 | hostAndDeviceProperties hostAndDeviceProperties |
| 195 | generalProperties []interface{} |
| 196 | archProperties []*archProperties |
Colin Cross | 28d7659 | 2015-03-26 16:14:04 -0700 | [diff] [blame] | 197 | extendedProperties map[string]struct{} |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 198 | |
| 199 | noAddressSanitizer bool |
| 200 | installFiles []string |
| 201 | checkbuildFiles []string |
Colin Cross | 1f8c52b | 2015-06-16 16:38:17 -0700 | [diff] [blame] | 202 | |
| 203 | // Used by buildTargetSingleton to create checkbuild and per-directory build targets |
| 204 | // Only set on the final variant of each module |
| 205 | installTarget string |
| 206 | checkbuildTarget string |
| 207 | blueprintDir string |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | func (a *AndroidModuleBase) base() *AndroidModuleBase { |
| 211 | return a |
| 212 | } |
| 213 | |
Colin Cross | d3ba039 | 2015-05-07 14:11:29 -0700 | [diff] [blame] | 214 | func (a *AndroidModuleBase) SetHostOrDevice(hod HostOrDevice) { |
| 215 | a.commonProperties.CompileHostOrDevice = hod |
| 216 | } |
| 217 | |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 218 | func (a *AndroidModuleBase) SetArch(arch Arch) { |
| 219 | a.commonProperties.CompileArch = arch |
| 220 | } |
| 221 | |
| 222 | func (a *AndroidModuleBase) HostOrDevice() HostOrDevice { |
Colin Cross | d3ba039 | 2015-05-07 14:11:29 -0700 | [diff] [blame] | 223 | return a.commonProperties.CompileHostOrDevice |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | func (a *AndroidModuleBase) HostSupported() bool { |
| 227 | return a.commonProperties.HostOrDeviceSupported == HostSupported || |
| 228 | a.commonProperties.HostOrDeviceSupported == HostAndDeviceSupported && |
| 229 | a.hostAndDeviceProperties.Host_supported |
| 230 | } |
| 231 | |
| 232 | func (a *AndroidModuleBase) DeviceSupported() bool { |
| 233 | return a.commonProperties.HostOrDeviceSupported == DeviceSupported || |
| 234 | a.commonProperties.HostOrDeviceSupported == HostAndDeviceSupported && |
| 235 | a.hostAndDeviceProperties.Device_supported |
| 236 | } |
| 237 | |
| 238 | func (a *AndroidModuleBase) Disabled() bool { |
| 239 | return a.commonProperties.Disabled |
| 240 | } |
| 241 | |
| 242 | func (a *AndroidModuleBase) computeInstallDeps( |
| 243 | ctx blueprint.ModuleContext) []string { |
| 244 | |
| 245 | result := []string{} |
| 246 | ctx.VisitDepsDepthFirstIf(isFileInstaller, |
| 247 | func(m blueprint.Module) { |
| 248 | fileInstaller := m.(fileInstaller) |
| 249 | files := fileInstaller.filesToInstall() |
| 250 | result = append(result, files...) |
| 251 | }) |
| 252 | |
| 253 | return result |
| 254 | } |
| 255 | |
| 256 | func (a *AndroidModuleBase) filesToInstall() []string { |
| 257 | return a.installFiles |
| 258 | } |
| 259 | |
| 260 | func (p *AndroidModuleBase) NoAddressSanitizer() bool { |
| 261 | return p.noAddressSanitizer |
| 262 | } |
| 263 | |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 264 | func (a *AndroidModuleBase) generateModuleTarget(ctx blueprint.ModuleContext) { |
| 265 | if a != ctx.FinalModule().(AndroidModule).base() { |
| 266 | return |
| 267 | } |
| 268 | |
| 269 | allInstalledFiles := []string{} |
Colin Cross | 9454bfa | 2015-03-17 13:24:18 -0700 | [diff] [blame] | 270 | allCheckbuildFiles := []string{} |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 271 | ctx.VisitAllModuleVariants(func(module blueprint.Module) { |
Colin Cross | c940435 | 2015-03-26 16:10:12 -0700 | [diff] [blame] | 272 | a := module.(AndroidModule).base() |
| 273 | allInstalledFiles = append(allInstalledFiles, a.installFiles...) |
| 274 | allCheckbuildFiles = append(allCheckbuildFiles, a.checkbuildFiles...) |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 275 | }) |
| 276 | |
Colin Cross | 9454bfa | 2015-03-17 13:24:18 -0700 | [diff] [blame] | 277 | deps := []string{} |
| 278 | |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 279 | if len(allInstalledFiles) > 0 { |
Colin Cross | 9454bfa | 2015-03-17 13:24:18 -0700 | [diff] [blame] | 280 | name := ctx.ModuleName() + "-install" |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 281 | ctx.Build(pctx, blueprint.BuildParams{ |
Colin Cross | 9454bfa | 2015-03-17 13:24:18 -0700 | [diff] [blame] | 282 | Rule: blueprint.Phony, |
| 283 | Outputs: []string{name}, |
| 284 | Implicits: allInstalledFiles, |
| 285 | }) |
| 286 | deps = append(deps, name) |
Colin Cross | 1f8c52b | 2015-06-16 16:38:17 -0700 | [diff] [blame] | 287 | a.installTarget = name |
Colin Cross | 9454bfa | 2015-03-17 13:24:18 -0700 | [diff] [blame] | 288 | } |
| 289 | |
| 290 | if len(allCheckbuildFiles) > 0 { |
| 291 | name := ctx.ModuleName() + "-checkbuild" |
| 292 | ctx.Build(pctx, blueprint.BuildParams{ |
| 293 | Rule: blueprint.Phony, |
| 294 | Outputs: []string{name}, |
| 295 | Implicits: allCheckbuildFiles, |
| 296 | Optional: true, |
| 297 | }) |
| 298 | deps = append(deps, name) |
Colin Cross | 1f8c52b | 2015-06-16 16:38:17 -0700 | [diff] [blame] | 299 | a.checkbuildTarget = name |
Colin Cross | 9454bfa | 2015-03-17 13:24:18 -0700 | [diff] [blame] | 300 | } |
| 301 | |
| 302 | if len(deps) > 0 { |
| 303 | ctx.Build(pctx, blueprint.BuildParams{ |
| 304 | Rule: blueprint.Phony, |
| 305 | Outputs: []string{ctx.ModuleName()}, |
| 306 | Implicits: deps, |
| 307 | Optional: true, |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 308 | }) |
Colin Cross | 1f8c52b | 2015-06-16 16:38:17 -0700 | [diff] [blame] | 309 | |
| 310 | a.blueprintDir = ctx.ModuleDir() |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 311 | } |
| 312 | } |
| 313 | |
| 314 | func (a *AndroidModuleBase) DynamicDependencies(ctx blueprint.DynamicDependerModuleContext) []string { |
| 315 | actx := &androidDynamicDependerContext{ |
| 316 | DynamicDependerModuleContext: ctx, |
Colin Cross | f6566ed | 2015-03-24 11:13:38 -0700 | [diff] [blame] | 317 | androidBaseContextImpl: androidBaseContextImpl{ |
Colin Cross | 1332b00 | 2015-04-07 17:11:30 -0700 | [diff] [blame] | 318 | arch: a.commonProperties.CompileArch, |
Colin Cross | d3ba039 | 2015-05-07 14:11:29 -0700 | [diff] [blame] | 319 | hod: a.commonProperties.CompileHostOrDevice, |
Colin Cross | 1332b00 | 2015-04-07 17:11:30 -0700 | [diff] [blame] | 320 | config: ctx.Config().(Config), |
Colin Cross | f6566ed | 2015-03-24 11:13:38 -0700 | [diff] [blame] | 321 | }, |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 322 | } |
| 323 | |
| 324 | if dynamic, ok := a.module.(AndroidDynamicDepender); ok { |
| 325 | return dynamic.AndroidDynamicDependencies(actx) |
| 326 | } |
| 327 | |
| 328 | return nil |
| 329 | } |
| 330 | |
| 331 | func (a *AndroidModuleBase) GenerateBuildActions(ctx blueprint.ModuleContext) { |
| 332 | androidCtx := &androidModuleContext{ |
| 333 | ModuleContext: ctx, |
Colin Cross | f6566ed | 2015-03-24 11:13:38 -0700 | [diff] [blame] | 334 | androidBaseContextImpl: androidBaseContextImpl{ |
Colin Cross | 1332b00 | 2015-04-07 17:11:30 -0700 | [diff] [blame] | 335 | arch: a.commonProperties.CompileArch, |
Colin Cross | d3ba039 | 2015-05-07 14:11:29 -0700 | [diff] [blame] | 336 | hod: a.commonProperties.CompileHostOrDevice, |
Colin Cross | 1332b00 | 2015-04-07 17:11:30 -0700 | [diff] [blame] | 337 | config: ctx.Config().(Config), |
Colin Cross | f6566ed | 2015-03-24 11:13:38 -0700 | [diff] [blame] | 338 | }, |
Colin Cross | 28d7659 | 2015-03-26 16:14:04 -0700 | [diff] [blame] | 339 | installDeps: a.computeInstallDeps(ctx), |
| 340 | installFiles: a.installFiles, |
| 341 | extendedProperties: a.extendedProperties, |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 342 | } |
| 343 | |
| 344 | if a.commonProperties.Disabled { |
| 345 | return |
| 346 | } |
| 347 | |
| 348 | a.module.GenerateAndroidBuildActions(androidCtx) |
| 349 | if ctx.Failed() { |
| 350 | return |
| 351 | } |
| 352 | |
Colin Cross | c940435 | 2015-03-26 16:10:12 -0700 | [diff] [blame] | 353 | a.installFiles = append(a.installFiles, androidCtx.installFiles...) |
| 354 | a.checkbuildFiles = append(a.checkbuildFiles, androidCtx.checkbuildFiles...) |
| 355 | |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 356 | a.generateModuleTarget(ctx) |
| 357 | if ctx.Failed() { |
| 358 | return |
| 359 | } |
| 360 | } |
| 361 | |
Colin Cross | f6566ed | 2015-03-24 11:13:38 -0700 | [diff] [blame] | 362 | type androidBaseContextImpl struct { |
Colin Cross | 1332b00 | 2015-04-07 17:11:30 -0700 | [diff] [blame] | 363 | arch Arch |
Colin Cross | d3ba039 | 2015-05-07 14:11:29 -0700 | [diff] [blame] | 364 | hod HostOrDevice |
Colin Cross | 1332b00 | 2015-04-07 17:11:30 -0700 | [diff] [blame] | 365 | debug bool |
| 366 | config Config |
Colin Cross | f6566ed | 2015-03-24 11:13:38 -0700 | [diff] [blame] | 367 | } |
| 368 | |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 369 | type androidModuleContext struct { |
| 370 | blueprint.ModuleContext |
Colin Cross | f6566ed | 2015-03-24 11:13:38 -0700 | [diff] [blame] | 371 | androidBaseContextImpl |
Colin Cross | 28d7659 | 2015-03-26 16:14:04 -0700 | [diff] [blame] | 372 | installDeps []string |
| 373 | installFiles []string |
| 374 | checkbuildFiles []string |
| 375 | extendedProperties map[string]struct{} |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 376 | } |
| 377 | |
| 378 | func (a *androidModuleContext) Build(pctx *blueprint.PackageContext, params blueprint.BuildParams) { |
| 379 | params.Optional = true |
| 380 | a.ModuleContext.Build(pctx, params) |
| 381 | } |
| 382 | |
Colin Cross | 28d7659 | 2015-03-26 16:14:04 -0700 | [diff] [blame] | 383 | func (a *androidModuleContext) ContainsProperty(property string) bool { |
| 384 | if a.ModuleContext.ContainsProperty(property) { |
| 385 | return true |
| 386 | } |
| 387 | _, ok := a.extendedProperties[property] |
| 388 | return ok |
| 389 | } |
| 390 | |
Colin Cross | f6566ed | 2015-03-24 11:13:38 -0700 | [diff] [blame] | 391 | func (a *androidBaseContextImpl) Arch() Arch { |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 392 | return a.arch |
| 393 | } |
| 394 | |
Colin Cross | d3ba039 | 2015-05-07 14:11:29 -0700 | [diff] [blame] | 395 | func (a *androidBaseContextImpl) HostOrDevice() HostOrDevice { |
| 396 | return a.hod |
| 397 | } |
| 398 | |
Colin Cross | f6566ed | 2015-03-24 11:13:38 -0700 | [diff] [blame] | 399 | func (a *androidBaseContextImpl) Host() bool { |
Colin Cross | d3ba039 | 2015-05-07 14:11:29 -0700 | [diff] [blame] | 400 | return a.hod.Host() |
Colin Cross | f6566ed | 2015-03-24 11:13:38 -0700 | [diff] [blame] | 401 | } |
| 402 | |
| 403 | func (a *androidBaseContextImpl) Device() bool { |
Colin Cross | d3ba039 | 2015-05-07 14:11:29 -0700 | [diff] [blame] | 404 | return a.hod.Device() |
Colin Cross | f6566ed | 2015-03-24 11:13:38 -0700 | [diff] [blame] | 405 | } |
| 406 | |
Colin Cross | 0af4b84 | 2015-04-30 16:36:18 -0700 | [diff] [blame] | 407 | func (a *androidBaseContextImpl) Darwin() bool { |
Colin Cross | d3ba039 | 2015-05-07 14:11:29 -0700 | [diff] [blame] | 408 | return a.hod.Host() && runtime.GOOS == "darwin" |
Colin Cross | 0af4b84 | 2015-04-30 16:36:18 -0700 | [diff] [blame] | 409 | } |
| 410 | |
Colin Cross | f6566ed | 2015-03-24 11:13:38 -0700 | [diff] [blame] | 411 | func (a *androidBaseContextImpl) Debug() bool { |
| 412 | return a.debug |
| 413 | } |
| 414 | |
Colin Cross | 1332b00 | 2015-04-07 17:11:30 -0700 | [diff] [blame] | 415 | func (a *androidBaseContextImpl) AConfig() Config { |
| 416 | return a.config |
| 417 | } |
| 418 | |
Colin Cross | 35cec12 | 2015-04-02 14:37:16 -0700 | [diff] [blame] | 419 | func (a *androidModuleContext) InstallFileName(installPath, name, srcPath string, |
| 420 | deps ...string) string { |
| 421 | |
Colin Cross | 1332b00 | 2015-04-07 17:11:30 -0700 | [diff] [blame] | 422 | config := a.AConfig() |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 423 | var fullInstallPath string |
Colin Cross | d3ba039 | 2015-05-07 14:11:29 -0700 | [diff] [blame] | 424 | if a.hod.Device() { |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 425 | // TODO: replace unset with a device name once we have device targeting |
Colin Cross | 35cec12 | 2015-04-02 14:37:16 -0700 | [diff] [blame] | 426 | fullInstallPath = filepath.Join(config.DeviceOut(), "system", |
| 427 | installPath, name) |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 428 | } else { |
Colin Cross | 35cec12 | 2015-04-02 14:37:16 -0700 | [diff] [blame] | 429 | fullInstallPath = filepath.Join(config.HostOut(), installPath, name) |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 430 | } |
| 431 | |
Colin Cross | 35cec12 | 2015-04-02 14:37:16 -0700 | [diff] [blame] | 432 | deps = append(deps, a.installDeps...) |
| 433 | |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 434 | a.ModuleContext.Build(pctx, blueprint.BuildParams{ |
| 435 | Rule: Cp, |
| 436 | Outputs: []string{fullInstallPath}, |
| 437 | Inputs: []string{srcPath}, |
Colin Cross | 35cec12 | 2015-04-02 14:37:16 -0700 | [diff] [blame] | 438 | OrderOnly: deps, |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 439 | }) |
| 440 | |
| 441 | a.installFiles = append(a.installFiles, fullInstallPath) |
Colin Cross | 1f8c52b | 2015-06-16 16:38:17 -0700 | [diff] [blame] | 442 | a.checkbuildFiles = append(a.checkbuildFiles, srcPath) |
Colin Cross | 35cec12 | 2015-04-02 14:37:16 -0700 | [diff] [blame] | 443 | return fullInstallPath |
| 444 | } |
| 445 | |
| 446 | func (a *androidModuleContext) InstallFile(installPath, srcPath string, deps ...string) string { |
| 447 | return a.InstallFileName(installPath, filepath.Base(srcPath), srcPath, deps...) |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 448 | } |
| 449 | |
| 450 | func (a *androidModuleContext) CheckbuildFile(srcPath string) { |
| 451 | a.checkbuildFiles = append(a.checkbuildFiles, srcPath) |
| 452 | } |
| 453 | |
| 454 | type androidDynamicDependerContext struct { |
| 455 | blueprint.DynamicDependerModuleContext |
Colin Cross | f6566ed | 2015-03-24 11:13:38 -0700 | [diff] [blame] | 456 | androidBaseContextImpl |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 457 | } |
| 458 | |
| 459 | type fileInstaller interface { |
| 460 | filesToInstall() []string |
| 461 | } |
| 462 | |
| 463 | func isFileInstaller(m blueprint.Module) bool { |
| 464 | _, ok := m.(fileInstaller) |
| 465 | return ok |
| 466 | } |
| 467 | |
| 468 | func isAndroidModule(m blueprint.Module) bool { |
| 469 | _, ok := m.(AndroidModule) |
| 470 | return ok |
| 471 | } |
Colin Cross | fce5327 | 2015-04-08 11:21:40 -0700 | [diff] [blame] | 472 | |
Colin Cross | 8f101b4 | 2015-06-17 15:09:06 -0700 | [diff] [blame^] | 473 | func (ctx *androidModuleContext) ExpandSources(srcFiles []string) []string { |
Colin Cross | fce5327 | 2015-04-08 11:21:40 -0700 | [diff] [blame] | 474 | prefix := ModuleSrcDir(ctx) |
| 475 | for i, srcFile := range srcFiles { |
| 476 | if srcFile[0] == '-' { |
| 477 | srcFiles[i] = "-" + filepath.Join(prefix, srcFile[1:]) |
| 478 | } else { |
| 479 | srcFiles[i] = filepath.Join(prefix, srcFile) |
| 480 | } |
| 481 | } |
| 482 | |
Colin Cross | 8f101b4 | 2015-06-17 15:09:06 -0700 | [diff] [blame^] | 483 | if !hasGlob(srcFiles) { |
| 484 | return srcFiles |
| 485 | } |
| 486 | |
| 487 | var excludes []string |
| 488 | for _, s := range srcFiles { |
| 489 | if s[0] == '-' { |
| 490 | excludes = append(excludes, s[1:]) |
| 491 | } |
| 492 | } |
| 493 | |
| 494 | globbedSrcFiles := make([]string, 0, len(srcFiles)) |
| 495 | for _, s := range srcFiles { |
| 496 | if s[0] == '-' { |
| 497 | continue |
| 498 | } else if glob.IsGlob(s) { |
| 499 | globbedSrcFiles = append(globbedSrcFiles, ctx.Glob(s, excludes)...) |
| 500 | } else { |
| 501 | globbedSrcFiles = append(globbedSrcFiles, s) |
| 502 | } |
| 503 | } |
| 504 | |
| 505 | return globbedSrcFiles |
| 506 | } |
| 507 | |
| 508 | func (ctx *androidModuleContext) Glob(globPattern string, excludes []string) []string { |
| 509 | ret, err := Glob(ctx, ModuleOutDir(ctx), globPattern, excludes) |
| 510 | if err != nil { |
| 511 | ctx.ModuleErrorf("glob: %s", err.Error()) |
| 512 | } |
| 513 | return ret |
Colin Cross | fce5327 | 2015-04-08 11:21:40 -0700 | [diff] [blame] | 514 | } |
Colin Cross | 1f8c52b | 2015-06-16 16:38:17 -0700 | [diff] [blame] | 515 | |
| 516 | func BuildTargetSingleton() blueprint.Singleton { |
| 517 | return &buildTargetSingleton{} |
| 518 | } |
| 519 | |
| 520 | type buildTargetSingleton struct{} |
| 521 | |
| 522 | func (c *buildTargetSingleton) GenerateBuildActions(ctx blueprint.SingletonContext) { |
| 523 | checkbuildDeps := []string{} |
| 524 | |
| 525 | dirModules := make(map[string][]string) |
| 526 | |
| 527 | ctx.VisitAllModules(func(module blueprint.Module) { |
| 528 | if a, ok := module.(AndroidModule); ok { |
| 529 | blueprintDir := a.base().blueprintDir |
| 530 | installTarget := a.base().installTarget |
| 531 | checkbuildTarget := a.base().checkbuildTarget |
| 532 | |
| 533 | if checkbuildTarget != "" { |
| 534 | checkbuildDeps = append(checkbuildDeps, checkbuildTarget) |
| 535 | dirModules[blueprintDir] = append(dirModules[blueprintDir], checkbuildTarget) |
| 536 | } |
| 537 | |
| 538 | if installTarget != "" { |
| 539 | dirModules[blueprintDir] = append(dirModules[blueprintDir], installTarget) |
| 540 | } |
| 541 | } |
| 542 | }) |
| 543 | |
| 544 | // Create a top-level checkbuild target that depends on all modules |
| 545 | ctx.Build(pctx, blueprint.BuildParams{ |
| 546 | Rule: blueprint.Phony, |
| 547 | Outputs: []string{"checkbuild"}, |
| 548 | Implicits: checkbuildDeps, |
| 549 | // HACK: checkbuild should be an optional build, but force it enabled for now |
| 550 | //Optional: true, |
| 551 | }) |
| 552 | |
| 553 | // Create a mm/<directory> target that depends on all modules in a directory |
| 554 | dirs := sortedKeys(dirModules) |
| 555 | for _, dir := range dirs { |
| 556 | ctx.Build(pctx, blueprint.BuildParams{ |
| 557 | Rule: blueprint.Phony, |
| 558 | Outputs: []string{filepath.Join("mm", dir)}, |
| 559 | Implicits: dirModules[dir], |
| 560 | Optional: true, |
| 561 | }) |
| 562 | } |
| 563 | } |