diff --git a/docs/programming-oak.md b/docs/programming-oak.md index 1ef3e4d71c1..d7dceb3237e 100644 --- a/docs/programming-oak.md +++ b/docs/programming-oak.md @@ -112,9 +112,9 @@ next section. ### Generated gRPC service code The Oak SDK includes a `proto_rust_grpc` tool (forked from -https://github.com/stepancheg/rust-protobuf) which takes a -[gRPC service definition](https://grpc.io/docs/guides/concepts/) and -autogenerates Rust code for the corresponding Oak Node that implements that +https://github.com/stepancheg/rust-protobuf and wrapped in the `oak_utils`) +which takes a [gRPC service definition](https://grpc.io/docs/guides/concepts/) +and autogenerates Rust code for the corresponding Oak Node that implements that service. Adding a `build.rs` file to the Node that invokes this tool results in a @@ -124,10 +124,10 @@ generated file appearing in `src/proto/_grpc.rs`. [embedmd]:# (../examples/hello_world/module/rust/build.rs Rust /fn main/ /^}/) ```Rust fn main() { - protoc_rust_grpc::run(protoc_rust_grpc::Args { + oak_utils::run_protoc_rust_grpc(protoc_rust_grpc::Args { out_dir: "src/proto", input: &["../../proto/hello_world.proto"], - includes: &["../../proto", "../../third_party"], + includes: &["../../proto", "../../../../third_party"], rust_protobuf: true, // also generate protobuf messages, not just services ..Default::default() }) diff --git a/examples/Cargo.lock b/examples/Cargo.lock index f04b9b348f3..7317bebb0e6 100644 --- a/examples/Cargo.lock +++ b/examples/Cargo.lock @@ -11,6 +11,7 @@ dependencies = [ "oak 0.1.0", "oak_abi 0.1.0", "oak_log 0.1.0", + "oak_utils 0.1.0", "protobuf 2.10.1 (registry+https://github.com/rust-lang/crates.io-index)", "protoc-rust-grpc 0.7.0", "rand 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)", @@ -238,8 +239,8 @@ dependencies = [ "oak_log 0.1.0", "protobuf 2.10.1 (registry+https://github.com/rust-lang/crates.io-index)", "protoc-rust-grpc 0.7.0", - "rand 0.3.23 (registry+https://github.com/rust-lang/crates.io-index)", - "rand_core 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)", + "rand 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)", + "rand_core 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", "serde 1.0.97 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -493,6 +494,7 @@ dependencies = [ "oak_derive 0.1.0", "oak_log 0.1.0", "oak_tests 0.1.0", + "oak_utils 0.1.0", "protobuf 2.10.1 (registry+https://github.com/rust-lang/crates.io-index)", "protoc-rust-grpc 0.7.0", "serial_test 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", @@ -596,6 +598,11 @@ dependencies = [ "rusty-machine 0.5.4 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "maplit" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "matrixmultiply" version = "0.1.15" @@ -680,6 +687,7 @@ dependencies = [ "fmt 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.4.7 (registry+https://github.com/rust-lang/crates.io-index)", "oak_abi 0.1.0", + "oak_utils 0.1.0", "protobuf 2.10.1 (registry+https://github.com/rust-lang/crates.io-index)", "protoc-rust 2.10.1 (registry+https://github.com/rust-lang/crates.io-index)", "rand_core 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", @@ -690,6 +698,7 @@ dependencies = [ name = "oak_abi" version = "0.1.0" dependencies = [ + "oak_utils 0.1.0", "protobuf 2.10.1 (registry+https://github.com/rust-lang/crates.io-index)", "protoc-rust 2.10.1 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -720,6 +729,7 @@ dependencies = [ "log 0.4.7 (registry+https://github.com/rust-lang/crates.io-index)", "oak 0.1.0", "oak_abi 0.1.0", + "oak_utils 0.1.0", "protobuf 2.10.1 (registry+https://github.com/rust-lang/crates.io-index)", "protoc-rust 2.10.1 (registry+https://github.com/rust-lang/crates.io-index)", "rand 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)", @@ -743,6 +753,17 @@ dependencies = [ "simple_logger 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "oak_utils" +version = "0.1.0" +dependencies = [ + "maplit 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)", + "protoc-rust 2.10.1 (registry+https://github.com/rust-lang/crates.io-index)", + "protoc-rust-grpc 0.7.0", + "tempfile 3.1.0 (registry+https://github.com/rust-lang/crates.io-index)", + "walkdir 2.2.9 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "owning_ref" version = "0.4.0" @@ -784,6 +805,7 @@ dependencies = [ "log 0.4.7 (registry+https://github.com/rust-lang/crates.io-index)", "oak 0.1.0", "oak_derive 0.1.0", + "oak_utils 0.1.0", "protobuf 2.10.1 (registry+https://github.com/rust-lang/crates.io-index)", "protoc-rust-grpc 0.7.0", ] @@ -1092,6 +1114,7 @@ dependencies = [ "log 0.4.7 (registry+https://github.com/rust-lang/crates.io-index)", "oak 0.1.0", "oak_derive 0.1.0", + "oak_utils 0.1.0", "protobuf 2.10.1 (registry+https://github.com/rust-lang/crates.io-index)", "protoc-rust-grpc 0.7.0", ] @@ -1279,6 +1302,7 @@ dependencies = [ "oak 0.1.0", "oak_derive 0.1.0", "oak_log 0.1.0", + "oak_utils 0.1.0", "protobuf 2.10.1 (registry+https://github.com/rust-lang/crates.io-index)", "protoc-rust-grpc 0.7.0", "rustfmt-nightly 1.3.3 (registry+https://github.com/rust-lang/crates.io-index)", @@ -1721,6 +1745,7 @@ dependencies = [ "checksum libc 0.2.60 (registry+https://github.com/rust-lang/crates.io-index)" = "d44e80633f007889c7eff624b709ab43c92d708caad982295768a7b13ca3b5eb" "checksum lock_api 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "62ebf1391f6acad60e5c8b43706dde4582df75c06698ab44511d15016bc2442c" "checksum log 0.4.7 (registry+https://github.com/rust-lang/crates.io-index)" = "c275b6ad54070ac2d665eef9197db647b32239c9d244bfb6f041a766d00da5b3" +"checksum maplit 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "3e2e65a1a2e43cfcb47a895c4c8b10d1f4a61097f9f254f183aee60cad9c651d" "checksum matrixmultiply 0.1.15 (registry+https://github.com/rust-lang/crates.io-index)" = "dcad67dcec2d58ff56f6292582377e6921afdf3bfbd533e26fb8900ae575e002" "checksum memchr 2.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "88579771288728879b57485cc7d6b07d648c9f0141eb955f8ab7f9d45394468e" "checksum memoffset 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "0f9dc261e2b62d7a622bf416ea3c5245cdd5d9a7fcc428c0d06804dfce1775b3" diff --git a/examples/Cargo.toml b/examples/Cargo.toml index 3afa2af4633..3f53e76fdaa 100644 --- a/examples/Cargo.toml +++ b/examples/Cargo.toml @@ -28,3 +28,4 @@ oak_derive = { path = "../sdk/rust/oak_derive" } oak_log = { path = "../sdk/rust/oak_log" } oak_runtime = { path = "../oak/server/rust/oak_runtime" } oak_tests = { path = "../sdk/rust/oak_tests" } +oak_utils = { path = "../sdk/rust/oak_utils" } diff --git a/examples/abitest/module_0/rust/Cargo.toml b/examples/abitest/module_0/rust/Cargo.toml index 218bd48f7f2..9ce46cc0028 100644 --- a/examples/abitest/module_0/rust/Cargo.toml +++ b/examples/abitest/module_0/rust/Cargo.toml @@ -23,4 +23,5 @@ serde = { version = "*", features = ["derive"] } serde_json = "*" [build-dependencies] +oak_utils = "*" protoc-rust-grpc = { path = "../../../../third_party/grpc-rust/protoc-rust-grpc" } diff --git a/examples/abitest/module_0/rust/build.rs b/examples/abitest/module_0/rust/build.rs index c53dd354d86..effedacd7f3 100644 --- a/examples/abitest/module_0/rust/build.rs +++ b/examples/abitest/module_0/rust/build.rs @@ -1,8 +1,8 @@ fn main() { - protoc_rust_grpc::run(protoc_rust_grpc::Args { + oak_utils::run_protoc_rust_grpc(protoc_rust_grpc::Args { out_dir: "src/proto", input: &["../../proto/abitest.proto"], - includes: &["../../proto", "../../third_party"], + includes: &["../../proto", "../../../../third_party"], rust_protobuf: true, // also generate protobuf messages, not just services ..Default::default() }) diff --git a/examples/hello_world/module/rust/Cargo.toml b/examples/hello_world/module/rust/Cargo.toml index 037994ec49e..6182d24b2fa 100644 --- a/examples/hello_world/module/rust/Cargo.toml +++ b/examples/hello_world/module/rust/Cargo.toml @@ -22,4 +22,5 @@ serial_test = "*" serial_test_derive = "*" [build-dependencies] +oak_utils = "*" protoc-rust-grpc = { path = "../../../../third_party/grpc-rust/protoc-rust-grpc" } diff --git a/examples/hello_world/module/rust/build.rs b/examples/hello_world/module/rust/build.rs index 8800890314d..f9cd87f2d49 100644 --- a/examples/hello_world/module/rust/build.rs +++ b/examples/hello_world/module/rust/build.rs @@ -1,8 +1,8 @@ fn main() { - protoc_rust_grpc::run(protoc_rust_grpc::Args { + oak_utils::run_protoc_rust_grpc(protoc_rust_grpc::Args { out_dir: "src/proto", input: &["../../proto/hello_world.proto"], - includes: &["../../proto", "../../third_party"], + includes: &["../../proto", "../../../../third_party"], rust_protobuf: true, // also generate protobuf messages, not just services ..Default::default() }) diff --git a/examples/private_set_intersection/module/rust/Cargo.toml b/examples/private_set_intersection/module/rust/Cargo.toml index 753f18ee236..acdd3721e3d 100644 --- a/examples/private_set_intersection/module/rust/Cargo.toml +++ b/examples/private_set_intersection/module/rust/Cargo.toml @@ -14,4 +14,5 @@ oak_derive = "*" protobuf = "*" [build-dependencies] +oak_utils = "*" protoc-rust-grpc = { path = "../../../../third_party/grpc-rust/protoc-rust-grpc" } diff --git a/examples/private_set_intersection/module/rust/build.rs b/examples/private_set_intersection/module/rust/build.rs index faa97642ae5..917808f228e 100644 --- a/examples/private_set_intersection/module/rust/build.rs +++ b/examples/private_set_intersection/module/rust/build.rs @@ -1,5 +1,5 @@ fn main() { - protoc_rust_grpc::run(protoc_rust_grpc::Args { + oak_utils::run_protoc_rust_grpc(protoc_rust_grpc::Args { out_dir: "src/proto", input: &["../../proto/private_set_intersection.proto"], includes: &["../../proto"], diff --git a/examples/running_average/module/rust/Cargo.toml b/examples/running_average/module/rust/Cargo.toml index 5268a990e93..6439fce745c 100644 --- a/examples/running_average/module/rust/Cargo.toml +++ b/examples/running_average/module/rust/Cargo.toml @@ -14,4 +14,5 @@ oak_derive = "*" protobuf = "*" [build-dependencies] +oak_utils = "*" protoc-rust-grpc = { path = "../../../../third_party/grpc-rust/protoc-rust-grpc" } diff --git a/examples/running_average/module/rust/build.rs b/examples/running_average/module/rust/build.rs index 9b170a59391..781ed1b8fd1 100644 --- a/examples/running_average/module/rust/build.rs +++ b/examples/running_average/module/rust/build.rs @@ -1,5 +1,5 @@ fn main() { - protoc_rust_grpc::run(protoc_rust_grpc::Args { + oak_utils::run_protoc_rust_grpc(protoc_rust_grpc::Args { out_dir: "src/proto", input: &["../../proto/running_average.proto"], includes: &["../../proto"], diff --git a/examples/rustfmt/module/rust/Cargo.toml b/examples/rustfmt/module/rust/Cargo.toml index 5c623f4bbb0..2ccdaaa7ef6 100644 --- a/examples/rustfmt/module/rust/Cargo.toml +++ b/examples/rustfmt/module/rust/Cargo.toml @@ -16,4 +16,5 @@ protobuf = "*" rustfmt-nightly = "*" [build-dependencies] +oak_utils = "*" protoc-rust-grpc = { path = "../../../../third_party/grpc-rust/protoc-rust-grpc" } diff --git a/examples/rustfmt/module/rust/build.rs b/examples/rustfmt/module/rust/build.rs index c5e9aa65234..a19b5ef77c4 100644 --- a/examples/rustfmt/module/rust/build.rs +++ b/examples/rustfmt/module/rust/build.rs @@ -1,5 +1,5 @@ fn main() { - protoc_rust_grpc::run(protoc_rust_grpc::Args { + oak_utils::run_protoc_rust_grpc(protoc_rust_grpc::Args { out_dir: "src/proto", input: &["../../proto/rustfmt.proto"], includes: &["../../proto"], diff --git a/oak/server/rust/Cargo.lock b/oak/server/rust/Cargo.lock index e5f8d93b4d9..345ff47838f 100644 --- a/oak/server/rust/Cargo.lock +++ b/oak/server/rust/Cargo.lock @@ -23,6 +23,11 @@ name = "fmt" version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "fuchsia-cprng" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "getrandom" version = "0.1.14" @@ -33,6 +38,14 @@ dependencies = [ "wasi 0.9.0+wasi-snapshot-preview1 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "grpc-compiler" +version = "0.7.0" +dependencies = [ + "protobuf 2.10.1 (registry+https://github.com/rust-lang/crates.io-index)", + "protobuf-codegen 2.10.1 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "libc" version = "0.2.66" @@ -46,6 +59,11 @@ dependencies = [ "cfg-if 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "maplit" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "oak" version = "0.1.0" @@ -54,6 +72,7 @@ dependencies = [ "fmt 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)", "oak_abi 0.1.0", + "oak_utils 0.1.0", "protobuf 2.10.1 (registry+https://github.com/rust-lang/crates.io-index)", "protoc-rust 2.10.1 (registry+https://github.com/rust-lang/crates.io-index)", "rand_core 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)", @@ -64,6 +83,7 @@ dependencies = [ name = "oak_abi" version = "0.1.0" dependencies = [ + "oak_utils 0.1.0", "protobuf 2.10.1 (registry+https://github.com/rust-lang/crates.io-index)", "protoc-rust 2.10.1 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -75,11 +95,23 @@ dependencies = [ "log 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)", "oak 0.1.0", "oak_abi 0.1.0", + "oak_utils 0.1.0", "protobuf 2.10.1 (registry+https://github.com/rust-lang/crates.io-index)", "protoc-rust 2.10.1 (registry+https://github.com/rust-lang/crates.io-index)", "rand 0.7.3 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "oak_utils" +version = "0.1.0" +dependencies = [ + "maplit 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)", + "protoc-rust 2.10.1 (registry+https://github.com/rust-lang/crates.io-index)", + "protoc-rust-grpc 0.7.0", + "tempfile 3.1.0 (registry+https://github.com/rust-lang/crates.io-index)", + "walkdir 2.3.1 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "ppv-lite86" version = "0.2.6" @@ -125,6 +157,17 @@ dependencies = [ "tempfile 3.1.0 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "protoc-rust-grpc" +version = "0.7.0" +dependencies = [ + "grpc-compiler 0.7.0", + "protobuf 2.10.1 (registry+https://github.com/rust-lang/crates.io-index)", + "protoc 2.10.1 (registry+https://github.com/rust-lang/crates.io-index)", + "protoc-rust 2.10.1 (registry+https://github.com/rust-lang/crates.io-index)", + "tempdir 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "quote" version = "1.0.2" @@ -133,6 +176,18 @@ dependencies = [ "proc-macro2 1.0.8 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "rand" +version = "0.4.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "fuchsia-cprng 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.66 (registry+https://github.com/rust-lang/crates.io-index)", + "rand_core 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)", + "rdrand 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "rand" version = "0.7.3" @@ -154,6 +209,19 @@ dependencies = [ "rand_core 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "rand_core" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "rand_core 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "rand_core" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "rand_core" version = "0.5.1" @@ -170,6 +238,14 @@ dependencies = [ "rand_core 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "rdrand" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "rand_core 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "redox_syscall" version = "0.1.56" @@ -183,6 +259,14 @@ dependencies = [ "winapi 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "same-file" +version = "1.0.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "winapi-util 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "serde" version = "1.0.104" @@ -211,6 +295,15 @@ dependencies = [ "unicode-xid 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "tempdir" +version = "0.3.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "rand 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", + "remove_dir_all 0.5.2 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "tempfile" version = "3.1.0" @@ -229,6 +322,16 @@ name = "unicode-xid" version = "0.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "walkdir" +version = "2.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "same-file 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi-util 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "wasi" version = "0.9.0+wasi-snapshot-preview1" @@ -248,6 +351,14 @@ name = "winapi-i686-pc-windows-gnu" version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "winapi-util" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "winapi 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "winapi-x86_64-pc-windows-gnu" version = "0.4.0" @@ -258,9 +369,11 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum c2-chacha 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "214238caa1bf3a496ec3392968969cab8549f96ff30652c9e56885329315f6bb" "checksum cfg-if 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)" = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822" "checksum fmt 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "09904adae26440d46daeacb5ed7922fee69d43ebf84d31e078cd49652cecd718" +"checksum fuchsia-cprng 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "a06f77d526c1a601b7c4cdd98f54b5eaabffc14d5f2f0296febdc7f357c6d3ba" "checksum getrandom 0.1.14 (registry+https://github.com/rust-lang/crates.io-index)" = "7abc8dd8451921606d809ba32e95b6111925cd2906060d2dcc29c070220503eb" "checksum libc 0.2.66 (registry+https://github.com/rust-lang/crates.io-index)" = "d515b1f41455adea1313a4a2ac8a8a477634fbae63cc6100e3aebb207ce61558" "checksum log 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)" = "14b6052be84e6b71ab17edffc2eeabf5c2c3ae1fdb464aae35ac50c67a44e1f7" +"checksum maplit 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "3e2e65a1a2e43cfcb47a895c4c8b10d1f4a61097f9f254f183aee60cad9c651d" "checksum ppv-lite86 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)" = "74490b50b9fbe561ac330df47c08f3f33073d2d00c150f719147d7c54522fa1b" "checksum proc-macro2 1.0.8 (registry+https://github.com/rust-lang/crates.io-index)" = "3acb317c6ff86a4e579dfa00fc5e6cca91ecbb4e7eb2df0468805b674eb88548" "checksum protobuf 2.10.1 (registry+https://github.com/rust-lang/crates.io-index)" = "6686ddd96a8dbe2687b5f2a687b2cfb520854010ec480f2d74c32e7c9873d3c5" @@ -268,18 +381,26 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum protoc 2.10.1 (registry+https://github.com/rust-lang/crates.io-index)" = "9fd83d2547a9e2c8bc6016607281b3ec7ef4871c55be6930915481d80350ab88" "checksum protoc-rust 2.10.1 (registry+https://github.com/rust-lang/crates.io-index)" = "30fe03ab363474c2f5d1062f5d169ba8defd1d30c161261d7c71afd8412727d8" "checksum quote 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "053a8c8bcc71fcce321828dc897a98ab9760bef03a4fc36693c231e5b3216cfe" +"checksum rand 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)" = "552840b97013b1a26992c11eac34bdd778e464601a4c2054b5f0bff7c6761293" "checksum rand 0.7.3 (registry+https://github.com/rust-lang/crates.io-index)" = "6a6b1679d49b24bbfe0c803429aa1874472f50d9b363131f0e89fc356b544d03" "checksum rand_chacha 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "03a2a90da8c7523f554344f921aa97283eadf6ac484a6d2a7d0212fa7f8d6853" +"checksum rand_core 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "7a6fdeb83b075e8266dcc8762c22776f6877a63111121f5f8c7411e5be7eed4b" +"checksum rand_core 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)" = "9c33a3c44ca05fa6f1807d8e6743f3824e8509beca625669633be0acbdf509dc" "checksum rand_core 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "90bde5296fc891b0cef12a6d03ddccc162ce7b2aff54160af9338f8d40df6d19" "checksum rand_hc 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ca3129af7b92a17112d59ad498c6f81eaf463253766b90396d39ea7a39d6613c" +"checksum rdrand 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "678054eb77286b51581ba43620cc911abf02758c91f93f479767aed0f90458b2" "checksum redox_syscall 0.1.56 (registry+https://github.com/rust-lang/crates.io-index)" = "2439c63f3f6139d1b57529d16bc3b8bb855230c8efcc5d3a896c8bea7c3b1e84" "checksum remove_dir_all 0.5.2 (registry+https://github.com/rust-lang/crates.io-index)" = "4a83fa3702a688b9359eccba92d153ac33fd2e8462f9e0e3fdf155239ea7792e" +"checksum same-file 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)" = "93fc1dc3aaa9bfed95e02e6eadabb4baf7e3078b0bd1b4d7b6b0b68378900502" "checksum serde 1.0.104 (registry+https://github.com/rust-lang/crates.io-index)" = "414115f25f818d7dfccec8ee535d76949ae78584fc4f79a6f45a904bf8ab4449" "checksum serde_derive 1.0.104 (registry+https://github.com/rust-lang/crates.io-index)" = "128f9e303a5a29922045a830221b8f78ec74a5f544944f3d5984f8ec3895ef64" "checksum syn 1.0.14 (registry+https://github.com/rust-lang/crates.io-index)" = "af6f3550d8dff9ef7dc34d384ac6f107e5d31c8f57d9f28e0081503f547ac8f5" +"checksum tempdir 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)" = "15f2b5fb00ccdf689e0149d1b1b3c03fead81c2b37735d812fa8bddbbf41b6d8" "checksum tempfile 3.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "7a6e24d9338a0a5be79593e2fa15a648add6138caa803e2d5bc782c371732ca9" "checksum unicode-xid 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "826e7639553986605ec5979c7dd957c7895e93eabed50ab2ffa7f6128a75097c" +"checksum walkdir 2.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "777182bc735b6424e1a57516d35ed72cb8019d85c8c9bf536dccb3445c1a2f7d" "checksum wasi 0.9.0+wasi-snapshot-preview1 (registry+https://github.com/rust-lang/crates.io-index)" = "cccddf32554fecc6acb585f82a32a72e28b48f8c4c1883ddfeeeaa96f7d8e519" "checksum winapi 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)" = "8093091eeb260906a183e6ae1abdba2ef5ef2257a21801128899c3fc699229c6" "checksum winapi-i686-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" +"checksum winapi-util 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "4ccfbf554c6ad11084fb7517daca16cfdcaccbdadba4fc336f032a8b12c2ad80" "checksum winapi-x86_64-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" diff --git a/oak/server/rust/Cargo.toml b/oak/server/rust/Cargo.toml index 880bbd61fcf..0e9f2348a11 100644 --- a/oak/server/rust/Cargo.toml +++ b/oak/server/rust/Cargo.toml @@ -14,3 +14,4 @@ members = [ oak = { path = "../../../sdk/rust/oak" } oak_abi = { path = "oak_abi" } oak_runtime = { path = "oak_runtime" } +oak_utils = { path = "../../../sdk/rust/oak_utils" } diff --git a/oak/server/rust/oak_abi/Cargo.toml b/oak/server/rust/oak_abi/Cargo.toml index c6580251fa3..f686bc0ee70 100644 --- a/oak/server/rust/oak_abi/Cargo.toml +++ b/oak/server/rust/oak_abi/Cargo.toml @@ -9,4 +9,5 @@ license = "Apache-2.0" protobuf = "*" [build-dependencies] +oak_utils = "*" protoc-rust = "*" diff --git a/oak/server/rust/oak_abi/build.rs b/oak/server/rust/oak_abi/build.rs index 5903f2eeeec..8b2ee51aa39 100644 --- a/oak/server/rust/oak_abi/build.rs +++ b/oak/server/rust/oak_abi/build.rs @@ -1,5 +1,5 @@ fn main() { - protoc_rust::run(protoc_rust::Args { + oak_utils::run_protoc_rust(protoc_rust::Args { out_dir: "src/proto", input: &["../../../../oak/proto/oak_api.proto"], includes: &["../../../../oak/proto"], diff --git a/oak/server/rust/oak_runtime/Cargo.toml b/oak/server/rust/oak_runtime/Cargo.toml index 616335798cf..dff9b946c8b 100644 --- a/oak/server/rust/oak_runtime/Cargo.toml +++ b/oak/server/rust/oak_runtime/Cargo.toml @@ -13,4 +13,5 @@ protobuf = "*" rand = { version = "*", features = ["std"] } [build-dependencies] +oak_utils = "*" protoc-rust = "*" diff --git a/oak/server/rust/oak_runtime/build.rs b/oak/server/rust/oak_runtime/build.rs index 5350aef8f0a..170649bbf63 100644 --- a/oak/server/rust/oak_runtime/build.rs +++ b/oak/server/rust/oak_runtime/build.rs @@ -1,5 +1,5 @@ fn main() { - protoc_rust::run(protoc_rust::Args { + oak_utils::run_protoc_rust(protoc_rust::Args { out_dir: "src/proto", input: &["../../../../oak/proto/manager.proto"], includes: &["../../../../oak/proto"], diff --git a/sdk/rust/Cargo.lock b/sdk/rust/Cargo.lock index e59f6604351..15dd1124b8d 100644 --- a/sdk/rust/Cargo.lock +++ b/sdk/rust/Cargo.lock @@ -93,6 +93,14 @@ dependencies = [ "libc 0.2.60 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "grpc-compiler" +version = "0.7.0" +dependencies = [ + "protobuf 2.10.1 (registry+https://github.com/rust-lang/crates.io-index)", + "protobuf-codegen 2.10.1 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "hermit-abi" version = "0.1.6" @@ -127,6 +135,11 @@ dependencies = [ "cfg-if 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "maplit" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "num-integer" version = "0.1.41" @@ -163,6 +176,7 @@ dependencies = [ "oak_abi 0.1.0", "oak_derive 0.1.0", "oak_tests 0.1.0", + "oak_utils 0.1.0", "protobuf 2.10.1 (registry+https://github.com/rust-lang/crates.io-index)", "protoc-rust 2.10.1 (registry+https://github.com/rust-lang/crates.io-index)", "rand_core 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", @@ -176,6 +190,7 @@ dependencies = [ name = "oak_abi" version = "0.1.0" dependencies = [ + "oak_utils 0.1.0", "protobuf 2.10.1 (registry+https://github.com/rust-lang/crates.io-index)", "protoc-rust 2.10.1 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -209,6 +224,7 @@ dependencies = [ "log 0.4.7 (registry+https://github.com/rust-lang/crates.io-index)", "oak 0.1.0", "oak_abi 0.1.0", + "oak_utils 0.1.0", "protobuf 2.10.1 (registry+https://github.com/rust-lang/crates.io-index)", "protoc-rust 2.10.1 (registry+https://github.com/rust-lang/crates.io-index)", "rand 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)", @@ -232,6 +248,17 @@ dependencies = [ "simple_logger 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "oak_utils" +version = "0.1.0" +dependencies = [ + "maplit 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)", + "protoc-rust 2.10.1 (registry+https://github.com/rust-lang/crates.io-index)", + "protoc-rust-grpc 0.7.0", + "tempfile 3.1.0 (registry+https://github.com/rust-lang/crates.io-index)", + "walkdir 2.3.1 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "ppv-lite86" version = "0.2.5" @@ -285,6 +312,17 @@ dependencies = [ "tempfile 3.1.0 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "protoc-rust-grpc" +version = "0.7.0" +dependencies = [ + "grpc-compiler 0.7.0", + "protobuf 2.10.1 (registry+https://github.com/rust-lang/crates.io-index)", + "protoc 2.10.1 (registry+https://github.com/rust-lang/crates.io-index)", + "protoc-rust 2.10.1 (registry+https://github.com/rust-lang/crates.io-index)", + "tempdir 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "quote" version = "0.6.13" @@ -394,6 +432,14 @@ name = "ryu" version = "1.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "same-file" +version = "1.0.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "winapi-util 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "serde" version = "1.0.104" @@ -476,6 +522,15 @@ dependencies = [ "unicode-xid 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "tempdir" +version = "0.3.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "rand 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", + "remove_dir_all 0.5.2 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "tempfile" version = "3.1.0" @@ -509,6 +564,16 @@ name = "unicode-xid" version = "0.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "walkdir" +version = "2.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "same-file 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi-util 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "winapi" version = "0.3.7" @@ -523,6 +588,14 @@ name = "winapi-i686-pc-windows-gnu" version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "winapi-util" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "winapi 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "winapi-x86_64-pc-windows-gnu" version = "0.4.0" @@ -558,6 +631,7 @@ dependencies = [ "checksum lazy_static 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" "checksum libc 0.2.60 (registry+https://github.com/rust-lang/crates.io-index)" = "d44e80633f007889c7eff624b709ab43c92d708caad982295768a7b13ca3b5eb" "checksum log 0.4.7 (registry+https://github.com/rust-lang/crates.io-index)" = "c275b6ad54070ac2d665eef9197db647b32239c9d244bfb6f041a766d00da5b3" +"checksum maplit 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "3e2e65a1a2e43cfcb47a895c4c8b10d1f4a61097f9f254f183aee60cad9c651d" "checksum num-integer 0.1.41 (registry+https://github.com/rust-lang/crates.io-index)" = "b85e541ef8255f6cf42bbfe4ef361305c6c135d10919ecc26126c4e5ae94bc09" "checksum num-traits 0.1.43 (registry+https://github.com/rust-lang/crates.io-index)" = "92e5113e9fd4cc14ded8e499429f396a20f98c772a47cc8622a736e1ec843c31" "checksum num-traits 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)" = "6ba9a427cfca2be13aa6f6403b0b7e7368fe982bfa16fccc450ce74c46cd9b32" @@ -582,6 +656,7 @@ dependencies = [ "checksum remove_dir_all 0.5.2 (registry+https://github.com/rust-lang/crates.io-index)" = "4a83fa3702a688b9359eccba92d153ac33fd2e8462f9e0e3fdf155239ea7792e" "checksum rgb 0.8.14 (registry+https://github.com/rust-lang/crates.io-index)" = "2089e4031214d129e201f8c3c8c2fe97cd7322478a0d1cdf78e7029b0042efdb" "checksum ryu 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "bfa8506c1de11c9c4e4c38863ccbe02a305c8188e85a05a784c9e11e1c3910c8" +"checksum same-file 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)" = "93fc1dc3aaa9bfed95e02e6eadabb4baf7e3078b0bd1b4d7b6b0b68378900502" "checksum serde 1.0.104 (registry+https://github.com/rust-lang/crates.io-index)" = "414115f25f818d7dfccec8ee535d76949ae78584fc4f79a6f45a904bf8ab4449" "checksum serde_derive 1.0.104 (registry+https://github.com/rust-lang/crates.io-index)" = "128f9e303a5a29922045a830221b8f78ec74a5f544944f3d5984f8ec3895ef64" "checksum serde_json 1.0.44 (registry+https://github.com/rust-lang/crates.io-index)" = "48c575e0cc52bdd09b47f330f646cf59afc586e9c4e3ccd6fc1f625b8ea1dad7" @@ -591,11 +666,14 @@ dependencies = [ "checksum spin 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)" = "44363f6f51401c34e7be73db0db371c04705d35efbe9f7d6082e03a921a32c55" "checksum syn 0.15.42 (registry+https://github.com/rust-lang/crates.io-index)" = "eadc09306ca51a40555dd6fc2b415538e9e18bc9f870e47b1a524a79fe2dcf5e" "checksum syn 1.0.13 (registry+https://github.com/rust-lang/crates.io-index)" = "1e4ff033220a41d1a57d8125eab57bf5263783dfdcc18688b1dacc6ce9651ef8" +"checksum tempdir 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)" = "15f2b5fb00ccdf689e0149d1b1b3c03fead81c2b37735d812fa8bddbbf41b6d8" "checksum tempfile 3.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "7a6e24d9338a0a5be79593e2fa15a648add6138caa803e2d5bc782c371732ca9" "checksum time 0.1.42 (registry+https://github.com/rust-lang/crates.io-index)" = "db8dcfca086c1143c9270ac42a2bbd8a7ee477b78ac8e45b19abfb0cbede4b6f" "checksum unicode-xid 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "fc72304796d0818e357ead4e000d19c9c174ab23dc11093ac919054d20a6a7fc" "checksum unicode-xid 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "826e7639553986605ec5979c7dd957c7895e93eabed50ab2ffa7f6128a75097c" +"checksum walkdir 2.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "777182bc735b6424e1a57516d35ed72cb8019d85c8c9bf536dccb3445c1a2f7d" "checksum winapi 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)" = "f10e386af2b13e47c89e7236a7a14a086791a2b88ebad6df9bf42040195cf770" "checksum winapi-i686-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" +"checksum winapi-util 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "4ccfbf554c6ad11084fb7517daca16cfdcaccbdadba4fc336f032a8b12c2ad80" "checksum winapi-x86_64-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" "checksum winconsole 0.10.0 (registry+https://github.com/rust-lang/crates.io-index)" = "3ef84b96d10db72dd980056666d7f1e7663ce93d82fa33b63e71c966f4cf5032" diff --git a/sdk/rust/Cargo.toml b/sdk/rust/Cargo.toml index 47f09b05de8..a09ea27e5bd 100644 --- a/sdk/rust/Cargo.toml +++ b/sdk/rust/Cargo.toml @@ -5,6 +5,7 @@ members = [ "oak_derive", "oak_log", "oak_tests", + "oak_utils", ] # Patch dependencies on oak crates so that they refer to the versions within this same repository. @@ -19,3 +20,4 @@ oak_derive = { path = "oak_derive" } oak_log = { path = "oak_log" } oak_runtime = { path = "../../oak/server/rust/oak_runtime" } oak_tests = { path = "oak_tests" } +oak_utils = { path = "oak_utils" } diff --git a/sdk/rust/oak/Cargo.toml b/sdk/rust/oak/Cargo.toml index 16c41d2821a..8c01b8253c7 100644 --- a/sdk/rust/oak/Cargo.toml +++ b/sdk/rust/oak/Cargo.toml @@ -15,6 +15,7 @@ rand_core = { version = "*", features = ["std"] } serde = { version = "*", features = ["derive"] } [build-dependencies] +oak_utils = "*" protoc-rust = "*" [dev-dependencies] diff --git a/sdk/rust/oak/build.rs b/sdk/rust/oak/build.rs index 468c2ab7392..9c7bb328c72 100644 --- a/sdk/rust/oak/build.rs +++ b/sdk/rust/oak/build.rs @@ -1,5 +1,5 @@ fn main() { - protoc_rust::run(protoc_rust::Args { + oak_utils::run_protoc_rust(protoc_rust::Args { out_dir: "src/proto", input: &[ "../../../third_party/google/rpc/code.proto", diff --git a/sdk/rust/oak_utils/Cargo.toml b/sdk/rust/oak_utils/Cargo.toml new file mode 100644 index 00000000000..48e0d17ec82 --- /dev/null +++ b/sdk/rust/oak_utils/Cargo.toml @@ -0,0 +1,12 @@ +[package] +name = "oak_utils" +version = "0.1.0" +edition = "2018" +license = "Apache-2.0" + +[dependencies] +maplit = "*" +protoc-rust = "*" +protoc-rust-grpc = { path = "../../../third_party/grpc-rust/protoc-rust-grpc" } +tempfile = "*" +walkdir = "*" diff --git a/sdk/rust/oak_utils/src/lib.rs b/sdk/rust/oak_utils/src/lib.rs new file mode 100644 index 00000000000..656fdc9d857 --- /dev/null +++ b/sdk/rust/oak_utils/src/lib.rs @@ -0,0 +1,126 @@ +// +// Copyright 2020 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::collections::HashMap; +use std::fs; +use std::io; +use std::path::Path; + +#[cfg(test)] +mod tests; + +// Generates Rust `proto` files in a temporary directory using `protoc_rust`, +// checks previously generated files and updates them if their contents have changed. +// This is a workaround for `protoc_rust` issue that always updates files, thus provoking +// recompilation of all dependent targets. +// https://github.com/rust-lang/cargo/issues/6529 +// Function doesn't support nested directories since `protoc_rust` doesn't generate them. +pub fn run_protoc_rust(args: protoc_rust::Args) -> io::Result<()> { + let out_path = Path::new(args.out_dir); + + // Create a temporary directory. + let temp_dir = tempfile::tempdir()?; + let temp_path = temp_dir.path(); + + // Generate Rust `proto` files in the temporary directory. + let mut temp_args = args; + temp_args.out_dir = temp_path.to_str().expect("Temporary path error"); + protoc_rust::run(temp_args)?; + + // Copy changed Rust `proto` files to the `out_path`. + copy_changed_files(temp_path, out_path)?; + + Ok(()) +} + +// Generates Rust `proto` files in a temporary directory using `run_protoc_rust`, +// checks previously generated files and updates them if their contents have changed. +pub fn run_protoc_rust_grpc(args: protoc_rust_grpc::Args) -> io::Result<()> { + let out_path = Path::new(args.out_dir); + + // Create a temporary directory. + let temp_dir = tempfile::tempdir()?; + let temp_path = temp_dir.path(); + + // Generate Rust `grpc` files in the temporary directory. + let mut temp_args = args; + temp_args.out_dir = temp_path.to_str().expect("Temporary path error"); + protoc_rust_grpc::run(temp_args)?; + + // Copy changed Rust `grpc` files to the `out_path`. + copy_changed_files(temp_path, out_path)?; + + Ok(()) +} + +// Traverses `dir` and produces a `HashMap` of filenames and their contents. +fn get_files(dir: &Path) -> HashMap { + walkdir::WalkDir::new(dir) + .into_iter() + .filter_map(|entry| entry.ok()) + .filter(|entry| entry.path().is_file()) + .map(|entry| { + let path = entry.into_path(); + let content = fs::read_to_string(&path).expect("Read error"); + let filename = path + .file_name() + .expect("Filename error") + .to_os_string() + .into_string() + .expect("OsString error"); + (filename, content) + }) + .collect() +} + +// This function returns a list of files in the `new_dir` that are different from files with +// same names in the `old_dir`. +fn get_changed_and_removed_files(old_dir: &Path, new_dir: &Path) -> (Vec, Vec) { + let mut old_files = get_files(old_dir); + let changed_files = get_files(new_dir) + .iter() + .filter_map(|(filename, new_content)| { + old_files + .remove(filename) + .map_or(Some(filename), |old_content| { + if *new_content == old_content { + None + } else { + Some(filename) + } + }) + }) + .cloned() + .collect::>(); + let removed_files = old_files.keys().cloned().collect::>(); + (changed_files, removed_files) +} + +// This function copies changed files from `src_dir` to `dst_dir` directory and removes files +// from `dst_dir` that are not present in `src_dir`. +fn copy_changed_files(src_dir: &Path, dst_dir: &Path) -> io::Result<()> { + let (changed_files, removed_files) = get_changed_and_removed_files(dst_dir, src_dir); + for changed_file in changed_files.iter() { + fs::copy(src_dir.join(&changed_file), dst_dir.join(&changed_file))?; + } + for removed_file in removed_files.iter() { + // `mod.rs` file is not generated by `protoc_rust` and `protoc_rust_grpc`. + if "mod.rs" != removed_file { + fs::remove_file(dst_dir.join(&removed_file))?; + } + } + Ok(()) +} diff --git a/sdk/rust/oak_utils/src/tests.rs b/sdk/rust/oak_utils/src/tests.rs new file mode 100644 index 00000000000..2502cd4d6d6 --- /dev/null +++ b/sdk/rust/oak_utils/src/tests.rs @@ -0,0 +1,93 @@ +// +// Copyright 2020 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 crate::*; + +use maplit::hashmap; +use std::collections::HashMap; +use std::fs::File; +use std::io::Write; +use std::path::Path; + +fn write_files(dir: &Path, files: &HashMap<&str, &str>) { + for (filename, data) in files.iter() { + let path = dir.join(filename); + let mut file = File::create(path).unwrap(); + file.write_all(data.as_bytes()).expect("Write error"); + } +} + +fn are_equal_files(files: &[String], expected_files: &[&str]) -> bool { + files.len() == expected_files.len() + && files.iter().zip(expected_files.iter()).all(|(a, b)| a == b) +} + +#[test] +fn get_files_test() { + let temp_files = hashmap! { + "1" => "string1", + "2" => "string2", + "3" => "string3", + }; + + let temp_dir = tempfile::tempdir().unwrap(); + write_files(temp_dir.path(), &temp_files); + + let files = get_files(temp_dir.path()); + for (filename, data) in temp_files.iter() { + assert_eq!( + files.get(&String::from(*filename)), + Some(&String::from(*data)) + ); + } +} + +#[test] +fn get_changed_and_removed_files_test() { + let old_files = hashmap! { + "1" => "string1", + "2" => "string2", + "3" => "string3", + }; + let new_files = hashmap! { + "1" => "changed_string1", + "3" => "string3", + "4" => "string4", + }; + let expected_changed_files: &[&str] = &["1", "4"]; + let expected_removed_files: &[&str] = &["2"]; + + let old_temp_dir = tempfile::tempdir().unwrap(); + let new_temp_dir = tempfile::tempdir().unwrap(); + + write_files(old_temp_dir.path(), &old_files); + write_files(new_temp_dir.path(), &new_files); + + let (mut changed_files, mut removed_files) = + get_changed_and_removed_files(old_temp_dir.path(), new_temp_dir.path()); + + changed_files.sort(); + assert_eq!( + true, + are_equal_files(&changed_files, expected_changed_files) + ); + + removed_files.sort(); + assert_eq!( + true, + are_equal_files(&removed_files, expected_removed_files) + ); +} diff --git a/third_party/grpc-rust/protoc-rust-grpc/src/lib.rs b/third_party/grpc-rust/protoc-rust-grpc/src/lib.rs index a9c7bae97a6..f7b4ba04e50 100644 --- a/third_party/grpc-rust/protoc-rust-grpc/src/lib.rs +++ b/third_party/grpc-rust/protoc-rust-grpc/src/lib.rs @@ -41,7 +41,6 @@ pub fn run(args: Args) -> Result<()> { includes: args.includes, input: args.input, customize: args.rust_protobuf_customize, - ..Default::default() })?; } @@ -68,7 +67,7 @@ pub fn run(args: Args) -> Result<()> { let mut includes = args.includes; if includes.is_empty() { - static DOT_SLICE: &'static [&'static str] = &["."]; + static DOT_SLICE: &[&str] = &["."]; includes = DOT_SLICE; } @@ -121,7 +120,7 @@ fn remove_path_prefix<'a>(mut path: &'a str, mut prefix: &str) -> Option<&'a str return Some(path); } - if prefix.ends_with("/") || prefix.ends_with("\\") { + if prefix.ends_with('/') || prefix.ends_with('\\') { prefix = &prefix[..prefix.len() - 1]; } @@ -134,9 +133,9 @@ fn remove_path_prefix<'a>(mut path: &'a str, mut prefix: &str) -> Option<&'a str } if path.as_bytes()[prefix.len()] == b'/' || path.as_bytes()[prefix.len()] == b'\\' { - return Some(&path[prefix.len() + 1..]); + Some(&path[prefix.len() + 1..]) } else { - return None; + None } }