blob: 81e5cb462600c252072a46ffc83c8934d7a2a795 [file] [log] [blame]
Colin Cross1369cdb2017-09-29 17:58:17 -07001// 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
14package java
15
16import (
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
30func init() {
Paul Duffin43dc1cc2019-12-19 11:18:54 +000031 RegisterSystemModulesBuildComponents(android.InitRegistrationContext)
Colin Cross1369cdb2017-09-29 17:58:17 -070032
33 pctx.SourcePathVariable("moduleInfoJavaPath", "build/soong/scripts/jars-to-module-info-java.sh")
34}
35
Paul Duffin43dc1cc2019-12-19 11:18:54 +000036func RegisterSystemModulesBuildComponents(ctx android.RegistrationContext) {
37 ctx.RegisterModuleType("java_system_modules", SystemModulesFactory)
38}
39
Colin Cross1369cdb2017-09-29 17:58:17 -070040var (
41 jarsTosystemModules = pctx.AndroidStaticRule("jarsTosystemModules", blueprint.RuleParams{
42 Command: `rm -rf ${outDir} ${workDir} && mkdir -p ${workDir}/jmod && ` +
Pete Gillindf7dc822019-10-09 17:09:38 +010043 `${moduleInfoJavaPath} java.base $in > ${workDir}/module-info.java && ` +
Colin Cross1369cdb2017-09-29 17:58:17 -070044 `${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 Gillin1f52e932019-10-09 17:10:08 +010047 // 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 Gillindf7dc822019-10-09 17:09:38 +010050 ` --class-path ${workDir}/module.jar ${workDir}/jmod/java.base.jmod && ` +
51 `${config.JlinkCmd} --module-path ${workDir}/jmod --add-modules java.base --output ${outDir} ` +
Pete Gillin4eb6be32019-06-05 20:10:55 +010052 // 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 Cross1369cdb2017-09-29 17:58:17 -070056 `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 Gillindf7dc822019-10-09 17:09:38 +010067 "classpath", "outDir", "workDir")
Colin Cross1369cdb2017-09-29 17:58:17 -070068)
69
Pete Gillindf7dc822019-10-09 17:09:38 +010070func TransformJarsToSystemModules(ctx android.ModuleContext, jars android.Paths) (android.Path, android.Paths) {
Colin Cross1369cdb2017-09-29 17:58:17 -070071 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 Crossae887032017-10-23 17:16:14 -070080 ctx.Build(pctx, android.BuildParams{
Colin Cross1369cdb2017-09-29 17:58:17 -070081 Rule: jarsTosystemModules,
82 Description: "system modules",
83 Outputs: outputs,
84 Inputs: jars,
85 Args: map[string]string{
Pete Gillindf7dc822019-10-09 17:09:38 +010086 "classpath": strings.Join(jars.Strings(), ":"),
87 "workDir": workDir.String(),
88 "outDir": outDir.String(),
Colin Cross1369cdb2017-09-29 17:58:17 -070089 },
90 })
91
Dan Willemsenff60a732019-06-13 16:52:01 +000092 return outDir, outputs.Paths()
Colin Cross1369cdb2017-09-29 17:58:17 -070093}
94
Paul Duffincded5ec2020-01-10 17:30:36 +000095// 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 Cross1369cdb2017-09-29 17:58:17 -070098func SystemModulesFactory() android.Module {
99 module := &SystemModules{}
100 module.AddProperties(&module.properties)
101 android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibCommon)
Colin Cross667ffa12019-05-28 13:30:02 -0700102 android.InitDefaultableModule(module)
Colin Cross1369cdb2017-09-29 17:58:17 -0700103 return module
104}
105
106type SystemModules struct {
107 android.ModuleBase
Colin Cross667ffa12019-05-28 13:30:02 -0700108 android.DefaultableModuleBase
Colin Cross1369cdb2017-09-29 17:58:17 -0700109
110 properties SystemModulesProperties
111
Paul Duffin68289b02019-09-20 13:50:52 +0100112 // 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 Willemsenff60a732019-06-13 16:52:01 +0000115 outputDir android.Path
116 outputDeps android.Paths
Colin Cross1369cdb2017-09-29 17:58:17 -0700117}
118
119type SystemModulesProperties struct {
120 // List of java library modules that should be included in the system modules
121 Libs []string
Colin Cross1369cdb2017-09-29 17:58:17 -0700122}
123
124func (system *SystemModules) GenerateAndroidBuildActions(ctx android.ModuleContext) {
125 var jars android.Paths
126
Colin Crossee6143c2017-12-30 17:54:27 -0800127 ctx.VisitDirectDepsWithTag(libTag, func(module android.Module) {
128 dep, _ := module.(Dependency)
129 jars = append(jars, dep.HeaderJars()...)
Colin Cross1369cdb2017-09-29 17:58:17 -0700130 })
131
Paul Duffin68289b02019-09-20 13:50:52 +0100132 system.headerJars = jars
133
Pete Gillindf7dc822019-10-09 17:09:38 +0100134 system.outputDir, system.outputDeps = TransformJarsToSystemModules(ctx, jars)
Colin Cross1369cdb2017-09-29 17:58:17 -0700135}
136
137func (system *SystemModules) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross42d48b72018-08-29 14:10:52 -0700138 ctx.AddVariationDependencies(nil, libTag, system.properties.Libs...)
Colin Cross1369cdb2017-09-29 17:58:17 -0700139}
140
141func (system *SystemModules) AndroidMk() android.AndroidMkData {
142 return android.AndroidMkData{
143 Custom: func(w io.Writer, name, prefix, moduleDir string, data android.AndroidMkData) {
Tobias Thiererb1c697d2018-03-26 22:33:59 +0100144 fmt.Fprintln(w)
Dan Willemsenff60a732019-06-13 16:52:01 +0000145
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 Willemsenfe310be2019-06-20 10:16:12 -0700154 makevar = "SOONG_SYSTEM_MODULES_DEPS_" + name
Dan Willemsenff60a732019-06-13 16:52:01 +0000155 fmt.Fprintln(w, makevar, ":=$=", strings.Join(system.outputDeps.Strings(), " "))
156 fmt.Fprintln(w)
157
Tobias Thiererb1c697d2018-03-26 22:33:59 +0100158 fmt.Fprintln(w, name+":", "$("+makevar+")")
Dan Willemsena03c43a2018-07-24 13:00:52 -0700159 fmt.Fprintln(w, ".PHONY:", name)
Colin Cross1369cdb2017-09-29 17:58:17 -0700160 },
161 }
162}