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 | |
Colin Cross | 74d73e2 | 2017-08-02 11:05:49 -0700 | [diff] [blame] | 17 | import ( |
| 18 | "fmt" |
| 19 | |
| 20 | "github.com/google/blueprint" |
| 21 | ) |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 22 | |
| 23 | // This file implements common functionality for handling modules that may exist as prebuilts, |
| 24 | // source, or both. |
| 25 | |
Nan Zhang | 2502e12 | 2017-03-09 18:43:01 -0800 | [diff] [blame] | 26 | type prebuiltDependencyTag struct { |
| 27 | blueprint.BaseDependencyTag |
| 28 | } |
| 29 | |
| 30 | var prebuiltDepTag prebuiltDependencyTag |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 31 | |
Colin Cross | 74d73e2 | 2017-08-02 11:05:49 -0700 | [diff] [blame] | 32 | type PrebuiltProperties struct { |
| 33 | // When prefer is set to true the prebuilt will be used instead of any source module with |
| 34 | // a matching name. |
Nan Zhang | 0007d81 | 2017-11-07 10:57:05 -0800 | [diff] [blame] | 35 | Prefer *bool `android:"arch_variant"` |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 36 | |
Colin Cross | 74d73e2 | 2017-08-02 11:05:49 -0700 | [diff] [blame] | 37 | SourceExists bool `blueprint:"mutated"` |
| 38 | UsePrebuilt bool `blueprint:"mutated"` |
| 39 | } |
| 40 | |
| 41 | type Prebuilt struct { |
| 42 | properties PrebuiltProperties |
| 43 | module Module |
| 44 | srcs *[]string |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | func (p *Prebuilt) Name(name string) string { |
| 48 | return "prebuilt_" + name |
| 49 | } |
| 50 | |
Colin Cross | 74d73e2 | 2017-08-02 11:05:49 -0700 | [diff] [blame] | 51 | func (p *Prebuilt) SingleSourcePath(ctx ModuleContext) Path { |
| 52 | if len(*p.srcs) == 0 { |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 53 | ctx.PropertyErrorf("srcs", "missing prebuilt source file") |
| 54 | return nil |
| 55 | } |
| 56 | |
Colin Cross | 74d73e2 | 2017-08-02 11:05:49 -0700 | [diff] [blame] | 57 | if len(*p.srcs) > 1 { |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 58 | ctx.PropertyErrorf("srcs", "multiple prebuilt source files") |
| 59 | return nil |
| 60 | } |
| 61 | |
Pirama Arumuga Nainar | 955dc49 | 2018-04-17 14:58:42 -0700 | [diff] [blame^] | 62 | // Return the singleton source after expanding any filegroup in the |
| 63 | // sources. |
| 64 | return ctx.ExpandSource((*p.srcs)[0], "") |
Colin Cross | 74d73e2 | 2017-08-02 11:05:49 -0700 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | func InitPrebuiltModule(module PrebuiltInterface, srcs *[]string) { |
| 68 | p := module.Prebuilt() |
| 69 | module.AddProperties(&p.properties) |
| 70 | p.srcs = srcs |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | type PrebuiltInterface interface { |
| 74 | Module |
| 75 | Prebuilt() *Prebuilt |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 76 | } |
| 77 | |
Colin Cross | 5ea9bcc | 2017-07-27 15:41:32 -0700 | [diff] [blame] | 78 | func RegisterPrebuiltsPreArchMutators(ctx RegisterMutatorsContext) { |
Colin Cross | cec8171 | 2017-07-13 14:43:27 -0700 | [diff] [blame] | 79 | ctx.BottomUp("prebuilts", prebuiltMutator).Parallel() |
| 80 | } |
| 81 | |
Colin Cross | 5ea9bcc | 2017-07-27 15:41:32 -0700 | [diff] [blame] | 82 | func RegisterPrebuiltsPostDepsMutators(ctx RegisterMutatorsContext) { |
Colin Cross | cec8171 | 2017-07-13 14:43:27 -0700 | [diff] [blame] | 83 | ctx.TopDown("prebuilt_select", PrebuiltSelectModuleMutator).Parallel() |
Pirama Arumuga Nainar | 955dc49 | 2018-04-17 14:58:42 -0700 | [diff] [blame^] | 84 | ctx.BottomUp("prebuilt_postdeps", PrebuiltPostDepsMutator).Parallel() |
Colin Cross | cec8171 | 2017-07-13 14:43:27 -0700 | [diff] [blame] | 85 | } |
| 86 | |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 87 | // prebuiltMutator ensures that there is always a module with an undecorated name, and marks |
| 88 | // prebuilt modules that have both a prebuilt and a source module. |
| 89 | func prebuiltMutator(ctx BottomUpMutatorContext) { |
| 90 | if m, ok := ctx.Module().(PrebuiltInterface); ok && m.Prebuilt() != nil { |
| 91 | p := m.Prebuilt() |
| 92 | name := m.base().BaseModuleName() |
| 93 | if ctx.OtherModuleExists(name) { |
Nan Zhang | 2502e12 | 2017-03-09 18:43:01 -0800 | [diff] [blame] | 94 | ctx.AddReverseDependency(ctx.Module(), prebuiltDepTag, name) |
Colin Cross | 74d73e2 | 2017-08-02 11:05:49 -0700 | [diff] [blame] | 95 | p.properties.SourceExists = true |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 96 | } else { |
| 97 | ctx.Rename(name) |
| 98 | } |
| 99 | } |
| 100 | } |
| 101 | |
Colin Cross | c3e7fa6 | 2017-03-17 13:14:32 -0700 | [diff] [blame] | 102 | // PrebuiltSelectModuleMutator marks prebuilts that are used, either overriding source modules or |
| 103 | // 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] | 104 | func PrebuiltSelectModuleMutator(ctx TopDownMutatorContext) { |
Colin Cross | c3e7fa6 | 2017-03-17 13:14:32 -0700 | [diff] [blame] | 105 | if m, ok := ctx.Module().(PrebuiltInterface); ok && m.Prebuilt() != nil { |
| 106 | p := m.Prebuilt() |
Colin Cross | 74d73e2 | 2017-08-02 11:05:49 -0700 | [diff] [blame] | 107 | if p.srcs == nil { |
| 108 | panic(fmt.Errorf("prebuilt module did not have InitPrebuiltModule called on it")) |
| 109 | } |
| 110 | if !p.properties.SourceExists { |
| 111 | p.properties.UsePrebuilt = p.usePrebuilt(ctx, nil) |
Colin Cross | c3e7fa6 | 2017-03-17 13:14:32 -0700 | [diff] [blame] | 112 | } |
| 113 | } else if s, ok := ctx.Module().(Module); ok { |
Colin Cross | ee6143c | 2017-12-30 17:54:27 -0800 | [diff] [blame] | 114 | ctx.VisitDirectDepsWithTag(prebuiltDepTag, func(m Module) { |
| 115 | p := m.(PrebuiltInterface).Prebuilt() |
| 116 | if p.usePrebuilt(ctx, s) { |
| 117 | p.properties.UsePrebuilt = true |
| 118 | s.SkipInstall() |
Colin Cross | a2f296f | 2016-11-29 15:16:18 -0800 | [diff] [blame] | 119 | } |
| 120 | }) |
| 121 | } |
| 122 | } |
| 123 | |
Pirama Arumuga Nainar | 955dc49 | 2018-04-17 14:58:42 -0700 | [diff] [blame^] | 124 | // PrebuiltPostDepsMutator does two operations. It replace dependencies on the |
| 125 | // source module with dependencies on the prebuilt when both modules exist and |
| 126 | // the prebuilt should be used. When the prebuilt should not be used, disable |
| 127 | // installing it. Secondly, it also adds a sourcegroup to any filegroups found |
| 128 | // in the prebuilt's 'Srcs' property. |
| 129 | func PrebuiltPostDepsMutator(ctx BottomUpMutatorContext) { |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 130 | if m, ok := ctx.Module().(PrebuiltInterface); ok && m.Prebuilt() != nil { |
| 131 | p := m.Prebuilt() |
| 132 | name := m.base().BaseModuleName() |
Colin Cross | 74d73e2 | 2017-08-02 11:05:49 -0700 | [diff] [blame] | 133 | if p.properties.UsePrebuilt { |
| 134 | if p.properties.SourceExists { |
Colin Cross | 0f3c72f | 2016-11-23 15:44:07 -0800 | [diff] [blame] | 135 | ctx.ReplaceDependencies(name) |
| 136 | } |
| 137 | } else { |
| 138 | m.SkipInstall() |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 139 | } |
Pirama Arumuga Nainar | 955dc49 | 2018-04-17 14:58:42 -0700 | [diff] [blame^] | 140 | if len(*p.srcs) > 0 { |
| 141 | ExtractSourceDeps(ctx, &(*p.srcs)[0]) |
| 142 | } |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 143 | } |
| 144 | } |
| 145 | |
Colin Cross | a2f296f | 2016-11-29 15:16:18 -0800 | [diff] [blame] | 146 | // usePrebuilt returns true if a prebuilt should be used instead of the source module. The prebuilt |
| 147 | // will be used if it is marked "prefer" or if the source module is disabled. |
| 148 | func (p *Prebuilt) usePrebuilt(ctx TopDownMutatorContext, source Module) bool { |
Colin Cross | 74d73e2 | 2017-08-02 11:05:49 -0700 | [diff] [blame] | 149 | if len(*p.srcs) == 0 { |
Colin Cross | a2f296f | 2016-11-29 15:16:18 -0800 | [diff] [blame] | 150 | return false |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 151 | } |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 152 | |
Colin Cross | a2f296f | 2016-11-29 15:16:18 -0800 | [diff] [blame] | 153 | // TODO: use p.Properties.Name and ctx.ModuleDir to override preference |
Nan Zhang | 0007d81 | 2017-11-07 10:57:05 -0800 | [diff] [blame] | 154 | if Bool(p.properties.Prefer) { |
Colin Cross | a2f296f | 2016-11-29 15:16:18 -0800 | [diff] [blame] | 155 | return true |
| 156 | } |
| 157 | |
Colin Cross | c3e7fa6 | 2017-03-17 13:14:32 -0700 | [diff] [blame] | 158 | return source == nil || !source.Enabled() |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 159 | } |