blob: 359bee11f5dbe21a427f1665c280ebb022c408e9 [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{
Paul Duffin2d3da312021-05-06 12:02:27 +010038 PropertyName: "java_system_modules",
39 SupportsSdk: true,
Paul Duffin7b81f5e2020-01-13 21:03:22 +000040 },
41 })
Colin Cross1369cdb2017-09-29 17:58:17 -070042}
43
Paul Duffin43dc1cc2019-12-19 11:18:54 +000044func RegisterSystemModulesBuildComponents(ctx android.RegistrationContext) {
45 ctx.RegisterModuleType("java_system_modules", SystemModulesFactory)
Paul Duffin90169ba2020-01-10 17:16:44 +000046 ctx.RegisterModuleType("java_system_modules_import", systemModulesImportFactory)
Paul Duffin43dc1cc2019-12-19 11:18:54 +000047}
48
Colin Cross1369cdb2017-09-29 17:58:17 -070049var (
50 jarsTosystemModules = pctx.AndroidStaticRule("jarsTosystemModules", blueprint.RuleParams{
51 Command: `rm -rf ${outDir} ${workDir} && mkdir -p ${workDir}/jmod && ` +
Pete Gillindf7dc822019-10-09 17:09:38 +010052 `${moduleInfoJavaPath} java.base $in > ${workDir}/module-info.java && ` +
Colin Cross1369cdb2017-09-29 17:58:17 -070053 `${config.JavacCmd} --system=none --patch-module=java.base=${classpath} ${workDir}/module-info.java && ` +
54 `${config.SoongZipCmd} -jar -o ${workDir}/classes.jar -C ${workDir} -f ${workDir}/module-info.class && ` +
55 `${config.MergeZipsCmd} -j ${workDir}/module.jar ${workDir}/classes.jar $in && ` +
Pete Gillin1f52e932019-10-09 17:10:08 +010056 // Note: The version of the java.base module created must match the version
57 // of the jlink tool which consumes it.
Sorin Basca088e0792023-12-21 11:49:29 +000058 // Use LINUX-OTHER to be compatible with JDK 21+ (b/294137077)
59 `${config.JmodCmd} create --module-version ${config.JlinkVersion} --target-platform LINUX-OTHER ` +
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
Paul Duffina7208112021-04-23 21:20:20 +010080 // to the sdk/module_exports/snapshot. Dependencies that are added automatically via this tag are
81 // not automatically exported.
82 systemModulesLibsTag = android.DependencyTagForSdkMemberType(javaHeaderLibsSdkMemberType, false)
Colin Cross1369cdb2017-09-29 17:58:17 -070083)
84
Pete Gillindf7dc822019-10-09 17:09:38 +010085func TransformJarsToSystemModules(ctx android.ModuleContext, jars android.Paths) (android.Path, android.Paths) {
Colin Cross1369cdb2017-09-29 17:58:17 -070086 outDir := android.PathForModuleOut(ctx, "system")
87 workDir := android.PathForModuleOut(ctx, "modules")
88 outputFile := android.PathForModuleOut(ctx, "system/lib/modules")
89 outputs := android.WritablePaths{
90 outputFile,
91 android.PathForModuleOut(ctx, "system/lib/jrt-fs.jar"),
92 android.PathForModuleOut(ctx, "system/release"),
93 }
94
Colin Crossae887032017-10-23 17:16:14 -070095 ctx.Build(pctx, android.BuildParams{
Colin Cross1369cdb2017-09-29 17:58:17 -070096 Rule: jarsTosystemModules,
97 Description: "system modules",
98 Outputs: outputs,
99 Inputs: jars,
100 Args: map[string]string{
Pete Gillindf7dc822019-10-09 17:09:38 +0100101 "classpath": strings.Join(jars.Strings(), ":"),
102 "workDir": workDir.String(),
103 "outDir": outDir.String(),
Colin Cross1369cdb2017-09-29 17:58:17 -0700104 },
105 })
106
Dan Willemsenff60a732019-06-13 16:52:01 +0000107 return outDir, outputs.Paths()
Colin Cross1369cdb2017-09-29 17:58:17 -0700108}
109
Paul Duffincded5ec2020-01-10 17:30:36 +0000110// java_system_modules creates a system module from a set of java libraries that can
111// be referenced from the system_modules property. It must contain at a minimum the
112// java.base module which must include classes from java.lang amongst other java packages.
Colin Cross1369cdb2017-09-29 17:58:17 -0700113func SystemModulesFactory() android.Module {
114 module := &SystemModules{}
115 module.AddProperties(&module.properties)
116 android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibCommon)
Colin Cross667ffa12019-05-28 13:30:02 -0700117 android.InitDefaultableModule(module)
Colin Cross1369cdb2017-09-29 17:58:17 -0700118 return module
119}
120
Paul Duffin83a2d962019-11-19 19:44:10 +0000121type SystemModulesProvider interface {
122 HeaderJars() android.Paths
123 OutputDirAndDeps() (android.Path, android.Paths)
124}
125
126var _ SystemModulesProvider = (*SystemModules)(nil)
127
128var _ SystemModulesProvider = (*systemModulesImport)(nil)
129
Colin Cross1369cdb2017-09-29 17:58:17 -0700130type SystemModules struct {
131 android.ModuleBase
Colin Cross667ffa12019-05-28 13:30:02 -0700132 android.DefaultableModuleBase
Colin Cross1369cdb2017-09-29 17:58:17 -0700133
134 properties SystemModulesProperties
135
Paul Duffin68289b02019-09-20 13:50:52 +0100136 // The aggregated header jars from all jars specified in the libs property.
137 // Used when system module is added as a dependency to bootclasspath.
138 headerJars android.Paths
Dan Willemsenff60a732019-06-13 16:52:01 +0000139 outputDir android.Path
140 outputDeps android.Paths
Colin Cross1369cdb2017-09-29 17:58:17 -0700141}
142
143type SystemModulesProperties struct {
144 // List of java library modules that should be included in the system modules
145 Libs []string
Colin Cross1369cdb2017-09-29 17:58:17 -0700146}
147
Paul Duffin83a2d962019-11-19 19:44:10 +0000148func (system *SystemModules) HeaderJars() android.Paths {
149 return system.headerJars
150}
151
152func (system *SystemModules) OutputDirAndDeps() (android.Path, android.Paths) {
153 if system.outputDir == nil || len(system.outputDeps) == 0 {
154 panic("Missing directory for system module dependency")
155 }
156 return system.outputDir, system.outputDeps
157}
158
Colin Cross1369cdb2017-09-29 17:58:17 -0700159func (system *SystemModules) GenerateAndroidBuildActions(ctx android.ModuleContext) {
160 var jars android.Paths
161
Paul Duffin7b81f5e2020-01-13 21:03:22 +0000162 ctx.VisitDirectDepsWithTag(systemModulesLibsTag, func(module android.Module) {
Colin Crossdcf71b22021-02-01 13:59:03 -0800163 dep, _ := ctx.OtherModuleProvider(module, JavaInfoProvider).(JavaInfo)
164 jars = append(jars, dep.HeaderJars...)
Colin Cross1369cdb2017-09-29 17:58:17 -0700165 })
166
Paul Duffin68289b02019-09-20 13:50:52 +0100167 system.headerJars = jars
168
Pete Gillindf7dc822019-10-09 17:09:38 +0100169 system.outputDir, system.outputDeps = TransformJarsToSystemModules(ctx, jars)
Colin Cross1369cdb2017-09-29 17:58:17 -0700170}
171
Paul Duffin4f0a75a2021-03-11 07:23:27 +0000172// ComponentDepsMutator is called before prebuilt modules without a corresponding source module are
173// renamed so unless the supplied libs specifically includes the prebuilt_ prefix this is guaranteed
174// to only add dependencies on source modules.
175//
176// The systemModuleLibsTag will prevent the prebuilt mutators from replacing this dependency so it
177// will never be changed to depend on a prebuilt either.
178func (system *SystemModules) ComponentDepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffin7b81f5e2020-01-13 21:03:22 +0000179 ctx.AddVariationDependencies(nil, systemModulesLibsTag, system.properties.Libs...)
Colin Cross1369cdb2017-09-29 17:58:17 -0700180}
181
182func (system *SystemModules) AndroidMk() android.AndroidMkData {
183 return android.AndroidMkData{
184 Custom: func(w io.Writer, name, prefix, moduleDir string, data android.AndroidMkData) {
Tobias Thiererb1c697d2018-03-26 22:33:59 +0100185 fmt.Fprintln(w)
Dan Willemsenff60a732019-06-13 16:52:01 +0000186
187 makevar := "SOONG_SYSTEM_MODULES_" + name
188 fmt.Fprintln(w, makevar, ":=$=", system.outputDir.String())
189 fmt.Fprintln(w)
190
191 makevar = "SOONG_SYSTEM_MODULES_LIBS_" + name
192 fmt.Fprintln(w, makevar, ":=$=", strings.Join(system.properties.Libs, " "))
193 fmt.Fprintln(w)
194
Dan Willemsenfe310be2019-06-20 10:16:12 -0700195 makevar = "SOONG_SYSTEM_MODULES_DEPS_" + name
Dan Willemsenff60a732019-06-13 16:52:01 +0000196 fmt.Fprintln(w, makevar, ":=$=", strings.Join(system.outputDeps.Strings(), " "))
197 fmt.Fprintln(w)
198
Tobias Thiererb1c697d2018-03-26 22:33:59 +0100199 fmt.Fprintln(w, name+":", "$("+makevar+")")
Dan Willemsena03c43a2018-07-24 13:00:52 -0700200 fmt.Fprintln(w, ".PHONY:", name)
Bob Badourb4999222021-01-07 03:34:31 +0000201 // TODO(b/151177513): Licenses: Doesn't go through base_rules. May have to generate meta_lic and meta_module here.
Colin Cross1369cdb2017-09-29 17:58:17 -0700202 },
203 }
204}
Paul Duffin90169ba2020-01-10 17:16:44 +0000205
206// A prebuilt version of java_system_modules. It does not import the
207// generated system module, it generates the system module from imported
208// java libraries in the same way that java_system_modules does. It just
209// acts as a prebuilt, i.e. can have the same base name as another module
210// type and the one to use is selected at runtime.
211func systemModulesImportFactory() android.Module {
212 module := &systemModulesImport{}
213 module.AddProperties(&module.properties)
214 android.InitPrebuiltModule(module, &module.properties.Libs)
215 android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibCommon)
216 android.InitDefaultableModule(module)
217 return module
218}
219
220type systemModulesImport struct {
221 SystemModules
222 prebuilt android.Prebuilt
223}
224
225func (system *systemModulesImport) Name() string {
226 return system.prebuilt.Name(system.ModuleBase.Name())
227}
228
229func (system *systemModulesImport) Prebuilt() *android.Prebuilt {
230 return &system.prebuilt
231}
Paul Duffin7b81f5e2020-01-13 21:03:22 +0000232
Paul Duffin4f0a75a2021-03-11 07:23:27 +0000233// ComponentDepsMutator is called before prebuilt modules without a corresponding source module are
234// renamed so as this adds a prebuilt_ prefix this is guaranteed to only add dependencies on source
235// modules.
236func (system *systemModulesImport) ComponentDepsMutator(ctx android.BottomUpMutatorContext) {
237 for _, lib := range system.properties.Libs {
Paul Duffin864116c2021-04-02 10:24:13 +0100238 ctx.AddVariationDependencies(nil, systemModulesLibsTag, android.PrebuiltNameFromSource(lib))
Paul Duffin4f0a75a2021-03-11 07:23:27 +0000239 }
240}
241
Paul Duffin7b81f5e2020-01-13 21:03:22 +0000242type systemModulesSdkMemberType struct {
243 android.SdkMemberTypeBase
244}
245
Paul Duffin296701e2021-07-14 10:29:36 +0100246func (mt *systemModulesSdkMemberType) AddDependencies(ctx android.SdkDependencyContext, dependencyTag blueprint.DependencyTag, names []string) {
247 ctx.AddVariationDependencies(nil, dependencyTag, names...)
Paul Duffin7b81f5e2020-01-13 21:03:22 +0000248}
249
250func (mt *systemModulesSdkMemberType) IsInstance(module android.Module) bool {
251 if _, ok := module.(*SystemModules); ok {
252 // A prebuilt system module cannot be added as a member of an sdk because the source and
253 // snapshot instances would conflict.
254 _, ok := module.(*systemModulesImport)
255 return !ok
256 }
257 return false
258}
259
Paul Duffin495ffb92020-03-20 13:35:40 +0000260func (mt *systemModulesSdkMemberType) AddPrebuiltModule(ctx android.SdkMemberContext, member android.SdkMember) android.BpModule {
261 return ctx.SnapshotBuilder().AddPrebuiltModule(member, "java_system_modules_import")
262}
Paul Duffin7b81f5e2020-01-13 21:03:22 +0000263
Paul Duffin495ffb92020-03-20 13:35:40 +0000264type systemModulesInfoProperties struct {
265 android.SdkMemberPropertiesBase
266
267 Libs []string
268}
269
270func (mt *systemModulesSdkMemberType) CreateVariantPropertiesStruct() android.SdkMemberProperties {
271 return &systemModulesInfoProperties{}
272}
273
274func (p *systemModulesInfoProperties) PopulateFromVariant(ctx android.SdkMemberContext, variant android.Module) {
275 systemModule := variant.(*SystemModules)
276 p.Libs = systemModule.properties.Libs
277}
278
279func (p *systemModulesInfoProperties) AddToPropertySet(ctx android.SdkMemberContext, propertySet android.BpPropertySet) {
280 if len(p.Libs) > 0 {
281 // Add the references to the libraries that form the system module.
282 propertySet.AddPropertyWithTag("libs", p.Libs, ctx.SnapshotBuilder().SdkMemberReferencePropertyTag(true))
283 }
Paul Duffin7b81f5e2020-01-13 21:03:22 +0000284}