blob: 92297c416e18968ce3a7d92781f6312813e637ca [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)
Paul Duffin90169ba2020-01-10 17:16:44 +000038 ctx.RegisterModuleType("java_system_modules_import", systemModulesImportFactory)
Paul Duffin43dc1cc2019-12-19 11:18:54 +000039}
40
Colin Cross1369cdb2017-09-29 17:58:17 -070041var (
42 jarsTosystemModules = pctx.AndroidStaticRule("jarsTosystemModules", blueprint.RuleParams{
43 Command: `rm -rf ${outDir} ${workDir} && mkdir -p ${workDir}/jmod && ` +
Pete Gillindf7dc822019-10-09 17:09:38 +010044 `${moduleInfoJavaPath} java.base $in > ${workDir}/module-info.java && ` +
Colin Cross1369cdb2017-09-29 17:58:17 -070045 `${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 Gillin1f52e932019-10-09 17:10:08 +010048 // 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 Gillindf7dc822019-10-09 17:09:38 +010051 ` --class-path ${workDir}/module.jar ${workDir}/jmod/java.base.jmod && ` +
52 `${config.JlinkCmd} --module-path ${workDir}/jmod --add-modules java.base --output ${outDir} ` +
Pete Gillin4eb6be32019-06-05 20:10:55 +010053 // 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 Cross1369cdb2017-09-29 17:58:17 -070057 `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 Gillindf7dc822019-10-09 17:09:38 +010068 "classpath", "outDir", "workDir")
Colin Cross1369cdb2017-09-29 17:58:17 -070069)
70
Pete Gillindf7dc822019-10-09 17:09:38 +010071func TransformJarsToSystemModules(ctx android.ModuleContext, jars android.Paths) (android.Path, android.Paths) {
Colin Cross1369cdb2017-09-29 17:58:17 -070072 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 Crossae887032017-10-23 17:16:14 -070081 ctx.Build(pctx, android.BuildParams{
Colin Cross1369cdb2017-09-29 17:58:17 -070082 Rule: jarsTosystemModules,
83 Description: "system modules",
84 Outputs: outputs,
85 Inputs: jars,
86 Args: map[string]string{
Pete Gillindf7dc822019-10-09 17:09:38 +010087 "classpath": strings.Join(jars.Strings(), ":"),
88 "workDir": workDir.String(),
89 "outDir": outDir.String(),
Colin Cross1369cdb2017-09-29 17:58:17 -070090 },
91 })
92
Dan Willemsenff60a732019-06-13 16:52:01 +000093 return outDir, outputs.Paths()
Colin Cross1369cdb2017-09-29 17:58:17 -070094}
95
Paul Duffincded5ec2020-01-10 17:30:36 +000096// 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 Cross1369cdb2017-09-29 17:58:17 -070099func SystemModulesFactory() android.Module {
100 module := &SystemModules{}
101 module.AddProperties(&module.properties)
102 android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibCommon)
Colin Cross667ffa12019-05-28 13:30:02 -0700103 android.InitDefaultableModule(module)
Colin Cross1369cdb2017-09-29 17:58:17 -0700104 return module
105}
106
107type SystemModules struct {
108 android.ModuleBase
Colin Cross667ffa12019-05-28 13:30:02 -0700109 android.DefaultableModuleBase
Colin Cross1369cdb2017-09-29 17:58:17 -0700110
111 properties SystemModulesProperties
112
Paul Duffin68289b02019-09-20 13:50:52 +0100113 // 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 Willemsenff60a732019-06-13 16:52:01 +0000116 outputDir android.Path
117 outputDeps android.Paths
Colin Cross1369cdb2017-09-29 17:58:17 -0700118}
119
120type SystemModulesProperties struct {
121 // List of java library modules that should be included in the system modules
122 Libs []string
Colin Cross1369cdb2017-09-29 17:58:17 -0700123}
124
125func (system *SystemModules) GenerateAndroidBuildActions(ctx android.ModuleContext) {
126 var jars android.Paths
127
Colin Crossee6143c2017-12-30 17:54:27 -0800128 ctx.VisitDirectDepsWithTag(libTag, func(module android.Module) {
129 dep, _ := module.(Dependency)
130 jars = append(jars, dep.HeaderJars()...)
Colin Cross1369cdb2017-09-29 17:58:17 -0700131 })
132
Paul Duffin68289b02019-09-20 13:50:52 +0100133 system.headerJars = jars
134
Pete Gillindf7dc822019-10-09 17:09:38 +0100135 system.outputDir, system.outputDeps = TransformJarsToSystemModules(ctx, jars)
Colin Cross1369cdb2017-09-29 17:58:17 -0700136}
137
138func (system *SystemModules) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross42d48b72018-08-29 14:10:52 -0700139 ctx.AddVariationDependencies(nil, libTag, system.properties.Libs...)
Colin Cross1369cdb2017-09-29 17:58:17 -0700140}
141
142func (system *SystemModules) AndroidMk() android.AndroidMkData {
143 return android.AndroidMkData{
144 Custom: func(w io.Writer, name, prefix, moduleDir string, data android.AndroidMkData) {
Tobias Thiererb1c697d2018-03-26 22:33:59 +0100145 fmt.Fprintln(w)
Dan Willemsenff60a732019-06-13 16:52:01 +0000146
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 Willemsenfe310be2019-06-20 10:16:12 -0700155 makevar = "SOONG_SYSTEM_MODULES_DEPS_" + name
Dan Willemsenff60a732019-06-13 16:52:01 +0000156 fmt.Fprintln(w, makevar, ":=$=", strings.Join(system.outputDeps.Strings(), " "))
157 fmt.Fprintln(w)
158
Tobias Thiererb1c697d2018-03-26 22:33:59 +0100159 fmt.Fprintln(w, name+":", "$("+makevar+")")
Dan Willemsena03c43a2018-07-24 13:00:52 -0700160 fmt.Fprintln(w, ".PHONY:", name)
Colin Cross1369cdb2017-09-29 17:58:17 -0700161 },
162 }
163}
Paul Duffin90169ba2020-01-10 17:16:44 +0000164
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.
170func 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
179type systemModulesImport struct {
180 SystemModules
181 prebuilt android.Prebuilt
182}
183
184func (system *systemModulesImport) Name() string {
185 return system.prebuilt.Name(system.ModuleBase.Name())
186}
187
188func (system *systemModulesImport) Prebuilt() *android.Prebuilt {
189 return &system.prebuilt
190}