From 3a4d9316aa9e3f0afec58e83ed744b0be4d337fa Mon Sep 17 00:00:00 2001 From: Derek Perez Date: Thu, 4 Nov 2021 22:57:29 +0000 Subject: [PATCH] Creates a protoc release zip (#9188) * Creates a protoc release zip Mimics what we're doing manually today but powered by Bazel. * don't special case this. * Addressing feedback. * adding comment about _cc_toolchain --- BUILD | 67 ++++++++++++++++++++++++++++++- Makefile.am | 1 + WORKSPACE | 3 ++ protobuf_deps.bzl | 10 +++++ protobuf_release.bzl | 28 +++++++++++++ toolchain/cc_toolchain_config.bzl | 1 - 6 files changed, 108 insertions(+), 2 deletions(-) create mode 100644 protobuf_release.bzl diff --git a/BUILD b/BUILD index 1690d42198..87bd219c07 100644 --- a/BUILD +++ b/BUILD @@ -2,11 +2,13 @@ load("@bazel_skylib//rules:common_settings.bzl", "string_flag") load("@rules_cc//cc:defs.bzl", "cc_binary", "cc_library", "cc_test", "objc_library", native_cc_proto_library = "cc_proto_library") +load("@rules_pkg//:pkg.bzl", "pkg_zip") +load("@rules_pkg//:mappings.bzl", "pkg_files") load("@rules_proto//proto:defs.bzl", "proto_lang_toolchain", "proto_library") load("@rules_python//python:defs.bzl", "py_library") load("@rules_java//java:defs.bzl", "java_binary", "java_lite_proto_library", "java_proto_library") load(":cc_proto_blacklist_test.bzl", "cc_proto_blacklist_test") - +load(":protobuf_release.bzl", "package_naming") licenses(["notice"]) exports_files(["LICENSE"]) @@ -518,6 +520,69 @@ cc_binary( deps = [":protoc_lib"], ) + +################################################################################ +# Generates protoc release artifacts. +################################################################################ + +genrule( + name = "protoc_readme", + visibility = ["//visibility:private"], + cmd = """ +echo "Protocol Buffers - Google's data interchange format +Copyright 2008 Google Inc. +https://developers.google.com/protocol-buffers/ +This package contains a precompiled binary version of the protocol buffer +compiler (protoc). This binary is intended for users who want to use Protocol +Buffers in languages other than C++ but do not want to compile protoc +themselves. To install, simply place this binary somewhere in your PATH. +If you intend to use the included well known types then don't forget to +copy the contents of the 'include' directory somewhere as well, for example +into '/usr/local/include/'. +Please refer to our official github site for more installation instructions: + https://github.com/protocolbuffers/protobuf" > $@ + """, + outs = ["readme.txt"], +) + +# plugin.proto is excluded from this list because it belongs in a nested folder (protobuf/compiler/plugin.proto) +pkg_files( + name = "wkt_protos_files", + srcs = [value[0] for value in WELL_KNOWN_PROTO_MAP.values() if not value[0].endswith("plugin.proto")], + visibility = ["//visibility:private"], + prefix = "include/google/protobuf", +) + +pkg_files( + name = "compiler_plugin_protos_files", + srcs = ["src/google/protobuf/compiler/plugin.proto"], + visibility = ["//visibility:private"], + prefix = "include/google/protobuf/compiler", +) + +pkg_files( + name = "protoc_files", + srcs = [":protoc"], + visibility = ["//visibility:private"], + prefix = "bin/", +) + +package_naming( + name = "protoc_pkg_naming", +) + +pkg_zip( + name = "protoc_release", + package_file_name = "protoc-{version}-{cpu}.zip", + package_variables = ":protoc_pkg_naming", + srcs = [ + ":protoc_files", + ":wkt_protos_files", + ":compiler_plugin_protos_files", + "readme.txt", + ], +) + ################################################################################ # Tests ################################################################################ diff --git a/Makefile.am b/Makefile.am index 8e9f1bfdc7..ae86b8f05d 100644 --- a/Makefile.am +++ b/Makefile.am @@ -1429,6 +1429,7 @@ EXTRA_DIST = $(@DIST_LANG@_EXTRA_DIST) \ maven_install.json \ protobuf.bzl \ protobuf_deps.bzl \ + protobuf_release.bzl \ protobuf_version.bzl \ third_party/zlib.BUILD \ util/python/BUILD \ diff --git a/WORKSPACE b/WORKSPACE index e500967fe4..8104dccaf1 100644 --- a/WORKSPACE +++ b/WORKSPACE @@ -56,3 +56,6 @@ pinned_maven_install() load("@bazel_skylib//:workspace.bzl", "bazel_skylib_workspace") bazel_skylib_workspace() + +load("@rules_pkg//:deps.bzl", "rules_pkg_dependencies") +rules_pkg_dependencies() diff --git a/protobuf_deps.bzl b/protobuf_deps.bzl index 45e413d94b..2e628868e6 100644 --- a/protobuf_deps.bzl +++ b/protobuf_deps.bzl @@ -73,3 +73,13 @@ def protobuf_deps(): strip_prefix = "rules_jvm_external-4.1", urls = ["https://github.com/bazelbuild/rules_jvm_external/archive/4.1.zip"], ) + + if not native.existing_rule("rules_pkg"): + http_archive( + name = "rules_pkg", + urls = [ + "https://mirror.bazel.build/github.com/bazelbuild/rules_pkg/releases/download/0.5.1/rules_pkg-0.5.1.tar.gz", + "https://github.com/bazelbuild/rules_pkg/releases/download/0.5.1/rules_pkg-0.5.1.tar.gz", + ], + sha256 = "a89e203d3cf264e564fcb96b6e06dd70bc0557356eb48400ce4b5d97c2c3720d", + ) diff --git a/protobuf_release.bzl b/protobuf_release.bzl new file mode 100644 index 0000000000..e007e004d7 --- /dev/null +++ b/protobuf_release.bzl @@ -0,0 +1,28 @@ +""" +Generates package naming variables for use with rules_pkg. +""" + +load("@rules_pkg//:providers.bzl", "PackageVariablesInfo") +load("@bazel_tools//tools/cpp:toolchain_utils.bzl", "find_cpp_toolchain") +load(":protobuf_version.bzl", "PROTOBUF_VERSION") + +def _package_naming_impl(ctx): + values = {} + values["version"] = PROTOBUF_VERSION + + # infer from the current cpp toolchain. + toolchain = find_cpp_toolchain(ctx) + values["cpu"] = toolchain.cpu + + return PackageVariablesInfo(values = values) + + +package_naming = rule( + implementation = _package_naming_impl, + attrs = { + # Necessary data dependency for find_cpp_toolchain. + "_cc_toolchain": attr.label(default = Label("@bazel_tools//tools/cpp:current_cc_toolchain")), + }, + toolchains = ["@bazel_tools//tools/cpp:toolchain_type"], + incompatible_use_toolchain_transition = True, +) diff --git a/toolchain/cc_toolchain_config.bzl b/toolchain/cc_toolchain_config.bzl index 4478a11416..568b056c70 100644 --- a/toolchain/cc_toolchain_config.bzl +++ b/toolchain/cc_toolchain_config.bzl @@ -4,7 +4,6 @@ load( "feature", "flag_group", "flag_set", - "tool", "tool_path", )