blob: 43e4e118b9cd8f42ae9b84c733b4332f4ed6c637 [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 && ` +
39 `${moduleInfoJavaPath} ${moduleName} $in > ${workDir}/module-info.java && ` +
40 `${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 && ` +
43 `${config.JmodCmd} create --module-version 9 --target-platform android ` +
44 ` --class-path ${workDir}/module.jar ${workDir}/jmod/${moduleName}.jmod && ` +
Pete Gillin4eb6be32019-06-05 20:10:55 +010045 `${config.JlinkCmd} --module-path ${workDir}/jmod --add-modules ${moduleName} --output ${outDir} ` +
46 // Note: The system-modules jlink plugin is disabled because (a) it is not
47 // useful on Android, and (b) it causes errors with later versions of jlink
48 // when the jdk.internal.module is absent from java.base (as it is here).
49 ` --disable-plugin system-modules && ` +
Colin Cross1369cdb2017-09-29 17:58:17 -070050 `cp ${config.JrtFsJar} ${outDir}/lib/`,
51 CommandDeps: []string{
52 "${moduleInfoJavaPath}",
53 "${config.JavacCmd}",
54 "${config.SoongZipCmd}",
55 "${config.MergeZipsCmd}",
56 "${config.JmodCmd}",
57 "${config.JlinkCmd}",
58 "${config.JrtFsJar}",
59 },
60 },
61 "moduleName", "classpath", "outDir", "workDir")
62)
63
Dan Willemsenff60a732019-06-13 16:52:01 +000064func TransformJarsToSystemModules(ctx android.ModuleContext, moduleName string, jars android.Paths) (android.Path, android.Paths) {
Colin Cross1369cdb2017-09-29 17:58:17 -070065 outDir := android.PathForModuleOut(ctx, "system")
66 workDir := android.PathForModuleOut(ctx, "modules")
67 outputFile := android.PathForModuleOut(ctx, "system/lib/modules")
68 outputs := android.WritablePaths{
69 outputFile,
70 android.PathForModuleOut(ctx, "system/lib/jrt-fs.jar"),
71 android.PathForModuleOut(ctx, "system/release"),
72 }
73
Colin Crossae887032017-10-23 17:16:14 -070074 ctx.Build(pctx, android.BuildParams{
Colin Cross1369cdb2017-09-29 17:58:17 -070075 Rule: jarsTosystemModules,
76 Description: "system modules",
77 Outputs: outputs,
78 Inputs: jars,
79 Args: map[string]string{
80 "moduleName": moduleName,
81 "classpath": strings.Join(jars.Strings(), ":"),
82 "workDir": workDir.String(),
83 "outDir": outDir.String(),
84 },
85 })
86
Dan Willemsenff60a732019-06-13 16:52:01 +000087 return outDir, outputs.Paths()
Colin Cross1369cdb2017-09-29 17:58:17 -070088}
89
90func SystemModulesFactory() android.Module {
91 module := &SystemModules{}
92 module.AddProperties(&module.properties)
93 android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibCommon)
Colin Cross667ffa12019-05-28 13:30:02 -070094 android.InitDefaultableModule(module)
Colin Cross1369cdb2017-09-29 17:58:17 -070095 return module
96}
97
98type SystemModules struct {
99 android.ModuleBase
Colin Cross667ffa12019-05-28 13:30:02 -0700100 android.DefaultableModuleBase
Colin Cross1369cdb2017-09-29 17:58:17 -0700101
102 properties SystemModulesProperties
103
Paul Duffin68289b02019-09-20 13:50:52 +0100104 // The aggregated header jars from all jars specified in the libs property.
105 // Used when system module is added as a dependency to bootclasspath.
106 headerJars android.Paths
Dan Willemsenff60a732019-06-13 16:52:01 +0000107 outputDir android.Path
108 outputDeps android.Paths
Colin Cross1369cdb2017-09-29 17:58:17 -0700109}
110
111type SystemModulesProperties struct {
112 // List of java library modules that should be included in the system modules
113 Libs []string
Colin Cross1369cdb2017-09-29 17:58:17 -0700114}
115
116func (system *SystemModules) GenerateAndroidBuildActions(ctx android.ModuleContext) {
117 var jars android.Paths
118
Colin Crossee6143c2017-12-30 17:54:27 -0800119 ctx.VisitDirectDepsWithTag(libTag, func(module android.Module) {
120 dep, _ := module.(Dependency)
121 jars = append(jars, dep.HeaderJars()...)
Colin Cross1369cdb2017-09-29 17:58:17 -0700122 })
123
Paul Duffin68289b02019-09-20 13:50:52 +0100124 system.headerJars = jars
125
Dan Willemsenff60a732019-06-13 16:52:01 +0000126 system.outputDir, system.outputDeps = TransformJarsToSystemModules(ctx, "java.base", jars)
Colin Cross1369cdb2017-09-29 17:58:17 -0700127}
128
129func (system *SystemModules) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross42d48b72018-08-29 14:10:52 -0700130 ctx.AddVariationDependencies(nil, libTag, system.properties.Libs...)
Colin Cross1369cdb2017-09-29 17:58:17 -0700131}
132
133func (system *SystemModules) AndroidMk() android.AndroidMkData {
134 return android.AndroidMkData{
135 Custom: func(w io.Writer, name, prefix, moduleDir string, data android.AndroidMkData) {
Tobias Thiererb1c697d2018-03-26 22:33:59 +0100136 fmt.Fprintln(w)
Dan Willemsenff60a732019-06-13 16:52:01 +0000137
138 makevar := "SOONG_SYSTEM_MODULES_" + name
139 fmt.Fprintln(w, makevar, ":=$=", system.outputDir.String())
140 fmt.Fprintln(w)
141
142 makevar = "SOONG_SYSTEM_MODULES_LIBS_" + name
143 fmt.Fprintln(w, makevar, ":=$=", strings.Join(system.properties.Libs, " "))
144 fmt.Fprintln(w)
145
Dan Willemsenfe310be2019-06-20 10:16:12 -0700146 makevar = "SOONG_SYSTEM_MODULES_DEPS_" + name
Dan Willemsenff60a732019-06-13 16:52:01 +0000147 fmt.Fprintln(w, makevar, ":=$=", strings.Join(system.outputDeps.Strings(), " "))
148 fmt.Fprintln(w)
149
Tobias Thiererb1c697d2018-03-26 22:33:59 +0100150 fmt.Fprintln(w, name+":", "$("+makevar+")")
Dan Willemsena03c43a2018-07-24 13:00:52 -0700151 fmt.Fprintln(w, ".PHONY:", name)
Colin Cross1369cdb2017-09-29 17:58:17 -0700152 },
153 }
154}