platform_build_soong/java/system_modules.go

156 lines
5.4 KiB
Go
Raw Normal View History

// 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() {
android.RegisterModuleType("java_system_modules", SystemModulesFactory)
pctx.SourcePathVariable("moduleInfoJavaPath", "build/soong/scripts/jars-to-module-info-java.sh")
}
var (
jarsTosystemModules = pctx.AndroidStaticRule("jarsTosystemModules", blueprint.RuleParams{
Command: `rm -rf ${outDir} ${workDir} && mkdir -p ${workDir}/jmod && ` +
`${moduleInfoJavaPath} java.base $in > ${workDir}/module-info.java && ` +
`${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 && ` +
// 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 ` +
` --class-path ${workDir}/module.jar ${workDir}/jmod/java.base.jmod && ` +
`${config.JlinkCmd} --module-path ${workDir}/jmod --add-modules java.base --output ${outDir} ` +
Disable the system-modules plugin for jlink invocations. This jlink plugin is intended to optimise startup times for Java runtimes by embedding a pre-processed and pre-validated module graph in the system image. This provides no benefit on Android, since the Android runtime does not make use of the module graph it produces. (It doesn't seem useful on Android anyway, since the system image only contains one module, namely java.base.) Furthermore, the plugin causes the jlink invocation to fail when using the jlink tool from OpenJDK 11. The issue here is the plugin uses classes in the jdk.internal.module package to describe the module graph; that package is not part of libcore and therefore not listed in the module-info.java for java.base on Android; but the plugin has the side-effect of adding the package to java.base; this causes jlink to subsequently fail with an error "Module java.base's descriptor indicates the set of packages is : <lots of packages>, but module contains packages: <same packages plus jdk.internal.module>". (The implementation of the plugin changed significantly in OpenJDK 10, which is presumably why this issue does not occur using OpenJDK 9's jlink.) Therefore, it is safe and beneficial to disable the plugin. Test: EXPERIMENTAL_JAVA_LANGUAGE_LEVEL_9=true make core-all-system-modules, using an OpenJDK 11 toolchain via OVERRIDE_ANDROID_JAVA_HOME and changing the jmod create invocation to use --module-version 11 Test: EXPERIMENTAL_JAVA_LANGUAGE_LEVEL_9=true make droid, flash to device & sanity check Bug: 131683177 Change-Id: I52333f32c88aa85cd3652ad91d50d9927ff61daf
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 && ` +
`cp ${config.JrtFsJar} ${outDir}/lib/`,
CommandDeps: []string{
"${moduleInfoJavaPath}",
"${config.JavacCmd}",
"${config.SoongZipCmd}",
"${config.MergeZipsCmd}",
"${config.JmodCmd}",
"${config.JlinkCmd}",
"${config.JrtFsJar}",
},
},
"classpath", "outDir", "workDir")
)
func TransformJarsToSystemModules(ctx android.ModuleContext, jars android.Paths) (android.Path, android.Paths) {
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"),
}
ctx.Build(pctx, android.BuildParams{
Rule: jarsTosystemModules,
Description: "system modules",
Outputs: outputs,
Inputs: jars,
Args: map[string]string{
"classpath": strings.Join(jars.Strings(), ":"),
"workDir": workDir.String(),
"outDir": outDir.String(),
},
})
return outDir, outputs.Paths()
}
func SystemModulesFactory() android.Module {
module := &SystemModules{}
module.AddProperties(&module.properties)
android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibCommon)
android.InitDefaultableModule(module)
return module
}
type SystemModules struct {
android.ModuleBase
android.DefaultableModuleBase
properties SystemModulesProperties
// 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
outputDir android.Path
outputDeps android.Paths
}
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
ctx.VisitDirectDepsWithTag(libTag, func(module android.Module) {
dep, _ := module.(Dependency)
jars = append(jars, dep.HeaderJars()...)
})
system.headerJars = jars
system.outputDir, system.outputDeps = TransformJarsToSystemModules(ctx, jars)
}
func (system *SystemModules) DepsMutator(ctx android.BottomUpMutatorContext) {
ctx.AddVariationDependencies(nil, libTag, system.properties.Libs...)
}
func (system *SystemModules) AndroidMk() android.AndroidMkData {
return android.AndroidMkData{
Custom: func(w io.Writer, name, prefix, moduleDir string, data android.AndroidMkData) {
fmt.Fprintln(w)
makevar := "SOONG_SYSTEM_MODULES_" + name
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, " "))
fmt.Fprintln(w)
makevar = "SOONG_SYSTEM_MODULES_DEPS_" + name
fmt.Fprintln(w, makevar, ":=$=", strings.Join(system.outputDeps.Strings(), " "))
fmt.Fprintln(w)
fmt.Fprintln(w, name+":", "$("+makevar+")")
fmt.Fprintln(w, ".PHONY:", name)
},
}
}