blob: b140a308025b7008feee6556e0f842d68c54f52d [file] [log] [blame]
Paul Duffinb67d8782021-04-22 11:49:41 +01001// Copyright 2021 Google Inc. All rights reserved.
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 (
18 "fmt"
19
20 "android/soong/android"
21 "github.com/google/blueprint"
22 "github.com/google/blueprint/proptools"
23)
24
25// Contains code that is common to both platform_bootclasspath and bootclasspath_fragment.
26
27// addDependencyOntoApexVariants adds dependencies onto the appropriate apex specific variants of
28// the module as specified in the ApexVariantReference list.
29func addDependencyOntoApexVariants(ctx android.BottomUpMutatorContext, propertyName string, refs []ApexVariantReference, tag blueprint.DependencyTag) {
30 for i, ref := range refs {
31 apex := proptools.StringDefault(ref.Apex, "platform")
32
33 if ref.Module == nil {
34 ctx.PropertyErrorf(propertyName, "missing module name at position %d", i)
35 continue
36 }
37 name := proptools.String(ref.Module)
38
39 addDependencyOntoApexModulePair(ctx, apex, name, tag)
40 }
41}
42
43// addDependencyOntoApexModulePair adds a dependency onto the specified APEX specific variant or the
44// specified module.
45//
46// If apex="platform" then this adds a dependency onto the platform variant of the module. This adds
47// dependencies onto the prebuilt and source modules with the specified name, depending on which
48// ones are available. Visiting must use isActiveModule to select the preferred module when both
49// source and prebuilt modules are available.
50func addDependencyOntoApexModulePair(ctx android.BottomUpMutatorContext, apex string, name string, tag blueprint.DependencyTag) {
51 var variations []blueprint.Variation
52 if apex != "platform" {
53 // Pick the correct apex variant.
54 variations = []blueprint.Variation{
55 {Mutator: "apex", Variation: apex},
56 }
57 }
58
59 addedDep := false
60 if ctx.OtherModuleDependencyVariantExists(variations, name) {
61 ctx.AddFarVariationDependencies(variations, tag, name)
62 addedDep = true
63 }
64
65 // Add a dependency on the prebuilt module if it exists.
66 prebuiltName := android.PrebuiltNameFromSource(name)
67 if ctx.OtherModuleDependencyVariantExists(variations, prebuiltName) {
68 ctx.AddVariationDependencies(variations, tag, prebuiltName)
69 addedDep = true
70 }
71
72 // If no appropriate variant existing for this, so no dependency could be added, then it is an
73 // error, unless missing dependencies are allowed. The simplest way to handle that is to add a
74 // dependency that will not be satisfied and the default behavior will handle it.
75 if !addedDep {
76 // Add dependency on the unprefixed (i.e. source or renamed prebuilt) module which we know does
77 // not exist. The resulting error message will contain useful information about the available
78 // variants.
79 reportMissingVariationDependency(ctx, variations, name)
80
81 // Add dependency on the missing prefixed prebuilt variant too if a module with that name exists
82 // so that information about its available variants will be reported too.
83 if ctx.OtherModuleExists(prebuiltName) {
84 reportMissingVariationDependency(ctx, variations, prebuiltName)
85 }
86 }
87}
88
89// reportMissingVariationDependency intentionally adds a dependency on a missing variation in order
90// to generate an appropriate error message with information about the available variations.
91func reportMissingVariationDependency(ctx android.BottomUpMutatorContext, variations []blueprint.Variation, name string) {
92 modules := ctx.AddFarVariationDependencies(variations, nil, name)
93 if len(modules) != 1 {
94 panic(fmt.Errorf("Internal Error: expected one module, found %d", len(modules)))
95 return
96 }
97 if modules[0] != nil {
98 panic(fmt.Errorf("Internal Error: expected module to be missing but was found: %q", modules[0]))
99 return
100 }
101}
102
103// ApexVariantReference specifies a particular apex variant of a module.
104type ApexVariantReference struct {
105 // The name of the module apex variant, i.e. the apex containing the module variant.
106 //
107 // If this is not specified then it defaults to "platform" which will cause a dependency to be
108 // added to the module's platform variant.
109 Apex *string
110
111 // The name of the module.
112 Module *string
113}
114
115// BootclasspathFragmentsDepsProperties contains properties related to dependencies onto fragments.
116type BootclasspathFragmentsDepsProperties struct {
117 // The names of the bootclasspath_fragment modules that form part of this module.
118 Fragments []ApexVariantReference
119}
120
121// addDependenciesOntoFragments adds dependencies to the fragments specified in this properties
122// structure.
123func (p *BootclasspathFragmentsDepsProperties) addDependenciesOntoFragments(ctx android.BottomUpMutatorContext) {
124 addDependencyOntoApexVariants(ctx, "fragments", p.Fragments, bootclasspathFragmentDepTag)
125}
126
127// bootclasspathDependencyTag defines dependencies from/to bootclasspath_fragment,
128// prebuilt_bootclasspath_fragment and platform_bootclasspath onto either source or prebuilt
129// modules.
130type bootclasspathDependencyTag struct {
131 blueprint.BaseDependencyTag
132
133 name string
134}
135
136func (t bootclasspathDependencyTag) ExcludeFromVisibilityEnforcement() {
137}
138
139// Dependencies that use the bootclasspathDependencyTag instances are only added after all the
140// visibility checking has been done so this has no functional effect. However, it does make it
141// clear that visibility is not being enforced on these tags.
142var _ android.ExcludeFromVisibilityEnforcementTag = bootclasspathDependencyTag{}
143
144// The tag used for dependencies onto bootclasspath_fragments.
145var bootclasspathFragmentDepTag = bootclasspathDependencyTag{name: "fragment"}