blob: 76524291e6fa0a8a951ad2b47f18559266515d76 [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
24func 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
38type 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
50func (p *Prebuilt) Name(name string) string {
51 return "prebuilt_" + name
52}
53
54func (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
68type PrebuiltInterface interface {
69 Module
70 Prebuilt() *Prebuilt
Colin Cross0f3c72f2016-11-23 15:44:07 -080071 SkipInstall()
Colin Crossce75d2c2016-10-06 16:12:58 -070072}
73
74type PrebuiltSourceInterface interface {
75 SkipInstall()
76}
77
78// prebuiltMutator ensures that there is always a module with an undecorated name, and marks
79// prebuilt modules that have both a prebuilt and a source module.
80func prebuiltMutator(ctx BottomUpMutatorContext) {
81 if m, ok := ctx.Module().(PrebuiltInterface); ok && m.Prebuilt() != nil {
82 p := m.Prebuilt()
83 name := m.base().BaseModuleName()
84 if ctx.OtherModuleExists(name) {
85 ctx.AddReverseDependency(ctx.Module(), prebuiltDependencyTag, name)
86 p.Properties.SourceExists = true
87 } else {
88 ctx.Rename(name)
89 }
90 }
91}
92
Colin Cross0f3c72f2016-11-23 15:44:07 -080093// PrebuiltReplaceMutator replaces dependencies on the source module with dependencies on the
94// prebuilt when both modules exist and the prebuilt should be used. When the prebuilt should not
95// be used, disable installing it.
Colin Crossce75d2c2016-10-06 16:12:58 -070096func PrebuiltReplaceMutator(ctx BottomUpMutatorContext) {
97 if m, ok := ctx.Module().(PrebuiltInterface); ok && m.Prebuilt() != nil {
98 p := m.Prebuilt()
99 name := m.base().BaseModuleName()
Colin Cross0f3c72f2016-11-23 15:44:07 -0800100 if p.usePrebuilt(ctx) {
101 if p.Properties.SourceExists {
102 ctx.ReplaceDependencies(name)
103 }
104 } else {
105 m.SkipInstall()
Colin Crossce75d2c2016-10-06 16:12:58 -0700106 }
107 }
108}
109
110// PrebuiltDisableMutator disables source modules that have prebuilts that should be used instead.
111func PrebuiltDisableMutator(ctx TopDownMutatorContext) {
112 if s, ok := ctx.Module().(PrebuiltSourceInterface); ok {
113 ctx.VisitDirectDeps(func(m blueprint.Module) {
114 if ctx.OtherModuleDependencyTag(m) == prebuiltDependencyTag {
115 p := m.(PrebuiltInterface).Prebuilt()
116 if p.usePrebuilt(ctx) {
117 s.SkipInstall()
118 }
119 }
120 })
121 }
122}
123
124func (p *Prebuilt) usePrebuilt(ctx BaseContext) bool {
125 // TODO: use p.Properties.Name and ctx.ModuleDir to override prefer
126 return p.Properties.Prefer && len(p.Properties.Srcs) > 0
127}