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