Paul Duffin | 3451e16 | 2021-01-20 15:16:56 +0000 | [diff] [blame] | 1 | // 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 | |
| 15 | package java |
| 16 | |
| 17 | import ( |
Paul Duffin | a1d6025 | 2021-01-21 18:13:43 +0000 | [diff] [blame] | 18 | "fmt" |
Paul Duffin | 3451e16 | 2021-01-20 15:16:56 +0000 | [diff] [blame] | 19 | "strings" |
| 20 | |
| 21 | "android/soong/android" |
Paul Duffin | a1d6025 | 2021-01-21 18:13:43 +0000 | [diff] [blame] | 22 | "android/soong/dexpreopt" |
Martin Stjernholm | b79c7f1 | 2021-03-17 00:26:25 +0000 | [diff] [blame] | 23 | |
Paul Duffin | 3451e16 | 2021-01-20 15:16:56 +0000 | [diff] [blame] | 24 | "github.com/google/blueprint" |
| 25 | ) |
| 26 | |
| 27 | func init() { |
| 28 | RegisterBootImageBuildComponents(android.InitRegistrationContext) |
Paul Duffin | f7f65da | 2021-03-10 15:00:46 +0000 | [diff] [blame] | 29 | |
Paul Duffin | 4b64ba0 | 2021-03-29 11:02:53 +0100 | [diff] [blame^] | 30 | // TODO(b/177892522): Remove after has been replaced by bootclasspath_fragments |
Paul Duffin | f7f65da | 2021-03-10 15:00:46 +0000 | [diff] [blame] | 31 | android.RegisterSdkMemberType(&bootImageMemberType{ |
| 32 | SdkMemberTypeBase: android.SdkMemberTypeBase{ |
| 33 | PropertyName: "boot_images", |
| 34 | SupportsSdk: true, |
| 35 | }, |
| 36 | }) |
Paul Duffin | 4b64ba0 | 2021-03-29 11:02:53 +0100 | [diff] [blame^] | 37 | |
| 38 | android.RegisterSdkMemberType(&bootImageMemberType{ |
| 39 | SdkMemberTypeBase: android.SdkMemberTypeBase{ |
| 40 | PropertyName: "bootclasspath_fragments", |
| 41 | SupportsSdk: true, |
| 42 | }, |
| 43 | }) |
Paul Duffin | 3451e16 | 2021-01-20 15:16:56 +0000 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | func RegisterBootImageBuildComponents(ctx android.RegistrationContext) { |
Paul Duffin | 4b64ba0 | 2021-03-29 11:02:53 +0100 | [diff] [blame^] | 47 | // TODO(b/177892522): Remove after has been replaced by bootclasspath_fragment |
Paul Duffin | 3451e16 | 2021-01-20 15:16:56 +0000 | [diff] [blame] | 48 | ctx.RegisterModuleType("boot_image", bootImageFactory) |
Paul Duffin | f7f65da | 2021-03-10 15:00:46 +0000 | [diff] [blame] | 49 | ctx.RegisterModuleType("prebuilt_boot_image", prebuiltBootImageFactory) |
Paul Duffin | 4b64ba0 | 2021-03-29 11:02:53 +0100 | [diff] [blame^] | 50 | |
| 51 | ctx.RegisterModuleType("bootclasspath_fragment", bootImageFactory) |
| 52 | ctx.RegisterModuleType("prebuilt_bootclasspath_fragment", prebuiltBootImageFactory) |
Paul Duffin | 3451e16 | 2021-01-20 15:16:56 +0000 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | type bootImageProperties struct { |
| 56 | // The name of the image this represents. |
| 57 | // |
| 58 | // Must be one of "art" or "boot". |
Paul Duffin | 64be7bb | 2021-03-23 23:06:38 +0000 | [diff] [blame] | 59 | Image_name *string |
Paul Duffin | 3451e16 | 2021-01-20 15:16:56 +0000 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | type BootImageModule struct { |
| 63 | android.ModuleBase |
Paul Duffin | a1d6025 | 2021-01-21 18:13:43 +0000 | [diff] [blame] | 64 | android.ApexModuleBase |
Paul Duffin | f7f65da | 2021-03-10 15:00:46 +0000 | [diff] [blame] | 65 | android.SdkBase |
Paul Duffin | 3451e16 | 2021-01-20 15:16:56 +0000 | [diff] [blame] | 66 | properties bootImageProperties |
| 67 | } |
| 68 | |
| 69 | func bootImageFactory() android.Module { |
| 70 | m := &BootImageModule{} |
| 71 | m.AddProperties(&m.properties) |
Paul Duffin | a1d6025 | 2021-01-21 18:13:43 +0000 | [diff] [blame] | 72 | android.InitApexModule(m) |
Paul Duffin | f7f65da | 2021-03-10 15:00:46 +0000 | [diff] [blame] | 73 | android.InitSdkAwareModule(m) |
Martin Stjernholm | b79c7f1 | 2021-03-17 00:26:25 +0000 | [diff] [blame] | 74 | android.InitAndroidArchModule(m, android.HostAndDeviceSupported, android.MultilibCommon) |
Paul Duffin | 3451e16 | 2021-01-20 15:16:56 +0000 | [diff] [blame] | 75 | return m |
| 76 | } |
| 77 | |
| 78 | var BootImageInfoProvider = blueprint.NewProvider(BootImageInfo{}) |
| 79 | |
| 80 | type BootImageInfo struct { |
| 81 | // The image config, internal to this module (and the dex_bootjars singleton). |
Paul Duffin | a1d6025 | 2021-01-21 18:13:43 +0000 | [diff] [blame] | 82 | // |
| 83 | // Will be nil if the BootImageInfo has not been provided for a specific module. That can occur |
| 84 | // when SkipDexpreoptBootJars(ctx) returns true. |
Paul Duffin | 3451e16 | 2021-01-20 15:16:56 +0000 | [diff] [blame] | 85 | imageConfig *bootImageConfig |
| 86 | } |
| 87 | |
| 88 | func (i BootImageInfo) Modules() android.ConfiguredJarList { |
| 89 | return i.imageConfig.modules |
| 90 | } |
| 91 | |
Paul Duffin | a1d6025 | 2021-01-21 18:13:43 +0000 | [diff] [blame] | 92 | // Get a map from ArchType to the associated boot image's contents for Android. |
| 93 | // |
| 94 | // Extension boot images only return their own files, not the files of the boot images they extend. |
| 95 | func (i BootImageInfo) AndroidBootImageFilesByArchType() map[android.ArchType]android.OutputPaths { |
| 96 | files := map[android.ArchType]android.OutputPaths{} |
| 97 | if i.imageConfig != nil { |
| 98 | for _, variant := range i.imageConfig.variants { |
| 99 | // We also generate boot images for host (for testing), but we don't need those in the apex. |
| 100 | // TODO(b/177892522) - consider changing this to check Os.OsClass = android.Device |
| 101 | if variant.target.Os == android.Android { |
| 102 | files[variant.target.Arch.ArchType] = variant.imagesDeps |
| 103 | } |
| 104 | } |
| 105 | } |
| 106 | return files |
| 107 | } |
| 108 | |
| 109 | func (b *BootImageModule) DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool { |
| 110 | tag := ctx.OtherModuleDependencyTag(dep) |
Bob Badour | 07065cd | 2021-02-05 19:59:11 -0800 | [diff] [blame] | 111 | if android.IsMetaDependencyTag(tag) { |
| 112 | // Cross-cutting metadata dependencies are metadata. |
| 113 | return false |
| 114 | } |
Paul Duffin | a1d6025 | 2021-01-21 18:13:43 +0000 | [diff] [blame] | 115 | panic(fmt.Errorf("boot_image module %q should not have a dependency on %q via tag %s", b, dep, android.PrettyPrintTag(tag))) |
| 116 | } |
| 117 | |
| 118 | func (b *BootImageModule) ShouldSupportSdkVersion(ctx android.BaseModuleContext, sdkVersion android.ApiLevel) error { |
| 119 | return nil |
| 120 | } |
| 121 | |
| 122 | func (b *BootImageModule) DepsMutator(ctx android.BottomUpMutatorContext) { |
| 123 | if SkipDexpreoptBootJars(ctx) { |
| 124 | return |
| 125 | } |
| 126 | |
| 127 | // Add a dependency onto the dex2oat tool which is needed for creating the boot image. The |
| 128 | // path is retrieved from the dependency by GetGlobalSoongConfig(ctx). |
| 129 | dexpreopt.RegisterToolDeps(ctx) |
| 130 | } |
| 131 | |
Paul Duffin | 3451e16 | 2021-01-20 15:16:56 +0000 | [diff] [blame] | 132 | func (b *BootImageModule) GenerateAndroidBuildActions(ctx android.ModuleContext) { |
| 133 | // Nothing to do if skipping the dexpreopt of boot image jars. |
| 134 | if SkipDexpreoptBootJars(ctx) { |
| 135 | return |
| 136 | } |
| 137 | |
Paul Duffin | a1d6025 | 2021-01-21 18:13:43 +0000 | [diff] [blame] | 138 | // Force the GlobalSoongConfig to be created and cached for use by the dex_bootjars |
| 139 | // GenerateSingletonBuildActions method as it cannot create it for itself. |
| 140 | dexpreopt.GetGlobalSoongConfig(ctx) |
| 141 | |
Paul Duffin | 64be7bb | 2021-03-23 23:06:38 +0000 | [diff] [blame] | 142 | imageConfig := b.getImageConfig(ctx) |
Paul Duffin | 3451e16 | 2021-01-20 15:16:56 +0000 | [diff] [blame] | 143 | if imageConfig == nil { |
Paul Duffin | 3451e16 | 2021-01-20 15:16:56 +0000 | [diff] [blame] | 144 | return |
| 145 | } |
| 146 | |
| 147 | // Construct the boot image info from the config. |
| 148 | info := BootImageInfo{imageConfig: imageConfig} |
| 149 | |
| 150 | // Make it available for other modules. |
| 151 | ctx.SetProvider(BootImageInfoProvider, info) |
| 152 | } |
Paul Duffin | f7f65da | 2021-03-10 15:00:46 +0000 | [diff] [blame] | 153 | |
Paul Duffin | 64be7bb | 2021-03-23 23:06:38 +0000 | [diff] [blame] | 154 | func (b *BootImageModule) getImageConfig(ctx android.EarlyModuleContext) *bootImageConfig { |
| 155 | // Get a map of the image configs that are supported. |
| 156 | imageConfigs := genBootImageConfigs(ctx) |
| 157 | |
| 158 | // Retrieve the config for this image. |
| 159 | imageNamePtr := b.properties.Image_name |
| 160 | if imageNamePtr == nil { |
| 161 | return nil |
| 162 | } |
| 163 | |
| 164 | imageName := *imageNamePtr |
| 165 | imageConfig := imageConfigs[imageName] |
| 166 | if imageConfig == nil { |
| 167 | ctx.PropertyErrorf("image_name", "Unknown image name %q, expected one of %s", imageName, strings.Join(android.SortedStringKeys(imageConfigs), ", ")) |
| 168 | return nil |
| 169 | } |
| 170 | return imageConfig |
| 171 | } |
| 172 | |
Paul Duffin | f7f65da | 2021-03-10 15:00:46 +0000 | [diff] [blame] | 173 | type bootImageMemberType struct { |
| 174 | android.SdkMemberTypeBase |
| 175 | } |
| 176 | |
| 177 | func (b *bootImageMemberType) AddDependencies(mctx android.BottomUpMutatorContext, dependencyTag blueprint.DependencyTag, names []string) { |
| 178 | mctx.AddVariationDependencies(nil, dependencyTag, names...) |
| 179 | } |
| 180 | |
| 181 | func (b *bootImageMemberType) IsInstance(module android.Module) bool { |
| 182 | _, ok := module.(*BootImageModule) |
| 183 | return ok |
| 184 | } |
| 185 | |
| 186 | func (b *bootImageMemberType) AddPrebuiltModule(ctx android.SdkMemberContext, member android.SdkMember) android.BpModule { |
Paul Duffin | 4b64ba0 | 2021-03-29 11:02:53 +0100 | [diff] [blame^] | 187 | if b.PropertyName == "boot_images" { |
| 188 | return ctx.SnapshotBuilder().AddPrebuiltModule(member, "prebuilt_boot_image") |
| 189 | } else { |
| 190 | return ctx.SnapshotBuilder().AddPrebuiltModule(member, "prebuilt_bootclasspath_fragment") |
| 191 | } |
Paul Duffin | f7f65da | 2021-03-10 15:00:46 +0000 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | func (b *bootImageMemberType) CreateVariantPropertiesStruct() android.SdkMemberProperties { |
| 195 | return &bootImageSdkMemberProperties{} |
| 196 | } |
| 197 | |
| 198 | type bootImageSdkMemberProperties struct { |
| 199 | android.SdkMemberPropertiesBase |
| 200 | |
Paul Duffin | 64be7bb | 2021-03-23 23:06:38 +0000 | [diff] [blame] | 201 | Image_name *string |
Paul Duffin | f7f65da | 2021-03-10 15:00:46 +0000 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | func (b *bootImageSdkMemberProperties) PopulateFromVariant(ctx android.SdkMemberContext, variant android.Module) { |
| 205 | module := variant.(*BootImageModule) |
| 206 | |
| 207 | b.Image_name = module.properties.Image_name |
| 208 | } |
| 209 | |
| 210 | func (b *bootImageSdkMemberProperties) AddToPropertySet(ctx android.SdkMemberContext, propertySet android.BpPropertySet) { |
Paul Duffin | 64be7bb | 2021-03-23 23:06:38 +0000 | [diff] [blame] | 211 | if b.Image_name != nil { |
| 212 | propertySet.AddProperty("image_name", *b.Image_name) |
Paul Duffin | f7f65da | 2021-03-10 15:00:46 +0000 | [diff] [blame] | 213 | } |
| 214 | } |
| 215 | |
| 216 | var _ android.SdkMemberType = (*bootImageMemberType)(nil) |
| 217 | |
| 218 | // A prebuilt version of the boot image module. |
| 219 | // |
| 220 | // At the moment this is basically just a boot image module that can be used as a prebuilt. |
| 221 | // Eventually as more functionality is migrated into the boot image module from the singleton then |
| 222 | // this will diverge. |
| 223 | type prebuiltBootImageModule struct { |
| 224 | BootImageModule |
| 225 | prebuilt android.Prebuilt |
| 226 | } |
| 227 | |
| 228 | func (module *prebuiltBootImageModule) Prebuilt() *android.Prebuilt { |
| 229 | return &module.prebuilt |
| 230 | } |
| 231 | |
| 232 | func (module *prebuiltBootImageModule) Name() string { |
| 233 | return module.prebuilt.Name(module.ModuleBase.Name()) |
| 234 | } |
| 235 | |
| 236 | func prebuiltBootImageFactory() android.Module { |
| 237 | m := &prebuiltBootImageModule{} |
| 238 | m.AddProperties(&m.properties) |
Paul Duffin | f7f65da | 2021-03-10 15:00:46 +0000 | [diff] [blame] | 239 | // This doesn't actually have any prebuilt files of its own so pass a placeholder for the srcs |
| 240 | // array. |
| 241 | android.InitPrebuiltModule(m, &[]string{"placeholder"}) |
| 242 | android.InitApexModule(m) |
| 243 | android.InitSdkAwareModule(m) |
Martin Stjernholm | b79c7f1 | 2021-03-17 00:26:25 +0000 | [diff] [blame] | 244 | android.InitAndroidArchModule(m, android.HostAndDeviceSupported, android.MultilibCommon) |
Paul Duffin | f7f65da | 2021-03-10 15:00:46 +0000 | [diff] [blame] | 245 | return m |
| 246 | } |