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