Skip to content

Commit 4c05a03

Browse files
committed
Rely on baze make variable location to find protobuf include paths.
Similar change was applied to oak_proto_rust. The current approach assumes that protobuf is included under com_google_protobuf. However, that might not be always the case as it might be protobuf~ if included from MODULE.bazel or protobuf if included from WORKSPACE. This change would allow repositories depending on `oak` not rename the imports. Bug: b/344012112 Change-Id: I4bd43ca5dc236645e93e927c4771d3222cae3522
1 parent 22f4fea commit 4c05a03

File tree

2 files changed

+34
-7
lines changed

2 files changed

+34
-7
lines changed

oak_containers_orchestrator/BUILD

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ cargo_build_script(
9292
],
9393
build_script_env = {
9494
"PROTOC": "$(execpath @com_google_protobuf//:protoc)",
95+
"DESCRIPTOR_PROTO_PATH": "$(location @com_google_protobuf//:descriptor_proto_srcs)",
9596
},
9697
crate_features = ["bazel"],
9798
data = [
@@ -100,7 +101,9 @@ cargo_build_script(
100101
"//proto/containers:orchestrator_crypto_proto",
101102
"//proto/key_provisioning:key_provisioning_proto",
102103
"//proto/session:messages_proto",
104+
"@com_google_protobuf//:descriptor_proto_srcs",
103105
"@com_google_protobuf//:protoc",
106+
"@com_google_protobuf//:well_known_type_protos",
104107
],
105108
deps = [
106109
"//oak_grpc_utils",

oak_containers_orchestrator/build.rs

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,38 @@
1313
// See the License for the specific language governing permissions and
1414
// limitations under the License.
1515

16+
use std::path::PathBuf;
17+
1618
use oak_grpc_utils::{generate_grpc_code, CodegenOptions};
1719

20+
#[cfg(feature = "bazel")]
21+
fn get_included_protos() -> Vec<PathBuf> {
22+
// The root of all Oak protos
23+
let oak_proto_root = PathBuf::from("..");
24+
// Rely on bazel make variable `location` to find protobuf include paths.
25+
// We do this as protobuf might be imported under different names in the
26+
// external directory based on the setup (BzlMod, WORKSPACE or others).
27+
// Possible names are: com_google_protobuf, protobuf~, and protobuf.
28+
// The goal is to allow dependent repositories to use this
29+
// library without renaming their explicit import of protobuf library.
30+
let protobuf_include_path = PathBuf::from(
31+
std::env::var("DESCRIPTOR_PROTO_PATH")
32+
.unwrap()
33+
.replace("google/protobuf/descriptor.proto", ""),
34+
);
35+
vec![oak_proto_root, protobuf_include_path]
36+
}
37+
38+
#[cfg(not(feature = "bazel"))]
39+
fn get_included_protos() -> Vec<PathBuf> {
40+
// The root of all Oak protos, relative to this directory.
41+
let oak_proto_root = PathBuf::from("..");
42+
43+
// In cargo mode, the protoc invocations already include the google
44+
// protobufs, so we only need to provide the Oak proto root.
45+
vec![oak_proto_root]
46+
}
47+
1848
fn main() -> Result<(), Box<dyn std::error::Error>> {
1949
// Generate gRPC code for Orchestrator services.
2050
generate_grpc_code(
@@ -28,13 +58,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
2858
"../proto/containers/hostlib_key_provisioning.proto",
2959
"../proto/session/messages.proto",
3060
],
31-
&[
32-
"..",
33-
// When building with Bazel, the build script doesn't automatically
34-
// contain the information needed to find the well-known Google
35-
// protos. So we need to include these paths here.
36-
"../external/com_google_protobuf/src/google/protobuf/_virtual_imports/empty_proto",
37-
],
61+
&get_included_protos(),
3862
CodegenOptions { build_server: true, build_client: true, ..Default::default() },
3963
)?;
4064

0 commit comments

Comments
 (0)