blob: d9430b25e27f4690b30bdf384e565f5145b370fe [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"
Spandan Dascc530632024-01-19 00:22:22 +000022 "github.com/google/blueprint/proptools"
Colin Cross1369cdb2017-09-29 17:58:17 -070023
24 "android/soong/android"
25)
26
27// OpenJDK 9 introduces the concept of "system modules", which replace the bootclasspath. This
28// file will produce the rules necessary to convert each unique set of bootclasspath jars into
29// system modules in a runtime image using the jmod and jlink tools.
30
31func init() {
Paul Duffin43dc1cc2019-12-19 11:18:54 +000032 RegisterSystemModulesBuildComponents(android.InitRegistrationContext)
Colin Cross1369cdb2017-09-29 17:58:17 -070033
34 pctx.SourcePathVariable("moduleInfoJavaPath", "build/soong/scripts/jars-to-module-info-java.sh")
Paul Duffin7b81f5e2020-01-13 21:03:22 +000035
36 // Register sdk member types.
37 android.RegisterSdkMemberType(&systemModulesSdkMemberType{
38 android.SdkMemberTypeBase{
Paul Duffin2d3da312021-05-06 12:02:27 +010039 PropertyName: "java_system_modules",
40 SupportsSdk: true,
Paul Duffin7b81f5e2020-01-13 21:03:22 +000041 },
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.
Sorin Basca088e0792023-12-21 11:49:29 +000059 // Use LINUX-OTHER to be compatible with JDK 21+ (b/294137077)
60 `${config.JmodCmd} create --module-version ${config.JlinkVersion} --target-platform LINUX-OTHER ` +
Pete Gillindf7dc822019-10-09 17:09:38 +010061 ` --class-path ${workDir}/module.jar ${workDir}/jmod/java.base.jmod && ` +
62 `${config.JlinkCmd} --module-path ${workDir}/jmod --add-modules java.base --output ${outDir} ` +
Pete Gillin4eb6be32019-06-05 20:10:55 +010063 // Note: The system-modules jlink plugin is disabled because (a) it is not
64 // useful on Android, and (b) it causes errors with later versions of jlink
65 // when the jdk.internal.module is absent from java.base (as it is here).
66 ` --disable-plugin system-modules && ` +
Cole Faust7ef61d72024-03-14 18:15:28 -070067 `rm -rf ${workDir} && ` +
Colin Cross1369cdb2017-09-29 17:58:17 -070068 `cp ${config.JrtFsJar} ${outDir}/lib/`,
69 CommandDeps: []string{
70 "${moduleInfoJavaPath}",
71 "${config.JavacCmd}",
72 "${config.SoongZipCmd}",
73 "${config.MergeZipsCmd}",
74 "${config.JmodCmd}",
75 "${config.JlinkCmd}",
76 "${config.JrtFsJar}",
77 },
78 },
Pete Gillindf7dc822019-10-09 17:09:38 +010079 "classpath", "outDir", "workDir")
Paul Duffin7b81f5e2020-01-13 21:03:22 +000080
81 // Dependency tag that causes the added dependencies to be added as java_header_libs
Paul Duffina7208112021-04-23 21:20:20 +010082 // to the sdk/module_exports/snapshot. Dependencies that are added automatically via this tag are
83 // not automatically exported.
84 systemModulesLibsTag = android.DependencyTagForSdkMemberType(javaHeaderLibsSdkMemberType, false)
Colin Cross1369cdb2017-09-29 17:58:17 -070085)
86
Pete Gillindf7dc822019-10-09 17:09:38 +010087func TransformJarsToSystemModules(ctx android.ModuleContext, jars android.Paths) (android.Path, android.Paths) {
Colin Cross1369cdb2017-09-29 17:58:17 -070088 outDir := android.PathForModuleOut(ctx, "system")
89 workDir := android.PathForModuleOut(ctx, "modules")
90 outputFile := android.PathForModuleOut(ctx, "system/lib/modules")
91 outputs := android.WritablePaths{
92 outputFile,
93 android.PathForModuleOut(ctx, "system/lib/jrt-fs.jar"),
94 android.PathForModuleOut(ctx, "system/release"),
95 }
96
Colin Crossae887032017-10-23 17:16:14 -070097 ctx.Build(pctx, android.BuildParams{
Colin Cross1369cdb2017-09-29 17:58:17 -070098 Rule: jarsTosystemModules,
99 Description: "system modules",
100 Outputs: outputs,
101 Inputs: jars,
102 Args: map[string]string{
Pete Gillindf7dc822019-10-09 17:09:38 +0100103 "classpath": strings.Join(jars.Strings(), ":"),
104 "workDir": workDir.String(),
105 "outDir": outDir.String(),
Colin Cross1369cdb2017-09-29 17:58:17 -0700106 },
107 })
108
Dan Willemsenff60a732019-06-13 16:52:01 +0000109 return outDir, outputs.Paths()
Colin Cross1369cdb2017-09-29 17:58:17 -0700110}
111
Paul Duffincded5ec2020-01-10 17:30:36 +0000112// java_system_modules creates a system module from a set of java libraries that can
113// be referenced from the system_modules property. It must contain at a minimum the
114// java.base module which must include classes from java.lang amongst other java packages.
Colin Cross1369cdb2017-09-29 17:58:17 -0700115func SystemModulesFactory() android.Module {
116 module := &SystemModules{}
117 module.AddProperties(&module.properties)
118 android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibCommon)
Colin Cross667ffa12019-05-28 13:30:02 -0700119 android.InitDefaultableModule(module)
Colin Cross1369cdb2017-09-29 17:58:17 -0700120 return module
121}
122
Colin Crossb61c2262024-08-08 14:04:42 -0700123type SystemModulesProviderInfo struct {
124 // The aggregated header jars from all jars specified in the libs property.
125 // Used when system module is added as a dependency to bootclasspath.
126 HeaderJars android.Paths
127
128 OutputDir android.Path
129 OutputDirDeps android.Paths
Colin Crossc9b4f6b2024-07-26 15:25:46 -0700130
131 // depset of header jars for this module and all transitive static dependencies
132 TransitiveStaticLibsHeaderJars *android.DepSet[android.Path]
Paul Duffin83a2d962019-11-19 19:44:10 +0000133}
134
Colin Crossb61c2262024-08-08 14:04:42 -0700135var SystemModulesProvider = blueprint.NewProvider[*SystemModulesProviderInfo]()
Paul Duffin83a2d962019-11-19 19:44:10 +0000136
Colin Cross1369cdb2017-09-29 17:58:17 -0700137type SystemModules struct {
138 android.ModuleBase
Colin Cross667ffa12019-05-28 13:30:02 -0700139 android.DefaultableModuleBase
Colin Cross1369cdb2017-09-29 17:58:17 -0700140
141 properties SystemModulesProperties
142
Dan Willemsenff60a732019-06-13 16:52:01 +0000143 outputDir android.Path
144 outputDeps android.Paths
Colin Cross1369cdb2017-09-29 17:58:17 -0700145}
146
147type SystemModulesProperties struct {
148 // List of java library modules that should be included in the system modules
149 Libs []string
Colin Cross1369cdb2017-09-29 17:58:17 -0700150}
151
152func (system *SystemModules) GenerateAndroidBuildActions(ctx android.ModuleContext) {
153 var jars android.Paths
154
Colin Crossc9b4f6b2024-07-26 15:25:46 -0700155 var transitiveStaticLibsHeaderJars []*android.DepSet[android.Path]
Paul Duffin7b81f5e2020-01-13 21:03:22 +0000156 ctx.VisitDirectDepsWithTag(systemModulesLibsTag, func(module android.Module) {
Colin Cross7727c7f2024-07-18 15:36:32 -0700157 if dep, ok := android.OtherModuleProvider(ctx, module, JavaInfoProvider); ok {
158 jars = append(jars, dep.HeaderJars...)
Colin Crossc9b4f6b2024-07-26 15:25:46 -0700159 if dep.TransitiveStaticLibsHeaderJars != nil {
160 transitiveStaticLibsHeaderJars = append(transitiveStaticLibsHeaderJars, dep.TransitiveStaticLibsHeaderJars)
161 }
Colin Cross7727c7f2024-07-18 15:36:32 -0700162 }
Colin Cross1369cdb2017-09-29 17:58:17 -0700163 })
164
Pete Gillindf7dc822019-10-09 17:09:38 +0100165 system.outputDir, system.outputDeps = TransformJarsToSystemModules(ctx, jars)
Colin Crossb61c2262024-08-08 14:04:42 -0700166
167 android.SetProvider(ctx, SystemModulesProvider, &SystemModulesProviderInfo{
Colin Crossc9b4f6b2024-07-26 15:25:46 -0700168 HeaderJars: jars,
169 OutputDir: system.outputDir,
170 OutputDirDeps: system.outputDeps,
171 TransitiveStaticLibsHeaderJars: android.NewDepSet(android.PREORDER, nil, transitiveStaticLibsHeaderJars),
Colin Crossb61c2262024-08-08 14:04:42 -0700172 })
Colin Cross1369cdb2017-09-29 17:58:17 -0700173}
174
Paul Duffin4f0a75a2021-03-11 07:23:27 +0000175// ComponentDepsMutator is called before prebuilt modules without a corresponding source module are
176// renamed so unless the supplied libs specifically includes the prebuilt_ prefix this is guaranteed
177// to only add dependencies on source modules.
178//
179// The systemModuleLibsTag will prevent the prebuilt mutators from replacing this dependency so it
180// will never be changed to depend on a prebuilt either.
181func (system *SystemModules) ComponentDepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffin7b81f5e2020-01-13 21:03:22 +0000182 ctx.AddVariationDependencies(nil, systemModulesLibsTag, system.properties.Libs...)
Colin Cross1369cdb2017-09-29 17:58:17 -0700183}
184
185func (system *SystemModules) AndroidMk() android.AndroidMkData {
186 return android.AndroidMkData{
187 Custom: func(w io.Writer, name, prefix, moduleDir string, data android.AndroidMkData) {
Tobias Thiererb1c697d2018-03-26 22:33:59 +0100188 fmt.Fprintln(w)
Dan Willemsenff60a732019-06-13 16:52:01 +0000189
190 makevar := "SOONG_SYSTEM_MODULES_" + name
191 fmt.Fprintln(w, makevar, ":=$=", system.outputDir.String())
192 fmt.Fprintln(w)
193
194 makevar = "SOONG_SYSTEM_MODULES_LIBS_" + name
195 fmt.Fprintln(w, makevar, ":=$=", strings.Join(system.properties.Libs, " "))
196 fmt.Fprintln(w)
197
Dan Willemsenfe310be2019-06-20 10:16:12 -0700198 makevar = "SOONG_SYSTEM_MODULES_DEPS_" + name
Dan Willemsenff60a732019-06-13 16:52:01 +0000199 fmt.Fprintln(w, makevar, ":=$=", strings.Join(system.outputDeps.Strings(), " "))
200 fmt.Fprintln(w)
201
Tobias Thiererb1c697d2018-03-26 22:33:59 +0100202 fmt.Fprintln(w, name+":", "$("+makevar+")")
Dan Willemsena03c43a2018-07-24 13:00:52 -0700203 fmt.Fprintln(w, ".PHONY:", name)
Bob Badourb4999222021-01-07 03:34:31 +0000204 // 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 -0700205 },
206 }
207}
Paul Duffin90169ba2020-01-10 17:16:44 +0000208
209// A prebuilt version of java_system_modules. It does not import the
210// generated system module, it generates the system module from imported
211// java libraries in the same way that java_system_modules does. It just
212// acts as a prebuilt, i.e. can have the same base name as another module
213// type and the one to use is selected at runtime.
214func systemModulesImportFactory() android.Module {
215 module := &systemModulesImport{}
Spandan Dascc530632024-01-19 00:22:22 +0000216 module.AddProperties(&module.properties, &module.prebuiltProperties)
Paul Duffin90169ba2020-01-10 17:16:44 +0000217 android.InitPrebuiltModule(module, &module.properties.Libs)
218 android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibCommon)
219 android.InitDefaultableModule(module)
220 return module
221}
222
223type systemModulesImport struct {
224 SystemModules
Spandan Dascc530632024-01-19 00:22:22 +0000225 prebuilt android.Prebuilt
226 prebuiltProperties prebuiltSystemModulesProperties
227}
228
229type prebuiltSystemModulesProperties struct {
230 // Name of the source soong module that gets shadowed by this prebuilt
231 // If unspecified, follows the naming convention that the source module of
232 // the prebuilt is Name() without "prebuilt_" prefix
233 Source_module_name *string
Paul Duffin90169ba2020-01-10 17:16:44 +0000234}
235
236func (system *systemModulesImport) Name() string {
237 return system.prebuilt.Name(system.ModuleBase.Name())
238}
239
Spandan Dascc530632024-01-19 00:22:22 +0000240// BaseModuleName returns the source module that will get shadowed by this prebuilt
241// e.g.
242//
243// java_system_modules_import {
244// name: "my_system_modules.v1",
245// source_module_name: "my_system_modules",
246// }
247//
248// java_system_modules_import {
249// name: "my_system_modules.v2",
250// source_module_name: "my_system_modules",
251// }
252//
253// `BaseModuleName` for both will return `my_system_modules`
254func (system *systemModulesImport) BaseModuleName() string {
255 return proptools.StringDefault(system.prebuiltProperties.Source_module_name, system.ModuleBase.Name())
256}
257
Paul Duffin90169ba2020-01-10 17:16:44 +0000258func (system *systemModulesImport) Prebuilt() *android.Prebuilt {
259 return &system.prebuilt
260}
Paul Duffin7b81f5e2020-01-13 21:03:22 +0000261
Paul Duffin4f0a75a2021-03-11 07:23:27 +0000262// ComponentDepsMutator is called before prebuilt modules without a corresponding source module are
263// renamed so as this adds a prebuilt_ prefix this is guaranteed to only add dependencies on source
264// modules.
265func (system *systemModulesImport) ComponentDepsMutator(ctx android.BottomUpMutatorContext) {
266 for _, lib := range system.properties.Libs {
Paul Duffin864116c2021-04-02 10:24:13 +0100267 ctx.AddVariationDependencies(nil, systemModulesLibsTag, android.PrebuiltNameFromSource(lib))
Paul Duffin4f0a75a2021-03-11 07:23:27 +0000268 }
269}
270
Paul Duffin7b81f5e2020-01-13 21:03:22 +0000271type systemModulesSdkMemberType struct {
272 android.SdkMemberTypeBase
273}
274
Paul Duffin296701e2021-07-14 10:29:36 +0100275func (mt *systemModulesSdkMemberType) AddDependencies(ctx android.SdkDependencyContext, dependencyTag blueprint.DependencyTag, names []string) {
276 ctx.AddVariationDependencies(nil, dependencyTag, names...)
Paul Duffin7b81f5e2020-01-13 21:03:22 +0000277}
278
279func (mt *systemModulesSdkMemberType) IsInstance(module android.Module) bool {
280 if _, ok := module.(*SystemModules); ok {
281 // A prebuilt system module cannot be added as a member of an sdk because the source and
282 // snapshot instances would conflict.
283 _, ok := module.(*systemModulesImport)
284 return !ok
285 }
286 return false
287}
288
Paul Duffin495ffb92020-03-20 13:35:40 +0000289func (mt *systemModulesSdkMemberType) AddPrebuiltModule(ctx android.SdkMemberContext, member android.SdkMember) android.BpModule {
290 return ctx.SnapshotBuilder().AddPrebuiltModule(member, "java_system_modules_import")
291}
Paul Duffin7b81f5e2020-01-13 21:03:22 +0000292
Paul Duffin495ffb92020-03-20 13:35:40 +0000293type systemModulesInfoProperties struct {
294 android.SdkMemberPropertiesBase
295
296 Libs []string
297}
298
299func (mt *systemModulesSdkMemberType) CreateVariantPropertiesStruct() android.SdkMemberProperties {
300 return &systemModulesInfoProperties{}
301}
302
303func (p *systemModulesInfoProperties) PopulateFromVariant(ctx android.SdkMemberContext, variant android.Module) {
304 systemModule := variant.(*SystemModules)
305 p.Libs = systemModule.properties.Libs
306}
307
308func (p *systemModulesInfoProperties) AddToPropertySet(ctx android.SdkMemberContext, propertySet android.BpPropertySet) {
309 if len(p.Libs) > 0 {
310 // Add the references to the libraries that form the system module.
311 propertySet.AddPropertyWithTag("libs", p.Libs, ctx.SnapshotBuilder().SdkMemberReferencePropertyTag(true))
312 }
Paul Duffin7b81f5e2020-01-13 21:03:22 +0000313}
Spandan Dasf5ee86c2024-08-08 21:11:42 +0000314
315// implement the following interface for IDE completion.
316var _ android.IDEInfo = (*SystemModules)(nil)
317
Cole Faustb36d31d2024-08-27 16:04:28 -0700318func (s *SystemModules) IDEInfo(ctx android.BaseModuleContext, ideInfo *android.IdeInfo) {
Spandan Dasf5ee86c2024-08-08 21:11:42 +0000319 ideInfo.Deps = append(ideInfo.Deps, s.properties.Libs...)
320 ideInfo.Libs = append(ideInfo.Libs, s.properties.Libs...)
321}