From f0cb9cdb957e27c191d67a3154b72f383a4ad60f Mon Sep 17 00:00:00 2001 From: Yannic Date: Thu, 13 Feb 2020 22:04:14 +0100 Subject: [PATCH] [bazel] Move Java runtime/toolchains into //java (#7190) * [bazel] Move Java runtime/toolchains into //java This change moves `java_library` targets from the top-level BUILD file into `//java/{core,lite,util}` and declares `alias` targets to point to their new locations (hence, this is not a breaking change). This will allow users that don't use Java to stop depending on `@rules_java` (e.g. as requested in https://github.com/bazelbuild/rules_scala/pull/989#issuecomment-583405161). Note that there is no intention to deprecate + remove the top-level targets in the foreseeable future. * Add BUILD files in //java to java_EXTRA_DIST --- BUILD | 199 +++++------------- Makefile.am | 8 +- compiler_config_setting.bzl | 4 +- examples/.gitignore | 2 + java/BUILD | 9 + java/core/BUILD | 137 ++++++++++++ java/lite/BUILD | 14 ++ java/lite/pom.xml | 2 +- java/util/BUILD | 20 ++ kokoro/linux/bazel/build.sh | 3 +- protobuf.bzl | 71 +++++-- .../compiler/command_line_interface.cc | 3 +- 12 files changed, 295 insertions(+), 177 deletions(-) create mode 100644 examples/.gitignore create mode 100644 java/BUILD create mode 100644 java/core/BUILD create mode 100644 java/lite/BUILD create mode 100644 java/util/BUILD diff --git a/BUILD b/BUILD index 79871d621c..c45fc455b1 100644 --- a/BUILD +++ b/BUILD @@ -1,7 +1,6 @@ # Bazel (https://bazel.build/) BUILD file for Protobuf. load("@rules_cc//cc:defs.bzl", "cc_binary", "cc_library", "cc_test", "objc_library", native_cc_proto_library = "cc_proto_library") -load("@rules_java//java:defs.bzl", "java_library") load("@rules_proto//proto:defs.bzl", "proto_lang_toolchain", "proto_library") load("@rules_proto//proto/private:native.bzl", "native_proto_common") load("@rules_python//python:defs.bzl", "py_library") @@ -11,17 +10,6 @@ licenses(["notice"]) exports_files(["LICENSE"]) -################################################################################ -# Java 9 configuration -################################################################################ - -config_setting( - name = "jdk9", - values = { - "java_toolchain": "@bazel_tools//tools/jdk:toolchain_jdk9", - }, -) - ################################################################################ # ZLIB configuration ################################################################################ @@ -70,6 +58,10 @@ load(":compiler_config_setting.bzl", "create_compiler_config_setting") create_compiler_config_setting( name = "msvc", value = "msvc-cl", + visibility = [ + # Public, but Protobuf only visibility. + "//:__subpackages__", + ], ) config_setting( @@ -77,6 +69,10 @@ config_setting( values = { "crosstool_top": "//external:android/crosstool", }, + visibility = [ + # Public, but Protobuf only visibility. + "//:__subpackages__", + ], ) config_setting( @@ -84,6 +80,10 @@ config_setting( values = { "crosstool_top": "@androidndk//:toolchain-libcpp", }, + visibility = [ + # Public, but Protobuf only visibility. + "//:__subpackages__", + ], ) config_setting( @@ -91,6 +91,10 @@ config_setting( values = { "crosstool_top": "@androidndk//:toolchain-gnu-libstdcpp", }, + visibility = [ + # Public, but Protobuf only visibility. + "//:__subpackages__", + ], ) # Android and MSVC builds do not need to link in a separate pthread library. @@ -653,144 +657,43 @@ cc_test( ################################################################################ # Java support ################################################################################ + internal_gen_well_known_protos_java( - srcs = WELL_KNOWN_PROTOS, + name = "gen_well_known_protos_java", + deps = [proto + "_proto" for proto in WELL_KNOWN_PROTO_MAP.keys()], + visibility = [ + "//java:__subpackages__", + ], ) -java_library( +alias( name = "protobuf_java", - srcs = glob([ - "java/core/src/main/java/com/google/protobuf/*.java", - ]) + [ - ":gen_well_known_protos_java", - ], - javacopts = select({ - "//:jdk9": ["--add-modules=jdk.unsupported"], - "//conditions:default": [ - "-source 7", - "-target 7", - ], - }), + actual = "//java/core", visibility = ["//visibility:public"], ) -java_library( +alias( name = "protobuf_javalite", - srcs = [ - # Keep in sync with java/lite/pom.xml - "java/core/src/main/java/com/google/protobuf/AbstractMessageLite.java", - "java/core/src/main/java/com/google/protobuf/AbstractParser.java", - "java/core/src/main/java/com/google/protobuf/AbstractProtobufList.java", - "java/core/src/main/java/com/google/protobuf/AllocatedBuffer.java", - "java/core/src/main/java/com/google/protobuf/Android.java", - "java/core/src/main/java/com/google/protobuf/ArrayDecoders.java", - "java/core/src/main/java/com/google/protobuf/BinaryReader.java", - "java/core/src/main/java/com/google/protobuf/BinaryWriter.java", - "java/core/src/main/java/com/google/protobuf/BooleanArrayList.java", - "java/core/src/main/java/com/google/protobuf/BufferAllocator.java", - "java/core/src/main/java/com/google/protobuf/ByteBufferWriter.java", - "java/core/src/main/java/com/google/protobuf/ByteOutput.java", - "java/core/src/main/java/com/google/protobuf/ByteString.java", - "java/core/src/main/java/com/google/protobuf/CodedInputStream.java", - "java/core/src/main/java/com/google/protobuf/CodedInputStreamReader.java", - "java/core/src/main/java/com/google/protobuf/CodedOutputStream.java", - "java/core/src/main/java/com/google/protobuf/CodedOutputStreamWriter.java", - "java/core/src/main/java/com/google/protobuf/DoubleArrayList.java", - "java/core/src/main/java/com/google/protobuf/ExperimentalApi.java", - "java/core/src/main/java/com/google/protobuf/ExtensionLite.java", - "java/core/src/main/java/com/google/protobuf/ExtensionRegistryFactory.java", - "java/core/src/main/java/com/google/protobuf/ExtensionRegistryLite.java", - "java/core/src/main/java/com/google/protobuf/ExtensionSchema.java", - "java/core/src/main/java/com/google/protobuf/ExtensionSchemaLite.java", - "java/core/src/main/java/com/google/protobuf/ExtensionSchemas.java", - "java/core/src/main/java/com/google/protobuf/FieldInfo.java", - "java/core/src/main/java/com/google/protobuf/FieldSet.java", - "java/core/src/main/java/com/google/protobuf/FieldType.java", - "java/core/src/main/java/com/google/protobuf/FloatArrayList.java", - "java/core/src/main/java/com/google/protobuf/GeneratedMessageInfoFactory.java", - "java/core/src/main/java/com/google/protobuf/GeneratedMessageLite.java", - "java/core/src/main/java/com/google/protobuf/IntArrayList.java", - "java/core/src/main/java/com/google/protobuf/Internal.java", - "java/core/src/main/java/com/google/protobuf/InvalidProtocolBufferException.java", - "java/core/src/main/java/com/google/protobuf/IterableByteBufferInputStream.java", - "java/core/src/main/java/com/google/protobuf/JavaType.java", - "java/core/src/main/java/com/google/protobuf/LazyField.java", - "java/core/src/main/java/com/google/protobuf/LazyFieldLite.java", - "java/core/src/main/java/com/google/protobuf/LazyStringArrayList.java", - "java/core/src/main/java/com/google/protobuf/LazyStringList.java", - "java/core/src/main/java/com/google/protobuf/ListFieldSchema.java", - "java/core/src/main/java/com/google/protobuf/LongArrayList.java", - "java/core/src/main/java/com/google/protobuf/ManifestSchemaFactory.java", - "java/core/src/main/java/com/google/protobuf/MapEntryLite.java", - "java/core/src/main/java/com/google/protobuf/MapFieldLite.java", - "java/core/src/main/java/com/google/protobuf/MapFieldSchema.java", - "java/core/src/main/java/com/google/protobuf/MapFieldSchemaLite.java", - "java/core/src/main/java/com/google/protobuf/MapFieldSchemas.java", - "java/core/src/main/java/com/google/protobuf/MessageInfo.java", - "java/core/src/main/java/com/google/protobuf/MessageInfoFactory.java", - "java/core/src/main/java/com/google/protobuf/MessageLite.java", - "java/core/src/main/java/com/google/protobuf/MessageLiteOrBuilder.java", - "java/core/src/main/java/com/google/protobuf/MessageLiteToString.java", - "java/core/src/main/java/com/google/protobuf/MessageSchema.java", - "java/core/src/main/java/com/google/protobuf/MessageSetSchema.java", - "java/core/src/main/java/com/google/protobuf/MutabilityOracle.java", - "java/core/src/main/java/com/google/protobuf/NewInstanceSchema.java", - "java/core/src/main/java/com/google/protobuf/NewInstanceSchemaLite.java", - "java/core/src/main/java/com/google/protobuf/NewInstanceSchemas.java", - "java/core/src/main/java/com/google/protobuf/NioByteString.java", - "java/core/src/main/java/com/google/protobuf/OneofInfo.java", - "java/core/src/main/java/com/google/protobuf/Parser.java", - "java/core/src/main/java/com/google/protobuf/PrimitiveNonBoxingCollection.java", - "java/core/src/main/java/com/google/protobuf/ProtoSyntax.java", - "java/core/src/main/java/com/google/protobuf/Protobuf.java", - "java/core/src/main/java/com/google/protobuf/ProtobufArrayList.java", - "java/core/src/main/java/com/google/protobuf/ProtobufLists.java", - "java/core/src/main/java/com/google/protobuf/ProtocolStringList.java", - "java/core/src/main/java/com/google/protobuf/RawMessageInfo.java", - "java/core/src/main/java/com/google/protobuf/Reader.java", - "java/core/src/main/java/com/google/protobuf/RopeByteString.java", - "java/core/src/main/java/com/google/protobuf/Schema.java", - "java/core/src/main/java/com/google/protobuf/SchemaFactory.java", - "java/core/src/main/java/com/google/protobuf/SchemaUtil.java", - "java/core/src/main/java/com/google/protobuf/SmallSortedMap.java", - "java/core/src/main/java/com/google/protobuf/StructuralMessageInfo.java", - "java/core/src/main/java/com/google/protobuf/TextFormatEscaper.java", - "java/core/src/main/java/com/google/protobuf/UninitializedMessageException.java", - "java/core/src/main/java/com/google/protobuf/UnknownFieldSchema.java", - "java/core/src/main/java/com/google/protobuf/UnknownFieldSetLite.java", - "java/core/src/main/java/com/google/protobuf/UnknownFieldSetLiteSchema.java", - "java/core/src/main/java/com/google/protobuf/UnmodifiableLazyStringList.java", - "java/core/src/main/java/com/google/protobuf/UnsafeUtil.java", - "java/core/src/main/java/com/google/protobuf/Utf8.java", - "java/core/src/main/java/com/google/protobuf/WireFormat.java", - "java/core/src/main/java/com/google/protobuf/Writer.java", - ], - javacopts = select({ - "//:jdk9": ["--add-modules=jdk.unsupported"], - "//conditions:default": [ - "-source 7", - "-target 7", - ], - }), + actual = "//java/lite", visibility = ["//visibility:public"], ) -java_library( +alias( name = "protobuf_java_util", - srcs = glob([ - "java/util/src/main/java/com/google/protobuf/util/*.java", - ]), - javacopts = [ - "-source 7", - "-target 7", - ], + actual = "//java/util", + visibility = ["//visibility:public"], +) + +alias( + name = "java_toolchain", + actual = "//java/core:toolchain", + visibility = ["//visibility:public"], +) + +alias( + name = "javalite_toolchain", + actual = "//java/lite:toolchain", visibility = ["//visibility:public"], - deps = [ - "protobuf_java", - "//external:error_prone_annotations", - "//external:gson", - "//external:guava", - ], ) ################################################################################ @@ -859,6 +762,10 @@ config_setting( values = { "define": "use_fast_cpp_protos=true", }, + visibility = [ + # Public, but Protobuf only visibility. + "//:__subpackages__", + ], ) config_setting( @@ -866,6 +773,10 @@ config_setting( values = { "define": "allow_oversize_protos=true", }, + visibility = [ + # Public, but Protobuf only visibility. + "//:__subpackages__", + ], ) # Copy the builtin proto files from src/google/protobuf to @@ -1011,20 +922,6 @@ proto_lang_toolchain( visibility = ["//visibility:public"], ) -proto_lang_toolchain( - name = "java_toolchain", - command_line = "--java_out=$(OUT)", - runtime = ":protobuf_java", - visibility = ["//visibility:public"], -) - -proto_lang_toolchain( - name = "javalite_toolchain", - command_line = "--java_out=lite:$(OUT)", - runtime = ":protobuf_javalite", - visibility = ["//visibility:public"], -) - alias( name = "objectivec", actual = ":protobuf_objc", diff --git a/Makefile.am b/Makefile.am index 704aad699a..07968e941c 100644 --- a/Makefile.am +++ b/Makefile.am @@ -247,8 +247,10 @@ csharp_EXTRA_DIST= \ csharp/src/Google.Protobuf/UnknownFieldSet.cs java_EXTRA_DIST= \ + java/BUILD \ java/README.md \ java/bom/pom.xml \ + java/core/BUILD \ java/core/generate-sources-build.xml \ java/core/generate-test-sources-build.xml \ java/core/pom.xml \ @@ -504,6 +506,7 @@ java_EXTRA_DIST= java/core/src/test/proto/com/google/protobuf/test_extra_interfaces.proto \ java/core/src/test/proto/com/google/protobuf/wrappers_test.proto \ java/lite.md \ + java/lite/BUILD \ java/lite/generate-sources-build.xml \ java/lite/generate-test-sources-build.xml \ java/lite/lite.awk \ @@ -512,6 +515,7 @@ java_EXTRA_DIST= java/lite/src/test/java/com/google/protobuf/LiteTest.java \ java/lite/src/test/java/com/google/protobuf/Proto2MessageLiteInfoFactory.java \ java/pom.xml \ + java/util/BUILD \ java/util/pom.xml \ java/util/src/main/java/com/google/protobuf/util/Durations.java \ java/util/src/main/java/com/google/protobuf/util/FieldMaskTree.java \ @@ -1298,7 +1302,7 @@ EXTRA_DIST = $(@DIST_LANG@_EXTRA_DIST) \ WORKSPACE \ cmake/CMakeLists.txt \ cmake/README.md \ - cmake/conformance.cmake \ + cmake/conformance.cmake \ cmake/examples.cmake \ cmake/extract_includes.bat.in \ cmake/install.cmake \ @@ -1315,6 +1319,8 @@ EXTRA_DIST = $(@DIST_LANG@_EXTRA_DIST) \ cmake/tests.cmake \ cmake/version.rc.in \ compiler_config_setting.bzl \ + build_files_updated_unittest.sh \ + cc_proto_blacklist_test.bzl \ editors/README.txt \ editors/proto.vim \ editors/protobuf-mode.el \ diff --git a/compiler_config_setting.bzl b/compiler_config_setting.bzl index 5e52a6524f..f4d1f7b9ef 100644 --- a/compiler_config_setting.bzl +++ b/compiler_config_setting.bzl @@ -1,6 +1,6 @@ """Creates config_setting that allows selecting based on 'compiler' value.""" -def create_compiler_config_setting(name, value): +def create_compiler_config_setting(name, value, visibility = None): # The "do_not_use_tools_cpp_compiler_present" attribute exists to # distinguish between older versions of Bazel that do not support # "@bazel_tools//tools/cpp:compiler" flag_value, and newer ones that do. @@ -13,9 +13,11 @@ def create_compiler_config_setting(name, value): flag_values = { "@bazel_tools//tools/cpp:compiler": value, }, + visibility = visibility, ) else: native.config_setting( name = name, values = {"compiler": value}, + visibility = visibility, ) diff --git a/examples/.gitignore b/examples/.gitignore new file mode 100644 index 0000000000..229542d23c --- /dev/null +++ b/examples/.gitignore @@ -0,0 +1,2 @@ +# Ignore the bazel symlinks +/bazel-* diff --git a/java/BUILD b/java/BUILD new file mode 100644 index 0000000000..779fa9e5bb --- /dev/null +++ b/java/BUILD @@ -0,0 +1,9 @@ +config_setting( + name = "jdk9", + values = { + "java_toolchain": "@bazel_tools//tools/jdk:toolchain_jdk9", + }, + visibility = [ + "//java:__subpackages__", + ], +) diff --git a/java/core/BUILD b/java/core/BUILD new file mode 100644 index 0000000000..5f4a7ab177 --- /dev/null +++ b/java/core/BUILD @@ -0,0 +1,137 @@ +load("@rules_java//java:defs.bzl", "java_library") +load("@rules_proto//proto:defs.bzl", "proto_lang_toolchain") + +LITE_SRCS = [ + # Keep in sync with `//java/lite:pom.xml`. + "src/main/java/com/google/protobuf/AbstractMessageLite.java", + "src/main/java/com/google/protobuf/AbstractParser.java", + "src/main/java/com/google/protobuf/AbstractProtobufList.java", + "src/main/java/com/google/protobuf/AllocatedBuffer.java", + "src/main/java/com/google/protobuf/Android.java", + "src/main/java/com/google/protobuf/ArrayDecoders.java", + "src/main/java/com/google/protobuf/BinaryReader.java", + "src/main/java/com/google/protobuf/BinaryWriter.java", + "src/main/java/com/google/protobuf/BooleanArrayList.java", + "src/main/java/com/google/protobuf/BufferAllocator.java", + "src/main/java/com/google/protobuf/ByteBufferWriter.java", + "src/main/java/com/google/protobuf/ByteOutput.java", + "src/main/java/com/google/protobuf/ByteString.java", + "src/main/java/com/google/protobuf/CodedInputStream.java", + "src/main/java/com/google/protobuf/CodedInputStreamReader.java", + "src/main/java/com/google/protobuf/CodedOutputStream.java", + "src/main/java/com/google/protobuf/CodedOutputStreamWriter.java", + "src/main/java/com/google/protobuf/DoubleArrayList.java", + "src/main/java/com/google/protobuf/ExperimentalApi.java", + "src/main/java/com/google/protobuf/ExtensionLite.java", + "src/main/java/com/google/protobuf/ExtensionRegistryFactory.java", + "src/main/java/com/google/protobuf/ExtensionRegistryLite.java", + "src/main/java/com/google/protobuf/ExtensionSchema.java", + "src/main/java/com/google/protobuf/ExtensionSchemaLite.java", + "src/main/java/com/google/protobuf/ExtensionSchemas.java", + "src/main/java/com/google/protobuf/FieldInfo.java", + "src/main/java/com/google/protobuf/FieldSet.java", + "src/main/java/com/google/protobuf/FieldType.java", + "src/main/java/com/google/protobuf/FloatArrayList.java", + "src/main/java/com/google/protobuf/GeneratedMessageInfoFactory.java", + "src/main/java/com/google/protobuf/GeneratedMessageLite.java", + "src/main/java/com/google/protobuf/IntArrayList.java", + "src/main/java/com/google/protobuf/Internal.java", + "src/main/java/com/google/protobuf/InvalidProtocolBufferException.java", + "src/main/java/com/google/protobuf/IterableByteBufferInputStream.java", + "src/main/java/com/google/protobuf/JavaType.java", + "src/main/java/com/google/protobuf/LazyField.java", + "src/main/java/com/google/protobuf/LazyFieldLite.java", + "src/main/java/com/google/protobuf/LazyStringArrayList.java", + "src/main/java/com/google/protobuf/LazyStringList.java", + "src/main/java/com/google/protobuf/ListFieldSchema.java", + "src/main/java/com/google/protobuf/LongArrayList.java", + "src/main/java/com/google/protobuf/ManifestSchemaFactory.java", + "src/main/java/com/google/protobuf/MapEntryLite.java", + "src/main/java/com/google/protobuf/MapFieldLite.java", + "src/main/java/com/google/protobuf/MapFieldSchema.java", + "src/main/java/com/google/protobuf/MapFieldSchemaLite.java", + "src/main/java/com/google/protobuf/MapFieldSchemas.java", + "src/main/java/com/google/protobuf/MessageInfo.java", + "src/main/java/com/google/protobuf/MessageInfoFactory.java", + "src/main/java/com/google/protobuf/MessageLite.java", + "src/main/java/com/google/protobuf/MessageLiteOrBuilder.java", + "src/main/java/com/google/protobuf/MessageLiteToString.java", + "src/main/java/com/google/protobuf/MessageSchema.java", + "src/main/java/com/google/protobuf/MessageSetSchema.java", + "src/main/java/com/google/protobuf/MutabilityOracle.java", + "src/main/java/com/google/protobuf/NewInstanceSchema.java", + "src/main/java/com/google/protobuf/NewInstanceSchemaLite.java", + "src/main/java/com/google/protobuf/NewInstanceSchemas.java", + "src/main/java/com/google/protobuf/NioByteString.java", + "src/main/java/com/google/protobuf/OneofInfo.java", + "src/main/java/com/google/protobuf/Parser.java", + "src/main/java/com/google/protobuf/PrimitiveNonBoxingCollection.java", + "src/main/java/com/google/protobuf/ProtoSyntax.java", + "src/main/java/com/google/protobuf/Protobuf.java", + "src/main/java/com/google/protobuf/ProtobufArrayList.java", + "src/main/java/com/google/protobuf/ProtobufLists.java", + "src/main/java/com/google/protobuf/ProtocolStringList.java", + "src/main/java/com/google/protobuf/RawMessageInfo.java", + "src/main/java/com/google/protobuf/Reader.java", + "src/main/java/com/google/protobuf/RopeByteString.java", + "src/main/java/com/google/protobuf/Schema.java", + "src/main/java/com/google/protobuf/SchemaFactory.java", + "src/main/java/com/google/protobuf/SchemaUtil.java", + "src/main/java/com/google/protobuf/SmallSortedMap.java", + "src/main/java/com/google/protobuf/StructuralMessageInfo.java", + "src/main/java/com/google/protobuf/TextFormatEscaper.java", + "src/main/java/com/google/protobuf/UninitializedMessageException.java", + "src/main/java/com/google/protobuf/UnknownFieldSchema.java", + "src/main/java/com/google/protobuf/UnknownFieldSetLite.java", + "src/main/java/com/google/protobuf/UnknownFieldSetLiteSchema.java", + "src/main/java/com/google/protobuf/UnmodifiableLazyStringList.java", + "src/main/java/com/google/protobuf/UnsafeUtil.java", + "src/main/java/com/google/protobuf/Utf8.java", + "src/main/java/com/google/protobuf/WireFormat.java", + "src/main/java/com/google/protobuf/Writer.java", +] + +javacopts = select({ + "//java:jdk9": ["--add-modules=jdk.unsupported"], + "//conditions:default": [ + "-source 7", + "-target 7", + ], +}) + +# Should be used as `//java/lite`. +java_library( + name = "lite", + srcs = LITE_SRCS, + javacopts = javacopts, + visibility = [ + "//java/lite:__pkg__", + ], +) + +java_library( + name = "core", + srcs = glob( + [ + "src/main/java/com/google/protobuf/*.java", + ], + exclude = LITE_SRCS, + ) + [ + "//:gen_well_known_protos_java", + ], + javacopts = javacopts, + visibility = ["//visibility:public"], + exports = [ + "//java/lite", + ], + deps = [ + "//java/lite", + ], +) + +proto_lang_toolchain( + name = "toolchain", + command_line = "--java_out=$(OUT)", + runtime = ":core", + visibility = ["//visibility:public"], +) diff --git a/java/lite/BUILD b/java/lite/BUILD new file mode 100644 index 0000000000..22840ec48c --- /dev/null +++ b/java/lite/BUILD @@ -0,0 +1,14 @@ +load("@rules_proto//proto:defs.bzl", "proto_lang_toolchain") + +alias( + name = "lite", + actual = "//java/core:lite", + visibility = ["//visibility:public"], +) + +proto_lang_toolchain( + name = "toolchain", + command_line = "--java_out=lite:$(OUT)", + runtime = ":lite", + visibility = ["//visibility:public"], +) diff --git a/java/lite/pom.xml b/java/lite/pom.xml index 81e052104f..61863adafc 100644 --- a/java/lite/pom.xml +++ b/java/lite/pom.xml @@ -89,7 +89,7 @@ ${basedir}/../core/src/main/java/com/google/protobuf - + AbstractMessageLite.java AbstractParser.java AbstractProtobufList.java diff --git a/java/util/BUILD b/java/util/BUILD new file mode 100644 index 0000000000..cfdb28e2e1 --- /dev/null +++ b/java/util/BUILD @@ -0,0 +1,20 @@ +load("@rules_java//java:defs.bzl", "java_library") + +java_library( + name = "util", + srcs = glob([ + "src/main/java/com/google/protobuf/util/*.java", + ]), + javacopts = [ + "-source 7", + "-target 7", + ], + visibility = ["//visibility:public"], + deps = [ + "//external:error_prone_annotations", + "//external:gson", + "//external:guava", + "//java/core", + "//java/lite", + ], +) diff --git a/kokoro/linux/bazel/build.sh b/kokoro/linux/bazel/build.sh index b5991d773a..9355ebae30 100755 --- a/kokoro/linux/bazel/build.sh +++ b/kokoro/linux/bazel/build.sh @@ -25,9 +25,10 @@ git submodule update --init --recursive trap print_test_logs EXIT bazel test --copt=-Werror --host_copt=-Werror \ //:build_files_updated_unittest \ + //java/... \ //:protobuf_test \ @com_google_protobuf//:cc_proto_blacklist_test trap - EXIT cd examples -bazel build :all +bazel build //... diff --git a/protobuf.bzl b/protobuf.bzl index 5fa5543b18..26e84f5ad8 100644 --- a/protobuf.bzl +++ b/protobuf.bzl @@ -318,30 +318,59 @@ def cc_proto_library( **kargs ) -def internal_gen_well_known_protos_java(srcs): - """Bazel rule to generate the gen_well_known_protos_java genrule +def _internal_gen_well_known_protos_java_impl(ctx): + args = ctx.actions.args() - Args: - srcs: the well known protos - """ - root = Label("%s//protobuf_java" % (native.repository_name())).workspace_root - pkg = native.package_name() + "/" if native.package_name() else "" - if root == "": - include = " -I%ssrc " % pkg - else: - include = " -I%s/%ssrc " % (root, pkg) - native.genrule( - name = "gen_well_known_protos_java", - srcs = srcs, - outs = [ - "wellknown.srcjar", - ], - cmd = "$(location :protoc) --java_out=$(@D)/wellknown.jar" + - " %s $(SRCS) " % include + - " && mv $(@D)/wellknown.jar $(@D)/wellknown.srcjar", - tools = [":protoc"], + deps = [d[ProtoInfo] for d in ctx.attr.deps] + + srcjar = ctx.actions.declare_file("{}.srcjar".format(ctx.attr.name)) + args.add("--java_out", srcjar) + + descriptors = depset( + transitive = [dep.transitive_descriptor_sets for dep in deps], + ) + args.add_joined( + "--descriptor_set_in", + descriptors, + join_with = ctx.configuration.host_path_separator, + ) + + for dep in deps: + if "." == dep.proto_source_root: + args.add_all([src.path for src in dep.direct_sources]) + else: + source_root = dep.proto_source_root + offset = len(source_root) + 1 # + '/'. + args.add_all([src.path[offset:] for src in dep.direct_sources]) + + ctx.actions.run( + executable = ctx.executable._protoc, + inputs = descriptors, + outputs = [srcjar], + arguments = [args], ) + return [ + DefaultInfo( + files = depset([srcjar]), + ), + ] + +internal_gen_well_known_protos_java = rule( + implementation = _internal_gen_well_known_protos_java_impl, + attrs = { + "deps": attr.label_list( + mandatory = True, + providers = [ProtoInfo], + ), + "_protoc": attr.label( + executable = True, + cfg = "host", + default = "@com_google_protobuf//:protoc", + ), + }, +) + def internal_copied_filegroup(name, srcs, strip_prefix, dest, **kwargs): """Macro to copy files to a different directory and then create a filegroup. diff --git a/src/google/protobuf/compiler/command_line_interface.cc b/src/google/protobuf/compiler/command_line_interface.cc index 060c2f6566..fd64d0f4a0 100644 --- a/src/google/protobuf/compiler/command_line_interface.cc +++ b/src/google/protobuf/compiler/command_line_interface.cc @@ -886,7 +886,8 @@ int CommandLineInterface::Run(int argc, const char* const argv[]) { for (int i = 0; i < output_directives_.size(); i++) { std::string output_location = output_directives_[i].output_location; if (!HasSuffixString(output_location, ".zip") && - !HasSuffixString(output_location, ".jar")) { + !HasSuffixString(output_location, ".jar") && + !HasSuffixString(output_location, ".srcjar")) { AddTrailingSlash(&output_location); }