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