blob: ed2fc1856587f5a509e16fdf11d05e87abe7e207 [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
95func SystemModulesFactory() android.Module {
96 module := &SystemModules{}
97 module.AddProperties(&module.properties)
98 android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibCommon)
Colin Cross667ffa12019-05-28 13:30:02 -070099 android.InitDefaultableModule(module)
Colin Cross1369cdb2017-09-29 17:58:17 -0700100 return module
101}
102
103type SystemModules struct {
104 android.ModuleBase
Colin Cross667ffa12019-05-28 13:30:02 -0700105 android.DefaultableModuleBase
Colin Cross1369cdb2017-09-29 17:58:17 -0700106
107 properties SystemModulesProperties
108
Paul Duffin68289b02019-09-20 13:50:52 +0100109 // The aggregated header jars from all jars specified in the libs property.
110 // Used when system module is added as a dependency to bootclasspath.
111 headerJars android.Paths
Dan Willemsenff60a732019-06-13 16:52:01 +0000112 outputDir android.Path
113 outputDeps android.Paths
Colin Cross1369cdb2017-09-29 17:58:17 -0700114}
115
116type SystemModulesProperties struct {
117 // List of java library modules that should be included in the system modules
118 Libs []string
Colin Cross1369cdb2017-09-29 17:58:17 -0700119}
120
121func (system *SystemModules) GenerateAndroidBuildActions(ctx android.ModuleContext) {
122 var jars android.Paths
123
Colin Crossee6143c2017-12-30 17:54:27 -0800124 ctx.VisitDirectDepsWithTag(libTag, func(module android.Module) {
125 dep, _ := module.(Dependency)
126 jars = append(jars, dep.HeaderJars()...)
Colin Cross1369cdb2017-09-29 17:58:17 -0700127 })
128
Paul Duffin68289b02019-09-20 13:50:52 +0100129 system.headerJars = jars
130
Pete Gillindf7dc822019-10-09 17:09:38 +0100131 system.outputDir, system.outputDeps = TransformJarsToSystemModules(ctx, jars)
Colin Cross1369cdb2017-09-29 17:58:17 -0700132}
133
134func (system *SystemModules) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross42d48b72018-08-29 14:10:52 -0700135 ctx.AddVariationDependencies(nil, libTag, system.properties.Libs...)
Colin Cross1369cdb2017-09-29 17:58:17 -0700136}
137
138func (system *SystemModules) AndroidMk() android.AndroidMkData {
139 return android.AndroidMkData{
140 Custom: func(w io.Writer, name, prefix, moduleDir string, data android.AndroidMkData) {
Tobias Thiererb1c697d2018-03-26 22:33:59 +0100141 fmt.Fprintln(w)
Dan Willemsenff60a732019-06-13 16:52:01 +0000142
143 makevar := "SOONG_SYSTEM_MODULES_" + name
144 fmt.Fprintln(w, makevar, ":=$=", system.outputDir.String())
145 fmt.Fprintln(w)
146
147 makevar = "SOONG_SYSTEM_MODULES_LIBS_" + name
148 fmt.Fprintln(w, makevar, ":=$=", strings.Join(system.properties.Libs, " "))
149 fmt.Fprintln(w)
150
Dan Willemsenfe310be2019-06-20 10:16:12 -0700151 makevar = "SOONG_SYSTEM_MODULES_DEPS_" + name
Dan Willemsenff60a732019-06-13 16:52:01 +0000152 fmt.Fprintln(w, makevar, ":=$=", strings.Join(system.outputDeps.Strings(), " "))
153 fmt.Fprintln(w)
154
Tobias Thiererb1c697d2018-03-26 22:33:59 +0100155 fmt.Fprintln(w, name+":", "$("+makevar+")")
Dan Willemsena03c43a2018-07-24 13:00:52 -0700156 fmt.Fprintln(w, ".PHONY:", name)
Colin Cross1369cdb2017-09-29 17:58:17 -0700157 },
158 }
159}