Skip to content

Commit

Permalink
Create /release_crates/ directory that contains one directory for eac…
Browse files Browse the repository at this point in the history
…h of our 3 crates that we package up.

PiperOrigin-RevId: 702324286
  • Loading branch information
protobuf-github-bot authored and copybara-github committed Dec 3, 2024
1 parent 8f6afdc commit 16260b1
Show file tree
Hide file tree
Showing 20 changed files with 125 additions and 164 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test_rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ jobs:
- config: { name: Cargo }
image: "us-docker.pkg.dev/protobuf-build/containers/release/linux/rust:6.3.0-1.74.0-8858126dd9480abf91e6ce8d6e41a5cd3c03882c"
bazel_cmd: "run"
targets: "//rust:cargo_test"
targets: "//rust/release_crates:cargo_test"
name: Linux ${{ matrix.config.name }}
runs-on: ubuntu-22-4core
steps:
Expand Down
115 changes: 4 additions & 111 deletions rust/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

load("@bazel_skylib//rules:common_settings.bzl", "string_flag")
load("@rules_pkg//pkg:mappings.bzl", "pkg_filegroup", "pkg_files", "strip_prefix")
load("@rules_pkg//pkg:tar.bzl", "pkg_tar")
load("@rules_rust//rust:defs.bzl", "rust_library", "rust_test")
load("//bazel/toolchains:proto_lang_toolchain.bzl", "proto_lang_toolchain")
load("//rust:dist.bzl", "pkg_cross_compiled_binaries")
Expand Down Expand Up @@ -261,20 +260,10 @@ config_setting(
},
)

# Note: strip_prefix.from_pkg() should work for this below, but has surprising behavior in bazel
# with filegroups and so we only use strip_prefix.from_root() and this constant instead.
SRC_ROOT = "rust"

pkg_files(
name = "rust_protobuf_src",
srcs = ALL_RUST_SRCS,
strip_prefix = strip_prefix.from_root(SRC_ROOT),
)

pkg_files(
name = "crate_root_files",
srcs = ["//rust/cargo:srcs"],
strip_prefix = strip_prefix.from_root(SRC_ROOT + "/cargo"),
strip_prefix = strip_prefix.from_root("rust"),
)

pkg_filegroup(
Expand All @@ -284,6 +273,7 @@ pkg_filegroup(
"//rust/upb:rust_protobuf_upb_src",
],
prefix = "src",
visibility = ["//rust/release_crates:__subpackages__"],
)

pkg_files(
Expand All @@ -299,79 +289,7 @@ pkg_filegroup(
"//upb/cmake:upb_cmake_dist",
],
prefix = "libupb",
)

pkg_tar(
name = "protobuf_crate_dist",
srcs = [
":crate_root_files",
":rust_protobuf_libupb_src",
":rust_protobuf_src_dir",
"//:LICENSE",
],
)

pkg_files(
name = "protobuf_codegen_files",
srcs = ["//rust/protobuf_codegen:srcs"],
strip_prefix = strip_prefix.from_root(SRC_ROOT + "/protobuf_codegen"),
)

pkg_tar(
name = "codegen_crate_dist",
srcs = [
":protobuf_codegen_files",
":vendored_protocs_dist",
"//:LICENSE",
],
tags = ["manual"],
)

pkg_tar(
name = "codegen_crate_test",
srcs = [
":protobuf_codegen_files",
":vendored_protocs_test",
"//:LICENSE",
],
tags = ["manual"],
)

pkg_files(
name = "codegen_example_files",
srcs = ["//rust/protobuf_codegen/example:srcs"],
strip_prefix = strip_prefix.from_root(SRC_ROOT + "/protobuf_codegen/example"),
)

pkg_tar(
name = "codegen_example_test",
srcs = [
":codegen_example_files",
"//:LICENSE",
],
)

# Bundle all protoc binaries for all platforms. Requires the toolchains to be installed.
pkg_cross_compiled_binaries(
name = "vendored_protocs_dist",
cpus = [
# TODO: Re-enable these platforms once the toolchains are available.
# "osx-x86_64",
# "osx-aarch_64",
"linux-aarch_64",
"linux-ppcle_64",
# "linux-s390_64",
"linux-x86_32",
"linux-x86_64",
"win32",
"win64",
],
prefix = "bin",
tags = ["manual"],
targets = [
"//upb_generator/minitable:protoc-gen-upb_minitable",
"//:protoc",
],
visibility = ["//rust/release_crates:__subpackages__"],
)

