blob: d3f970416328e4830b215ab6114c504e60241494 [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
Colin Cross74d73e22017-08-02 11:05:49 -070017import (
18 "fmt"
19
20 "github.com/google/blueprint"
21)
Colin Crossce75d2c2016-10-06 16:12:58 -070022
23// This file implements common functionality for handling modules that may exist as prebuilts,
24// source, or both.
25
Nan Zhang2502e122017-03-09 18:43:01 -080026type prebuiltDependencyTag struct {
27 blueprint.BaseDependencyTag
28}
29
30var prebuiltDepTag prebuiltDependencyTag
Colin Crossce75d2c2016-10-06 16:12:58 -070031
Colin Cross74d73e22017-08-02 11:05:49 -070032type PrebuiltProperties struct {
33 // When prefer is set to true the prebuilt will be used instead of any source module with
34 // a matching name.
Nan Zhang0007d812017-11-07 10:57:05 -080035 Prefer *bool `android:"arch_variant"`
Colin Crossce75d2c2016-10-06 16:12:58 -070036
Colin Cross74d73e22017-08-02 11:05:49 -070037 SourceExists bool `blueprint:"mutated"`
38 UsePrebuilt bool `blueprint:"mutated"`
39}
40
41type Prebuilt struct {
42 properties PrebuiltProperties
43 module Module
44 srcs *[]string
Colin Crossce75d2c2016-10-06 16:12:58 -070045}
46
47func (p *Prebuilt) Name(name string) string {
48 return "prebuilt_" + name
49}
50
Colin Cross74d73e22017-08-02 11:05:49 -070051func (p *Prebuilt) SingleSourcePath(ctx ModuleContext) Path {
52 if len(*p.srcs) == 0 {
Colin Crossce75d2c2016-10-06 16:12:58 -070053 ctx.PropertyErrorf("srcs", "missing prebuilt source file")
54 return nil
55 }
56
Colin Cross74d73e22017-08-02 11:05:49 -070057 if len(*p.srcs) > 1 {
Colin Crossce75d2c2016-10-06 16:12:58 -070058 ctx.PropertyErrorf("srcs", "multiple prebuilt source files")
59 return nil
60 }
61
Colin Cross74d73e22017-08-02 11:05:49 -070062 return PathForModuleSrc(ctx, (*p.srcs)[0])
63}
64
65func InitPrebuiltModule(module PrebuiltInterface, srcs *[]string) {
66 p := module.Prebuilt()
67 module.AddProperties(&p.properties)
68 p.srcs = srcs
Colin Crossce75d2c2016-10-06 16:12:58 -070069}
70
71type PrebuiltInterface interface {
72 Module
73 Prebuilt() *Prebuilt
Colin Crossce75d2c2016-10-06 16:12:58 -070074}
75
Colin Cross5ea9bcc2017-07-27 15:41:32 -070076func RegisterPrebuiltsPreArchMutators(ctx RegisterMutatorsContext) {
Colin Crosscec81712017-07-13 14:43:27 -070077 ctx.BottomUp("prebuilts", prebuiltMutator).Parallel()
78}
79
Colin Cross5ea9bcc2017-07-27 15:41:32 -070080func RegisterPrebuiltsPostDepsMutators(ctx RegisterMutatorsContext) {
Colin Crosscec81712017-07-13 14:43:27 -070081 ctx.TopDown("prebuilt_select", PrebuiltSelectModuleMutator).Parallel()
82 ctx.BottomUp("prebuilt_replace", PrebuiltReplaceMutator).Parallel()
83}
84
Colin Crossce75d2c2016-10-06 16:12:58 -070085// prebuiltMutator ensures that there is always a module with an undecorated name, and marks
86// prebuilt modules that have both a prebuilt and a source module.
87func prebuiltMutator(ctx BottomUpMutatorContext) {
88 if m, ok := ctx.Module().(PrebuiltInterface); ok && m.Prebuilt() != nil {
89 p := m.Prebuilt()
90 name := m.base().BaseModuleName()
91 if ctx.OtherModuleExists(name) {
Nan Zhang2502e122017-03-09 18:43:01 -080092 ctx.AddReverseDependency(ctx.Module(), prebuiltDepTag, name)
Colin Cross74d73e22017-08-02 11:05:49 -070093 p.properties.SourceExists = true
Colin Crossce75d2c2016-10-06 16:12:58 -070094 } else {
95 ctx.Rename(name)
96 }
97 }
98}
99
Colin Crossc3e7fa62017-03-17 13:14:32 -0700100// PrebuiltSelectModuleMutator marks prebuilts that are used, either overriding source modules or
101// because the source module doesn't exist. It also disables installing overridden source modules.
Colin Crossa2f296f2016-11-29 15:16:18 -0800102func PrebuiltSelectModuleMutator(ctx TopDownMutatorContext) {
Colin Crossc3e7fa62017-03-17 13:14:32 -0700103 if m, ok := ctx.Module().(PrebuiltInterface); ok && m.Prebuilt() != nil {
104 p := m.Prebuilt()
Colin Cross74d73e22017-08-02 11:05:49 -0700105 if p.srcs == nil {
106 panic(fmt.Errorf("prebuilt module did not have InitPrebuiltModule called on it"))
107 }
108 if !p.properties.SourceExists {
109 p.properties.UsePrebuilt = p.usePrebuilt(ctx, nil)
Colin Crossc3e7fa62017-03-17 13:14:32 -0700110 }
111 } else if s, ok := ctx.Module().(Module); ok {
Colin Crossd11fcda2017-10-23 17:59:01 -0700112 ctx.VisitDirectDeps(func(m Module) {
Nan Zhang2502e122017-03-09 18:43:01 -0800113 if ctx.OtherModuleDependencyTag(m) == prebuiltDepTag {
Colin Crossa2f296f2016-11-29 15:16:18 -0800114 p := m.(PrebuiltInterface).Prebuilt()
115 if p.usePrebuilt(ctx, s) {
Colin Cross74d73e22017-08-02 11:05:49 -0700116 p.properties.UsePrebuilt = true
Colin Crossa2f296f2016-11-29 15:16:18 -0800117 s.SkipInstall()
118 }
119 }
120 })
121 }
122}
123
Colin Cross0f3c72f2016-11-23 15:44:07 -0800124// PrebuiltReplaceMutator replaces dependencies on the source module with dependencies on the
125// prebuilt when both modules exist and the prebuilt should be used. When the prebuilt should not
126// be used, disable installing it.
Colin Crossce75d2c2016-10-06 16:12:58 -0700127func PrebuiltReplaceMutator(ctx BottomUpMutatorContext) {
128 if m, ok := ctx.Module().(PrebuiltInterface); ok && m.Prebuilt() != nil {
129 p := m.Prebuilt()
130 name := m.base().BaseModuleName()
Colin Cross74d73e22017-08-02 11:05:49 -0700131 if p.properties.UsePrebuilt {
132 if p.properties.SourceExists {
Colin Cross0f3c72f2016-11-23 15:44:07 -0800133 ctx.ReplaceDependencies(name)
134 }
135 } else {
136 m.SkipInstall()
Colin Crossce75d2c2016-10-06 16:12:58 -0700137 }
138 }
139}
140
Colin Crossa2f296f2016-11-29 15:16:18 -0800141// usePrebuilt returns true if a prebuilt should be used instead of the source module. The prebuilt
142// will be used if it is marked "prefer" or if the source module is disabled.
143func (p *Prebuilt) usePrebuilt(ctx TopDownMutatorContext, source Module) bool {
Colin Cross74d73e22017-08-02 11:05:49 -0700144 if len(*p.srcs) == 0 {
Colin Crossa2f296f2016-11-29 15:16:18 -0800145 return false
Colin Crossce75d2c2016-10-06 16:12:58 -0700146 }
Colin Crossce75d2c2016-10-06 16:12:58 -0700147
Colin Crossa2f296f2016-11-29 15:16:18 -0800148 // TODO: use p.Properties.Name and ctx.ModuleDir to override preference
Nan Zhang0007d812017-11-07 10:57:05 -0800149 if Bool(p.properties.Prefer) {
Colin Crossa2f296f2016-11-29 15:16:18 -0800150 return true
151 }
152
Colin Crossc3e7fa62017-03-17 13:14:32 -0700153 return source == nil || !source.Enabled()
Colin Crossce75d2c2016-10-06 16:12:58 -0700154}