Skip to content

Commit

Permalink
Build oak_proto_rust for bare metal with Bazel.
Browse files Browse the repository at this point in the history
BUG: 340185565
Change-Id: Iae5cef128b32e549a50991764fdb6fa60e2a2f92
  • Loading branch information
ernoc committed May 24, 2024
1 parent 88928b9 commit ecfc91c
Show file tree
Hide file tree
Showing 25 changed files with 4,636 additions and 50 deletions.
1 change: 1 addition & 0 deletions enclave_apps/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion justfile
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ kokoro_run_tests: all_ensure_no_std
clang-tidy:
bazel build $BAZEL_CONFIG_FLAG --config=clang-tidy //cc/...

bare_metal_crates := "//oak_linux_boot_params //oak_channel //oak_core //oak_virtio //third_party/rust-hypervisor-firmware-virtio //micro_rpc"
bare_metal_crates := "//oak_linux_boot_params //oak_channel //oak_core //oak_virtio //third_party/rust-hypervisor-firmware-virtio //micro_rpc //oak_proto_rust"

bazel-ci:
bazel build --config=unsafe-fast-presubmit -- @jemalloc //...:all
Expand Down
1 change: 1 addition & 0 deletions micro_rpc/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -64,5 +64,6 @@ cargo_build_script(
],
deps = [
"//micro_rpc_build",
"//oak_proto_build_utils",
],
)
39 changes: 1 addition & 38 deletions micro_rpc/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@
// limitations under the License.
//

#[cfg(feature = "bazel")]
use std::fs;

fn main() -> Result<(), Box<dyn std::error::Error>> {
micro_rpc_build::compile(
&["../proto/micro_rpc/messages.proto"],
Expand All @@ -25,41 +22,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
);

#[cfg(feature = "bazel")]
fix_prost_derives()?;

Ok(())
}

