blob: 815da7d63efa3ac8b9f6430f0fe0bf688282fcf4 [file] [log] [blame]
Artur Satayeveabf2c12021-04-07 15:45:02 +01001/*
2 * Copyright (C) 2021 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package java
18
19import (
Artur Satayev97259dc2021-04-07 15:17:14 +010020 "fmt"
satayev72ede0f2021-05-17 21:03:07 +010021 "github.com/google/blueprint"
satayevffe79712021-06-15 16:49:50 +010022 "github.com/google/blueprint/proptools"
Artur Satayev97259dc2021-04-07 15:17:14 +010023 "strings"
24
Artur Satayeveabf2c12021-04-07 15:45:02 +010025 "android/soong/android"
26)
27
28// Build rules and utilities to generate individual packages/modules/SdkExtensions/proto/classpaths.proto
29// config files based on build configuration to embed into /system and /apex on a device.
30//
31// See `derive_classpath` service that reads the configs at runtime and defines *CLASSPATH variables
32// on the device.
33
34type classpathType int
35
36const (
37 // Matches definition in packages/modules/SdkExtensions/proto/classpaths.proto
38 BOOTCLASSPATH classpathType = iota
39 DEX2OATBOOTCLASSPATH
40 SYSTEMSERVERCLASSPATH
41)
42
43func (c classpathType) String() string {
44 return [...]string{"BOOTCLASSPATH", "DEX2OATBOOTCLASSPATH", "SYSTEMSERVERCLASSPATH"}[c]
45}
46
47type classpathFragmentProperties struct {
satayevffe79712021-06-15 16:49:50 +010048 // Whether to generated classpaths.proto config instance for the fragment. If the config is not
49 // generated, then relevant boot jars are added to platform classpath, i.e. platform_bootclasspath
50 // or platform_systemserverclasspath. This is useful for non-updatable APEX boot jars, to keep
51 // them as part of dexopt on device. Defaults to true.
52 Generate_classpaths_proto *bool
Artur Satayeveabf2c12021-04-07 15:45:02 +010053}
54
55// classpathFragment interface is implemented by a module that contributes jars to a *CLASSPATH
56// variables at runtime.
57type classpathFragment interface {
58 android.Module
59
Artur Satayev97259dc2021-04-07 15:17:14 +010060 classpathFragmentBase() *ClasspathFragmentBase
Artur Satayeveabf2c12021-04-07 15:45:02 +010061}
62
Artur Satayev97259dc2021-04-07 15:17:14 +010063// ClasspathFragmentBase is meant to be embedded in any module types that implement classpathFragment;
Artur Satayeveabf2c12021-04-07 15:45:02 +010064// such modules are expected to call initClasspathFragment().
Artur Satayev97259dc2021-04-07 15:17:14 +010065type ClasspathFragmentBase struct {
Artur Satayeveabf2c12021-04-07 15:45:02 +010066 properties classpathFragmentProperties
67
satayev95e9c5b2021-04-29 11:50:26 +010068 classpathType classpathType
69
Artur Satayeveabf2c12021-04-07 15:45:02 +010070 outputFilepath android.OutputPath
Artur Satayev97259dc2021-04-07 15:17:14 +010071 installDirPath android.InstallPath
Artur Satayeveabf2c12021-04-07 15:45:02 +010072}
73
Artur Satayev97259dc2021-04-07 15:17:14 +010074func (c *ClasspathFragmentBase) classpathFragmentBase() *ClasspathFragmentBase {
75 return c
76}
77
78// Initializes ClasspathFragmentBase struct. Must be called by all modules that include ClasspathFragmentBase.
satayev95e9c5b2021-04-29 11:50:26 +010079func initClasspathFragment(c classpathFragment, classpathType classpathType) {
Artur Satayeveabf2c12021-04-07 15:45:02 +010080 base := c.classpathFragmentBase()
satayev95e9c5b2021-04-29 11:50:26 +010081 base.classpathType = classpathType
Artur Satayeveabf2c12021-04-07 15:45:02 +010082 c.AddProperties(&base.properties)
83}
Artur Satayev97259dc2021-04-07 15:17:14 +010084
85// Matches definition of Jar in packages/modules/SdkExtensions/proto/classpaths.proto
86type classpathJar struct {
87 path string
88 classpath classpathType
89 // TODO(satayev): propagate min/max sdk versions for the jars
90 minSdkVersion int32
91 maxSdkVersion int32
92}
93
Paul Duffinaba52752021-06-29 20:04:45 +010094// gatherPossibleUpdatableModuleNamesAndStems returns a set of module and stem names from the
95// supplied contents that may be in the updatable boot jars.
96//
97// The module names are included because sometimes the stem is set to just change the name of
98// the installed file and it expects the configuration to still use the actual module name.
99//
100// The stem names are included because sometimes the stem is set to change the effective name of the
101// module that is used in the configuration as well,e .g. when a test library is overriding an
102// actual boot jar
103func gatherPossibleUpdatableModuleNamesAndStems(ctx android.ModuleContext, contents []string, tag blueprint.DependencyTag) []string {
104 set := map[string]struct{}{}
105 for _, name := range contents {
106 dep := ctx.GetDirectDepWithTag(name, tag)
107 set[name] = struct{}{}
108 if m, ok := dep.(ModuleWithStem); ok {
109 set[m.Stem()] = struct{}{}
110 } else {
111 ctx.PropertyErrorf("contents", "%v is not a ModuleWithStem", name)
112 }
113 }
114 return android.SortedStringKeys(set)
115}
116
satayev013485b2021-05-06 23:38:10 +0100117// Converts android.ConfiguredJarList into a list of classpathJars for each given classpathType.
118func configuredJarListToClasspathJars(ctx android.ModuleContext, configuredJars android.ConfiguredJarList, classpaths ...classpathType) []classpathJar {
119 paths := configuredJars.DevicePaths(ctx.Config(), android.Android)
120 jars := make([]classpathJar, 0, len(paths)*len(classpaths))
121 for i := 0; i < len(paths); i++ {
122 for _, classpathType := range classpaths {
123 jars = append(jars, classpathJar{
124 classpath: classpathType,
125 path: paths[i],
126 })
127 }
128 }
129 return jars
130}
131
132func (c *ClasspathFragmentBase) generateClasspathProtoBuildActions(ctx android.ModuleContext, jars []classpathJar) {
satayevffe79712021-06-15 16:49:50 +0100133 generateProto := proptools.BoolDefault(c.properties.Generate_classpaths_proto, true)
134 if generateProto {
135 outputFilename := strings.ToLower(c.classpathType.String()) + ".pb"
136 c.outputFilepath = android.PathForModuleOut(ctx, outputFilename).OutputPath
137 c.installDirPath = android.PathForModuleInstall(ctx, "etc", "classpaths")
Artur Satayev97259dc2021-04-07 15:17:14 +0100138
satayevffe79712021-06-15 16:49:50 +0100139 generatedJson := android.PathForModuleOut(ctx, outputFilename+".json")
140 writeClasspathsJson(ctx, generatedJson, jars)
Artur Satayev97259dc2021-04-07 15:17:14 +0100141
satayevffe79712021-06-15 16:49:50 +0100142 rule := android.NewRuleBuilder(pctx, ctx)
143 rule.Command().
144 BuiltTool("conv_classpaths_proto").
145 Flag("encode").
146 Flag("--format=json").
147 FlagWithInput("--input=", generatedJson).
148 FlagWithOutput("--output=", c.outputFilepath)
Artur Satayev97259dc2021-04-07 15:17:14 +0100149
satayevffe79712021-06-15 16:49:50 +0100150 rule.Build("classpath_fragment", "Compiling "+c.outputFilepath.String())
151 }
satayev72ede0f2021-05-17 21:03:07 +0100152
153 classpathProtoInfo := ClasspathFragmentProtoContentInfo{
satayevffe79712021-06-15 16:49:50 +0100154 ClasspathFragmentProtoGenerated: generateProto,
satayev72ede0f2021-05-17 21:03:07 +0100155 ClasspathFragmentProtoInstallDir: c.installDirPath,
156 ClasspathFragmentProtoOutput: c.outputFilepath,
157 }
158 ctx.SetProvider(ClasspathFragmentProtoContentInfoProvider, classpathProtoInfo)
Artur Satayev97259dc2021-04-07 15:17:14 +0100159}
160
161func writeClasspathsJson(ctx android.ModuleContext, output android.WritablePath, jars []classpathJar) {
162 var content strings.Builder
163 fmt.Fprintf(&content, "{\n")
164 fmt.Fprintf(&content, "\"jars\": [\n")
165 for idx, jar := range jars {
166 fmt.Fprintf(&content, "{\n")
167
satayev6bd82322021-05-18 19:37:32 +0100168 fmt.Fprintf(&content, "\"path\": \"%s\",\n", jar.path)
Artur Satayev97259dc2021-04-07 15:17:14 +0100169 fmt.Fprintf(&content, "\"classpath\": \"%s\"\n", jar.classpath)
170
171 if idx < len(jars)-1 {
172 fmt.Fprintf(&content, "},\n")
173 } else {
174 fmt.Fprintf(&content, "}\n")
175 }
176 }
177 fmt.Fprintf(&content, "]\n")
178 fmt.Fprintf(&content, "}\n")
179 android.WriteFileRule(ctx, output, content.String())
180}
181
satayev3db35472021-05-06 23:59:58 +0100182// Returns AndroidMkEntries objects to install generated classpath.proto.
183// Do not use this to install into APEXes as the injection of the generated files happen separately for APEXes.
satayev128ce2f2021-05-06 13:21:15 +0100184func (c *ClasspathFragmentBase) androidMkEntries() []android.AndroidMkEntries {
satayev013485b2021-05-06 23:38:10 +0100185 return []android.AndroidMkEntries{{
Artur Satayev97259dc2021-04-07 15:17:14 +0100186 Class: "ETC",
187 OutputFile: android.OptionalPathForPath(c.outputFilepath),
188 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
189 func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
190 entries.SetString("LOCAL_MODULE_PATH", c.installDirPath.ToMakePath().String())
191 entries.SetString("LOCAL_INSTALLED_MODULE_STEM", c.outputFilepath.Base())
192 },
193 },
194 }}
195}
satayev72ede0f2021-05-17 21:03:07 +0100196
197var ClasspathFragmentProtoContentInfoProvider = blueprint.NewProvider(ClasspathFragmentProtoContentInfo{})
198
199type ClasspathFragmentProtoContentInfo struct {
satayevffe79712021-06-15 16:49:50 +0100200 // Whether the classpaths.proto config is generated for the fragment.
201 ClasspathFragmentProtoGenerated bool
202
satayev72ede0f2021-05-17 21:03:07 +0100203 // ClasspathFragmentProtoOutput is an output path for the generated classpaths.proto config of this module.
204 //
205 // The file should be copied to a relevant place on device, see ClasspathFragmentProtoInstallDir
206 // for more details.
207 ClasspathFragmentProtoOutput android.OutputPath
208
209 // ClasspathFragmentProtoInstallDir contains information about on device location for the generated classpaths.proto file.
210 //
211 // The path encodes expected sub-location within partitions, i.e. etc/classpaths/<proto-file>,
212 // for ClasspathFragmentProtoOutput. To get sub-location, instead of the full output / make path
213 // use android.InstallPath#Rel().
214 //
215 // This is only relevant for APEX modules as they perform their own installation; while regular
216 // system files are installed via ClasspathFragmentBase#androidMkEntries().
217 ClasspathFragmentProtoInstallDir android.InstallPath
218}