blob: 5f9b4b09473617bc44a687d0a6db7b4615ad0a62 [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
22var prebuiltDependencyTag blueprint.BaseDependencyTag
23
Colin Crossce75d2c2016-10-06 16:12:58 -070024type Prebuilt struct {
25 Properties struct {
26 Srcs []string `android:"arch_variant"`
27 // When prefer is set to true the prebuilt will be used instead of any source module with
28 // a matching name.
29 Prefer bool `android:"arch_variant"`
30
31 SourceExists bool `blueprint:"mutated"`
Colin Crossa2f296f2016-11-29 15:16:18 -080032 UsePrebuilt bool `blueprint:"mutated"`
Colin Crossce75d2c2016-10-06 16:12:58 -070033 }
34 module Module
35}
36
37func (p *Prebuilt) Name(name string) string {
38 return "prebuilt_" + name
39}
40
41func (p *Prebuilt) Path(ctx ModuleContext) Path {
42 if len(p.Properties.Srcs) == 0 {
43 ctx.PropertyErrorf("srcs", "missing prebuilt source file")
44 return nil
45 }
46
47 if len(p.Properties.Srcs) > 1 {
48 ctx.PropertyErrorf("srcs", "multiple prebuilt source files")
49 return nil
50 }
51
52 return PathForModuleSrc(ctx, p.Properties.Srcs[0])
53}
54
55type PrebuiltInterface interface {
56 Module
57 Prebuilt() *Prebuilt
Colin Crossce75d2c2016-10-06 16:12:58 -070058}
59
60// prebuiltMutator ensures that there is always a module with an undecorated name, and marks
61// prebuilt modules that have both a prebuilt and a source module.
62func prebuiltMutator(ctx BottomUpMutatorContext) {
63 if m, ok := ctx.Module().(PrebuiltInterface); ok && m.Prebuilt() != nil {
64 p := m.Prebuilt()
65 name := m.base().BaseModuleName()
66 if ctx.OtherModuleExists(name) {
67 ctx.AddReverseDependency(ctx.Module(), prebuiltDependencyTag, name)
68 p.Properties.SourceExists = true
69 } else {
70 ctx.Rename(name)
71 }
72 }
73}
74
Colin Crossa2f296f2016-11-29 15:16:18 -080075// PrebuiltSelectModuleMutator marks prebuilts that are overriding source modules, and disables
76// installing the source module.
77func PrebuiltSelectModuleMutator(ctx TopDownMutatorContext) {
78 if s, ok := ctx.Module().(Module); ok {
79 ctx.VisitDirectDeps(func(m blueprint.Module) {
80 if ctx.OtherModuleDependencyTag(m) == prebuiltDependencyTag {
81 p := m.(PrebuiltInterface).Prebuilt()
82 if p.usePrebuilt(ctx, s) {
83 p.Properties.UsePrebuilt = true
84 s.SkipInstall()
85 }
86 }
87 })
88 }
89}
90
Colin Cross0f3c72f2016-11-23 15:44:07 -080091// PrebuiltReplaceMutator replaces dependencies on the source module with dependencies on the
92// prebuilt when both modules exist and the prebuilt should be used. When the prebuilt should not
93// be used, disable installing it.
Colin Crossce75d2c2016-10-06 16:12:58 -070094func PrebuiltReplaceMutator(ctx BottomUpMutatorContext) {
95 if m, ok := ctx.Module().(PrebuiltInterface); ok && m.Prebuilt() != nil {
96 p := m.Prebuilt()
97 name := m.base().BaseModuleName()
Colin Crossa2f296f2016-11-29 15:16:18 -080098 if p.Properties.UsePrebuilt {
Colin Cross0f3c72f2016-11-23 15:44:07 -080099 if p.Properties.SourceExists {
100 ctx.ReplaceDependencies(name)
101 }
102 } else {
103 m.SkipInstall()
Colin Crossce75d2c2016-10-06 16:12:58 -0700104 }
105 }
106}
107
Colin Crossa2f296f2016-11-29 15:16:18 -0800108// usePrebuilt returns true if a prebuilt should be used instead of the source module. The prebuilt
109// will be used if it is marked "prefer" or if the source module is disabled.
110func (p *Prebuilt) usePrebuilt(ctx TopDownMutatorContext, source Module) bool {
111 if len(p.Properties.Srcs) == 0 {
112 return false
Colin Crossce75d2c2016-10-06 16:12:58 -0700113 }
Colin Crossce75d2c2016-10-06 16:12:58 -0700114
Colin Crossa2f296f2016-11-29 15:16:18 -0800115 // TODO: use p.Properties.Name and ctx.ModuleDir to override preference
116 if p.Properties.Prefer {
117 return true
118 }
119
120 return !source.Enabled()
Colin Crossce75d2c2016-10-06 16:12:58 -0700121}