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 { |
| 88 | path string |
| 89 | classpath classpathType |
| 90 | // TODO(satayev): propagate min/max sdk versions for the jars |
| 91 | minSdkVersion int32 |
| 92 | maxSdkVersion int32 |
| 93 | } |
| 94 | |
satayev | d604b21 | 2021-07-21 14:23:52 +0100 | [diff] [blame] | 95 | // gatherPossibleApexModuleNamesAndStems returns a set of module and stem names from the |
| 96 | // supplied contents that may be in the apex boot jars. |
Paul Duffin | 56c93e8 | 2021-06-29 20:04:45 +0100 | [diff] [blame] | 97 | // |
| 98 | // The module names are included because sometimes the stem is set to just change the name of |
| 99 | // the installed file and it expects the configuration to still use the actual module name. |
| 100 | // |
| 101 | // The stem names are included because sometimes the stem is set to change the effective name of the |
| 102 | // module that is used in the configuration as well,e .g. when a test library is overriding an |
| 103 | // actual boot jar |
satayev | d604b21 | 2021-07-21 14:23:52 +0100 | [diff] [blame] | 104 | func gatherPossibleApexModuleNamesAndStems(ctx android.ModuleContext, contents []string, tag blueprint.DependencyTag) []string { |
Paul Duffin | 56c93e8 | 2021-06-29 20:04:45 +0100 | [diff] [blame] | 105 | set := map[string]struct{}{} |
| 106 | for _, name := range contents { |
| 107 | dep := ctx.GetDirectDepWithTag(name, tag) |
| 108 | set[name] = struct{}{} |
| 109 | if m, ok := dep.(ModuleWithStem); ok { |
| 110 | set[m.Stem()] = struct{}{} |
| 111 | } else { |
| 112 | ctx.PropertyErrorf("contents", "%v is not a ModuleWithStem", name) |
| 113 | } |
| 114 | } |
| 115 | return android.SortedStringKeys(set) |
| 116 | } |
| 117 | |
satayev | 013485b | 2021-05-06 23:38:10 +0100 | [diff] [blame] | 118 | // Converts android.ConfiguredJarList into a list of classpathJars for each given classpathType. |
| 119 | func configuredJarListToClasspathJars(ctx android.ModuleContext, configuredJars android.ConfiguredJarList, classpaths ...classpathType) []classpathJar { |
| 120 | paths := configuredJars.DevicePaths(ctx.Config(), android.Android) |
| 121 | jars := make([]classpathJar, 0, len(paths)*len(classpaths)) |
| 122 | for i := 0; i < len(paths); i++ { |
| 123 | for _, classpathType := range classpaths { |
| 124 | jars = append(jars, classpathJar{ |
| 125 | classpath: classpathType, |
| 126 | path: paths[i], |
| 127 | }) |
| 128 | } |
| 129 | } |
| 130 | return jars |
| 131 | } |
| 132 | |
satayev | b309050 | 2021-06-15 17:49:10 +0100 | [diff] [blame] | 133 | func (c *ClasspathFragmentBase) generateClasspathProtoBuildActions(ctx android.ModuleContext, configuredJars android.ConfiguredJarList, jars []classpathJar) { |
satayev | b98371c | 2021-06-15 16:49:50 +0100 | [diff] [blame] | 134 | generateProto := proptools.BoolDefault(c.properties.Generate_classpaths_proto, true) |
| 135 | if generateProto { |
| 136 | outputFilename := strings.ToLower(c.classpathType.String()) + ".pb" |
| 137 | c.outputFilepath = android.PathForModuleOut(ctx, outputFilename).OutputPath |
| 138 | c.installDirPath = android.PathForModuleInstall(ctx, "etc", "classpaths") |
Artur Satayev | 97259dc | 2021-04-07 15:17:14 +0100 | [diff] [blame] | 139 | |
satayev | b98371c | 2021-06-15 16:49:50 +0100 | [diff] [blame] | 140 | generatedJson := android.PathForModuleOut(ctx, outputFilename+".json") |
| 141 | writeClasspathsJson(ctx, generatedJson, jars) |
Artur Satayev | 97259dc | 2021-04-07 15:17:14 +0100 | [diff] [blame] | 142 | |
satayev | b98371c | 2021-06-15 16:49:50 +0100 | [diff] [blame] | 143 | rule := android.NewRuleBuilder(pctx, ctx) |
| 144 | rule.Command(). |
| 145 | BuiltTool("conv_classpaths_proto"). |
| 146 | Flag("encode"). |
| 147 | Flag("--format=json"). |
| 148 | FlagWithInput("--input=", generatedJson). |
| 149 | FlagWithOutput("--output=", c.outputFilepath) |
Artur Satayev | 97259dc | 2021-04-07 15:17:14 +0100 | [diff] [blame] | 150 | |
satayev | b98371c | 2021-06-15 16:49:50 +0100 | [diff] [blame] | 151 | rule.Build("classpath_fragment", "Compiling "+c.outputFilepath.String()) |
| 152 | } |
satayev | 14e4913 | 2021-05-17 21:03:07 +0100 | [diff] [blame] | 153 | |
| 154 | classpathProtoInfo := ClasspathFragmentProtoContentInfo{ |
satayev | b98371c | 2021-06-15 16:49:50 +0100 | [diff] [blame] | 155 | ClasspathFragmentProtoGenerated: generateProto, |
satayev | b309050 | 2021-06-15 17:49:10 +0100 | [diff] [blame] | 156 | ClasspathFragmentProtoContents: configuredJars, |
satayev | 14e4913 | 2021-05-17 21:03:07 +0100 | [diff] [blame] | 157 | ClasspathFragmentProtoInstallDir: c.installDirPath, |
| 158 | ClasspathFragmentProtoOutput: c.outputFilepath, |
| 159 | } |
| 160 | ctx.SetProvider(ClasspathFragmentProtoContentInfoProvider, classpathProtoInfo) |
Artur Satayev | 97259dc | 2021-04-07 15:17:14 +0100 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | func writeClasspathsJson(ctx android.ModuleContext, output android.WritablePath, jars []classpathJar) { |
| 164 | var content strings.Builder |
| 165 | fmt.Fprintf(&content, "{\n") |
| 166 | fmt.Fprintf(&content, "\"jars\": [\n") |
| 167 | for idx, jar := range jars { |
| 168 | fmt.Fprintf(&content, "{\n") |
| 169 | |
satayev | 7d22657 | 2021-05-18 19:37:32 +0100 | [diff] [blame] | 170 | fmt.Fprintf(&content, "\"path\": \"%s\",\n", jar.path) |
Artur Satayev | 97259dc | 2021-04-07 15:17:14 +0100 | [diff] [blame] | 171 | fmt.Fprintf(&content, "\"classpath\": \"%s\"\n", jar.classpath) |
| 172 | |
| 173 | if idx < len(jars)-1 { |
| 174 | fmt.Fprintf(&content, "},\n") |
| 175 | } else { |
| 176 | fmt.Fprintf(&content, "}\n") |
| 177 | } |
| 178 | } |
| 179 | fmt.Fprintf(&content, "]\n") |
| 180 | fmt.Fprintf(&content, "}\n") |
| 181 | android.WriteFileRule(ctx, output, content.String()) |
| 182 | } |
| 183 | |
satayev | 3db3547 | 2021-05-06 23:59:58 +0100 | [diff] [blame] | 184 | // Returns AndroidMkEntries objects to install generated classpath.proto. |
| 185 | // 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] | 186 | func (c *ClasspathFragmentBase) androidMkEntries() []android.AndroidMkEntries { |
satayev | 013485b | 2021-05-06 23:38:10 +0100 | [diff] [blame] | 187 | return []android.AndroidMkEntries{{ |
Artur Satayev | 97259dc | 2021-04-07 15:17:14 +0100 | [diff] [blame] | 188 | Class: "ETC", |
| 189 | OutputFile: android.OptionalPathForPath(c.outputFilepath), |
| 190 | ExtraEntries: []android.AndroidMkExtraEntriesFunc{ |
| 191 | func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) { |
| 192 | entries.SetString("LOCAL_MODULE_PATH", c.installDirPath.ToMakePath().String()) |
| 193 | entries.SetString("LOCAL_INSTALLED_MODULE_STEM", c.outputFilepath.Base()) |
| 194 | }, |
| 195 | }, |
| 196 | }} |
| 197 | } |
satayev | 14e4913 | 2021-05-17 21:03:07 +0100 | [diff] [blame] | 198 | |
| 199 | var ClasspathFragmentProtoContentInfoProvider = blueprint.NewProvider(ClasspathFragmentProtoContentInfo{}) |
| 200 | |
| 201 | type ClasspathFragmentProtoContentInfo struct { |
satayev | b98371c | 2021-06-15 16:49:50 +0100 | [diff] [blame] | 202 | // Whether the classpaths.proto config is generated for the fragment. |
| 203 | ClasspathFragmentProtoGenerated bool |
| 204 | |
satayev | b309050 | 2021-06-15 17:49:10 +0100 | [diff] [blame] | 205 | // ClasspathFragmentProtoContents contains a list of jars that are part of this classpath fragment. |
| 206 | ClasspathFragmentProtoContents android.ConfiguredJarList |
| 207 | |
satayev | 14e4913 | 2021-05-17 21:03:07 +0100 | [diff] [blame] | 208 | // ClasspathFragmentProtoOutput is an output path for the generated classpaths.proto config of this module. |
| 209 | // |
| 210 | // The file should be copied to a relevant place on device, see ClasspathFragmentProtoInstallDir |
| 211 | // for more details. |
| 212 | ClasspathFragmentProtoOutput android.OutputPath |
| 213 | |
| 214 | // ClasspathFragmentProtoInstallDir contains information about on device location for the generated classpaths.proto file. |
| 215 | // |
| 216 | // The path encodes expected sub-location within partitions, i.e. etc/classpaths/<proto-file>, |
| 217 | // for ClasspathFragmentProtoOutput. To get sub-location, instead of the full output / make path |
| 218 | // use android.InstallPath#Rel(). |
| 219 | // |
| 220 | // This is only relevant for APEX modules as they perform their own installation; while regular |
| 221 | // system files are installed via ClasspathFragmentBase#androidMkEntries(). |
| 222 | ClasspathFragmentProtoInstallDir android.InstallPath |
| 223 | } |