# Bundle only the linux-x86_64 protoc for testing.
Expand All @@ -386,30 +304,5 @@ pkg_cross_compiled_binaries(
"//upb_generator/minitable:protoc-gen-upb_minitable",
"//:protoc",
],
)

# Run the cargo test with only a bundled linux-x86_64 protoc.
sh_binary(
name = "cargo_test",
srcs = ["cargo_test.sh"],
data = [
":codegen_crate_test",
":codegen_example_test",
":protobuf_crate_dist",
],
tags = ["manual"],
deps = ["@bazel_tools//tools/bash/runfiles"],
)

# Run the cargo test with all bundled protocs.
sh_binary(
name = "cargo_release_test",
srcs = ["cargo_test.sh"],
data = [
":codegen_crate_dist",
":codegen_example_test",
":protobuf_crate_dist",
],
tags = ["manual"],
deps = ["@bazel_tools//tools/bash/runfiles"],
visibility = ["//rust/release_crates:__subpackages__"],
)
17 changes: 0 additions & 17 deletions rust/cargo/BUILD

This file was deleted.

4 changes: 3 additions & 1 deletion rust/dist.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ _cross_compiled_binary = rule(
},
)

def pkg_cross_compiled_binaries(name, cpus, targets, prefix, tags):
def pkg_cross_compiled_binaries(name, cpus, targets, prefix, tags, visibility = None):
"""Creates a pkg_filegroup that contains the cross compiled binaries for each cpu.
This rule is used to create a pkg_filegroup that contains the cross compiled binaries for each
Expand All @@ -60,6 +60,7 @@ def pkg_cross_compiled_binaries(name, cpus, targets, prefix, tags):
targets: The targets to cross compile.
prefix: The prefix to add to the pkg_filegroup.
tags: The tags to add to the pkg_filegroup.
visibility: The visibility of the pkg_filegroup.
"""

filegroups = []
Expand Down Expand Up @@ -94,5 +95,6 @@ def pkg_cross_compiled_binaries(name, cpus, targets, prefix, tags):
srcs = filegroups,
prefix = prefix,
tags = tags,
visibility = visibility,
)
return
16 changes: 0 additions & 16 deletions rust/protobuf_codegen/BUILD

This file was deleted.

5 changes: 0 additions & 5 deletions rust/protobuf_codegen/example/BUILD

This file was deleted.

14 changes: 14 additions & 0 deletions rust/release_crates/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Protobuf Rust crate packaging for release.

# Run the cargo test with only a bundled linux-x86_64 protoc.
sh_binary(
name = "cargo_test",
srcs = ["cargo_test.sh"],
data = [
"//rust/release_crates/protobuf:protobuf_crate",
"//rust/release_crates/protobuf_codegen:protobuf_codegen_crate",
"//rust/release_crates/protobuf_example:protobuf_example_crate",
],
tags = ["manual"],
deps = ["@bazel_tools//tools/bash/runfiles"],
)
18 changes: 7 additions & 11 deletions rust/cargo_test.sh → rust/release_crates/cargo_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -35,29 +35,25 @@ mkdir $CARGO_HOME
CRATE_ROOT=$TMP_DIR/protobuf
mkdir $CRATE_ROOT

PROTOBUF_TAR=$(rlocation com_google_protobuf/rust/protobuf_crate_dist.tar)
PROTOBUF_TAR=$(rlocation com_google_protobuf/rust/release_crates/protobuf/protobuf_crate.tar)

echo "Expanding protobuf_crate tar"
echo "Expanding protobuf crate tar"
tar -xvf $PROTOBUF_TAR -C $CRATE_ROOT

CODEGEN_ROOT=$TMP_DIR/protobuf_codegen
mkdir $CODEGEN_ROOT

CODEGEN_TAR=$(rlocation com_google_protobuf/rust/codegen_crate_dist.tar)
CODEGEN_TAR=$(rlocation com_google_protobuf/rust/release_crates/protobuf_codegen/protobuf_codegen_crate.tar)

if [[ ! -f $CODEGEN_TAR ]]; then
CODEGEN_TAR=$(rlocation com_google_protobuf/rust/codegen_crate_test.tar)
fi

echo "Expanding codegen_crate tar"
echo "Expanding protbuf_codegen crate tar"
tar -xvf $CODEGEN_TAR -C $CODEGEN_ROOT

