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