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") |
| 34 | } |
| 35 | |
Paul Duffin | 43dc1cc | 2019-12-19 11:18:54 +0000 | [diff] [blame] | 36 | func RegisterSystemModulesBuildComponents(ctx android.RegistrationContext) { |
| 37 | ctx.RegisterModuleType("java_system_modules", SystemModulesFactory) |
Paul Duffin | 90169ba | 2020-01-10 17:16:44 +0000 | [diff] [blame] | 38 | ctx.RegisterModuleType("java_system_modules_import", systemModulesImportFactory) |
Paul Duffin | 43dc1cc | 2019-12-19 11:18:54 +0000 | [diff] [blame] | 39 | } |
| 40 | |
Colin Cross | 1369cdb | 2017-09-29 17:58:17 -0700 | [diff] [blame] | 41 | var ( |
| 42 | jarsTosystemModules = pctx.AndroidStaticRule("jarsTosystemModules", blueprint.RuleParams{ |
| 43 | Command: `rm -rf ${outDir} ${workDir} && mkdir -p ${workDir}/jmod && ` + |
Pete Gillin | df7dc82 | 2019-10-09 17:09:38 +0100 | [diff] [blame] | 44 | `${moduleInfoJavaPath} java.base $in > ${workDir}/module-info.java && ` + |
Colin Cross | 1369cdb | 2017-09-29 17:58:17 -0700 | [diff] [blame] | 45 | `${config.JavacCmd} --system=none --patch-module=java.base=${classpath} ${workDir}/module-info.java && ` + |
| 46 | `${config.SoongZipCmd} -jar -o ${workDir}/classes.jar -C ${workDir} -f ${workDir}/module-info.class && ` + |
| 47 | `${config.MergeZipsCmd} -j ${workDir}/module.jar ${workDir}/classes.jar $in && ` + |
Pete Gillin | 1f52e93 | 2019-10-09 17:10:08 +0100 | [diff] [blame] | 48 | // Note: The version of the java.base module created must match the version |
| 49 | // of the jlink tool which consumes it. |
| 50 | `${config.JmodCmd} create --module-version ${config.JlinkVersion} --target-platform android ` + |
Pete Gillin | df7dc82 | 2019-10-09 17:09:38 +0100 | [diff] [blame] | 51 | ` --class-path ${workDir}/module.jar ${workDir}/jmod/java.base.jmod && ` + |
| 52 | `${config.JlinkCmd} --module-path ${workDir}/jmod --add-modules java.base --output ${outDir} ` + |
Pete Gillin | 4eb6be3 | 2019-06-05 20:10:55 +0100 | [diff] [blame] | 53 | // Note: The system-modules jlink plugin is disabled because (a) it is not |
| 54 | // useful on Android, and (b) it causes errors with later versions of jlink |
| 55 | // when the jdk.internal.module is absent from java.base (as it is here). |
| 56 | ` --disable-plugin system-modules && ` + |
Colin Cross | 1369cdb | 2017-09-29 17:58:17 -0700 | [diff] [blame] | 57 | `cp ${config.JrtFsJar} ${outDir}/lib/`, |
| 58 | CommandDeps: []string{ |
| 59 | "${moduleInfoJavaPath}", |
| 60 | "${config.JavacCmd}", |
| 61 | "${config.SoongZipCmd}", |
| 62 | "${config.MergeZipsCmd}", |
| 63 | "${config.JmodCmd}", |
| 64 | "${config.JlinkCmd}", |
| 65 | "${config.JrtFsJar}", |
| 66 | }, |
| 67 | }, |
Pete Gillin | df7dc82 | 2019-10-09 17:09:38 +0100 | [diff] [blame] | 68 | "classpath", "outDir", "workDir") |
Colin Cross | 1369cdb | 2017-09-29 17:58:17 -0700 | [diff] [blame] | 69 | ) |
| 70 | |
Pete Gillin | df7dc82 | 2019-10-09 17:09:38 +0100 | [diff] [blame] | 71 | func TransformJarsToSystemModules(ctx android.ModuleContext, jars android.Paths) (android.Path, android.Paths) { |
Colin Cross | 1369cdb | 2017-09-29 17:58:17 -0700 | [diff] [blame] | 72 | outDir := android.PathForModuleOut(ctx, "system") |
| 73 | workDir := android.PathForModuleOut(ctx, "modules") |
| 74 | outputFile := android.PathForModuleOut(ctx, "system/lib/modules") |
| 75 | outputs := android.WritablePaths{ |
| 76 | outputFile, |
| 77 | android.PathForModuleOut(ctx, "system/lib/jrt-fs.jar"), |
| 78 | android.PathForModuleOut(ctx, "system/release"), |
| 79 | } |
| 80 | |
Colin Cross | ae88703 | 2017-10-23 17:16:14 -0700 | [diff] [blame] | 81 | ctx.Build(pctx, android.BuildParams{ |
Colin Cross | 1369cdb | 2017-09-29 17:58:17 -0700 | [diff] [blame] | 82 | Rule: jarsTosystemModules, |
| 83 | Description: "system modules", |
| 84 | Outputs: outputs, |
| 85 | Inputs: jars, |
| 86 | Args: map[string]string{ |
Pete Gillin | df7dc82 | 2019-10-09 17:09:38 +0100 | [diff] [blame] | 87 | "classpath": strings.Join(jars.Strings(), ":"), |
| 88 | "workDir": workDir.String(), |
| 89 | "outDir": outDir.String(), |
Colin Cross | 1369cdb | 2017-09-29 17:58:17 -0700 | [diff] [blame] | 90 | }, |
| 91 | }) |
| 92 | |
Dan Willemsen | ff60a73 | 2019-06-13 16:52:01 +0000 | [diff] [blame] | 93 | return outDir, outputs.Paths() |
Colin Cross | 1369cdb | 2017-09-29 17:58:17 -0700 | [diff] [blame] | 94 | } |
| 95 | |
Paul Duffin | cded5ec | 2020-01-10 17:30:36 +0000 | [diff] [blame] | 96 | // java_system_modules creates a system module from a set of java libraries that can |
| 97 | // be referenced from the system_modules property. It must contain at a minimum the |
| 98 | // 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] | 99 | func SystemModulesFactory() android.Module { |
| 100 | module := &SystemModules{} |
| 101 | module.AddProperties(&module.properties) |
| 102 | android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibCommon) |
Colin Cross | 667ffa1 | 2019-05-28 13:30:02 -0700 | [diff] [blame] | 103 | android.InitDefaultableModule(module) |
Colin Cross | 1369cdb | 2017-09-29 17:58:17 -0700 | [diff] [blame] | 104 | return module |
| 105 | } |
| 106 | |
| 107 | type SystemModules struct { |
| 108 | android.ModuleBase |
Colin Cross | 667ffa1 | 2019-05-28 13:30:02 -0700 | [diff] [blame] | 109 | android.DefaultableModuleBase |
Colin Cross | 1369cdb | 2017-09-29 17:58:17 -0700 | [diff] [blame] | 110 | |
| 111 | properties SystemModulesProperties |
| 112 | |
Paul Duffin | 68289b0 | 2019-09-20 13:50:52 +0100 | [diff] [blame] | 113 | // The aggregated header jars from all jars specified in the libs property. |
| 114 | // Used when system module is added as a dependency to bootclasspath. |
| 115 | headerJars android.Paths |
Dan Willemsen | ff60a73 | 2019-06-13 16:52:01 +0000 | [diff] [blame] | 116 | outputDir android.Path |
| 117 | outputDeps android.Paths |
Colin Cross | 1369cdb | 2017-09-29 17:58:17 -0700 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | type SystemModulesProperties struct { |
| 121 | // List of java library modules that should be included in the system modules |
| 122 | Libs []string |
Colin Cross | 1369cdb | 2017-09-29 17:58:17 -0700 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | func (system *SystemModules) GenerateAndroidBuildActions(ctx android.ModuleContext) { |
| 126 | var jars android.Paths |
| 127 | |
Colin Cross | ee6143c | 2017-12-30 17:54:27 -0800 | [diff] [blame] | 128 | ctx.VisitDirectDepsWithTag(libTag, func(module android.Module) { |
| 129 | dep, _ := module.(Dependency) |
| 130 | jars = append(jars, dep.HeaderJars()...) |
Colin Cross | 1369cdb | 2017-09-29 17:58:17 -0700 | [diff] [blame] | 131 | }) |
| 132 | |
Paul Duffin | 68289b0 | 2019-09-20 13:50:52 +0100 | [diff] [blame] | 133 | system.headerJars = jars |
| 134 | |
Pete Gillin | df7dc82 | 2019-10-09 17:09:38 +0100 | [diff] [blame] | 135 | system.outputDir, system.outputDeps = TransformJarsToSystemModules(ctx, jars) |
Colin Cross | 1369cdb | 2017-09-29 17:58:17 -0700 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | func (system *SystemModules) DepsMutator(ctx android.BottomUpMutatorContext) { |
Colin Cross | 42d48b7 | 2018-08-29 14:10:52 -0700 | [diff] [blame] | 139 | ctx.AddVariationDependencies(nil, libTag, system.properties.Libs...) |
Colin Cross | 1369cdb | 2017-09-29 17:58:17 -0700 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | func (system *SystemModules) AndroidMk() android.AndroidMkData { |
| 143 | return android.AndroidMkData{ |
| 144 | Custom: func(w io.Writer, name, prefix, moduleDir string, data android.AndroidMkData) { |
Tobias Thierer | b1c697d | 2018-03-26 22:33:59 +0100 | [diff] [blame] | 145 | fmt.Fprintln(w) |
Dan Willemsen | ff60a73 | 2019-06-13 16:52:01 +0000 | [diff] [blame] | 146 | |
| 147 | makevar := "SOONG_SYSTEM_MODULES_" + name |
| 148 | fmt.Fprintln(w, makevar, ":=$=", system.outputDir.String()) |
| 149 | fmt.Fprintln(w) |
| 150 | |
| 151 | makevar = "SOONG_SYSTEM_MODULES_LIBS_" + name |
| 152 | fmt.Fprintln(w, makevar, ":=$=", strings.Join(system.properties.Libs, " ")) |
| 153 | fmt.Fprintln(w) |
| 154 | |
Dan Willemsen | fe310be | 2019-06-20 10:16:12 -0700 | [diff] [blame] | 155 | makevar = "SOONG_SYSTEM_MODULES_DEPS_" + name |
Dan Willemsen | ff60a73 | 2019-06-13 16:52:01 +0000 | [diff] [blame] | 156 | fmt.Fprintln(w, makevar, ":=$=", strings.Join(system.outputDeps.Strings(), " ")) |
| 157 | fmt.Fprintln(w) |
| 158 | |
Tobias Thierer | b1c697d | 2018-03-26 22:33:59 +0100 | [diff] [blame] | 159 | fmt.Fprintln(w, name+":", "$("+makevar+")") |
Dan Willemsen | a03c43a | 2018-07-24 13:00:52 -0700 | [diff] [blame] | 160 | fmt.Fprintln(w, ".PHONY:", name) |
Colin Cross | 1369cdb | 2017-09-29 17:58:17 -0700 | [diff] [blame] | 161 | }, |
| 162 | } |
| 163 | } |
Paul Duffin | 90169ba | 2020-01-10 17:16:44 +0000 | [diff] [blame] | 164 | |
| 165 | // A prebuilt version of java_system_modules. It does not import the |
| 166 | // generated system module, it generates the system module from imported |
| 167 | // java libraries in the same way that java_system_modules does. It just |
| 168 | // acts as a prebuilt, i.e. can have the same base name as another module |
| 169 | // type and the one to use is selected at runtime. |
| 170 | func systemModulesImportFactory() android.Module { |
| 171 | module := &systemModulesImport{} |
| 172 | module.AddProperties(&module.properties) |
| 173 | android.InitPrebuiltModule(module, &module.properties.Libs) |
| 174 | android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibCommon) |
| 175 | android.InitDefaultableModule(module) |
| 176 | return module |
| 177 | } |
| 178 | |
| 179 | type systemModulesImport struct { |
| 180 | SystemModules |
| 181 | prebuilt android.Prebuilt |
| 182 | } |
| 183 | |
| 184 | func (system *systemModulesImport) Name() string { |
| 185 | return system.prebuilt.Name(system.ModuleBase.Name()) |
| 186 | } |
| 187 | |
| 188 | func (system *systemModulesImport) Prebuilt() *android.Prebuilt { |
| 189 | return &system.prebuilt |
| 190 | } |