blob: 5087b1879845218d8bebad477cc1f2de9eaa9b6d [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"
Jaewoong Jung939ebd52019-03-26 15:07:36 -070021 "github.com/google/blueprint/proptools"
Colin Cross74d73e22017-08-02 11:05:49 -070022)
Colin Crossce75d2c2016-10-06 16:12:58 -070023
24// This file implements common functionality for handling modules that may exist as prebuilts,
25// source, or both.
26
Nan Zhang2502e122017-03-09 18:43:01 -080027type prebuiltDependencyTag struct {
28 blueprint.BaseDependencyTag
29}
30
31var prebuiltDepTag prebuiltDependencyTag
Colin Crossce75d2c2016-10-06 16:12:58 -070032
Colin Cross74d73e22017-08-02 11:05:49 -070033type PrebuiltProperties struct {
34 // When prefer is set to true the prebuilt will be used instead of any source module with
35 // a matching name.
Nan Zhang0007d812017-11-07 10:57:05 -080036 Prefer *bool `android:"arch_variant"`
Colin Crossce75d2c2016-10-06 16:12:58 -070037
Colin Cross74d73e22017-08-02 11:05:49 -070038 SourceExists bool `blueprint:"mutated"`
39 UsePrebuilt bool `blueprint:"mutated"`
40}
41
42type Prebuilt struct {
43 properties PrebuiltProperties
44 module Module
45 srcs *[]string
Jaewoong Jung939ebd52019-03-26 15:07:36 -070046 src *string
Colin Crossce75d2c2016-10-06 16:12:58 -070047}
48
49func (p *Prebuilt) Name(name string) string {
50 return "prebuilt_" + name
51}
52
Jaewoong Junga5e5abc2019-04-26 14:31:50 -070053// The below source-related functions and the srcs, src fields are based on an assumption that
54// prebuilt modules have a static source property at the moment. Currently there is only one
55// exception, android_app_import, which chooses a source file depending on the product's DPI
56// preference configs. We'll want to add native support for dynamic source cases if we end up having
57// more modules like this.
Colin Cross74d73e22017-08-02 11:05:49 -070058func (p *Prebuilt) SingleSourcePath(ctx ModuleContext) Path {
Jaewoong Jung939ebd52019-03-26 15:07:36 -070059 if p.srcs != nil {
60 if len(*p.srcs) == 0 {
61 ctx.PropertyErrorf("srcs", "missing prebuilt source file")
62 return nil
63 }
Colin Crossce75d2c2016-10-06 16:12:58 -070064
Jaewoong Jung939ebd52019-03-26 15:07:36 -070065 if len(*p.srcs) > 1 {
66 ctx.PropertyErrorf("srcs", "multiple prebuilt source files")
67 return nil
68 }
Colin Crossce75d2c2016-10-06 16:12:58 -070069
Jaewoong Jung939ebd52019-03-26 15:07:36 -070070 // Return the singleton source after expanding any filegroup in the
71 // sources.
72 return PathForModuleSrc(ctx, (*p.srcs)[0])
73 } else {
74 if proptools.String(p.src) == "" {
75 ctx.PropertyErrorf("src", "missing prebuilt source file")
76 return nil
77 }
78 return PathForModuleSrc(ctx, *p.src)
79 }
Colin Cross74d73e22017-08-02 11:05:49 -070080}
81
Jiyong Park4d277042019-04-23 18:00:10 +090082func (p *Prebuilt) UsePrebuilt() bool {
83 return p.properties.UsePrebuilt
84}
85
Colin Cross74d73e22017-08-02 11:05:49 -070086func InitPrebuiltModule(module PrebuiltInterface, srcs *[]string) {
87 p := module.Prebuilt()
88 module.AddProperties(&p.properties)
89 p.srcs = srcs
Colin Crossce75d2c2016-10-06 16:12:58 -070090}
91
Jaewoong Jung939ebd52019-03-26 15:07:36 -070092func InitSingleSourcePrebuiltModule(module PrebuiltInterface, src *string) {
93 p := module.Prebuilt()
94 module.AddProperties(&p.properties)
95 p.src = src
96}
97
Colin Crossce75d2c2016-10-06 16:12:58 -070098type PrebuiltInterface interface {
99 Module
100 Prebuilt() *Prebuilt
Colin Crossce75d2c2016-10-06 16:12:58 -0700101}
102
Colin Cross5ea9bcc2017-07-27 15:41:32 -0700103func RegisterPrebuiltsPreArchMutators(ctx RegisterMutatorsContext) {
Jaewoong Jung939ebd52019-03-26 15:07:36 -0700104 ctx.BottomUp("prebuilts", PrebuiltMutator).Parallel()
Colin Crosscec81712017-07-13 14:43:27 -0700105}
106
Colin Cross5ea9bcc2017-07-27 15:41:32 -0700107func RegisterPrebuiltsPostDepsMutators(ctx RegisterMutatorsContext) {
Colin Crosscec81712017-07-13 14:43:27 -0700108 ctx.TopDown("prebuilt_select", PrebuiltSelectModuleMutator).Parallel()
Pirama Arumuga Nainar955dc492018-04-17 14:58:42 -0700109 ctx.BottomUp("prebuilt_postdeps", PrebuiltPostDepsMutator).Parallel()
Colin Crosscec81712017-07-13 14:43:27 -0700110}
111
Jaewoong Jung939ebd52019-03-26 15:07:36 -0700112// PrebuiltMutator ensures that there is always a module with an undecorated name, and marks
Colin Crossce75d2c2016-10-06 16:12:58 -0700113// prebuilt modules that have both a prebuilt and a source module.
Jaewoong Jung939ebd52019-03-26 15:07:36 -0700114func PrebuiltMutator(ctx BottomUpMutatorContext) {
Colin Crossce75d2c2016-10-06 16:12:58 -0700115 if m, ok := ctx.Module().(PrebuiltInterface); ok && m.Prebuilt() != nil {
116 p := m.Prebuilt()
117 name := m.base().BaseModuleName()
118 if ctx.OtherModuleExists(name) {
Nan Zhang2502e122017-03-09 18:43:01 -0800119 ctx.AddReverseDependency(ctx.Module(), prebuiltDepTag, name)
Colin Cross74d73e22017-08-02 11:05:49 -0700120 p.properties.SourceExists = true
Colin Crossce75d2c2016-10-06 16:12:58 -0700121 } else {
122 ctx.Rename(name)
123 }
124 }
125}
126
Colin Crossc3e7fa62017-03-17 13:14:32 -0700127// PrebuiltSelectModuleMutator marks prebuilts that are used, either overriding source modules or
128// because the source module doesn't exist. It also disables installing overridden source modules.
Colin Crossa2f296f2016-11-29 15:16:18 -0800129func PrebuiltSelectModuleMutator(ctx TopDownMutatorContext) {
Colin Crossc3e7fa62017-03-17 13:14:32 -0700130 if m, ok := ctx.Module().(PrebuiltInterface); ok && m.Prebuilt() != nil {
131 p := m.Prebuilt()
Jaewoong Jung939ebd52019-03-26 15:07:36 -0700132 if p.srcs == nil && p.src == nil {
Colin Cross74d73e22017-08-02 11:05:49 -0700133 panic(fmt.Errorf("prebuilt module did not have InitPrebuiltModule called on it"))
134 }
135 if !p.properties.SourceExists {
136 p.properties.UsePrebuilt = p.usePrebuilt(ctx, nil)
Colin Crossc3e7fa62017-03-17 13:14:32 -0700137 }
138 } else if s, ok := ctx.Module().(Module); ok {
Colin Crossee6143c2017-12-30 17:54:27 -0800139 ctx.VisitDirectDepsWithTag(prebuiltDepTag, func(m Module) {
140 p := m.(PrebuiltInterface).Prebuilt()
141 if p.usePrebuilt(ctx, s) {
142 p.properties.UsePrebuilt = true
143 s.SkipInstall()
Colin Crossa2f296f2016-11-29 15:16:18 -0800144 }
145 })
146 }
147}
148
Pirama Arumuga Nainar955dc492018-04-17 14:58:42 -0700149// PrebuiltPostDepsMutator does two operations. It replace dependencies on the
150// source module with dependencies on the prebuilt when both modules exist and
151// the prebuilt should be used. When the prebuilt should not be used, disable
152// installing it. Secondly, it also adds a sourcegroup to any filegroups found
153// in the prebuilt's 'Srcs' property.
154func PrebuiltPostDepsMutator(ctx BottomUpMutatorContext) {
Colin Crossce75d2c2016-10-06 16:12:58 -0700155 if m, ok := ctx.Module().(PrebuiltInterface); ok && m.Prebuilt() != nil {
156 p := m.Prebuilt()
157 name := m.base().BaseModuleName()
Colin Cross74d73e22017-08-02 11:05:49 -0700158 if p.properties.UsePrebuilt {
159 if p.properties.SourceExists {
Colin Cross0f3c72f2016-11-23 15:44:07 -0800160 ctx.ReplaceDependencies(name)
161 }
162 } else {
163 m.SkipInstall()
Colin Crossce75d2c2016-10-06 16:12:58 -0700164 }
165 }
166}
167
Colin Crossa2f296f2016-11-29 15:16:18 -0800168// usePrebuilt returns true if a prebuilt should be used instead of the source module. The prebuilt
169// will be used if it is marked "prefer" or if the source module is disabled.
170func (p *Prebuilt) usePrebuilt(ctx TopDownMutatorContext, source Module) bool {
Jaewoong Jung939ebd52019-03-26 15:07:36 -0700171 if p.srcs != nil && len(*p.srcs) == 0 {
172 return false
173 }
174
175 if p.src != nil && *p.src == "" {
Colin Crossa2f296f2016-11-29 15:16:18 -0800176 return false
Colin Crossce75d2c2016-10-06 16:12:58 -0700177 }
Colin Crossce75d2c2016-10-06 16:12:58 -0700178
Colin Crossa2f296f2016-11-29 15:16:18 -0800179 // TODO: use p.Properties.Name and ctx.ModuleDir to override preference
Nan Zhang0007d812017-11-07 10:57:05 -0800180 if Bool(p.properties.Prefer) {
Colin Crossa2f296f2016-11-29 15:16:18 -0800181 return true
182 }
183
Colin Crossc3e7fa62017-03-17 13:14:32 -0700184 return source == nil || !source.Enabled()
Colin Crossce75d2c2016-10-06 16:12:58 -0700185}