blob: 6f50c27e648dfd130bd5ed9eea240509fe072fed [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 Duffin7c955552021-04-19 13:23:53 +010019 "path/filepath"
Paul Duffin3451e162021-01-20 15:16:56 +000020 "strings"
21
22 "android/soong/android"
Paul Duffina1d60252021-01-21 18:13:43 +000023 "android/soong/dexpreopt"
Paul Duffinc7ef9892021-03-23 23:21:59 +000024 "github.com/google/blueprint/proptools"
Martin Stjernholmb79c7f12021-03-17 00:26:25 +000025
Paul Duffin3451e162021-01-20 15:16:56 +000026 "github.com/google/blueprint"
27)
28
29func init() {
30 RegisterBootImageBuildComponents(android.InitRegistrationContext)
Paul Duffinf7f65da2021-03-10 15:00:46 +000031
Paul Duffin4b64ba02021-03-29 11:02:53 +010032 // TODO(b/177892522): Remove after has been replaced by bootclasspath_fragments
Paul Duffinf7f65da2021-03-10 15:00:46 +000033 android.RegisterSdkMemberType(&bootImageMemberType{
34 SdkMemberTypeBase: android.SdkMemberTypeBase{
35 PropertyName: "boot_images",
36 SupportsSdk: true,
37 },
38 })
Paul Duffin4b64ba02021-03-29 11:02:53 +010039
40 android.RegisterSdkMemberType(&bootImageMemberType{
41 SdkMemberTypeBase: android.SdkMemberTypeBase{
42 PropertyName: "bootclasspath_fragments",
43 SupportsSdk: true,
44 },
45 })
Paul Duffin3451e162021-01-20 15:16:56 +000046}
47
48func RegisterBootImageBuildComponents(ctx android.RegistrationContext) {
Paul Duffin4b64ba02021-03-29 11:02:53 +010049 // TODO(b/177892522): Remove after has been replaced by bootclasspath_fragment
Paul Duffin3451e162021-01-20 15:16:56 +000050 ctx.RegisterModuleType("boot_image", bootImageFactory)
Paul Duffinf7f65da2021-03-10 15:00:46 +000051 ctx.RegisterModuleType("prebuilt_boot_image", prebuiltBootImageFactory)
Paul Duffin4b64ba02021-03-29 11:02:53 +010052
53 ctx.RegisterModuleType("bootclasspath_fragment", bootImageFactory)
54 ctx.RegisterModuleType("prebuilt_bootclasspath_fragment", prebuiltBootImageFactory)
Paul Duffin3451e162021-01-20 15:16:56 +000055}
56
Paul Duffin65898052021-04-20 22:47:03 +010057type bootclasspathFragmentContentDependencyTag struct {
Paul Duffinc7ef9892021-03-23 23:21:59 +000058 blueprint.BaseDependencyTag
59}
60
61// Avoid having to make boot image content visible to the boot image.
62//
63// This is a temporary workaround to make it easier to migrate to boot image modules with proper
64// dependencies.
65// TODO(b/177892522): Remove this and add needed visibility.
Paul Duffin65898052021-04-20 22:47:03 +010066func (b bootclasspathFragmentContentDependencyTag) ExcludeFromVisibilityEnforcement() {
67}
68
69// The bootclasspath_fragment contents must never depend on prebuilts.
70func (b bootclasspathFragmentContentDependencyTag) ReplaceSourceWithPrebuilt() bool {
71 return false
Paul Duffinc7ef9892021-03-23 23:21:59 +000072}
73
74// The tag used for the dependency between the boot image module and its contents.
Paul Duffin65898052021-04-20 22:47:03 +010075var bootclasspathFragmentContentDepTag = bootclasspathFragmentContentDependencyTag{}
Paul Duffinc7ef9892021-03-23 23:21:59 +000076
Paul Duffin65898052021-04-20 22:47:03 +010077var _ android.ExcludeFromVisibilityEnforcementTag = bootclasspathFragmentContentDepTag
78var _ android.ReplaceSourceWithPrebuilt = bootclasspathFragmentContentDepTag
Paul Duffinc7ef9892021-03-23 23:21:59 +000079
Paul Duffin65898052021-04-20 22:47:03 +010080func IsBootclasspathFragmentContentDepTag(tag blueprint.DependencyTag) bool {
81 return tag == bootclasspathFragmentContentDepTag
Paul Duffin4d101b62021-03-24 15:42:20 +000082}
83
Paul Duffin3451e162021-01-20 15:16:56 +000084type bootImageProperties struct {
85 // The name of the image this represents.
86 //
Paul Duffin82886d62021-03-24 01:34:57 +000087 // If specified then it must be one of "art" or "boot".
Paul Duffin64be7bb2021-03-23 23:06:38 +000088 Image_name *string
Paul Duffinc7ef9892021-03-23 23:21:59 +000089
90 // The contents of this boot image, could be either java_library, java_sdk_library, or boot_image.
91 //
92 // The order of this list matters as it is the order that is used in the bootclasspath.
Paul Duffin82886d62021-03-24 01:34:57 +000093 Contents []string
Paul Duffin9b381ef2021-04-08 23:01:37 +010094
95 Hidden_api HiddenAPIFlagFileProperties
Paul Duffin3451e162021-01-20 15:16:56 +000096}
97
98type BootImageModule struct {
99 android.ModuleBase
Paul Duffina1d60252021-01-21 18:13:43 +0000100 android.ApexModuleBase
Paul Duffinf7f65da2021-03-10 15:00:46 +0000101 android.SdkBase
Paul Duffin3451e162021-01-20 15:16:56 +0000102 properties bootImageProperties
103}
104
105func bootImageFactory() android.Module {
106 m := &BootImageModule{}
107 m.AddProperties(&m.properties)
Paul Duffina1d60252021-01-21 18:13:43 +0000108 android.InitApexModule(m)
Paul Duffinf7f65da2021-03-10 15:00:46 +0000109 android.InitSdkAwareModule(m)
Martin Stjernholmb79c7f12021-03-17 00:26:25 +0000110 android.InitAndroidArchModule(m, android.HostAndDeviceSupported, android.MultilibCommon)
Paul Duffinc7ef9892021-03-23 23:21:59 +0000111
112 // Perform some consistency checking to ensure that the configuration is correct.
113 android.AddLoadHook(m, func(ctx android.LoadHookContext) {
114 bootImageConsistencyCheck(ctx, m)
115 })
Paul Duffin3451e162021-01-20 15:16:56 +0000116 return m
117}
118
Paul Duffinc7ef9892021-03-23 23:21:59 +0000119func bootImageConsistencyCheck(ctx android.EarlyModuleContext, m *BootImageModule) {
Paul Duffin82886d62021-03-24 01:34:57 +0000120 contents := m.properties.Contents
121 if m.properties.Image_name == nil && len(contents) == 0 {
122 ctx.ModuleErrorf(`neither of the "image_name" and "contents" properties have been supplied, please supply exactly one`)
123 }
124 if m.properties.Image_name != nil && len(contents) != 0 {
125 ctx.ModuleErrorf(`both of the "image_name" and "contents" properties have been supplied, please supply exactly one`)
126 }
Paul Duffin023dba02021-04-22 01:45:29 +0100127
Paul Duffinc7ef9892021-03-23 23:21:59 +0000128 imageName := proptools.String(m.properties.Image_name)
129 if imageName == "art" {
Paul Duffin023dba02021-04-22 01:45:29 +0100130 // TODO(b/177892522): Prebuilts (versioned or not) should not use the image_name property.
131 if m.MemberName() != "" {
132 // The module is a versioned prebuilt so ignore it. This is done for a couple of reasons:
133 // 1. There is no way to use this at the moment so ignoring it is safe.
134 // 2. Attempting to initialize the contents property from the configuration will end up having
135 // the versioned prebuilt depending on the unversioned prebuilt. That will cause problems
136 // as the unversioned prebuilt could end up with an APEX variant created for the source
137 // APEX which will prevent it from having an APEX variant for the prebuilt APEX which in
138 // turn will prevent it from accessing the dex implementation jar from that which will
139 // break hidden API processing, amongst others.
140 return
141 }
142
Paul Duffinc7ef9892021-03-23 23:21:59 +0000143 // Get the configuration for the art apex jars. Do not use getImageConfig(ctx) here as this is
144 // too early in the Soong processing for that to work.
145 global := dexpreopt.GetGlobalConfig(ctx)
146 modules := global.ArtApexJars
147
148 // Make sure that the apex specified in the configuration is consistent and is one for which
149 // this boot image is available.
150 jars := []string{}
151 commonApex := ""
152 for i := 0; i < modules.Len(); i++ {
153 apex := modules.Apex(i)
154 jar := modules.Jar(i)
155 if apex == "platform" {
156 ctx.ModuleErrorf("ArtApexJars is invalid as it requests a platform variant of %q", jar)
157 continue
158 }
159 if !m.AvailableFor(apex) {
160 ctx.ModuleErrorf("incompatible with ArtApexJars which expects this to be in apex %q but this is only in apexes %q",
161 apex, m.ApexAvailable())
162 continue
163 }
164 if commonApex == "" {
165 commonApex = apex
166 } else if commonApex != apex {
167 ctx.ModuleErrorf("ArtApexJars configuration is inconsistent, expected all jars to be in the same apex but it specifies apex %q and %q",
168 commonApex, apex)
169 }
170 jars = append(jars, jar)
171 }
172
173 // Store the jars in the Contents property so that they can be used to add dependencies.
174 m.properties.Contents = jars
175 }
176}
177
Paul Duffin3451e162021-01-20 15:16:56 +0000178var BootImageInfoProvider = blueprint.NewProvider(BootImageInfo{})
179
180type BootImageInfo struct {
181 // The image config, internal to this module (and the dex_bootjars singleton).
Paul Duffina1d60252021-01-21 18:13:43 +0000182 //
183 // Will be nil if the BootImageInfo has not been provided for a specific module. That can occur
184 // when SkipDexpreoptBootJars(ctx) returns true.
Paul Duffin3451e162021-01-20 15:16:56 +0000185 imageConfig *bootImageConfig
186}
187
188func (i BootImageInfo) Modules() android.ConfiguredJarList {
189 return i.imageConfig.modules
190}
191
Paul Duffina1d60252021-01-21 18:13:43 +0000192// Get a map from ArchType to the associated boot image's contents for Android.
193//
194// Extension boot images only return their own files, not the files of the boot images they extend.
195func (i BootImageInfo) AndroidBootImageFilesByArchType() map[android.ArchType]android.OutputPaths {
196 files := map[android.ArchType]android.OutputPaths{}
197 if i.imageConfig != nil {
198 for _, variant := range i.imageConfig.variants {
199 // We also generate boot images for host (for testing), but we don't need those in the apex.
200 // TODO(b/177892522) - consider changing this to check Os.OsClass = android.Device
201 if variant.target.Os == android.Android {
202 files[variant.target.Arch.ArchType] = variant.imagesDeps
203 }
204 }
205 }
206 return files
207}
208
209func (b *BootImageModule) DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool {
210 tag := ctx.OtherModuleDependencyTag(dep)
Paul Duffin65898052021-04-20 22:47:03 +0100211 if IsBootclasspathFragmentContentDepTag(tag) {
Paul Duffin4d101b62021-03-24 15:42:20 +0000212 // Boot image contents are automatically added to apex.
213 return true
Paul Duffinc7ef9892021-03-23 23:21:59 +0000214 }
Bob Badour07065cd2021-02-05 19:59:11 -0800215 if android.IsMetaDependencyTag(tag) {
216 // Cross-cutting metadata dependencies are metadata.
217 return false
218 }
Paul Duffina1d60252021-01-21 18:13:43 +0000219 panic(fmt.Errorf("boot_image module %q should not have a dependency on %q via tag %s", b, dep, android.PrettyPrintTag(tag)))
220}
221
222func (b *BootImageModule) ShouldSupportSdkVersion(ctx android.BaseModuleContext, sdkVersion android.ApiLevel) error {
223 return nil
224}
225
Paul Duffin65898052021-04-20 22:47:03 +0100226// ComponentDepsMutator adds dependencies onto modules before any prebuilt modules without a
227// corresponding source module are renamed. This means that adding a dependency using a name without
228// a prebuilt_ prefix will always resolve to a source module and when using a name with that prefix
229// it will always resolve to a prebuilt module.
230func (b *BootImageModule) ComponentDepsMutator(ctx android.BottomUpMutatorContext) {
231 module := ctx.Module()
232 _, isSourceModule := module.(*BootImageModule)
233
234 for _, name := range b.properties.Contents {
235 // A bootclasspath_fragment must depend only on other source modules, while the
236 // prebuilt_bootclasspath_fragment must only depend on other prebuilt modules.
Paul Duffina9dd6fa2021-04-22 17:25:57 +0100237 //
238 // TODO(b/177892522) - avoid special handling of jacocoagent.
239 if !isSourceModule && name != "jacocoagent" {
Paul Duffin65898052021-04-20 22:47:03 +0100240 name = android.PrebuiltNameFromSource(name)
241 }
242 ctx.AddDependency(module, bootclasspathFragmentContentDepTag, name)
243 }
244
245}
246
Paul Duffina1d60252021-01-21 18:13:43 +0000247func (b *BootImageModule) DepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffinc7ef9892021-03-23 23:21:59 +0000248
Paul Duffina1d60252021-01-21 18:13:43 +0000249 if SkipDexpreoptBootJars(ctx) {
250 return
251 }
252
253 // Add a dependency onto the dex2oat tool which is needed for creating the boot image. The
254 // path is retrieved from the dependency by GetGlobalSoongConfig(ctx).
255 dexpreopt.RegisterToolDeps(ctx)
256}
257
Paul Duffin3451e162021-01-20 15:16:56 +0000258func (b *BootImageModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Paul Duffin9b381ef2021-04-08 23:01:37 +0100259 // Perform hidden API processing.
260 b.generateHiddenAPIBuildActions(ctx)
261
Paul Duffin3451e162021-01-20 15:16:56 +0000262 // Nothing to do if skipping the dexpreopt of boot image jars.
263 if SkipDexpreoptBootJars(ctx) {
264 return
265 }
266
Paul Duffina1d60252021-01-21 18:13:43 +0000267 // Force the GlobalSoongConfig to be created and cached for use by the dex_bootjars
268 // GenerateSingletonBuildActions method as it cannot create it for itself.
269 dexpreopt.GetGlobalSoongConfig(ctx)
270
Paul Duffin64be7bb2021-03-23 23:06:38 +0000271 imageConfig := b.getImageConfig(ctx)
Paul Duffin3451e162021-01-20 15:16:56 +0000272 if imageConfig == nil {
Paul Duffin3451e162021-01-20 15:16:56 +0000273 return
274 }
275
276 // Construct the boot image info from the config.
277 info := BootImageInfo{imageConfig: imageConfig}
278
279 // Make it available for other modules.
280 ctx.SetProvider(BootImageInfoProvider, info)
281}
Paul Duffinf7f65da2021-03-10 15:00:46 +0000282
Paul Duffin64be7bb2021-03-23 23:06:38 +0000283func (b *BootImageModule) getImageConfig(ctx android.EarlyModuleContext) *bootImageConfig {
284 // Get a map of the image configs that are supported.
285 imageConfigs := genBootImageConfigs(ctx)
286
287 // Retrieve the config for this image.
288 imageNamePtr := b.properties.Image_name
289 if imageNamePtr == nil {
290 return nil
291 }
292
293 imageName := *imageNamePtr
294 imageConfig := imageConfigs[imageName]
295 if imageConfig == nil {
296 ctx.PropertyErrorf("image_name", "Unknown image name %q, expected one of %s", imageName, strings.Join(android.SortedStringKeys(imageConfigs), ", "))
297 return nil
298 }
299 return imageConfig
300}
301
Paul Duffin9b381ef2021-04-08 23:01:37 +0100302// generateHiddenAPIBuildActions generates all the hidden API related build rules.
303func (b *BootImageModule) generateHiddenAPIBuildActions(ctx android.ModuleContext) {
304 // Resolve the properties to paths.
305 flagFileInfo := b.properties.Hidden_api.hiddenAPIFlagFileInfo(ctx)
306
307 // Store the information for use by platform_bootclasspath.
308 ctx.SetProvider(hiddenAPIFlagFileInfoProvider, flagFileInfo)
309}
310
Paul Duffinf7f65da2021-03-10 15:00:46 +0000311type bootImageMemberType struct {
312 android.SdkMemberTypeBase
313}
314
315func (b *bootImageMemberType) AddDependencies(mctx android.BottomUpMutatorContext, dependencyTag blueprint.DependencyTag, names []string) {
316 mctx.AddVariationDependencies(nil, dependencyTag, names...)
317}
318
319func (b *bootImageMemberType) IsInstance(module android.Module) bool {
320 _, ok := module.(*BootImageModule)
321 return ok
322}
323
324func (b *bootImageMemberType) AddPrebuiltModule(ctx android.SdkMemberContext, member android.SdkMember) android.BpModule {
Paul Duffin4b64ba02021-03-29 11:02:53 +0100325 if b.PropertyName == "boot_images" {
326 return ctx.SnapshotBuilder().AddPrebuiltModule(member, "prebuilt_boot_image")
327 } else {
328 return ctx.SnapshotBuilder().AddPrebuiltModule(member, "prebuilt_bootclasspath_fragment")
329 }
Paul Duffinf7f65da2021-03-10 15:00:46 +0000330}
331
332func (b *bootImageMemberType) CreateVariantPropertiesStruct() android.SdkMemberProperties {
333 return &bootImageSdkMemberProperties{}
334}
335
336type bootImageSdkMemberProperties struct {
337 android.SdkMemberPropertiesBase
338
Paul Duffina57835e2021-04-19 13:23:06 +0100339 // The image name
Paul Duffin64be7bb2021-03-23 23:06:38 +0000340 Image_name *string
Paul Duffina57835e2021-04-19 13:23:06 +0100341
342 // Contents of the bootclasspath fragment
343 Contents []string
Paul Duffin7c955552021-04-19 13:23:53 +0100344
345 // Flag files by *hiddenAPIFlagFileCategory
346 Flag_files_by_category map[*hiddenAPIFlagFileCategory]android.Paths
Paul Duffinf7f65da2021-03-10 15:00:46 +0000347}
348
349func (b *bootImageSdkMemberProperties) PopulateFromVariant(ctx android.SdkMemberContext, variant android.Module) {
350 module := variant.(*BootImageModule)
351
352 b.Image_name = module.properties.Image_name
Paul Duffina57835e2021-04-19 13:23:06 +0100353 if b.Image_name == nil {
354 // Only one of image_name or contents can be specified. However, if image_name is set then the
355 // contents property is updated to match the configuration used to create the corresponding
356 // boot image. Therefore, contents property is only copied if the image name is not specified.
357 b.Contents = module.properties.Contents
358 }
Paul Duffin7c955552021-04-19 13:23:53 +0100359
360 // Get the flag file information from the module.
361 mctx := ctx.SdkModuleContext()
362 flagFileInfo := mctx.OtherModuleProvider(module, hiddenAPIFlagFileInfoProvider).(hiddenAPIFlagFileInfo)
363 b.Flag_files_by_category = flagFileInfo.categoryToPaths
Paul Duffinf7f65da2021-03-10 15:00:46 +0000364}
365
366func (b *bootImageSdkMemberProperties) AddToPropertySet(ctx android.SdkMemberContext, propertySet android.BpPropertySet) {
Paul Duffin64be7bb2021-03-23 23:06:38 +0000367 if b.Image_name != nil {
368 propertySet.AddProperty("image_name", *b.Image_name)
Paul Duffinf7f65da2021-03-10 15:00:46 +0000369 }
Paul Duffina57835e2021-04-19 13:23:06 +0100370
371 if len(b.Contents) > 0 {
372 propertySet.AddPropertyWithTag("contents", b.Contents, ctx.SnapshotBuilder().SdkMemberReferencePropertyTag(true))
373 }
Paul Duffin7c955552021-04-19 13:23:53 +0100374
375 builder := ctx.SnapshotBuilder()
376 if b.Flag_files_by_category != nil {
377 hiddenAPISet := propertySet.AddPropertySet("hidden_api")
378 for _, category := range hiddenAPIFlagFileCategories {
379 paths := b.Flag_files_by_category[category]
380 if len(paths) > 0 {
381 dests := []string{}
382 for _, p := range paths {
383 dest := filepath.Join("hiddenapi", p.Base())
384 builder.CopyToSnapshot(p, dest)
385 dests = append(dests, dest)
386 }
387 hiddenAPISet.AddProperty(category.propertyName, dests)
388 }
389 }
390 }
Paul Duffinf7f65da2021-03-10 15:00:46 +0000391}
392
393var _ android.SdkMemberType = (*bootImageMemberType)(nil)
394
395// A prebuilt version of the boot image module.
396//
397// At the moment this is basically just a boot image module that can be used as a prebuilt.
398// Eventually as more functionality is migrated into the boot image module from the singleton then
399// this will diverge.
400type prebuiltBootImageModule struct {
401 BootImageModule
402 prebuilt android.Prebuilt
403}
404
405func (module *prebuiltBootImageModule) Prebuilt() *android.Prebuilt {
406 return &module.prebuilt
407}
408
409func (module *prebuiltBootImageModule) Name() string {
410 return module.prebuilt.Name(module.ModuleBase.Name())
411}
412
413func prebuiltBootImageFactory() android.Module {
414 m := &prebuiltBootImageModule{}
415 m.AddProperties(&m.properties)
Paul Duffinf7f65da2021-03-10 15:00:46 +0000416 // This doesn't actually have any prebuilt files of its own so pass a placeholder for the srcs
417 // array.
418 android.InitPrebuiltModule(m, &[]string{"placeholder"})
419 android.InitApexModule(m)
420 android.InitSdkAwareModule(m)
Martin Stjernholmb79c7f12021-03-17 00:26:25 +0000421 android.InitAndroidArchModule(m, android.HostAndDeviceSupported, android.MultilibCommon)
Paul Duffinc7ef9892021-03-23 23:21:59 +0000422
423 // Perform some consistency checking to ensure that the configuration is correct.
424 android.AddLoadHook(m, func(ctx android.LoadHookContext) {
425 bootImageConsistencyCheck(ctx, &m.BootImageModule)
426 })
Paul Duffinf7f65da2021-03-10 15:00:46 +0000427 return m
428}