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 | |
| 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 | |
| 34 | type classpathType int |
| 35 | |
| 36 | const ( |
| 37 | // Matches definition in packages/modules/SdkExtensions/proto/classpaths.proto |
| 38 | BOOTCLASSPATH classpathType = iota |
| 39 | DEX2OATBOOTCLASSPATH |
| 40 | SYSTEMSERVERCLASSPATH |
| 41 | ) |
| 42 | |
| 43 | func (c classpathType) String() string { |
| 44 | return [...]string{"BOOTCLASSPATH", "DEX2OATBOOTCLASSPATH", "SYSTEMSERVERCLASSPATH"}[c] |
| 45 | } |
| 46 | |
| 47 | type classpathFragmentProperties struct { |
satayev | b98371c | 2021-06-15 16:49:50 +0100 | [diff] [blame^] | 48 | // 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 Satayev | eabf2c1 | 2021-04-07 15:45:02 +0100 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | // classpathFragment interface is implemented by a module that contributes jars to a *CLASSPATH |
| 56 | // variables at runtime. |
| 57 | type classpathFragment interface { |
| 58 | android.Module |
| 59 | |
Artur Satayev | 97259dc | 2021-04-07 15:17:14 +0100 | [diff] [blame] | 60 | classpathFragmentBase() *ClasspathFragmentBase |
Artur Satayev | eabf2c1 | 2021-04-07 15:45:02 +0100 | [diff] [blame] | 61 | } |
| 62 | |
Artur Satayev | 97259dc | 2021-04-07 15:17:14 +0100 | [diff] [blame] | 63 | // 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] | 64 | // such modules are expected to call initClasspathFragment(). |
Artur Satayev | 97259dc | 2021-04-07 15:17:14 +0100 | [diff] [blame] | 65 | type ClasspathFragmentBase struct { |
Artur Satayev | eabf2c1 | 2021-04-07 15:45:02 +0100 | [diff] [blame] | 66 | properties classpathFragmentProperties |
| 67 | |
satayev | 95e9c5b | 2021-04-29 11:50:26 +0100 | [diff] [blame] | 68 | classpathType classpathType |
| 69 | |
Artur Satayev | eabf2c1 | 2021-04-07 15:45:02 +0100 | [diff] [blame] | 70 | outputFilepath android.OutputPath |
Artur Satayev | 97259dc | 2021-04-07 15:17:14 +0100 | [diff] [blame] | 71 | installDirPath android.InstallPath |
Artur Satayev | eabf2c1 | 2021-04-07 15:45:02 +0100 | [diff] [blame] | 72 | } |
| 73 | |
Artur Satayev | 97259dc | 2021-04-07 15:17:14 +0100 | [diff] [blame] | 74 | func (c *ClasspathFragmentBase) classpathFragmentBase() *ClasspathFragmentBase { |
| 75 | return c |
| 76 | } |
| 77 | |
| 78 | // Initializes ClasspathFragmentBase struct. Must be called by all modules that include ClasspathFragmentBase. |
satayev | 95e9c5b | 2021-04-29 11:50:26 +0100 | [diff] [blame] | 79 | func initClasspathFragment(c classpathFragment, classpathType classpathType) { |
Artur Satayev | eabf2c1 | 2021-04-07 15:45:02 +0100 | [diff] [blame] | 80 | base := c.classpathFragmentBase() |
satayev | 95e9c5b | 2021-04-29 11:50:26 +0100 | [diff] [blame] | 81 | base.classpathType = classpathType |
Artur Satayev | eabf2c1 | 2021-04-07 15:45:02 +0100 | [diff] [blame] | 82 | c.AddProperties(&base.properties) |
| 83 | } |
Artur Satayev | 97259dc | 2021-04-07 15:17:14 +0100 | [diff] [blame] | 84 | |
| 85 | // Matches definition of Jar in packages/modules/SdkExtensions/proto/classpaths.proto |
| 86 | type 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 | |
satayev | 013485b | 2021-05-06 23:38:10 +0100 | [diff] [blame] | 94 | // Converts android.ConfiguredJarList into a list of classpathJars for each given classpathType. |
| 95 | func configuredJarListToClasspathJars(ctx android.ModuleContext, configuredJars android.ConfiguredJarList, classpaths ...classpathType) []classpathJar { |
| 96 | paths := configuredJars.DevicePaths(ctx.Config(), android.Android) |
| 97 | jars := make([]classpathJar, 0, len(paths)*len(classpaths)) |
| 98 | for i := 0; i < len(paths); i++ { |
| 99 | for _, classpathType := range classpaths { |
| 100 | jars = append(jars, classpathJar{ |
| 101 | classpath: classpathType, |
| 102 | path: paths[i], |
| 103 | }) |
| 104 | } |
| 105 | } |
| 106 | return jars |
| 107 | } |
| 108 | |
| 109 | func (c *ClasspathFragmentBase) generateClasspathProtoBuildActions(ctx android.ModuleContext, jars []classpathJar) { |
satayev | b98371c | 2021-06-15 16:49:50 +0100 | [diff] [blame^] | 110 | generateProto := proptools.BoolDefault(c.properties.Generate_classpaths_proto, true) |
| 111 | if generateProto { |
| 112 | outputFilename := strings.ToLower(c.classpathType.String()) + ".pb" |
| 113 | c.outputFilepath = android.PathForModuleOut(ctx, outputFilename).OutputPath |
| 114 | c.installDirPath = android.PathForModuleInstall(ctx, "etc", "classpaths") |
Artur Satayev | 97259dc | 2021-04-07 15:17:14 +0100 | [diff] [blame] | 115 | |
satayev | b98371c | 2021-06-15 16:49:50 +0100 | [diff] [blame^] | 116 | generatedJson := android.PathForModuleOut(ctx, outputFilename+".json") |
| 117 | writeClasspathsJson(ctx, generatedJson, jars) |
Artur Satayev | 97259dc | 2021-04-07 15:17:14 +0100 | [diff] [blame] | 118 | |
satayev | b98371c | 2021-06-15 16:49:50 +0100 | [diff] [blame^] | 119 | rule := android.NewRuleBuilder(pctx, ctx) |
| 120 | rule.Command(). |
| 121 | BuiltTool("conv_classpaths_proto"). |
| 122 | Flag("encode"). |
| 123 | Flag("--format=json"). |
| 124 | FlagWithInput("--input=", generatedJson). |
| 125 | FlagWithOutput("--output=", c.outputFilepath) |
Artur Satayev | 97259dc | 2021-04-07 15:17:14 +0100 | [diff] [blame] | 126 | |
satayev | b98371c | 2021-06-15 16:49:50 +0100 | [diff] [blame^] | 127 | rule.Build("classpath_fragment", "Compiling "+c.outputFilepath.String()) |
| 128 | } |
satayev | 14e4913 | 2021-05-17 21:03:07 +0100 | [diff] [blame] | 129 | |
| 130 | classpathProtoInfo := ClasspathFragmentProtoContentInfo{ |
satayev | b98371c | 2021-06-15 16:49:50 +0100 | [diff] [blame^] | 131 | ClasspathFragmentProtoGenerated: generateProto, |
satayev | 14e4913 | 2021-05-17 21:03:07 +0100 | [diff] [blame] | 132 | ClasspathFragmentProtoInstallDir: c.installDirPath, |
| 133 | ClasspathFragmentProtoOutput: c.outputFilepath, |
| 134 | } |
| 135 | ctx.SetProvider(ClasspathFragmentProtoContentInfoProvider, classpathProtoInfo) |
Artur Satayev | 97259dc | 2021-04-07 15:17:14 +0100 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | func writeClasspathsJson(ctx android.ModuleContext, output android.WritablePath, jars []classpathJar) { |
| 139 | var content strings.Builder |
| 140 | fmt.Fprintf(&content, "{\n") |
| 141 | fmt.Fprintf(&content, "\"jars\": [\n") |
| 142 | for idx, jar := range jars { |
| 143 | fmt.Fprintf(&content, "{\n") |
| 144 | |
satayev | 7d22657 | 2021-05-18 19:37:32 +0100 | [diff] [blame] | 145 | fmt.Fprintf(&content, "\"path\": \"%s\",\n", jar.path) |
Artur Satayev | 97259dc | 2021-04-07 15:17:14 +0100 | [diff] [blame] | 146 | fmt.Fprintf(&content, "\"classpath\": \"%s\"\n", jar.classpath) |
| 147 | |
| 148 | if idx < len(jars)-1 { |
| 149 | fmt.Fprintf(&content, "},\n") |
| 150 | } else { |
| 151 | fmt.Fprintf(&content, "}\n") |
| 152 | } |
| 153 | } |
| 154 | fmt.Fprintf(&content, "]\n") |
| 155 | fmt.Fprintf(&content, "}\n") |
| 156 | android.WriteFileRule(ctx, output, content.String()) |
| 157 | } |
| 158 | |
satayev | 3db3547 | 2021-05-06 23:59:58 +0100 | [diff] [blame] | 159 | // Returns AndroidMkEntries objects to install generated classpath.proto. |
| 160 | // 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] | 161 | func (c *ClasspathFragmentBase) androidMkEntries() []android.AndroidMkEntries { |
satayev | 013485b | 2021-05-06 23:38:10 +0100 | [diff] [blame] | 162 | return []android.AndroidMkEntries{{ |
Artur Satayev | 97259dc | 2021-04-07 15:17:14 +0100 | [diff] [blame] | 163 | Class: "ETC", |
| 164 | OutputFile: android.OptionalPathForPath(c.outputFilepath), |
| 165 | ExtraEntries: []android.AndroidMkExtraEntriesFunc{ |
| 166 | func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) { |
| 167 | entries.SetString("LOCAL_MODULE_PATH", c.installDirPath.ToMakePath().String()) |
| 168 | entries.SetString("LOCAL_INSTALLED_MODULE_STEM", c.outputFilepath.Base()) |
| 169 | }, |
| 170 | }, |
| 171 | }} |
| 172 | } |
satayev | 14e4913 | 2021-05-17 21:03:07 +0100 | [diff] [blame] | 173 | |
| 174 | var ClasspathFragmentProtoContentInfoProvider = blueprint.NewProvider(ClasspathFragmentProtoContentInfo{}) |
| 175 | |
| 176 | type ClasspathFragmentProtoContentInfo struct { |
satayev | b98371c | 2021-06-15 16:49:50 +0100 | [diff] [blame^] | 177 | // Whether the classpaths.proto config is generated for the fragment. |
| 178 | ClasspathFragmentProtoGenerated bool |
| 179 | |
satayev | 14e4913 | 2021-05-17 21:03:07 +0100 | [diff] [blame] | 180 | // ClasspathFragmentProtoOutput is an output path for the generated classpaths.proto config of this module. |
| 181 | // |
| 182 | // The file should be copied to a relevant place on device, see ClasspathFragmentProtoInstallDir |
| 183 | // for more details. |
| 184 | ClasspathFragmentProtoOutput android.OutputPath |
| 185 | |
| 186 | // ClasspathFragmentProtoInstallDir contains information about on device location for the generated classpaths.proto file. |
| 187 | // |
| 188 | // The path encodes expected sub-location within partitions, i.e. etc/classpaths/<proto-file>, |
| 189 | // for ClasspathFragmentProtoOutput. To get sub-location, instead of the full output / make path |
| 190 | // use android.InstallPath#Rel(). |
| 191 | // |
| 192 | // This is only relevant for APEX modules as they perform their own installation; while regular |
| 193 | // system files are installed via ClasspathFragmentBase#androidMkEntries(). |
| 194 | ClasspathFragmentProtoInstallDir android.InstallPath |
| 195 | } |