2017-09-30 08:58:17 +08:00
|
|
|
// Copyright 2017 Google Inc. All rights reserved.
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package java
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/google/blueprint"
|
|
|
|
|
|
|
|
"android/soong/android"
|
|
|
|
)
|
|
|
|
|
|
|
|
// OpenJDK 9 introduces the concept of "system modules", which replace the bootclasspath. This
|
|
|
|
// file will produce the rules necessary to convert each unique set of bootclasspath jars into
|
|
|
|
// system modules in a runtime image using the jmod and jlink tools.
|
|
|
|
|
|
|
|
func init() {
|
2019-12-19 19:18:54 +08:00
|
|
|
RegisterSystemModulesBuildComponents(android.InitRegistrationContext)
|
2017-09-30 08:58:17 +08:00
|
|
|
|
|
|
|
pctx.SourcePathVariable("moduleInfoJavaPath", "build/soong/scripts/jars-to-module-info-java.sh")
|
|
|
|
}
|
|
|
|
|
2019-12-19 19:18:54 +08:00
|
|
|
func RegisterSystemModulesBuildComponents(ctx android.RegistrationContext) {
|
|
|
|
ctx.RegisterModuleType("java_system_modules", SystemModulesFactory)
|
2020-01-11 01:16:44 +08:00
|
|
|
ctx.RegisterModuleType("java_system_modules_import", systemModulesImportFactory)
|
2019-12-19 19:18:54 +08:00
|
|
|
}
|
|
|
|
|
2017-09-30 08:58:17 +08:00
|
|
|
var (
|
|
|
|
jarsTosystemModules = pctx.AndroidStaticRule("jarsTosystemModules", blueprint.RuleParams{
|
|
|
|
Command: `rm -rf ${outDir} ${workDir} && mkdir -p ${workDir}/jmod && ` +
|
2019-10-10 00:09:38 +08:00
|
|
|
`${moduleInfoJavaPath} java.base $in > ${workDir}/module-info.java && ` +
|
2017-09-30 08:58:17 +08:00
|
|
|
`${config.JavacCmd} --system=none --patch-module=java.base=${classpath} ${workDir}/module-info.java && ` +
|
|
|
|
`${config.SoongZipCmd} -jar -o ${workDir}/classes.jar -C ${workDir} -f ${workDir}/module-info.class && ` +
|
|
|
|
`${config.MergeZipsCmd} -j ${workDir}/module.jar ${workDir}/classes.jar $in && ` +
|
2019-10-10 00:10:08 +08:00
|
|
|
// Note: The version of the java.base module created must match the version
|
|
|
|
// of the jlink tool which consumes it.
|
|
|
|
`${config.JmodCmd} create --module-version ${config.JlinkVersion} --target-platform android ` +
|
2019-10-10 00:09:38 +08:00
|
|
|
` --class-path ${workDir}/module.jar ${workDir}/jmod/java.base.jmod && ` +
|
|
|
|
`${config.JlinkCmd} --module-path ${workDir}/jmod --add-modules java.base --output ${outDir} ` +
|
2019-06-06 03:10:55 +08:00
|
|
|
// Note: The system-modules jlink plugin is disabled because (a) it is not
|
|
|
|
// useful on Android, and (b) it causes errors with later versions of jlink
|
|
|
|
// when the jdk.internal.module is absent from java.base (as it is here).
|
|
|
|
` --disable-plugin system-modules && ` +
|
2017-09-30 08:58:17 +08:00
|
|
|
`cp ${config.JrtFsJar} ${outDir}/lib/`,
|
|
|
|
CommandDeps: []string{
|
|
|
|
"${moduleInfoJavaPath}",
|
|
|
|
"${config.JavacCmd}",
|
|
|
|
"${config.SoongZipCmd}",
|
|
|
|
"${config.MergeZipsCmd}",
|
|
|
|
"${config.JmodCmd}",
|
|
|
|
"${config.JlinkCmd}",
|
|
|
|
"${config.JrtFsJar}",
|
|
|
|
},
|
|
|
|
},
|
2019-10-10 00:09:38 +08:00
|
|
|
"classpath", "outDir", "workDir")
|
2017-09-30 08:58:17 +08:00
|
|
|
)
|
|
|
|
|
2019-10-10 00:09:38 +08:00
|
|
|
func TransformJarsToSystemModules(ctx android.ModuleContext, jars android.Paths) (android.Path, android.Paths) {
|
2017-09-30 08:58:17 +08:00
|
|
|
outDir := android.PathForModuleOut(ctx, "system")
|
|
|
|
workDir := android.PathForModuleOut(ctx, "modules")
|
|
|
|
outputFile := android.PathForModuleOut(ctx, "system/lib/modules")
|
|
|
|
outputs := android.WritablePaths{
|
|
|
|
outputFile,
|
|
|
|
android.PathForModuleOut(ctx, "system/lib/jrt-fs.jar"),
|
|
|
|
android.PathForModuleOut(ctx, "system/release"),
|
|
|
|
}
|
|
|
|
|
2017-10-24 08:16:14 +08:00
|
|
|
ctx.Build(pctx, android.BuildParams{
|
2017-09-30 08:58:17 +08:00
|
|
|
Rule: jarsTosystemModules,
|
|
|
|
Description: "system modules",
|
|
|
|
Outputs: outputs,
|
|
|
|
Inputs: jars,
|
|
|
|
Args: map[string]string{
|
2019-10-10 00:09:38 +08:00
|
|
|
"classpath": strings.Join(jars.Strings(), ":"),
|
|
|
|
"workDir": workDir.String(),
|
|
|
|
"outDir": outDir.String(),
|
2017-09-30 08:58:17 +08:00
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2019-06-14 00:52:01 +08:00
|
|
|
return outDir, outputs.Paths()
|
2017-09-30 08:58:17 +08:00
|
|
|
}
|
|
|
|
|
2020-01-11 01:30:36 +08:00
|
|
|
// java_system_modules creates a system module from a set of java libraries that can
|
|
|
|
// be referenced from the system_modules property. It must contain at a minimum the
|
|
|
|
// java.base module which must include classes from java.lang amongst other java packages.
|
2017-09-30 08:58:17 +08:00
|
|
|
func SystemModulesFactory() android.Module {
|
|
|
|
module := &SystemModules{}
|
|
|
|
module.AddProperties(&module.properties)
|
|
|
|
android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibCommon)
|
2019-05-29 04:30:02 +08:00
|
|
|
android.InitDefaultableModule(module)
|
2017-09-30 08:58:17 +08:00
|
|
|
return module
|
|
|
|
}
|
|
|
|
|
|
|
|
type SystemModules struct {
|
|
|
|
android.ModuleBase
|
2019-05-29 04:30:02 +08:00
|
|
|
android.DefaultableModuleBase
|
2017-09-30 08:58:17 +08:00
|
|
|
|
|
|
|
properties SystemModulesProperties
|
|
|
|
|
2019-09-20 20:50:52 +08:00
|
|
|
// The aggregated header jars from all jars specified in the libs property.
|
|
|
|
// Used when system module is added as a dependency to bootclasspath.
|
|
|
|
headerJars android.Paths
|
2019-06-14 00:52:01 +08:00
|
|
|
outputDir android.Path
|
|
|
|
outputDeps android.Paths
|
2017-09-30 08:58:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
type SystemModulesProperties struct {
|
|
|
|
// List of java library modules that should be included in the system modules
|
|
|
|
Libs []string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (system *SystemModules) GenerateAndroidBuildActions(ctx android.ModuleContext) {
|
|
|
|
var jars android.Paths
|
|
|
|
|
2017-12-31 09:54:27 +08:00
|
|
|
ctx.VisitDirectDepsWithTag(libTag, func(module android.Module) {
|
|
|
|
dep, _ := module.(Dependency)
|
|
|
|
jars = append(jars, dep.HeaderJars()...)
|
2017-09-30 08:58:17 +08:00
|
|
|
})
|
|
|
|
|
2019-09-20 20:50:52 +08:00
|
|
|
system.headerJars = jars
|
|
|
|
|
2019-10-10 00:09:38 +08:00
|
|
|
system.outputDir, system.outputDeps = TransformJarsToSystemModules(ctx, jars)
|
2017-09-30 08:58:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (system *SystemModules) DepsMutator(ctx android.BottomUpMutatorContext) {
|
2018-08-30 05:10:52 +08:00
|
|
|
ctx.AddVariationDependencies(nil, libTag, system.properties.Libs...)
|
2017-09-30 08:58:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (system *SystemModules) AndroidMk() android.AndroidMkData {
|
|
|
|
return android.AndroidMkData{
|
|
|
|
Custom: func(w io.Writer, name, prefix, moduleDir string, data android.AndroidMkData) {
|
2019-06-14 00:52:01 +08:00
|
|
|
fmt.Fprintln(w)
|
|
|
|
|
2018-03-27 05:33:59 +08:00
|
|
|
makevar := "SOONG_SYSTEM_MODULES_" + name
|
2019-06-14 00:52:01 +08:00
|
|
|
fmt.Fprintln(w, makevar, ":=$=", system.outputDir.String())
|
|
|
|
fmt.Fprintln(w)
|
|
|
|
|
|
|
|
makevar = "SOONG_SYSTEM_MODULES_LIBS_" + name
|
|
|
|
fmt.Fprintln(w, makevar, ":=$=", strings.Join(system.properties.Libs, " "))
|
2018-03-27 05:33:59 +08:00
|
|
|
fmt.Fprintln(w)
|
2019-06-14 00:52:01 +08:00
|
|
|
|
2019-06-21 01:16:12 +08:00
|
|
|
makevar = "SOONG_SYSTEM_MODULES_DEPS_" + name
|
2019-06-14 00:52:01 +08:00
|
|
|
fmt.Fprintln(w, makevar, ":=$=", strings.Join(system.outputDeps.Strings(), " "))
|
|
|
|
fmt.Fprintln(w)
|
|
|
|
|
2018-03-27 05:33:59 +08:00
|
|
|
fmt.Fprintln(w, name+":", "$("+makevar+")")
|
2018-07-25 04:00:52 +08:00
|
|
|
fmt.Fprintln(w, ".PHONY:", name)
|
2017-09-30 08:58:17 +08:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
2020-01-11 01:16:44 +08:00
|
|
|
|
|
|
|
// A prebuilt version of java_system_modules. It does not import the
|
|
|
|
// generated system module, it generates the system module from imported
|
|
|
|
// java libraries in the same way that java_system_modules does. It just
|
|
|
|
// acts as a prebuilt, i.e. can have the same base name as another module
|
|
|
|
// type and the one to use is selected at runtime.
|
|
|
|
func systemModulesImportFactory() android.Module {
|
|
|
|
module := &systemModulesImport{}
|
|
|
|
module.AddProperties(&module.properties)
|
|
|
|
android.InitPrebuiltModule(module, &module.properties.Libs)
|
|
|
|
android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibCommon)
|
|
|
|
android.InitDefaultableModule(module)
|
|
|
|
return module
|
|
|
|
}
|
|
|
|
|
|
|
|
type systemModulesImport struct {
|
|
|
|
SystemModules
|
|
|
|
prebuilt android.Prebuilt
|
|
|
|
}
|
|
|
|
|
|
|
|
func (system *systemModulesImport) Name() string {
|
|
|
|
return system.prebuilt.Name(system.ModuleBase.Name())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (system *systemModulesImport) Prebuilt() *android.Prebuilt {
|
|
|
|
return &system.prebuilt
|
|
|
|
}
|