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