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 | f6566ed | 2015-03-24 11:13:38 -0700 | [diff] [blame] | 19 | |
| 20 | "github.com/google/blueprint" |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 21 | ) |
| 22 | |
Colin Cross | 68f5510 | 2015-03-25 14:43:57 -0700 | [diff] [blame^] | 23 | type Config interface { |
| 24 | CpPreserveSymlinksFlags() string |
| 25 | SrcDir() string |
| 26 | Getenv(string) string |
| 27 | EnvDeps() map[string]string |
| 28 | } |
| 29 | |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 30 | var ( |
| 31 | DeviceSharedLibrary = "shared_library" |
| 32 | DeviceStaticLibrary = "static_library" |
| 33 | DeviceExecutable = "executable" |
| 34 | HostSharedLibrary = "host_shared_library" |
| 35 | HostStaticLibrary = "host_static_library" |
| 36 | HostExecutable = "host_executable" |
| 37 | ) |
| 38 | |
Colin Cross | f6566ed | 2015-03-24 11:13:38 -0700 | [diff] [blame] | 39 | type androidBaseContext interface { |
| 40 | Arch() Arch |
| 41 | Host() bool |
| 42 | Device() bool |
| 43 | Debug() bool |
| 44 | } |
| 45 | |
| 46 | type AndroidBaseContext interface { |
| 47 | blueprint.BaseModuleContext |
| 48 | androidBaseContext |
| 49 | } |
| 50 | |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 51 | type AndroidModuleContext interface { |
| 52 | blueprint.ModuleContext |
Colin Cross | f6566ed | 2015-03-24 11:13:38 -0700 | [diff] [blame] | 53 | androidBaseContext |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 54 | |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 55 | InstallFile(installPath, srcPath string) |
| 56 | CheckbuildFile(srcPath string) |
| 57 | } |
| 58 | |
| 59 | type AndroidModule interface { |
| 60 | blueprint.Module |
| 61 | |
| 62 | GenerateAndroidBuildActions(AndroidModuleContext) |
| 63 | |
| 64 | base() *AndroidModuleBase |
| 65 | Disabled() bool |
| 66 | HostOrDevice() HostOrDevice |
| 67 | } |
| 68 | |
| 69 | type AndroidDynamicDepender interface { |
| 70 | AndroidDynamicDependencies(ctx AndroidDynamicDependerModuleContext) []string |
| 71 | } |
| 72 | |
| 73 | type AndroidDynamicDependerModuleContext interface { |
| 74 | blueprint.DynamicDependerModuleContext |
Colin Cross | f6566ed | 2015-03-24 11:13:38 -0700 | [diff] [blame] | 75 | androidBaseContext |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | type commonProperties struct { |
| 79 | Name string |
| 80 | Deps []string |
| 81 | ResourceDirs []string |
| 82 | |
| 83 | // disabled: don't emit any build rules for this module |
| 84 | Disabled bool `android:"arch_variant"` |
| 85 | |
| 86 | // multilib: control whether this module compiles for 32-bit, 64-bit, or both. Possible values |
| 87 | // are "32" (compile for 32-bit only), "64" (compile for 64-bit only), "both" (compile for both |
| 88 | // architectures), or "first" (compile for 64-bit on a 64-bit platform, and 32-bit on a 32-bit |
| 89 | // platform |
| 90 | Compile_multilib string |
| 91 | |
| 92 | // Set by ArchMutator |
| 93 | CompileArch Arch `blueprint:"mutated"` |
| 94 | |
| 95 | // Set by InitAndroidModule |
| 96 | HostOrDeviceSupported HostOrDeviceSupported `blueprint:"mutated"` |
| 97 | } |
| 98 | |
| 99 | type hostAndDeviceProperties struct { |
| 100 | Host_supported bool |
| 101 | Device_supported bool |
| 102 | } |
| 103 | |
Colin Cross | c472d57 | 2015-03-17 15:06:21 -0700 | [diff] [blame] | 104 | type Multilib string |
| 105 | |
| 106 | const ( |
| 107 | MultilibBoth Multilib = "both" |
| 108 | MultilibFirst Multilib = "first" |
| 109 | ) |
| 110 | |
Colin Cross | 5049f02 | 2015-03-18 13:28:46 -0700 | [diff] [blame] | 111 | func InitAndroidModule(m AndroidModule, |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 112 | propertyStructs ...interface{}) (blueprint.Module, []interface{}) { |
| 113 | |
| 114 | base := m.base() |
| 115 | base.module = m |
Colin Cross | 5049f02 | 2015-03-18 13:28:46 -0700 | [diff] [blame] | 116 | |
| 117 | propertyStructs = append(propertyStructs, &base.commonProperties) |
| 118 | |
| 119 | return m, propertyStructs |
| 120 | } |
| 121 | |
| 122 | func InitAndroidArchModule(m AndroidModule, hod HostOrDeviceSupported, defaultMultilib Multilib, |
| 123 | propertyStructs ...interface{}) (blueprint.Module, []interface{}) { |
| 124 | |
| 125 | _, propertyStructs = InitAndroidModule(m, propertyStructs...) |
| 126 | |
| 127 | base := m.base() |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 128 | base.commonProperties.HostOrDeviceSupported = hod |
| 129 | |
| 130 | if hod == HostAndDeviceSupported { |
| 131 | // Default to module to device supported, host not supported, can override in module |
| 132 | // properties |
| 133 | base.hostAndDeviceProperties.Device_supported = true |
| 134 | propertyStructs = append(propertyStructs, &base.hostAndDeviceProperties) |
| 135 | } |
| 136 | |
| 137 | return InitArchModule(m, defaultMultilib, propertyStructs...) |
| 138 | } |
| 139 | |
| 140 | // A AndroidModuleBase object contains the properties that are common to all Android |
| 141 | // modules. It should be included as an anonymous field in every module |
| 142 | // struct definition. InitAndroidModule should then be called from the module's |
| 143 | // factory function, and the return values from InitAndroidModule should be |
| 144 | // returned from the factory function. |
| 145 | // |
| 146 | // The AndroidModuleBase type is responsible for implementing the |
| 147 | // GenerateBuildActions method to support the blueprint.Module interface. This |
| 148 | // method will then call the module's GenerateAndroidBuildActions method once |
| 149 | // for each build variant that is to be built. GenerateAndroidBuildActions is |
| 150 | // passed a AndroidModuleContext rather than the usual blueprint.ModuleContext. |
| 151 | // AndroidModuleContext exposes extra functionality specific to the Android build |
| 152 | // system including details about the particular build variant that is to be |
| 153 | // generated. |
| 154 | // |
| 155 | // For example: |
| 156 | // |
| 157 | // import ( |
| 158 | // "android/soong/common" |
Colin Cross | 70b4059 | 2015-03-23 12:57:34 -0700 | [diff] [blame] | 159 | // "github.com/google/blueprint" |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 160 | // ) |
| 161 | // |
| 162 | // type myModule struct { |
| 163 | // common.AndroidModuleBase |
| 164 | // properties struct { |
| 165 | // MyProperty string |
| 166 | // } |
| 167 | // } |
| 168 | // |
| 169 | // func NewMyModule() (blueprint.Module, []interface{}) { |
| 170 | // m := &myModule{} |
| 171 | // return common.InitAndroidModule(m, &m.properties) |
| 172 | // } |
| 173 | // |
| 174 | // func (m *myModule) GenerateAndroidBuildActions(ctx common.AndroidModuleContext) { |
| 175 | // // Get the CPU architecture for the current build variant. |
| 176 | // variantArch := ctx.Arch() |
| 177 | // |
| 178 | // // ... |
| 179 | // } |
| 180 | type AndroidModuleBase struct { |
| 181 | // Putting the curiously recurring thing pointing to the thing that contains |
| 182 | // the thing pattern to good use. |
| 183 | module AndroidModule |
| 184 | |
| 185 | commonProperties commonProperties |
| 186 | hostAndDeviceProperties hostAndDeviceProperties |
| 187 | generalProperties []interface{} |
| 188 | archProperties []*archProperties |
| 189 | |
| 190 | noAddressSanitizer bool |
| 191 | installFiles []string |
| 192 | checkbuildFiles []string |
| 193 | } |
| 194 | |
| 195 | func (a *AndroidModuleBase) base() *AndroidModuleBase { |
| 196 | return a |
| 197 | } |
| 198 | |
| 199 | func (a *AndroidModuleBase) SetArch(arch Arch) { |
| 200 | a.commonProperties.CompileArch = arch |
| 201 | } |
| 202 | |
| 203 | func (a *AndroidModuleBase) HostOrDevice() HostOrDevice { |
| 204 | return a.commonProperties.CompileArch.HostOrDevice |
| 205 | } |
| 206 | |
| 207 | func (a *AndroidModuleBase) HostSupported() bool { |
| 208 | return a.commonProperties.HostOrDeviceSupported == HostSupported || |
| 209 | a.commonProperties.HostOrDeviceSupported == HostAndDeviceSupported && |
| 210 | a.hostAndDeviceProperties.Host_supported |
| 211 | } |
| 212 | |
| 213 | func (a *AndroidModuleBase) DeviceSupported() bool { |
| 214 | return a.commonProperties.HostOrDeviceSupported == DeviceSupported || |
| 215 | a.commonProperties.HostOrDeviceSupported == HostAndDeviceSupported && |
| 216 | a.hostAndDeviceProperties.Device_supported |
| 217 | } |
| 218 | |
| 219 | func (a *AndroidModuleBase) Disabled() bool { |
| 220 | return a.commonProperties.Disabled |
| 221 | } |
| 222 | |
| 223 | func (a *AndroidModuleBase) computeInstallDeps( |
| 224 | ctx blueprint.ModuleContext) []string { |
| 225 | |
| 226 | result := []string{} |
| 227 | ctx.VisitDepsDepthFirstIf(isFileInstaller, |
| 228 | func(m blueprint.Module) { |
| 229 | fileInstaller := m.(fileInstaller) |
| 230 | files := fileInstaller.filesToInstall() |
| 231 | result = append(result, files...) |
| 232 | }) |
| 233 | |
| 234 | return result |
| 235 | } |
| 236 | |
| 237 | func (a *AndroidModuleBase) filesToInstall() []string { |
| 238 | return a.installFiles |
| 239 | } |
| 240 | |
| 241 | func (p *AndroidModuleBase) NoAddressSanitizer() bool { |
| 242 | return p.noAddressSanitizer |
| 243 | } |
| 244 | |
| 245 | func (p *AndroidModuleBase) resourceDirs() []string { |
| 246 | return p.commonProperties.ResourceDirs |
| 247 | } |
| 248 | |
| 249 | func (a *AndroidModuleBase) generateModuleTarget(ctx blueprint.ModuleContext) { |
| 250 | if a != ctx.FinalModule().(AndroidModule).base() { |
| 251 | return |
| 252 | } |
| 253 | |
| 254 | allInstalledFiles := []string{} |
Colin Cross | 9454bfa | 2015-03-17 13:24:18 -0700 | [diff] [blame] | 255 | allCheckbuildFiles := []string{} |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 256 | ctx.VisitAllModuleVariants(func(module blueprint.Module) { |
| 257 | if androidModule, ok := module.(AndroidModule); ok { |
| 258 | files := androidModule.base().installFiles |
| 259 | allInstalledFiles = append(allInstalledFiles, files...) |
Colin Cross | 9454bfa | 2015-03-17 13:24:18 -0700 | [diff] [blame] | 260 | files = androidModule.base().checkbuildFiles |
| 261 | allCheckbuildFiles = append(allCheckbuildFiles, files...) |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 262 | } |
| 263 | }) |
| 264 | |
Colin Cross | 9454bfa | 2015-03-17 13:24:18 -0700 | [diff] [blame] | 265 | deps := []string{} |
| 266 | |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 267 | if len(allInstalledFiles) > 0 { |
Colin Cross | 9454bfa | 2015-03-17 13:24:18 -0700 | [diff] [blame] | 268 | name := ctx.ModuleName() + "-install" |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 269 | ctx.Build(pctx, blueprint.BuildParams{ |
Colin Cross | 9454bfa | 2015-03-17 13:24:18 -0700 | [diff] [blame] | 270 | Rule: blueprint.Phony, |
| 271 | Outputs: []string{name}, |
| 272 | Implicits: allInstalledFiles, |
| 273 | }) |
| 274 | deps = append(deps, name) |
| 275 | } |
| 276 | |
| 277 | if len(allCheckbuildFiles) > 0 { |
| 278 | name := ctx.ModuleName() + "-checkbuild" |
| 279 | ctx.Build(pctx, blueprint.BuildParams{ |
| 280 | Rule: blueprint.Phony, |
| 281 | Outputs: []string{name}, |
| 282 | Implicits: allCheckbuildFiles, |
| 283 | Optional: true, |
| 284 | }) |
| 285 | deps = append(deps, name) |
| 286 | } |
| 287 | |
| 288 | if len(deps) > 0 { |
| 289 | ctx.Build(pctx, blueprint.BuildParams{ |
| 290 | Rule: blueprint.Phony, |
| 291 | Outputs: []string{ctx.ModuleName()}, |
| 292 | Implicits: deps, |
| 293 | Optional: true, |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 294 | }) |
| 295 | } |
| 296 | } |
| 297 | |
| 298 | func (a *AndroidModuleBase) DynamicDependencies(ctx blueprint.DynamicDependerModuleContext) []string { |
| 299 | actx := &androidDynamicDependerContext{ |
| 300 | DynamicDependerModuleContext: ctx, |
Colin Cross | f6566ed | 2015-03-24 11:13:38 -0700 | [diff] [blame] | 301 | androidBaseContextImpl: androidBaseContextImpl{ |
| 302 | arch: a.commonProperties.CompileArch, |
| 303 | }, |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 304 | } |
| 305 | |
| 306 | if dynamic, ok := a.module.(AndroidDynamicDepender); ok { |
| 307 | return dynamic.AndroidDynamicDependencies(actx) |
| 308 | } |
| 309 | |
| 310 | return nil |
| 311 | } |
| 312 | |
| 313 | func (a *AndroidModuleBase) GenerateBuildActions(ctx blueprint.ModuleContext) { |
| 314 | androidCtx := &androidModuleContext{ |
| 315 | ModuleContext: ctx, |
Colin Cross | f6566ed | 2015-03-24 11:13:38 -0700 | [diff] [blame] | 316 | androidBaseContextImpl: androidBaseContextImpl{ |
| 317 | arch: a.commonProperties.CompileArch, |
| 318 | }, |
| 319 | installDeps: a.computeInstallDeps(ctx), |
| 320 | installFiles: a.installFiles, |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 321 | } |
| 322 | |
| 323 | if a.commonProperties.Disabled { |
| 324 | return |
| 325 | } |
| 326 | |
| 327 | a.module.GenerateAndroidBuildActions(androidCtx) |
| 328 | if ctx.Failed() { |
| 329 | return |
| 330 | } |
| 331 | |
| 332 | a.generateModuleTarget(ctx) |
| 333 | if ctx.Failed() { |
| 334 | return |
| 335 | } |
Colin Cross | 9454bfa | 2015-03-17 13:24:18 -0700 | [diff] [blame] | 336 | |
| 337 | a.installFiles = append(a.installFiles, androidCtx.installFiles...) |
| 338 | a.checkbuildFiles = append(a.checkbuildFiles, androidCtx.checkbuildFiles...) |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 339 | } |
| 340 | |
Colin Cross | f6566ed | 2015-03-24 11:13:38 -0700 | [diff] [blame] | 341 | type androidBaseContextImpl struct { |
| 342 | arch Arch |
| 343 | debug bool |
| 344 | } |
| 345 | |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 346 | type androidModuleContext struct { |
| 347 | blueprint.ModuleContext |
Colin Cross | f6566ed | 2015-03-24 11:13:38 -0700 | [diff] [blame] | 348 | androidBaseContextImpl |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 349 | installDeps []string |
| 350 | installFiles []string |
| 351 | checkbuildFiles []string |
| 352 | } |
| 353 | |
| 354 | func (a *androidModuleContext) Build(pctx *blueprint.PackageContext, params blueprint.BuildParams) { |
| 355 | params.Optional = true |
| 356 | a.ModuleContext.Build(pctx, params) |
| 357 | } |
| 358 | |
Colin Cross | f6566ed | 2015-03-24 11:13:38 -0700 | [diff] [blame] | 359 | func (a *androidBaseContextImpl) Arch() Arch { |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 360 | return a.arch |
| 361 | } |
| 362 | |
Colin Cross | f6566ed | 2015-03-24 11:13:38 -0700 | [diff] [blame] | 363 | func (a *androidBaseContextImpl) Host() bool { |
| 364 | return a.arch.HostOrDevice.Host() |
| 365 | } |
| 366 | |
| 367 | func (a *androidBaseContextImpl) Device() bool { |
| 368 | return a.arch.HostOrDevice.Device() |
| 369 | } |
| 370 | |
| 371 | func (a *androidBaseContextImpl) Debug() bool { |
| 372 | return a.debug |
| 373 | } |
| 374 | |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 375 | func (a *androidModuleContext) InstallFile(installPath, srcPath string) { |
| 376 | var fullInstallPath string |
| 377 | if a.arch.HostOrDevice.Device() { |
| 378 | // TODO: replace unset with a device name once we have device targeting |
| 379 | fullInstallPath = filepath.Join("out/target/product/unset/system", installPath, |
| 380 | filepath.Base(srcPath)) |
| 381 | } else { |
| 382 | // TODO: replace unset with a host name |
| 383 | fullInstallPath = filepath.Join("out/host/unset/", installPath, filepath.Base(srcPath)) |
| 384 | } |
| 385 | |
| 386 | a.ModuleContext.Build(pctx, blueprint.BuildParams{ |
| 387 | Rule: Cp, |
| 388 | Outputs: []string{fullInstallPath}, |
| 389 | Inputs: []string{srcPath}, |
| 390 | OrderOnly: a.installDeps, |
| 391 | }) |
| 392 | |
| 393 | a.installFiles = append(a.installFiles, fullInstallPath) |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 394 | } |
| 395 | |
| 396 | func (a *androidModuleContext) CheckbuildFile(srcPath string) { |
| 397 | a.checkbuildFiles = append(a.checkbuildFiles, srcPath) |
| 398 | } |
| 399 | |
| 400 | type androidDynamicDependerContext struct { |
| 401 | blueprint.DynamicDependerModuleContext |
Colin Cross | f6566ed | 2015-03-24 11:13:38 -0700 | [diff] [blame] | 402 | androidBaseContextImpl |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 403 | } |
| 404 | |
| 405 | type fileInstaller interface { |
| 406 | filesToInstall() []string |
| 407 | } |
| 408 | |
| 409 | func isFileInstaller(m blueprint.Module) bool { |
| 410 | _, ok := m.(fileInstaller) |
| 411 | return ok |
| 412 | } |
| 413 | |
| 414 | func isAndroidModule(m blueprint.Module) bool { |
| 415 | _, ok := m.(AndroidModule) |
| 416 | return ok |
| 417 | } |