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