blob: 50e892e8bbd49de3021ab7834cf6714911739f30 [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
Paul Duffin851f3992021-01-13 17:03:51 +0000110type ApexFileProperties struct {
Jiyong Park09d77522019-11-18 11:16:27 +0900111 // 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 }
Paul Duffin851f3992021-01-13 17:03:51 +0000129}
130
131func (p *ApexFileProperties) selectSource(ctx android.BottomUpMutatorContext) error {
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 return fmt.Errorf("compile_multilib shouldn't be \"both\" for prebuilt_apex")
137 }
138 var src string
139 switch ctx.MultiTargets()[0].Arch.ArchType {
140 case android.Arm:
141 src = String(p.Arch.Arm.Src)
142 case android.Arm64:
143 src = String(p.Arch.Arm64.Src)
144 case android.X86:
145 src = String(p.Arch.X86.Src)
146 case android.X86_64:
147 src = String(p.Arch.X86_64.Src)
148 default:
149 return fmt.Errorf("prebuilt_apex does not support %q", ctx.MultiTargets()[0].Arch.String())
150 }
151 if src == "" {
152 src = String(p.Src)
153 }
154 p.Source = src
155
156 return nil
157}
158
159type PrebuiltProperties struct {
160 ApexFileProperties
Jiyong Park09d77522019-11-18 11:16:27 +0900161
162 Installable *bool
163 // Optional name for the installed apex. If unspecified, name of the
164 // module is used as the file name
165 Filename *string
166
167 // Names of modules to be overridden. Listed modules can only be other binaries
168 // (in Make or Soong).
169 // This does not completely prevent installation of the overridden binaries, but if both
170 // binaries would be installed by default (in PRODUCT_PACKAGES) the other binary will be removed
171 // from PRODUCT_PACKAGES.
172 Overrides []string
173}
174
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700175func (a *Prebuilt) hasSanitizedSource(sanitizer string) bool {
176 return false
177}
178
Jiyong Park09d77522019-11-18 11:16:27 +0900179func (p *Prebuilt) installable() bool {
180 return p.properties.Installable == nil || proptools.Bool(p.properties.Installable)
181}
182
Jiyong Park09d77522019-11-18 11:16:27 +0900183func (p *Prebuilt) OutputFiles(tag string) (android.Paths, error) {
184 switch tag {
185 case "":
186 return android.Paths{p.outputApex}, nil
187 default:
188 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
189 }
190}
191
192func (p *Prebuilt) InstallFilename() string {
193 return proptools.StringDefault(p.properties.Filename, p.BaseModuleName()+imageApexSuffix)
194}
195
Jiyong Park09d77522019-11-18 11:16:27 +0900196func (p *Prebuilt) Name() string {
Jiyong Park10e926b2020-07-16 21:38:56 +0900197 return p.prebuiltCommon.prebuilt.Name(p.ModuleBase.Name())
Jiyong Park09d77522019-11-18 11:16:27 +0900198}
199
200// prebuilt_apex imports an `.apex` file into the build graph as if it was built with apex.
201func PrebuiltFactory() android.Module {
202 module := &Prebuilt{}
203 module.AddProperties(&module.properties)
204 android.InitSingleSourcePrebuiltModule(module, &module.properties, "Source")
205 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
206 return module
207}
208
209func (p *Prebuilt) DepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffin851f3992021-01-13 17:03:51 +0000210 if err := p.properties.selectSource(ctx); err != nil {
211 ctx.ModuleErrorf("%s", err)
Jiyong Park09d77522019-11-18 11:16:27 +0900212 return
213 }
Jiyong Park09d77522019-11-18 11:16:27 +0900214}
215
216func (p *Prebuilt) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jiyong Park09d77522019-11-18 11:16:27 +0900217 // TODO(jungjw): Check the key validity.
218 p.inputApex = p.Prebuilt().SingleSourcePath(ctx)
219 p.installDir = android.PathForModuleInstall(ctx, "apex")
220 p.installFilename = p.InstallFilename()
221 if !strings.HasSuffix(p.installFilename, imageApexSuffix) {
222 ctx.ModuleErrorf("filename should end in %s for prebuilt_apex", imageApexSuffix)
223 }
224 p.outputApex = android.PathForModuleOut(ctx, p.installFilename)
225 ctx.Build(pctx, android.BuildParams{
226 Rule: android.Cp,
227 Input: p.inputApex,
228 Output: p.outputApex,
229 })
Jiyong Park10e926b2020-07-16 21:38:56 +0900230
231 if p.prebuiltCommon.checkForceDisable(ctx) {
Colin Crossa9c8c9f2020-12-16 10:20:23 -0800232 p.HideFromMake()
Jiyong Park10e926b2020-07-16 21:38:56 +0900233 return
234 }
235
Jiyong Park09d77522019-11-18 11:16:27 +0900236 if p.installable() {
237 ctx.InstallFile(p.installDir, p.installFilename, p.inputApex)
238 }
239
Jooyung Han002ab682020-01-08 01:57:58 +0900240 // in case that prebuilt_apex replaces source apex (using prefer: prop)
241 p.compatSymlinks = makeCompatSymlinks(p.BaseModuleName(), ctx)
242 // or that prebuilt_apex overrides other apexes (using overrides: prop)
243 for _, overridden := range p.properties.Overrides {
244 p.compatSymlinks = append(p.compatSymlinks, makeCompatSymlinks(overridden, ctx)...)
245 }
Jiyong Park09d77522019-11-18 11:16:27 +0900246}
247
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900248func (p *Prebuilt) AndroidMkEntries() []android.AndroidMkEntries {
249 return []android.AndroidMkEntries{android.AndroidMkEntries{
Jiyong Park09d77522019-11-18 11:16:27 +0900250 Class: "ETC",
251 OutputFile: android.OptionalPathForPath(p.inputApex),
252 Include: "$(BUILD_PREBUILT)",
253 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
254 func(entries *android.AndroidMkEntries) {
255 entries.SetString("LOCAL_MODULE_PATH", p.installDir.ToMakePath().String())
256 entries.SetString("LOCAL_MODULE_STEM", p.installFilename)
257 entries.SetBoolIfTrue("LOCAL_UNINSTALLABLE_MODULE", !p.installable())
258 entries.AddStrings("LOCAL_OVERRIDES_MODULES", p.properties.Overrides...)
Jooyung Han002ab682020-01-08 01:57:58 +0900259 if len(p.compatSymlinks) > 0 {
260 entries.SetString("LOCAL_POST_INSTALL_CMD", strings.Join(p.compatSymlinks, " && "))
261 }
Jiyong Park09d77522019-11-18 11:16:27 +0900262 },
263 },
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900264 }}
Jiyong Park09d77522019-11-18 11:16:27 +0900265}
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700266
267type ApexSet struct {
268 android.ModuleBase
Jiyong Park10e926b2020-07-16 21:38:56 +0900269 prebuiltCommon
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700270
271 properties ApexSetProperties
272
273 installDir android.InstallPath
274 installFilename string
275 outputApex android.WritablePath
276
277 // list of commands to create symlinks for backward compatibility.
278 // these commands will be attached as LOCAL_POST_INSTALL_CMD
279 compatSymlinks []string
Jooyung Han29637162020-06-30 06:34:23 +0900280
281 hostRequired []string
282 postInstallCommands []string
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700283}
284
285type ApexSetProperties struct {
286 // the .apks file path that contains prebuilt apex files to be extracted.
287 Set *string
288
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700289 Sanitized struct {
290 None struct {
291 Set *string
292 }
293 Address struct {
294 Set *string
295 }
296 Hwaddress struct {
297 Set *string
298 }
299 }
300
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700301 // whether the extracted apex file installable.
302 Installable *bool
303
304 // optional name for the installed apex. If unspecified, name of the
305 // module is used as the file name
306 Filename *string
307
308 // names of modules to be overridden. Listed modules can only be other binaries
309 // (in Make or Soong).
310 // This does not completely prevent installation of the overridden binaries, but if both
311 // binaries would be installed by default (in PRODUCT_PACKAGES) the other binary will be removed
312 // from PRODUCT_PACKAGES.
313 Overrides []string
314
315 // apexes in this set use prerelease SDK version
316 Prerelease *bool
317}
318
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700319func (a *ApexSet) prebuiltSrcs(ctx android.BaseModuleContext) []string {
320 var srcs []string
321 if a.properties.Set != nil {
322 srcs = append(srcs, *a.properties.Set)
323 }
324
325 var sanitizers []string
326 if ctx.Host() {
327 sanitizers = ctx.Config().SanitizeHost()
328 } else {
329 sanitizers = ctx.Config().SanitizeDevice()
330 }
331
332 if android.InList("address", sanitizers) && a.properties.Sanitized.Address.Set != nil {
333 srcs = append(srcs, *a.properties.Sanitized.Address.Set)
334 } else if android.InList("hwaddress", sanitizers) && a.properties.Sanitized.Hwaddress.Set != nil {
335 srcs = append(srcs, *a.properties.Sanitized.Hwaddress.Set)
336 } else if a.properties.Sanitized.None.Set != nil {
337 srcs = append(srcs, *a.properties.Sanitized.None.Set)
338 }
339
340 return srcs
341}
342
343func (a *ApexSet) hasSanitizedSource(sanitizer string) bool {
344 if sanitizer == "address" {
345 return a.properties.Sanitized.Address.Set != nil
346 }
347 if sanitizer == "hwaddress" {
348 return a.properties.Sanitized.Hwaddress.Set != nil
349 }
350
351 return false
352}
353
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700354func (a *ApexSet) installable() bool {
355 return a.properties.Installable == nil || proptools.Bool(a.properties.Installable)
356}
357
358func (a *ApexSet) InstallFilename() string {
359 return proptools.StringDefault(a.properties.Filename, a.BaseModuleName()+imageApexSuffix)
360}
361
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700362func (a *ApexSet) Name() string {
Jiyong Park10e926b2020-07-16 21:38:56 +0900363 return a.prebuiltCommon.prebuilt.Name(a.ModuleBase.Name())
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700364}
365
Jiyong Park8d6c51e2020-06-12 17:26:31 +0900366func (a *ApexSet) Overrides() []string {
367 return a.properties.Overrides
368}
369
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700370// prebuilt_apex imports an `.apex` file into the build graph as if it was built with apex.
371func apexSetFactory() android.Module {
372 module := &ApexSet{}
373 module.AddProperties(&module.properties)
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700374
375 srcsSupplier := func(ctx android.BaseModuleContext) []string {
376 return module.prebuiltSrcs(ctx)
377 }
378
379 android.InitPrebuiltModuleWithSrcSupplier(module, srcsSupplier, "set")
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700380 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
381 return module
382}
383
384func (a *ApexSet) GenerateAndroidBuildActions(ctx android.ModuleContext) {
385 a.installFilename = a.InstallFilename()
386 if !strings.HasSuffix(a.installFilename, imageApexSuffix) {
387 ctx.ModuleErrorf("filename should end in %s for apex_set", imageApexSuffix)
388 }
389
Jiyong Park10e926b2020-07-16 21:38:56 +0900390 apexSet := a.prebuiltCommon.prebuilt.SingleSourcePath(ctx)
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700391 a.outputApex = android.PathForModuleOut(ctx, a.installFilename)
392 ctx.Build(pctx,
393 android.BuildParams{
394 Rule: extractMatchingApex,
395 Description: "Extract an apex from an apex set",
396 Inputs: android.Paths{apexSet},
397 Output: a.outputApex,
398 Args: map[string]string{
399 "abis": strings.Join(java.SupportedAbis(ctx), ","),
400 "allow-prereleased": strconv.FormatBool(proptools.Bool(a.properties.Prerelease)),
Dan Albert4f378d72020-07-23 17:32:15 -0700401 "sdk-version": ctx.Config().PlatformSdkVersion().String(),
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700402 },
403 })
Jiyong Park10e926b2020-07-16 21:38:56 +0900404
405 if a.prebuiltCommon.checkForceDisable(ctx) {
Colin Crossa9c8c9f2020-12-16 10:20:23 -0800406 a.HideFromMake()
Jiyong Park10e926b2020-07-16 21:38:56 +0900407 return
408 }
409
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700410 a.installDir = android.PathForModuleInstall(ctx, "apex")
411 if a.installable() {
412 ctx.InstallFile(a.installDir, a.installFilename, a.outputApex)
413 }
414
415 // in case that apex_set replaces source apex (using prefer: prop)
416 a.compatSymlinks = makeCompatSymlinks(a.BaseModuleName(), ctx)
417 // or that apex_set overrides other apexes (using overrides: prop)
418 for _, overridden := range a.properties.Overrides {
419 a.compatSymlinks = append(a.compatSymlinks, makeCompatSymlinks(overridden, ctx)...)
420 }
Jooyung Han29637162020-06-30 06:34:23 +0900421
422 if ctx.Config().InstallExtraFlattenedApexes() {
423 // flattened apex should be in /system_ext/apex
424 flattenedApexDir := android.PathForModuleInstall(&systemExtContext{ctx}, "apex", a.BaseModuleName())
425 a.postInstallCommands = append(a.postInstallCommands,
426 fmt.Sprintf("$(HOST_OUT_EXECUTABLES)/deapexer --debugfs_path $(HOST_OUT_EXECUTABLES)/debugfs extract %s %s",
427 a.outputApex.String(),
428 flattenedApexDir.ToMakePath().String(),
429 ))
430 a.hostRequired = []string{"deapexer", "debugfs"}
431 }
432}
433
434type systemExtContext struct {
435 android.ModuleContext
436}
437
438func (*systemExtContext) SystemExtSpecific() bool {
439 return true
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700440}
441
442func (a *ApexSet) AndroidMkEntries() []android.AndroidMkEntries {
443 return []android.AndroidMkEntries{android.AndroidMkEntries{
Jooyung Han29637162020-06-30 06:34:23 +0900444 Class: "ETC",
445 OutputFile: android.OptionalPathForPath(a.outputApex),
446 Include: "$(BUILD_PREBUILT)",
447 Host_required: a.hostRequired,
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700448 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
449 func(entries *android.AndroidMkEntries) {
450 entries.SetString("LOCAL_MODULE_PATH", a.installDir.ToMakePath().String())
451 entries.SetString("LOCAL_MODULE_STEM", a.installFilename)
452 entries.SetBoolIfTrue("LOCAL_UNINSTALLABLE_MODULE", !a.installable())
453 entries.AddStrings("LOCAL_OVERRIDES_MODULES", a.properties.Overrides...)
Jooyung Han29637162020-06-30 06:34:23 +0900454 postInstallCommands := append([]string{}, a.postInstallCommands...)
455 postInstallCommands = append(postInstallCommands, a.compatSymlinks...)
456 if len(postInstallCommands) > 0 {
457 entries.SetString("LOCAL_POST_INSTALL_CMD", strings.Join(postInstallCommands, " && "))
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700458 }
459 },
460 },
461 }}
462}