Artur Satayev | eabf2c1 | 2021-04-07 15:45:02 +0100 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package java |
| 18 | |
| 19 | import ( |
Artur Satayev | 97259dc | 2021-04-07 15:17:14 +0100 | [diff] [blame] | 20 | "fmt" |
satayev | 14e4913 | 2021-05-17 21:03:07 +0100 | [diff] [blame] | 21 | "github.com/google/blueprint" |
satayev | b98371c | 2021-06-15 16:49:50 +0100 | [diff] [blame] | 22 | "github.com/google/blueprint/proptools" |
Artur Satayev | 97259dc | 2021-04-07 15:17:14 +0100 | [diff] [blame] | 23 | "strings" |
| 24 | |
Artur Satayev | eabf2c1 | 2021-04-07 15:45:02 +0100 | [diff] [blame] | 25 | "android/soong/android" |
| 26 | ) |
| 27 | |
Jiakai Zhang | cee9e19 | 2021-10-29 19:46:45 +0000 | [diff] [blame] | 28 | // Build rules and utilities to generate individual packages/modules/common/proto/classpaths.proto |
Artur Satayev | eabf2c1 | 2021-04-07 15:45:02 +0100 | [diff] [blame] | 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 | |
| 34 | type classpathType int |
| 35 | |
| 36 | const ( |
Jiakai Zhang | cee9e19 | 2021-10-29 19:46:45 +0000 | [diff] [blame] | 37 | // Matches definition in packages/modules/common/proto/classpaths.proto |
Artur Satayev | eabf2c1 | 2021-04-07 15:45:02 +0100 | [diff] [blame] | 38 | BOOTCLASSPATH classpathType = iota |
| 39 | DEX2OATBOOTCLASSPATH |
| 40 | SYSTEMSERVERCLASSPATH |
Jiakai Zhang | cee9e19 | 2021-10-29 19:46:45 +0000 | [diff] [blame] | 41 | STANDALONE_SYSTEMSERVER_JARS |
Artur Satayev | eabf2c1 | 2021-04-07 15:45:02 +0100 | [diff] [blame] | 42 | ) |
| 43 | |
| 44 | func (c classpathType) String() string { |
Jiakai Zhang | cee9e19 | 2021-10-29 19:46:45 +0000 | [diff] [blame] | 45 | return [...]string{"BOOTCLASSPATH", "DEX2OATBOOTCLASSPATH", "SYSTEMSERVERCLASSPATH", "STANDALONE_SYSTEMSERVER_JARS"}[c] |
Artur Satayev | eabf2c1 | 2021-04-07 15:45:02 +0100 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | type classpathFragmentProperties struct { |
satayev | b98371c | 2021-06-15 16:49:50 +0100 | [diff] [blame] | 49 | // Whether to generated classpaths.proto config instance for the fragment. If the config is not |
| 50 | // generated, then relevant boot jars are added to platform classpath, i.e. platform_bootclasspath |
| 51 | // or platform_systemserverclasspath. This is useful for non-updatable APEX boot jars, to keep |
| 52 | // them as part of dexopt on device. Defaults to true. |
| 53 | Generate_classpaths_proto *bool |
Artur Satayev | eabf2c1 | 2021-04-07 15:45:02 +0100 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | // classpathFragment interface is implemented by a module that contributes jars to a *CLASSPATH |
| 57 | // variables at runtime. |
| 58 | type classpathFragment interface { |
| 59 | android.Module |
| 60 | |
Artur Satayev | 97259dc | 2021-04-07 15:17:14 +0100 | [diff] [blame] | 61 | classpathFragmentBase() *ClasspathFragmentBase |
Artur Satayev | eabf2c1 | 2021-04-07 15:45:02 +0100 | [diff] [blame] | 62 | } |
| 63 | |
Artur Satayev | 97259dc | 2021-04-07 15:17:14 +0100 | [diff] [blame] | 64 | // ClasspathFragmentBase is meant to be embedded in any module types that implement classpathFragment; |
Artur Satayev | eabf2c1 | 2021-04-07 15:45:02 +0100 | [diff] [blame] | 65 | // such modules are expected to call initClasspathFragment(). |
Artur Satayev | 97259dc | 2021-04-07 15:17:14 +0100 | [diff] [blame] | 66 | type ClasspathFragmentBase struct { |
Artur Satayev | eabf2c1 | 2021-04-07 15:45:02 +0100 | [diff] [blame] | 67 | properties classpathFragmentProperties |
| 68 | |
satayev | 95e9c5b | 2021-04-29 11:50:26 +0100 | [diff] [blame] | 69 | classpathType classpathType |
| 70 | |
Artur Satayev | eabf2c1 | 2021-04-07 15:45:02 +0100 | [diff] [blame] | 71 | outputFilepath android.OutputPath |
Artur Satayev | 97259dc | 2021-04-07 15:17:14 +0100 | [diff] [blame] | 72 | installDirPath android.InstallPath |
Artur Satayev | eabf2c1 | 2021-04-07 15:45:02 +0100 | [diff] [blame] | 73 | } |
| 74 | |
Artur Satayev | 97259dc | 2021-04-07 15:17:14 +0100 | [diff] [blame] | 75 | func (c *ClasspathFragmentBase) classpathFragmentBase() *ClasspathFragmentBase { |
| 76 | return c |
| 77 | } |
| 78 | |
| 79 | // Initializes ClasspathFragmentBase struct. Must be called by all modules that include ClasspathFragmentBase. |
satayev | 95e9c5b | 2021-04-29 11:50:26 +0100 | [diff] [blame] | 80 | func initClasspathFragment(c classpathFragment, classpathType classpathType) { |
Artur Satayev | eabf2c1 | 2021-04-07 15:45:02 +0100 | [diff] [blame] | 81 | base := c.classpathFragmentBase() |
satayev | 95e9c5b | 2021-04-29 11:50:26 +0100 | [diff] [blame] | 82 | base.classpathType = classpathType |
Artur Satayev | eabf2c1 | 2021-04-07 15:45:02 +0100 | [diff] [blame] | 83 | c.AddProperties(&base.properties) |
| 84 | } |
Artur Satayev | 97259dc | 2021-04-07 15:17:14 +0100 | [diff] [blame] | 85 | |
| 86 | // Matches definition of Jar in packages/modules/SdkExtensions/proto/classpaths.proto |
| 87 | type classpathJar struct { |
satayev | cca4ab7 | 2021-11-30 12:33:55 +0000 | [diff] [blame] | 88 | path string |
| 89 | classpath classpathType |
| 90 | minSdkVersion string |
| 91 | maxSdkVersion string |
Artur Satayev | 97259dc | 2021-04-07 15:17:14 +0100 | [diff] [blame] | 92 | } |
| 93 | |
satayev | d604b21 | 2021-07-21 14:23:52 +0100 | [diff] [blame] | 94 | // gatherPossibleApexModuleNamesAndStems returns a set of module and stem names from the |
| 95 | // supplied contents that may be in the apex boot jars. |
Paul Duffin | 56c93e8 | 2021-06-29 20:04:45 +0100 | [diff] [blame] | 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 |
satayev | d604b21 | 2021-07-21 14:23:52 +0100 | [diff] [blame] | 103 | func gatherPossibleApexModuleNamesAndStems(ctx android.ModuleContext, contents []string, tag blueprint.DependencyTag) []string { |
Paul Duffin | 56c93e8 | 2021-06-29 20:04:45 +0100 | [diff] [blame] | 104 | set := map[string]struct{}{} |
| 105 | for _, name := range contents { |
Spandan Das | 161e468 | 2024-01-19 00:22:22 +0000 | [diff] [blame] | 106 | dep, _ := ctx.GetDirectDepWithTag(name, tag).(android.Module) |
| 107 | set[ModuleStemForDeapexing(dep)] = struct{}{} |
Paul Duffin | 56c93e8 | 2021-06-29 20:04:45 +0100 | [diff] [blame] | 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 | } |
Cole Faust | 18994c7 | 2023-02-28 16:02:16 -0800 | [diff] [blame] | 114 | return android.SortedKeys(set) |
Paul Duffin | 56c93e8 | 2021-06-29 20:04:45 +0100 | [diff] [blame] | 115 | } |
| 116 | |
satayev | 013485b | 2021-05-06 23:38:10 +0100 | [diff] [blame] | 117 | // Converts android.ConfiguredJarList into a list of classpathJars for each given classpathType. |
| 118 | func 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 { |
satayev | cca4ab7 | 2021-11-30 12:33:55 +0000 | [diff] [blame] | 123 | jar := classpathJar{ |
satayev | 013485b | 2021-05-06 23:38:10 +0100 | [diff] [blame] | 124 | classpath: classpathType, |
| 125 | path: paths[i], |
satayev | cca4ab7 | 2021-11-30 12:33:55 +0000 | [diff] [blame] | 126 | } |
| 127 | ctx.VisitDirectDepsIf(func(m android.Module) bool { |
| 128 | return m.Name() == configuredJars.Jar(i) |
| 129 | }, func(m android.Module) { |
| 130 | if s, ok := m.(*SdkLibrary); ok { |
| 131 | // TODO(208456999): instead of mapping "current" to latest, min_sdk_version should never be set to "current" |
| 132 | if s.minSdkVersion.Specified() { |
Spandan Das | 8c9ae7e | 2023-03-03 21:20:36 +0000 | [diff] [blame] | 133 | if s.minSdkVersion.IsCurrent() { |
Yurii Zubrytskyi | 1611334 | 2022-05-03 01:40:32 -0700 | [diff] [blame] | 134 | jar.minSdkVersion = ctx.Config().DefaultAppTargetSdk(ctx).String() |
satayev | cca4ab7 | 2021-11-30 12:33:55 +0000 | [diff] [blame] | 135 | } else { |
Spandan Das | 8c9ae7e | 2023-03-03 21:20:36 +0000 | [diff] [blame] | 136 | jar.minSdkVersion = s.minSdkVersion.String() |
satayev | cca4ab7 | 2021-11-30 12:33:55 +0000 | [diff] [blame] | 137 | } |
| 138 | } |
Spandan Das | a26eda7 | 2023-03-02 00:56:06 +0000 | [diff] [blame] | 139 | if s.maxSdkVersion.Specified() { |
| 140 | if s.maxSdkVersion.IsCurrent() { |
Yurii Zubrytskyi | 1611334 | 2022-05-03 01:40:32 -0700 | [diff] [blame] | 141 | jar.maxSdkVersion = ctx.Config().DefaultAppTargetSdk(ctx).String() |
satayev | cca4ab7 | 2021-11-30 12:33:55 +0000 | [diff] [blame] | 142 | } else { |
Spandan Das | a26eda7 | 2023-03-02 00:56:06 +0000 | [diff] [blame] | 143 | jar.maxSdkVersion = s.maxSdkVersion.String() |
satayev | cca4ab7 | 2021-11-30 12:33:55 +0000 | [diff] [blame] | 144 | } |
| 145 | } |
| 146 | } |
satayev | 013485b | 2021-05-06 23:38:10 +0100 | [diff] [blame] | 147 | }) |
satayev | cca4ab7 | 2021-11-30 12:33:55 +0000 | [diff] [blame] | 148 | jars = append(jars, jar) |
satayev | 013485b | 2021-05-06 23:38:10 +0100 | [diff] [blame] | 149 | } |
| 150 | } |
| 151 | return jars |
| 152 | } |
| 153 | |
Inseob Kim | dd53249 | 2024-04-30 17:22:58 +0900 | [diff] [blame^] | 154 | func (c *ClasspathFragmentBase) outputFilename() string { |
| 155 | return strings.ToLower(c.classpathType.String()) + ".pb" |
| 156 | } |
| 157 | |
satayev | b309050 | 2021-06-15 17:49:10 +0100 | [diff] [blame] | 158 | func (c *ClasspathFragmentBase) generateClasspathProtoBuildActions(ctx android.ModuleContext, configuredJars android.ConfiguredJarList, jars []classpathJar) { |
satayev | b98371c | 2021-06-15 16:49:50 +0100 | [diff] [blame] | 159 | generateProto := proptools.BoolDefault(c.properties.Generate_classpaths_proto, true) |
| 160 | if generateProto { |
Inseob Kim | dd53249 | 2024-04-30 17:22:58 +0900 | [diff] [blame^] | 161 | outputFilename := c.outputFilename() |
satayev | b98371c | 2021-06-15 16:49:50 +0100 | [diff] [blame] | 162 | c.outputFilepath = android.PathForModuleOut(ctx, outputFilename).OutputPath |
| 163 | c.installDirPath = android.PathForModuleInstall(ctx, "etc", "classpaths") |
Artur Satayev | 97259dc | 2021-04-07 15:17:14 +0100 | [diff] [blame] | 164 | |
satayev | 48dae67 | 2021-12-01 18:24:09 +0000 | [diff] [blame] | 165 | generatedTextproto := android.PathForModuleOut(ctx, outputFilename+".textproto") |
| 166 | writeClasspathsTextproto(ctx, generatedTextproto, jars) |
Artur Satayev | 97259dc | 2021-04-07 15:17:14 +0100 | [diff] [blame] | 167 | |
satayev | b98371c | 2021-06-15 16:49:50 +0100 | [diff] [blame] | 168 | rule := android.NewRuleBuilder(pctx, ctx) |
| 169 | rule.Command(). |
| 170 | BuiltTool("conv_classpaths_proto"). |
| 171 | Flag("encode"). |
satayev | 48dae67 | 2021-12-01 18:24:09 +0000 | [diff] [blame] | 172 | Flag("--format=textproto"). |
| 173 | FlagWithInput("--input=", generatedTextproto). |
satayev | b98371c | 2021-06-15 16:49:50 +0100 | [diff] [blame] | 174 | FlagWithOutput("--output=", c.outputFilepath) |
Artur Satayev | 97259dc | 2021-04-07 15:17:14 +0100 | [diff] [blame] | 175 | |
satayev | b98371c | 2021-06-15 16:49:50 +0100 | [diff] [blame] | 176 | rule.Build("classpath_fragment", "Compiling "+c.outputFilepath.String()) |
| 177 | } |
satayev | 14e4913 | 2021-05-17 21:03:07 +0100 | [diff] [blame] | 178 | |
| 179 | classpathProtoInfo := ClasspathFragmentProtoContentInfo{ |
satayev | b98371c | 2021-06-15 16:49:50 +0100 | [diff] [blame] | 180 | ClasspathFragmentProtoGenerated: generateProto, |
satayev | b309050 | 2021-06-15 17:49:10 +0100 | [diff] [blame] | 181 | ClasspathFragmentProtoContents: configuredJars, |
satayev | 14e4913 | 2021-05-17 21:03:07 +0100 | [diff] [blame] | 182 | ClasspathFragmentProtoInstallDir: c.installDirPath, |
| 183 | ClasspathFragmentProtoOutput: c.outputFilepath, |
| 184 | } |
Colin Cross | 4021302 | 2023-12-13 15:19:49 -0800 | [diff] [blame] | 185 | android.SetProvider(ctx, ClasspathFragmentProtoContentInfoProvider, classpathProtoInfo) |
Artur Satayev | 97259dc | 2021-04-07 15:17:14 +0100 | [diff] [blame] | 186 | } |
| 187 | |
Inseob Kim | dd53249 | 2024-04-30 17:22:58 +0900 | [diff] [blame^] | 188 | func (c *ClasspathFragmentBase) installClasspathProto(ctx android.ModuleContext) android.InstallPath { |
| 189 | return ctx.InstallFile(c.installDirPath, c.outputFilename(), c.outputFilepath) |
| 190 | } |
| 191 | |
satayev | 48dae67 | 2021-12-01 18:24:09 +0000 | [diff] [blame] | 192 | func writeClasspathsTextproto(ctx android.ModuleContext, output android.WritablePath, jars []classpathJar) { |
Artur Satayev | 97259dc | 2021-04-07 15:17:14 +0100 | [diff] [blame] | 193 | var content strings.Builder |
satayev | cca4ab7 | 2021-11-30 12:33:55 +0000 | [diff] [blame] | 194 | |
satayev | 48dae67 | 2021-12-01 18:24:09 +0000 | [diff] [blame] | 195 | for _, jar := range jars { |
| 196 | fmt.Fprintf(&content, "jars {\n") |
| 197 | fmt.Fprintf(&content, "path: \"%s\"\n", jar.path) |
| 198 | fmt.Fprintf(&content, "classpath: %s\n", jar.classpath) |
| 199 | fmt.Fprintf(&content, "min_sdk_version: \"%s\"\n", jar.minSdkVersion) |
| 200 | fmt.Fprintf(&content, "max_sdk_version: \"%s\"\n", jar.maxSdkVersion) |
| 201 | fmt.Fprintf(&content, "}\n") |
Artur Satayev | 97259dc | 2021-04-07 15:17:14 +0100 | [diff] [blame] | 202 | } |
satayev | cca4ab7 | 2021-11-30 12:33:55 +0000 | [diff] [blame] | 203 | |
Artur Satayev | 97259dc | 2021-04-07 15:17:14 +0100 | [diff] [blame] | 204 | android.WriteFileRule(ctx, output, content.String()) |
| 205 | } |
| 206 | |
satayev | 3db3547 | 2021-05-06 23:59:58 +0100 | [diff] [blame] | 207 | // Returns AndroidMkEntries objects to install generated classpath.proto. |
| 208 | // Do not use this to install into APEXes as the injection of the generated files happen separately for APEXes. |
satayev | 128ce2f | 2021-05-06 13:21:15 +0100 | [diff] [blame] | 209 | func (c *ClasspathFragmentBase) androidMkEntries() []android.AndroidMkEntries { |
satayev | 013485b | 2021-05-06 23:38:10 +0100 | [diff] [blame] | 210 | return []android.AndroidMkEntries{{ |
Artur Satayev | 97259dc | 2021-04-07 15:17:14 +0100 | [diff] [blame] | 211 | Class: "ETC", |
| 212 | OutputFile: android.OptionalPathForPath(c.outputFilepath), |
| 213 | ExtraEntries: []android.AndroidMkExtraEntriesFunc{ |
| 214 | func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) { |
Colin Cross | c68db4b | 2021-11-11 18:59:15 -0800 | [diff] [blame] | 215 | entries.SetString("LOCAL_MODULE_PATH", c.installDirPath.String()) |
Artur Satayev | 97259dc | 2021-04-07 15:17:14 +0100 | [diff] [blame] | 216 | entries.SetString("LOCAL_INSTALLED_MODULE_STEM", c.outputFilepath.Base()) |
| 217 | }, |
| 218 | }, |
| 219 | }} |
| 220 | } |
satayev | 14e4913 | 2021-05-17 21:03:07 +0100 | [diff] [blame] | 221 | |
Colin Cross | bc7d76c | 2023-12-12 16:39:03 -0800 | [diff] [blame] | 222 | var ClasspathFragmentProtoContentInfoProvider = blueprint.NewProvider[ClasspathFragmentProtoContentInfo]() |
satayev | 14e4913 | 2021-05-17 21:03:07 +0100 | [diff] [blame] | 223 | |
| 224 | type ClasspathFragmentProtoContentInfo struct { |
satayev | b98371c | 2021-06-15 16:49:50 +0100 | [diff] [blame] | 225 | // Whether the classpaths.proto config is generated for the fragment. |
| 226 | ClasspathFragmentProtoGenerated bool |
| 227 | |
satayev | b309050 | 2021-06-15 17:49:10 +0100 | [diff] [blame] | 228 | // ClasspathFragmentProtoContents contains a list of jars that are part of this classpath fragment. |
| 229 | ClasspathFragmentProtoContents android.ConfiguredJarList |
| 230 | |
satayev | 14e4913 | 2021-05-17 21:03:07 +0100 | [diff] [blame] | 231 | // ClasspathFragmentProtoOutput is an output path for the generated classpaths.proto config of this module. |
| 232 | // |
| 233 | // The file should be copied to a relevant place on device, see ClasspathFragmentProtoInstallDir |
| 234 | // for more details. |
| 235 | ClasspathFragmentProtoOutput android.OutputPath |
| 236 | |
| 237 | // ClasspathFragmentProtoInstallDir contains information about on device location for the generated classpaths.proto file. |
| 238 | // |
| 239 | // The path encodes expected sub-location within partitions, i.e. etc/classpaths/<proto-file>, |
| 240 | // for ClasspathFragmentProtoOutput. To get sub-location, instead of the full output / make path |
| 241 | // use android.InstallPath#Rel(). |
| 242 | // |
| 243 | // This is only relevant for APEX modules as they perform their own installation; while regular |
| 244 | // system files are installed via ClasspathFragmentBase#androidMkEntries(). |
| 245 | ClasspathFragmentProtoInstallDir android.InstallPath |
| 246 | } |