blob: 006b66f157d9c7ed35aae86e3f9c45f411c99f11 [file] [log] [blame]
Colin Crossce75d2c2016-10-06 16:12:58 -07001// 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
15package android
16
17import "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 Zhang2502e122017-03-09 18:43:01 -080022type prebuiltDependencyTag struct {
23 blueprint.BaseDependencyTag
24}
25
26var prebuiltDepTag prebuiltDependencyTag
Colin Crossce75d2c2016-10-06 16:12:58 -070027
Colin Crossce75d2c2016-10-06 16:12:58 -070028type 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 Crossa2f296f2016-11-29 15:16:18 -080036 UsePrebuilt bool `blueprint:"mutated"`
Colin Crossce75d2c2016-10-06 16:12:58 -070037 }
38 module Module
39}
40
41func (p *Prebuilt) Name(name string) string {
42 return "prebuilt_" + name
43}
44
45func (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
59type PrebuiltInterface interface {
60 Module
61 Prebuilt() *Prebuilt
Colin Crossce75d2c2016-10-06 16:12:58 -070062}
63
64// prebuiltMutator ensures that there is always a module with an undecorated name, and marks
65// prebuilt modules that have both a prebuilt and a source module.
66func prebuiltMutator(ctx BottomUpMutatorContext) {
67 if m, ok := ctx.Module().(PrebuiltInterface); ok && m.Prebuilt() != nil {
68 p := m.Prebuilt()
69 name := m.base().BaseModuleName()
70 if ctx.OtherModuleExists(name) {
Nan Zhang2502e122017-03-09 18:43:01 -080071 ctx.AddReverseDependency(ctx.Module(), prebuiltDepTag, name)
Colin Crossce75d2c2016-10-06 16:12:58 -070072 p.Properties.SourceExists = true
73 } else {
74 ctx.Rename(name)
75 }
76 }
77}
78
Colin Crossa2f296f2016-11-29 15:16:18 -080079// PrebuiltSelectModuleMutator marks prebuilts that are overriding source modules, and disables
80// installing the source module.
81func PrebuiltSelectModuleMutator(ctx TopDownMutatorContext) {
82 if s, ok := ctx.Module().(Module); ok {
83 ctx.VisitDirectDeps(func(m blueprint.Module) {
Nan Zhang2502e122017-03-09 18:43:01 -080084 if ctx.OtherModuleDependencyTag(m) == prebuiltDepTag {
Colin Crossa2f296f2016-11-29 15:16:18 -080085 p := m.(PrebuiltInterface).Prebuilt()
86 if p.usePrebuilt(ctx, s) {
87 p.Properties.UsePrebuilt = true
88 s.SkipInstall()
89 }
90 }
91 })
92 }
93}
94
Colin Cross0f3c72f2016-11-23 15:44:07 -080095// PrebuiltReplaceMutator replaces dependencies on the source module with dependencies on the
96// prebuilt when both modules exist and the prebuilt should be used. When the prebuilt should not
97// be used, disable installing it.
Colin Crossce75d2c2016-10-06 16:12:58 -070098func PrebuiltReplaceMutator(ctx BottomUpMutatorContext) {
99 if m, ok := ctx.Module().(PrebuiltInterface); ok && m.Prebuilt() != nil {
100 p := m.Prebuilt()
101 name := m.base().BaseModuleName()
Colin Crossa2f296f2016-11-29 15:16:18 -0800102 if p.Properties.UsePrebuilt {
Colin Cross0f3c72f2016-11-23 15:44:07 -0800103 if p.Properties.SourceExists {
104 ctx.ReplaceDependencies(name)
105 }
106 } else {
107 m.SkipInstall()
Colin Crossce75d2c2016-10-06 16:12:58 -0700108 }
109 }
110}
111
Colin Crossa2f296f2016-11-29 15:16:18 -0800112// usePrebuilt returns true if a prebuilt should be used instead of the source module. The prebuilt
113// will be used if it is marked "prefer" or if the source module is disabled.
114func (p *Prebuilt) usePrebuilt(ctx TopDownMutatorContext, source Module) bool {
115 if len(p.Properties.Srcs) == 0 {
116 return false
Colin Crossce75d2c2016-10-06 16:12:58 -0700117 }
Colin Crossce75d2c2016-10-06 16:12:58 -0700118
Colin Crossa2f296f2016-11-29 15:16:18 -0800119 // TODO: use p.Properties.Name and ctx.ModuleDir to override preference
120 if p.Properties.Prefer {
121 return true
122 }
123
124 return !source.Enabled()
Colin Crossce75d2c2016-10-06 16:12:58 -0700125}