// Copyright 2017 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; 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; import static com.google.devtools.build.lib.packages.BuildType.LABEL_KEYED_STRING_DICT; import static com.google.devtools.build.lib.rules.android.AndroidRuleClasses.getAndroidSdkLabel; import static com.google.devtools.build.lib.syntax.Type.STRING; import static com.google.devtools.build.lib.syntax.Type.STRING_DICT; import com.google.common.collect.ImmutableList; 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.rules.config.ConfigFeatureFlagProvider; import com.google.devtools.build.lib.rules.java.JavaConfiguration; import com.google.devtools.build.lib.util.FileTypeSet; /** Base rule definition for android_local_test */ public class AndroidLocalTestBaseRule implements RuleDefinition { @Override public RuleClass build(RuleClass.Builder builder, RuleDefinitionEnvironment environment) { return builder .requiresConfigurationFragments( JavaConfiguration.class, AndroidLocalTestConfiguration.class) // Update documentation for inherited attributes /* The list of libraries to be tested as well as additional libraries to be linked in to the target. All resources, assets and manifest files declared in Android rules in the transitive closure of this attribute are made available in the test.

The list of allowed rules in deps are android_library, aar_import, java_import, java_library, and java_lite_proto_library. */ /* The list of source files that are processed to create the target. Required except in special case described below.

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.)

All other files are ignored, as long as there is at least one file of a file type described above. Otherwise an error is raised.

The srcs attribute is required and cannot be empty, unless runtime_deps is specified.

*/ .add( attr(AndroidFeatureFlagSetProvider.FEATURE_FLAG_ATTR, LABEL_KEYED_STRING_DICT) .undocumented("the feature flag feature has not yet been launched") .allowedRuleClasses("config_feature_flag") .allowedFileTypes() .nonconfigurable("defines an aspect of configuration") .mandatoryProviders(ImmutableList.of(ConfigFeatureFlagProvider.id()))) .add(AndroidFeatureFlagSetProvider.getWhitelistAttribute(environment)) // TODO(b/38314524): Move $android_resources_busybox and :android_sdk to a separate // rule so they're not defined in multiple places .add( attr("$android_resources_busybox", LABEL) .cfg(HostTransition.INSTANCE) .exec() .value(environment.getToolsLabel(AndroidRuleClasses.DEFAULT_RESOURCES_BUSYBOX))) .add( attr(":android_sdk", LABEL) .allowedRuleClasses("android_sdk") .value( getAndroidSdkLabel(environment.getToolsLabel(AndroidRuleClasses.DEFAULT_SDK)))) /* The Java class to be loaded by the test runner.

This attribute specifies the name of a Java class to be run by this test. It is rare to need to set this. If this argument is omitted, the Java class whose name corresponds to the name of this android_local_test rule will be used. The test class needs to be annotated with org.junit.runner.RunWith.

*/ .add(attr("test_class", STRING)) // every test class adds this /* A dictionary of values to be overridden in the manifest. Any instance of ${name} in the manifest will be replaced with the value corresponding to name in this dictionary. applicationId, versionCode, versionName, minSdkVersion, targetSdkVersion and maxSdkVersion will also override the corresponding attributes of the manifest and uses-sdk tags. packageName will be ignored and will be set from either applicationId if specified or the package in the manifest. It is not necessary to have a manifest on the rule in order to use manifest_values. */ .add(attr("manifest_values", STRING_DICT)) /* The name of the Android manifest file, normally AndroidManifest.xml. Must be defined if resource_files or assets are defined or if any of the manifests from the libraries under test have a minSdkVersion tag in them. */ .add(attr("manifest", LABEL).allowedFileTypes(FileTypeSet.ANY_FILE)) /* Java package in which the R class will be generated. By default the package is inferred from the directory where the BUILD file containing the rule is. If you use this attribute, you will likely need to use test_class as well. */ .add(attr("custom_package", STRING)) .build(); } @Override public Metadata getMetadata() { return RuleDefinition.Metadata.builder() .name("$android_local_test_base") .type(RuleClassType.ABSTRACT) .build(); } }