Skip to content

Commit

Permalink
Creates a protoc release zip (#9188)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
perezd committed Nov 4, 2021
1 parent 11de748 commit 3a4d931
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 2 deletions.
67 changes: 66 additions & 1 deletion BUILD
Expand Up @@ -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"])
Expand Down Expand Up @@ -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
################################################################################
Expand Down
1 change: 1 addition & 0 deletions Makefile.am
Expand Up @@ -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 \
Expand Down
3 changes: 3 additions & 0 deletions WORKSPACE
Expand Up @@ -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()
10 changes: 10 additions & 0 deletions protobuf_deps.bzl
Expand Up @@ -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",
)
28 changes: 28 additions & 0 deletions 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,
)
1 change: 0 additions & 1 deletion toolchain/cc_toolchain_config.bzl
Expand Up @@ -4,7 +4,6 @@ load(
"feature",
"flag_group",
"flag_set",
"tool",
"tool_path",
)

Expand Down

0 comments on commit 3a4d931

Please sign in to comment.