EXAMPLE_ROOT=$TMP_DIR/codegen_example
EXAMPLE_ROOT=$TMP_DIR/protobuf_example
mkdir $EXAMPLE_ROOT

EXAMPLE_TAR=$(rlocation com_google_protobuf/rust/codegen_example_test.tar)
EXAMPLE_TAR=$(rlocation com_google_protobuf/rust/release_crates/protobuf_example/protobuf_example_crate.tar)

echo "Expanding codegen_example tar"
echo "Expanding protobuf_example crate tar"
tar -xvf $EXAMPLE_TAR -C $EXAMPLE_ROOT

cd $CRATE_ROOT
Expand Down
36 changes: 36 additions & 0 deletions rust/release_crates/protobuf/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
load("@rules_pkg//pkg:mappings.bzl", "pkg_files", "strip_prefix")
load("@rules_pkg//pkg:tar.bzl", "pkg_tar")
load("//:protobuf_version.bzl", "PROTOBUF_RUST_VERSION")

pkg_tar(
name = "protobuf_crate",
srcs = [
":crate_root_files",
"//:LICENSE",
"//rust:rust_protobuf_libupb_src",
"//rust:rust_protobuf_src_dir",
],
visibility = ["//rust:__subpackages__"],
)

pkg_files(
name = "crate_root_files",
srcs = [":srcs"],
strip_prefix = strip_prefix.from_root("rust/release_crates/protobuf"),
)

genrule(
name = "gen_cargo_toml",
srcs = ["Cargo-template.toml"],
outs = ["Cargo.toml"],
cmd = "cat $(SRCS) | sed -e 's/{{VERSION}}/{0}-beta/g' > $(OUTS)".format(PROTOBUF_RUST_VERSION),
)

filegroup(
name = "srcs",
srcs = [
"build.rs",
":gen_cargo_toml",
],
visibility = ["//rust:__subpackages__"],
)
File renamed without changes.
File renamed without changes.
35 changes: 35 additions & 0 deletions rust/release_crates/protobuf_codegen/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
load("@rules_pkg//pkg:mappings.bzl", "pkg_files", "strip_prefix")
load("@rules_pkg//pkg:tar.bzl", "pkg_tar")
load("//:protobuf_version.bzl", "PROTOBUF_RUST_VERSION")

pkg_tar(
name = "protobuf_codegen_crate",
srcs = [
":protobuf_codegen_files",
"//:LICENSE",
"//rust:vendored_protocs_test",
],
tags = ["manual"],
visibility = ["//rust:__subpackages__"],
)

pkg_files(
name = "protobuf_codegen_files",
srcs = [":srcs"],
strip_prefix = strip_prefix.from_root("rust/release_crates/protobuf_codegen"),
)

genrule(
name = "gen_cargo_toml",
srcs = ["Cargo-template.toml"],
outs = ["Cargo.toml"],
cmd = "cat $(SRCS) | sed -e 's/{{VERSION}}/{0}-beta/g' > $(OUTS)".format(PROTOBUF_RUST_VERSION),
)

filegroup(
name = "srcs",
srcs = [
":gen_cargo_toml",
] + glob(["src/**/*"]),
visibility = ["//rust:__subpackages__"],
)
File renamed without changes.
File renamed without changes.
23 changes: 23 additions & 0 deletions rust/release_crates/protobuf_example/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
load("@rules_pkg//pkg:mappings.bzl", "pkg_files", "strip_prefix")
load("@rules_pkg//pkg:tar.bzl", "pkg_tar")

pkg_tar(
name = "protobuf_example_crate",
srcs = [
":protobuf_example_files",
"//:LICENSE",
],
visibility = ["//rust:__subpackages__"],
)

pkg_files(
name = "protobuf_example_files",
srcs = [":srcs"],
strip_prefix = strip_prefix.from_root("rust/release_crates/protobuf_example"),
)

filegroup(
name = "srcs",
srcs = glob(["**/*"]),
visibility = ["//rust:__subpackages__"],
)
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ fn main() {
mod tests {
use super::*;

#[test]
#[test] // allow_core_test
fn set_strings() {
let foo = proto!(Foo { name: "foo", bar: __ { name: "bar" } });

assert_eq!(foo.name(), "foo");
assert_eq!(foo.bar().name(), "bar");
}

#[test]
#[test] // allow_core_test
fn set_ints() {
let foo = proto!(Foo { int: 42, bar: __ { numbers: [1, 2, 3] } });

Expand Down

0 comments on commit 16260b1

Please sign in to comment.