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