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" |
Paul Duffin | 3451e16 | 2021-01-20 15:16:56 +0000 | [diff] [blame] | 23 | "github.com/google/blueprint" |
| 24 | ) |
| 25 | |
| 26 | func init() { |
| 27 | RegisterBootImageBuildComponents(android.InitRegistrationContext) |
| 28 | } |
| 29 | |
| 30 | func RegisterBootImageBuildComponents(ctx android.RegistrationContext) { |
| 31 | ctx.RegisterModuleType("boot_image", bootImageFactory) |
| 32 | } |
| 33 | |
| 34 | type 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 | |
| 41 | type BootImageModule struct { |
| 42 | android.ModuleBase |
Paul Duffin | a1d6025 | 2021-01-21 18:13:43 +0000 | [diff] [blame] | 43 | android.ApexModuleBase |
Paul Duffin | 3451e16 | 2021-01-20 15:16:56 +0000 | [diff] [blame] | 44 | |
| 45 | properties bootImageProperties |
| 46 | } |
| 47 | |
| 48 | func bootImageFactory() android.Module { |
| 49 | m := &BootImageModule{} |
| 50 | m.AddProperties(&m.properties) |
Paul Duffin | 5bbfef8 | 2021-01-30 12:57:26 +0000 | [diff] [blame] | 51 | android.InitAndroidArchModule(m, android.HostAndDeviceSupported, android.MultilibCommon) |
Paul Duffin | a1d6025 | 2021-01-21 18:13:43 +0000 | [diff] [blame] | 52 | android.InitApexModule(m) |
Paul Duffin | 3451e16 | 2021-01-20 15:16:56 +0000 | [diff] [blame] | 53 | return m |
| 54 | } |
| 55 | |
| 56 | var BootImageInfoProvider = blueprint.NewProvider(BootImageInfo{}) |
| 57 | |
| 58 | type BootImageInfo struct { |
| 59 | // The image config, internal to this module (and the dex_bootjars singleton). |
Paul Duffin | a1d6025 | 2021-01-21 18:13:43 +0000 | [diff] [blame] | 60 | // |
| 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 Duffin | 3451e16 | 2021-01-20 15:16:56 +0000 | [diff] [blame] | 63 | imageConfig *bootImageConfig |
| 64 | } |
| 65 | |
| 66 | func (i BootImageInfo) Modules() android.ConfiguredJarList { |
| 67 | return i.imageConfig.modules |
| 68 | } |
| 69 | |
Paul Duffin | a1d6025 | 2021-01-21 18:13:43 +0000 | [diff] [blame] | 70 | // 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. |
| 73 | func (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 | |
| 87 | func (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 Badour | 07065cd | 2021-02-05 19:59:11 -0800 | [diff] [blame^] | 93 | if android.IsMetaDependencyTag(tag) { |
| 94 | // Cross-cutting metadata dependencies are metadata. |
| 95 | return false |
| 96 | } |
Paul Duffin | a1d6025 | 2021-01-21 18:13:43 +0000 | [diff] [blame] | 97 | panic(fmt.Errorf("boot_image module %q should not have a dependency on %q via tag %s", b, dep, android.PrettyPrintTag(tag))) |
| 98 | } |
| 99 | |
| 100 | func (b *BootImageModule) ShouldSupportSdkVersion(ctx android.BaseModuleContext, sdkVersion android.ApiLevel) error { |
| 101 | return nil |
| 102 | } |
| 103 | |
| 104 | func (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 Duffin | 3451e16 | 2021-01-20 15:16:56 +0000 | [diff] [blame] | 114 | func (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 Duffin | a1d6025 | 2021-01-21 18:13:43 +0000 | [diff] [blame] | 120 | // 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 Duffin | 3451e16 | 2021-01-20 15:16:56 +0000 | [diff] [blame] | 124 | // 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 | } |