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 | |
| 24 | func SourceModuleHasPrebuilt(ctx ModuleContext) OptionalPath { |
| 25 | var path Path |
| 26 | ctx.VisitDirectDeps(func(m blueprint.Module) { |
| 27 | if ctx.OtherModuleDependencyTag(m) == prebuiltDependencyTag { |
| 28 | p := m.(PrebuiltInterface).Prebuilt() |
| 29 | if p.usePrebuilt(ctx) { |
| 30 | path = p.Path(ctx) |
| 31 | } |
| 32 | } |
| 33 | }) |
| 34 | |
| 35 | return OptionalPathForPath(path) |
| 36 | } |
| 37 | |
| 38 | type Prebuilt struct { |
| 39 | Properties struct { |
| 40 | Srcs []string `android:"arch_variant"` |
| 41 | // When prefer is set to true the prebuilt will be used instead of any source module with |
| 42 | // a matching name. |
| 43 | Prefer bool `android:"arch_variant"` |
| 44 | |
| 45 | SourceExists bool `blueprint:"mutated"` |
| 46 | } |
| 47 | module Module |
| 48 | } |
| 49 | |
| 50 | func (p *Prebuilt) Name(name string) string { |
| 51 | return "prebuilt_" + name |
| 52 | } |
| 53 | |
| 54 | func (p *Prebuilt) Path(ctx ModuleContext) Path { |
| 55 | if len(p.Properties.Srcs) == 0 { |
| 56 | ctx.PropertyErrorf("srcs", "missing prebuilt source file") |
| 57 | return nil |
| 58 | } |
| 59 | |
| 60 | if len(p.Properties.Srcs) > 1 { |
| 61 | ctx.PropertyErrorf("srcs", "multiple prebuilt source files") |
| 62 | return nil |
| 63 | } |
| 64 | |
| 65 | return PathForModuleSrc(ctx, p.Properties.Srcs[0]) |
| 66 | } |
| 67 | |
| 68 | type PrebuiltInterface interface { |
| 69 | Module |
| 70 | Prebuilt() *Prebuilt |
| 71 | } |
| 72 | |
| 73 | type PrebuiltSourceInterface interface { |
| 74 | SkipInstall() |
| 75 | } |
| 76 | |
| 77 | // prebuiltMutator ensures that there is always a module with an undecorated name, and marks |
| 78 | // prebuilt modules that have both a prebuilt and a source module. |
| 79 | func prebuiltMutator(ctx BottomUpMutatorContext) { |
| 80 | if m, ok := ctx.Module().(PrebuiltInterface); ok && m.Prebuilt() != nil { |
| 81 | p := m.Prebuilt() |
| 82 | name := m.base().BaseModuleName() |
| 83 | if ctx.OtherModuleExists(name) { |
| 84 | ctx.AddReverseDependency(ctx.Module(), prebuiltDependencyTag, name) |
| 85 | p.Properties.SourceExists = true |
| 86 | } else { |
| 87 | ctx.Rename(name) |
| 88 | } |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | // PrebuiltReplaceMutator replaces dependencies on the source module with dependencies on the prebuilt |
| 93 | // when both modules exist and the prebuilt should be used. |
| 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() |
| 98 | if p.Properties.SourceExists && p.usePrebuilt(ctx) { |
| 99 | ctx.ReplaceDependencies(name) |
| 100 | } |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | // PrebuiltDisableMutator disables source modules that have prebuilts that should be used instead. |
| 105 | func PrebuiltDisableMutator(ctx TopDownMutatorContext) { |
| 106 | if s, ok := ctx.Module().(PrebuiltSourceInterface); ok { |
| 107 | ctx.VisitDirectDeps(func(m blueprint.Module) { |
| 108 | if ctx.OtherModuleDependencyTag(m) == prebuiltDependencyTag { |
| 109 | p := m.(PrebuiltInterface).Prebuilt() |
| 110 | if p.usePrebuilt(ctx) { |
| 111 | s.SkipInstall() |
| 112 | } |
| 113 | } |
| 114 | }) |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | func (p *Prebuilt) usePrebuilt(ctx BaseContext) bool { |
| 119 | // TODO: use p.Properties.Name and ctx.ModuleDir to override prefer |
| 120 | return p.Properties.Prefer && len(p.Properties.Srcs) > 0 |
| 121 | } |