| Colin Cross | 1369cdb | 2017-09-29 17:58:17 -0700 | [diff] [blame] | 1 | // Copyright 2017 Google Inc. All rights reserved. | 
|  | 2 | // Licensed under the Apache License, Version 2.0 (the "License"); | 
|  | 3 | // you may not use this file except in compliance with the License. | 
|  | 4 | // You may obtain a copy of the License at | 
|  | 5 | // | 
|  | 6 | //     http://www.apache.org/licenses/LICENSE-2.0 | 
|  | 7 | // | 
|  | 8 | // Unless required by applicable law or agreed to in writing, software | 
|  | 9 | // distributed under the License is distributed on an "AS IS" BASIS, | 
|  | 10 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
|  | 11 | // See the License for the specific language governing permissions and | 
|  | 12 | // limitations under the License. | 
|  | 13 |  | 
|  | 14 | package java | 
|  | 15 |  | 
|  | 16 | import ( | 
|  | 17 | "fmt" | 
|  | 18 | "io" | 
|  | 19 | "strings" | 
|  | 20 |  | 
|  | 21 | "github.com/google/blueprint" | 
|  | 22 |  | 
|  | 23 | "android/soong/android" | 
|  | 24 | ) | 
|  | 25 |  | 
|  | 26 | // OpenJDK 9 introduces the concept of "system modules", which replace the bootclasspath.  This | 
|  | 27 | // file will produce the rules necessary to convert each unique set of bootclasspath jars into | 
|  | 28 | // system modules in a runtime image using the jmod and jlink tools. | 
|  | 29 |  | 
|  | 30 | func init() { | 
| Paul Duffin | 43dc1cc | 2019-12-19 11:18:54 +0000 | [diff] [blame] | 31 | RegisterSystemModulesBuildComponents(android.InitRegistrationContext) | 
| Colin Cross | 1369cdb | 2017-09-29 17:58:17 -0700 | [diff] [blame] | 32 |  | 
|  | 33 | pctx.SourcePathVariable("moduleInfoJavaPath", "build/soong/scripts/jars-to-module-info-java.sh") | 
| Paul Duffin | 7b81f5e | 2020-01-13 21:03:22 +0000 | [diff] [blame] | 34 |  | 
|  | 35 | // Register sdk member types. | 
|  | 36 | android.RegisterSdkMemberType(&systemModulesSdkMemberType{ | 
|  | 37 | android.SdkMemberTypeBase{ | 
| Paul Duffin | 2d3da31 | 2021-05-06 12:02:27 +0100 | [diff] [blame] | 38 | PropertyName: "java_system_modules", | 
|  | 39 | SupportsSdk:  true, | 
| Paul Duffin | 7b81f5e | 2020-01-13 21:03:22 +0000 | [diff] [blame] | 40 | }, | 
|  | 41 | }) | 
| Colin Cross | 1369cdb | 2017-09-29 17:58:17 -0700 | [diff] [blame] | 42 | } | 
|  | 43 |  | 
| Paul Duffin | 43dc1cc | 2019-12-19 11:18:54 +0000 | [diff] [blame] | 44 | func RegisterSystemModulesBuildComponents(ctx android.RegistrationContext) { | 
|  | 45 | ctx.RegisterModuleType("java_system_modules", SystemModulesFactory) | 
| Paul Duffin | 90169ba | 2020-01-10 17:16:44 +0000 | [diff] [blame] | 46 | ctx.RegisterModuleType("java_system_modules_import", systemModulesImportFactory) | 
| Paul Duffin | 43dc1cc | 2019-12-19 11:18:54 +0000 | [diff] [blame] | 47 | } | 
|  | 48 |  | 
| Colin Cross | 1369cdb | 2017-09-29 17:58:17 -0700 | [diff] [blame] | 49 | var ( | 
|  | 50 | jarsTosystemModules = pctx.AndroidStaticRule("jarsTosystemModules", blueprint.RuleParams{ | 
|  | 51 | Command: `rm -rf ${outDir} ${workDir} && mkdir -p ${workDir}/jmod && ` + | 
| Pete Gillin | df7dc82 | 2019-10-09 17:09:38 +0100 | [diff] [blame] | 52 | `${moduleInfoJavaPath} java.base $in > ${workDir}/module-info.java && ` + | 
| Colin Cross | 1369cdb | 2017-09-29 17:58:17 -0700 | [diff] [blame] | 53 | `${config.JavacCmd} --system=none --patch-module=java.base=${classpath} ${workDir}/module-info.java && ` + | 
|  | 54 | `${config.SoongZipCmd} -jar -o ${workDir}/classes.jar -C ${workDir} -f ${workDir}/module-info.class && ` + | 
|  | 55 | `${config.MergeZipsCmd} -j ${workDir}/module.jar ${workDir}/classes.jar $in && ` + | 
| Pete Gillin | 1f52e93 | 2019-10-09 17:10:08 +0100 | [diff] [blame] | 56 | // Note: The version of the java.base module created must match the version | 
|  | 57 | // of the jlink tool which consumes it. | 
|  | 58 | `${config.JmodCmd} create --module-version ${config.JlinkVersion} --target-platform android ` + | 
| Pete Gillin | df7dc82 | 2019-10-09 17:09:38 +0100 | [diff] [blame] | 59 | `  --class-path ${workDir}/module.jar ${workDir}/jmod/java.base.jmod && ` + | 
|  | 60 | `${config.JlinkCmd} --module-path ${workDir}/jmod --add-modules java.base --output ${outDir} ` + | 
| Pete Gillin | 4eb6be3 | 2019-06-05 20:10:55 +0100 | [diff] [blame] | 61 | // Note: The system-modules jlink plugin is disabled because (a) it is not | 
|  | 62 | // useful on Android, and (b) it causes errors with later versions of jlink | 
|  | 63 | // when the jdk.internal.module is absent from java.base (as it is here). | 
|  | 64 | `  --disable-plugin system-modules && ` + | 
| Colin Cross | 1369cdb | 2017-09-29 17:58:17 -0700 | [diff] [blame] | 65 | `cp ${config.JrtFsJar} ${outDir}/lib/`, | 
|  | 66 | CommandDeps: []string{ | 
|  | 67 | "${moduleInfoJavaPath}", | 
|  | 68 | "${config.JavacCmd}", | 
|  | 69 | "${config.SoongZipCmd}", | 
|  | 70 | "${config.MergeZipsCmd}", | 
|  | 71 | "${config.JmodCmd}", | 
|  | 72 | "${config.JlinkCmd}", | 
|  | 73 | "${config.JrtFsJar}", | 
|  | 74 | }, | 
|  | 75 | }, | 
| Pete Gillin | df7dc82 | 2019-10-09 17:09:38 +0100 | [diff] [blame] | 76 | "classpath", "outDir", "workDir") | 
| Paul Duffin | 7b81f5e | 2020-01-13 21:03:22 +0000 | [diff] [blame] | 77 |  | 
|  | 78 | // Dependency tag that causes the added dependencies to be added as java_header_libs | 
| Paul Duffin | a720811 | 2021-04-23 21:20:20 +0100 | [diff] [blame] | 79 | // to the sdk/module_exports/snapshot. Dependencies that are added automatically via this tag are | 
|  | 80 | // not automatically exported. | 
|  | 81 | systemModulesLibsTag = android.DependencyTagForSdkMemberType(javaHeaderLibsSdkMemberType, false) | 
| Colin Cross | 1369cdb | 2017-09-29 17:58:17 -0700 | [diff] [blame] | 82 | ) | 
|  | 83 |  | 
| Pete Gillin | df7dc82 | 2019-10-09 17:09:38 +0100 | [diff] [blame] | 84 | func TransformJarsToSystemModules(ctx android.ModuleContext, jars android.Paths) (android.Path, android.Paths) { | 
| Colin Cross | 1369cdb | 2017-09-29 17:58:17 -0700 | [diff] [blame] | 85 | outDir := android.PathForModuleOut(ctx, "system") | 
|  | 86 | workDir := android.PathForModuleOut(ctx, "modules") | 
|  | 87 | outputFile := android.PathForModuleOut(ctx, "system/lib/modules") | 
|  | 88 | outputs := android.WritablePaths{ | 
|  | 89 | outputFile, | 
|  | 90 | android.PathForModuleOut(ctx, "system/lib/jrt-fs.jar"), | 
|  | 91 | android.PathForModuleOut(ctx, "system/release"), | 
|  | 92 | } | 
|  | 93 |  | 
| Colin Cross | ae88703 | 2017-10-23 17:16:14 -0700 | [diff] [blame] | 94 | ctx.Build(pctx, android.BuildParams{ | 
| Colin Cross | 1369cdb | 2017-09-29 17:58:17 -0700 | [diff] [blame] | 95 | Rule:        jarsTosystemModules, | 
|  | 96 | Description: "system modules", | 
|  | 97 | Outputs:     outputs, | 
|  | 98 | Inputs:      jars, | 
|  | 99 | Args: map[string]string{ | 
| Pete Gillin | df7dc82 | 2019-10-09 17:09:38 +0100 | [diff] [blame] | 100 | "classpath": strings.Join(jars.Strings(), ":"), | 
|  | 101 | "workDir":   workDir.String(), | 
|  | 102 | "outDir":    outDir.String(), | 
| Colin Cross | 1369cdb | 2017-09-29 17:58:17 -0700 | [diff] [blame] | 103 | }, | 
|  | 104 | }) | 
|  | 105 |  | 
| Dan Willemsen | ff60a73 | 2019-06-13 16:52:01 +0000 | [diff] [blame] | 106 | return outDir, outputs.Paths() | 
| Colin Cross | 1369cdb | 2017-09-29 17:58:17 -0700 | [diff] [blame] | 107 | } | 
|  | 108 |  | 
| Paul Duffin | cded5ec | 2020-01-10 17:30:36 +0000 | [diff] [blame] | 109 | // java_system_modules creates a system module from a set of java libraries that can | 
|  | 110 | // be referenced from the system_modules property. It must contain at a minimum the | 
|  | 111 | // java.base module which must include classes from java.lang amongst other java packages. | 
| Colin Cross | 1369cdb | 2017-09-29 17:58:17 -0700 | [diff] [blame] | 112 | func SystemModulesFactory() android.Module { | 
|  | 113 | module := &SystemModules{} | 
|  | 114 | module.AddProperties(&module.properties) | 
|  | 115 | android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibCommon) | 
| Colin Cross | 667ffa1 | 2019-05-28 13:30:02 -0700 | [diff] [blame] | 116 | android.InitDefaultableModule(module) | 
| Colin Cross | 1369cdb | 2017-09-29 17:58:17 -0700 | [diff] [blame] | 117 | return module | 
|  | 118 | } | 
|  | 119 |  | 
| Paul Duffin | 83a2d96 | 2019-11-19 19:44:10 +0000 | [diff] [blame] | 120 | type SystemModulesProvider interface { | 
|  | 121 | HeaderJars() android.Paths | 
|  | 122 | OutputDirAndDeps() (android.Path, android.Paths) | 
|  | 123 | } | 
|  | 124 |  | 
|  | 125 | var _ SystemModulesProvider = (*SystemModules)(nil) | 
|  | 126 |  | 
|  | 127 | var _ SystemModulesProvider = (*systemModulesImport)(nil) | 
|  | 128 |  | 
| Colin Cross | 1369cdb | 2017-09-29 17:58:17 -0700 | [diff] [blame] | 129 | type SystemModules struct { | 
|  | 130 | android.ModuleBase | 
| Colin Cross | 667ffa1 | 2019-05-28 13:30:02 -0700 | [diff] [blame] | 131 | android.DefaultableModuleBase | 
| Colin Cross | 1369cdb | 2017-09-29 17:58:17 -0700 | [diff] [blame] | 132 |  | 
|  | 133 | properties SystemModulesProperties | 
|  | 134 |  | 
| Paul Duffin | 68289b0 | 2019-09-20 13:50:52 +0100 | [diff] [blame] | 135 | // The aggregated header jars from all jars specified in the libs property. | 
|  | 136 | // Used when system module is added as a dependency to bootclasspath. | 
|  | 137 | headerJars android.Paths | 
| Dan Willemsen | ff60a73 | 2019-06-13 16:52:01 +0000 | [diff] [blame] | 138 | outputDir  android.Path | 
|  | 139 | outputDeps android.Paths | 
| Colin Cross | 1369cdb | 2017-09-29 17:58:17 -0700 | [diff] [blame] | 140 | } | 
|  | 141 |  | 
|  | 142 | type SystemModulesProperties struct { | 
|  | 143 | // List of java library modules that should be included in the system modules | 
|  | 144 | Libs []string | 
| Colin Cross | 1369cdb | 2017-09-29 17:58:17 -0700 | [diff] [blame] | 145 | } | 
|  | 146 |  | 
| Paul Duffin | 83a2d96 | 2019-11-19 19:44:10 +0000 | [diff] [blame] | 147 | func (system *SystemModules) HeaderJars() android.Paths { | 
|  | 148 | return system.headerJars | 
|  | 149 | } | 
|  | 150 |  | 
|  | 151 | func (system *SystemModules) OutputDirAndDeps() (android.Path, android.Paths) { | 
|  | 152 | if system.outputDir == nil || len(system.outputDeps) == 0 { | 
|  | 153 | panic("Missing directory for system module dependency") | 
|  | 154 | } | 
|  | 155 | return system.outputDir, system.outputDeps | 
|  | 156 | } | 
|  | 157 |  | 
| Colin Cross | 1369cdb | 2017-09-29 17:58:17 -0700 | [diff] [blame] | 158 | func (system *SystemModules) GenerateAndroidBuildActions(ctx android.ModuleContext) { | 
|  | 159 | var jars android.Paths | 
|  | 160 |  | 
| Paul Duffin | 7b81f5e | 2020-01-13 21:03:22 +0000 | [diff] [blame] | 161 | ctx.VisitDirectDepsWithTag(systemModulesLibsTag, func(module android.Module) { | 
| Colin Cross | dcf71b2 | 2021-02-01 13:59:03 -0800 | [diff] [blame] | 162 | dep, _ := ctx.OtherModuleProvider(module, JavaInfoProvider).(JavaInfo) | 
|  | 163 | jars = append(jars, dep.HeaderJars...) | 
| Colin Cross | 1369cdb | 2017-09-29 17:58:17 -0700 | [diff] [blame] | 164 | }) | 
|  | 165 |  | 
| Paul Duffin | 68289b0 | 2019-09-20 13:50:52 +0100 | [diff] [blame] | 166 | system.headerJars = jars | 
|  | 167 |  | 
| Pete Gillin | df7dc82 | 2019-10-09 17:09:38 +0100 | [diff] [blame] | 168 | system.outputDir, system.outputDeps = TransformJarsToSystemModules(ctx, jars) | 
| Colin Cross | 1369cdb | 2017-09-29 17:58:17 -0700 | [diff] [blame] | 169 | } | 
|  | 170 |  | 
| Paul Duffin | 4f0a75a | 2021-03-11 07:23:27 +0000 | [diff] [blame] | 171 | // ComponentDepsMutator is called before prebuilt modules without a corresponding source module are | 
|  | 172 | // renamed so unless the supplied libs specifically includes the prebuilt_ prefix this is guaranteed | 
|  | 173 | // to only add dependencies on source modules. | 
|  | 174 | // | 
|  | 175 | // The systemModuleLibsTag will prevent the prebuilt mutators from replacing this dependency so it | 
|  | 176 | // will never be changed to depend on a prebuilt either. | 
|  | 177 | func (system *SystemModules) ComponentDepsMutator(ctx android.BottomUpMutatorContext) { | 
| Paul Duffin | 7b81f5e | 2020-01-13 21:03:22 +0000 | [diff] [blame] | 178 | ctx.AddVariationDependencies(nil, systemModulesLibsTag, system.properties.Libs...) | 
| Colin Cross | 1369cdb | 2017-09-29 17:58:17 -0700 | [diff] [blame] | 179 | } | 
|  | 180 |  | 
|  | 181 | func (system *SystemModules) AndroidMk() android.AndroidMkData { | 
|  | 182 | return android.AndroidMkData{ | 
|  | 183 | Custom: func(w io.Writer, name, prefix, moduleDir string, data android.AndroidMkData) { | 
| Tobias Thierer | b1c697d | 2018-03-26 22:33:59 +0100 | [diff] [blame] | 184 | fmt.Fprintln(w) | 
| Dan Willemsen | ff60a73 | 2019-06-13 16:52:01 +0000 | [diff] [blame] | 185 |  | 
|  | 186 | makevar := "SOONG_SYSTEM_MODULES_" + name | 
|  | 187 | fmt.Fprintln(w, makevar, ":=$=", system.outputDir.String()) | 
|  | 188 | fmt.Fprintln(w) | 
|  | 189 |  | 
|  | 190 | makevar = "SOONG_SYSTEM_MODULES_LIBS_" + name | 
|  | 191 | fmt.Fprintln(w, makevar, ":=$=", strings.Join(system.properties.Libs, " ")) | 
|  | 192 | fmt.Fprintln(w) | 
|  | 193 |  | 
| Dan Willemsen | fe310be | 2019-06-20 10:16:12 -0700 | [diff] [blame] | 194 | makevar = "SOONG_SYSTEM_MODULES_DEPS_" + name | 
| Dan Willemsen | ff60a73 | 2019-06-13 16:52:01 +0000 | [diff] [blame] | 195 | fmt.Fprintln(w, makevar, ":=$=", strings.Join(system.outputDeps.Strings(), " ")) | 
|  | 196 | fmt.Fprintln(w) | 
|  | 197 |  | 
| Tobias Thierer | b1c697d | 2018-03-26 22:33:59 +0100 | [diff] [blame] | 198 | fmt.Fprintln(w, name+":", "$("+makevar+")") | 
| Dan Willemsen | a03c43a | 2018-07-24 13:00:52 -0700 | [diff] [blame] | 199 | fmt.Fprintln(w, ".PHONY:", name) | 
| Bob Badour | b499922 | 2021-01-07 03:34:31 +0000 | [diff] [blame] | 200 | // TODO(b/151177513): Licenses: Doesn't go through base_rules. May have to generate meta_lic and meta_module here. | 
| Colin Cross | 1369cdb | 2017-09-29 17:58:17 -0700 | [diff] [blame] | 201 | }, | 
|  | 202 | } | 
|  | 203 | } | 
| Paul Duffin | 90169ba | 2020-01-10 17:16:44 +0000 | [diff] [blame] | 204 |  | 
|  | 205 | // A prebuilt version of java_system_modules. It does not import the | 
|  | 206 | // generated system module, it generates the system module from imported | 
|  | 207 | // java libraries in the same way that java_system_modules does. It just | 
|  | 208 | // acts as a prebuilt, i.e. can have the same base name as another module | 
|  | 209 | // type and the one to use is selected at runtime. | 
|  | 210 | func systemModulesImportFactory() android.Module { | 
|  | 211 | module := &systemModulesImport{} | 
|  | 212 | module.AddProperties(&module.properties) | 
|  | 213 | android.InitPrebuiltModule(module, &module.properties.Libs) | 
|  | 214 | android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibCommon) | 
|  | 215 | android.InitDefaultableModule(module) | 
|  | 216 | return module | 
|  | 217 | } | 
|  | 218 |  | 
|  | 219 | type systemModulesImport struct { | 
|  | 220 | SystemModules | 
|  | 221 | prebuilt android.Prebuilt | 
|  | 222 | } | 
|  | 223 |  | 
|  | 224 | func (system *systemModulesImport) Name() string { | 
|  | 225 | return system.prebuilt.Name(system.ModuleBase.Name()) | 
|  | 226 | } | 
|  | 227 |  | 
|  | 228 | func (system *systemModulesImport) Prebuilt() *android.Prebuilt { | 
|  | 229 | return &system.prebuilt | 
|  | 230 | } | 
| Paul Duffin | 7b81f5e | 2020-01-13 21:03:22 +0000 | [diff] [blame] | 231 |  | 
| Paul Duffin | 4f0a75a | 2021-03-11 07:23:27 +0000 | [diff] [blame] | 232 | // ComponentDepsMutator is called before prebuilt modules without a corresponding source module are | 
|  | 233 | // renamed so as this adds a prebuilt_ prefix this is guaranteed to only add dependencies on source | 
|  | 234 | // modules. | 
|  | 235 | func (system *systemModulesImport) ComponentDepsMutator(ctx android.BottomUpMutatorContext) { | 
|  | 236 | for _, lib := range system.properties.Libs { | 
| Paul Duffin | 864116c | 2021-04-02 10:24:13 +0100 | [diff] [blame] | 237 | ctx.AddVariationDependencies(nil, systemModulesLibsTag, android.PrebuiltNameFromSource(lib)) | 
| Paul Duffin | 4f0a75a | 2021-03-11 07:23:27 +0000 | [diff] [blame] | 238 | } | 
|  | 239 | } | 
|  | 240 |  | 
| Paul Duffin | 7b81f5e | 2020-01-13 21:03:22 +0000 | [diff] [blame] | 241 | type systemModulesSdkMemberType struct { | 
|  | 242 | android.SdkMemberTypeBase | 
|  | 243 | } | 
|  | 244 |  | 
| Paul Duffin | 296701e | 2021-07-14 10:29:36 +0100 | [diff] [blame] | 245 | func (mt *systemModulesSdkMemberType) AddDependencies(ctx android.SdkDependencyContext, dependencyTag blueprint.DependencyTag, names []string) { | 
|  | 246 | ctx.AddVariationDependencies(nil, dependencyTag, names...) | 
| Paul Duffin | 7b81f5e | 2020-01-13 21:03:22 +0000 | [diff] [blame] | 247 | } | 
|  | 248 |  | 
|  | 249 | func (mt *systemModulesSdkMemberType) IsInstance(module android.Module) bool { | 
|  | 250 | if _, ok := module.(*SystemModules); ok { | 
|  | 251 | // A prebuilt system module cannot be added as a member of an sdk because the source and | 
|  | 252 | // snapshot instances would conflict. | 
|  | 253 | _, ok := module.(*systemModulesImport) | 
|  | 254 | return !ok | 
|  | 255 | } | 
|  | 256 | return false | 
|  | 257 | } | 
|  | 258 |  | 
| Paul Duffin | 495ffb9 | 2020-03-20 13:35:40 +0000 | [diff] [blame] | 259 | func (mt *systemModulesSdkMemberType) AddPrebuiltModule(ctx android.SdkMemberContext, member android.SdkMember) android.BpModule { | 
|  | 260 | return ctx.SnapshotBuilder().AddPrebuiltModule(member, "java_system_modules_import") | 
|  | 261 | } | 
| Paul Duffin | 7b81f5e | 2020-01-13 21:03:22 +0000 | [diff] [blame] | 262 |  | 
| Paul Duffin | 495ffb9 | 2020-03-20 13:35:40 +0000 | [diff] [blame] | 263 | type systemModulesInfoProperties struct { | 
|  | 264 | android.SdkMemberPropertiesBase | 
|  | 265 |  | 
|  | 266 | Libs []string | 
|  | 267 | } | 
|  | 268 |  | 
|  | 269 | func (mt *systemModulesSdkMemberType) CreateVariantPropertiesStruct() android.SdkMemberProperties { | 
|  | 270 | return &systemModulesInfoProperties{} | 
|  | 271 | } | 
|  | 272 |  | 
|  | 273 | func (p *systemModulesInfoProperties) PopulateFromVariant(ctx android.SdkMemberContext, variant android.Module) { | 
|  | 274 | systemModule := variant.(*SystemModules) | 
|  | 275 | p.Libs = systemModule.properties.Libs | 
|  | 276 | } | 
|  | 277 |  | 
|  | 278 | func (p *systemModulesInfoProperties) AddToPropertySet(ctx android.SdkMemberContext, propertySet android.BpPropertySet) { | 
|  | 279 | if len(p.Libs) > 0 { | 
|  | 280 | // Add the references to the libraries that form the system module. | 
|  | 281 | propertySet.AddPropertyWithTag("libs", p.Libs, ctx.SnapshotBuilder().SdkMemberReferencePropertyTag(true)) | 
|  | 282 | } | 
| Paul Duffin | 7b81f5e | 2020-01-13 21:03:22 +0000 | [diff] [blame] | 283 | } |