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