blob: 8a1e3c957e9dfa97e09a1fa218407f8249e53e5f [file] [log] [blame]
Paul Duffin3451e162021-01-20 15:16:56 +00001// Copyright (C) 2021 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 java
16
17import (
Paul Duffina1d60252021-01-21 18:13:43 +000018 "fmt"
Paul Duffin3451e162021-01-20 15:16:56 +000019 "strings"
20
21 "android/soong/android"
Paul Duffina1d60252021-01-21 18:13:43 +000022 "android/soong/dexpreopt"
Paul Duffin3451e162021-01-20 15:16:56 +000023 "github.com/google/blueprint"
24)
25
26func init() {
27 RegisterBootImageBuildComponents(android.InitRegistrationContext)
28}
29
30func RegisterBootImageBuildComponents(ctx android.RegistrationContext) {
31 ctx.RegisterModuleType("boot_image", bootImageFactory)
32}
33
34type bootImageProperties struct {
35 // The name of the image this represents.
36 //
37 // Must be one of "art" or "boot".
38 Image_name string
39}
40
41type BootImageModule struct {
42 android.ModuleBase
Paul Duffina1d60252021-01-21 18:13:43 +000043 android.ApexModuleBase
Paul Duffin3451e162021-01-20 15:16:56 +000044
45 properties bootImageProperties
46}
47
48func bootImageFactory() android.Module {
49 m := &BootImageModule{}
50 m.AddProperties(&m.properties)
Paul Duffin5bbfef82021-01-30 12:57:26 +000051 android.InitAndroidArchModule(m, android.HostAndDeviceSupported, android.MultilibCommon)
Paul Duffina1d60252021-01-21 18:13:43 +000052 android.InitApexModule(m)
Paul Duffin3451e162021-01-20 15:16:56 +000053 return m
54}
55
56var BootImageInfoProvider = blueprint.NewProvider(BootImageInfo{})
57
58type BootImageInfo struct {
59 // The image config, internal to this module (and the dex_bootjars singleton).
Paul Duffina1d60252021-01-21 18:13:43 +000060 //
61 // Will be nil if the BootImageInfo has not been provided for a specific module. That can occur
62 // when SkipDexpreoptBootJars(ctx) returns true.
Paul Duffin3451e162021-01-20 15:16:56 +000063 imageConfig *bootImageConfig
64}
65
66func (i BootImageInfo) Modules() android.ConfiguredJarList {
67 return i.imageConfig.modules
68}
69
Paul Duffina1d60252021-01-21 18:13:43 +000070// Get a map from ArchType to the associated boot image's contents for Android.
71//
72// Extension boot images only return their own files, not the files of the boot images they extend.
73func (i BootImageInfo) AndroidBootImageFilesByArchType() map[android.ArchType]android.OutputPaths {
74 files := map[android.ArchType]android.OutputPaths{}
75 if i.imageConfig != nil {
76 for _, variant := range i.imageConfig.variants {
77 // We also generate boot images for host (for testing), but we don't need those in the apex.
78 // TODO(b/177892522) - consider changing this to check Os.OsClass = android.Device
79 if variant.target.Os == android.Android {
80 files[variant.target.Arch.ArchType] = variant.imagesDeps
81 }
82 }
83 }
84 return files
85}
86
87func (b *BootImageModule) DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool {
88 tag := ctx.OtherModuleDependencyTag(dep)
89 if tag == dexpreopt.Dex2oatDepTag {
90 // The dex2oat tool is only needed for building and is not required in the apex.
91 return false
92 }
Bob Badour07065cd2021-02-05 19:59:11 -080093 if android.IsMetaDependencyTag(tag) {
94 // Cross-cutting metadata dependencies are metadata.
95 return false
96 }
Paul Duffina1d60252021-01-21 18:13:43 +000097 panic(fmt.Errorf("boot_image module %q should not have a dependency on %q via tag %s", b, dep, android.PrettyPrintTag(tag)))
98}
99
100func (b *BootImageModule) ShouldSupportSdkVersion(ctx android.BaseModuleContext, sdkVersion android.ApiLevel) error {
101 return nil
102}
103
104func (b *BootImageModule) DepsMutator(ctx android.BottomUpMutatorContext) {
105 if SkipDexpreoptBootJars(ctx) {
106 return
107 }
108
109 // Add a dependency onto the dex2oat tool which is needed for creating the boot image. The
110 // path is retrieved from the dependency by GetGlobalSoongConfig(ctx).
111 dexpreopt.RegisterToolDeps(ctx)
112}
113
Paul Duffin3451e162021-01-20 15:16:56 +0000114func (b *BootImageModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
115 // Nothing to do if skipping the dexpreopt of boot image jars.
116 if SkipDexpreoptBootJars(ctx) {
117 return
118 }
119
Paul Duffina1d60252021-01-21 18:13:43 +0000120 // Force the GlobalSoongConfig to be created and cached for use by the dex_bootjars
121 // GenerateSingletonBuildActions method as it cannot create it for itself.
122 dexpreopt.GetGlobalSoongConfig(ctx)
123
Paul Duffin3451e162021-01-20 15:16:56 +0000124 // Get a map of the image configs that are supported.
125 imageConfigs := genBootImageConfigs(ctx)
126
127 // Retrieve the config for this image.
128 imageName := b.properties.Image_name
129 imageConfig := imageConfigs[imageName]
130 if imageConfig == nil {
131 ctx.PropertyErrorf("image_name", "Unknown image name %q, expected one of %s", imageName, strings.Join(android.SortedStringKeys(imageConfigs), ", "))
132 return
133 }
134
135 // Construct the boot image info from the config.
136 info := BootImageInfo{imageConfig: imageConfig}
137
138 // Make it available for other modules.
139 ctx.SetProvider(BootImageInfoProvider, info)
140}