diff options
author | ajmichael <ajmichael@google.com> | 2017-11-30 09:19:32 -0800 |
---|---|---|
committer | Copybara-Service <copybara-piper@google.com> | 2017-11-30 09:21:33 -0800 |
commit | 2db75f6af8785d897ced86af3a80bc2811589d42 (patch) | |
tree | 7f032f006213e4e423af2732729a8dac508cb7b6 /src/main/java/com/google/devtools/build/lib/rules | |
parent | 39c2a36bf8f502d55addd58e9e97bf2ebb46e4b0 (diff) |
Refactor Android rule classes to minimize bazel specific stuff.
RELNOTES: None
PiperOrigin-RevId: 177463650
Diffstat (limited to 'src/main/java/com/google/devtools/build/lib/rules')
3 files changed, 109 insertions, 113 deletions
diff --git a/src/main/java/com/google/devtools/build/lib/rules/android/AndroidBinaryOnlyRule.java b/src/main/java/com/google/devtools/build/lib/rules/android/AndroidBinaryOnlyRule.java deleted file mode 100644 index 54b632bcbd..0000000000 --- a/src/main/java/com/google/devtools/build/lib/rules/android/AndroidBinaryOnlyRule.java +++ /dev/null @@ -1,99 +0,0 @@ -// 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.ConfigurationTransition.HOST; -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.TRISTATE; -import static com.google.devtools.build.lib.syntax.Type.BOOLEAN; -import static com.google.devtools.build.lib.syntax.Type.STRING_LIST; - -import com.google.devtools.build.lib.analysis.RuleDefinition; -import com.google.devtools.build.lib.analysis.RuleDefinitionEnvironment; -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.TriState; - -/** - * Attributes for {@code android_binary} that are not present on {@code android_test}. - */ -public final class AndroidBinaryOnlyRule implements RuleDefinition { - @Override - public RuleClass build(RuleClass.Builder builder, final RuleDefinitionEnvironment env) { - return builder - /* <!-- #BLAZE_RULE(android_binary).ATTRIBUTE(nocompress_extensions) --> - A list of file extension to leave uncompressed in apk. - <!-- #END_BLAZE_RULE.ATTRIBUTE --> */ - .add(attr("nocompress_extensions", STRING_LIST)) - /* <!-- #BLAZE_RULE(android_binary).ATTRIBUTE(crunch_png) --> - Do PNG crunching (or not). This is independent of nine-patch processing, which is always - done. Currently only supported for local resources (not android_resources). - <!-- #END_BLAZE_RULE.ATTRIBUTE --> */ - .add(attr("crunch_png", BOOLEAN).value(true)) - /* <!-- #BLAZE_RULE(android_binary).ATTRIBUTE(resource_configuration_filters) --> - A list of resource configuration filters, such 'en' that will limit the resources in the - apk to only the ones in the 'en' configuration. - <!-- #END_BLAZE_RULE.ATTRIBUTE --> */ - .add(attr(ResourceFilterFactory.RESOURCE_CONFIGURATION_FILTERS_NAME, STRING_LIST)) - /* <!-- #BLAZE_RULE(android_binary).ATTRIBUTE(shrink_resources) --> - Whether to perform resource shrinking. Resources that are not used by the binary will be - removed from the APK. This is only supported for rules using local resources (i.e. the - <code>manifest</code> and <code>resource_files</code> attributes) and requires ProGuard. It - operates in mostly the same manner as the Gradle resource shrinker - (https://developer.android.com/studio/build/shrink-code.html#shrink-resources). - <p>Notable differences: - <ul> - <li>resources in <code>values/</code> will be removed as well as file based resources</li> - <li>uses <code>strict mode</code> by default</li> - <li>removing unused ID resources is not supported</li> - </ul> - If resource shrinking is enabled, <code><var>name</var>_files/resource_shrinker.log</code> - will also be generated, detailing the analysis and deletions performed. - <p>Possible values: - <ul> - <li><code>shrink_resources = 1</code>: Turns on Android resource shrinking</li> - <li><code>shrink_resources = 0</code>: Turns off Android resource shrinking</li> - <li><code>shrink_resources = -1</code>: Shrinking is controlled by the - <a href="../user-manual.html#flag--android_resource_shrinking"> - --android_resource_shrinking</a> flag.</li> - </ul> - <!-- #END_BLAZE_RULE.ATTRIBUTE --> */ - .add(attr("shrink_resources", TRISTATE).value(TriState.AUTO)) - /* <!-- #BLAZE_RULE(android_binary).ATTRIBUTE(densities) --> - Densities to filter for when building the apk. - This will strip out raster drawable resources that would not be loaded by a device with - the specified screen densities, to reduce APK size. A corresponding compatible-screens - section will also be added to the manifest if it does not already contain a superset - listing. - <!-- #END_BLAZE_RULE.ATTRIBUTE --> */ - .add(attr(ResourceFilterFactory.DENSITIES_NAME, STRING_LIST)) - .add(attr("$android_manifest_merge_tool", LABEL) - .cfg(HOST) - .exec() - .value(env.getToolsLabel(AndroidRuleClasses.MANIFEST_MERGE_TOOL_LABEL))) - .removeAttribute("data") - .advertiseProvider(ApkProvider.class) - .build(); - } - - @Override - public Metadata getMetadata() { - return RuleDefinition.Metadata.builder() - .name("$android_binary_only") - .type(RuleClassType.ABSTRACT) - .ancestors(AndroidRuleClasses.AndroidBinaryBaseRule.class) - .build(); - } -} diff --git a/src/main/java/com/google/devtools/build/lib/rules/android/AndroidLibraryBaseRule.java b/src/main/java/com/google/devtools/build/lib/rules/android/AndroidLibraryBaseRule.java index da4177f2f8..4ccd2967b8 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/android/AndroidLibraryBaseRule.java +++ b/src/main/java/com/google/devtools/build/lib/rules/android/AndroidLibraryBaseRule.java @@ -15,7 +15,6 @@ package com.google.devtools.build.lib.rules.android; import static com.google.devtools.build.lib.packages.Attribute.ConfigurationTransition.HOST; 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_LIST; import static com.google.devtools.build.lib.packages.BuildType.TRISTATE; import static com.google.devtools.build.lib.syntax.Type.BOOLEAN; @@ -28,6 +27,7 @@ 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.JavaSemantics; import com.google.devtools.build.lib.rules.java.ProguardLibraryRule; @@ -46,6 +46,7 @@ public final class AndroidLibraryBaseRule implements RuleDefinition { @Override public RuleClass build(RuleClass.Builder builder, final RuleDefinitionEnvironment env) { return builder + .requiresConfigurationFragments(JavaConfiguration.class, AndroidConfiguration.class) /* <!-- #BLAZE_RULE(android_library).ATTRIBUTE(srcs) --> The list of <code>.java</code> or <code>.srcjar</code> files that are processed to create the target. @@ -101,7 +102,7 @@ public final class AndroidLibraryBaseRule implements RuleDefinition { Whether to export manifest entries to <code>android_binary</code> targets that depend on this target. <code>uses-permissions</code> attributes are never exported. <!-- #END_BLAZE_RULE.ATTRIBUTE --> */ - .add(attr("exports_manifest", TRISTATE).value(TriState.AUTO)) + .add(attr("exports_manifest", TRISTATE).value(TriState.YES)) /* <!-- #BLAZE_RULE(android_library).ATTRIBUTE(exported_plugins) --> The list of <code><a href="#${link java_plugin}">java_plugin</a></code>s (e.g. annotation processors) to export to libraries that directly depend on this library. @@ -190,11 +191,6 @@ public final class AndroidLibraryBaseRule implements RuleDefinition { attr("idl_preprocessed", LABEL_LIST) .direct_compile_time_input() .allowedFileTypes(AndroidRuleClasses.ANDROID_IDL)) - .add( - attr("$android_manifest_merge_tool", LABEL) - .cfg(HOST) - .exec() - .value(env.getToolsLabel(AndroidRuleClasses.MANIFEST_MERGE_TOOL_LABEL))) .advertiseSkylarkProvider(SkylarkProviderIdentifier.forKey(JavaInfo.PROVIDER.getKey())) .build(); } diff --git a/src/main/java/com/google/devtools/build/lib/rules/android/AndroidRuleClasses.java b/src/main/java/com/google/devtools/build/lib/rules/android/AndroidRuleClasses.java index 5b385796d7..efb39edca6 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/android/AndroidRuleClasses.java +++ b/src/main/java/com/google/devtools/build/lib/rules/android/AndroidRuleClasses.java @@ -43,7 +43,6 @@ import com.google.devtools.build.lib.packages.Attribute.AllowedValueSet; import com.google.devtools.build.lib.packages.Attribute.LateBoundDefault; import com.google.devtools.build.lib.packages.Attribute.SplitTransition; import com.google.devtools.build.lib.packages.AttributeMap; -import com.google.devtools.build.lib.packages.BuildType; import com.google.devtools.build.lib.packages.ImplicitOutputsFunction.SafeImplicitOutputsFunction; import com.google.devtools.build.lib.packages.Rule; import com.google.devtools.build.lib.packages.RuleClass; @@ -56,7 +55,9 @@ import com.google.devtools.build.lib.rules.android.AndroidConfiguration.AndroidA import com.google.devtools.build.lib.rules.android.AndroidConfiguration.AndroidManifestMerger; import com.google.devtools.build.lib.rules.android.AndroidConfiguration.ConfigurationDistinguisher; import com.google.devtools.build.lib.rules.config.ConfigFeatureFlagProvider; +import com.google.devtools.build.lib.rules.cpp.CppConfiguration; import com.google.devtools.build.lib.rules.cpp.CppOptions; +import com.google.devtools.build.lib.rules.cpp.CppRuleClasses; 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.JavaSemantics; @@ -65,6 +66,7 @@ import com.google.devtools.build.lib.skylarkinterface.SkylarkPrinter; import com.google.devtools.build.lib.skylarkinterface.SkylarkValue; import com.google.devtools.build.lib.syntax.Type; import com.google.devtools.build.lib.util.FileType; +import com.google.devtools.build.lib.util.FileTypeSet; import java.util.List; /** Rule definitions for Android rules. */ @@ -442,7 +444,7 @@ public final class AndroidRuleClasses { The name of the Android manifest file, normally <code>AndroidManifest.xml</code>. Must be defined if resource_files or assets are defined. <!-- #END_BLAZE_RULE.ATTRIBUTE --> */ - .add(attr("manifest", LABEL).legacyAllowAnyFileType()) + .add(attr("manifest", LABEL).allowedFileTypes(FileType.of(".xml"))) /* <!-- #BLAZE_RULE($android_resource_support).ATTRIBUTE(resource_files) --> The list of resources to be packaged. This is typically a <code>glob</code> of all files under the @@ -453,7 +455,7 @@ public final class AndroidRuleClasses { the generated outputs must be under the same "<code>res</code>" directory as any other resource files that are included. <!-- #END_BLAZE_RULE.ATTRIBUTE --> */ - .add(attr("resource_files", LABEL_LIST).legacyAllowAnyFileType()) + .add(attr("resource_files", LABEL_LIST).allowedFileTypes(FileTypeSet.ANY_FILE)) /* <!-- #BLAZE_RULE($android_resource_support).ATTRIBUTE(assets_dir) --> The string giving the path to the files in <code>assets</code>. The pair <code>assets</code> and <code>assets_dir</code> describe packaged @@ -467,7 +469,7 @@ public final class AndroidRuleClasses { files) or exported files in the other packages, as long as all those files are under the <code>assets_dir</code> directory in the corresponding package. <!-- #END_BLAZE_RULE.ATTRIBUTE --> */ - .add(attr("assets", LABEL_LIST).legacyAllowAnyFileType()) + .add(attr("assets", LABEL_LIST).allowedFileTypes(FileTypeSet.ANY_FILE)) /* <!-- #BLAZE_RULE($android_resource_support).ATTRIBUTE(inline_constants) --> Let the compiler inline the constants defined in the generated java sources. This attribute must be set to 0 for all <code>android_library</code> rules @@ -507,9 +509,15 @@ public final class AndroidRuleClasses { // The javac annotation processor from Android's data binding library that turns // processed XML expressions into Java code. .add( - attr(DataBinding.DATABINDING_ANNOTATION_PROCESSOR_ATTR, BuildType.LABEL) + attr(DataBinding.DATABINDING_ANNOTATION_PROCESSOR_ATTR, LABEL) .cfg(HOST) .value(env.getToolsLabel("//tools/android:databinding_annotation_processor"))) + // TODO(b/30816740): Remove this once legacy manifest merging is no longer supported. + .add( + attr("$android_manifest_merge_tool", LABEL) + .cfg(HOST) + .exec() + .value(env.getToolsLabel(AndroidRuleClasses.MANIFEST_MERGE_TOOL_LABEL))) .build(); } @@ -606,6 +614,8 @@ public final class AndroidRuleClasses { @Override public RuleClass build(RuleClass.Builder builder, final RuleDefinitionEnvironment env) { return builder + .requiresConfigurationFragments( + AndroidConfiguration.class, JavaConfiguration.class, CppConfiguration.class) /* <!-- #BLAZE_RULE($android_binary_base).ATTRIBUTE(srcs) --> The list of source files that are processed to create the target. <p><code>srcs</code> files of type <code>.java</code> are compiled. @@ -625,6 +635,8 @@ public final class AndroidRuleClasses { attr("srcs", LABEL_LIST) .direct_compile_time_input() .allowedFileTypes(JavaSemantics.JAVA_SOURCE, JavaSemantics.SOURCE_JAR)) + // manifest is required for android_binary to ensure that we have an Android package. + .override(builder.copy("manifest").mandatory()) /* <!-- #BLAZE_RULE($android_binary_base).ATTRIBUTE(deps) --> The list of other libraries to be linked in to the binary target. Permitted library types are: <code>android_library</code>, @@ -663,6 +675,53 @@ public final class AndroidRuleClasses { .allowedRuleClasses("android_binary") .allowedFileTypes() .undocumented("experimental, see b/36226333")) + /* <!-- #BLAZE_RULE($android_binary_base).ATTRIBUTE(nocompress_extensions) --> + A list of file extension to leave uncompressed in apk. + <!-- #END_BLAZE_RULE.ATTRIBUTE --> */ + .add(attr("nocompress_extensions", STRING_LIST)) + /* <!-- #BLAZE_RULE($android_binary_base).ATTRIBUTE(crunch_png) --> + Do PNG crunching (or not). This is independent of nine-patch processing, which is always + done. Currently only supported for local resources (not android_resources). + <!-- #END_BLAZE_RULE.ATTRIBUTE --> */ + .add(attr("crunch_png", BOOLEAN).value(true)) + /* <!-- #BLAZE_RULE($android_binary_base).ATTRIBUTE(resource_configuration_filters) --> + A list of resource configuration filters, such 'en' that will limit the resources in the + apk to only the ones in the 'en' configuration. + <!-- #END_BLAZE_RULE.ATTRIBUTE --> */ + .add(attr(ResourceFilterFactory.RESOURCE_CONFIGURATION_FILTERS_NAME, STRING_LIST)) + /* <!-- #BLAZE_RULE($android_binary_base).ATTRIBUTE(shrink_resources) --> + Whether to perform resource shrinking. Resources that are not used by the binary will be + removed from the APK. This is only supported for rules using local resources (i.e. the + <code>manifest</code> and <code>resource_files</code> attributes) and requires ProGuard. + It operates in mostly the same manner as the Gradle resource shrinker + (https://developer.android.com/studio/build/shrink-code.html#shrink-resources). + <p>Notable differences: + <ul> + <li>resources in <code>values/</code> will be removed as well as file based + resources</li> + <li>uses <code>strict mode</code> by default</li> + <li>removing unused ID resources is not supported</li> + </ul> + If resource shrinking is enabled, <code><var>name</var>_files/resource_shrinker.log</code> + will also be generated, detailing the analysis and deletions performed. + <p>Possible values: + <ul> + <li><code>shrink_resources = 1</code>: Turns on Android resource shrinking</li> + <li><code>shrink_resources = 0</code>: Turns off Android resource shrinking</li> + <li><code>shrink_resources = -1</code>: Shrinking is controlled by the + <a href="../user-manual.html#flag--android_resource_shrinking"> + --android_resource_shrinking</a> flag.</li> + </ul> + <!-- #END_BLAZE_RULE.ATTRIBUTE --> */ + .add(attr("shrink_resources", TRISTATE).value(TriState.AUTO)) + /* <!-- #BLAZE_RULE($android_binary_base).ATTRIBUTE(densities) --> + Densities to filter for when building the apk. + This will strip out raster drawable resources that would not be loaded by a device with + the specified screen densities, to reduce APK size. A corresponding compatible-screens + section will also be added to the manifest if it does not already contain a superset + listing. + <!-- #END_BLAZE_RULE.ATTRIBUTE --> */ + .add(attr(ResourceFilterFactory.DENSITIES_NAME, STRING_LIST)) .add( attr("$build_incremental_dexmanifest", LABEL) .cfg(HOST) @@ -845,6 +904,11 @@ public final class AndroidRuleClasses { attr(":bytecode_optimizers", LABEL_LIST) .cfg(HOST) .value(JavaSemantics.BYTECODE_OPTIMIZERS)) + // We need the C++ toolchain for every sub-configuration to get the correct linker. + .add( + attr(":cc_toolchain_split", LABEL) + .cfg(AndroidRuleClasses.ANDROID_SPLIT_TRANSITION) + .value(CppRuleClasses.ccToolchainAttribute(env))) .add(attr("rewrite_dexes_with_rex", BOOLEAN).value(false).undocumented("experimental")) /* File to be used as a package map for Rex tool that keeps the assignment of classes to @@ -924,7 +988,7 @@ public final class AndroidRuleClasses { .exec() .value(env.getToolsLabel("//tools/android:resource_extractor"))) .add( - attr("instruments", BuildType.LABEL) + attr("instruments", LABEL) .undocumented("blocked by android_instrumentation_test") .allowedRuleClasses("android_binary") .allowedFileTypes(NO_FILE)) @@ -933,6 +997,8 @@ public final class AndroidRuleClasses { .cfg(HOST) .exec() .value(env.getToolsLabel("//tools/android:zip_filter"))) + .removeAttribute("data") + .advertiseProvider(ApkProvider.class) .advertiseSkylarkProvider(SkylarkProviderIdentifier.forKey(JavaInfo.PROVIDER.getKey())) .build(); } @@ -942,7 +1008,7 @@ public final class AndroidRuleClasses { return RuleDefinition.Metadata.builder() .name("$android_binary_base") .type(RuleClassType.ABSTRACT) - .ancestors(AndroidRuleClasses.AndroidBaseRule.class, AndroidResourceSupportRule.class) + .ancestors(AndroidResourceSupportRule.class) .build(); } } @@ -990,4 +1056,37 @@ public final class AndroidRuleClasses { return ans; } } + + /** Definition of the {@code android_tools_defaults_jar} rule. */ + public static final class AndroidToolsDefaultsJarRule implements RuleDefinition { + + private final Label[] compatibleWithAndroidEnvironments; + + public AndroidToolsDefaultsJarRule(Label... compatibleWithAndroidEnvironments) { + this.compatibleWithAndroidEnvironments = compatibleWithAndroidEnvironments; + } + + @Override + public RuleClass build(Builder builder, RuleDefinitionEnvironment environment) { + builder + .setUndocumented() + .add( + attr(":android_sdk", LABEL) + .allowedRuleClasses("android_sdk", "filegroup") + .value(getAndroidSdkLabel(environment.getToolsLabel(DEFAULT_SDK)))); + if (compatibleWithAndroidEnvironments.length > 0) { + builder.compatibleWith(compatibleWithAndroidEnvironments); + } + return builder.build(); + } + + @Override + public Metadata getMetadata() { + return Metadata.builder() + .name("android_tools_defaults_jar") + .ancestors(BaseRuleClasses.BaseRule.class) + .factoryClass(AndroidToolsDefaultsJar.class) + .build(); + } + } } |