From ec8593dfc6ca9224ff42e0e64fa00b8afa7ba27f Mon Sep 17 00:00:00 2001 From: arunkumar9t2 Date: Wed, 10 Aug 2022 22:43:47 +0800 Subject: [PATCH] Disable header compilation for r-classes --- tools/build_config/build_config.bzl | 23 ++++++++--------------- tools/databinding/databinding.bzl | 9 ++++++++- tools/java/BUILD.bazel | 0 tools/java/java_library_no_header.bzl | 26 ++++++++++++++++++++++++++ 4 files changed, 42 insertions(+), 16 deletions(-) create mode 100644 tools/java/BUILD.bazel create mode 100644 tools/java/java_library_no_header.bzl diff --git a/tools/build_config/build_config.bzl b/tools/build_config/build_config.bzl index e2b42a76..86bcb288 100644 --- a/tools/build_config/build_config.bzl +++ b/tools/build_config/build_config.bzl @@ -51,7 +51,7 @@ def build_config( booleans = {}, ints = {}, longs = {}): - """Generates a Jar file containing BuildConfig class just like AGP. + """Generates a kt_jvm_library target containing build config fields just like AGP. Usage: Add the field variables in the relevant dicts like (strings, booleans etc) and add a dependency @@ -68,10 +68,7 @@ def build_config( """ build_config = "BuildConfig" - build_config_file_path = "$(RULEDIR)/{}.kt".format(build_config) - - # Uses srcjar so that the generated .kt file can be compiled to class file via kt_jvm_library - build_config_jar = name + ".srcjar" + build_config_file_path = "src/main/%s/%s.kt" % (name, build_config) strings_statements = _build_statement( _STRING_TYPE, @@ -87,10 +84,9 @@ def build_config( ints_statements = _build_statement(_INT_TYPE, ints, False) longs_statements = _build_statement(_LONG_TYPE, longs, False) - # Cmd for genrule. Generates BuildConfig.kt and packages it to a Java jar file in the build - # directory + # Cmd for genrule. Generates BuildConfig. cmd = """ -cat << EOF > {build_config_file_path} +cat << EOF > $@ /** * Generated file do not modify */ @@ -102,9 +98,7 @@ object {build_config} {{ {ints_statements} {longs_statements} }} -EOF -$(location @bazel_tools//tools/zip:zipper) c $@ {build_config_file_path} -""".format( +EOF""".format( build_config = build_config, build_config_file_path = build_config_file_path, package_name = package_name, @@ -115,14 +109,13 @@ $(location @bazel_tools//tools/zip:zipper) c $@ {build_config_file_path} ) native.genrule( - name = "_" + name, - outs = [build_config_jar], + name = "_%s_gen" % name, + outs = [build_config_file_path], cmd = cmd, message = "Generating %s's build config class" % (native.package_name()), - tools = ["@bazel_tools//tools/zip:zipper"], ) kt_jvm_library( name = name, - srcs = [build_config_jar], + srcs = [build_config_file_path], ) diff --git a/tools/databinding/databinding.bzl b/tools/databinding/databinding.bzl index 870b5ddc..2c83f36a 100644 --- a/tools/databinding/databinding.bzl +++ b/tools/databinding/databinding.bzl @@ -1,6 +1,7 @@ load("@io_bazel_rules_kotlin//kotlin:kotlin.bzl", "kt_android_library") load("@io_bazel_rules_kotlin//kotlin:jvm.bzl", "kt_jvm_library") load(":databinding_stubs.bzl", "databinding_stubs") +load("@grab_bazel_common//tools/java:java_library_no_header.bzl", "java_library_no_header") # load(":databinding_aar.bzl", "databinding_aar") # TODO(arun) Replace with configurable maven targets @@ -95,6 +96,12 @@ def kt_db_android_library( neverlink = 1, # Use the R classes only for compiling and not at runtime. ) + r_classes_no_header = name + "-r-classes-no-header" + java_library_no_header( + name = r_classes_no_header, + dep = ":" + r_classes, + ) + # Create an intermediate target for compiling all Kotlin classes used in Databinding kotlin_target = name + "-kotlin" kotlin_targets = [] @@ -114,7 +121,7 @@ def kt_db_android_library( name = kotlin_target, srcs = srcs + [binding_classes_sources], plugins = plugins, - deps = deps + _DATABINDING_DEPS + [r_classes] + [ + deps = deps + _DATABINDING_DEPS + [r_classes_no_header] + [ "@grab_bazel_common//tools/binding-adapter-bridge:binding-adapter-bridge", "@grab_bazel_common//tools/android:android_sdk", ], diff --git a/tools/java/BUILD.bazel b/tools/java/BUILD.bazel new file mode 100644 index 00000000..e69de29b diff --git a/tools/java/java_library_no_header.bzl b/tools/java/java_library_no_header.bzl new file mode 100644 index 00000000..ed507f24 --- /dev/null +++ b/tools/java/java_library_no_header.bzl @@ -0,0 +1,26 @@ +def _java_header_compilation_transition(settings, attr): + _ignore = (settings, attr) + return {"//command_line_option:java_header_compilation": "False"} + +java_header_compilation_transition = transition( + implementation = _java_header_compilation_transition, + inputs = [], + outputs = ["//command_line_option:java_header_compilation"], +) + +def _java_library_without_header_compilation(ctx): + return [java_common.merge([d[JavaInfo] for d in ctx.attr.dep])] + +java_library_no_header = rule( + implementation = _java_library_without_header_compilation, + attrs = { + "dep": attr.label( + providers = [JavaInfo], + mandatory = True, + cfg = java_header_compilation_transition, + ), + "_allowlist_function_transition": attr.label( + default = "@bazel_tools//tools/allowlists/function_transition_allowlist", + ), + }, +)