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