diff options
Diffstat (limited to 'java/java.go')
-rw-r--r-- | java/java.go | 2446 |
1 files changed, 692 insertions, 1754 deletions
diff --git a/java/java.go b/java/java.go index 69826eec3..bbed42def 100644 --- a/java/java.go +++ b/java/java.go @@ -21,46 +21,25 @@ package java import ( "fmt" "path/filepath" - "strconv" "strings" "github.com/google/blueprint" - "github.com/google/blueprint/pathtools" "github.com/google/blueprint/proptools" "android/soong/android" + "android/soong/cc" + "android/soong/dexpreopt" "android/soong/java/config" "android/soong/tradefed" ) func init() { - RegisterJavaBuildComponents(android.InitRegistrationContext) + registerJavaBuildComponents(android.InitRegistrationContext) - // Register sdk member types. - android.RegisterSdkMemberType(javaHeaderLibsSdkMemberType) - - android.RegisterSdkMemberType(&librarySdkMemberType{ - android.SdkMemberTypeBase{ - PropertyName: "java_libs", - }, - func(j *Library) android.Path { - implementationJars := j.ImplementationJars() - if len(implementationJars) != 1 { - panic(fmt.Errorf("there must be only one implementation jar from %q", j.Name())) - } - - return implementationJars[0] - }, - }) - - android.RegisterSdkMemberType(&testSdkMemberType{ - SdkMemberTypeBase: android.SdkMemberTypeBase{ - PropertyName: "java_tests", - }, - }) + RegisterJavaSdkMemberTypes() } -func RegisterJavaBuildComponents(ctx android.RegistrationContext) { +func registerJavaBuildComponents(ctx android.RegistrationContext) { ctx.RegisterModuleType("java_defaults", DefaultsFactory) ctx.RegisterModuleType("java_library", LibraryFactory) @@ -78,6 +57,10 @@ func RegisterJavaBuildComponents(ctx android.RegistrationContext) { ctx.RegisterModuleType("java_host_for_device", HostForDeviceFactory) ctx.RegisterModuleType("dex_import", DexImportFactory) + // This mutator registers dependencies on dex2oat for modules that should be + // dexpreopted. This is done late when the final variants have been + // established, to not get the dependencies split into the wrong variants and + // to support the checks in dexpreoptDisabled(). ctx.FinalDepsMutators(func(ctx android.RegisterMutatorsContext) { ctx.BottomUp("dexpreopt_tool_deps", dexpreoptToolDepsMutator).Parallel() }) @@ -86,442 +69,140 @@ func RegisterJavaBuildComponents(ctx android.RegistrationContext) { ctx.RegisterSingletonType("kythe_java_extract", kytheExtractJavaFactory) } -func (j *Module) CheckStableSdkVersion() error { - sdkVersion := j.sdkVersion() - if sdkVersion.stable() { - return nil - } - return fmt.Errorf("non stable SDK %v", sdkVersion) +func RegisterJavaSdkMemberTypes() { + // Register sdk member types. + android.RegisterSdkMemberType(javaHeaderLibsSdkMemberType) + android.RegisterSdkMemberType(javaLibsSdkMemberType) + android.RegisterSdkMemberType(javaBootLibsSdkMemberType) + android.RegisterSdkMemberType(javaTestSdkMemberType) } -func (j *Module) checkSdkVersions(ctx android.ModuleContext) { - if j.RequiresStableAPIs(ctx) { - if sc, ok := ctx.Module().(sdkContext); ok { - if !sc.sdkVersion().specified() { - ctx.PropertyErrorf("sdk_version", - "sdk_version must have a value when the module is located at vendor or product(only if PRODUCT_ENFORCE_PRODUCT_PARTITION_INTERFACE is set).") - } - } - } - - ctx.VisitDirectDeps(func(module android.Module) { - tag := ctx.OtherModuleDependencyTag(module) - switch module.(type) { - // TODO(satayev): cover other types as well, e.g. imports - case *Library, *AndroidLibrary: - switch tag { - case bootClasspathTag, libTag, staticLibTag, java9LibTag: - checkLinkType(ctx, j, module.(linkTypeContext), tag.(dependencyTag)) +var ( + // Supports adding java header libraries to module_exports and sdk. + javaHeaderLibsSdkMemberType = &librarySdkMemberType{ + android.SdkMemberTypeBase{ + PropertyName: "java_header_libs", + SupportsSdk: true, + }, + func(_ android.SdkMemberContext, j *Library) android.Path { + headerJars := j.HeaderJars() + if len(headerJars) != 1 { + panic(fmt.Errorf("there must be only one header jar from %q", j.Name())) } - } - }) -} - -func (j *Module) checkPlatformAPI(ctx android.ModuleContext) { - if sc, ok := ctx.Module().(sdkContext); ok { - usePlatformAPI := proptools.Bool(j.deviceProperties.Platform_apis) - sdkVersionSpecified := sc.sdkVersion().specified() - if usePlatformAPI && sdkVersionSpecified { - ctx.PropertyErrorf("platform_apis", "platform_apis must be false when sdk_version is not empty.") - } else if !usePlatformAPI && !sdkVersionSpecified { - ctx.PropertyErrorf("platform_apis", "platform_apis must be true when sdk_version is empty.") - } - - } -} - -// TODO: -// Autogenerated files: -// Renderscript -// Post-jar passes: -// Proguard -// Rmtypedefs -// DroidDoc -// Findbugs - -type CompilerProperties struct { - // list of source files used to compile the Java module. May be .java, .logtags, .proto, - // or .aidl files. - Srcs []string `android:"path,arch_variant"` - - // list of source files that should not be used to build the Java module. - // This is most useful in the arch/multilib variants to remove non-common files - Exclude_srcs []string `android:"path,arch_variant"` - - // list of directories containing Java resources - Java_resource_dirs []string `android:"arch_variant"` - - // list of directories that should be excluded from java_resource_dirs - Exclude_java_resource_dirs []string `android:"arch_variant"` - - // list of files to use as Java resources - Java_resources []string `android:"path,arch_variant"` - - // list of files that should be excluded from java_resources and java_resource_dirs - Exclude_java_resources []string `android:"path,arch_variant"` - - // list of module-specific flags that will be used for javac compiles - Javacflags []string `android:"arch_variant"` - - // list of module-specific flags that will be used for kotlinc compiles - Kotlincflags []string `android:"arch_variant"` - - // list of of java libraries that will be in the classpath - Libs []string `android:"arch_variant"` - - // list of java libraries that will be compiled into the resulting jar - Static_libs []string `android:"arch_variant"` - // manifest file to be included in resulting jar - Manifest *string `android:"path"` - - // if not blank, run jarjar using the specified rules file - Jarjar_rules *string `android:"path,arch_variant"` - - // If not blank, set the java version passed to javac as -source and -target - Java_version *string - - // If set to true, allow this module to be dexed and installed on devices. Has no - // effect on host modules, which are always considered installable. - Installable *bool - - // If set to true, include sources used to compile the module in to the final jar - Include_srcs *bool - - // If not empty, classes are restricted to the specified packages and their sub-packages. - // This restriction is checked after applying jarjar rules and including static libs. - Permitted_packages []string - - // List of modules to use as annotation processors - Plugins []string - - // List of modules to export to libraries that directly depend on this library as annotation processors - Exported_plugins []string - - // The number of Java source entries each Javac instance can process - Javac_shard_size *int64 - - // Add host jdk tools.jar to bootclasspath - Use_tools_jar *bool - - Openjdk9 struct { - // List of source files that should only be used when passing -source 1.9 or higher - Srcs []string `android:"path"` - - // List of javac flags that should only be used when passing -source 1.9 or higher - Javacflags []string - } - - // When compiling language level 9+ .java code in packages that are part of - // a system module, patch_module names the module that your sources and - // dependencies should be patched into. The Android runtime currently - // doesn't implement the JEP 261 module system so this option is only - // supported at compile time. It should only be needed to compile tests in - // packages that exist in libcore and which are inconvenient to move - // elsewhere. - Patch_module *string `android:"arch_variant"` - - Jacoco struct { - // List of classes to include for instrumentation with jacoco to collect coverage - // information at runtime when building with coverage enabled. If unset defaults to all - // classes. - // Supports '*' as the last character of an entry in the list as a wildcard match. - // If preceded by '.' it matches all classes in the package and subpackages, otherwise - // it matches classes in the package that have the class name as a prefix. - Include_filter []string - - // List of classes to exclude from instrumentation with jacoco to collect coverage - // information at runtime when building with coverage enabled. Overrides classes selected - // by the include_filter property. - // Supports '*' as the last character of an entry in the list as a wildcard match. - // If preceded by '.' it matches all classes in the package and subpackages, otherwise - // it matches classes in the package that have the class name as a prefix. - Exclude_filter []string - } - - Errorprone struct { - // List of javac flags that should only be used when running errorprone. - Javacflags []string - } - - Proto struct { - // List of extra options that will be passed to the proto generator. - Output_params []string - } - - Instrument bool `blueprint:"mutated"` - - // List of files to include in the META-INF/services folder of the resulting jar. - Services []string `android:"path,arch_variant"` - - // If true, package the kotlin stdlib into the jar. Defaults to true. - Static_kotlin_stdlib *bool `android:"arch_variant"` -} - -type CompilerDeviceProperties struct { - // list of module-specific flags that will be used for dex compiles - Dxflags []string `android:"arch_variant"` - - // if not blank, set to the version of the sdk to compile against. - // Defaults to compiling against the current platform. - Sdk_version *string - - // if not blank, set the minimum version of the sdk that the compiled artifacts will run against. - // Defaults to sdk_version if not set. - Min_sdk_version *string - - // if not blank, set the targetSdkVersion in the AndroidManifest.xml. - // Defaults to sdk_version if not set. - Target_sdk_version *string - - // Whether to compile against the platform APIs instead of an SDK. - // If true, then sdk_version must be empty. The value of this field - // is ignored when module's type isn't android_app. - Platform_apis *bool - - Aidl struct { - // Top level directories to pass to aidl tool - Include_dirs []string - - // Directories rooted at the Android.bp file to pass to aidl tool - Local_include_dirs []string - - // directories that should be added as include directories for any aidl sources of modules - // that depend on this module, as well as to aidl for this module. - Export_include_dirs []string - - // whether to generate traces (for systrace) for this interface - Generate_traces *bool - - // whether to generate Binder#GetTransaction name method. - Generate_get_transaction_name *bool + return headerJars[0] + }, + sdkSnapshotFilePathForJar, + copyEverythingToSnapshot, } - // If true, export a copy of the module as a -hostdex module for host testing. - Hostdex *bool - - Target struct { - Hostdex struct { - // Additional required dependencies to add to -hostdex modules. - Required []string + // Export implementation classes jar as part of the sdk. + exportImplementationClassesJar = func(_ android.SdkMemberContext, j *Library) android.Path { + implementationJars := j.ImplementationAndResourcesJars() + if len(implementationJars) != 1 { + panic(fmt.Errorf("there must be only one implementation jar from %q", j.Name())) } + return implementationJars[0] } - // If set to true, compile dex regardless of installable. Defaults to false. - Compile_dex *bool - - Optimize struct { - // If false, disable all optimization. Defaults to true for android_app and android_test - // modules, false for java_library and java_test modules. - Enabled *bool - // True if the module containing this has it set by default. - EnabledByDefault bool `blueprint:"mutated"` - - // If true, optimize for size by removing unused code. Defaults to true for apps, - // false for libraries and tests. - Shrink *bool - - // If true, optimize bytecode. Defaults to false. - Optimize *bool - - // If true, obfuscate bytecode. Defaults to false. - Obfuscate *bool - - // If true, do not use the flag files generated by aapt that automatically keep - // classes referenced by the app manifest. Defaults to false. - No_aapt_flags *bool - - // Flags to pass to proguard. - Proguard_flags []string - - // Specifies the locations of files containing proguard flags. - Proguard_flags_files []string `android:"path"` + // Supports adding java implementation libraries to module_exports but not sdk. + javaLibsSdkMemberType = &librarySdkMemberType{ + android.SdkMemberTypeBase{ + PropertyName: "java_libs", + }, + exportImplementationClassesJar, + sdkSnapshotFilePathForJar, + copyEverythingToSnapshot, } - // When targeting 1.9 and above, override the modules to use with --system, - // otherwise provides defaults libraries to add to the bootclasspath. - System_modules *string - - // The name of the module as used in build configuration. + // Supports adding java boot libraries to module_exports and sdk. // - // Allows a library to separate its actual name from the name used in - // build configuration, e.g.ctx.Config().BootJars(). - ConfigurationName *string `blueprint:"mutated"` - - // set the name of the output - Stem *string - - // Keep the data uncompressed. We always need uncompressed dex for execution, - // so this might actually save space by avoiding storing the same data twice. - // This defaults to reasonable value based on module and should not be set. - // It exists only to support ART tests. - Uncompress_dex *bool - - 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 -} - -func (me *CompilerDeviceProperties) EffectiveOptimizeEnabled() bool { - return BoolDefault(me.Optimize.Enabled, me.Optimize.EnabledByDefault) -} - -// Functionality common to Module and Import -// -// It is embedded in Module so its functionality can be used by methods in Module -// but it is currently only initialized by Import and Library. -type embeddableInModuleAndImport struct { - - // Functionality related to this being used as a component of a java_sdk_library. - EmbeddableSdkLibraryComponent -} - -func (e *embeddableInModuleAndImport) initModuleAndImport(moduleBase *android.ModuleBase) { - e.initSdkLibraryComponent(moduleBase) -} - -// Module/Import's DepIsInSameApex(...) delegates to this method. -// -// This cannot implement DepIsInSameApex(...) directly as that leads to ambiguity with -// the one provided by ApexModuleBase. -func (e *embeddableInModuleAndImport) depIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool { - // dependencies other than the static linkage are all considered crossing APEX boundary - if staticLibTag == ctx.OtherModuleDependencyTag(dep) { - return true + // The build has some implicit dependencies (via the boot jars configuration) on a number of + // modules, e.g. core-oj, apache-xml, that are part of the java boot class path and which are + // provided by mainline modules (e.g. art, conscrypt, runtime-i18n) but which are not otherwise + // used outside those mainline modules. + // + // As they are not needed outside the mainline modules adding them to the sdk/module-exports as + // either java_libs, or java_header_libs would end up exporting more information than was strictly + // necessary. The java_boot_libs property to allow those modules to be exported as part of the + // sdk/module_exports without exposing any unnecessary information. + javaBootLibsSdkMemberType = &librarySdkMemberType{ + android.SdkMemberTypeBase{ + PropertyName: "java_boot_libs", + SupportsSdk: true, + }, + // Temporarily export implementation classes jar for java_boot_libs as it is required for the + // hiddenapi processing. + // TODO(b/179354495): Revert once hiddenapi processing has been modularized. + exportImplementationClassesJar, + sdkSnapshotFilePathForJar, + onlyCopyJarToSnapshot, } - return false -} - -// Module contains the properties and members used by all java module types -type Module struct { - android.ModuleBase - android.DefaultableModuleBase - android.ApexModuleBase - android.SdkBase - - // Functionality common to Module and Import. - embeddableInModuleAndImport - - properties CompilerProperties - protoProperties android.ProtoProperties - deviceProperties CompilerDeviceProperties - - // jar file containing header classes including static library dependencies, suitable for - // inserting into the bootclasspath/classpath of another compile - headerJarFile android.Path - - // jar file containing implementation classes including static library dependencies but no - // resources - implementationJarFile android.Path - - // jar file containing only resources including from static library dependencies - resourceJar android.Path - - // args and dependencies to package source files into a srcjar - srcJarArgs []string - srcJarDeps android.Paths - - // jar file containing implementation classes and resources including static library - // dependencies - implementationAndResourcesJar android.Path - - // output file containing classes.dex and resources - dexJarFile android.Path - - // output file that contains classes.dex if it should be in the output file - maybeStrippedDexJarFile android.Path - // output file containing uninstrumented classes that will be instrumented by jacoco - jacocoReportClassesFile android.Path - - // output file containing mapping of obfuscated names - proguardDictionary android.Path - - // output file of the module, which may be a classes jar or a dex jar - outputFile android.Path - extraOutputFiles android.Paths - - exportAidlIncludeDirs android.Paths - - logtagsSrcs android.Paths - - // installed file for binary dependency - installFile android.Path - - // list of .java files and srcjars that was passed to javac - compiledJavaSrcs android.Paths - compiledSrcJars android.Paths - - // list of extra progurad flag files - extraProguardFlagFiles android.Paths + // Supports adding java test libraries to module_exports but not sdk. + javaTestSdkMemberType = &testSdkMemberType{ + SdkMemberTypeBase: android.SdkMemberTypeBase{ + PropertyName: "java_tests", + }, + } +) - // manifest file to use instead of properties.Manifest - overrideManifest android.OptionalPath +// JavaInfo contains information about a java module for use by modules that depend on it. +type JavaInfo struct { + // HeaderJars is a list of jars that can be passed as the javac classpath in order to link + // against this module. If empty, ImplementationJars should be used instead. + HeaderJars android.Paths - // list of SDK lib names that this java module is exporting - exportedSdkLibs []string + // ImplementationAndResourceJars is a list of jars that contain the implementations of classes + // in the module as well as any resources included in the module. + ImplementationAndResourcesJars android.Paths - // list of plugins that this java module is exporting - exportedPluginJars android.Paths + // ImplementationJars is a list of jars that contain the implementations of classes in the + //module. + ImplementationJars android.Paths - // list of plugins that this java module is exporting - exportedPluginClasses []string + // ResourceJars is a list of jars that contain the resources included in the module. + ResourceJars android.Paths - // list of source files, collected from srcFiles with unique java and all kt files, - // will be used by android.IDEInfo struct - expandIDEInfoCompiledSrcs []string + // AidlIncludeDirs is a list of directories that should be passed to the aidl tool when + // depending on this module. + AidlIncludeDirs android.Paths - // expanded Jarjar_rules - expandJarjarRules android.Path + // SrcJarArgs is a list of arguments to pass to soong_zip to package the sources of this + // module. + SrcJarArgs []string - // list of additional targets for checkbuild - additionalCheckedModules android.Paths + // SrcJarDeps is a list of paths to depend on when packaging the sources of this module. + SrcJarDeps android.Paths - // Extra files generated by the module type to be added as java resources. - extraResources android.Paths + // ExportedPlugins is a list of paths that should be used as annotation processors for any + // module that depends on this module. + ExportedPlugins android.Paths - hiddenAPI - dexpreopter - linter + // ExportedPluginClasses is a list of classes that should be run as annotation processors for + // any module that depends on this module. + ExportedPluginClasses []string - // list of the xref extraction files - kytheFiles android.Paths + // ExportedPluginDisableTurbine is true if this module's annotation processors generate APIs, + // requiring disbling turbine for any modules that depend on it. + ExportedPluginDisableTurbine bool - distFile android.Path + // JacocoReportClassesFile is the path to a jar containing uninstrumented classes that will be + // instrumented by jacoco. + JacocoReportClassesFile android.Path } -func (j *Module) addHostProperties() { - j.AddProperties( - &j.properties, - &j.protoProperties, - ) -} +var JavaInfoProvider = blueprint.NewProvider(JavaInfo{}) -func (j *Module) addHostAndDeviceProperties() { - j.addHostProperties() - j.AddProperties( - &j.deviceProperties, - &j.dexpreoptProperties, - &j.linter.properties, - ) +// SyspropPublicStubInfo contains info about the sysprop public stub library that corresponds to +// the sysprop implementation library. +type SyspropPublicStubInfo struct { + // JavaInfo is the JavaInfoProvider of the sysprop public stub library that corresponds to + // the sysprop implementation library. + JavaInfo JavaInfo } -func (j *Module) OutputFiles(tag string) (android.Paths, error) { - switch tag { - case "": - return append(android.Paths{j.outputFile}, j.extraOutputFiles...), nil - case ".jar": - return android.Paths{j.implementationAndResourcesJar}, nil - case ".proguard_map": - return android.Paths{j.proguardDictionary}, nil - default: - return nil, fmt.Errorf("unsupported module reference tag %q", tag) - } -} - -var _ android.OutputFileProducer = (*Module)(nil) +var SyspropPublicStubInfoProvider = blueprint.NewProvider(SyspropPublicStubInfo{}) // Methods that need to be implemented for a module that is added to apex java_libs property. type ApexDependency interface { @@ -529,19 +210,14 @@ type ApexDependency interface { ImplementationAndResourcesJars() android.Paths } -type Dependency interface { - ApexDependency - ImplementationJars() android.Paths - ResourceJars() android.Paths - DexJar() android.Path - AidlIncludeDirs() android.Paths - ExportedSdkLibs() []string - ExportedPlugins() (android.Paths, []string) - SrcJarArgs() ([]string, android.Paths) - BaseModuleName() string - JacocoReportClassesFile() android.Path +// Provides build path and install path to DEX jars. +type UsesLibraryDependency interface { + DexJarBuildPath() android.Path + DexJarInstallPath() android.Path + ClassLoaderContexts() dexpreopt.ClassLoaderContextMap } +// TODO(jungjw): Move this to kythe.go once it's created. type xref interface { XrefJavaFiles() android.Paths } @@ -550,41 +226,60 @@ func (j *Module) XrefJavaFiles() android.Paths { return j.kytheFiles } -func InitJavaModule(module android.DefaultableModule, hod android.HostOrDeviceSupported) { - android.InitAndroidArchModule(module, hod, android.MultilibCommon) - android.InitDefaultableModule(module) -} - type dependencyTag struct { blueprint.BaseDependencyTag 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 ( - staticLibTag = dependencyTag{name: "staticlib"} - libTag = dependencyTag{name: "javalib"} - java9LibTag = dependencyTag{name: "java9lib"} - pluginTag = dependencyTag{name: "plugin"} - exportedPluginTag = dependencyTag{name: "exported-plugin"} - bootClasspathTag = dependencyTag{name: "bootclasspath"} - systemModulesTag = dependencyTag{name: "system modules"} - frameworkResTag = dependencyTag{name: "framework-res"} - kotlinStdlibTag = dependencyTag{name: "kotlin-stdlib"} - kotlinAnnotationsTag = dependencyTag{name: "kotlin-annotations"} - 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"} + dataNativeBinsTag = dependencyTag{name: "dataNativeBins"} + staticLibTag = dependencyTag{name: "staticlib"} + libTag = dependencyTag{name: "javalib"} + java9LibTag = dependencyTag{name: "java9lib"} + pluginTag = dependencyTag{name: "plugin"} + errorpronePluginTag = dependencyTag{name: "errorprone-plugin"} + exportedPluginTag = dependencyTag{name: "exported-plugin"} + bootClasspathTag = dependencyTag{name: "bootclasspath"} + systemModulesTag = dependencyTag{name: "system modules"} + frameworkResTag = dependencyTag{name: "framework-res"} + kotlinStdlibTag = dependencyTag{name: "kotlin-stdlib"} + kotlinAnnotationsTag = dependencyTag{name: "kotlin-annotations"} + proguardRaiseTag = dependencyTag{name: "proguard-raise"} + certificateTag = dependencyTag{name: "certificate"} + instrumentationForTag = dependencyTag{name: "instrumentation_for"} + extraLintCheckTag = dependencyTag{name: "extra-lint-check"} + jniLibTag = dependencyTag{name: "jnilib"} + syspropPublicStubDepTag = dependencyTag{name: "sysprop public stub"} + 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 { @@ -596,7 +291,7 @@ func IsStaticLibDepTag(depTag blueprint.DependencyTag) bool { } type sdkDep struct { - useModule, useFiles, useDefaultLibs, invalidVersion bool + useModule, useFiles, invalidVersion bool // The modules that will be added to the bootclasspath when targeting 1.8 or lower bootclasspath []string @@ -605,7 +300,11 @@ type sdkDep struct { // modules are to be used. systemModules string + // The modules that will be added to the classpath regardless of the Java language level targeted + classpath []string + // The modules that will be added ot the classpath when targeting 1.9 or higher + // (normally these will be on the bootclasspath when targeting 1.8 or lower) java9Classpath []string frameworkResModule string @@ -632,209 +331,41 @@ type jniLib struct { unstrippedFile android.Path } -func (j *Module) shouldInstrument(ctx android.BaseModuleContext) bool { - return j.properties.Instrument && - ctx.Config().IsEnvTrue("EMMA_INSTRUMENT") && - ctx.DeviceConfig().JavaCoverageEnabledForPath(ctx.ModuleDir()) -} - -func (j *Module) shouldInstrumentStatic(ctx android.BaseModuleContext) bool { - return j.shouldInstrument(ctx) && - (ctx.Config().IsEnvTrue("EMMA_INSTRUMENT_STATIC") || - ctx.Config().UnbundledBuild()) -} - -func (j *Module) sdkVersion() sdkSpec { - return sdkSpecFrom(String(j.deviceProperties.Sdk_version)) -} - -func (j *Module) systemModules() string { - return proptools.String(j.deviceProperties.System_modules) -} - -func (j *Module) minSdkVersion() sdkSpec { - if j.deviceProperties.Min_sdk_version != nil { - return sdkSpecFrom(*j.deviceProperties.Min_sdk_version) - } - return j.sdkVersion() -} - -func (j *Module) targetSdkVersion() sdkSpec { - if j.deviceProperties.Target_sdk_version != nil { - return sdkSpecFrom(*j.deviceProperties.Target_sdk_version) - } - return j.sdkVersion() -} - -func (j *Module) MinSdkVersion() string { - return j.minSdkVersion().version.String() -} - -func (j *Module) AvailableFor(what string) bool { - if what == android.AvailableToPlatform && Bool(j.deviceProperties.Hostdex) { - // Exception: for hostdex: true libraries, the platform variant is created - // even if it's not marked as available to platform. In that case, the platform - // variant is used only for the hostdex and not installed to the device. - return true - } - return j.ApexModuleBase.AvailableFor(what) -} - -func (j *Module) deps(ctx android.BottomUpMutatorContext) { - if ctx.Device() { - j.linter.deps(ctx) - - sdkDep := decodeSdkDep(ctx, sdkContext(j)) - if sdkDep.useDefaultLibs { - ctx.AddVariationDependencies(nil, bootClasspathTag, config.DefaultBootclasspathLibraries...) - ctx.AddVariationDependencies(nil, systemModulesTag, config.DefaultSystemModules) - if sdkDep.hasFrameworkLibs() { - ctx.AddVariationDependencies(nil, libTag, config.DefaultLibraries...) - } - } else if sdkDep.useModule { - ctx.AddVariationDependencies(nil, bootClasspathTag, sdkDep.bootclasspath...) - ctx.AddVariationDependencies(nil, java9LibTag, sdkDep.java9Classpath...) - if j.deviceProperties.EffectiveOptimizeEnabled() && sdkDep.hasStandardLibs() { - ctx.AddVariationDependencies(nil, proguardRaiseTag, config.DefaultBootclasspathLibraries...) - ctx.AddVariationDependencies(nil, proguardRaiseTag, config.DefaultLibraries...) - } - } - if sdkDep.systemModules != "" { - ctx.AddVariationDependencies(nil, systemModulesTag, sdkDep.systemModules) - } - } - - syspropPublicStubs := syspropPublicStubs(ctx.Config()) - - // rewriteSyspropLibs validates if a java module can link against platform's sysprop_library, - // and redirects dependency to public stub depending on the link type. - rewriteSyspropLibs := func(libs []string, prop string) []string { - // make a copy - ret := android.CopyOf(libs) - - for idx, lib := range libs { - stub, ok := syspropPublicStubs[lib] - - if !ok { - continue - } - - linkType, _ := j.getLinkType(ctx.ModuleName()) - // only platform modules can use internal props - if linkType != javaPlatform { - ret[idx] = stub - } - } - - return ret - } - - ctx.AddVariationDependencies(nil, libTag, rewriteSyspropLibs(j.properties.Libs, "libs")...) - ctx.AddVariationDependencies(nil, staticLibTag, rewriteSyspropLibs(j.properties.Static_libs, "static_libs")...) - - ctx.AddFarVariationDependencies(ctx.Config().BuildOSCommonTarget.Variations(), pluginTag, j.properties.Plugins...) - ctx.AddFarVariationDependencies(ctx.Config().BuildOSCommonTarget.Variations(), exportedPluginTag, j.properties.Exported_plugins...) - - android.ProtoDeps(ctx, &j.protoProperties) - if j.hasSrcExt(".proto") { - protoDeps(ctx, &j.protoProperties) - } - - if j.hasSrcExt(".kt") { - // TODO(ccross): move this to a mutator pass that can tell if generated sources contain - // Kotlin files - ctx.AddVariationDependencies(nil, kotlinStdlibTag, - "kotlin-stdlib", "kotlin-stdlib-jdk7", "kotlin-stdlib-jdk8") - if len(j.properties.Plugins) > 0 { - ctx.AddVariationDependencies(nil, kotlinAnnotationsTag, "kotlin-annotations") +func sdkDeps(ctx android.BottomUpMutatorContext, sdkContext android.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...) } - } - - // Framework libraries need special handling in static coverage builds: they should not have - // static dependency on jacoco, otherwise there would be multiple conflicting definitions of - // the same jacoco classes coming from different bootclasspath jars. - if inList(ctx.ModuleName(), config.InstrumentFrameworkModules) { - if ctx.Config().IsEnvTrue("EMMA_INSTRUMENT_FRAMEWORK") { - j.properties.Instrument = true + if d.effectiveOptimizeEnabled() && sdkDep.hasFrameworkLibs() { + ctx.AddVariationDependencies(nil, proguardRaiseTag, config.FrameworkLibraries...) } - } else if j.shouldInstrumentStatic(ctx) { - ctx.AddVariationDependencies(nil, staticLibTag, "jacocoagent") } -} - -func hasSrcExt(srcs []string, ext string) bool { - for _, src := range srcs { - if filepath.Ext(src) == ext { - return true - } + if sdkDep.systemModules != "" { + ctx.AddVariationDependencies(nil, systemModulesTag, sdkDep.systemModules) } - - return false -} - -func (j *Module) hasSrcExt(ext string) bool { - return hasSrcExt(j.properties.Srcs, ext) -} - -func (j *Module) aidlFlags(ctx android.ModuleContext, aidlPreprocess android.OptionalPath, - aidlIncludeDirs android.Paths) (string, android.Paths) { - - aidlIncludes := android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl.Local_include_dirs) - aidlIncludes = append(aidlIncludes, - android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl.Export_include_dirs)...) - aidlIncludes = append(aidlIncludes, - android.PathsForSource(ctx, j.deviceProperties.Aidl.Include_dirs)...) - - var flags []string - var deps android.Paths - - if aidlPreprocess.Valid() { - flags = append(flags, "-p"+aidlPreprocess.String()) - deps = append(deps, aidlPreprocess.Path()) - } else if len(aidlIncludeDirs) > 0 { - flags = append(flags, android.JoinWithPrefix(aidlIncludeDirs.Strings(), "-I")) - } - - if len(j.exportAidlIncludeDirs) > 0 { - flags = append(flags, android.JoinWithPrefix(j.exportAidlIncludeDirs.Strings(), "-I")) - } - - if len(aidlIncludes) > 0 { - flags = append(flags, android.JoinWithPrefix(aidlIncludes.Strings(), "-I")) - } - - flags = append(flags, "-I"+android.PathForModuleSrc(ctx).String()) - if src := android.ExistentPathForSource(ctx, ctx.ModuleDir(), "src"); src.Valid() { - flags = append(flags, "-I"+src.String()) - } - - if Bool(j.deviceProperties.Aidl.Generate_traces) { - flags = append(flags, "-t") - } - - if Bool(j.deviceProperties.Aidl.Generate_get_transaction_name) { - flags = append(flags, "--transaction_names") - } - - return strings.Join(flags, " "), deps } type deps struct { - classpath classpath - java9Classpath classpath - bootClasspath classpath - processorPath classpath - processorClasses []string - staticJars android.Paths - staticHeaderJars android.Paths - staticResourceJars android.Paths - aidlIncludeDirs android.Paths - srcs android.Paths - srcJars android.Paths - systemModules *systemModules - aidlPreprocess android.OptionalPath - kotlinStdlib android.Paths - kotlinAnnotations android.Paths + classpath classpath + java9Classpath classpath + bootClasspath classpath + processorPath classpath + errorProneProcessorPath classpath + processorClasses []string + staticJars android.Paths + staticHeaderJars android.Paths + staticResourceJars android.Paths + aidlIncludeDirs android.Paths + srcs android.Paths + srcJars android.Paths + systemModules *systemModules + aidlPreprocess android.OptionalPath + kotlinStdlib android.Paths + kotlinAnnotations android.Paths disableTurbine bool } @@ -848,259 +379,11 @@ func checkProducesJars(ctx android.ModuleContext, dep android.SourceFileProducer } } -type linkType int - -const ( - // TODO(jiyong) rename these for better readability. Make the allowed - // and disallowed link types explicit - javaCore linkType = iota - javaSdk - javaSystem - javaModule - javaSystemServer - javaPlatform -) - -type linkTypeContext interface { - android.Module - getLinkType(name string) (ret linkType, stubs bool) -} - -func (m *Module) getLinkType(name string) (ret linkType, stubs bool) { - switch name { - case "core.current.stubs", "core.platform.api.stubs", "stub-annotations", - "private-stub-annotations-jar", "core-lambda-stubs", "core-generated-annotation-stubs": - return javaCore, true - case "android_stubs_current": - return javaSdk, true - case "android_system_stubs_current": - return javaSystem, true - case "android_module_lib_stubs_current": - return javaModule, true - case "android_system_server_stubs_current": - return javaSystemServer, true - case "android_test_stubs_current": - return javaSystem, true - } - - if stub, linkType := moduleStubLinkType(name); stub { - return linkType, true - } - - ver := m.sdkVersion() - switch ver.kind { - case sdkCore: - return javaCore, false - case sdkSystem: - return javaSystem, false - case sdkPublic: - return javaSdk, false - case sdkModule: - return javaModule, false - case sdkSystemServer: - return javaSystemServer, false - case sdkPrivate, sdkNone, sdkCorePlatform, sdkTest: - return javaPlatform, false - } - - if !ver.valid() { - panic(fmt.Errorf("sdk_version is invalid. got %q", ver.raw)) - } - return javaSdk, false -} - -func checkLinkType(ctx android.ModuleContext, from *Module, to linkTypeContext, tag dependencyTag) { - if ctx.Host() { - return - } - - myLinkType, stubs := from.getLinkType(ctx.ModuleName()) - if stubs { - return - } - otherLinkType, _ := to.getLinkType(ctx.OtherModuleName(to)) - commonMessage := "Adjust sdk_version: property of the source or target module so that target module is built with the same or smaller API set than the source." - - switch myLinkType { - case javaCore: - if otherLinkType != javaCore { - ctx.ModuleErrorf("compiles against core Java API, but dependency %q is compiling against non-core Java APIs."+commonMessage, - ctx.OtherModuleName(to)) - } - break - case javaSdk: - if otherLinkType != javaCore && otherLinkType != javaSdk { - ctx.ModuleErrorf("compiles against Android API, but dependency %q is compiling against non-public Android API."+commonMessage, - ctx.OtherModuleName(to)) - } - break - case javaSystem: - if otherLinkType == javaPlatform || otherLinkType == javaModule || otherLinkType == javaSystemServer { - ctx.ModuleErrorf("compiles against system API, but dependency %q is compiling against private API."+commonMessage, - ctx.OtherModuleName(to)) - } - break - case javaModule: - if otherLinkType == javaPlatform || otherLinkType == javaSystemServer { - ctx.ModuleErrorf("compiles against module API, but dependency %q is compiling against private API."+commonMessage, - ctx.OtherModuleName(to)) - } - break - case javaSystemServer: - if otherLinkType == javaPlatform { - ctx.ModuleErrorf("compiles against system server API, but dependency %q is compiling against private API."+commonMessage, - ctx.OtherModuleName(to)) - } - break - case javaPlatform: - // no restriction on link-type - break - } -} - -func (j *Module) collectDeps(ctx android.ModuleContext) deps { - var deps deps - - if ctx.Device() { - 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. - deps.classpath = append(deps.classpath, sdkDep.jars...) - deps.aidlPreprocess = sdkDep.aidl - } else { - deps.aidlPreprocess = sdkDep.aidl - } - } - - // 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 { - // Handled by AndroidApp.collectAppDeps - return - } - if tag == certificateTag { - // Handled by AndroidApp.collectAppDeps - return - } - - switch dep := module.(type) { - case SdkLibraryDependency: - switch tag { - 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()...) - case staticLibTag: - ctx.ModuleErrorf("dependency on java_sdk_library %q can only be in libs", otherName) - } - case Dependency: - switch tag { - case bootClasspathTag: - deps.bootClasspath = append(deps.bootClasspath, dep.HeaderJars()...) - 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()...) - deps.aidlIncludeDirs = append(deps.aidlIncludeDirs, dep.AidlIncludeDirs()...) - pluginJars, pluginClasses := dep.ExportedPlugins() - addPlugins(&deps, pluginJars, pluginClasses...) - case java9LibTag: - deps.java9Classpath = append(deps.java9Classpath, dep.HeaderJars()...) - case staticLibTag: - deps.classpath = append(deps.classpath, dep.HeaderJars()...) - deps.staticJars = append(deps.staticJars, dep.ImplementationJars()...) - 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()...) - deps.aidlIncludeDirs = append(deps.aidlIncludeDirs, dep.AidlIncludeDirs()...) - pluginJars, pluginClasses := dep.ExportedPlugins() - addPlugins(&deps, pluginJars, pluginClasses...) - case pluginTag: - if plugin, ok := dep.(*Plugin); ok { - if plugin.pluginProperties.Processor_class != nil { - addPlugins(&deps, plugin.ImplementationAndResourcesJars(), *plugin.pluginProperties.Processor_class) - } else { - addPlugins(&deps, plugin.ImplementationAndResourcesJars()) - } - deps.disableTurbine = deps.disableTurbine || Bool(plugin.pluginProperties.Generates_api) - } else { - ctx.PropertyErrorf("plugins", "%q is not a java_plugin module", otherName) - } - case exportedPluginTag: - if plugin, ok := dep.(*Plugin); ok { - if plugin.pluginProperties.Generates_api != nil && *plugin.pluginProperties.Generates_api { - ctx.PropertyErrorf("exported_plugins", "Cannot export plugins with generates_api = true, found %v", otherName) - } - j.exportedPluginJars = append(j.exportedPluginJars, plugin.ImplementationAndResourcesJars()...) - if plugin.pluginProperties.Processor_class != nil { - j.exportedPluginClasses = append(j.exportedPluginClasses, *plugin.pluginProperties.Processor_class) - } - } else { - ctx.PropertyErrorf("exported_plugins", "%q is not a java_plugin module", otherName) - } - case kotlinStdlibTag: - deps.kotlinStdlib = append(deps.kotlinStdlib, dep.HeaderJars()...) - case kotlinAnnotationsTag: - deps.kotlinAnnotations = dep.HeaderJars() - } - - case android.SourceFileProducer: - switch tag { - case libTag: - checkProducesJars(ctx, dep) - deps.classpath = append(deps.classpath, dep.Srcs()...) - case staticLibTag: - checkProducesJars(ctx, dep) - deps.classpath = append(deps.classpath, dep.Srcs()...) - deps.staticJars = append(deps.staticJars, dep.Srcs()...) - deps.staticHeaderJars = append(deps.staticHeaderJars, dep.Srcs()...) - } - default: - switch tag { - case bootClasspathTag: - // If a system modules dependency has been added to the bootclasspath - // then add its libs to the bootclasspath. - sm := module.(SystemModulesProvider) - deps.bootClasspath = append(deps.bootClasspath, sm.HeaderJars()...) - - case systemModulesTag: - if deps.systemModules != nil { - panic("Found two system module dependencies") - } - sm := module.(SystemModulesProvider) - outputDir, outputDeps := sm.OutputDirAndDeps() - deps.systemModules = &systemModules{outputDir, outputDeps} - } - } - }) - - j.exportedSdkLibs = android.FirstUniqueStrings(j.exportedSdkLibs) - - return deps -} - -func addPlugins(deps *deps, pluginJars android.Paths, pluginClasses ...string) { - deps.processorPath = append(deps.processorPath, pluginJars...) - deps.processorClasses = append(deps.processorClasses, pluginClasses...) -} - -func getJavaVersion(ctx android.ModuleContext, javaVersion string, sdkContext sdkContext) javaVersion { +func getJavaVersion(ctx android.ModuleContext, javaVersion string, sdkContext android.SdkContext) javaVersion { if javaVersion != "" { return normalizeJavaVersion(ctx, javaVersion) } else if ctx.Device() { - return sdkContext.sdkVersion().defaultJavaLanguageVersion(ctx) + return defaultJavaLanguageVersion(ctx, sdkContext.SdkVersion(ctx)) } else { return JAVA_VERSION_9 } @@ -1155,750 +438,18 @@ func normalizeJavaVersion(ctx android.BaseModuleContext, javaVersion string) jav } } -func (j *Module) collectBuilderFlags(ctx android.ModuleContext, deps deps) javaBuilderFlags { - - var flags javaBuilderFlags - - // 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() { - // 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?") - } - - errorProneFlags := []string{ - "-Xplugin:ErrorProne", - "${config.ErrorProneChecks}", - } - errorProneFlags = append(errorProneFlags, j.properties.Errorprone.Javacflags...) - - flags.errorProneExtraJavacFlags = "${config.ErrorProneFlags} " + - "'" + strings.Join(errorProneFlags, " ") + "'" - flags.errorProneProcessorPath = classpath(android.PathsForSource(ctx, config.ErrorProneClasspath)) - } - - // classpath - flags.bootClasspath = append(flags.bootClasspath, deps.bootClasspath...) - flags.classpath = append(flags.classpath, deps.classpath...) - flags.java9Classpath = append(flags.java9Classpath, deps.java9Classpath...) - flags.processorPath = append(flags.processorPath, deps.processorPath...) - - flags.processors = append(flags.processors, deps.processorClasses...) - flags.processors = android.FirstUniqueStrings(flags.processors) - - if len(flags.bootClasspath) == 0 && ctx.Host() && !flags.javaVersion.usesJavaModules() && - decodeSdkDep(ctx, sdkContext(j)).hasStandardLibs() { - // Give host-side tools a version of OpenJDK's standard libraries - // close to what they're targeting. As of Dec 2017, AOSP is only - // bundling OpenJDK 8 and 9, so nothing < 8 is available. - // - // When building with OpenJDK 8, the following should have no - // effect since those jars would be available by default. - // - // When building with OpenJDK 9 but targeting a version < 1.8, - // putting them on the bootclasspath means that: - // a) code can't (accidentally) refer to OpenJDK 9 specific APIs - // b) references to existing APIs are not reinterpreted in an - // OpenJDK 9-specific way, eg. calls to subclasses of - // java.nio.Buffer as in http://b/70862583 - java8Home := ctx.Config().Getenv("ANDROID_JAVA8_HOME") - flags.bootClasspath = append(flags.bootClasspath, - android.PathForSource(ctx, java8Home, "jre/lib/jce.jar"), - android.PathForSource(ctx, java8Home, "jre/lib/rt.jar")) - if Bool(j.properties.Use_tools_jar) { - flags.bootClasspath = append(flags.bootClasspath, - android.PathForSource(ctx, java8Home, "lib/tools.jar")) - } - } - - 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) - - if len(javacFlags) > 0 { - // optimization. - ctx.Variable(pctx, "javacFlags", strings.Join(javacFlags, " ")) - flags.javacFlags = "$javacFlags" - } - - return flags -} - -func (j *Module) compile(ctx android.ModuleContext, aaptSrcJar android.Path) { - j.exportAidlIncludeDirs = android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl.Export_include_dirs) - - deps := j.collectDeps(ctx) - flags := j.collectBuilderFlags(ctx, deps) - - if flags.javaVersion.usesJavaModules() { - j.properties.Srcs = append(j.properties.Srcs, j.properties.Openjdk9.Srcs...) - } - srcFiles := android.PathsForModuleSrcExcludes(ctx, j.properties.Srcs, j.properties.Exclude_srcs) - if hasSrcExt(srcFiles.Strings(), ".proto") { - flags = protoFlags(ctx, &j.properties, &j.protoProperties, flags) - } - - srcFiles = j.genSources(ctx, srcFiles, flags) - - srcJars := srcFiles.FilterByExt(".srcjar") - srcJars = append(srcJars, deps.srcJars...) - if aaptSrcJar != nil { - srcJars = append(srcJars, aaptSrcJar) - } - - if j.properties.Jarjar_rules != nil { - j.expandJarjarRules = android.PathForModuleSrc(ctx, *j.properties.Jarjar_rules) - } - - jarName := ctx.ModuleName() + ".jar" - - javaSrcFiles := srcFiles.FilterByExt(".java") - var uniqueSrcFiles android.Paths - set := make(map[string]bool) - for _, v := range javaSrcFiles { - if _, found := set[v.String()]; !found { - set[v.String()] = true - uniqueSrcFiles = append(uniqueSrcFiles, v) - } - } - - // Collect .java files for AIDEGen - j.expandIDEInfoCompiledSrcs = append(j.expandIDEInfoCompiledSrcs, uniqueSrcFiles.Strings()...) - - var kotlinJars android.Paths - - if srcFiles.HasExt(".kt") { - // user defined kotlin flags. - kotlincFlags := j.properties.Kotlincflags - CheckKotlincFlags(ctx, kotlincFlags) - - // If there are kotlin files, compile them first but pass all the kotlin and java files - // kotlinc will use the java files to resolve types referenced by the kotlin files, but - // won't emit any classes for them. - kotlincFlags = append(kotlincFlags, "-no-stdlib") - if ctx.Device() { - kotlincFlags = append(kotlincFlags, "-no-jdk") - } - if len(kotlincFlags) > 0 { - // optimization. - ctx.Variable(pctx, "kotlincFlags", strings.Join(kotlincFlags, " ")) - flags.kotlincFlags += "$kotlincFlags" - } - - var kotlinSrcFiles android.Paths - kotlinSrcFiles = append(kotlinSrcFiles, uniqueSrcFiles...) - kotlinSrcFiles = append(kotlinSrcFiles, srcFiles.FilterByExt(".kt")...) - - // Collect .kt files for AIDEGen - j.expandIDEInfoCompiledSrcs = append(j.expandIDEInfoCompiledSrcs, srcFiles.FilterByExt(".kt").Strings()...) - - flags.classpath = append(flags.classpath, deps.kotlinStdlib...) - flags.classpath = append(flags.classpath, deps.kotlinAnnotations...) - - flags.kotlincClasspath = append(flags.kotlincClasspath, flags.bootClasspath...) - flags.kotlincClasspath = append(flags.kotlincClasspath, flags.classpath...) - - if len(flags.processorPath) > 0 { - // Use kapt for annotation processing - kaptSrcJar := android.PathForModuleOut(ctx, "kapt", "kapt-sources.jar") - kaptResJar := android.PathForModuleOut(ctx, "kapt", "kapt-res.jar") - kotlinKapt(ctx, kaptSrcJar, kaptResJar, kotlinSrcFiles, srcJars, flags) - srcJars = append(srcJars, kaptSrcJar) - kotlinJars = append(kotlinJars, kaptResJar) - // Disable annotation processing in javac, it's already been handled by kapt - flags.processorPath = nil - flags.processors = nil - } - - kotlinJar := android.PathForModuleOut(ctx, "kotlin", jarName) - kotlinCompile(ctx, kotlinJar, kotlinSrcFiles, srcJars, flags) - if ctx.Failed() { - return - } - - // Make javac rule depend on the kotlinc rule - flags.classpath = append(flags.classpath, kotlinJar) - - kotlinJars = append(kotlinJars, kotlinJar) - // Jar kotlin classes into the final jar after javac - if BoolDefault(j.properties.Static_kotlin_stdlib, true) { - kotlinJars = append(kotlinJars, deps.kotlinStdlib...) - } - } - - jars := append(android.Paths(nil), kotlinJars...) - - // Store the list of .java files that was passed to javac - j.compiledJavaSrcs = uniqueSrcFiles - j.compiledSrcJars = srcJars - - enable_sharding := false - var headerJarFileWithoutJarjar android.Path - if ctx.Device() && !ctx.Config().IsEnvFalse("TURBINE_ENABLED") && !deps.disableTurbine { - if j.properties.Javac_shard_size != nil && *(j.properties.Javac_shard_size) > 0 { - enable_sharding = true - // Formerly, there was a check here that prevented annotation processors - // from being used when sharding was enabled, as some annotation processors - // do not function correctly in sharded environments. It was removed to - // allow for the use of annotation processors that do function correctly - // with sharding enabled. See: b/77284273. - } - headerJarFileWithoutJarjar, j.headerJarFile = - j.compileJavaHeader(ctx, uniqueSrcFiles, srcJars, deps, flags, jarName, kotlinJars) - if ctx.Failed() { - return - } - } - if len(uniqueSrcFiles) > 0 || len(srcJars) > 0 { - var extraJarDeps android.Paths - if ctx.Config().RunErrorProne() { - // If error-prone is enabled, add an additional rule to compile the java files into - // a separate set of classes (so that they don't overwrite the normal ones and require - // a rebuild when error-prone is turned off). - // TODO(ccross): Once we always compile with javac9 we may be able to conditionally - // enable error-prone without affecting the output class files. - errorprone := android.PathForModuleOut(ctx, "errorprone", jarName) - RunErrorProne(ctx, errorprone, uniqueSrcFiles, srcJars, flags) - extraJarDeps = append(extraJarDeps, errorprone) - } - - if enable_sharding { - flags.classpath = append(flags.classpath, headerJarFileWithoutJarjar) - shardSize := int(*(j.properties.Javac_shard_size)) - var shardSrcs []android.Paths - if len(uniqueSrcFiles) > 0 { - shardSrcs = android.ShardPaths(uniqueSrcFiles, shardSize) - for idx, shardSrc := range shardSrcs { - classes := j.compileJavaClasses(ctx, jarName, idx, shardSrc, - nil, flags, extraJarDeps) - jars = append(jars, classes) - } - } - if len(srcJars) > 0 { - classes := j.compileJavaClasses(ctx, jarName, len(shardSrcs), - nil, srcJars, flags, extraJarDeps) - jars = append(jars, classes) - } - } else { - classes := j.compileJavaClasses(ctx, jarName, -1, uniqueSrcFiles, srcJars, flags, extraJarDeps) - jars = append(jars, classes) - } - if ctx.Failed() { - return - } - } - - j.srcJarArgs, j.srcJarDeps = resourcePathsToJarArgs(srcFiles), srcFiles - - var includeSrcJar android.WritablePath - if Bool(j.properties.Include_srcs) { - includeSrcJar = android.PathForModuleOut(ctx, ctx.ModuleName()+".srcjar") - TransformResourcesToJar(ctx, includeSrcJar, j.srcJarArgs, j.srcJarDeps) - } - - dirArgs, dirDeps := ResourceDirsToJarArgs(ctx, j.properties.Java_resource_dirs, - j.properties.Exclude_java_resource_dirs, j.properties.Exclude_java_resources) - fileArgs, fileDeps := ResourceFilesToJarArgs(ctx, j.properties.Java_resources, j.properties.Exclude_java_resources) - extraArgs, extraDeps := resourcePathsToJarArgs(j.extraResources), j.extraResources - - var resArgs []string - var resDeps android.Paths - - resArgs = append(resArgs, dirArgs...) - resDeps = append(resDeps, dirDeps...) - - resArgs = append(resArgs, fileArgs...) - resDeps = append(resDeps, fileDeps...) - - resArgs = append(resArgs, extraArgs...) - resDeps = append(resDeps, extraDeps...) - - if len(resArgs) > 0 { - resourceJar := android.PathForModuleOut(ctx, "res", jarName) - TransformResourcesToJar(ctx, resourceJar, resArgs, resDeps) - j.resourceJar = resourceJar - if ctx.Failed() { - return - } - } - - var resourceJars android.Paths - if j.resourceJar != nil { - resourceJars = append(resourceJars, j.resourceJar) - } - if Bool(j.properties.Include_srcs) { - resourceJars = append(resourceJars, includeSrcJar) - } - resourceJars = append(resourceJars, deps.staticResourceJars...) - - if len(resourceJars) > 1 { - combinedJar := android.PathForModuleOut(ctx, "res-combined", jarName) - TransformJarsToJar(ctx, combinedJar, "for resources", resourceJars, android.OptionalPath{}, - false, nil, nil) - j.resourceJar = combinedJar - } else if len(resourceJars) == 1 { - j.resourceJar = resourceJars[0] - } - - if len(deps.staticJars) > 0 { - jars = append(jars, deps.staticJars...) - } - - manifest := j.overrideManifest - if !manifest.Valid() && j.properties.Manifest != nil { - manifest = android.OptionalPathForPath(android.PathForModuleSrc(ctx, *j.properties.Manifest)) - } - - services := android.PathsForModuleSrc(ctx, j.properties.Services) - if len(services) > 0 { - servicesJar := android.PathForModuleOut(ctx, "services", jarName) - var zipargs []string - for _, file := range services { - serviceFile := file.String() - zipargs = append(zipargs, "-C", filepath.Dir(serviceFile), "-f", serviceFile) - } - rule := zip - args := map[string]string{ - "jarArgs": "-P META-INF/services/ " + strings.Join(proptools.NinjaAndShellEscapeList(zipargs), " "), - } - if ctx.Config().IsEnvTrue("RBE_ZIP") { - rule = zipRE - args["implicits"] = strings.Join(services.Strings(), ",") - } - ctx.Build(pctx, android.BuildParams{ - Rule: rule, - Output: servicesJar, - Implicits: services, - Args: args, - }) - jars = append(jars, servicesJar) - } - - // Combine the classes built from sources, any manifests, and any static libraries into - // classes.jar. If there is only one input jar this step will be skipped. - var outputFile android.ModuleOutPath - - if len(jars) == 1 && !manifest.Valid() { - if moduleOutPath, ok := jars[0].(android.ModuleOutPath); ok { - // Optimization: skip the combine step if there is nothing to do - // TODO(ccross): this leaves any module-info.class files, but those should only come from - // prebuilt dependencies until we support modules in the platform build, so there shouldn't be - // any if len(jars) == 1. - outputFile = moduleOutPath - } else { - combinedJar := android.PathForModuleOut(ctx, "combined", jarName) - ctx.Build(pctx, android.BuildParams{ - Rule: android.Cp, - Input: jars[0], - Output: combinedJar, - }) - outputFile = combinedJar - } - } else { - combinedJar := android.PathForModuleOut(ctx, "combined", jarName) - TransformJarsToJar(ctx, combinedJar, "for javac", jars, manifest, - false, nil, nil) - outputFile = combinedJar - } - - // jarjar implementation jar if necessary - if j.expandJarjarRules != nil { - // Transform classes.jar into classes-jarjar.jar - jarjarFile := android.PathForModuleOut(ctx, "jarjar", jarName) - TransformJarJar(ctx, jarjarFile, outputFile, j.expandJarjarRules) - outputFile = jarjarFile - - // jarjar resource jar if necessary - if j.resourceJar != nil { - resourceJarJarFile := android.PathForModuleOut(ctx, "res-jarjar", jarName) - TransformJarJar(ctx, resourceJarJarFile, j.resourceJar, j.expandJarjarRules) - j.resourceJar = resourceJarJarFile - } - - if ctx.Failed() { - return - } - } - - // Check package restrictions if necessary. - if len(j.properties.Permitted_packages) > 0 { - // Check packages and copy to package-checked file. - pkgckFile := android.PathForModuleOut(ctx, "package-check.stamp") - CheckJarPackages(ctx, pkgckFile, outputFile, j.properties.Permitted_packages) - j.additionalCheckedModules = append(j.additionalCheckedModules, pkgckFile) - - if ctx.Failed() { - return - } - } - - j.implementationJarFile = outputFile - if j.headerJarFile == nil { - j.headerJarFile = j.implementationJarFile - } - - // 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) - isJacocoAgent := ctx.ModuleName() == "jacocoagent" - if android.DirectlyInAnyApex(ctx, ctx.ModuleName()) && !isJacocoAgent && !j.IsForPlatform() { - j.properties.Instrument = true - } - - if j.shouldInstrument(ctx) { - outputFile = j.instrument(ctx, flags, outputFile, jarName) - } - - // merge implementation jar with resources if necessary - implementationAndResourcesJar := outputFile - if j.resourceJar != nil { - jars := android.Paths{j.resourceJar, implementationAndResourcesJar} - combinedJar := android.PathForModuleOut(ctx, "withres", jarName) - TransformJarsToJar(ctx, combinedJar, "for resources", jars, manifest, - false, nil, nil) - implementationAndResourcesJar = combinedJar - } - - j.implementationAndResourcesJar = implementationAndResourcesJar - - // Enable dex compilation for the APEX variants, unless it is disabled explicitly - if android.DirectlyInAnyApex(ctx, ctx.ModuleName()) && !j.IsForPlatform() { - if j.deviceProperties.Compile_dex == nil { - j.deviceProperties.Compile_dex = proptools.BoolPtr(true) - } - if j.deviceProperties.Hostdex == nil { - j.deviceProperties.Hostdex = proptools.BoolPtr(true) - } - } - - if ctx.Device() && j.hasCode(ctx) && - (Bool(j.properties.Installable) || Bool(j.deviceProperties.Compile_dex)) { - // Dex compilation - var dexOutputFile android.ModuleOutPath - dexOutputFile = j.compileDex(ctx, flags, outputFile, jarName) - if ctx.Failed() { - return - } - - configurationName := j.ConfigurationName() - primary := configurationName == ctx.ModuleName() - - // Hidden API CSV generation and dex encoding - dexOutputFile = j.hiddenAPI.hiddenAPI(ctx, configurationName, primary, dexOutputFile, j.implementationJarFile, - proptools.Bool(j.deviceProperties.Uncompress_dex)) - - // merge dex jar with resources if necessary - if j.resourceJar != nil { - jars := android.Paths{dexOutputFile, j.resourceJar} - combinedJar := android.PathForModuleOut(ctx, "dex-withres", jarName) - TransformJarsToJar(ctx, combinedJar, "for dex resources", jars, android.OptionalPath{}, - false, nil, nil) - if *j.deviceProperties.Uncompress_dex { - combinedAlignedJar := android.PathForModuleOut(ctx, "dex-withres-aligned", jarName) - TransformZipAlign(ctx, combinedAlignedJar, combinedJar) - dexOutputFile = combinedAlignedJar - } else { - dexOutputFile = combinedJar - } - } - - j.dexJarFile = dexOutputFile - - // Dexpreopting - dexOutputFile = j.dexpreopt(ctx, dexOutputFile) - - j.maybeStrippedDexJarFile = dexOutputFile - - outputFile = dexOutputFile - - if ctx.Failed() { - return - } - } else { - outputFile = implementationAndResourcesJar - } - - if ctx.Device() { - lintSDKVersionString := func(sdkSpec sdkSpec) string { - if v := sdkSpec.version; v.isNumbered() { - return v.String() - } else { - return ctx.Config().DefaultAppTargetSdk() - } - } - - j.linter.name = ctx.ModuleName() - j.linter.srcs = srcFiles - j.linter.srcJars = srcJars - j.linter.classpath = append(append(android.Paths(nil), flags.bootClasspath...), flags.classpath...) - j.linter.classes = j.implementationJarFile - j.linter.minSdkVersion = lintSDKVersionString(j.minSdkVersion()) - j.linter.targetSdkVersion = lintSDKVersionString(j.targetSdkVersion()) - j.linter.compileSdkVersion = lintSDKVersionString(j.sdkVersion()) - j.linter.javaLanguageLevel = flags.javaVersion.String() - j.linter.kotlinLanguageLevel = "1.3" - if j.ApexName() != "" && ctx.Config().UnbundledBuild() { - j.linter.buildModuleReportZip = true - } - j.linter.lint(ctx) - } - - ctx.CheckbuildFile(outputFile) - - // Save the output file with no relative path so that it doesn't end up in a subdirectory when used as a resource - j.outputFile = outputFile.WithoutRel() -} - -func (j *Module) compileJavaClasses(ctx android.ModuleContext, jarName string, idx int, - srcFiles, srcJars android.Paths, flags javaBuilderFlags, extraJarDeps android.Paths) android.WritablePath { - - kzipName := pathtools.ReplaceExtension(jarName, "kzip") - if idx >= 0 { - kzipName = strings.TrimSuffix(jarName, filepath.Ext(jarName)) + strconv.Itoa(idx) + ".kzip" - jarName += strconv.Itoa(idx) - } - - classes := android.PathForModuleOut(ctx, "javac", jarName) - TransformJavaToClasses(ctx, classes, idx, srcFiles, srcJars, flags, extraJarDeps) - - if ctx.Config().EmitXrefRules() { - extractionFile := android.PathForModuleOut(ctx, kzipName) - emitXrefRule(ctx, extractionFile, idx, srcFiles, srcJars, flags, extraJarDeps) - j.kytheFiles = append(j.kytheFiles, extractionFile) - } - - return classes -} - -// Check for invalid kotlinc flags. Only use this for flags explicitly passed by the user, -// since some of these flags may be used internally. -func CheckKotlincFlags(ctx android.ModuleContext, flags []string) { - for _, flag := range flags { - flag = strings.TrimSpace(flag) - - if !strings.HasPrefix(flag, "-") { - ctx.PropertyErrorf("kotlincflags", "Flag `%s` must start with `-`", flag) - } else if strings.HasPrefix(flag, "-Xintellij-plugin-root") { - ctx.PropertyErrorf("kotlincflags", - "Bad flag: `%s`, only use internal compiler for consistency.", flag) - } else if inList(flag, config.KotlincIllegalFlags) { - ctx.PropertyErrorf("kotlincflags", "Flag `%s` already used by build system", flag) - } else if flag == "-include-runtime" { - ctx.PropertyErrorf("kotlincflags", "Bad flag: `%s`, do not include runtime.", flag) - } else { - args := strings.Split(flag, " ") - if args[0] == "-kotlin-home" { - ctx.PropertyErrorf("kotlincflags", - "Bad flag: `%s`, kotlin home already set to default (path to kotlinc in the repo).", flag) - } - } - } -} - -func (j *Module) compileJavaHeader(ctx android.ModuleContext, srcFiles, srcJars android.Paths, - deps deps, flags javaBuilderFlags, jarName string, - extraJars android.Paths) (headerJar, jarjarHeaderJar android.Path) { - - var jars android.Paths - if len(srcFiles) > 0 || len(srcJars) > 0 { - // Compile java sources into turbine.jar. - turbineJar := android.PathForModuleOut(ctx, "turbine", jarName) - TransformJavaToHeaderClasses(ctx, turbineJar, srcFiles, srcJars, flags) - if ctx.Failed() { - return nil, nil - } - jars = append(jars, turbineJar) - } - - jars = append(jars, extraJars...) - - // Combine any static header libraries into classes-header.jar. If there is only - // one input jar this step will be skipped. - jars = append(jars, deps.staticHeaderJars...) - - // we cannot skip the combine step for now if there is only one jar - // since we have to strip META-INF/TRANSITIVE dir from turbine.jar - combinedJar := android.PathForModuleOut(ctx, "turbine-combined", jarName) - TransformJarsToJar(ctx, combinedJar, "for turbine", jars, android.OptionalPath{}, - false, nil, []string{"META-INF/TRANSITIVE"}) - headerJar = combinedJar - jarjarHeaderJar = combinedJar - - if j.expandJarjarRules != nil { - // Transform classes.jar into classes-jarjar.jar - jarjarFile := android.PathForModuleOut(ctx, "turbine-jarjar", jarName) - TransformJarJar(ctx, jarjarFile, headerJar, j.expandJarjarRules) - jarjarHeaderJar = jarjarFile - if ctx.Failed() { - return nil, nil - } - } - - return headerJar, jarjarHeaderJar -} - -func (j *Module) instrument(ctx android.ModuleContext, flags javaBuilderFlags, - classesJar android.Path, jarName string) android.ModuleOutPath { - - specs := j.jacocoModuleToZipCommand(ctx) - - jacocoReportClassesFile := android.PathForModuleOut(ctx, "jacoco-report-classes", jarName) - instrumentedJar := android.PathForModuleOut(ctx, "jacoco", jarName) - - jacocoInstrumentJar(ctx, instrumentedJar, jacocoReportClassesFile, classesJar, specs) - - j.jacocoReportClassesFile = jacocoReportClassesFile - - return instrumentedJar -} - -var _ Dependency = (*Module)(nil) - -func (j *Module) HeaderJars() android.Paths { - if j.headerJarFile == nil { - return nil - } - return android.Paths{j.headerJarFile} -} - -func (j *Module) ImplementationJars() android.Paths { - if j.implementationJarFile == nil { - return nil - } - return android.Paths{j.implementationJarFile} -} - -func (j *Module) DexJar() android.Path { - return j.dexJarFile -} - -func (j *Module) ResourceJars() android.Paths { - if j.resourceJar == nil { - return nil - } - return android.Paths{j.resourceJar} -} - -func (j *Module) ImplementationAndResourcesJars() android.Paths { - if j.implementationAndResourcesJar == nil { - return nil - } - return android.Paths{j.implementationAndResourcesJar} -} - -func (j *Module) AidlIncludeDirs() android.Paths { - // exportAidlIncludeDirs is type android.Paths already - return j.exportAidlIncludeDirs -} - -func (j *Module) ExportedSdkLibs() []string { - // exportedSdkLibs is type []string - return j.exportedSdkLibs -} - -func (j *Module) ExportedPlugins() (android.Paths, []string) { - return j.exportedPluginJars, j.exportedPluginClasses -} - -func (j *Module) SrcJarArgs() ([]string, android.Paths) { - return j.srcJarArgs, j.srcJarDeps -} - -var _ logtagsProducer = (*Module)(nil) - -func (j *Module) logtags() android.Paths { - return j.logtagsSrcs -} - -// Collect information for opening IDE project files in java/jdeps.go. -func (j *Module) IDEInfo(dpInfo *android.IdeInfo) { - dpInfo.Deps = append(dpInfo.Deps, j.CompilerDeps()...) - dpInfo.Srcs = append(dpInfo.Srcs, j.expandIDEInfoCompiledSrcs...) - dpInfo.SrcJars = append(dpInfo.SrcJars, j.compiledSrcJars.Strings()...) - dpInfo.Aidl_include_dirs = append(dpInfo.Aidl_include_dirs, j.deviceProperties.Aidl.Include_dirs...) - if j.expandJarjarRules != nil { - dpInfo.Jarjar_rules = append(dpInfo.Jarjar_rules, j.expandJarjarRules.String()) - } -} - -func (j *Module) CompilerDeps() []string { - jdeps := []string{} - jdeps = append(jdeps, j.properties.Libs...) - jdeps = append(jdeps, j.properties.Static_libs...) - return jdeps -} - -func (j *Module) hasCode(ctx android.ModuleContext) bool { - srcFiles := android.PathsForModuleSrcExcludes(ctx, j.properties.Srcs, j.properties.Exclude_srcs) - return len(srcFiles) > 0 || len(ctx.GetDirectDepsWithTag(staticLibTag)) > 0 -} - -func (j *Module) DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool { - return j.depIsInSameApex(ctx, dep) -} - -func (j *Module) Stem() string { - return proptools.StringDefault(j.deviceProperties.Stem, j.Name()) -} - -func (j *Module) ConfigurationName() string { - return proptools.StringDefault(j.deviceProperties.ConfigurationName, j.BaseModuleName()) -} - -func (j *Module) JacocoReportClassesFile() android.Path { - return j.jacocoReportClassesFile -} - -func (j *Module) IsInstallable() bool { - return Bool(j.properties.Installable) -} - // // Java libraries (.jar file) // -type LibraryProperties struct { - Dist struct { - // The tag of the output of this module that should be output. - Tag *string `android:"arch_variant"` - } `android:"arch_variant"` -} - type Library struct { Module - libraryProperties LibraryProperties - InstallMixin func(ctx android.ModuleContext, installPath android.Path) (extraInstallDeps android.Paths) } +var _ android.ApexModule = (*Library)(nil) + // Provides access to the list of permitted packages from updatable boot jars. type PermittedPackagesForUpdatableBootJars interface { PermittedPackagesForUpdatableBootJars() []string @@ -1912,7 +463,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 } @@ -1934,17 +485,29 @@ func shouldUncompressDex(ctx android.ModuleContext, dexpreopter *dexpreopter) bo } func (j *Library) GenerateAndroidBuildActions(ctx android.ModuleContext) { + j.sdkVersion = j.SdkVersion(ctx) + j.minSdkVersion = j.MinSdkVersion(ctx) + + 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 - if j.deviceProperties.Uncompress_dex == nil { + if j.dexProperties.Uncompress_dex == nil { // If the value was not force-set by the user, use reasonable default based on the module. - j.deviceProperties.Uncompress_dex = proptools.BoolPtr(shouldUncompressDex(ctx, &j.dexpreopter)) + j.dexProperties.Uncompress_dex = proptools.BoolPtr(shouldUncompressDex(ctx, &j.dexpreopter)) } - j.dexpreopter.uncompressedDex = *j.deviceProperties.Uncompress_dex + j.dexpreopter.uncompressedDex = *j.dexProperties.Uncompress_dex + j.classLoaderContexts = make(dexpreopt.ClassLoaderContextMap) j.compile(ctx, nil) - exclusivelyForApex := android.InAnyApex(ctx.ModuleName()) && !j.IsForPlatform() + // Collect the module directory for IDE info in java/jdeps.go. + j.modulePaths = append(j.modulePaths, ctx.ModuleDir()) + + exclusivelyForApex := !apexInfo.IsForPlatform() if (Bool(j.properties.Installable) || ctx.Host()) && !exclusivelyForApex { var extraInstallDeps android.Paths if j.InstallMixin != nil { @@ -1953,15 +516,6 @@ func (j *Library) GenerateAndroidBuildActions(ctx android.ModuleContext) { j.installFile = ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"), j.Stem()+".jar", j.outputFile, extraInstallDeps...) } - - // Verify Dist.Tag is set to a supported output - if j.libraryProperties.Dist.Tag != nil { - distFiles, err := j.OutputFiles(*j.libraryProperties.Dist.Tag) - if err != nil { - ctx.PropertyErrorf("dist.tag", "%s", err.Error()) - } - j.distFile = distFiles[0] - } } func (j *Library) DepsMutator(ctx android.BottomUpMutatorContext) { @@ -1989,9 +543,22 @@ type librarySdkMemberType struct { // Function to retrieve the appropriate output jar (implementation or header) from // the library. - jarToExportGetter func(j *Library) android.Path + jarToExportGetter func(ctx android.SdkMemberContext, j *Library) android.Path + + // Function to compute the snapshot relative path to which the named library's + // jar should be copied. + snapshotPathGetter func(osPrefix, name string) string + + // True if only the jar should be copied to the snapshot, false if the jar plus any additional + // files like aidl files should also be copied. + onlyCopyJarToSnapshot bool } +const ( + onlyCopyJarToSnapshot = true + copyEverythingToSnapshot = false +) + func (mt *librarySdkMemberType) AddDependencies(mctx android.BottomUpMutatorContext, dependencyTag blueprint.DependencyTag, names []string) { mctx.AddVariationDependencies(nil, dependencyTag, names...) } @@ -2014,26 +581,47 @@ type librarySdkMemberProperties struct { JarToExport android.Path `android:"arch_variant"` AidlIncludeDirs android.Paths + + // The list of permitted packages that need to be passed to the prebuilts as they are used to + // create the updatable-bcp-packages.txt file. + PermittedPackages []string } func (p *librarySdkMemberProperties) PopulateFromVariant(ctx android.SdkMemberContext, variant android.Module) { j := variant.(*Library) - p.JarToExport = ctx.MemberType().(*librarySdkMemberType).jarToExportGetter(j) + p.JarToExport = ctx.MemberType().(*librarySdkMemberType).jarToExportGetter(ctx, j) + p.AidlIncludeDirs = j.AidlIncludeDirs() + + p.PermittedPackages = j.PermittedPackagesForUpdatableBootJars() } func (p *librarySdkMemberProperties) AddToPropertySet(ctx android.SdkMemberContext, propertySet android.BpPropertySet) { builder := ctx.SnapshotBuilder() + memberType := ctx.MemberType().(*librarySdkMemberType) + exportedJar := p.JarToExport if exportedJar != nil { - snapshotRelativeJavaLibPath := sdkSnapshotFilePathForJar(p.OsPrefix(), ctx.Name()) + // Delegate the creation of the snapshot relative path to the member type. + snapshotRelativeJavaLibPath := memberType.snapshotPathGetter(p.OsPrefix(), ctx.Name()) + + // Copy the exported jar to the snapshot. builder.CopyToSnapshot(exportedJar, snapshotRelativeJavaLibPath) propertySet.AddProperty("jars", []string{snapshotRelativeJavaLibPath}) } + if len(p.PermittedPackages) > 0 { + propertySet.AddProperty("permitted_packages", p.PermittedPackages) + } + + // Do not copy anything else to the snapshot. + if memberType.onlyCopyJarToSnapshot { + return + } + aidlIncludeDirs := p.AidlIncludeDirs if len(aidlIncludeDirs) != 0 { sdkModuleContext := ctx.SdkModuleContext() @@ -2049,21 +637,6 @@ func (p *librarySdkMemberProperties) AddToPropertySet(ctx android.SdkMemberConte } } -var javaHeaderLibsSdkMemberType android.SdkMemberType = &librarySdkMemberType{ - android.SdkMemberTypeBase{ - PropertyName: "java_header_libs", - SupportsSdk: true, - }, - func(j *Library) android.Path { - headerJars := j.HeaderJars() - if len(headerJars) != 1 { - panic(fmt.Errorf("there must be only one header jar from %q", j.Name())) - } - - return headerJars[0] - }, -} - // java_library builds and links sources into a `.jar` file for the device, and possibly for the host as well. // // By default, a java_library has a single variant that produces a `.jar` file containing `.class` files that were @@ -2079,9 +652,8 @@ func LibraryFactory() android.Module { module := &Library{} module.addHostAndDeviceProperties() - module.AddProperties(&module.libraryProperties) - module.initModuleAndImport(&module.ModuleBase) + module.initModuleAndImport(module) android.InitApexModule(module) android.InitSdkAwareModule(module) @@ -2106,6 +678,7 @@ func LibraryHostFactory() android.Module { module.Module.properties.Installable = proptools.BoolPtr(true) android.InitApexModule(module) + android.InitSdkAwareModule(module) InitJavaModule(module, android.HostSupported) return module } @@ -2114,6 +687,15 @@ 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"` + + // If the test is a hostside(no device required) unittest that shall be run during presubmit check. + Unit_test *bool +} + type testProperties struct { // list of compatibility suites (for example "cts", "vts") that the module should be // installed into. @@ -2139,6 +721,17 @@ 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 + + // Names of modules containing JNI libraries that should be installed alongside the test. + Jni_libs []string +} + +type hostTestProperties struct { + // list of native binary modules that should be installed alongside the test + Data_native_bins []string `android:"arch_variant"` } type testHelperLibraryProperties struct { @@ -2162,8 +755,15 @@ type Test struct { testProperties testProperties - testConfig android.Path - data android.Paths + testConfig android.Path + extraTestConfigs android.Paths + data android.Paths +} + +type TestHost struct { + Test + + testHostProperties hostTestProperties } type TestHelperLibrary struct { @@ -2178,13 +778,70 @@ type JavaTestImport struct { prebuiltTestProperties prebuiltTestProperties testConfig android.Path + dexJarFile android.Path +} + +func (j *TestHost) DepsMutator(ctx android.BottomUpMutatorContext) { + if len(j.testHostProperties.Data_native_bins) > 0 { + for _, target := range ctx.MultiTargets() { + ctx.AddVariationDependencies(target.Variations(), dataNativeBinsTag, j.testHostProperties.Data_native_bins...) + } + } + + if len(j.testProperties.Jni_libs) > 0 { + for _, target := range ctx.MultiTargets() { + sharedLibVariations := append(target.Variations(), blueprint.Variation{Mutator: "link", Variation: "shared"}) + ctx.AddFarVariationDependencies(sharedLibVariations, jniLibTag, j.testProperties.Jni_libs...) + } + } + + j.deps(ctx) +} + +func (j *TestHost) AddExtraResource(p android.Path) { + j.extraResources = append(j.extraResources, p) } func (j *Test) GenerateAndroidBuildActions(ctx android.ModuleContext) { + if j.testProperties.Test_options.Unit_test == nil && ctx.Host() { + // TODO(b/): Clean temporary heuristic to avoid unexpected onboarding. + defaultUnitTest := !inList("tradefed", j.properties.Libs) && !inList("cts", j.testProperties.Test_suites) + j.testProperties.Test_options.Unit_test = proptools.BoolPtr(defaultUnitTest) + } j.testConfig = tradefed.AutoGenJavaTestConfig(ctx, j.testProperties.Test_config, j.testProperties.Test_config_template, - j.testProperties.Test_suites, j.testProperties.Auto_gen_config) + j.testProperties.Test_suites, j.testProperties.Auto_gen_config, j.testProperties.Test_options.Unit_test) + 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, "")) + }) + + ctx.VisitDirectDepsWithTag(jniLibTag, func(dep android.Module) { + sharedLibInfo := ctx.OtherModuleProvider(dep, cc.SharedLibraryInfoProvider).(cc.SharedLibraryInfo) + if sharedLibInfo.SharedLibrary != nil { + // Copy to an intermediate output directory to append "lib[64]" to the path, + // so that it's compatible with the default rpath values. + var relPath string + if sharedLibInfo.Target.Arch.ArchType.Multilib == "lib64" { + relPath = filepath.Join("lib64", sharedLibInfo.SharedLibrary.Base()) + } else { + relPath = filepath.Join("lib", sharedLibInfo.SharedLibrary.Base()) + } + relocatedLib := android.PathForModuleOut(ctx, "relocated").Join(ctx, relPath) + ctx.Build(pctx, android.BuildParams{ + Rule: android.Cp, + Input: sharedLibInfo.SharedLibrary, + Output: relocatedLib, + }) + j.data = append(j.data, relocatedLib) + } else { + ctx.PropertyErrorf("jni_libs", "%q of type %q is not supported", dep.Name(), ctx.OtherModuleType(dep)) + } + }) + j.Library.GenerateAndroidBuildActions(ctx) } @@ -2194,7 +851,7 @@ func (j *TestHelperLibrary) GenerateAndroidBuildActions(ctx android.ModuleContex func (j *JavaTestImport) GenerateAndroidBuildActions(ctx android.ModuleContext) { j.testConfig = tradefed.AutoGenJavaTestConfig(ctx, j.prebuiltTestProperties.Test_config, nil, - j.prebuiltTestProperties.Test_suites, nil) + j.prebuiltTestProperties.Test_suites, nil, nil) j.Import.GenerateAndroidBuildActions(ctx) } @@ -2276,6 +933,7 @@ func TestFactory() android.Module { module.Module.dexpreopter.isTest = true module.Module.linter.test = true + android.InitSdkAwareModule(module) InitJavaModule(module, android.HostAndDeviceSupported) return module } @@ -2325,17 +983,29 @@ func JavaTestImportFactory() android.Module { // A java_test_host has a single variant that produces a `.jar` file containing `.class` files that were // compiled against the host bootclasspath. func TestHostFactory() android.Module { - module := &Test{} + module := &TestHost{} module.addHostProperties() module.AddProperties(&module.testProperties) + module.AddProperties(&module.testHostProperties) - module.Module.properties.Installable = proptools.BoolPtr(true) + InitTestHost( + module, + proptools.BoolPtr(true), + nil, + nil) + + InitJavaModuleMultiTargets(module, android.HostSupported) - InitJavaModule(module, android.HostSupported) return module } +func InitTestHost(th *TestHost, installable *bool, testSuites []string, autoGenConfig *bool) { + th.properties.Installable = installable + th.testProperties.Auto_gen_config = autoGenConfig + th.testProperties.Test_suites = testSuites +} + // // Java Binaries (.jar file plus wrapper script) // @@ -2346,6 +1016,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 { @@ -2386,19 +1060,26 @@ 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 { + if ctx.Arch().ArchType == android.Common || ctx.BazelConversionMode() { j.deps(ctx) } + if ctx.Arch().ArchType != android.Common || ctx.BazelConversionMode() { + // 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()) + } } // java_binary builds a `.jar` file and a shell script that executes it for the device, and possibly for the host @@ -2446,10 +1127,20 @@ func BinaryHostFactory() android.Module { type ImportProperties struct { Jars []string `android:"path,arch_variant"` + // The version of the SDK that the source prebuilt file was built against. Defaults to the + // current version if not specified. Sdk_version *string + // The minimum version of the SDK that this module supports. Defaults to sdk_version if not + // specified. + Min_sdk_version *string + Installable *bool + // If not empty, classes are restricted to the specified packages and their sub-packages. + // This information is used to generate the updatable-bcp-packages.txt file. + Permitted_packages []string + // List of shared java libs that this module has dependencies to Libs []string @@ -2464,6 +1155,12 @@ type ImportProperties struct { // set the name of the output Stem *string + + Aidl struct { + // directories that should be added as include directories for any aidl sources of modules + // that depend on this module, as well as to aidl for this module. + Export_include_dirs []string + } } type Import struct { @@ -2476,22 +1173,48 @@ type Import struct { // Functionality common to Module and Import. embeddableInModuleAndImport + hiddenAPI + dexer + dexpreopter + properties ImportProperties + // output file containing classes.dex and resources + dexJarFile android.Path + combinedClasspathFile android.Path - exportedSdkLibs []string + classLoaderContexts dexpreopt.ClassLoaderContextMap + exportAidlIncludeDirs android.Paths + + hideApexVariantFromMake bool + + sdkVersion android.SdkSpec + minSdkVersion android.SdkSpec +} + +var _ PermittedPackagesForUpdatableBootJars = (*Import)(nil) + +func (j *Import) PermittedPackagesForUpdatableBootJars() []string { + return j.properties.Permitted_packages } -func (j *Import) sdkVersion() sdkSpec { - return sdkSpecFrom(String(j.properties.Sdk_version)) +func (j *Import) SdkVersion(ctx android.EarlyModuleContext) android.SdkSpec { + return android.SdkSpecFrom(ctx, String(j.properties.Sdk_version)) } -func (j *Import) minSdkVersion() sdkSpec { - return j.sdkVersion() +func (j *Import) SystemModules() string { + return "none" } -func (j *Import) MinSdkVersion() string { - return j.minSdkVersion().version.String() +func (j *Import) MinSdkVersion(ctx android.EarlyModuleContext) android.SdkSpec { + if j.properties.Min_sdk_version != nil { + return android.SdkSpecFrom(ctx, *j.properties.Min_sdk_version) + } + return j.SdkVersion(ctx) +} + +func (j *Import) TargetSdkVersion(ctx android.EarlyModuleContext) android.SdkSpec { + return j.SdkVersion(ctx) } func (j *Import) Prebuilt() *android.Prebuilt { @@ -2514,11 +1237,33 @@ func (a *Import) JacocoReportClassesFile() android.Path { return nil } +func (j *Import) LintDepSets() LintDepSets { + return LintDepSets{} +} + +func (j *Import) getStrictUpdatabilityLinting() bool { + return false +} + +func (j *Import) setStrictUpdatabilityLinting(bool) { +} + func (j *Import) DepsMutator(ctx android.BottomUpMutatorContext) { ctx.AddVariationDependencies(nil, libTag, j.properties.Libs...) + + if ctx.Device() && Bool(j.dexProperties.Compile_dex) { + sdkDeps(ctx, android.SdkContext(j), j.dexer) + } } func (j *Import) GenerateAndroidBuildActions(ctx android.ModuleContext) { + j.sdkVersion = j.SdkVersion(ctx) + j.minSdkVersion = j.MinSdkVersion(ctx) + + if !ctx.Provider(android.ApexInfoProvider).(android.ApexInfo).IsForPlatform() { + j.hideApexVariantFromMake = true + } + jars := android.PathsForModuleSrc(ctx, j.properties.Jars) jarName := j.Stem() + ".jar" @@ -2531,41 +1276,126 @@ func (j *Import) GenerateAndroidBuildActions(ctx android.ModuleContext) { TransformJetifier(ctx, outputFile, inputFile) } j.combinedClasspathFile = outputFile + j.classLoaderContexts = 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 + var deapexerModule android.Module ctx.VisitDirectDeps(func(module android.Module) { - otherName := ctx.OtherModuleName(module) tag := ctx.OtherModuleDependencyTag(module) - switch dep := module.(type) { - case Dependency: + if ctx.OtherModuleHasProvider(module, JavaInfoProvider) { + dep := ctx.OtherModuleProvider(module, JavaInfoProvider).(JavaInfo) switch tag { case libTag, staticLibTag: - // sdk lib names from dependencies are re-exported - j.exportedSdkLibs = append(j.exportedSdkLibs, dep.ExportedSdkLibs()...) + flags.classpath = append(flags.classpath, dep.HeaderJars...) + case bootClasspathTag: + flags.bootClasspath = append(flags.bootClasspath, dep.HeaderJars...) } - case SdkLibraryDependency: + } else if dep, ok := module.(SdkLibraryDependency); ok { switch tag { case libTag: - // names of sdk libs that are directly depended are exported - j.exportedSdkLibs = append(j.exportedSdkLibs, otherName) + flags.classpath = append(flags.classpath, dep.SdkHeaderJars(ctx, j.SdkVersion(ctx))...) + } + } + + addCLCFromDep(ctx, module, j.classLoaderContexts) + + // Save away the `deapexer` module on which this depends, if any. + if tag == android.DeapexerTag { + if deapexerModule != nil { + ctx.ModuleErrorf("Ambiguous duplicate deapexer module dependencies %q and %q", + deapexerModule.Name(), module.Name()) } + deapexerModule = module } }) - j.exportedSdkLibs = android.FirstUniqueStrings(j.exportedSdkLibs) if Bool(j.properties.Installable) { ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"), jarName, outputFile) } + + j.exportAidlIncludeDirs = android.PathsForModuleSrc(ctx, j.properties.Aidl.Export_include_dirs) + + if ctx.Device() { + // If this is a variant created for a prebuilt_apex then use the dex implementation jar + // obtained from the associated deapexer module. + ai := ctx.Provider(android.ApexInfoProvider).(android.ApexInfo) + if ai.ForPrebuiltApex { + if deapexerModule == nil { + // This should never happen as a variant for a prebuilt_apex is only created if the + // deapexer module has been configured to export the dex implementation jar for this module. + ctx.ModuleErrorf("internal error: module %q does not depend on a `deapexer` module for prebuilt_apex %q", + j.Name(), ai.ApexVariationName) + return + } + + // Get the path of the dex implementation jar from the `deapexer` module. + di := ctx.OtherModuleProvider(deapexerModule, android.DeapexerProvider).(android.DeapexerInfo) + if dexOutputPath := di.PrebuiltExportPath(apexRootRelativePathToJavaLib(j.BaseModuleName())); dexOutputPath != nil { + j.dexJarFile = dexOutputPath + + // Initialize the hiddenapi structure. + j.initHiddenAPI(ctx, dexOutputPath, outputFile, nil) + } else { + // This should never happen as a variant for a prebuilt_apex is only created if the + // prebuilt_apex has been configured to export the java library dex file. + ctx.ModuleErrorf("internal error: no dex implementation jar available from prebuilt_apex %q", deapexerModule.Name()) + } + } else if Bool(j.dexProperties.Compile_dex) { + sdkDep := decodeSdkDep(ctx, android.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 + + j.dexpreopter.installPath = android.PathForModuleInstall(ctx, "framework", jarName) + if j.dexProperties.Uncompress_dex == nil { + // If the value was not force-set by the user, use reasonable default based on the module. + j.dexProperties.Uncompress_dex = proptools.BoolPtr(shouldUncompressDex(ctx, &j.dexpreopter)) + } + j.dexpreopter.uncompressedDex = *j.dexProperties.Uncompress_dex + + var dexOutputFile android.OutputPath + dexOutputFile = j.dexer.compileDex(ctx, flags, j.MinSdkVersion(ctx), outputFile, jarName) + if ctx.Failed() { + return + } + + // Initialize the hiddenapi structure. + j.initHiddenAPI(ctx, dexOutputFile, outputFile, j.dexProperties.Uncompress_dex) + + // Encode hidden API flags in dex file. + dexOutputFile = j.hiddenAPIEncodeDex(ctx, dexOutputFile) + + j.dexJarFile = dexOutputFile + } + } + + ctx.SetProvider(JavaInfoProvider, JavaInfo{ + HeaderJars: android.PathsIfNonNil(j.combinedClasspathFile), + ImplementationAndResourcesJars: android.PathsIfNonNil(j.combinedClasspathFile), + ImplementationJars: android.PathsIfNonNil(j.combinedClasspathFile), + AidlIncludeDirs: j.exportAidlIncludeDirs, + }) +} + +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 _ Dependency = (*Import)(nil) +var _ android.OutputFileProducer = (*Import)(nil) func (j *Import) HeaderJars() android.Paths { if j.combinedClasspathFile == nil { @@ -2574,46 +1404,73 @@ func (j *Import) HeaderJars() android.Paths { return android.Paths{j.combinedClasspathFile} } -func (j *Import) ImplementationJars() android.Paths { +func (j *Import) ImplementationAndResourcesJars() android.Paths { if j.combinedClasspathFile == nil { return nil } return android.Paths{j.combinedClasspathFile} } -func (j *Import) ResourceJars() android.Paths { +func (j *Import) DexJarBuildPath() android.Path { + return j.dexJarFile +} + +func (j *Import) DexJarInstallPath() android.Path { return nil } -func (j *Import) ImplementationAndResourcesJars() android.Paths { - if j.combinedClasspathFile == nil { - return nil - } - return android.Paths{j.combinedClasspathFile} +func (j *Import) ClassLoaderContexts() dexpreopt.ClassLoaderContextMap { + return j.classLoaderContexts } -func (j *Import) DexJar() android.Path { - return nil +var _ android.ApexModule = (*Import)(nil) + +// Implements android.ApexModule +func (j *Import) DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool { + return j.depIsInSameApex(ctx, dep) } -func (j *Import) AidlIncludeDirs() android.Paths { +// Implements android.ApexModule +func (j *Import) ShouldSupportSdkVersion(ctx android.BaseModuleContext, + sdkVersion android.ApiLevel) error { + sdkSpec := j.MinSdkVersion(ctx) + if !sdkSpec.Specified() { + return fmt.Errorf("min_sdk_version is not specified") + } + if sdkSpec.Kind == android.SdkCore { + return nil + } + ver, err := sdkSpec.EffectiveVersion(ctx) + if err != nil { + return err + } + if ver.GreaterThan(sdkVersion) { + return fmt.Errorf("newer SDK(%v)", ver) + } return nil } -func (j *Import) ExportedSdkLibs() []string { - return j.exportedSdkLibs +// requiredFilesFromPrebuiltApexForImport returns information about the files that a java_import or +// java_sdk_library_import with the specified base module name requires to be exported from a +// prebuilt_apex/apex_set. +func requiredFilesFromPrebuiltApexForImport(name string) []string { + // Add the dex implementation jar to the set of exported files. + return []string{ + apexRootRelativePathToJavaLib(name), + } } -func (j *Import) ExportedPlugins() (android.Paths, []string) { - return nil, nil +// apexRootRelativePathToJavaLib returns the path, relative to the root of the apex's contents, for +// the java library with the specified name. +func apexRootRelativePathToJavaLib(name string) string { + return filepath.Join("javalib", name+".jar") } -func (j *Import) SrcJarArgs() ([]string, android.Paths) { - return nil, nil -} +var _ android.RequiredFilesFromPrebuiltApex = (*Import)(nil) -func (j *Import) DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool { - return j.depIsInSameApex(ctx, dep) +func (j *Import) RequiredFilesFromPrebuiltApex(_ android.BaseModuleContext) []string { + name := j.BaseModuleName() + return requiredFilesFromPrebuiltApexForImport(name) } // Add compile time check for interface implementation @@ -2642,6 +1499,12 @@ func (j *Import) IDECustomizedModuleName() string { var _ android.PrebuiltInterface = (*Import)(nil) +func (j *Import) IsInstallable() bool { + return Bool(j.properties.Installable) +} + +var _ dexpreopterInterface = (*Import)(nil) + // java_import imports one or more `.jar` files into the build graph as if they were built by a java_library module. // // By default, a java_import has a single variant that expects a `.jar` file containing `.class` files that were @@ -2652,9 +1515,14 @@ 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) - module.initModuleAndImport(&module.ModuleBase) + module.dexProperties.Optimize.EnabledByDefault = false android.InitPrebuiltModule(module, &module.properties.Jars) android.InitApexModule(module) @@ -2696,10 +1564,11 @@ type DexImport struct { properties DexImportProperties - dexJarFile android.Path - maybeStrippedDexJarFile android.Path + dexJarFile android.Path dexpreopter + + hideApexVariantFromMake bool } func (j *DexImport) Prebuilt() *android.Prebuilt { @@ -2730,11 +1599,23 @@ func (j *DexImport) IsInstallable() bool { return true } +func (j *DexImport) getStrictUpdatabilityLinting() bool { + return false +} + +func (j *DexImport) setStrictUpdatabilityLinting(bool) { +} + func (j *DexImport) GenerateAndroidBuildActions(ctx android.ModuleContext) { if len(j.properties.Jars) != 1 { 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) @@ -2742,21 +1623,21 @@ func (j *DexImport) GenerateAndroidBuildActions(ctx android.ModuleContext) { dexOutputFile := android.PathForModuleOut(ctx, ctx.ModuleName()+".jar") if j.dexpreopter.uncompressedDex { - rule := android.NewRuleBuilder() + rule := android.NewRuleBuilder(pctx, ctx) temporary := android.PathForModuleOut(ctx, ctx.ModuleName()+".jar.unaligned") rule.Temporary(temporary) // use zip2zip to uncompress classes*.dex files rule.Command(). - BuiltTool(ctx, "zip2zip"). + BuiltTool("zip2zip"). FlagWithInput("-i ", inputJar). FlagWithOutput("-o ", temporary). FlagWithArg("-0 ", "'classes*.dex'") // use zipalign to align uncompressed classes*.dex files rule.Command(). - BuiltTool(ctx, "zipalign"). + BuiltTool("zipalign"). Flag("-f"). Text("4"). Input(temporary). @@ -2764,7 +1645,7 @@ func (j *DexImport) GenerateAndroidBuildActions(ctx android.ModuleContext) { rule.DeleteTemporaryFiles() - rule.Build(pctx, ctx, "uncompress_dex", "uncompress dex") + rule.Build("uncompress_dex", "uncompress dex") } else { ctx.Build(pctx, android.BuildParams{ Rule: android.Cp, @@ -2775,20 +1656,27 @@ func (j *DexImport) GenerateAndroidBuildActions(ctx android.ModuleContext) { j.dexJarFile = dexOutputFile - dexOutputFile = j.dexpreopt(ctx, dexOutputFile) - - j.maybeStrippedDexJarFile = dexOutputFile + j.dexpreopt(ctx, dexOutputFile) - if j.IsForPlatform() { + if apexInfo.IsForPlatform() { ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"), j.Stem()+".jar", dexOutputFile) } } -func (j *DexImport) DexJar() android.Path { +func (j *DexImport) DexJarBuildPath() android.Path { return j.dexJarFile } +var _ android.ApexModule = (*DexImport)(nil) + +// Implements android.ApexModule +func (j *DexImport) ShouldSupportSdkVersion(ctx android.BaseModuleContext, + sdkVersion android.ApiLevel) error { + // we don't check prebuilt modules for sdk_version + return nil +} + // dex_import imports a `.jar` file containing classes.dex files. // // A dex_import module cannot be used as a dependency of a java_* or android_* module, it can only be installed @@ -2844,16 +1732,13 @@ type Defaults struct { // ], // javacflags: ["-Xlint:all"], // } -func defaultsFactory() android.Module { - return DefaultsFactory() -} - func DefaultsFactory() android.Module { module := &Defaults{} module.AddProperties( - &CompilerProperties{}, - &CompilerDeviceProperties{}, + &CommonProperties{}, + &DeviceProperties{}, + &DexProperties{}, &DexpreoptProperties{}, &android.ProtoProperties{}, &aaptProperties{}, @@ -2861,6 +1746,7 @@ func DefaultsFactory() android.Module { &appProperties{}, &appTestProperties{}, &overridableAppProperties{}, + &testProperties{}, &ImportProperties{}, &AARImportProperties{}, &sdkLibraryProperties{}, @@ -2869,6 +1755,7 @@ func DefaultsFactory() android.Module { &android.ApexProperties{}, &RuntimeResourceOverlayProperties{}, &LintProperties{}, + &appTestHelperAppProperties{}, ) android.InitDefaultsModule(module) @@ -2899,3 +1786,54 @@ var Bool = proptools.Bool var BoolDefault = proptools.BoolDefault var String = proptools.String var inList = android.InList + +// Add class loader context (CLC) of a given dependency to the current CLC. +func addCLCFromDep(ctx android.ModuleContext, depModule android.Module, + clcMap dexpreopt.ClassLoaderContextMap) { + + dep, ok := depModule.(UsesLibraryDependency) + if !ok { + return + } + + // Find out if the dependency is either an SDK library or an ordinary library that is disguised + // as an SDK library by the means of `provides_uses_lib` property. If yes, the library is itself + // a <uses-library> and should be added as a node in the CLC tree, and its CLC should be added + // as subtree of that node. Otherwise the library is not a <uses_library> and should not be + // added to CLC, but the transitive <uses-library> dependencies from its CLC should be added to + // the current CLC. + var implicitSdkLib *string + comp, isComp := depModule.(SdkLibraryComponentDependency) + if isComp { + implicitSdkLib = comp.OptionalImplicitSdkLibrary() + // OptionalImplicitSdkLibrary() may be nil so need to fall through to ProvidesUsesLib(). + } + if implicitSdkLib == nil { + if ulib, ok := depModule.(ProvidesUsesLib); ok { + implicitSdkLib = ulib.ProvidesUsesLib() + } + } + + depTag := ctx.OtherModuleDependencyTag(depModule) + if depTag == libTag || depTag == usesLibTag { + // Ok, propagate <uses-library> through non-static library dependencies. + } else if depTag == staticLibTag { + // Propagate <uses-library> through static library dependencies, unless it is a component + // library (such as stubs). Component libraries have a dependency on their SDK library, + // which should not be pulled just because of a static component library. + if implicitSdkLib != nil { + return + } + } else { + // Don't propagate <uses-library> for other dependency tags. + return + } + + if implicitSdkLib != nil { + clcMap.AddContext(ctx, dexpreopt.AnySdkVersion, *implicitSdkLib, + dep.DexJarBuildPath(), dep.DexJarInstallPath(), dep.ClassLoaderContexts()) + } else { + depName := ctx.OtherModuleName(depModule) + clcMap.AddContextMap(dep.ClassLoaderContexts(), depName) + } +} |