Skip to content

Commit 69e5c0a

Browse files
acozzettecopybara-github
authored andcommitted
Set up Rust crates as members of a Cargo workspace
This has a few benefits: - We can do a full `cargo publish --dry-run` on all crates, which would not otherwise be possible since dependent crates would not be present yet on crates.io. - This makes manually testing the Cargo build easier, since you can have Bazel build all our crates into a single tar file. - This should make it easier to run our tests from `rust/test/shared/` with Cargo in addition to Bazel. PiperOrigin-RevId: 793688025
1 parent 4bd727d commit 69e5c0a

File tree

9 files changed

+43
-83
lines changed

9 files changed

+43
-83
lines changed

rust/BUILD

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -298,5 +298,5 @@ pkg_filegroup(
298298

299299
alias(
300300
name = "release",
301-
actual = "//rust/release_crates:crates",
301+
actual = "//rust/release_crates:workspace",
302302
)

rust/release_crates/BUILD

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,19 @@
11
# Protobuf Rust crate packaging for release.
22

3+
load("@rules_pkg//pkg:mappings.bzl", "pkg_files")
4+
load("@rules_pkg//pkg:tar.bzl", "pkg_tar")
35
load("@rules_shell//shell:sh_binary.bzl", "sh_binary")
46

5-
filegroup(
6-
name = "crates",
7+
pkg_files(
8+
name = "license",
9+
srcs = ["//:LICENSE"],
10+
visibility = ["//rust/release_crates:__subpackages__"],
11+
)
12+
13+
pkg_tar(
14+
name = "workspace",
715
srcs = [
16+
":Cargo.toml",
817
"//rust/release_crates/protobuf:protobuf_crate",
918
"//rust/release_crates/protobuf_codegen:protobuf_codegen_crate",
1019
"//rust/release_crates/protobuf_example:protobuf_example_crate",
@@ -19,7 +28,7 @@ sh_binary(
1928
name = "cargo_test",
2029
srcs = ["cargo_test.sh"],
2130
data = [
22-
":crates",
31+
":workspace",
2332
"//:protoc",
2433
],
2534
tags = ["manual"],

rust/release_crates/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[workspace]
2+
resolver = "2"
3+
members = ["protobuf", "protobuf_codegen", "protobuf_example", "protobuf_macros", "protobuf_well_known_types"]

rust/release_crates/cargo_test.sh

Lines changed: 7 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -34,71 +34,19 @@ trap 'rm -rf -- "$TMP_DIR"' EXIT
3434
CARGO_HOME=$TMP_DIR/cargo_home
3535
mkdir $CARGO_HOME
3636

37-
CRATE_ROOT=$TMP_DIR/protobuf
38-
mkdir $CRATE_ROOT
37+
WORKSPACE_ROOT=$TMP_DIR/workspace
38+
mkdir $WORKSPACE_ROOT
3939

40-
PROTOBUF_TAR=$(rlocation com_google_protobuf/rust/release_crates/protobuf/protobuf_crate.tar)
40+
WORKSPACE_TAR=$(rlocation com_google_protobuf/rust/release_crates/workspace.tar)
4141

42-
echo "Expanding protobuf crate tar"
43-
tar -xvf $PROTOBUF_TAR -C $CRATE_ROOT
44-
45-
CODEGEN_ROOT=$TMP_DIR/protobuf_codegen
46-
mkdir $CODEGEN_ROOT
47-
48-
CODEGEN_TAR=$(rlocation com_google_protobuf/rust/release_crates/protobuf_codegen/protobuf_codegen_crate.tar)
49-
50-
echo "Expanding protobuf_codegen crate tar"
51-
tar -xvf $CODEGEN_TAR -C $CODEGEN_ROOT
52-
53-
EXAMPLE_ROOT=$TMP_DIR/protobuf_example
54-
mkdir $EXAMPLE_ROOT
55-
56-
EXAMPLE_TAR=$(rlocation com_google_protobuf/rust/release_crates/protobuf_example/protobuf_example_crate.tar)
57-
58-
echo "Expanding protobuf_example crate tar"
59-
tar -xvf $EXAMPLE_TAR -C $EXAMPLE_ROOT
60-
61-
MACROS_ROOT=$TMP_DIR/protobuf_macros
62-
mkdir $MACROS_ROOT
63-
64-
MACROS_TAR=$(rlocation com_google_protobuf/rust/release_crates/protobuf_macros/protobuf_macros_crate.tar)
65-
66-
echo "Expanding protobuf_macros crate tar"
67-
tar -xvf $MACROS_TAR -C $MACROS_ROOT
68-
69-
WELL_KNOWN_TYPES_ROOT=$TMP_DIR/protobuf_well_known_types
70-
mkdir $WELL_KNOWN_TYPES_ROOT
71-
72-
WELL_KNOWN_TYPES_TAR=$(rlocation com_google_protobuf/rust/release_crates/protobuf_well_known_types/protobuf_well_known_types_crate.tar)
73-
74-
echo "Expanding protobuf_well_known_types crate tar"
75-
tar -xvf $WELL_KNOWN_TYPES_TAR -C $WELL_KNOWN_TYPES_ROOT
42+
echo "Expanding Cargo workspace tar"
43+
tar -xvf $WORKSPACE_TAR -C $WORKSPACE_ROOT
7644

7745
# Put the Bazel-built protoc at the beginning of $PATH
7846
PATH=$(dirname $(rlocation com_google_protobuf/protoc)):$PATH
7947

8048
export RUSTFLAGS="-Dmismatched-lifetime-syntaxes"
8149

82-
cd $CRATE_ROOT
83-
CARGO_HOME=$CARGO_HOME cargo test
84-
CARGO_HOME=$CARGO_HOME cargo publish --dry-run
85-
86-
cd $CODEGEN_ROOT
87-
CARGO_HOME=$CARGO_HOME cargo test
88-
CARGO_HOME=$CARGO_HOME cargo publish --dry-run
89-
90-
cd $EXAMPLE_ROOT
50+
cd $WORKSPACE_ROOT
9151
CARGO_HOME=$CARGO_HOME cargo test
92-
93-
cd $MACROS_ROOT
94-
# Macros should be tested by the main protobuf test suite.
95-
CARGO_HOME=$CARGO_HOME cargo publish --dry-run
96-
97-
cd $WELL_KNOWN_TYPES_ROOT
98-
CARGO_HOME=$CARGO_HOME cargo test
99-
100-
# TODO: Cannot enable this dry-run yet because it checks that the versions of
101-
# its dependencies are published on crates.io, which they are definitely not
102-
# in this case.
103-
# See also https://github.com/rust-lang/cargo/issues/1169.
104-
# CARGO_HOME=$CARGO_HOME cargo publish --dry-run
52+
CARGO_HOME=$CARGO_HOME cargo publish --dry-run --workspace -Z package-workspace

rust/release_crates/protobuf/BUILD

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
load("@rules_pkg//pkg:mappings.bzl", "pkg_files", "strip_prefix")
2-
load("@rules_pkg//pkg:tar.bzl", "pkg_tar")
1+
load("@rules_pkg//pkg:mappings.bzl", "pkg_filegroup", "pkg_files", "strip_prefix")
32
load("//rust/release_crates:substitute_rust_release_version.bzl", "substitute_rust_release_version")
43

5-
pkg_tar(
4+
pkg_filegroup(
65
name = "protobuf_crate",
76
srcs = [
87
":crate_root_files",
9-
"//:LICENSE",
108
"//rust:rust_protobuf_libupb_src",
119
"//rust:rust_protobuf_src_dir",
10+
"//rust/release_crates:license",
1211
],
12+
prefix = "protobuf",
1313
visibility = ["//rust:__subpackages__"],
1414
)
1515

rust/release_crates/protobuf_codegen/BUILD

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
load("@rules_pkg//pkg:mappings.bzl", "pkg_files", "strip_prefix")
2-
load("@rules_pkg//pkg:tar.bzl", "pkg_tar")
1+
load("@rules_pkg//pkg:mappings.bzl", "pkg_filegroup", "pkg_files", "strip_prefix")
32
load("//rust/release_crates:substitute_rust_release_version.bzl", "substitute_rust_release_version")
43

5-
pkg_tar(
4+
pkg_filegroup(
65
name = "protobuf_codegen_crate",
76
srcs = [
87
":protobuf_codegen_files",
9-
"//:LICENSE",
8+
"//rust/release_crates:license",
109
],
10+
prefix = "protobuf_codegen",
1111
tags = ["manual"],
1212
visibility = ["//rust:__subpackages__"],
1313
)

rust/release_crates/protobuf_example/BUILD

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
load("@rules_pkg//pkg:mappings.bzl", "pkg_files", "strip_prefix")
2-
load("@rules_pkg//pkg:tar.bzl", "pkg_tar")
1+
load("@rules_pkg//pkg:mappings.bzl", "pkg_filegroup", "pkg_files", "strip_prefix")
32
load("//rust/release_crates:substitute_rust_release_version.bzl", "substitute_rust_release_version")
43

5-
pkg_tar(
4+
pkg_filegroup(
65
name = "protobuf_example_crate",
76
srcs = [
87
":protobuf_example_files",
9-
"//:LICENSE",
8+
"//rust/release_crates:license",
109
],
10+
prefix = "protobuf_example",
1111
visibility = ["//rust:__subpackages__"],
1212
)
1313

rust/release_crates/protobuf_macros/BUILD

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
load("@rules_pkg//pkg:mappings.bzl", "pkg_files", "strip_prefix")
2-
load("@rules_pkg//pkg:tar.bzl", "pkg_tar")
1+
load("@rules_pkg//pkg:mappings.bzl", "pkg_filegroup", "pkg_files", "strip_prefix")
32
load("//rust/release_crates:substitute_rust_release_version.bzl", "substitute_rust_release_version")
43

5-
pkg_tar(
4+
pkg_filegroup(
65
name = "protobuf_macros_crate",
76
srcs = [
87
":crate_root_files",
9-
"//:LICENSE",
108
"//rust/proto_proc_macro:rust_protobuf_macros_src_dir",
9+
"//rust/release_crates:license",
1110
],
11+
prefix = "protobuf_macros",
1212
visibility = ["//rust:__subpackages__"],
1313
)
1414

rust/release_crates/protobuf_well_known_types/BUILD.bazel

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
load("@rules_pkg//pkg:mappings.bzl", "pkg_files", "strip_prefix")
2-
load("@rules_pkg//pkg:tar.bzl", "pkg_tar")
1+
load("@rules_pkg//pkg:mappings.bzl", "pkg_filegroup", "pkg_files", "strip_prefix")
32
load("//rust/release_crates:substitute_rust_release_version.bzl", "substitute_rust_release_version")
43

5-
pkg_tar(
4+
pkg_filegroup(
65
name = "protobuf_well_known_types_crate",
76
srcs = [
87
":crate_root_files",
98
":well_known_types",
10-
"//:LICENSE",
9+
"//rust/release_crates:license",
1110
],
11+
prefix = "protobuf_well_known_types",
1212
visibility = ["//rust:__subpackages__"],
1313
)
1414

0 commit comments

Comments
 (0)