diff options
Diffstat (limited to 'java/java.go')
| -rw-r--r-- | java/java.go | 414 |
1 files changed, 319 insertions, 95 deletions
diff --git a/java/java.go b/java/java.go index 288e2ebf1..d04968510 100644 --- a/java/java.go +++ b/java/java.go @@ -29,6 +29,7 @@ import ( "github.com/google/blueprint/proptools" "android/soong/android" + "android/soong/dexpreopt" "android/soong/java/config" "android/soong/tradefed" ) @@ -323,6 +324,10 @@ type CompilerDeviceProperties struct { Stem *string IsSDKLibrary bool `blueprint:"mutated"` + + // If true, generate the signature file of APK Signing Scheme V4, along side the signed APK file. + // Defaults to false. + V4_signature *bool } // Functionality common to Module and Import @@ -411,8 +416,8 @@ type Module struct { // manifest file to use instead of properties.Manifest overrideManifest android.OptionalPath - // list of SDK lib names that this java module is exporting - exportedSdkLibs []string + // map of SDK version to class loader context + exportedSdkLibs dexpreopt.ClassLoaderContextMap // list of plugins that this java module is exporting exportedPluginJars android.Paths @@ -436,6 +441,7 @@ type Module struct { hiddenAPI dexer dexpreopter + usesLibrary linter // list of the xref extraction files @@ -445,12 +451,15 @@ type Module struct { // Collect the module directory for IDE info in java/jdeps.go. modulePaths []string + + hideApexVariantFromMake bool } func (j *Module) addHostProperties() { j.AddProperties( &j.properties, &j.protoProperties, + &j.usesLibraryProperties, ) } @@ -488,14 +497,19 @@ type ApexDependency interface { ImplementationAndResourcesJars() android.Paths } +// Provides build path and install path to DEX jars. +type UsesLibraryDependency interface { + DexJarBuildPath() android.Path + DexJarInstallPath() android.Path +} + type Dependency interface { ApexDependency + UsesLibraryDependency ImplementationJars() android.Paths ResourceJars() android.Paths - DexJarBuildPath() android.Path - DexJarInstallPath() android.Path AidlIncludeDirs() android.Paths - ExportedSdkLibs() []string + ExportedSdkLibs() dexpreopt.ClassLoaderContextMap ExportedPlugins() (android.Paths, []string) SrcJarArgs() ([]string, android.Paths) BaseModuleName() string @@ -533,13 +547,28 @@ type dependencyTag struct { name string } -type jniDependencyTag struct { +// installDependencyTag is a dependency tag that is annotated to cause the installed files of the +// dependency to be installed when the parent module is installed. +type installDependencyTag struct { blueprint.BaseDependencyTag + android.InstallAlwaysNeededDependencyTag + name string +} + +type usesLibraryDependencyTag struct { + dependencyTag + sdkVersion int // SDK version in which the library appared as a standalone library. +} + +func makeUsesLibraryDependencyTag(sdkVersion int) usesLibraryDependencyTag { + return usesLibraryDependencyTag{ + dependencyTag: dependencyTag{name: fmt.Sprintf("uses-library-%d", sdkVersion)}, + sdkVersion: sdkVersion, + } } func IsJniDepTag(depTag blueprint.DependencyTag) bool { - _, ok := depTag.(*jniDependencyTag) - return ok + return depTag == jniLibTag } var ( @@ -557,8 +586,14 @@ var ( proguardRaiseTag = dependencyTag{name: "proguard-raise"} certificateTag = dependencyTag{name: "certificate"} instrumentationForTag = dependencyTag{name: "instrumentation_for"} - usesLibTag = dependencyTag{name: "uses-library"} extraLintCheckTag = dependencyTag{name: "extra-lint-check"} + jniLibTag = dependencyTag{name: "jnilib"} + jniInstallTag = installDependencyTag{name: "jni install"} + binaryInstallTag = installDependencyTag{name: "binary install"} + usesLibTag = makeUsesLibraryDependencyTag(dexpreopt.AnySdkVersion) + usesLibCompat28Tag = makeUsesLibraryDependencyTag(28) + usesLibCompat29Tag = makeUsesLibraryDependencyTag(29) + usesLibCompat30Tag = makeUsesLibraryDependencyTag(30) ) func IsLibDepTag(depTag blueprint.DependencyTag) bool { @@ -626,8 +661,9 @@ func (j *Module) shouldInstrumentInApex(ctx android.BaseModuleContext) bool { // Force enable the instrumentation for java code that is built for APEXes ... // except for the jacocoagent itself (because instrumenting jacocoagent using jacocoagent // doesn't make sense) or framework libraries (e.g. libraries found in the InstrumentFrameworkModules list) unless EMMA_INSTRUMENT_FRAMEWORK is true. + apexInfo := ctx.Provider(android.ApexInfoProvider).(android.ApexInfo) isJacocoAgent := ctx.ModuleName() == "jacocoagent" - if android.DirectlyInAnyApex(ctx, ctx.ModuleName()) && !isJacocoAgent && !j.IsForPlatform() { + if j.DirectlyInAnyApex() && !isJacocoAgent && !apexInfo.IsForPlatform() { if !inList(ctx.ModuleName(), config.InstrumentFrameworkModules) { return true } else if ctx.Config().IsEnvTrue("EMMA_INSTRUMENT_FRAMEWORK") { @@ -673,25 +709,29 @@ func (j *Module) AvailableFor(what string) bool { return j.ApexModuleBase.AvailableFor(what) } +func sdkDeps(ctx android.BottomUpMutatorContext, sdkContext sdkContext, d dexer) { + sdkDep := decodeSdkDep(ctx, sdkContext) + if sdkDep.useModule { + ctx.AddVariationDependencies(nil, bootClasspathTag, sdkDep.bootclasspath...) + ctx.AddVariationDependencies(nil, java9LibTag, sdkDep.java9Classpath...) + ctx.AddVariationDependencies(nil, libTag, sdkDep.classpath...) + if d.effectiveOptimizeEnabled() && sdkDep.hasStandardLibs() { + ctx.AddVariationDependencies(nil, proguardRaiseTag, config.LegacyCorePlatformBootclasspathLibraries...) + } + if d.effectiveOptimizeEnabled() && sdkDep.hasFrameworkLibs() { + ctx.AddVariationDependencies(nil, proguardRaiseTag, config.FrameworkLibraries...) + } + } + if sdkDep.systemModules != "" { + ctx.AddVariationDependencies(nil, systemModulesTag, sdkDep.systemModules) + } +} + func (j *Module) deps(ctx android.BottomUpMutatorContext) { if ctx.Device() { j.linter.deps(ctx) - sdkDep := decodeSdkDep(ctx, sdkContext(j)) - if sdkDep.useModule { - ctx.AddVariationDependencies(nil, bootClasspathTag, sdkDep.bootclasspath...) - ctx.AddVariationDependencies(nil, java9LibTag, sdkDep.java9Classpath...) - ctx.AddVariationDependencies(nil, libTag, sdkDep.classpath...) - if j.effectiveOptimizeEnabled() && sdkDep.hasStandardLibs() { - ctx.AddVariationDependencies(nil, proguardRaiseTag, config.LegacyCorePlatformBootclasspathLibraries...) - } - if j.effectiveOptimizeEnabled() && sdkDep.hasFrameworkLibs() { - ctx.AddVariationDependencies(nil, proguardRaiseTag, config.FrameworkLibraries...) - } - } - if sdkDep.systemModules != "" { - ctx.AddVariationDependencies(nil, systemModulesTag, sdkDep.systemModules) - } + sdkDeps(ctx, sdkContext(j), j.dexer) } syspropPublicStubs := syspropPublicStubs(ctx.Config()) @@ -719,9 +759,21 @@ func (j *Module) deps(ctx android.BottomUpMutatorContext) { return ret } - ctx.AddVariationDependencies(nil, libTag, rewriteSyspropLibs(j.properties.Libs, "libs")...) + libDeps := ctx.AddVariationDependencies(nil, libTag, rewriteSyspropLibs(j.properties.Libs, "libs")...) ctx.AddVariationDependencies(nil, staticLibTag, rewriteSyspropLibs(j.properties.Static_libs, "static_libs")...) + // For library dependencies that are component libraries (like stubs), add the implementation + // as a dependency (dexpreopt needs to be against the implementation library, not stubs). + for _, dep := range libDeps { + if dep != nil { + if component, ok := dep.(SdkLibraryComponentDependency); ok { + if lib := component.OptionalSdkLibraryImplementation(); lib != nil { + ctx.AddVariationDependencies(nil, usesLibTag, *lib) + } + } + } + } + ctx.AddFarVariationDependencies(ctx.Config().BuildOSCommonTarget.Variations(), pluginTag, j.properties.Plugins...) ctx.AddFarVariationDependencies(ctx.Config().BuildOSCommonTarget.Variations(), exportedPluginTag, j.properties.Exported_plugins...) @@ -966,17 +1018,11 @@ func (j *Module) collectDeps(ctx android.ModuleContext) deps { } } - // If this is a component library (stubs, etc.) for a java_sdk_library then - // add the name of that java_sdk_library to the exported sdk libs to make sure - // that, if necessary, a <uses-library> element for that java_sdk_library is - // added to the Android manifest. - j.exportedSdkLibs = append(j.exportedSdkLibs, j.OptionalImplicitSdkLibrary()...) - ctx.VisitDirectDeps(func(module android.Module) { otherName := ctx.OtherModuleName(module) tag := ctx.OtherModuleDependencyTag(module) - if _, ok := tag.(*jniDependencyTag); ok { + if IsJniDepTag(tag) { // Handled by AndroidApp.collectAppDeps return } @@ -991,7 +1037,8 @@ func (j *Module) collectDeps(ctx android.ModuleContext) deps { case libTag: deps.classpath = append(deps.classpath, dep.SdkHeaderJars(ctx, j.sdkVersion())...) // names of sdk libs that are directly depended are exported - j.exportedSdkLibs = append(j.exportedSdkLibs, dep.OptionalImplicitSdkLibrary()...) + j.exportedSdkLibs.MaybeAddContext(ctx, dep.OptionalImplicitSdkLibrary(), + dep.DexJarBuildPath(), dep.DexJarInstallPath()) case staticLibTag: ctx.ModuleErrorf("dependency on java_sdk_library %q can only be in libs", otherName) } @@ -1002,7 +1049,7 @@ func (j *Module) collectDeps(ctx android.ModuleContext) deps { case libTag, instrumentationForTag: deps.classpath = append(deps.classpath, dep.HeaderJars()...) // sdk lib names from dependencies are re-exported - j.exportedSdkLibs = append(j.exportedSdkLibs, dep.ExportedSdkLibs()...) + j.exportedSdkLibs.AddContextMap(dep.ExportedSdkLibs(), otherName) deps.aidlIncludeDirs = append(deps.aidlIncludeDirs, dep.AidlIncludeDirs()...) pluginJars, pluginClasses := dep.ExportedPlugins() addPlugins(&deps, pluginJars, pluginClasses...) @@ -1014,7 +1061,7 @@ func (j *Module) collectDeps(ctx android.ModuleContext) deps { deps.staticHeaderJars = append(deps.staticHeaderJars, dep.HeaderJars()...) deps.staticResourceJars = append(deps.staticResourceJars, dep.ResourceJars()...) // sdk lib names from dependencies are re-exported - j.exportedSdkLibs = append(j.exportedSdkLibs, dep.ExportedSdkLibs()...) + j.exportedSdkLibs.AddContextMap(dep.ExportedSdkLibs(), otherName) deps.aidlIncludeDirs = append(deps.aidlIncludeDirs, dep.AidlIncludeDirs()...) pluginJars, pluginClasses := dep.ExportedPlugins() addPlugins(&deps, pluginJars, pluginClasses...) @@ -1077,8 +1124,6 @@ func (j *Module) collectDeps(ctx android.ModuleContext) deps { } }) - j.exportedSdkLibs = android.FirstUniqueStrings(j.exportedSdkLibs) - return deps } @@ -1153,18 +1198,6 @@ func (j *Module) collectBuilderFlags(ctx android.ModuleContext, deps deps) javaB // javaVersion flag. flags.javaVersion = getJavaVersion(ctx, String(j.properties.Java_version), sdkContext(j)) - // javac flags. - javacFlags := j.properties.Javacflags - if flags.javaVersion.usesJavaModules() { - javacFlags = append(javacFlags, j.properties.Openjdk9.Javacflags...) - } - if ctx.Config().MinimizeJavaDebugInfo() && !ctx.Host() { - // For non-host binaries, override the -g flag passed globally to remove - // local variable debug info to reduce disk and memory usage. - javacFlags = append(javacFlags, "-g:source,lines") - } - javacFlags = append(javacFlags, "-Xlint:-dep-ann") - if ctx.Config().RunErrorProne() { if config.ErrorProneClasspath == nil { ctx.ModuleErrorf("cannot build with Error Prone, missing external/error_prone?") @@ -1215,24 +1248,77 @@ func (j *Module) collectBuilderFlags(ctx android.ModuleContext, deps deps) javaB } } - if j.properties.Patch_module != nil && flags.javaVersion.usesJavaModules() { - // Manually specify build directory in case it is not under the repo root. - // (javac doesn't seem to expand into symbolc links when searching for patch-module targets, so - // just adding a symlink under the root doesn't help.) - patchPaths := ".:" + ctx.Config().BuildDir() - classPath := flags.classpath.FormJavaClassPath("") - if classPath != "" { - patchPaths += ":" + classPath - } - javacFlags = append(javacFlags, "--patch-module="+String(j.properties.Patch_module)+"="+patchPaths) - } - // systemModules flags.systemModules = deps.systemModules // aidl flags. flags.aidlFlags, flags.aidlDeps = j.aidlFlags(ctx, deps.aidlPreprocess, deps.aidlIncludeDirs) + return flags +} + +func (j *Module) collectJavacFlags( + ctx android.ModuleContext, flags javaBuilderFlags, srcFiles android.Paths) javaBuilderFlags { + // javac flags. + javacFlags := j.properties.Javacflags + + if ctx.Config().MinimizeJavaDebugInfo() && !ctx.Host() { + // For non-host binaries, override the -g flag passed globally to remove + // local variable debug info to reduce disk and memory usage. + javacFlags = append(javacFlags, "-g:source,lines") + } + javacFlags = append(javacFlags, "-Xlint:-dep-ann") + + if flags.javaVersion.usesJavaModules() { + javacFlags = append(javacFlags, j.properties.Openjdk9.Javacflags...) + + if j.properties.Patch_module != nil { + // Manually specify build directory in case it is not under the repo root. + // (javac doesn't seem to expand into symbolic links when searching for patch-module targets, so + // just adding a symlink under the root doesn't help.) + patchPaths := []string{".", ctx.Config().BuildDir()} + + // b/150878007 + // + // Workaround to support *Bazel-executed* JDK9 javac in Bazel's + // execution root for --patch-module. If this javac command line is + // invoked within Bazel's execution root working directory, the top + // level directories (e.g. libcore/, tools/, frameworks/) are all + // symlinks. JDK9 javac does not traverse into symlinks, which causes + // --patch-module to fail source file lookups when invoked in the + // execution root. + // + // Short of patching javac or enumerating *all* directories as possible + // input dirs, manually add the top level dir of the source files to be + // compiled. + topLevelDirs := map[string]bool{} + for _, srcFilePath := range srcFiles { + srcFileParts := strings.Split(srcFilePath.String(), "/") + // Ignore source files that are already in the top level directory + // as well as generated files in the out directory. The out + // directory may be an absolute path, which means srcFileParts[0] is the + // empty string, so check that as well. Note that "out" in Bazel's execution + // root is *not* a symlink, which doesn't cause problems for --patch-modules + // anyway, so it's fine to not apply this workaround for generated + // source files. + if len(srcFileParts) > 1 && + srcFileParts[0] != "" && + srcFileParts[0] != "out" { + topLevelDirs[srcFileParts[0]] = true + } + } + patchPaths = append(patchPaths, android.SortedStringKeys(topLevelDirs)...) + + classPath := flags.classpath.FormJavaClassPath("") + if classPath != "" { + patchPaths = append(patchPaths, classPath) + } + javacFlags = append( + javacFlags, + "--patch-module="+String(j.properties.Patch_module)+"="+strings.Join(patchPaths, ":")) + } + } + if len(javacFlags) > 0 { // optimization. ctx.Variable(pctx, "javacFlags", strings.Join(javacFlags, " ")) @@ -1263,6 +1349,10 @@ func (j *Module) compile(ctx android.ModuleContext, aaptSrcJar android.Path) { srcFiles = j.genSources(ctx, srcFiles, flags) + // Collect javac flags only after computing the full set of srcFiles to + // ensure that the --patch-module lookup paths are complete. + flags = j.collectJavacFlags(ctx, flags, srcFiles) + srcJars := srcFiles.FilterByExt(".srcjar") srcJars = append(srcJars, deps.srcJars...) if aaptSrcJar != nil { @@ -1485,7 +1575,7 @@ func (j *Module) compile(ctx android.ModuleContext, aaptSrcJar android.Path) { args := map[string]string{ "jarArgs": "-P META-INF/services/ " + strings.Join(proptools.NinjaAndShellEscapeList(zipargs), " "), } - if ctx.Config().IsEnvTrue("RBE_ZIP") { + if ctx.Config().UseRBE() && ctx.Config().IsEnvTrue("RBE_ZIP") { rule = zipRE args["implicits"] = strings.Join(services.Strings(), ",") } @@ -1582,7 +1672,8 @@ func (j *Module) compile(ctx android.ModuleContext, aaptSrcJar android.Path) { j.implementationAndResourcesJar = implementationAndResourcesJar // Enable dex compilation for the APEX variants, unless it is disabled explicitly - if android.DirectlyInAnyApex(ctx, ctx.ModuleName()) && !j.IsForPlatform() { + apexInfo := ctx.Provider(android.ApexInfoProvider).(android.ApexInfo) + if j.DirectlyInAnyApex() && !apexInfo.IsForPlatform() { if j.dexProperties.Compile_dex == nil { j.dexProperties.Compile_dex = proptools.BoolPtr(true) } @@ -1606,6 +1697,9 @@ func (j *Module) compile(ctx android.ModuleContext, aaptSrcJar android.Path) { configurationName := j.ConfigurationName() primary := configurationName == ctx.ModuleName() + // If the prebuilt is being used rather than the from source, skip this + // module to prevent duplicated classes + primary = primary && !j.IsReplacedByPrebuilt() // Hidden API CSV generation and dex encoding dexOutputFile = j.hiddenAPI.hiddenAPI(ctx, configurationName, primary, dexOutputFile, j.implementationJarFile, @@ -1647,7 +1741,7 @@ func (j *Module) compile(ctx android.ModuleContext, aaptSrcJar android.Path) { if v := sdkSpec.version; v.isNumbered() { return v.String() } else { - return ctx.Config().DefaultAppTargetSdk() + return ctx.Config().DefaultAppTargetSdk(ctx).String() } } @@ -1661,7 +1755,7 @@ func (j *Module) compile(ctx android.ModuleContext, aaptSrcJar android.Path) { j.linter.compileSdkVersion = lintSDKVersionString(j.sdkVersion()) j.linter.javaLanguageLevel = flags.javaVersion.String() j.linter.kotlinLanguageLevel = "1.3" - if j.ApexVariationName() != "" && ctx.Config().UnbundledBuildApps() { + if !apexInfo.IsForPlatform() && ctx.Config().UnbundledBuildApps() { j.linter.buildModuleReportZip = true } j.linter.lint(ctx) @@ -1819,8 +1913,7 @@ func (j *Module) AidlIncludeDirs() android.Paths { return j.exportAidlIncludeDirs } -func (j *Module) ExportedSdkLibs() []string { - // exportedSdkLibs is type []string +func (j *Module) ExportedSdkLibs() dexpreopt.ClassLoaderContextMap { return j.exportedSdkLibs } @@ -1866,7 +1959,8 @@ func (j *Module) DepIsInSameApex(ctx android.BaseModuleContext, dep android.Modu return j.depIsInSameApex(ctx, dep) } -func (j *Module) ShouldSupportSdkVersion(ctx android.BaseModuleContext, sdkVersion int) error { +func (j *Module) ShouldSupportSdkVersion(ctx android.BaseModuleContext, + sdkVersion android.ApiLevel) error { sdkSpec := j.minSdkVersion() if !sdkSpec.specified() { return fmt.Errorf("min_sdk_version is not specified") @@ -1878,7 +1972,7 @@ func (j *Module) ShouldSupportSdkVersion(ctx android.BaseModuleContext, sdkVersi if err != nil { return err } - if int(ver) > sdkVersion { + if ver.ApiLevel(ctx).GreaterThan(sdkVersion) { return fmt.Errorf("newer SDK(%v)", ver) } return nil @@ -1923,7 +2017,7 @@ func (j *Library) PermittedPackagesForUpdatableBootJars() []string { func shouldUncompressDex(ctx android.ModuleContext, dexpreopter *dexpreopter) bool { // Store uncompressed (and aligned) any dex files from jars in APEXes. - if am, ok := ctx.Module().(android.ApexModule); ok && !am.IsForPlatform() { + if apexInfo := ctx.Provider(android.ApexInfoProvider).(android.ApexInfo); !apexInfo.IsForPlatform() { return true } @@ -1945,6 +2039,11 @@ func shouldUncompressDex(ctx android.ModuleContext, dexpreopter *dexpreopter) bo } func (j *Library) GenerateAndroidBuildActions(ctx android.ModuleContext) { + apexInfo := ctx.Provider(android.ApexInfoProvider).(android.ApexInfo) + if !apexInfo.IsForPlatform() { + j.hideApexVariantFromMake = true + } + j.checkSdkVersions(ctx) j.dexpreopter.installPath = android.PathForModuleInstall(ctx, "framework", j.Stem()+".jar") j.dexpreopter.isSDKLibrary = j.deviceProperties.IsSDKLibrary @@ -1953,12 +2052,13 @@ func (j *Library) GenerateAndroidBuildActions(ctx android.ModuleContext) { j.dexProperties.Uncompress_dex = proptools.BoolPtr(shouldUncompressDex(ctx, &j.dexpreopter)) } j.dexpreopter.uncompressedDex = *j.dexProperties.Uncompress_dex + j.exportedSdkLibs = make(dexpreopt.ClassLoaderContextMap) j.compile(ctx, nil) // Collect the module directory for IDE info in java/jdeps.go. j.modulePaths = append(j.modulePaths, ctx.ModuleDir()) - exclusivelyForApex := android.InAnyApex(ctx.ModuleName()) && !j.IsForPlatform() + exclusivelyForApex := !apexInfo.IsForPlatform() if (Bool(j.properties.Installable) || ctx.Host()) && !exclusivelyForApex { var extraInstallDeps android.Paths if j.InstallMixin != nil { @@ -1968,6 +2068,18 @@ func (j *Library) GenerateAndroidBuildActions(ctx android.ModuleContext) { j.Stem()+".jar", j.outputFile, extraInstallDeps...) } + // If this is a component library (stubs, etc.) for a java_sdk_library then + // add the name of that java_sdk_library to the exported sdk libs to make sure + // that, if necessary, a <uses-library> element for that java_sdk_library is + // added to the Android manifest. + j.exportedSdkLibs.MaybeAddContext(ctx, j.OptionalImplicitSdkLibrary(), + j.DexJarBuildPath(), j.DexJarInstallPath()) + + // A non-SDK library may provide a <uses-library> (the name may be different from the module name). + if lib := proptools.String(j.usesLibraryProperties.Provides_uses_lib); lib != "" { + j.exportedSdkLibs.AddContext(ctx, lib, j.DexJarBuildPath(), j.DexJarInstallPath()) + } + j.distFiles = j.GenerateTaggedDistFiles(ctx) } @@ -2120,6 +2232,12 @@ func LibraryHostFactory() android.Module { // Java Tests // +// Test option struct. +type TestOptions struct { + // a list of extra test configuration files that should be installed with the module. + Extra_test_configs []string `android:"path,arch_variant"` +} + type testProperties struct { // list of compatibility suites (for example "cts", "vts") that the module should be // installed into. @@ -2145,6 +2263,9 @@ type testProperties struct { // Add parameterized mainline modules to auto generated test config. The options will be // handled by TradeFed to do downloading and installing the specified modules on the device. Test_mainline_modules []string + + // Test options. + Test_options TestOptions } type hostTestProperties struct { @@ -2173,8 +2294,9 @@ type Test struct { testProperties testProperties - testConfig android.Path - data android.Paths + testConfig android.Path + extraTestConfigs android.Paths + data android.Paths } type TestHost struct { @@ -2195,6 +2317,7 @@ type JavaTestImport struct { prebuiltTestProperties prebuiltTestProperties testConfig android.Path + dexJarFile android.Path } func (j *TestHost) DepsMutator(ctx android.BottomUpMutatorContext) { @@ -2213,6 +2336,8 @@ func (j *Test) GenerateAndroidBuildActions(ctx android.ModuleContext) { j.data = android.PathsForModuleSrc(ctx, j.testProperties.Data) + j.extraTestConfigs = android.PathsForModuleSrc(ctx, j.testProperties.Test_options.Extra_test_configs) + ctx.VisitDirectDepsWithTag(dataNativeBinsTag, func(dep android.Module) { j.data = append(j.data, android.OutputFileForModule(ctx, dep, "")) }) @@ -2379,6 +2504,10 @@ type binaryProperties struct { // Name of the class containing main to be inserted into the manifest as Main-Class. Main_class *string + + // Names of modules containing JNI libraries that should be installed alongside the host + // variant of the binary. + Jni_libs []string } type Binary struct { @@ -2419,18 +2548,24 @@ func (j *Binary) GenerateAndroidBuildActions(ctx android.ModuleContext) { j.wrapperFile = android.PathForSource(ctx, "build/soong/scripts/jar-wrapper.sh") } - // Depend on the installed jar so that the wrapper doesn't get executed by - // another build rule before the jar has been installed. - jarFile := ctx.PrimaryModule().(*Binary).installFile - + // The host installation rules make the installed wrapper depend on all the dependencies + // of the wrapper variant, which will include the common variant's jar file and any JNI + // libraries. This is verified by TestBinary. j.binaryFile = ctx.InstallExecutable(android.PathForModuleInstall(ctx, "bin"), - ctx.ModuleName(), j.wrapperFile, jarFile) + ctx.ModuleName(), j.wrapperFile) } } func (j *Binary) DepsMutator(ctx android.BottomUpMutatorContext) { if ctx.Arch().ArchType == android.Common { j.deps(ctx) + } else { + // These dependencies ensure the host installation rules will install the jar file and + // the jni libraries when the wrapper is installed. + ctx.AddVariationDependencies(nil, jniInstallTag, j.binaryProperties.Jni_libs...) + ctx.AddVariationDependencies( + []blueprint.Variation{{Mutator: "arch", Variation: android.CommonArch.String()}}, + binaryInstallTag, ctx.ModuleName()) } } @@ -2515,21 +2650,41 @@ type Import struct { // Functionality common to Module and Import. embeddableInModuleAndImport + hiddenAPI + dexer + properties ImportProperties + // output file containing classes.dex and resources + dexJarFile android.Path + combinedClasspathFile android.Path - exportedSdkLibs []string + exportedSdkLibs dexpreopt.ClassLoaderContextMap exportAidlIncludeDirs android.Paths + + hideApexVariantFromMake bool } func (j *Import) sdkVersion() sdkSpec { return sdkSpecFrom(String(j.properties.Sdk_version)) } +func (j *Import) makeSdkVersion() string { + return j.sdkVersion().raw +} + +func (j *Import) systemModules() string { + return "none" +} + func (j *Import) minSdkVersion() sdkSpec { return j.sdkVersion() } +func (j *Import) targetSdkVersion() sdkSpec { + return j.sdkVersion() +} + func (j *Import) MinSdkVersion() string { return j.minSdkVersion().version.String() } @@ -2556,9 +2711,17 @@ func (a *Import) JacocoReportClassesFile() android.Path { func (j *Import) DepsMutator(ctx android.BottomUpMutatorContext) { ctx.AddVariationDependencies(nil, libTag, j.properties.Libs...) + + if ctx.Device() && Bool(j.dexProperties.Compile_dex) { + sdkDeps(ctx, sdkContext(j), j.dexer) + } } func (j *Import) GenerateAndroidBuildActions(ctx android.ModuleContext) { + if !ctx.Provider(android.ApexInfoProvider).(android.ApexInfo).IsForPlatform() { + j.hideApexVariantFromMake = true + } + jars := android.PathsForModuleSrc(ctx, j.properties.Jars) jarName := j.Stem() + ".jar" @@ -2571,12 +2734,9 @@ func (j *Import) GenerateAndroidBuildActions(ctx android.ModuleContext) { TransformJetifier(ctx, outputFile, inputFile) } j.combinedClasspathFile = outputFile + j.exportedSdkLibs = make(dexpreopt.ClassLoaderContextMap) - // If this is a component library (impl, stubs, etc.) for a java_sdk_library then - // add the name of that java_sdk_library to the exported sdk libs to make sure - // that, if necessary, a <uses-library> element for that java_sdk_library is - // added to the Android manifest. - j.exportedSdkLibs = append(j.exportedSdkLibs, j.OptionalImplicitSdkLibrary()...) + var flags javaBuilderFlags ctx.VisitDirectDeps(func(module android.Module) { otherName := ctx.OtherModuleName(module) @@ -2586,27 +2746,77 @@ func (j *Import) GenerateAndroidBuildActions(ctx android.ModuleContext) { case Dependency: switch tag { case libTag, staticLibTag: + flags.classpath = append(flags.classpath, dep.HeaderJars()...) // sdk lib names from dependencies are re-exported - j.exportedSdkLibs = append(j.exportedSdkLibs, dep.ExportedSdkLibs()...) + j.exportedSdkLibs.AddContextMap(dep.ExportedSdkLibs(), otherName) + case bootClasspathTag: + flags.bootClasspath = append(flags.bootClasspath, dep.HeaderJars()...) } case SdkLibraryDependency: switch tag { case libTag: + flags.classpath = append(flags.classpath, dep.SdkHeaderJars(ctx, j.sdkVersion())...) // names of sdk libs that are directly depended are exported - j.exportedSdkLibs = append(j.exportedSdkLibs, otherName) + j.exportedSdkLibs.AddContext(ctx, otherName, + dep.DexJarBuildPath(), dep.DexJarInstallPath()) } } }) - j.exportedSdkLibs = android.FirstUniqueStrings(j.exportedSdkLibs) + var installFile android.Path if Bool(j.properties.Installable) { - ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"), + installFile = ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"), jarName, outputFile) } + // If this is a component library (impl, stubs, etc.) for a java_sdk_library then + // add the name of that java_sdk_library to the exported sdk libs to make sure + // that, if necessary, a <uses-library> element for that java_sdk_library is + // added to the Android manifest. + j.exportedSdkLibs.MaybeAddContext(ctx, j.OptionalImplicitSdkLibrary(), + outputFile, installFile) + j.exportAidlIncludeDirs = android.PathsForModuleSrc(ctx, j.properties.Aidl.Export_include_dirs) + + if ctx.Device() && Bool(j.dexProperties.Compile_dex) { + sdkDep := decodeSdkDep(ctx, sdkContext(j)) + if sdkDep.invalidVersion { + ctx.AddMissingDependencies(sdkDep.bootclasspath) + ctx.AddMissingDependencies(sdkDep.java9Classpath) + } else if sdkDep.useFiles { + // sdkDep.jar is actually equivalent to turbine header.jar. + flags.classpath = append(flags.classpath, sdkDep.jars...) + } + + // Dex compilation + var dexOutputFile android.ModuleOutPath + dexOutputFile = j.dexer.compileDex(ctx, flags, j.minSdkVersion(), outputFile, jarName) + if ctx.Failed() { + return + } + + configurationName := j.BaseModuleName() + primary := j.Prebuilt().UsePrebuilt() + + // Hidden API CSV generation and dex encoding + dexOutputFile = j.hiddenAPI.hiddenAPI(ctx, configurationName, primary, dexOutputFile, outputFile, + proptools.Bool(j.dexProperties.Uncompress_dex)) + + j.dexJarFile = dexOutputFile + } +} + +func (j *Import) OutputFiles(tag string) (android.Paths, error) { + switch tag { + case "", ".jar": + return android.Paths{j.combinedClasspathFile}, nil + default: + return nil, fmt.Errorf("unsupported module reference tag %q", tag) + } } +var _ android.OutputFileProducer = (*Import)(nil) + var _ Dependency = (*Import)(nil) func (j *Import) HeaderJars() android.Paths { @@ -2635,7 +2845,7 @@ func (j *Import) ImplementationAndResourcesJars() android.Paths { } func (j *Import) DexJarBuildPath() android.Path { - return nil + return j.dexJarFile } func (j *Import) DexJarInstallPath() android.Path { @@ -2646,7 +2856,7 @@ func (j *Import) AidlIncludeDirs() android.Paths { return j.exportAidlIncludeDirs } -func (j *Import) ExportedSdkLibs() []string { +func (j *Import) ExportedSdkLibs() dexpreopt.ClassLoaderContextMap { return j.exportedSdkLibs } @@ -2662,7 +2872,8 @@ func (j *Import) DepIsInSameApex(ctx android.BaseModuleContext, dep android.Modu return j.depIsInSameApex(ctx, dep) } -func (j *Import) ShouldSupportSdkVersion(ctx android.BaseModuleContext, sdkVersion int) error { +func (j *Import) ShouldSupportSdkVersion(ctx android.BaseModuleContext, + sdkVersion android.ApiLevel) error { // Do not check for prebuilts against the min_sdk_version of enclosing APEX return nil } @@ -2703,10 +2914,15 @@ var _ android.PrebuiltInterface = (*Import)(nil) func ImportFactory() android.Module { module := &Import{} - module.AddProperties(&module.properties) + module.AddProperties( + &module.properties, + &module.dexer.dexProperties, + ) module.initModuleAndImport(&module.ModuleBase) + module.dexProperties.Optimize.EnabledByDefault = false + android.InitPrebuiltModule(module, &module.properties.Jars) android.InitApexModule(module) android.InitSdkAwareModule(module) @@ -2751,6 +2967,8 @@ type DexImport struct { maybeStrippedDexJarFile android.Path dexpreopter + + hideApexVariantFromMake bool } func (j *DexImport) Prebuilt() *android.Prebuilt { @@ -2786,6 +3004,11 @@ func (j *DexImport) GenerateAndroidBuildActions(ctx android.ModuleContext) { ctx.PropertyErrorf("jars", "exactly one jar must be provided") } + apexInfo := ctx.Provider(android.ApexInfoProvider).(android.ApexInfo) + if !apexInfo.IsForPlatform() { + j.hideApexVariantFromMake = true + } + j.dexpreopter.installPath = android.PathForModuleInstall(ctx, "framework", j.Stem()+".jar") j.dexpreopter.uncompressedDex = shouldUncompressDex(ctx, &j.dexpreopter) @@ -2830,7 +3053,7 @@ func (j *DexImport) GenerateAndroidBuildActions(ctx android.ModuleContext) { j.maybeStrippedDexJarFile = dexOutputFile - if j.IsForPlatform() { + if apexInfo.IsForPlatform() { ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"), j.Stem()+".jar", dexOutputFile) } @@ -2840,7 +3063,8 @@ func (j *DexImport) DexJarBuildPath() android.Path { return j.dexJarFile } -func (j *DexImport) ShouldSupportSdkVersion(ctx android.BaseModuleContext, sdkVersion int) error { +func (j *DexImport) ShouldSupportSdkVersion(ctx android.BaseModuleContext, + sdkVersion android.ApiLevel) error { // we don't check prebuilt modules for sdk_version return nil } |