blob: b56a401d3dad720111c0a8ccba6f47b57823d6e5 [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() {
31 android.RegisterModuleType("java_system_modules", SystemModulesFactory)
32
33 pctx.SourcePathVariable("moduleInfoJavaPath", "build/soong/scripts/jars-to-module-info-java.sh")
34}
35
36var (
37 jarsTosystemModules = pctx.AndroidStaticRule("jarsTosystemModules", blueprint.RuleParams{
38 Command: `rm -rf ${outDir} ${workDir} && mkdir -p ${workDir}/jmod && ` +
Pete Gillindf7dc822019-10-09 17:09:38 +010039 `${moduleInfoJavaPath} java.base $in > ${workDir}/module-info.java && ` +
Colin Cross1369cdb2017-09-29 17:58:17 -070040 `${config.JavacCmd} --system=none --patch-module=java.base=${classpath} ${workDir}/module-info.java && ` +
41 `${config.SoongZipCmd} -jar -o ${workDir}/classes.jar -C ${workDir} -f ${workDir}/module-info.class && ` +
42 `${config.MergeZipsCmd} -j ${workDir}/module.jar ${workDir}/classes.jar $in && ` +
Pete Gillin1f52e932019-10-09 17:10:08 +010043 // Note: The version of the java.base module created must match the version
44 // of the jlink tool which consumes it.
45 `${config.JmodCmd} create --module-version ${config.JlinkVersion} --target-platform android ` +
Pete Gillindf7dc822019-10-09 17:09:38 +010046 ` --class-path ${workDir}/module.jar ${workDir}/jmod/java.base.jmod && ` +
47 `${config.JlinkCmd} --module-path ${workDir}/jmod --add-modules java.base --output ${outDir} ` +
Pete Gillin4eb6be32019-06-05 20:10:55 +010048 // Note: The system-modules jlink plugin is disabled because (a) it is not
49 // useful on Android, and (b) it causes errors with later versions of jlink
50 // when the jdk.internal.module is absent from java.base (as it is here).
51 ` --disable-plugin system-modules && ` +
Colin Cross1369cdb2017-09-29 17:58:17 -070052 `cp ${config.JrtFsJar} ${outDir}/lib/`,
53 CommandDeps: []string{
54 "${moduleInfoJavaPath}",
55 "${config.JavacCmd}",
56 "${config.SoongZipCmd}",
57 "${config.MergeZipsCmd}",
58 "${config.JmodCmd}",
59 "${config.JlinkCmd}",
60 "${config.JrtFsJar}",
61 },
62 },
Pete Gillindf7dc822019-10-09 17:09:38 +010063 "classpath", "outDir", "workDir")
Colin Cross1369cdb2017-09-29 17:58:17 -070064)
65
Pete Gillindf7dc822019-10-09 17:09:38 +010066func TransformJarsToSystemModules(ctx android.ModuleContext, jars android.Paths) (android.Path, android.Paths) {
Colin Cross1369cdb2017-09-29 17:58:17 -070067 outDir := android.PathForModuleOut(ctx, "system")
68 workDir := android.PathForModuleOut(ctx, "modules")
69 outputFile := android.PathForModuleOut(ctx, "system/lib/modules")
70 outputs := android.WritablePaths{
71 outputFile,
72 android.PathForModuleOut(ctx, "system/lib/jrt-fs.jar"),
73 android.PathForModuleOut(ctx, "system/release"),
74 }
75
Colin Crossae887032017-10-23 17:16:14 -070076 ctx.Build(pctx, android.BuildParams{
Colin Cross1369cdb2017-09-29 17:58:17 -070077 Rule: jarsTosystemModules,
78 Description: "system modules",
79 Outputs: outputs,
80 Inputs: jars,
81 Args: map[string]string{
Pete Gillindf7dc822019-10-09 17:09:38 +010082 "classpath": strings.Join(jars.Strings(), ":"),
83 "workDir": workDir.String(),
84 "outDir": outDir.String(),
Colin Cross1369cdb2017-09-29 17:58:17 -070085 },
86 })
87
Dan Willemsenff60a732019-06-13 16:52:01 +000088 return outDir, outputs.Paths()
Colin Cross1369cdb2017-09-29 17:58:17 -070089}
90
91func SystemModulesFactory() android.Module {
92 module := &SystemModules{}
93 module.AddProperties(&module.properties)
94 android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibCommon)
Colin Cross667ffa12019-05-28 13:30:02 -070095 android.InitDefaultableModule(module)
Colin Cross1369cdb2017-09-29 17:58:17 -070096 return module
97}
98
99type SystemModules struct {
100 android.ModuleBase
Colin Cross667ffa12019-05-28 13:30:02 -0700101 android.DefaultableModuleBase
Colin Cross1369cdb2017-09-29 17:58:17 -0700102
103 properties SystemModulesProperties
104
Paul Duffin68289b02019-09-20 13:50:52 +0100105 // The aggregated header jars from all jars specified in the libs property.
106 // Used when system module is added as a dependency to bootclasspath.
107 headerJars android.Paths
Dan Willemsenff60a732019-06-13 16:52:01 +0000108 outputDir android.Path
109 outputDeps android.Paths
Colin Cross1369cdb2017-09-29 17:58:17 -0700110}
111
112type SystemModulesProperties struct {
113 // List of java library modules that should be included in the system modules
114 Libs []string
Colin Cross1369cdb2017-09-29 17:58:17 -0700115}
116
117func (system *SystemModules) GenerateAndroidBuildActions(ctx android.ModuleContext) {
118 var jars android.Paths
119
Colin Crossee6143c2017-12-30 17:54:27 -0800120 ctx.VisitDirectDepsWithTag(libTag, func(module android.Module) {
121 dep, _ := module.(Dependency)
122 jars = append(jars, dep.HeaderJars()...)
Colin Cross1369cdb2017-09-29 17:58:17 -0700123 })
124
Paul Duffin68289b02019-09-20 13:50:52 +0100125 system.headerJars = jars
126
Pete Gillindf7dc822019-10-09 17:09:38 +0100127 system.outputDir, system.outputDeps = TransformJarsToSystemModules(ctx, jars)
Colin Cross1369cdb2017-09-29 17:58:17 -0700128}
129
130func (system *SystemModules) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross42d48b72018-08-29 14:10:52 -0700131 ctx.AddVariationDependencies(nil, libTag, system.properties.Libs...)
Colin Cross1369cdb2017-09-29 17:58:17 -0700132}
133
134func (system *SystemModules) AndroidMk() android.AndroidMkData {
135 return android.AndroidMkData{
136 Custom: func(w io.Writer, name, prefix, moduleDir string, data android.AndroidMkData) {
Tobias Thiererb1c697d2018-03-26 22:33:59 +0100137 fmt.Fprintln(w)
Dan Willemsenff60a732019-06-13 16:52:01 +0000138
139 makevar := "SOONG_SYSTEM_MODULES_" + name
140 fmt.Fprintln(w, makevar, ":=$=", system.outputDir.String())
141 fmt.Fprintln(w)
142
143 makevar = "SOONG_SYSTEM_MODULES_LIBS_" + name
144 fmt.Fprintln(w, makevar, ":=$=", strings.Join(system.properties.Libs, " "))
145 fmt.Fprintln(w)
146
Dan Willemsenfe310be2019-06-20 10:16:12 -0700147 makevar = "SOONG_SYSTEM_MODULES_DEPS_" + name
Dan Willemsenff60a732019-06-13 16:52:01 +0000148 fmt.Fprintln(w, makevar, ":=$=", strings.Join(system.outputDeps.Strings(), " "))
149 fmt.Fprintln(w)
150
Tobias Thiererb1c697d2018-03-26 22:33:59 +0100151 fmt.Fprintln(w, name+":", "$("+makevar+")")
Dan Willemsena03c43a2018-07-24 13:00:52 -0700152 fmt.Fprintln(w, ".PHONY:", name)
Colin Cross1369cdb2017-09-29 17:58:17 -0700153 },
154 }
155}