Skip to content

Commit

Permalink
Rely on baze make variable location to find protobuf include paths.
Browse files Browse the repository at this point in the history
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
  • Loading branch information
alwabel1 committed Jun 21, 2024
1 parent 22f4fea commit 4c05a03
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 7 deletions.
3 changes: 3 additions & 0 deletions oak_containers_orchestrator/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ cargo_build_script(
],
build_script_env = {
"PROTOC": "$(execpath @com_google_protobuf//:protoc)",
"DESCRIPTOR_PROTO_PATH": "$(location @com_google_protobuf//:descriptor_proto_srcs)",
},
crate_features = ["bazel"],
data = [
Expand All @@ -100,7 +101,9 @@ cargo_build_script(
"//proto/containers:orchestrator_crypto_proto",
"//proto/key_provisioning:key_provisioning_proto",
"//proto/session:messages_proto",
"@com_google_protobuf//:descriptor_proto_srcs",
"@com_google_protobuf//:protoc",
"@com_google_protobuf//:well_known_type_protos",
],
deps = [
"//oak_grpc_utils",
Expand Down
38 changes: 31 additions & 7 deletions oak_containers_orchestrator/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,38 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use std::path::PathBuf;

use oak_grpc_utils::{generate_grpc_code, CodegenOptions};

#[cfg(feature = "bazel")]
fn get_included_protos() -> Vec<PathBuf> {
// The root of all Oak protos
let oak_proto_root = PathBuf::from("..");
// Rely on bazel make variable `location` to find protobuf include paths.
// We do this as protobuf might be imported under different names in the
// external directory based on the setup (BzlMod, WORKSPACE or others).
// Possible names are: com_google_protobuf, protobuf~, and protobuf.
// The goal is to allow dependent repositories to use this
// library without renaming their explicit import of protobuf library.
let protobuf_include_path = PathBuf::from(
std::env::var("DESCRIPTOR_PROTO_PATH")
.unwrap()
.replace("google/protobuf/descriptor.proto", ""),
);
vec![oak_proto_root, protobuf_include_path]
}

#[cfg(not(feature = "bazel"))]
fn get_included_protos() -> Vec<PathBuf> {
// The root of all Oak protos, relative to this directory.
let oak_proto_root = PathBuf::from("..");

// In cargo mode, the protoc invocations already include the google
// protobufs, so we only need to provide the Oak proto root.
vec![oak_proto_root]
}

fn main() -> Result<(), Box<dyn std::error::Error>> {
// Generate gRPC code for Orchestrator services.
generate_grpc_code(
Expand All @@ -28,13 +58,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
"../proto/containers/hostlib_key_provisioning.proto",
"../proto/session/messages.proto",
],
&[
"..",
// When building with Bazel, the build script doesn't automatically
// contain the information needed to find the well-known Google
// protos. So we need to include these paths here.
"../external/com_google_protobuf/src/google/protobuf/_virtual_imports/empty_proto",
],
&get_included_protos(),
CodegenOptions { build_server: true, build_client: true, ..Default::default() },
)?;

Expand Down

0 comments on commit 4c05a03

Please sign in to comment.