blob: ce16d73409abf6671769a5e9ead520272942a382 [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"
Jaewoong Jungfa00c062020-05-14 14:15:24 -070019 "strconv"
Jiyong Park09d77522019-11-18 11:16:27 +090020 "strings"
21
22 "android/soong/android"
Jaewoong Jungfa00c062020-05-14 14:15:24 -070023 "android/soong/java"
Jiyong Park10e926b2020-07-16 21:38:56 +090024
Jaewoong Jungfa00c062020-05-14 14:15:24 -070025 "github.com/google/blueprint"
Jiyong Park09d77522019-11-18 11:16:27 +090026
27 "github.com/google/blueprint/proptools"
28)
29
Jaewoong Jungfa00c062020-05-14 14:15:24 -070030var (
31 extractMatchingApex = pctx.StaticRule(
32 "extractMatchingApex",
33 blueprint.RuleParams{
34 Command: `rm -rf "$out" && ` +
35 `${extract_apks} -o "${out}" -allow-prereleased=${allow-prereleased} ` +
36 `-sdk-version=${sdk-version} -abis=${abis} -screen-densities=all -extract-single ` +
37 `${in}`,
38 CommandDeps: []string{"${extract_apks}"},
39 },
40 "abis", "allow-prereleased", "sdk-version")
41)
42
Jiyong Park10e926b2020-07-16 21:38:56 +090043type prebuilt interface {
44 isForceDisabled() bool
45 InstallFilename() string
46}
47
48type prebuiltCommon struct {
49 prebuilt android.Prebuilt
50 properties prebuiltCommonProperties
51}
52
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -070053type sanitizedPrebuilt interface {
54 hasSanitizedSource(sanitizer string) bool
55}
56
Jiyong Park10e926b2020-07-16 21:38:56 +090057type prebuiltCommonProperties struct {
58 ForceDisable bool `blueprint:"mutated"`
59}
60
61func (p *prebuiltCommon) Prebuilt() *android.Prebuilt {
62 return &p.prebuilt
63}
64
65func (p *prebuiltCommon) isForceDisabled() bool {
66 return p.properties.ForceDisable
67}
68
69func (p *prebuiltCommon) checkForceDisable(ctx android.ModuleContext) bool {
70 // If the device is configured to use flattened APEX, force disable the prebuilt because
71 // the prebuilt is a non-flattened one.
72 forceDisable := ctx.Config().FlattenApex()
73
74 // Force disable the prebuilts when we are doing unbundled build. We do unbundled build
75 // to build the prebuilts themselves.
76 forceDisable = forceDisable || ctx.Config().UnbundledBuild()
77
78 // Force disable the prebuilts when coverage is enabled.
79 forceDisable = forceDisable || ctx.DeviceConfig().NativeCoverageEnabled()
80 forceDisable = forceDisable || ctx.Config().IsEnvTrue("EMMA_INSTRUMENT")
81
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -070082 // b/137216042 don't use prebuilts when address sanitizer is on, unless the prebuilt has a sanitized source
83 sanitized := ctx.Module().(sanitizedPrebuilt)
84 forceDisable = forceDisable || (android.InList("address", ctx.Config().SanitizeDevice()) && !sanitized.hasSanitizedSource("address"))
85 forceDisable = forceDisable || (android.InList("hwaddress", ctx.Config().SanitizeDevice()) && !sanitized.hasSanitizedSource("hwaddress"))
Jiyong Park10e926b2020-07-16 21:38:56 +090086
87 if forceDisable && p.prebuilt.SourceExists() {
88 p.properties.ForceDisable = true
89 return true
90 }
91 return false
92}
93
Jiyong Park09d77522019-11-18 11:16:27 +090094type Prebuilt struct {
95 android.ModuleBase
Jiyong Park10e926b2020-07-16 21:38:56 +090096 prebuiltCommon
Jiyong Park09d77522019-11-18 11:16:27 +090097
98 properties PrebuiltProperties
99
100 inputApex android.Path
101 installDir android.InstallPath
102 installFilename string
103 outputApex android.WritablePath
Jooyung Han002ab682020-01-08 01:57:58 +0900104
105 // list of commands to create symlinks for backward compatibility.
106 // these commands will be attached as LOCAL_POST_INSTALL_CMD
107 compatSymlinks []string
Jiyong Park09d77522019-11-18 11:16:27 +0900108}
109
110type PrebuiltProperties struct {
111 // the path to the prebuilt .apex file to import.
Jiyong Park10e926b2020-07-16 21:38:56 +0900112 Source string `blueprint:"mutated"`
Jiyong Park09d77522019-11-18 11:16:27 +0900113
114 Src *string
115 Arch struct {
116 Arm struct {
117 Src *string
118 }
119 Arm64 struct {
120 Src *string
121 }
122 X86 struct {
123 Src *string
124 }
125 X86_64 struct {
126 Src *string
127 }
128 }
129
130 Installable *bool
131 // Optional name for the installed apex. If unspecified, name of the
132 // module is used as the file name
133 Filename *string
134
135 // Names of modules to be overridden. Listed modules can only be other binaries
136 // (in Make or Soong).
137 // This does not completely prevent installation of the overridden binaries, but if both
138 // binaries would be installed by default (in PRODUCT_PACKAGES) the other binary will be removed
139 // from PRODUCT_PACKAGES.
140 Overrides []string
141}
142
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700143func (a *Prebuilt) hasSanitizedSource(sanitizer string) bool {
144 return false
145}
146
Jiyong Park09d77522019-11-18 11:16:27 +0900147func (p *Prebuilt) installable() bool {
148 return p.properties.Installable == nil || proptools.Bool(p.properties.Installable)
149}
150
Jiyong Park09d77522019-11-18 11:16:27 +0900151func (p *Prebuilt) OutputFiles(tag string) (android.Paths, error) {
152 switch tag {
153 case "":
154 return android.Paths{p.outputApex}, nil
155 default:
156 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
157 }
158}
159
160func (p *Prebuilt) InstallFilename() string {
161 return proptools.StringDefault(p.properties.Filename, p.BaseModuleName()+imageApexSuffix)
162}
163
Jiyong Park09d77522019-11-18 11:16:27 +0900164func (p *Prebuilt) Name() string {
Jiyong Park10e926b2020-07-16 21:38:56 +0900165 return p.prebuiltCommon.prebuilt.Name(p.ModuleBase.Name())
Jiyong Park09d77522019-11-18 11:16:27 +0900166}
167
168// prebuilt_apex imports an `.apex` file into the build graph as if it was built with apex.
169func PrebuiltFactory() android.Module {
170 module := &Prebuilt{}
171 module.AddProperties(&module.properties)
172 android.InitSingleSourcePrebuiltModule(module, &module.properties, "Source")
173 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
174 return module
175}
176
177func (p *Prebuilt) DepsMutator(ctx android.BottomUpMutatorContext) {
Jiyong Park09d77522019-11-18 11:16:27 +0900178 // This is called before prebuilt_select and prebuilt_postdeps mutators
179 // The mutators requires that src to be set correctly for each arch so that
180 // arch variants are disabled when src is not provided for the arch.
181 if len(ctx.MultiTargets()) != 1 {
182 ctx.ModuleErrorf("compile_multilib shouldn't be \"both\" for prebuilt_apex")
183 return
184 }
185 var src string
186 switch ctx.MultiTargets()[0].Arch.ArchType {
187 case android.Arm:
188 src = String(p.properties.Arch.Arm.Src)
189 case android.Arm64:
190 src = String(p.properties.Arch.Arm64.Src)
191 case android.X86:
192 src = String(p.properties.Arch.X86.Src)
193 case android.X86_64:
194 src = String(p.properties.Arch.X86_64.Src)
195 default:
196 ctx.ModuleErrorf("prebuilt_apex does not support %q", ctx.MultiTargets()[0].Arch.String())
197 return
198 }
199 if src == "" {
200 src = String(p.properties.Src)
201 }
202 p.properties.Source = src
203}
204
205func (p *Prebuilt) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jiyong Park09d77522019-11-18 11:16:27 +0900206 // TODO(jungjw): Check the key validity.
207 p.inputApex = p.Prebuilt().SingleSourcePath(ctx)
208 p.installDir = android.PathForModuleInstall(ctx, "apex")
209 p.installFilename = p.InstallFilename()
210 if !strings.HasSuffix(p.installFilename, imageApexSuffix) {
211 ctx.ModuleErrorf("filename should end in %s for prebuilt_apex", imageApexSuffix)
212 }
213 p.outputApex = android.PathForModuleOut(ctx, p.installFilename)
214 ctx.Build(pctx, android.BuildParams{
215 Rule: android.Cp,
216 Input: p.inputApex,
217 Output: p.outputApex,
218 })
Jiyong Park10e926b2020-07-16 21:38:56 +0900219
220 if p.prebuiltCommon.checkForceDisable(ctx) {
221 p.SkipInstall()
222 return
223 }
224
Jiyong Park09d77522019-11-18 11:16:27 +0900225 if p.installable() {
226 ctx.InstallFile(p.installDir, p.installFilename, p.inputApex)
227 }
228
Jooyung Han002ab682020-01-08 01:57:58 +0900229 // in case that prebuilt_apex replaces source apex (using prefer: prop)
230 p.compatSymlinks = makeCompatSymlinks(p.BaseModuleName(), ctx)
231 // or that prebuilt_apex overrides other apexes (using overrides: prop)
232 for _, overridden := range p.properties.Overrides {
233 p.compatSymlinks = append(p.compatSymlinks, makeCompatSymlinks(overridden, ctx)...)
234 }
Jiyong Park09d77522019-11-18 11:16:27 +0900235}
236
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900237func (p *Prebuilt) AndroidMkEntries() []android.AndroidMkEntries {
238 return []android.AndroidMkEntries{android.AndroidMkEntries{
Jiyong Park09d77522019-11-18 11:16:27 +0900239 Class: "ETC",
240 OutputFile: android.OptionalPathForPath(p.inputApex),
241 Include: "$(BUILD_PREBUILT)",
242 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
243 func(entries *android.AndroidMkEntries) {
244 entries.SetString("LOCAL_MODULE_PATH", p.installDir.ToMakePath().String())
245 entries.SetString("LOCAL_MODULE_STEM", p.installFilename)
246 entries.SetBoolIfTrue("LOCAL_UNINSTALLABLE_MODULE", !p.installable())
247 entries.AddStrings("LOCAL_OVERRIDES_MODULES", p.properties.Overrides...)
Jooyung Han002ab682020-01-08 01:57:58 +0900248 if len(p.compatSymlinks) > 0 {
249 entries.SetString("LOCAL_POST_INSTALL_CMD", strings.Join(p.compatSymlinks, " && "))
250 }
Jiyong Park09d77522019-11-18 11:16:27 +0900251 },
252 },
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900253 }}
Jiyong Park09d77522019-11-18 11:16:27 +0900254}
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700255
256type ApexSet struct {
257 android.ModuleBase
Jiyong Park10e926b2020-07-16 21:38:56 +0900258 prebuiltCommon
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700259
260 properties ApexSetProperties
261
262 installDir android.InstallPath
263 installFilename string
264 outputApex android.WritablePath
265
266 // list of commands to create symlinks for backward compatibility.
267 // these commands will be attached as LOCAL_POST_INSTALL_CMD
268 compatSymlinks []string
Jooyung Han29637162020-06-30 06:34:23 +0900269
270 hostRequired []string
271 postInstallCommands []string
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700272}
273
274type ApexSetProperties struct {
275 // the .apks file path that contains prebuilt apex files to be extracted.
276 Set *string
277
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700278 Sanitized struct {
279 None struct {
280 Set *string
281 }
282 Address struct {
283 Set *string
284 }
285 Hwaddress struct {
286 Set *string
287 }
288 }
289
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700290 // whether the extracted apex file installable.
291 Installable *bool
292
293 // optional name for the installed apex. If unspecified, name of the
294 // module is used as the file name
295 Filename *string
296
297 // names of modules to be overridden. Listed modules can only be other binaries
298 // (in Make or Soong).
299 // This does not completely prevent installation of the overridden binaries, but if both
300 // binaries would be installed by default (in PRODUCT_PACKAGES) the other binary will be removed
301 // from PRODUCT_PACKAGES.
302 Overrides []string
303
304 // apexes in this set use prerelease SDK version
305 Prerelease *bool
306}
307
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700308func (a *ApexSet) prebuiltSrcs(ctx android.BaseModuleContext) []string {
309 var srcs []string
310 if a.properties.Set != nil {
311 srcs = append(srcs, *a.properties.Set)
312 }
313
314 var sanitizers []string
315 if ctx.Host() {
316 sanitizers = ctx.Config().SanitizeHost()
317 } else {
318 sanitizers = ctx.Config().SanitizeDevice()
319 }
320
321 if android.InList("address", sanitizers) && a.properties.Sanitized.Address.Set != nil {
322 srcs = append(srcs, *a.properties.Sanitized.Address.Set)
323 } else if android.InList("hwaddress", sanitizers) && a.properties.Sanitized.Hwaddress.Set != nil {
324 srcs = append(srcs, *a.properties.Sanitized.Hwaddress.Set)
325 } else if a.properties.Sanitized.None.Set != nil {
326 srcs = append(srcs, *a.properties.Sanitized.None.Set)
327 }
328
329 return srcs
330}
331
332func (a *ApexSet) hasSanitizedSource(sanitizer string) bool {
333 if sanitizer == "address" {
334 return a.properties.Sanitized.Address.Set != nil
335 }
336 if sanitizer == "hwaddress" {
337 return a.properties.Sanitized.Hwaddress.Set != nil
338 }
339
340 return false
341}
342
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700343func (a *ApexSet) installable() bool {
344 return a.properties.Installable == nil || proptools.Bool(a.properties.Installable)
345}
346
347func (a *ApexSet) InstallFilename() string {
348 return proptools.StringDefault(a.properties.Filename, a.BaseModuleName()+imageApexSuffix)
349}
350
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700351func (a *ApexSet) Name() string {
Jiyong Park10e926b2020-07-16 21:38:56 +0900352 return a.prebuiltCommon.prebuilt.Name(a.ModuleBase.Name())
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700353}
354
Jiyong Park8d6c51e2020-06-12 17:26:31 +0900355func (a *ApexSet) Overrides() []string {
356 return a.properties.Overrides
357}
358
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700359// prebuilt_apex imports an `.apex` file into the build graph as if it was built with apex.
360func apexSetFactory() android.Module {
361 module := &ApexSet{}
362 module.AddProperties(&module.properties)
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700363
364 srcsSupplier := func(ctx android.BaseModuleContext) []string {
365 return module.prebuiltSrcs(ctx)
366 }
367
368 android.InitPrebuiltModuleWithSrcSupplier(module, srcsSupplier, "set")
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700369 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
370 return module
371}
372
373func (a *ApexSet) GenerateAndroidBuildActions(ctx android.ModuleContext) {
374 a.installFilename = a.InstallFilename()
375 if !strings.HasSuffix(a.installFilename, imageApexSuffix) {
376 ctx.ModuleErrorf("filename should end in %s for apex_set", imageApexSuffix)
377 }
378
Jiyong Park10e926b2020-07-16 21:38:56 +0900379 apexSet := a.prebuiltCommon.prebuilt.SingleSourcePath(ctx)
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700380 a.outputApex = android.PathForModuleOut(ctx, a.installFilename)
381 ctx.Build(pctx,
382 android.BuildParams{
383 Rule: extractMatchingApex,
384 Description: "Extract an apex from an apex set",
385 Inputs: android.Paths{apexSet},
386 Output: a.outputApex,
387 Args: map[string]string{
388 "abis": strings.Join(java.SupportedAbis(ctx), ","),
389 "allow-prereleased": strconv.FormatBool(proptools.Bool(a.properties.Prerelease)),
Dan Albert4f378d72020-07-23 17:32:15 -0700390 "sdk-version": ctx.Config().PlatformSdkVersion().String(),
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700391 },
392 })
Jiyong Park10e926b2020-07-16 21:38:56 +0900393
394 if a.prebuiltCommon.checkForceDisable(ctx) {
395 a.SkipInstall()
396 return
397 }
398
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700399 a.installDir = android.PathForModuleInstall(ctx, "apex")
400 if a.installable() {
401 ctx.InstallFile(a.installDir, a.installFilename, a.outputApex)
402 }
403
404 // in case that apex_set replaces source apex (using prefer: prop)
405 a.compatSymlinks = makeCompatSymlinks(a.BaseModuleName(), ctx)
406 // or that apex_set overrides other apexes (using overrides: prop)
407 for _, overridden := range a.properties.Overrides {
408 a.compatSymlinks = append(a.compatSymlinks, makeCompatSymlinks(overridden, ctx)...)
409 }
Jooyung Han29637162020-06-30 06:34:23 +0900410
411 if ctx.Config().InstallExtraFlattenedApexes() {
412 // flattened apex should be in /system_ext/apex
413 flattenedApexDir := android.PathForModuleInstall(&systemExtContext{ctx}, "apex", a.BaseModuleName())
414 a.postInstallCommands = append(a.postInstallCommands,
415 fmt.Sprintf("$(HOST_OUT_EXECUTABLES)/deapexer --debugfs_path $(HOST_OUT_EXECUTABLES)/debugfs extract %s %s",
416 a.outputApex.String(),
417 flattenedApexDir.ToMakePath().String(),
418 ))
419 a.hostRequired = []string{"deapexer", "debugfs"}
420 }
421}
422
423type systemExtContext struct {
424 android.ModuleContext
425}
426
427func (*systemExtContext) SystemExtSpecific() bool {
428 return true
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700429}
430
431func (a *ApexSet) AndroidMkEntries() []android.AndroidMkEntries {
432 return []android.AndroidMkEntries{android.AndroidMkEntries{
Jooyung Han29637162020-06-30 06:34:23 +0900433 Class: "ETC",
434 OutputFile: android.OptionalPathForPath(a.outputApex),
435 Include: "$(BUILD_PREBUILT)",
436 Host_required: a.hostRequired,
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700437 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
438 func(entries *android.AndroidMkEntries) {
439 entries.SetString("LOCAL_MODULE_PATH", a.installDir.ToMakePath().String())
440 entries.SetString("LOCAL_MODULE_STEM", a.installFilename)
441 entries.SetBoolIfTrue("LOCAL_UNINSTALLABLE_MODULE", !a.installable())
442 entries.AddStrings("LOCAL_OVERRIDES_MODULES", a.properties.Overrides...)
Jooyung Han29637162020-06-30 06:34:23 +0900443 postInstallCommands := append([]string{}, a.postInstallCommands...)
444 postInstallCommands = append(postInstallCommands, a.compatSymlinks...)
445 if len(postInstallCommands) > 0 {
446 entries.SetString("LOCAL_POST_INSTALL_CMD", strings.Join(postInstallCommands, " && "))
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700447 }
448 },
449 },
450 }}
451}