#[cfg(feature = "bazel")]
/// Replaces refs to prost with prost_derive. See b/340185847.
/// Only to be called from build scripts of crates that generate prost code.
/// Only to be called when building with Bazel.
/// The issue that this fixes is that prost with "derive" feature uses
/// "prost-derive", which requires std. In Cargo, that works OK as the
/// feature "std" isn't propagated to deps of crates that don't use them,
/// but Bazel's can't handle that. With Bazel, if we bring prost-derive
/// into the index, it'll make deps like `bytes` also use the "std" feature.
/// Take into account that in Cargo, features of dependencies are declared in
/// the "depending" crate, that is, they belong to the dependency arc, while
/// in Bazel, one crate is brought into an index with a fixed set of features.
/// To solve this, and to be able to build for bare metal from Bazel, we
/// import prost without derive to oak_no_std_crates_index, use prost-derive
/// derive macro directly, but we need to change the crate name, as we no
/// longer have prost re-exporting the derive macros.
pub fn fix_prost_derives() -> Result<(), Box<dyn std::error::Error>> {
// let out_dir_path = std::path::PathBuf::from(std::env::var("OUT_DIR"))?;

let out_dir = std::env::var("OUT_DIR")?;
for entry in fs::read_dir(&out_dir)? {
let file_path = entry?.path();
let contents = fs::read_to_string(&file_path)?;

let updated = contents.replace("::prost::Message", "::prost_derive::Message");
let updated = updated.replace("::prost::Oneof", "::prost_derive::Oneof");
let updated = updated.replace("::prost::Enumeration", "::prost_derive::Enumeration");

fs::write(&file_path, updated)?;
}
oak_proto_build_utils::fix_prost_derives()?;

Ok(())
}
1 change: 0 additions & 1 deletion oak_channel/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ rust_library(
srcs = glob(["src/**"]),
deps = [
"//oak_core",
# "@oak_crates_index//:anyhow",
"@oak_crates_index//:static_assertions",
] + select({
"@platforms//os:none": [
Expand Down
30 changes: 30 additions & 0 deletions oak_proto_build_utils/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#
# Copyright 2024 The Project Oak Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

# This package provides build utils for protos. At the moment this is only
# used from Bazel therefore not present in Cargo workspaces.

load("@rules_rust//rust:defs.bzl", "rust_library")

package(
default_visibility = ["//visibility:public"],
licenses = ["notice"],
)

rust_library(
name = "oak_proto_build_utils",
srcs = glob(["src/**"]),
)
48 changes: 48 additions & 0 deletions oak_proto_build_utils/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Copyright 2024 The Project Oak Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use std::fs;

/// Replaces refs to prost with prost_derive. See b/340185847.
/// Only to be called from build scripts of crates that generate prost code.
/// Only to be called when building with Bazel.
/// The issue that this fixes is that prost with "derive" feature uses
/// "prost-derive", which requires std. In Cargo, that works OK as the
/// feature "std" isn't propagated to deps of crates that don't use them,
/// but Bazel's can't handle that. With Bazel, if we bring prost-derive
/// into the index, it'll make deps like `bytes` also use the "std" feature.
/// Take into account that in Cargo, features of dependencies are declared in
/// the "depending" crate, that is, they belong to the dependency arc, while
/// in Bazel, one crate is brought into an index with a fixed set of features.
/// To solve this, and to be able to build for bare metal from Bazel, we
/// import prost without derive to oak_no_std_crates_index, use prost-derive
/// derive macro directly, but we need to change the crate name, as we no
/// longer have prost re-exporting the derive macros.
pub fn fix_prost_derives() -> Result<(), Box<dyn std::error::Error>> {
// let out_dir_path = std::path::PathBuf::from(std::env::var("OUT_DIR"))?;

let out_dir = std::env::var("OUT_DIR")?;
for entry in fs::read_dir(&out_dir)? {
let file_path = entry?.path();
let contents = fs::read_to_string(&file_path)?;

let updated = contents.replace("::prost::Message", "::prost_derive::Message");
let updated = updated.replace("::prost::Oneof", "::prost_derive::Oneof");
let updated = updated.replace("::prost::Enumeration", "::prost_derive::Enumeration");

fs::write(&file_path, updated)?;
}

Ok(())
}
22 changes: 18 additions & 4 deletions oak_proto_rust/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,23 @@ package(
rust_library(
name = "oak_proto_rust",
srcs = glob(["src/**"]),
proc_macro_deps = [
"@oak_crates_index//:prost-derive",
],
deps = [
":build",
"//micro_rpc",
"@oak_crates_index//:prost",
"@oak_crates_index//:prost-types",
],
] + select({
"@platforms//os:none": [
"//third_party/prost-types",
"@oak_no_std_crates_index//:prost",
],
"//conditions:default": [
# See comment on micro_rpc/BUILD.
"@oak_crates_index//:prost",
"@oak_crates_index//:prost-types",
],
}),
)

cargo_build_script(
Expand All @@ -41,6 +52,7 @@ cargo_build_script(
build_script_env = {
"PROTOC": "$(execpath @com_google_protobuf//:protoc)",
},
crate_features = ["bazel"], # See b/340185847 and fix_prost_derives function doc.
data = [
"//proto:digest_proto",
"//proto/attestation:attachment_proto",
Expand All @@ -54,16 +66,18 @@ cargo_build_script(
"//proto/attestation:verification_proto",
"//proto/containers:interfaces_proto",
"//proto/crypto:crypto_proto",
"//proto/crypto:crypto_rust_prost",
"//proto/oak_functions:abi_proto",
"//proto/oak_functions:lookup_data_proto",
"//proto/oak_functions:testing_proto",
"//proto/oak_functions/service:oak_functions_proto",
"//proto/session:session_proto",
],
tools = [
"@com_google_protobuf//:protoc",
],
deps = [
"//micro_rpc_build",
"//oak_proto_build_utils",
"@oak_crates_index//:prost-build",
],
)
Expand Down
3 changes: 3 additions & 0 deletions oak_proto_rust/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,5 +92,8 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
println!("cargo:rerun-if-changed={}", proto_path);
}

#[cfg(feature = "bazel")]
oak_proto_build_utils::fix_prost_derives()?;

Ok(())
}
1 change: 1 addition & 0 deletions oak_restricted_kernel_bin/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 0 additions & 6 deletions proto/crypto/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
#

load("@bazel_skylib//rules:build_test.bzl", "build_test")
load("@rules_rust//proto/prost:defs.bzl", "rust_prost_library")

package(
default_visibility = ["//visibility:public"],
Expand All @@ -37,11 +36,6 @@ java_proto_library(
deps = [":crypto_proto"],
)

rust_prost_library(
name = "crypto_rust_prost",
proto = ":crypto_proto",
)

build_test(
name = "build_test",
targets = [
Expand Down
40 changes: 40 additions & 0 deletions third_party/prost-types/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#
# Copyright 2024 The Project Oak Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

# Upstream prost-types depends on prost-derive unconditionally, which means
# we can't use it in our oak_no_std_crates_index.

load("@rules_rust//rust:defs.bzl", "rust_library")

package(
default_visibility = ["//visibility:public"],
)

rust_library(
name = "prost-types",
srcs = glob(["src/**"]),
proc_macro_deps = [
"@oak_crates_index//:prost-derive",
],
deps = select({
"@platforms//os:none": [
"@oak_no_std_crates_index//:prost",
],
"//conditions:default": [
"@oak_crates_index//:prost",
],
}),
)
29 changes: 29 additions & 0 deletions third_party/prost-types/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
[package]
name = "prost-types"
version = "0.12.6"
authors = [
"Dan Burkert <[email protected]>",
"Lucio Franco <[email protected]>",
"Casper Meijn <[email protected]>",
"Tokio Contributors <[email protected]>",
]
license = "Apache-2.0"
repository = "https://github.com/tokio-rs/prost"
documentation = "https://docs.rs/prost-types"
readme = "README.md"
description = "Prost definitions of Protocol Buffers well known types."
edition = "2021"
rust-version = "1.70"

[lib]
doctest = false

[features]
default = ["std"]
std = ["prost/std"]

[dependencies]
prost = { version = "0.12.6", path = "../prost", default-features = false, features = ["prost-derive"] }

[dev-dependencies]
proptest = "1"
Loading

0 comments on commit ecfc91c

Please sign in to comment.