blob: 731503fd029ee207706134ad1495c7c6e21abe7e [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")
Paul Duffin7b81f5e2020-01-13 21:03:22 +000034
35 // Register sdk member types.
36 android.RegisterSdkMemberType(&systemModulesSdkMemberType{
37 android.SdkMemberTypeBase{
38 PropertyName: "java_system_modules",
39 SupportsSdk: true,
40 TransitiveSdkMembers: true,
41 },
42 })
Colin Cross1369cdb2017-09-29 17:58:17 -070043}
44
Paul Duffin43dc1cc2019-12-19 11:18:54 +000045func RegisterSystemModulesBuildComponents(ctx android.RegistrationContext) {
46 ctx.RegisterModuleType("java_system_modules", SystemModulesFactory)
Paul Duffin90169ba2020-01-10 17:16:44 +000047 ctx.RegisterModuleType("java_system_modules_import", systemModulesImportFactory)
Paul Duffin43dc1cc2019-12-19 11:18:54 +000048}
49
Colin Cross1369cdb2017-09-29 17:58:17 -070050var (
51 jarsTosystemModules = pctx.AndroidStaticRule("jarsTosystemModules", blueprint.RuleParams{
52 Command: `rm -rf ${outDir} ${workDir} && mkdir -p ${workDir}/jmod && ` +
Pete Gillindf7dc822019-10-09 17:09:38 +010053 `${moduleInfoJavaPath} java.base $in > ${workDir}/module-info.java && ` +
Colin Cross1369cdb2017-09-29 17:58:17 -070054 `${config.JavacCmd} --system=none --patch-module=java.base=${classpath} ${workDir}/module-info.java && ` +
55 `${config.SoongZipCmd} -jar -o ${workDir}/classes.jar -C ${workDir} -f ${workDir}/module-info.class && ` +
56 `${config.MergeZipsCmd} -j ${workDir}/module.jar ${workDir}/classes.jar $in && ` +
Pete Gillin1f52e932019-10-09 17:10:08 +010057 // Note: The version of the java.base module created must match the version
58 // of the jlink tool which consumes it.
59 `${config.JmodCmd} create --module-version ${config.JlinkVersion} --target-platform android ` +
Pete Gillindf7dc822019-10-09 17:09:38 +010060 ` --class-path ${workDir}/module.jar ${workDir}/jmod/java.base.jmod && ` +
61 `${config.JlinkCmd} --module-path ${workDir}/jmod --add-modules java.base --output ${outDir} ` +
Pete Gillin4eb6be32019-06-05 20:10:55 +010062 // Note: The system-modules jlink plugin is disabled because (a) it is not
63 // useful on Android, and (b) it causes errors with later versions of jlink
64 // when the jdk.internal.module is absent from java.base (as it is here).
65 ` --disable-plugin system-modules && ` +
Colin Cross1369cdb2017-09-29 17:58:17 -070066 `cp ${config.JrtFsJar} ${outDir}/lib/`,
67 CommandDeps: []string{
68 "${moduleInfoJavaPath}",
69 "${config.JavacCmd}",
70 "${config.SoongZipCmd}",
71 "${config.MergeZipsCmd}",
72 "${config.JmodCmd}",
73 "${config.JlinkCmd}",
74 "${config.JrtFsJar}",
75 },
76 },
Pete Gillindf7dc822019-10-09 17:09:38 +010077 "classpath", "outDir", "workDir")
Paul Duffin7b81f5e2020-01-13 21:03:22 +000078
79 // Dependency tag that causes the added dependencies to be added as java_header_libs
80 // to the sdk/module_exports/snapshot.
81 systemModulesLibsTag = android.DependencyTagForSdkMemberType(javaHeaderLibsSdkMemberType)
Colin Cross1369cdb2017-09-29 17:58:17 -070082)
83
Pete Gillindf7dc822019-10-09 17:09:38 +010084func TransformJarsToSystemModules(ctx android.ModuleContext, jars android.Paths) (android.Path, android.Paths) {
Colin Cross1369cdb2017-09-29 17:58:17 -070085 outDir := android.PathForModuleOut(ctx, "system")
86 workDir := android.PathForModuleOut(ctx, "modules")
87 outputFile := android.PathForModuleOut(ctx, "system/lib/modules")
88 outputs := android.WritablePaths{
89 outputFile,
90 android.PathForModuleOut(ctx, "system/lib/jrt-fs.jar"),
91 android.PathForModuleOut(ctx, "system/release"),
92 }
93
Colin Crossae887032017-10-23 17:16:14 -070094 ctx.Build(pctx, android.BuildParams{
Colin Cross1369cdb2017-09-29 17:58:17 -070095 Rule: jarsTosystemModules,
96 Description: "system modules",
97 Outputs: outputs,
98 Inputs: jars,
99 Args: map[string]string{
Pete Gillindf7dc822019-10-09 17:09:38 +0100100 "classpath": strings.Join(jars.Strings(), ":"),
101 "workDir": workDir.String(),
102 "outDir": outDir.String(),
Colin Cross1369cdb2017-09-29 17:58:17 -0700103 },
104 })
105
Dan Willemsenff60a732019-06-13 16:52:01 +0000106 return outDir, outputs.Paths()
Colin Cross1369cdb2017-09-29 17:58:17 -0700107}
108
Paul Duffincded5ec2020-01-10 17:30:36 +0000109// java_system_modules creates a system module from a set of java libraries that can
110// be referenced from the system_modules property. It must contain at a minimum the
111// java.base module which must include classes from java.lang amongst other java packages.
Colin Cross1369cdb2017-09-29 17:58:17 -0700112func SystemModulesFactory() android.Module {
113 module := &SystemModules{}
114 module.AddProperties(&module.properties)
115 android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibCommon)
Colin Cross667ffa12019-05-28 13:30:02 -0700116 android.InitDefaultableModule(module)
Colin Cross1369cdb2017-09-29 17:58:17 -0700117 return module
118}
119
120type SystemModules struct {
121 android.ModuleBase
Colin Cross667ffa12019-05-28 13:30:02 -0700122 android.DefaultableModuleBase
Paul Duffin7b81f5e2020-01-13 21:03:22 +0000123 android.SdkBase
Colin Cross1369cdb2017-09-29 17:58:17 -0700124
125 properties SystemModulesProperties
126
Paul Duffin68289b02019-09-20 13:50:52 +0100127 // The aggregated header jars from all jars specified in the libs property.
128 // Used when system module is added as a dependency to bootclasspath.
129 headerJars android.Paths
Dan Willemsenff60a732019-06-13 16:52:01 +0000130 outputDir android.Path
131 outputDeps android.Paths
Colin Cross1369cdb2017-09-29 17:58:17 -0700132}
133
134type SystemModulesProperties struct {
135 // List of java library modules that should be included in the system modules
136 Libs []string
Colin Cross1369cdb2017-09-29 17:58:17 -0700137}
138
139func (system *SystemModules) GenerateAndroidBuildActions(ctx android.ModuleContext) {
140 var jars android.Paths
141
Paul Duffin7b81f5e2020-01-13 21:03:22 +0000142 ctx.VisitDirectDepsWithTag(systemModulesLibsTag, func(module android.Module) {
Colin Crossee6143c2017-12-30 17:54:27 -0800143 dep, _ := module.(Dependency)
144 jars = append(jars, dep.HeaderJars()...)
Colin Cross1369cdb2017-09-29 17:58:17 -0700145 })
146
Paul Duffin68289b02019-09-20 13:50:52 +0100147 system.headerJars = jars
148
Pete Gillindf7dc822019-10-09 17:09:38 +0100149 system.outputDir, system.outputDeps = TransformJarsToSystemModules(ctx, jars)
Colin Cross1369cdb2017-09-29 17:58:17 -0700150}
151
152func (system *SystemModules) DepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffin7b81f5e2020-01-13 21:03:22 +0000153 ctx.AddVariationDependencies(nil, systemModulesLibsTag, system.properties.Libs...)
Colin Cross1369cdb2017-09-29 17:58:17 -0700154}
155
156func (system *SystemModules) AndroidMk() android.AndroidMkData {
157 return android.AndroidMkData{
158 Custom: func(w io.Writer, name, prefix, moduleDir string, data android.AndroidMkData) {
Tobias Thiererb1c697d2018-03-26 22:33:59 +0100159 fmt.Fprintln(w)
Dan Willemsenff60a732019-06-13 16:52:01 +0000160
161 makevar := "SOONG_SYSTEM_MODULES_" + name
162 fmt.Fprintln(w, makevar, ":=$=", system.outputDir.String())
163 fmt.Fprintln(w)
164
165 makevar = "SOONG_SYSTEM_MODULES_LIBS_" + name
166 fmt.Fprintln(w, makevar, ":=$=", strings.Join(system.properties.Libs, " "))
167 fmt.Fprintln(w)
168
Dan Willemsenfe310be2019-06-20 10:16:12 -0700169 makevar = "SOONG_SYSTEM_MODULES_DEPS_" + name
Dan Willemsenff60a732019-06-13 16:52:01 +0000170 fmt.Fprintln(w, makevar, ":=$=", strings.Join(system.outputDeps.Strings(), " "))
171 fmt.Fprintln(w)
172
Tobias Thiererb1c697d2018-03-26 22:33:59 +0100173 fmt.Fprintln(w, name+":", "$("+makevar+")")
Dan Willemsena03c43a2018-07-24 13:00:52 -0700174 fmt.Fprintln(w, ".PHONY:", name)
Colin Cross1369cdb2017-09-29 17:58:17 -0700175 },
176 }
177}
Paul Duffin90169ba2020-01-10 17:16:44 +0000178
179// A prebuilt version of java_system_modules. It does not import the
180// generated system module, it generates the system module from imported
181// java libraries in the same way that java_system_modules does. It just
182// acts as a prebuilt, i.e. can have the same base name as another module
183// type and the one to use is selected at runtime.
184func systemModulesImportFactory() android.Module {
185 module := &systemModulesImport{}
186 module.AddProperties(&module.properties)
187 android.InitPrebuiltModule(module, &module.properties.Libs)
188 android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibCommon)
189 android.InitDefaultableModule(module)
Paul Duffin7b81f5e2020-01-13 21:03:22 +0000190 android.InitSdkAwareModule(module)
Paul Duffin90169ba2020-01-10 17:16:44 +0000191 return module
192}
193
194type systemModulesImport struct {
195 SystemModules
196 prebuilt android.Prebuilt
197}
198
199func (system *systemModulesImport) Name() string {
200 return system.prebuilt.Name(system.ModuleBase.Name())
201}
202
203func (system *systemModulesImport) Prebuilt() *android.Prebuilt {
204 return &system.prebuilt
205}
Paul Duffin7b81f5e2020-01-13 21:03:22 +0000206
207type systemModulesSdkMemberType struct {
208 android.SdkMemberTypeBase
209}
210
211func (mt *systemModulesSdkMemberType) AddDependencies(mctx android.BottomUpMutatorContext, dependencyTag blueprint.DependencyTag, names []string) {
212 mctx.AddVariationDependencies(nil, dependencyTag, names...)
213}
214
215func (mt *systemModulesSdkMemberType) IsInstance(module android.Module) bool {
216 if _, ok := module.(*SystemModules); ok {
217 // A prebuilt system module cannot be added as a member of an sdk because the source and
218 // snapshot instances would conflict.
219 _, ok := module.(*systemModulesImport)
220 return !ok
221 }
222 return false
223}
224
225func (mt *systemModulesSdkMemberType) BuildSnapshot(sdkModuleContext android.ModuleContext, builder android.SnapshotBuilder, member android.SdkMember) {
226 variants := member.Variants()
227 if len(variants) != 1 {
228 sdkModuleContext.ModuleErrorf("sdk contains %d variants of member %q but only one is allowed", len(variants), member.Name())
229 for _, variant := range variants {
230 sdkModuleContext.ModuleErrorf(" %q", variant)
231 }
232 }
233 variant := variants[0]
234 systemModule := variant.(*SystemModules)
235
236 pbm := builder.AddPrebuiltModule(member, "java_system_modules_import")
237 // Add the references to the libraries that form the system module.
238 pbm.AddPropertyWithTag("libs", systemModule.properties.Libs, builder.SdkMemberReferencePropertyTag())
239}