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