// Copyright 2015 The Bazel Authors. 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 com.google.devtools.build.lib.rules.android; import static com.google.devtools.build.lib.packages.Attribute.attr; import static com.google.devtools.build.lib.packages.BuildType.LABEL_LIST; import static com.google.devtools.build.lib.packages.BuildType.TRISTATE; import static com.google.devtools.build.lib.syntax.Type.BOOLEAN; import static com.google.devtools.build.lib.syntax.Type.STRING; import com.google.devtools.build.lib.analysis.RuleDefinition; import com.google.devtools.build.lib.analysis.RuleDefinitionEnvironment; import com.google.devtools.build.lib.analysis.config.HostTransition; import com.google.devtools.build.lib.packages.RuleClass; import com.google.devtools.build.lib.packages.RuleClass.Builder.RuleClassType; import com.google.devtools.build.lib.packages.SkylarkProviderIdentifier; import com.google.devtools.build.lib.packages.TriState; import com.google.devtools.build.lib.rules.android.AndroidRuleClasses.AndroidResourceSupportRule; import com.google.devtools.build.lib.rules.java.JavaConfiguration; import com.google.devtools.build.lib.rules.java.JavaInfo; import com.google.devtools.build.lib.rules.java.JavaRuleClasses; import com.google.devtools.build.lib.rules.java.JavaSemantics; import com.google.devtools.build.lib.rules.java.ProguardLibraryRule; import com.google.devtools.build.lib.util.FileTypeSet; /** Rule definition for the android_library rule. */ public final class AndroidLibraryBaseRule implements RuleDefinition { private final AndroidNeverlinkAspect androidNeverlinkAspect; public AndroidLibraryBaseRule(AndroidNeverlinkAspect androidNeverlinkAspect) { this.androidNeverlinkAspect = androidNeverlinkAspect; } @Override public RuleClass build(RuleClass.Builder builder, final RuleDefinitionEnvironment env) { return builder .requiresConfigurationFragments(JavaConfiguration.class, AndroidConfiguration.class) /* The list of .java or .srcjar files that are processed to create the target.

srcs files of type .java are compiled. For readability's sake, it is not good to put the name of a generated .java source file into the srcs. Instead, put the depended-on rule name in the srcs, as described below.

srcs files of type .srcjar are unpacked and compiled. (This is useful if you need to generate a set of .java files with a genrule or build extension.)

This rule currently forces source and class compatibility with Java 7, although try with resources is not supported.

If srcs is omitted, then any dependency specified in deps is exported from this rule (see java_library's exports for more information about exporting dependencies). However, this behavior will be deprecated soon; try not to rely on it.

*/ .add( attr("srcs", LABEL_LIST) .direct_compile_time_input() .allowedFileTypes(JavaSemantics.JAVA_SOURCE, JavaSemantics.SOURCE_JAR)) /* The list of other libraries to link against. Permitted library types are: android_library, java_library with android constraint and cc_library wrapping or producing .so native libraries for the Android target platform. */ .override( builder .copy("deps") .allowedRuleClasses(AndroidRuleClasses.ALLOWED_DEPENDENCIES) .allowedFileTypes() .mandatoryProviders(JavaRuleClasses.CONTAINS_JAVA_PROVIDER) .aspect(androidNeverlinkAspect)) /* The closure of all rules reached via exports attributes are considered direct dependencies of any rule that directly depends on the target with exports.

The exports are not direct deps of the rule they belong to.

*/ .add( attr("exports", LABEL_LIST) .allowedRuleClasses(AndroidRuleClasses.ALLOWED_DEPENDENCIES) .allowedFileTypes(/*May not have files in exports!*/) .mandatoryProviders(JavaRuleClasses.CONTAINS_JAVA_PROVIDER) .aspect(androidNeverlinkAspect)) /* Whether to export manifest entries to android_binary targets that depend on this target. uses-permissions attributes are never exported. */ .add(attr("exports_manifest", TRISTATE).value(TriState.YES)) /* The list of java_plugins (e.g. annotation processors) to export to libraries that directly depend on this library.

The specified list of java_plugins will be applied to any library which directly depends on this library, just as if that library had explicitly declared these labels in plugins.

*/ .add( attr("exported_plugins", LABEL_LIST) .cfg(HostTransition.INSTANCE) .allowedRuleClasses("java_plugin") .allowedFileTypes(FileTypeSet.NO_FILE)) .add(attr("alwayslink", BOOLEAN).undocumented("purely informational for now")) /* Only use this library for compilation and not at runtime. The outputs of a rule marked as neverlink will not be used in .apk creation. Useful if the library will be provided by the runtime environment during execution. */ .add(attr("neverlink", BOOLEAN).value(false)) /* Package-relative path to the root of the java package tree containing idl sources included in this library.

This path will be used as the import root when processing idl sources that depend on this library.

When idl_import_root is specified, both idl_parcelables and idl_srcs must be at the path specified by the java package of the object they represent under idl_import_root. When idl_import_root is not specified, both idl_parcelables and idl_srcs must be at the path specified by their package under a Java root.

See examples.

*/ .add(attr("idl_import_root", STRING)) /* List of Android IDL definitions to translate to Java interfaces. After the Java interfaces are generated, they will be compiled together with the contents of srcs.

These files will be made available as imports for any android_library target that depends on this library, directly or via its transitive closure.

These files must be placed appropriately for the aidl compiler to find them. See the description of idl_import_root for information about what this means.

*/ .add( attr("idl_srcs", LABEL_LIST) .direct_compile_time_input() .allowedFileTypes(AndroidRuleClasses.ANDROID_IDL)) /* List of Android IDL definitions to supply as imports. These files will be made available as imports for any android_library target that depends on this library, directly or via its transitive closure, but will not be translated to Java or compiled.

Only .aidl files that correspond directly to .java sources in this library should be included (e.g., custom implementations of Parcelable), otherwise idl_srcs should be used.

These files must be placed appropriately for the aidl compiler to find them. See the description of idl_import_root for information about what this means.

*/ .add( attr("idl_parcelables", LABEL_LIST) .direct_compile_time_input() .allowedFileTypes(AndroidRuleClasses.ANDROID_IDL)) /* List of preprocessed Android IDL definitions to supply as imports. These files will be made available as imports for any android_library target that depends on this library, directly or via its transitive closure, but will not be translated to Java or compiled.

Only preprocessed .aidl files that correspond directly to .java sources in this library should be included (e.g., custom implementations of Parcelable), otherwise use idl_srcs for Android IDL definitions that need to be translated to Java interfaces and use idl_parcelable for non-preprcessed AIDL files.

*/ .add( attr("idl_preprocessed", LABEL_LIST) .direct_compile_time_input() .allowedFileTypes(AndroidRuleClasses.ANDROID_IDL)) .advertiseSkylarkProvider(SkylarkProviderIdentifier.forKey(JavaInfo.PROVIDER.getKey())) .build(); } @Override public Metadata getMetadata() { return RuleDefinition.Metadata.builder() .name("$android_library_base") .type(RuleClassType.ABSTRACT) .ancestors(AndroidResourceSupportRule.class, ProguardLibraryRule.class) .build(); } }