From a228504e7de23cd2c3e9c16eaf846972b53150da Mon Sep 17 00:00:00 2001 From: Brendan Linn Date: Wed, 29 Nov 2017 14:53:23 -0800 Subject: [PATCH] Use standard bazel proto toolchain This CL switches rules_closure to use Bazel's built-in java_proto_library rule instead of a custom Skylark rule. It also switches the protoc binary to the standard @com_google_protobuf//:protoc instead of a custom-downloaded dependency. This CL is a port that fixes #231 and #232. ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=177367734 --- closure/private/java_proto_library.bzl | 81 ---------------- closure/protobuf/BUILD | 1 - closure/protobuf/closure_js_proto_library.bzl | 2 +- closure/repositories.bzl | 97 ++++++------------- java/com/google/javascript/jscomp/BUILD | 2 +- java/io/bazel/rules/closure/BUILD | 9 +- java/io/bazel/rules/closure/webfiles/BUILD | 11 ++- .../bazel/rules/closure/webfiles/server/BUILD | 10 +- java/io/bazel/rules/closure/worker/BUILD | 11 ++- javatests/io/bazel/rules/closure/worker/BUILD | 2 +- 10 files changed, 58 insertions(+), 168 deletions(-) delete mode 100644 closure/private/java_proto_library.bzl diff --git a/closure/private/java_proto_library.bzl b/closure/private/java_proto_library.bzl deleted file mode 100644 index f8a2e42565..0000000000 --- a/closure/private/java_proto_library.bzl +++ /dev/null @@ -1,81 +0,0 @@ -# Copyright 2016 The Closure Rules Authors. All rights reserved. -# Copyright 2014 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. - - -def _impl(ctx): - out = ctx.outputs.srcjar - includes = ["-I."] - if ctx.attr.src.label.workspace_root: - includes += ["-I%s" % ctx.attr.src.label.workspace_root] - ctx.action( - command=' '.join([ - "JAR='%s'" % ctx.executable._jar.path, - "OUTPUT='%s'" % out.path, - "PROTO_COMPILER='%s'" % ctx.executable._proto_compiler.path, - "SOURCE='%s'" % ctx.file.src.path, - "INCLUDES='%s'" % ' '.join(includes), - ctx.executable._gensrcjar.path, - ]), - inputs=([ctx.file.src] + ctx.files._gensrcjar + ctx.files._jar + - ctx.files._jdk + ctx.files._proto_compiler), - outputs=[out], - mnemonic="GenProtoSrcJar", - use_default_shell_env=True) - return struct(runfiles=ctx.runfiles(collect_default=True)) - -_gensrcjar = rule( - implementation=_impl, - attrs = { - "src": attr.label( - allow_files = FileType([".proto"]), - single_file = True), - "_gensrcjar": attr.label( - default = Label(str(Label("//closure/private:gensrcjar"))), - executable = True, - cfg = "host"), - "_proto_compiler": attr.label( - default = Label("@com_google_protobuf_protoc"), - allow_files = True, - executable = True, - single_file = True, - cfg = "host"), - "_jar": attr.label( - default = Label("@local_jdk//:jar"), - allow_files = True, - executable = True, - single_file = True, - cfg = "host"), - "_jdk": attr.label( - default = Label("@local_jdk//:jdk-default"), - allow_files = True), - }, - outputs = {"srcjar": "lib%{name}.srcjar"}) - -def java_proto_library(name, src, testonly=None, visibility=None, **kwargs): - _gensrcjar( - name = name + "_srcjar", - src = src, - testonly = testonly, - visibility = visibility, - ) - native.java_library( - name = name, - srcs = [":%s_srcjar" % name], - deps = ["@com_google_protobuf_java"], - javacopts = ["-Xlint:-rawtypes"], - testonly = testonly, - visibility = visibility, - **kwargs - ) diff --git a/closure/protobuf/BUILD b/closure/protobuf/BUILD index f452eaa096..e3c29ce3f1 100644 --- a/closure/protobuf/BUILD +++ b/closure/protobuf/BUILD @@ -41,7 +41,6 @@ closure_js_library( "JSC_NULLABLE_RETURN_WITH_NAME", "JSC_OPTIONAL_PARAM_NOT_MARKED_OPTIONAL", "JSC_UNKNOWN_EXPR_TYPE", - "strictMissingRequire", ], deps = ["//closure/library"], ) diff --git a/closure/protobuf/closure_js_proto_library.bzl b/closure/protobuf/closure_js_proto_library.bzl index cc03d9a813..4bbeca1423 100644 --- a/closure/protobuf/closure_js_proto_library.bzl +++ b/closure/protobuf/closure_js_proto_library.bzl @@ -35,7 +35,7 @@ def closure_js_proto_library( testonly = None, binary = 1, import_style = None, - protocbin = Label("@com_google_protobuf_protoc"), + protocbin = Label("@com_google_protobuf//:protoc"), **kwargs): cmd = ["$(location %s)" % protocbin] js_out_options = ["library=%s,error_on_name_conflict" % name] diff --git a/closure/repositories.bzl b/closure/repositories.bzl index fe1c0a7c7c..015b5fb156 100644 --- a/closure/repositories.bzl +++ b/closure/repositories.bzl @@ -42,9 +42,9 @@ def closure_repositories( omit_com_google_javascript_closure_library=False, omit_com_google_javascript_incremental_dom=False, omit_com_google_jsinterop_annotations=False, + omit_com_google_protobuf=False, omit_com_google_protobuf_java=False, omit_com_google_protobuf_js=False, - omit_com_google_protobuf_protoc=False, omit_com_google_template_soy=False, omit_com_google_template_soy_jssrc=False, omit_com_ibm_icu_icu4j=False, @@ -113,12 +113,12 @@ def closure_repositories( com_google_javascript_incremental_dom() if not omit_com_google_jsinterop_annotations: com_google_jsinterop_annotations() + if not omit_com_google_protobuf: + com_google_protobuf() if not omit_com_google_protobuf_java: com_google_protobuf_java() if not omit_com_google_protobuf_js: com_google_protobuf_js() - if not omit_com_google_protobuf_protoc: - com_google_protobuf_protoc() if not omit_com_google_template_soy: com_google_template_soy() if not omit_com_google_template_soy_jssrc: @@ -401,7 +401,7 @@ def com_google_common_html_types(): "@com_google_errorprone_error_prone_annotations", "@com_google_guava", "@com_google_jsinterop_annotations", - "@com_google_protobuf_java", + "@com_google_protobuf_java//:protobuf_java", "@javax_annotation_jsr250_api", ], ) @@ -589,7 +589,7 @@ def com_google_javascript_closure_compiler(): "@com_google_code_gson", "@com_google_guava", "@com_google_code_findbugs_jsr305", - "@com_google_protobuf_java", + "@com_google_protobuf_java//:protobuf_java", ], extra_build_file_content = "\n".join([ "java_binary(", @@ -645,81 +645,40 @@ def com_google_jsinterop_annotations(): ], ) +def com_google_protobuf(): + native.http_archive( + name = "com_google_protobuf", + strip_prefix = "protobuf-3.5.0", + sha256 = "0cc6607e2daa675101e9b7398a436f09167dffb8ca0489b0307ff7260498c13c", + urls = [ + "https://mirror.bazel.build/github.com/google/protobuf/archive/v3.5.0.tar.gz", + "https://github.com/google/protobuf/archive/v3.5.0.tar.gz", + ], + ) + def com_google_protobuf_java(): - java_import_external( + native.http_archive( name = "com_google_protobuf_java", - jar_sha256 = "f3411ade77523d5f0d013d4f25c36879e66f0c5e1e4310f7096d54d0d2553554", - jar_urls = [ - "https://mirror.bazel.build/repo1.maven.org/maven2/com/google/protobuf/protobuf-java/3.3.0/protobuf-java-3.3.0.jar", - "https://repo1.maven.org/maven2/com/google/protobuf/protobuf-java/3.3.0/protobuf-java-3.3.0.jar", + sha256 = "0cc6607e2daa675101e9b7398a436f09167dffb8ca0489b0307ff7260498c13c", + strip_prefix = "protobuf-3.5.0", + urls = [ + "https://mirror.bazel.build/github.com/google/protobuf/archive/v3.5.0.tar.gz", + "https://github.com/google/protobuf/archive/v3.5.0.tar.gz", ], - licenses = ["notice"], # New BSD and Apache 2.0 ) def com_google_protobuf_js(): native.new_http_archive( name = "com_google_protobuf_js", urls = [ - # 3.3.0 has a Closure Compiler bug because it references Node's Buffer - # type. This was fixed in f00e06c95bc117fb2ed0ca56c96041c93039f1fe. - # - # TODO(jart): Update when https://github.com/google/protobuf/pull/3387 - # is merged. - "https://mirror.bazel.build/github.com/google/protobuf/archive/33545583286a31940b6a732b1888e639cdf2e3c4.tar.gz", - "https://github.com/google/protobuf/archive/33545583286a31940b6a732b1888e639cdf2e3c4.tar.gz", # 2017-07-17 - ], - sha256 = "ecd9f92f137e75d140a8b611cd2c0d6c0f34f561946dc5f7fcecde631bb13c25", - strip_prefix = "protobuf-33545583286a31940b6a732b1888e639cdf2e3c4/js", + "https://mirror.bazel.build/github.com/google/protobuf/archive/v3.5.0.tar.gz", + "https://github.com/google/protobuf/archive/v3.5.0.tar.gz", + ], + sha256 = "0cc6607e2daa675101e9b7398a436f09167dffb8ca0489b0307ff7260498c13c", + strip_prefix = "protobuf-3.5.0/js", build_file = str(Label("//closure/protobuf:protobuf_js.BUILD")), ) -def com_google_protobuf_protoc(): - filegroup_external( - name = "com_google_protobuf_protoc", - licenses = ["notice"], # BSD - sha256_urls_extract_macos = { - "d752ba0ea67239e327a48b2f23da0e673928a9ff06ee530319fc62200c0aff89": [ - "https://mirror.bazel.build/github.com/google/protobuf/releases/download/v3.3.0/protoc-3.3.0-osx-x86_64.zip", - "https://github.com/google/protobuf/releases/download/v3.3.0/protoc-3.3.0-osx-x86_64.zip", - ], - }, - sha256_urls_extract_windows = { - "19ec3d3853c1181912dc442840b3a76bfe0607ecc67d0854b323fdd1fdd8ab77": [ - "https://mirror.bazel.build/github.com/google/protobuf/releases/download/v3.3.0/protoc-3.3.0-win32.zip", - "https://github.com/google/protobuf/releases/download/v3.3.0/protoc-3.3.0-win32.zip", - ], - }, - sha256_urls_extract = { - "feb112bbc11ea4e2f7ef89a359b5e1c04428ba6cfa5ee628c410eccbfe0b64c3": [ - "https://mirror.bazel.build/github.com/google/protobuf/releases/download/v3.3.0/protoc-3.3.0-linux-x86_64.zip", - "https://github.com/google/protobuf/releases/download/v3.3.0/protoc-3.3.0-linux-x86_64.zip", - ], - }, - generated_rule_name = "files", - extra_build_file_content = "\n".join([ - "filegroup(", - " name = \"com_google_protobuf_protoc\",", - " srcs = select({", - " \":windows\": [\"bin/protoc.exe\"],", - " \":windows_msvc\": [\"bin/protoc.exe\"],", - " \"//conditions:default\": [\"bin/protoc\"],", - " }),", - ")", - "", - "config_setting(", - " name = \"windows\",", - " values = {\"cpu\": \"x64_windows\"},", - " visibility = [\"//visibility:private\"],", - ")", - "", - "config_setting(", - " name = \"windows_msvc\",", - " values = {\"cpu\": \"x64_windows_msvc\"},", - " visibility = [\"//visibility:private\"],", - ")", - ]), - ) - def com_google_template_soy(): java_import_external( name = "com_google_template_soy", @@ -738,7 +697,7 @@ def com_google_template_soy(): "@com_google_inject_extensions_guice_assistedinject", "@com_google_inject_extensions_guice_multibindings", "@com_google_inject_guice", - "@com_google_protobuf_java", + "@com_google_protobuf_java//:protobuf_java", "@com_ibm_icu_icu4j", "@javax_inject", "@org_json", diff --git a/java/com/google/javascript/jscomp/BUILD b/java/com/google/javascript/jscomp/BUILD index 4027b74c18..68a85438ad 100644 --- a/java/com/google/javascript/jscomp/BUILD +++ b/java/com/google/javascript/jscomp/BUILD @@ -44,6 +44,6 @@ java_library( "@com_google_dagger", "@com_google_guava", "@com_google_javascript_closure_compiler", - "@com_google_protobuf_java", + "@com_google_protobuf_java//:protobuf_java", ], ) diff --git a/java/io/bazel/rules/closure/BUILD b/java/io/bazel/rules/closure/BUILD index 8bfebfc0f0..e8d1003e3b 100644 --- a/java/io/bazel/rules/closure/BUILD +++ b/java/io/bazel/rules/closure/BUILD @@ -12,8 +12,6 @@ # See the License for the specific language governing permissions and # limitations under the License. -load("//closure/private:java_proto_library.bzl", "java_proto_library") - licenses(["notice"]) # Apache 2.0 java_binary( @@ -59,8 +57,13 @@ java_library( ], ) +proto_library( + name = "build_info_proto", + srcs = ["build_info.proto"], +) + java_proto_library( name = "build_info_java_proto", - src = "build_info.proto", visibility = ["//visibility:public"], + deps = [":build_info_proto"], ) diff --git a/java/io/bazel/rules/closure/webfiles/BUILD b/java/io/bazel/rules/closure/webfiles/BUILD index 6a0073aebc..d1fd02563f 100644 --- a/java/io/bazel/rules/closure/webfiles/BUILD +++ b/java/io/bazel/rules/closure/webfiles/BUILD @@ -12,8 +12,6 @@ # See the License for the specific language governing permissions and # limitations under the License. -load("//closure/private:java_proto_library.bzl", "java_proto_library") - licenses(["notice"]) # Apache 2.0 java_library( @@ -31,14 +29,19 @@ java_library( "@com_google_code_findbugs_jsr305", "@com_google_dagger", "@com_google_guava", - "@com_google_protobuf_java", + "@com_google_protobuf_java//:protobuf_java", "@javax_inject", "@org_jsoup", ], ) +proto_library( + name = "build_info_proto", + srcs = ["build_info.proto"], +) + java_proto_library( name = "build_info_java_proto", - src = "build_info.proto", visibility = ["//visibility:public"], + deps = [":build_info_proto"], ) diff --git a/java/io/bazel/rules/closure/webfiles/server/BUILD b/java/io/bazel/rules/closure/webfiles/server/BUILD index a2b2c3b130..3655e1a7b5 100644 --- a/java/io/bazel/rules/closure/webfiles/server/BUILD +++ b/java/io/bazel/rules/closure/webfiles/server/BUILD @@ -13,7 +13,6 @@ # limitations under the License. load("//closure:defs.bzl", "closure_java_template_library") -load("//closure/private:java_proto_library.bzl", "java_proto_library") licenses(["notice"]) # Apache 2.0 @@ -32,7 +31,7 @@ java_library( "@com_google_code_findbugs_jsr305", "@com_google_dagger", "@com_google_guava", - "@com_google_protobuf_java", + "@com_google_protobuf_java//:protobuf_java", "@com_google_template_soy", ], ) @@ -44,10 +43,15 @@ java_binary( runtime_deps = [":server"], ) +proto_library( + name = "build_info_proto", + srcs = ["build_info.proto"], +) + java_proto_library( name = "build_info_java_proto", - src = "build_info.proto", visibility = ["//visibility:public"], + deps = [":build_info_proto"], ) closure_java_template_library( diff --git a/java/io/bazel/rules/closure/worker/BUILD b/java/io/bazel/rules/closure/worker/BUILD index a41eeebbbb..511236ecd1 100644 --- a/java/io/bazel/rules/closure/worker/BUILD +++ b/java/io/bazel/rules/closure/worker/BUILD @@ -12,8 +12,6 @@ # See the License for the specific language governing permissions and # limitations under the License. -load("//closure/private:java_proto_library.bzl", "java_proto_library") - licenses(["notice"]) # Apache 2.0 java_library( @@ -31,12 +29,17 @@ java_library( "@com_google_code_findbugs_jsr305", "@com_google_dagger", "@com_google_guava", - "@com_google_protobuf_java", + "@com_google_protobuf_java//:protobuf_java", ], ) +proto_library( + name = "worker_protocol_proto", + srcs = ["worker_protocol.proto"], +) + java_proto_library( name = "worker_protocol_java_proto", - src = "worker_protocol.proto", visibility = ["//javatests/io/bazel/rules/closure/worker:__pkg__"], + deps = [":worker_protocol_proto"], ) diff --git a/javatests/io/bazel/rules/closure/worker/BUILD b/javatests/io/bazel/rules/closure/worker/BUILD index 1973a487f2..74b27fed51 100644 --- a/javatests/io/bazel/rules/closure/worker/BUILD +++ b/javatests/io/bazel/rules/closure/worker/BUILD @@ -25,7 +25,7 @@ java_test( "@com_google_dagger", "@com_google_guava", "@com_google_jimfs", - "@com_google_protobuf_java", + "@com_google_protobuf_java//:protobuf_java", "@com_google_truth", "@junit", "@org_mockito_all",