blob: e057b6e7aad9e1c16b0562dafd1c736a878b4039 [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"
Artur Satayev97259dc2021-04-07 15:17:14 +010022 "strings"
23
Artur Satayeveabf2c12021-04-07 15:45:02 +010024 "android/soong/android"
25)
26
27// Build rules and utilities to generate individual packages/modules/SdkExtensions/proto/classpaths.proto
28// config files based on build configuration to embed into /system and /apex on a device.
29//
30// See `derive_classpath` service that reads the configs at runtime and defines *CLASSPATH variables
31// on the device.
32
33type classpathType int
34
35const (
36 // Matches definition in packages/modules/SdkExtensions/proto/classpaths.proto
37 BOOTCLASSPATH classpathType = iota
38 DEX2OATBOOTCLASSPATH
39 SYSTEMSERVERCLASSPATH
40)
41
42func (c classpathType) String() string {
43 return [...]string{"BOOTCLASSPATH", "DEX2OATBOOTCLASSPATH", "SYSTEMSERVERCLASSPATH"}[c]
44}
45
46type classpathFragmentProperties struct {
47}
48
49// classpathFragment interface is implemented by a module that contributes jars to a *CLASSPATH
50// variables at runtime.
51type classpathFragment interface {
52 android.Module
53
Artur Satayev97259dc2021-04-07 15:17:14 +010054 classpathFragmentBase() *ClasspathFragmentBase
Artur Satayeveabf2c12021-04-07 15:45:02 +010055}
56
Artur Satayev97259dc2021-04-07 15:17:14 +010057// ClasspathFragmentBase is meant to be embedded in any module types that implement classpathFragment;
Artur Satayeveabf2c12021-04-07 15:45:02 +010058// such modules are expected to call initClasspathFragment().
Artur Satayev97259dc2021-04-07 15:17:14 +010059type ClasspathFragmentBase struct {
Artur Satayeveabf2c12021-04-07 15:45:02 +010060 properties classpathFragmentProperties
61
satayev95e9c5b2021-04-29 11:50:26 +010062 classpathType classpathType
63
Artur Satayeveabf2c12021-04-07 15:45:02 +010064 outputFilepath android.OutputPath
Artur Satayev97259dc2021-04-07 15:17:14 +010065 installDirPath android.InstallPath
Artur Satayeveabf2c12021-04-07 15:45:02 +010066}
67
Artur Satayev97259dc2021-04-07 15:17:14 +010068func (c *ClasspathFragmentBase) classpathFragmentBase() *ClasspathFragmentBase {
69 return c
70}
71
72// Initializes ClasspathFragmentBase struct. Must be called by all modules that include ClasspathFragmentBase.
satayev95e9c5b2021-04-29 11:50:26 +010073func initClasspathFragment(c classpathFragment, classpathType classpathType) {
Artur Satayeveabf2c12021-04-07 15:45:02 +010074 base := c.classpathFragmentBase()
satayev95e9c5b2021-04-29 11:50:26 +010075 base.classpathType = classpathType
Artur Satayeveabf2c12021-04-07 15:45:02 +010076 c.AddProperties(&base.properties)
77}
Artur Satayev97259dc2021-04-07 15:17:14 +010078
79// Matches definition of Jar in packages/modules/SdkExtensions/proto/classpaths.proto
80type classpathJar struct {
81 path string
82 classpath classpathType
83 // TODO(satayev): propagate min/max sdk versions for the jars
84 minSdkVersion int32
85 maxSdkVersion int32
86}
87
Paul Duffinaba52752021-06-29 20:04:45 +010088// gatherPossibleUpdatableModuleNamesAndStems returns a set of module and stem names from the
89// supplied contents that may be in the updatable boot jars.
90//
91// The module names are included because sometimes the stem is set to just change the name of
92// the installed file and it expects the configuration to still use the actual module name.
93//
94// The stem names are included because sometimes the stem is set to change the effective name of the
95// module that is used in the configuration as well,e .g. when a test library is overriding an
96// actual boot jar
97func gatherPossibleUpdatableModuleNamesAndStems(ctx android.ModuleContext, contents []string, tag blueprint.DependencyTag) []string {
98 set := map[string]struct{}{}
99 for _, name := range contents {
100 dep := ctx.GetDirectDepWithTag(name, tag)
101 set[name] = struct{}{}
102 if m, ok := dep.(ModuleWithStem); ok {
103 set[m.Stem()] = struct{}{}
104 } else {
105 ctx.PropertyErrorf("contents", "%v is not a ModuleWithStem", name)
106 }
107 }
108 return android.SortedStringKeys(set)
109}
110
satayev013485b2021-05-06 23:38:10 +0100111// Converts android.ConfiguredJarList into a list of classpathJars for each given classpathType.
112func configuredJarListToClasspathJars(ctx android.ModuleContext, configuredJars android.ConfiguredJarList, classpaths ...classpathType) []classpathJar {
113 paths := configuredJars.DevicePaths(ctx.Config(), android.Android)
114 jars := make([]classpathJar, 0, len(paths)*len(classpaths))
115 for i := 0; i < len(paths); i++ {
116 for _, classpathType := range classpaths {
117 jars = append(jars, classpathJar{
118 classpath: classpathType,
119 path: paths[i],
120 })
121 }
122 }
123 return jars
124}
125
126func (c *ClasspathFragmentBase) generateClasspathProtoBuildActions(ctx android.ModuleContext, jars []classpathJar) {
satayev53d06782021-05-20 21:35:06 +0100127 outputFilename := strings.ToLower(c.classpathType.String()) + ".pb"
Artur Satayev97259dc2021-04-07 15:17:14 +0100128 c.outputFilepath = android.PathForModuleOut(ctx, outputFilename).OutputPath
129 c.installDirPath = android.PathForModuleInstall(ctx, "etc", "classpaths")
130
Artur Satayev97259dc2021-04-07 15:17:14 +0100131 generatedJson := android.PathForModuleOut(ctx, outputFilename+".json")
132 writeClasspathsJson(ctx, generatedJson, jars)
133
134 rule := android.NewRuleBuilder(pctx, ctx)
135 rule.Command().
136 BuiltTool("conv_classpaths_proto").
137 Flag("encode").
138 Flag("--format=json").
139 FlagWithInput("--input=", generatedJson).
140 FlagWithOutput("--output=", c.outputFilepath)
141
142 rule.Build("classpath_fragment", "Compiling "+c.outputFilepath.String())
satayev72ede0f2021-05-17 21:03:07 +0100143
144 classpathProtoInfo := ClasspathFragmentProtoContentInfo{
145 ClasspathFragmentProtoInstallDir: c.installDirPath,
146 ClasspathFragmentProtoOutput: c.outputFilepath,
147 }
148 ctx.SetProvider(ClasspathFragmentProtoContentInfoProvider, classpathProtoInfo)
Artur Satayev97259dc2021-04-07 15:17:14 +0100149}
150
151func writeClasspathsJson(ctx android.ModuleContext, output android.WritablePath, jars []classpathJar) {
152 var content strings.Builder
153 fmt.Fprintf(&content, "{\n")
154 fmt.Fprintf(&content, "\"jars\": [\n")
155 for idx, jar := range jars {
156 fmt.Fprintf(&content, "{\n")
157
satayev6bd82322021-05-18 19:37:32 +0100158 fmt.Fprintf(&content, "\"path\": \"%s\",\n", jar.path)
Artur Satayev97259dc2021-04-07 15:17:14 +0100159 fmt.Fprintf(&content, "\"classpath\": \"%s\"\n", jar.classpath)
160
161 if idx < len(jars)-1 {
162 fmt.Fprintf(&content, "},\n")
163 } else {
164 fmt.Fprintf(&content, "}\n")
165 }
166 }
167 fmt.Fprintf(&content, "]\n")
168 fmt.Fprintf(&content, "}\n")
169 android.WriteFileRule(ctx, output, content.String())
170}
171
satayev3db35472021-05-06 23:59:58 +0100172// Returns AndroidMkEntries objects to install generated classpath.proto.
173// 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 +0100174func (c *ClasspathFragmentBase) androidMkEntries() []android.AndroidMkEntries {
satayev013485b2021-05-06 23:38:10 +0100175 return []android.AndroidMkEntries{{
Artur Satayev97259dc2021-04-07 15:17:14 +0100176 Class: "ETC",
177 OutputFile: android.OptionalPathForPath(c.outputFilepath),
178 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
179 func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
180 entries.SetString("LOCAL_MODULE_PATH", c.installDirPath.ToMakePath().String())
181 entries.SetString("LOCAL_INSTALLED_MODULE_STEM", c.outputFilepath.Base())
182 },
183 },
184 }}
185}
satayev72ede0f2021-05-17 21:03:07 +0100186
187var ClasspathFragmentProtoContentInfoProvider = blueprint.NewProvider(ClasspathFragmentProtoContentInfo{})
188
189type ClasspathFragmentProtoContentInfo struct {
190 // ClasspathFragmentProtoOutput is an output path for the generated classpaths.proto config of this module.
191 //
192 // The file should be copied to a relevant place on device, see ClasspathFragmentProtoInstallDir
193 // for more details.
194 ClasspathFragmentProtoOutput android.OutputPath
195
196 // ClasspathFragmentProtoInstallDir contains information about on device location for the generated classpaths.proto file.
197 //
198 // The path encodes expected sub-location within partitions, i.e. etc/classpaths/<proto-file>,
199 // for ClasspathFragmentProtoOutput. To get sub-location, instead of the full output / make path
200 // use android.InstallPath#Rel().
201 //
202 // This is only relevant for APEX modules as they perform their own installation; while regular
203 // system files are installed via ClasspathFragmentBase#androidMkEntries().
204 ClasspathFragmentProtoInstallDir android.InstallPath
205}