blob: 8005360a2b8fb396744d8e3f32d472ca9a5a24ca [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 && ` +
45 `${config.JlinkCmd} --module-path ${workDir}/jmod --add-modules ${moduleName} --output ${outDir} && ` +
46 `cp ${config.JrtFsJar} ${outDir}/lib/`,
47 CommandDeps: []string{
48 "${moduleInfoJavaPath}",
49 "${config.JavacCmd}",
50 "${config.SoongZipCmd}",
51 "${config.MergeZipsCmd}",
52 "${config.JmodCmd}",
53 "${config.JlinkCmd}",
54 "${config.JrtFsJar}",
55 },
56 },
57 "moduleName", "classpath", "outDir", "workDir")
58)
59
60func TransformJarsToSystemModules(ctx android.ModuleContext, moduleName string, jars android.Paths) android.WritablePath {
61 outDir := android.PathForModuleOut(ctx, "system")
62 workDir := android.PathForModuleOut(ctx, "modules")
63 outputFile := android.PathForModuleOut(ctx, "system/lib/modules")
64 outputs := android.WritablePaths{
65 outputFile,
66 android.PathForModuleOut(ctx, "system/lib/jrt-fs.jar"),
67 android.PathForModuleOut(ctx, "system/release"),
68 }
69
Colin Crossae887032017-10-23 17:16:14 -070070 ctx.Build(pctx, android.BuildParams{
Colin Cross1369cdb2017-09-29 17:58:17 -070071 Rule: jarsTosystemModules,
72 Description: "system modules",
73 Outputs: outputs,
74 Inputs: jars,
75 Args: map[string]string{
76 "moduleName": moduleName,
77 "classpath": strings.Join(jars.Strings(), ":"),
78 "workDir": workDir.String(),
79 "outDir": outDir.String(),
80 },
81 })
82
83 return outputFile
84}
85
86func SystemModulesFactory() android.Module {
87 module := &SystemModules{}
88 module.AddProperties(&module.properties)
89 android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibCommon)
Colin Cross667ffa12019-05-28 13:30:02 -070090 android.InitDefaultableModule(module)
Colin Cross1369cdb2017-09-29 17:58:17 -070091 return module
92}
93
94type SystemModules struct {
95 android.ModuleBase
Colin Cross667ffa12019-05-28 13:30:02 -070096 android.DefaultableModuleBase
Colin Cross1369cdb2017-09-29 17:58:17 -070097
98 properties SystemModulesProperties
99
100 outputFile android.Path
101}
102
103type SystemModulesProperties struct {
104 // List of java library modules that should be included in the system modules
105 Libs []string
106
107 // List of prebuilt jars that should be included in the system modules
108 Jars []string
109
110 // Sdk version that should be included in the system modules
111 Sdk_version *string
112}
113
114func (system *SystemModules) GenerateAndroidBuildActions(ctx android.ModuleContext) {
115 var jars android.Paths
116
Colin Crossee6143c2017-12-30 17:54:27 -0800117 ctx.VisitDirectDepsWithTag(libTag, func(module android.Module) {
118 dep, _ := module.(Dependency)
119 jars = append(jars, dep.HeaderJars()...)
Colin Cross1369cdb2017-09-29 17:58:17 -0700120 })
121
122 jars = append(jars, android.PathsForModuleSrc(ctx, system.properties.Jars)...)
123
Tobias Thiererb1c697d2018-03-26 22:33:59 +0100124 system.outputFile = TransformJarsToSystemModules(ctx, "java.base", jars)
Colin Cross1369cdb2017-09-29 17:58:17 -0700125}
126
127func (system *SystemModules) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross42d48b72018-08-29 14:10:52 -0700128 ctx.AddVariationDependencies(nil, libTag, system.properties.Libs...)
Colin Cross1369cdb2017-09-29 17:58:17 -0700129}
130
131func (system *SystemModules) AndroidMk() android.AndroidMkData {
132 return android.AndroidMkData{
133 Custom: func(w io.Writer, name, prefix, moduleDir string, data android.AndroidMkData) {
Tobias Thiererb1c697d2018-03-26 22:33:59 +0100134 makevar := "SOONG_SYSTEM_MODULES_" + name
135 fmt.Fprintln(w)
136 fmt.Fprintln(w, makevar, ":=", system.outputFile.String())
137 fmt.Fprintln(w, ".KATI_READONLY", ":=", makevar)
138 fmt.Fprintln(w, name+":", "$("+makevar+")")
Dan Willemsena03c43a2018-07-24 13:00:52 -0700139 fmt.Fprintln(w, ".PHONY:", name)
Tobias Thiererb1c697d2018-03-26 22:33:59 +0100140 fmt.Fprintln(w)
141 makevar = "SOONG_SYSTEM_MODULES_LIBS_" + name
142 fmt.Fprintln(w, makevar, ":=", strings.Join(system.properties.Libs, " "))
143 fmt.Fprintln(w, ".KATI_READONLY :=", makevar)
Colin Cross1369cdb2017-09-29 17:58:17 -0700144 },
145 }
146}