12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306 |
- // Copyright 2015 Google Inc. All rights reserved.
- //
- // Licensed under the Apache License, Version 2.0 (the "License");
- // you may not use this file except in compliance with the License.
- // You may obtain a copy of the License at
- //
- // http://www.apache.org/licenses/LICENSE-2.0
- //
- // Unless required by applicable law or agreed to in writing, software
- // distributed under the License is distributed on an "AS IS" BASIS,
- // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- // See the License for the specific language governing permissions and
- // limitations under the License.
- package java
- // This file contains the module types for compiling Java for Android, and converts the properties
- // into the flags and filenames necessary to pass to the Module. The final creation of the rules
- // is handled in builder.go
- 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/java/config"
- "android/soong/tradefed"
- )
- func init() {
- android.RegisterModuleType("java_defaults", defaultsFactory)
- android.RegisterModuleType("java_library", LibraryFactory)
- android.RegisterModuleType("java_library_static", LibraryStaticFactory)
- android.RegisterModuleType("java_library_host", LibraryHostFactory)
- android.RegisterModuleType("java_binary", BinaryFactory)
- android.RegisterModuleType("java_binary_host", BinaryHostFactory)
- android.RegisterModuleType("java_test", TestFactory)
- android.RegisterModuleType("java_test_helper_library", TestHelperLibraryFactory)
- android.RegisterModuleType("java_test_host", TestHostFactory)
- android.RegisterModuleType("java_import", ImportFactory)
- android.RegisterModuleType("java_import_host", ImportFactoryHost)
- android.RegisterModuleType("java_device_for_host", DeviceForHostFactory)
- android.RegisterModuleType("java_host_for_device", HostForDeviceFactory)
- android.RegisterModuleType("dex_import", DexImportFactory)
- android.RegisterSingletonType("logtags", LogtagsSingleton)
- android.RegisterSingletonType("kythe_java_extract", kytheExtractJavaFactory)
- }
- func (j *Module) checkPlatformAPI(ctx android.ModuleContext) {
- if sc, ok := ctx.Module().(sdkContext); ok {
- usePlatformAPI := proptools.Bool(j.deviceProperties.Platform_apis)
- if usePlatformAPI != (sc.sdkVersion() == "") {
- if usePlatformAPI {
- ctx.PropertyErrorf("platform_apis", "platform_apis must be false when sdk_version is not empty.")
- } else {
- 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
- // 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
- Srcs []string `android:"path"`
- // List of javac flags that should only be used when passing -source 1.9
- 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"`
- }
- 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
- }
- // 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
- }
- }
- // 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"`
- }
- // 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
- UncompressDex bool `blueprint:"mutated"`
- IsSDKLibrary bool `blueprint:"mutated"`
- }
- func (me *CompilerDeviceProperties) EffectiveOptimizeEnabled() bool {
- return BoolDefault(me.Optimize.Enabled, me.Optimize.EnabledByDefault)
- }
- // Module contains the properties and members used by all java module types
- type Module struct {
- android.ModuleBase
- android.DefaultableModuleBase
- android.ApexModuleBase
- android.SdkBase
- 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
- // manifest file to use instead of properties.Manifest
- overrideManifest android.OptionalPath
- // list of SDK lib names that this java moudule is exporting
- exportedSdkLibs []string
- // list of source files, collected from srcFiles with uniqie java and all kt files,
- // will be used by android.IDEInfo struct
- expandIDEInfoCompiledSrcs []string
- // expanded Jarjar_rules
- expandJarjarRules android.Path
- // list of additional targets for checkbuild
- additionalCheckedModules android.Paths
- // Extra files generated by the module type to be added as java resources.
- extraResources android.Paths
- hiddenAPI
- dexpreopter
- // list of the xref extraction files
- kytheFiles android.Paths
- }
- 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)
- }
- }
- func (j *Module) DexJarFile() android.Path {
- return j.dexJarFile
- }
- var _ android.OutputFileProducer = (*Module)(nil)
- type Dependency interface {
- HeaderJars() android.Paths
- ImplementationJars() android.Paths
- ResourceJars() android.Paths
- ImplementationAndResourcesJars() android.Paths
- DexJar() android.Path
- AidlIncludeDirs() android.Paths
- ExportedSdkLibs() []string
- SrcJarArgs() ([]string, android.Paths)
- BaseModuleName() string
- }
- type SdkLibraryDependency interface {
- SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion string) android.Paths
- SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion string) android.Paths
- }
- type xref interface {
- XrefJavaFiles() android.Paths
- }
- 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 {
- blueprint.BaseDependencyTag
- target android.Target
- }
- var (
- staticLibTag = dependencyTag{name: "staticlib"}
- libTag = dependencyTag{name: "javalib"}
- pluginTag = dependencyTag{name: "plugin"}
- bootClasspathTag = dependencyTag{name: "bootclasspath"}
- systemModulesTag = dependencyTag{name: "system modules"}
- frameworkResTag = dependencyTag{name: "framework-res"}
- frameworkApkTag = dependencyTag{name: "framework-apk"}
- 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"}
- )
- func defaultSdkVersion(ctx checkVendorModuleContext) string {
- if ctx.SocSpecific() || ctx.DeviceSpecific() {
- return "system_current"
- }
- return ""
- }
- type checkVendorModuleContext interface {
- SocSpecific() bool
- DeviceSpecific() bool
- }
- type sdkDep struct {
- useModule, useFiles, useDefaultLibs, invalidVersion bool
- modules []string
- // The default system modules to use. Will be an empty string if no system
- // modules are to be used.
- systemModules string
- frameworkResModule string
- jars android.Paths
- aidl android.OptionalPath
- noStandardLibs, noFrameworksLibs bool
- }
- func (s sdkDep) hasStandardLibs() bool {
- return !s.noStandardLibs
- }
- func (s sdkDep) hasFrameworkLibs() bool {
- return !s.noStandardLibs && !s.noFrameworksLibs
- }
- type jniLib struct {
- name string
- path android.Path
- target android.Target
- }
- func (j *Module) shouldInstrument(ctx android.BaseModuleContext) bool {
- return j.properties.Instrument && ctx.Config().IsEnvTrue("EMMA_INSTRUMENT")
- }
- 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() string {
- return proptools.StringDefault(j.deviceProperties.Sdk_version, defaultSdkVersion(j))
- }
- func (j *Module) systemModules() string {
- return proptools.String(j.deviceProperties.System_modules)
- }
- func (j *Module) minSdkVersion() string {
- if j.deviceProperties.Min_sdk_version != nil {
- return *j.deviceProperties.Min_sdk_version
- }
- return j.sdkVersion()
- }
- func (j *Module) targetSdkVersion() string {
- if j.deviceProperties.Target_sdk_version != nil {
- return *j.deviceProperties.Target_sdk_version
- }
- return j.sdkVersion()
- }
- func (j *Module) deps(ctx android.BottomUpMutatorContext) {
- if ctx.Device() {
- sdkDep := decodeSdkDep(ctx, sdkContext(j))
- if sdkDep.hasStandardLibs() {
- 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, systemModulesTag, sdkDep.systemModules)
- ctx.AddVariationDependencies(nil, bootClasspathTag, sdkDep.modules...)
- if j.deviceProperties.EffectiveOptimizeEnabled() {
- ctx.AddVariationDependencies(nil, proguardRaiseTag, config.DefaultBootclasspathLibraries...)
- ctx.AddVariationDependencies(nil, proguardRaiseTag, config.DefaultLibraries...)
- }
- }
- } else if sdkDep.systemModules != "" {
- // Add the system modules to both the system modules and bootclasspath.
- ctx.AddVariationDependencies(nil, systemModulesTag, sdkDep.systemModules)
- ctx.AddVariationDependencies(nil, bootClasspathTag, sdkDep.systemModules)
- }
- if ctx.ModuleName() == "android_stubs_current" ||
- ctx.ModuleName() == "android_system_stubs_current" ||
- ctx.ModuleName() == "android_test_stubs_current" {
- ctx.AddVariationDependencies(nil, frameworkApkTag, "framework-res")
- }
- }
- ctx.AddVariationDependencies(nil, libTag, j.properties.Libs...)
- ctx.AddVariationDependencies(nil, staticLibTag, j.properties.Static_libs...)
- ctx.AddFarVariationDependencies(ctx.Config().BuildOSCommonTarget.Variations(), pluginTag, j.properties.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")
- }
- }
- 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
- }
- }
- 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
- 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
- disableTurbine bool
- }
- func checkProducesJars(ctx android.ModuleContext, dep android.SourceFileProducer) {
- for _, f := range dep.Srcs() {
- if f.Ext() != ".jar" {
- ctx.ModuleErrorf("genrule %q must generate files ending with .jar to be used as a libs or static_libs dependency",
- ctx.OtherModuleName(dep.(blueprint.Module)))
- }
- }
- }
- type linkType int
- const (
- javaCore linkType = iota
- javaSdk
- javaSystem
- javaPlatform
- )
- func getLinkType(m *Module, name string) (ret linkType, stubs bool) {
- ver := m.sdkVersion()
- switch {
- case name == "core.current.stubs" || name == "core.platform.api.stubs" ||
- name == "stub-annotations" || name == "private-stub-annotations-jar" ||
- name == "core-lambda-stubs" || name == "core-generated-annotation-stubs":
- return javaCore, true
- case ver == "core_current":
- return javaCore, false
- case name == "android_system_stubs_current":
- return javaSystem, true
- case strings.HasPrefix(ver, "system_"):
- return javaSystem, false
- case name == "android_test_stubs_current":
- return javaSystem, true
- case strings.HasPrefix(ver, "test_"):
- return javaPlatform, false
- case name == "android_stubs_current":
- return javaSdk, true
- case ver == "current":
- return javaSdk, false
- case ver == "" || ver == "none" || ver == "core_platform":
- return javaPlatform, false
- default:
- if _, err := strconv.Atoi(ver); err != nil {
- panic(fmt.Errorf("expected sdk_version to be a number, got %q", ver))
- }
- return javaSdk, false
- }
- }
- func checkLinkType(ctx android.ModuleContext, from *Module, to *Library, tag dependencyTag) {
- if ctx.Host() {
- return
- }
- myLinkType, stubs := getLinkType(from, ctx.ModuleName())
- if stubs {
- return
- }
- otherLinkType, _ := getLinkType(&to.Module, 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 {
- ctx.ModuleErrorf("compiles against system 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.modules)
- } 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
- }
- }
- 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
- }
- if to, ok := module.(*Library); ok {
- switch tag {
- case bootClasspathTag, libTag, staticLibTag:
- checkLinkType(ctx, j, to, tag.(dependencyTag))
- }
- }
- 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, otherName)
- 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()...)
- 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()...)
- case pluginTag:
- if plugin, ok := dep.(*Plugin); ok {
- deps.processorPath = append(deps.processorPath, dep.ImplementationAndResourcesJars()...)
- if plugin.pluginProperties.Processor_class != nil {
- deps.processorClasses = append(deps.processorClasses, *plugin.pluginProperties.Processor_class)
- }
- deps.disableTurbine = deps.disableTurbine || Bool(plugin.pluginProperties.Generates_api)
- } else {
- ctx.PropertyErrorf("plugins", "%q is not a java_plugin module", otherName)
- }
- case frameworkApkTag:
- if ctx.ModuleName() == "android_stubs_current" ||
- ctx.ModuleName() == "android_system_stubs_current" ||
- ctx.ModuleName() == "android_test_stubs_current" {
- // framework stubs.jar need to depend on framework-res.apk, in order to pull the
- // resource files out of there for aapt.
- //
- // Normally the package rule runs aapt, which includes the resource,
- // but we're not running that in our package rule so just copy in the
- // resource files here.
- deps.staticResourceJars = append(deps.staticResourceJars, dep.(*AndroidApp).exportPackage)
- }
- 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.(*SystemModules)
- deps.bootClasspath = append(deps.bootClasspath, sm.headerJars...)
- case systemModulesTag:
- if deps.systemModules != nil {
- panic("Found two system module dependencies")
- }
- sm := module.(*SystemModules)
- if sm.outputDir == nil || len(sm.outputDeps) == 0 {
- panic("Missing directory for system module dependency")
- }
- deps.systemModules = &systemModules{sm.outputDir, sm.outputDeps}
- }
- }
- })
- j.exportedSdkLibs = android.FirstUniqueStrings(j.exportedSdkLibs)
- return deps
- }
- func getJavaVersion(ctx android.ModuleContext, javaVersion string, sdkContext sdkContext) string {
- var ret string
- v := sdkContext.sdkVersion()
- // For PDK builds, use the latest SDK version instead of "current"
- if ctx.Config().IsPdkBuild() &&
- (v == "" || v == "none" || v == "core_platform" || v == "current") {
- sdkVersions := ctx.Config().Get(sdkVersionsKey).([]int)
- latestSdkVersion := 0
- if len(sdkVersions) > 0 {
- latestSdkVersion = sdkVersions[len(sdkVersions)-1]
- }
- v = strconv.Itoa(latestSdkVersion)
- }
- sdk, err := sdkVersionToNumber(ctx, v)
- if err != nil {
- ctx.PropertyErrorf("sdk_version", "%s", err)
- }
- if javaVersion != "" {
- ret = normalizeJavaVersion(ctx, javaVersion)
- } else if ctx.Device() && sdk <= 23 {
- ret = "1.7"
- } else if ctx.Device() && sdk <= 29 || !ctx.Config().TargetOpenJDK9() {
- ret = "1.8"
- } else if ctx.Device() &&
- sdkContext.sdkVersion() != "" &&
- sdkContext.sdkVersion() != "none" &&
- sdkContext.sdkVersion() != "core_platform" &&
- sdk == android.FutureApiLevel {
- // TODO(ccross): once we generate stubs we should be able to use 1.9 for sdk_version: "current"
- ret = "1.8"
- } else {
- ret = "1.9"
- }
- return ret
- }
- func normalizeJavaVersion(ctx android.ModuleContext, javaVersion string) string {
- switch javaVersion {
- case "1.6", "6":
- return "1.6"
- case "1.7", "7":
- return "1.7"
- case "1.8", "8":
- return "1.8"
- case "1.9", "9":
- return "1.9"
- case "10", "11":
- ctx.PropertyErrorf("java_version", "Java language levels above 9 are not supported")
- return "unsupported"
- default:
- ctx.PropertyErrorf("java_version", "Unrecognized Java language level")
- return "unrecognized"
- }
- }
- 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 == "1.9" {
- 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")
- }
- 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.processorPath = append(flags.processorPath, deps.processorPath...)
- flags.processor = strings.Join(deps.processorClasses, ",")
- if len(flags.bootClasspath) == 0 && ctx.Host() && flags.javaVersion != "1.9" &&
- decodeSdkDep(ctx, sdkContext(j)).hasStandardLibs() &&
- inList(flags.javaVersion, []string{"1.6", "1.7", "1.8"}) {
- // 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 == "1.9" {
- // 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 == "1.9" {
- 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")
- kotlinKapt(ctx, kaptSrcJar, kotlinSrcFiles, srcJars, flags)
- srcJars = append(srcJars, kaptSrcJar)
- // Disable annotation processing in javac, it's already been handled by kapt
- flags.processorPath = nil
- flags.processor = ""
- }
- 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)
- // Jar kotlin classes into the final jar after javac
- kotlinJars = append(kotlinJars, kotlinJar)
- 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
- 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.
- }
- 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, j.headerJarFile)
- 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)
- }
- ctx.Build(pctx, android.BuildParams{
- Rule: zip,
- Output: servicesJar,
- Implicits: services,
- Args: map[string]string{
- "jarArgs": "-P META-INF/services/ " + strings.Join(proptools.NinjaAndShellEscapeList(zipargs), " "),
- },
- })
- 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
- }
- if ctx.Config().IsEnvTrue("EMMA_INSTRUMENT_FRAMEWORK") {
- if inList(ctx.ModuleName(), config.InstrumentFrameworkModules) {
- 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
- 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
- }
- // Hidden API CSV generation and dex encoding
- dexOutputFile = j.hiddenAPI.hiddenAPI(ctx, dexOutputFile, j.implementationJarFile,
- j.deviceProperties.UncompressDex)
- // 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.UncompressDex {
- 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
- }
- 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) 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
- }
- 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.
- var headerJar android.Path
- 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
- if j.expandJarjarRules != nil {
- // Transform classes.jar into classes-jarjar.jar
- jarjarFile := android.PathForModuleOut(ctx, "turbine-jarjar", jarName)
- TransformJarJar(ctx, jarjarFile, headerJar, j.expandJarjarRules)
- headerJar = jarjarFile
- if ctx.Failed() {
- return nil
- }
- }
- return headerJar
- }
- 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) 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
- }
- //
- // Java libraries (.jar file)
- //
- type Library struct {
- Module
- InstallMixin func(ctx android.ModuleContext, installPath android.Path) (extraInstallDeps android.Paths)
- }
- func shouldUncompressDex(ctx android.ModuleContext, dexpreopter *dexpreopter) bool {
- // Store uncompressed (and do not strip) dex files from boot class path jars.
- if inList(ctx.ModuleName(), ctx.Config().BootJars()) {
- return true
- }
- // Store uncompressed dex files that are preopted on /system.
- if !dexpreopter.dexpreoptDisabled(ctx) && (ctx.Host() || !odexOnSystemOther(ctx, dexpreopter.installPath)) {
- return true
- }
- if ctx.Config().UncompressPrivAppDex() &&
- inList(ctx.ModuleName(), ctx.Config().ModulesLoadedByPrivilegedModules()) {
- return true
- }
- return false
- }
- func (j *Library) GenerateAndroidBuildActions(ctx android.ModuleContext) {
- j.dexpreopter.installPath = android.PathForModuleInstall(ctx, "framework", ctx.ModuleName()+".jar")
- j.dexpreopter.isSDKLibrary = j.deviceProperties.IsSDKLibrary
- j.dexpreopter.isInstallable = Bool(j.properties.Installable)
- j.dexpreopter.uncompressedDex = shouldUncompressDex(ctx, &j.dexpreopter)
- j.deviceProperties.UncompressDex = j.dexpreopter.uncompressedDex
- j.compile(ctx, nil)
- exclusivelyForApex := android.InAnyApex(ctx.ModuleName()) && !j.IsForPlatform()
- if (Bool(j.properties.Installable) || ctx.Host()) && !exclusivelyForApex {
- var extraInstallDeps android.Paths
- if j.InstallMixin != nil {
- extraInstallDeps = j.InstallMixin(ctx, j.outputFile)
- }
- j.installFile = ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
- ctx.ModuleName()+".jar", j.outputFile, extraInstallDeps...)
- }
- }
- func (j *Library) DepsMutator(ctx android.BottomUpMutatorContext) {
- j.deps(ctx)
- }
- // 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
- // compiled against the device bootclasspath. This jar is not suitable for installing on a device, but can be used
- // as a `static_libs` dependency of another module.
- //
- // Specifying `installable: true` will product a `.jar` file containing `classes.dex` files, suitable for installing on
- // a device.
- //
- // Specifying `host_supported: true` will produce two variants, one compiled against the device bootclasspath and one
- // compiled against the host bootclasspath.
- func LibraryFactory() android.Module {
- module := &Library{}
- module.AddProperties(
- &module.Module.properties,
- &module.Module.deviceProperties,
- &module.Module.dexpreoptProperties,
- &module.Module.protoProperties)
- InitJavaModule(module, android.HostAndDeviceSupported)
- android.InitApexModule(module)
- android.InitSdkAwareModule(module)
- return module
- }
- // java_library_static is an obsolete alias for java_library.
- func LibraryStaticFactory() android.Module {
- return LibraryFactory()
- }
- // java_library_host builds and links sources into a `.jar` file for the host.
- //
- // A java_library_host has a single variant that produces a `.jar` file containing `.class` files that were
- // compiled against the host bootclasspath.
- func LibraryHostFactory() android.Module {
- module := &Library{}
- module.AddProperties(
- &module.Module.properties,
- &module.Module.protoProperties)
- module.Module.properties.Installable = proptools.BoolPtr(true)
- InitJavaModule(module, android.HostSupported)
- android.InitApexModule(module)
- return module
- }
- //
- // Java Tests
- //
- type testProperties struct {
- // list of compatibility suites (for example "cts", "vts") that the module should be
- // installed into.
- Test_suites []string `android:"arch_variant"`
- // the name of the test configuration (for example "AndroidTest.xml") that should be
- // installed with the module.
- Test_config *string `android:"path,arch_variant"`
- // the name of the test configuration template (for example "AndroidTestTemplate.xml") that
- // should be installed with the module.
- Test_config_template *string `android:"path,arch_variant"`
- // list of files or filegroup modules that provide data that should be installed alongside
- // the test
- Data []string `android:"path"`
- // Flag to indicate whether or not to create test config automatically. If AndroidTest.xml
- // doesn't exist next to the Android.bp, this attribute doesn't need to be set to true
- // explicitly.
- Auto_gen_config *bool
- }
- type testHelperLibraryProperties struct {
- // list of compatibility suites (for example "cts", "vts") that the module should be
- // installed into.
- Test_suites []string `android:"arch_variant"`
- }
- type Test struct {
- Library
- testProperties testProperties
- testConfig android.Path
- data android.Paths
- }
- type TestHelperLibrary struct {
- Library
- testHelperLibraryProperties testHelperLibraryProperties
- }
- func (j *Test) GenerateAndroidBuildActions(ctx android.ModuleContext) {
- j.testConfig = tradefed.AutoGenJavaTestConfig(ctx, j.testProperties.Test_config, j.testProperties.Test_config_template,
- j.testProperties.Test_suites, j.testProperties.Auto_gen_config)
- j.data = android.PathsForModuleSrc(ctx, j.testProperties.Data)
- j.Library.GenerateAndroidBuildActions(ctx)
- }
- func (j *TestHelperLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
- j.Library.GenerateAndroidBuildActions(ctx)
- }
- // java_test builds a and links sources into a `.jar` file for the device, and possibly for the host as well, and
- // creates an `AndroidTest.xml` file to allow running the test with `atest` or a `TEST_MAPPING` file.
- //
- // By default, a java_test has a single variant that produces a `.jar` file containing `classes.dex` files that were
- // compiled against the device bootclasspath.
- //
- // Specifying `host_supported: true` will produce two variants, one compiled against the device bootclasspath and one
- // compiled against the host bootclasspath.
- func TestFactory() android.Module {
- module := &Test{}
- module.AddProperties(
- &module.Module.properties,
- &module.Module.deviceProperties,
- &module.Module.dexpreoptProperties,
- &module.Module.protoProperties,
- &module.testProperties)
- module.Module.properties.Installable = proptools.BoolPtr(true)
- module.Module.dexpreopter.isTest = true
- InitJavaModule(module, android.HostAndDeviceSupported)
- return module
- }
- // java_test_helper_library creates a java library and makes sure that it is added to the appropriate test suite.
- func TestHelperLibraryFactory() android.Module {
- module := &TestHelperLibrary{}
- module.AddProperties(
- &module.Module.properties,
- &module.Module.deviceProperties,
- &module.Module.dexpreoptProperties,
- &module.Module.protoProperties,
- &module.testHelperLibraryProperties)
- module.Module.properties.Installable = proptools.BoolPtr(true)
- module.Module.dexpreopter.isTest = true
- InitJavaModule(module, android.HostAndDeviceSupported)
- return module
- }
- // java_test_host builds a and links sources into a `.jar` file for the host, and creates an `AndroidTest.xml` file to
- // allow running the test with `atest` or a `TEST_MAPPING` file.
- //
- // 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.AddProperties(
- &module.Module.properties,
- &module.Module.protoProperties,
- &module.testProperties)
- module.Module.properties.Installable = proptools.BoolPtr(true)
- InitJavaModule(module, android.HostSupported)
- return module
- }
- //
- // Java Binaries (.jar file plus wrapper script)
- //
- type binaryProperties struct {
- // installable script to execute the resulting jar
- Wrapper *string `android:"path"`
- // Name of the class containing main to be inserted into the manifest as Main-Class.
- Main_class *string
- }
- type Binary struct {
- Library
- binaryProperties binaryProperties
- isWrapperVariant bool
- wrapperFile android.Path
- binaryFile android.InstallPath
- }
- func (j *Binary) HostToolPath() android.OptionalPath {
- return android.OptionalPathForPath(j.binaryFile)
- }
- func (j *Binary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
- if ctx.Arch().ArchType == android.Common {
- // Compile the jar
- if j.binaryProperties.Main_class != nil {
- if j.properties.Manifest != nil {
- ctx.PropertyErrorf("main_class", "main_class cannot be used when manifest is set")
- }
- manifestFile := android.PathForModuleOut(ctx, "manifest.txt")
- GenerateMainClassManifest(ctx, manifestFile, String(j.binaryProperties.Main_class))
- j.overrideManifest = android.OptionalPathForPath(manifestFile)
- }
- j.Library.GenerateAndroidBuildActions(ctx)
- } else {
- // Handle the binary wrapper
- j.isWrapperVariant = true
- if j.binaryProperties.Wrapper != nil {
- j.wrapperFile = android.PathForModuleSrc(ctx, *j.binaryProperties.Wrapper)
- } else {
- 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
- j.binaryFile = ctx.InstallExecutable(android.PathForModuleInstall(ctx, "bin"),
- ctx.ModuleName(), j.wrapperFile, jarFile)
- }
- }
- func (j *Binary) DepsMutator(ctx android.BottomUpMutatorContext) {
- if ctx.Arch().ArchType == android.Common {
- j.deps(ctx)
- }
- }
- // java_binary builds a `.jar` file and a shell script that executes it for the device, and possibly for the host
- // as well.
- //
- // By default, a java_binary has a single variant that produces a `.jar` file containing `classes.dex` files that were
- // compiled against the device bootclasspath.
- //
- // Specifying `host_supported: true` will produce two variants, one compiled against the device bootclasspath and one
- // compiled against the host bootclasspath.
- func BinaryFactory() android.Module {
- module := &Binary{}
- module.AddProperties(
- &module.Module.properties,
- &module.Module.deviceProperties,
- &module.Module.dexpreoptProperties,
- &module.Module.protoProperties,
- &module.binaryProperties)
- module.Module.properties.Installable = proptools.BoolPtr(true)
- android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibCommonFirst)
- android.InitDefaultableModule(module)
- return module
- }
- // java_binary_host builds a `.jar` file and a shell script that executes it for the host.
- //
- // A java_binary_host has a single variant that produces a `.jar` file containing `.class` files that were
- // compiled against the host bootclasspath.
- func BinaryHostFactory() android.Module {
- module := &Binary{}
- module.AddProperties(
- &module.Module.properties,
- &module.Module.protoProperties,
- &module.binaryProperties)
- module.Module.properties.Installable = proptools.BoolPtr(true)
- android.InitAndroidArchModule(module, android.HostSupported, android.MultilibCommonFirst)
- android.InitDefaultableModule(module)
- return module
- }
- //
- // Java prebuilts
- //
- type ImportProperties struct {
- Jars []string `android:"path"`
- Sdk_version *string
- Installable *bool
- // List of shared java libs that this module has dependencies to
- Libs []string
- // List of files to remove from the jar file(s)
- Exclude_files []string
- // List of directories to remove from the jar file(s)
- Exclude_dirs []string
- // if set to true, run Jetifier against .jar file. Defaults to false.
- Jetifier *bool
- }
- type Import struct {
- android.ModuleBase
- android.DefaultableModuleBase
- android.ApexModuleBase
- prebuilt android.Prebuilt
- android.SdkBase
- properties ImportProperties
- combinedClasspathFile android.Path
- exportedSdkLibs []string
- }
- func (j *Import) sdkVersion() string {
- return proptools.StringDefault(j.properties.Sdk_version, defaultSdkVersion(j))
- }
- func (j *Import) minSdkVersion() string {
- return j.sdkVersion()
- }
- func (j *Import) Prebuilt() *android.Prebuilt {
- return &j.prebuilt
- }
- func (j *Import) PrebuiltSrcs() []string {
- return j.properties.Jars
- }
- func (j *Import) Name() string {
- return j.prebuilt.Name(j.ModuleBase.Name())
- }
- func (j *Import) DepsMutator(ctx android.BottomUpMutatorContext) {
- ctx.AddVariationDependencies(nil, libTag, j.properties.Libs...)
- }
- func (j *Import) GenerateAndroidBuildActions(ctx android.ModuleContext) {
- jars := android.PathsForModuleSrc(ctx, j.properties.Jars)
- jarName := ctx.ModuleName() + ".jar"
- outputFile := android.PathForModuleOut(ctx, "combined", jarName)
- TransformJarsToJar(ctx, outputFile, "for prebuilts", jars, android.OptionalPath{},
- false, j.properties.Exclude_files, j.properties.Exclude_dirs)
- if Bool(j.properties.Jetifier) {
- inputFile := outputFile
- outputFile = android.PathForModuleOut(ctx, "jetifier", jarName)
- TransformJetifier(ctx, outputFile, inputFile)
- }
- j.combinedClasspathFile = outputFile
- ctx.VisitDirectDeps(func(module android.Module) {
- otherName := ctx.OtherModuleName(module)
- tag := ctx.OtherModuleDependencyTag(module)
- switch dep := module.(type) {
- case Dependency:
- switch tag {
- case libTag, staticLibTag:
- // sdk lib names from dependencies are re-exported
- j.exportedSdkLibs = append(j.exportedSdkLibs, dep.ExportedSdkLibs()...)
- }
- case SdkLibraryDependency:
- switch tag {
- case libTag:
- // names of sdk libs that are directly depended are exported
- j.exportedSdkLibs = append(j.exportedSdkLibs, otherName)
- }
- }
- })
- j.exportedSdkLibs = android.FirstUniqueStrings(j.exportedSdkLibs)
- if Bool(j.properties.Installable) {
- ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
- ctx.ModuleName()+".jar", outputFile)
- }
- }
- var _ Dependency = (*Import)(nil)
- func (j *Import) HeaderJars() android.Paths {
- if j.combinedClasspathFile == nil {
- return nil
- }
- return android.Paths{j.combinedClasspathFile}
- }
- func (j *Import) ImplementationJars() android.Paths {
- if j.combinedClasspathFile == nil {
- return nil
- }
- return android.Paths{j.combinedClasspathFile}
- }
- func (j *Import) ResourceJars() android.Paths {
- return nil
- }
- func (j *Import) ImplementationAndResourcesJars() android.Paths {
- if j.combinedClasspathFile == nil {
- return nil
- }
- return android.Paths{j.combinedClasspathFile}
- }
- func (j *Import) DexJar() android.Path {
- return nil
- }
- func (j *Import) AidlIncludeDirs() android.Paths {
- return nil
- }
- func (j *Import) ExportedSdkLibs() []string {
- return j.exportedSdkLibs
- }
- func (j *Import) SrcJarArgs() ([]string, android.Paths) {
- return nil, nil
- }
- // Add compile time check for interface implementation
- var _ android.IDEInfo = (*Import)(nil)
- var _ android.IDECustomizedModuleName = (*Import)(nil)
- // Collect information for opening IDE project files in java/jdeps.go.
- const (
- removedPrefix = "prebuilt_"
- )
- func (j *Import) IDEInfo(dpInfo *android.IdeInfo) {
- dpInfo.Jars = append(dpInfo.Jars, j.PrebuiltSrcs()...)
- }
- func (j *Import) IDECustomizedModuleName() string {
- // TODO(b/113562217): Extract the base module name from the Import name, often the Import name
- // has a prefix "prebuilt_". Remove the prefix explicitly if needed until we find a better
- // solution to get the Import name.
- name := j.Name()
- if strings.HasPrefix(name, removedPrefix) {
- name = strings.TrimPrefix(name, removedPrefix)
- }
- return name
- }
- var _ android.PrebuiltInterface = (*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
- // compiled against an Android classpath.
- //
- // Specifying `host_supported: true` will produce two variants, one for use as a dependency of device modules and one
- // for host modules.
- func ImportFactory() android.Module {
- module := &Import{}
- module.AddProperties(&module.properties)
- android.InitPrebuiltModule(module, &module.properties.Jars)
- InitJavaModule(module, android.HostAndDeviceSupported)
- android.InitApexModule(module)
- android.InitSdkAwareModule(module)
- return module
- }
- // java_import imports one or more `.jar` files into the build graph as if they were built by a java_library_host
- // module.
- //
- // A java_import_host has a single variant that expects a `.jar` file containing `.class` files that were
- // compiled against a host bootclasspath.
- func ImportFactoryHost() android.Module {
- module := &Import{}
- module.AddProperties(&module.properties)
- android.InitPrebuiltModule(module, &module.properties.Jars)
- InitJavaModule(module, android.HostSupported)
- android.InitApexModule(module)
- return module
- }
- // dex_import module
- type DexImportProperties struct {
- Jars []string `android:"path"`
- }
- type DexImport struct {
- android.ModuleBase
- android.DefaultableModuleBase
- android.ApexModuleBase
- prebuilt android.Prebuilt
- properties DexImportProperties
- dexJarFile android.Path
- maybeStrippedDexJarFile android.Path
- dexpreopter
- }
- func (j *DexImport) Prebuilt() *android.Prebuilt {
- return &j.prebuilt
- }
- func (j *DexImport) PrebuiltSrcs() []string {
- return j.properties.Jars
- }
- func (j *DexImport) Name() string {
- return j.prebuilt.Name(j.ModuleBase.Name())
- }
- func (j *DexImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
- if len(j.properties.Jars) != 1 {
- ctx.PropertyErrorf("jars", "exactly one jar must be provided")
- }
- j.dexpreopter.installPath = android.PathForModuleInstall(ctx, "framework", ctx.ModuleName()+".jar")
- j.dexpreopter.isInstallable = true
- j.dexpreopter.uncompressedDex = shouldUncompressDex(ctx, &j.dexpreopter)
- inputJar := ctx.ExpandSource(j.properties.Jars[0], "jars")
- dexOutputFile := android.PathForModuleOut(ctx, ctx.ModuleName()+".jar")
- if j.dexpreopter.uncompressedDex {
- rule := android.NewRuleBuilder()
- temporary := android.PathForModuleOut(ctx, ctx.ModuleName()+".jar.unaligned")
- rule.Temporary(temporary)
- // use zip2zip to uncompress classes*.dex files
- rule.Command().
- BuiltTool(ctx, "zip2zip").
- FlagWithInput("-i ", inputJar).
- FlagWithOutput("-o ", temporary).
- FlagWithArg("-0 ", "'classes*.dex'")
- // use zipalign to align uncompressed classes*.dex files
- rule.Command().
- BuiltTool(ctx, "zipalign").
- Flag("-f").
- Text("4").
- Input(temporary).
- Output(dexOutputFile)
- rule.DeleteTemporaryFiles()
- rule.Build(pctx, ctx, "uncompress_dex", "uncompress dex")
- } else {
- ctx.Build(pctx, android.BuildParams{
- Rule: android.Cp,
- Input: inputJar,
- Output: dexOutputFile,
- })
- }
- j.dexJarFile = dexOutputFile
- dexOutputFile = j.dexpreopt(ctx, dexOutputFile)
- j.maybeStrippedDexJarFile = dexOutputFile
- ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
- ctx.ModuleName()+".jar", dexOutputFile)
- }
- func (j *DexImport) DexJar() android.Path {
- return j.dexJarFile
- }
- // 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
- // to the device.
- func DexImportFactory() android.Module {
- module := &DexImport{}
- module.AddProperties(&module.properties)
- android.InitPrebuiltModule(module, &module.properties.Jars)
- InitJavaModule(module, android.DeviceSupported)
- android.InitApexModule(module)
- return module
- }
- //
- // Defaults
- //
- type Defaults struct {
- android.ModuleBase
- android.DefaultsModuleBase
- android.ApexModuleBase
- }
- // java_defaults provides a set of properties that can be inherited by other java or android modules.
- //
- // A module can use the properties from a java_defaults module using `defaults: ["defaults_module_name"]`. Each
- // property in the defaults module that exists in the depending module will be prepended to the depending module's
- // value for that property.
- //
- // Example:
- //
- // java_defaults {
- // name: "example_defaults",
- // srcs: ["common/**/*.java"],
- // javacflags: ["-Xlint:all"],
- // aaptflags: ["--auto-add-overlay"],
- // }
- //
- // java_library {
- // name: "example",
- // defaults: ["example_defaults"],
- // srcs: ["example/**/*.java"],
- // }
- //
- // is functionally identical to:
- //
- // java_library {
- // name: "example",
- // srcs: [
- // "common/**/*.java",
- // "example/**/*.java",
- // ],
- // javacflags: ["-Xlint:all"],
- // }
- func defaultsFactory() android.Module {
- return DefaultsFactory()
- }
- func DefaultsFactory(props ...interface{}) android.Module {
- module := &Defaults{}
- module.AddProperties(props...)
- module.AddProperties(
- &CompilerProperties{},
- &CompilerDeviceProperties{},
- &DexpreoptProperties{},
- &android.ProtoProperties{},
- &aaptProperties{},
- &androidLibraryProperties{},
- &appProperties{},
- &appTestProperties{},
- &overridableAppProperties{},
- &ImportProperties{},
- &AARImportProperties{},
- &sdkLibraryProperties{},
- &DexImportProperties{},
- )
- android.InitDefaultsModule(module)
- android.InitApexModule(module)
- return module
- }
- func kytheExtractJavaFactory() android.Singleton {
- return &kytheExtractJavaSingleton{}
- }
- type kytheExtractJavaSingleton struct {
- }
- func (ks *kytheExtractJavaSingleton) GenerateBuildActions(ctx android.SingletonContext) {
- var xrefTargets android.Paths
- ctx.VisitAllModules(func(module android.Module) {
- if javaModule, ok := module.(xref); ok {
- xrefTargets = append(xrefTargets, javaModule.XrefJavaFiles()...)
- }
- })
- // TODO(asmundak): perhaps emit a rule to output a warning if there were no xrefTargets
- if len(xrefTargets) > 0 {
- ctx.Build(pctx, android.BuildParams{
- Rule: blueprint.Phony,
- Output: android.PathForPhony(ctx, "xref_java"),
- Inputs: xrefTargets,
- })
- }
- }
- var Bool = proptools.Bool
- var BoolDefault = proptools.BoolDefault
- var String = proptools.String
- var inList = android.InList
|