blob: db3b5ef8858b9cabfd482a1b08ca9040bbfc21e1 [file] [log] [blame]
Jiyong Park09d77522019-11-18 11:16:27 +09001// Copyright (C) 2019 The Android Open Source Project
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 apex
16
17import (
18 "fmt"
19 "strings"
20
21 "android/soong/android"
22
23 "github.com/google/blueprint/proptools"
24)
25
26type Prebuilt struct {
27 android.ModuleBase
28 prebuilt android.Prebuilt
29
30 properties PrebuiltProperties
31
32 inputApex android.Path
33 installDir android.InstallPath
34 installFilename string
35 outputApex android.WritablePath
36}
37
38type PrebuiltProperties struct {
39 // the path to the prebuilt .apex file to import.
40 Source string `blueprint:"mutated"`
41 ForceDisable bool `blueprint:"mutated"`
42
43 Src *string
44 Arch struct {
45 Arm struct {
46 Src *string
47 }
48 Arm64 struct {
49 Src *string
50 }
51 X86 struct {
52 Src *string
53 }
54 X86_64 struct {
55 Src *string
56 }
57 }
58
59 Installable *bool
60 // Optional name for the installed apex. If unspecified, name of the
61 // module is used as the file name
62 Filename *string
63
64 // Names of modules to be overridden. Listed modules can only be other binaries
65 // (in Make or Soong).
66 // This does not completely prevent installation of the overridden binaries, but if both
67 // binaries would be installed by default (in PRODUCT_PACKAGES) the other binary will be removed
68 // from PRODUCT_PACKAGES.
69 Overrides []string
70}
71
72func (p *Prebuilt) installable() bool {
73 return p.properties.Installable == nil || proptools.Bool(p.properties.Installable)
74}
75
76func (p *Prebuilt) isForceDisabled() bool {
77 return p.properties.ForceDisable
78}
79
80func (p *Prebuilt) OutputFiles(tag string) (android.Paths, error) {
81 switch tag {
82 case "":
83 return android.Paths{p.outputApex}, nil
84 default:
85 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
86 }
87}
88
89func (p *Prebuilt) InstallFilename() string {
90 return proptools.StringDefault(p.properties.Filename, p.BaseModuleName()+imageApexSuffix)
91}
92
93func (p *Prebuilt) Prebuilt() *android.Prebuilt {
94 return &p.prebuilt
95}
96
97func (p *Prebuilt) Name() string {
98 return p.prebuilt.Name(p.ModuleBase.Name())
99}
100
101// prebuilt_apex imports an `.apex` file into the build graph as if it was built with apex.
102func PrebuiltFactory() android.Module {
103 module := &Prebuilt{}
104 module.AddProperties(&module.properties)
105 android.InitSingleSourcePrebuiltModule(module, &module.properties, "Source")
106 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
107 return module
108}
109
110func (p *Prebuilt) DepsMutator(ctx android.BottomUpMutatorContext) {
111 // If the device is configured to use flattened APEX, force disable the prebuilt because
112 // the prebuilt is a non-flattened one.
113 forceDisable := ctx.Config().FlattenApex()
114
115 // Force disable the prebuilts when we are doing unbundled build. We do unbundled build
116 // to build the prebuilts themselves.
117 forceDisable = forceDisable || ctx.Config().UnbundledBuild()
118
119 // Force disable the prebuilts when coverage is enabled.
120 forceDisable = forceDisable || ctx.DeviceConfig().NativeCoverageEnabled()
121 forceDisable = forceDisable || ctx.Config().IsEnvTrue("EMMA_INSTRUMENT")
122
123 // b/137216042 don't use prebuilts when address sanitizer is on
124 forceDisable = forceDisable || android.InList("address", ctx.Config().SanitizeDevice()) ||
125 android.InList("hwaddress", ctx.Config().SanitizeDevice())
126
127 if forceDisable && p.prebuilt.SourceExists() {
128 p.properties.ForceDisable = true
129 return
130 }
131
132 // This is called before prebuilt_select and prebuilt_postdeps mutators
133 // The mutators requires that src to be set correctly for each arch so that
134 // arch variants are disabled when src is not provided for the arch.
135 if len(ctx.MultiTargets()) != 1 {
136 ctx.ModuleErrorf("compile_multilib shouldn't be \"both\" for prebuilt_apex")
137 return
138 }
139 var src string
140 switch ctx.MultiTargets()[0].Arch.ArchType {
141 case android.Arm:
142 src = String(p.properties.Arch.Arm.Src)
143 case android.Arm64:
144 src = String(p.properties.Arch.Arm64.Src)
145 case android.X86:
146 src = String(p.properties.Arch.X86.Src)
147 case android.X86_64:
148 src = String(p.properties.Arch.X86_64.Src)
149 default:
150 ctx.ModuleErrorf("prebuilt_apex does not support %q", ctx.MultiTargets()[0].Arch.String())
151 return
152 }
153 if src == "" {
154 src = String(p.properties.Src)
155 }
156 p.properties.Source = src
157}
158
159func (p *Prebuilt) GenerateAndroidBuildActions(ctx android.ModuleContext) {
160 if p.properties.ForceDisable {
161 return
162 }
163
164 // TODO(jungjw): Check the key validity.
165 p.inputApex = p.Prebuilt().SingleSourcePath(ctx)
166 p.installDir = android.PathForModuleInstall(ctx, "apex")
167 p.installFilename = p.InstallFilename()
168 if !strings.HasSuffix(p.installFilename, imageApexSuffix) {
169 ctx.ModuleErrorf("filename should end in %s for prebuilt_apex", imageApexSuffix)
170 }
171 p.outputApex = android.PathForModuleOut(ctx, p.installFilename)
172 ctx.Build(pctx, android.BuildParams{
173 Rule: android.Cp,
174 Input: p.inputApex,
175 Output: p.outputApex,
176 })
177 if p.installable() {
178 ctx.InstallFile(p.installDir, p.installFilename, p.inputApex)
179 }
180
181 // TODO(b/143192278): Add compat symlinks for prebuilt_apex
182}
183
184func (p *Prebuilt) AndroidMkEntries() android.AndroidMkEntries {
185 return android.AndroidMkEntries{
186 Class: "ETC",
187 OutputFile: android.OptionalPathForPath(p.inputApex),
188 Include: "$(BUILD_PREBUILT)",
189 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
190 func(entries *android.AndroidMkEntries) {
191 entries.SetString("LOCAL_MODULE_PATH", p.installDir.ToMakePath().String())
192 entries.SetString("LOCAL_MODULE_STEM", p.installFilename)
193 entries.SetBoolIfTrue("LOCAL_UNINSTALLABLE_MODULE", !p.installable())
194 entries.AddStrings("LOCAL_OVERRIDES_MODULES", p.properties.Overrides...)
195 },
196 },
197 }
198}