Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 1 | // Copyright 2016 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 android |
| 16 | |
| 17 | import "github.com/google/blueprint" |
| 18 | |
| 19 | // This file implements common functionality for handling modules that may exist as prebuilts, |
| 20 | // source, or both. |
| 21 | |
| 22 | var prebuiltDependencyTag blueprint.BaseDependencyTag |
| 23 | |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 24 | type Prebuilt struct { |
| 25 | Properties struct { |
| 26 | Srcs []string `android:"arch_variant"` |
| 27 | // When prefer is set to true the prebuilt will be used instead of any source module with |
| 28 | // a matching name. |
| 29 | Prefer bool `android:"arch_variant"` |
| 30 | |
| 31 | SourceExists bool `blueprint:"mutated"` |
Colin Cross | a2f296f | 2016-11-29 15:16:18 -0800 | [diff] [blame^] | 32 | UsePrebuilt bool `blueprint:"mutated"` |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 33 | } |
| 34 | module Module |
| 35 | } |
| 36 | |
| 37 | func (p *Prebuilt) Name(name string) string { |
| 38 | return "prebuilt_" + name |
| 39 | } |
| 40 | |
| 41 | func (p *Prebuilt) Path(ctx ModuleContext) Path { |
| 42 | if len(p.Properties.Srcs) == 0 { |
| 43 | ctx.PropertyErrorf("srcs", "missing prebuilt source file") |
| 44 | return nil |
| 45 | } |
| 46 | |
| 47 | if len(p.Properties.Srcs) > 1 { |
| 48 | ctx.PropertyErrorf("srcs", "multiple prebuilt source files") |
| 49 | return nil |
| 50 | } |
| 51 | |
| 52 | return PathForModuleSrc(ctx, p.Properties.Srcs[0]) |
| 53 | } |
| 54 | |
| 55 | type PrebuiltInterface interface { |
| 56 | Module |
| 57 | Prebuilt() *Prebuilt |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | // prebuiltMutator ensures that there is always a module with an undecorated name, and marks |
| 61 | // prebuilt modules that have both a prebuilt and a source module. |
| 62 | func prebuiltMutator(ctx BottomUpMutatorContext) { |
| 63 | if m, ok := ctx.Module().(PrebuiltInterface); ok && m.Prebuilt() != nil { |
| 64 | p := m.Prebuilt() |
| 65 | name := m.base().BaseModuleName() |
| 66 | if ctx.OtherModuleExists(name) { |
| 67 | ctx.AddReverseDependency(ctx.Module(), prebuiltDependencyTag, name) |
| 68 | p.Properties.SourceExists = true |
| 69 | } else { |
| 70 | ctx.Rename(name) |
| 71 | } |
| 72 | } |
| 73 | } |
| 74 | |
Colin Cross | a2f296f | 2016-11-29 15:16:18 -0800 | [diff] [blame^] | 75 | // PrebuiltSelectModuleMutator marks prebuilts that are overriding source modules, and disables |
| 76 | // installing the source module. |
| 77 | func PrebuiltSelectModuleMutator(ctx TopDownMutatorContext) { |
| 78 | if s, ok := ctx.Module().(Module); ok { |
| 79 | ctx.VisitDirectDeps(func(m blueprint.Module) { |
| 80 | if ctx.OtherModuleDependencyTag(m) == prebuiltDependencyTag { |
| 81 | p := m.(PrebuiltInterface).Prebuilt() |
| 82 | if p.usePrebuilt(ctx, s) { |
| 83 | p.Properties.UsePrebuilt = true |
| 84 | s.SkipInstall() |
| 85 | } |
| 86 | } |
| 87 | }) |
| 88 | } |
| 89 | } |
| 90 | |
Colin Cross | 0f3c72f | 2016-11-23 15:44:07 -0800 | [diff] [blame] | 91 | // PrebuiltReplaceMutator replaces dependencies on the source module with dependencies on the |
| 92 | // prebuilt when both modules exist and the prebuilt should be used. When the prebuilt should not |
| 93 | // be used, disable installing it. |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 94 | func PrebuiltReplaceMutator(ctx BottomUpMutatorContext) { |
| 95 | if m, ok := ctx.Module().(PrebuiltInterface); ok && m.Prebuilt() != nil { |
| 96 | p := m.Prebuilt() |
| 97 | name := m.base().BaseModuleName() |
Colin Cross | a2f296f | 2016-11-29 15:16:18 -0800 | [diff] [blame^] | 98 | if p.Properties.UsePrebuilt { |
Colin Cross | 0f3c72f | 2016-11-23 15:44:07 -0800 | [diff] [blame] | 99 | if p.Properties.SourceExists { |
| 100 | ctx.ReplaceDependencies(name) |
| 101 | } |
| 102 | } else { |
| 103 | m.SkipInstall() |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 104 | } |
| 105 | } |
| 106 | } |
| 107 | |
Colin Cross | a2f296f | 2016-11-29 15:16:18 -0800 | [diff] [blame^] | 108 | // usePrebuilt returns true if a prebuilt should be used instead of the source module. The prebuilt |
| 109 | // will be used if it is marked "prefer" or if the source module is disabled. |
| 110 | func (p *Prebuilt) usePrebuilt(ctx TopDownMutatorContext, source Module) bool { |
| 111 | if len(p.Properties.Srcs) == 0 { |
| 112 | return false |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 113 | } |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 114 | |
Colin Cross | a2f296f | 2016-11-29 15:16:18 -0800 | [diff] [blame^] | 115 | // TODO: use p.Properties.Name and ctx.ModuleDir to override preference |
| 116 | if p.Properties.Prefer { |
| 117 | return true |
| 118 | } |
| 119 | |
| 120 | return !source.Enabled() |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 121 | } |