Skip to content

Commit

Permalink
Use separate target dir when compiling Wasm code
Browse files Browse the repository at this point in the history
It should help with #971, until a more proper solution is in place.
  • Loading branch information
tiziano88 committed May 28, 2020
1 parent 8188216 commit 21fa650
Show file tree
Hide file tree
Showing 17 changed files with 43 additions and 47 deletions.
2 changes: 1 addition & 1 deletion BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ exports_files(["LICENSE"])
# These files are built via cargo outside of Bazel.
exports_files(srcs = glob(["target/x86_64-unknown-linux-musl/release/*oak_loader"]))

exports_files(srcs = glob(["target/wasm32-unknown-unknown/release/*.wasm"]))
exports_files(srcs = glob(["examples/target/wasm32-unknown-unknown/release/*.wasm"]))

# These files are necessary for the backend server in the Aggregator example application.
exports_files(srcs = glob(["target/release/aggregator_*"]))
13 changes: 0 additions & 13 deletions Cargo.lock

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

4 changes: 2 additions & 2 deletions examples/abitest/config/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ exports_files(srcs = glob(["*.textproto"]))
serialized_config(
name = "config",
modules = {
"frontend_module": "//:target/wasm32-unknown-unknown/release/abitest_0_frontend.wasm",
"backend_module": "//:target/wasm32-unknown-unknown/release/abitest_1_backend.wasm",
"frontend_module": "//:examples/target/wasm32-unknown-unknown/release/abitest_0_frontend.wasm",
"backend_module": "//:examples/target/wasm32-unknown-unknown/release/abitest_1_backend.wasm",
},
textproto = ":config.textproto",
)
2 changes: 1 addition & 1 deletion examples/aggregator/config/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ exports_files(srcs = glob(["*.textproto"]))
serialized_config(
name = "config",
modules = {
"app": "//:target/wasm32-unknown-unknown/release/aggregator.wasm",
"app": "//:examples/target/wasm32-unknown-unknown/release/aggregator.wasm",
},
textproto = ":config.textproto",
)
2 changes: 1 addition & 1 deletion examples/chat/config/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ exports_files(srcs = glob(["*.textproto"]))
serialized_config(
name = "config",
modules = {
"app": "//:target/wasm32-unknown-unknown/release/chat.wasm",
"app": "//:examples/target/wasm32-unknown-unknown/release/chat.wasm",
},
textproto = ":config.textproto",
)
4 changes: 2 additions & 2 deletions examples/hello_world/config/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ exports_files(srcs = glob(["*.textproto"]))
serialized_config(
name = "config",
modules = {
"app": "//:target/wasm32-unknown-unknown/release/hello_world.wasm",
"translator": "//:target/wasm32-unknown-unknown/release/translator.wasm",
"app": "//:examples/target/wasm32-unknown-unknown/release/hello_world.wasm",
"translator": "//:examples/target/wasm32-unknown-unknown/release/translator.wasm",
},
textproto = ":config.textproto",
)
Expand Down
2 changes: 1 addition & 1 deletion examples/machine_learning/config/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ exports_files(srcs = glob(["*.textproto"]))
serialized_config(
name = "config",
modules = {
"app": "//:target/wasm32-unknown-unknown/release/machine_learning.wasm",
"app": "//:examples/target/wasm32-unknown-unknown/release/machine_learning.wasm",
},
textproto = ":config.textproto",
)
2 changes: 1 addition & 1 deletion examples/private_set_intersection/config/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ exports_files(srcs = glob(["*.textproto"]))
serialized_config(
name = "config",
modules = {
"app": "//:target/wasm32-unknown-unknown/release/private_set_intersection.wasm",
"app": "//:examples/target/wasm32-unknown-unknown/release/private_set_intersection.wasm",
},
textproto = ":config.textproto",
)
2 changes: 1 addition & 1 deletion examples/running_average/config/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ exports_files(srcs = glob(["*.textproto"]))
serialized_config(
name = "config",
modules = {
"app": "//:target/wasm32-unknown-unknown/release/running_average.wasm",
"app": "//:examples/target/wasm32-unknown-unknown/release/running_average.wasm",
},
textproto = ":config.textproto",
)
2 changes: 1 addition & 1 deletion examples/rustfmt/config/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ exports_files(srcs = glob(["*.textproto"]))
serialized_config(
name = "config",
modules = {
"app": "//:target/wasm32-unknown-unknown/release/rustfmt.wasm",
"app": "//:examples/target/wasm32-unknown-unknown/release/rustfmt.wasm",
},
textproto = ":config.textproto",
)
2 changes: 1 addition & 1 deletion examples/translator/config/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ exports_files(srcs = glob(["*.textproto"]))
serialized_config(
name = "config",
modules = {
"app": "//:target/wasm32-unknown-unknown/release/translator.wasm",
"app": "//:examples/target/wasm32-unknown-unknown/release/translator.wasm",
},
textproto = ":config.textproto",
)
8 changes: 5 additions & 3 deletions scripts/build_example
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +48,18 @@ fi
case "${language}" in
rust)
for module in examples/"${EXAMPLE}"/module*/rust/Cargo.toml; do
cargo build --release --target=wasm32-unknown-unknown --manifest-path="${module}"
# Use a separate target dir for Wasm build artifacts. The precise name is not relevant, but it
# should end with `target` so that it gets automatically ignored by our `.gitignore`.
cargo build --release --target-dir=examples/target --target=wasm32-unknown-unknown --manifest-path="${module}"
done

# Serialize application configuration for Rust module.
if [[ "${EXAMPLE}" == "hello_world" ]]; then
# `hello_world` example has an optional additional `translator` module.
cargo build --release --target=wasm32-unknown-unknown --manifest-path="examples/translator/module/rust/Cargo.toml"
cargo build --release --target-dir=examples/target --target=wasm32-unknown-unknown --manifest-path="examples/translator/module/rust/Cargo.toml"
elif [[ "${EXAMPLE}" == "aggregator" ]]; then
# `aggregator` example has an additional Backend Docker image
cargo build --release --package=aggregator_backend
cargo build --release --target=x86_64-unknown-linux-musl --package=aggregator_backend
fi
bazel --output_base="${CACHE_DIR}/client" build "${bazel_build_flags[@]}" "//examples/${EXAMPLE}/config:config";;
cpp)
Expand Down
2 changes: 1 addition & 1 deletion scripts/build_reproducibility_index
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ source "$SCRIPTS_DIR/common"

# List of artifacts that are expected to be reproducibly built.
readonly REPRODUCIBLE_ARTIFACTS=(
./target/wasm32-unknown-unknown/release/*.wasm
./examples/target/wasm32-unknown-unknown/release/*.wasm
./target/x86_64-unknown-linux-musl/release/oak_loader
)

Expand Down
12 changes: 7 additions & 5 deletions scripts/check_formatting
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ find . \
-not \( -path ./cargo-cache -prune \) -and \
-not \( -path ./cargo -prune \) -and \
-not \( -path ./target -prune \) -and \
-not \( -path ./examples/target -prune \) -and \
-not \( -path ./third_party -prune \) \
\( -type f -name BUILD -or -name WORKSPACE -or -name '*.bzl' \) \
\) -exec buildifier -lint=warn -mode=check {} +
Expand All @@ -27,6 +28,7 @@ readonly MISSING_LICENSES=$(find . \
-not \( -path ./cargo-cache -prune \) -and \
-not \( -path ./cargo -prune \) -and \
-not \( -path ./target -prune \) -and \
-not \( -path ./examples/target -prune \) -and \
-not \( -path ./third_party -prune \) \
\( -type f -name BUILD \) \
\) -exec grep --files-without-match '^ licenses = \["notice"\],$' {} +)
Expand All @@ -53,7 +55,7 @@ find . \
-not \( -path ./.git -prune \) -and \
-not \( -path ./bazel-cache -prune \) -and \
-not \( -path ./cargo-cache -prune \) -and \
-not \( -path ./target -prune \) -and \
-not \( -path '*/target' -prune \) -and \
-not \( -path ./third_party -prune \) \
\( -type f -name '*Dockerfile' \) \
\) -exec hadolint {} +
Expand All @@ -63,7 +65,7 @@ find . \
-not \( -path ./.git -prune \) -and \
-not \( -path ./bazel-cache -prune \) -and \
-not \( -path ./cargo-cache -prune \) -and \
-not \( -path ./target -prune \) -and \
-not \( -path '*/target' -prune \) -and \
-not \( -path ./third_party -prune \) \
\( -type f -name '*.md' -or -name '*.yaml' -or -name '*.toml' \) \
\) -exec prettier --check {} +
Expand All @@ -73,7 +75,7 @@ find . \
-not \( -path ./.git -prune \) -and \
-not \( -path ./bazel-cache -prune \) -and \
-not \( -path ./cargo-cache -prune \) -and \
-not \( -path ./target -prune \) -and \
-not \( -path '*/target' -prune \) -and \
-not \( -path ./third_party -prune \) \
\( -type f -name '*.md' \) \
\) -exec markdownlint {} +
Expand All @@ -83,7 +85,7 @@ find . \
-not \( -path ./bazel-cache -prune \) -and \
-not \( -path ./cargo-cache -prune \) -and \
-not \( -path ./cargo -prune \) -and \
-not \( -path ./target -prune \) -and \
-not \( -path '*/target' -prune \) -and \
-not \( -path ./third_party -prune \) \
\( -type f \( -name '*.rs' -o -name '*.cc' \) \) \
\) -exec ./scripts/check_license {} +
Expand All @@ -92,7 +94,7 @@ find . \
\( \
-not \( -path ./bazel-cache -prune \) -and \
-not \( -path ./cargo-cache -prune \) -and \
-not \( -path ./target -prune \) -and \
-not \( -path '*/target' -prune \) -and \
-not \( -path ./third_party -prune \) \
\( -type f \( -name '*.rs' -o -name '*.cc' -o -name '*.proto' -o -name '*.h' \) \) \
\) -exec ./scripts/check_todo {} +
Expand Down
8 changes: 4 additions & 4 deletions scripts/format
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ find . \
-not \( -path ./bazel-cache -prune \) -and \
-not \( -path ./cargo-cache -prune \) -and \
-not \( -path ./cargo -prune \) -and \
-not \( -path ./target -prune \) -and \
-not \( -path '*/target' -prune \) -and \
-not \( -path ./third_party -prune \) \
\( -type f -name BUILD -or -name WORKSPACE -or -name '*.bzl' \) \
\) -exec buildifier -lint=warn -mode=fix {} +
Expand All @@ -25,7 +25,7 @@ find . \
-not \( -path ./.git -prune \) -and \
-not \( -path ./bazel-cache -prune \) -and \
-not \( -path ./cargo-cache -prune \) -and \
-not \( -path ./target -prune \) -and \
-not \( -path '*/target' -prune \) -and \
-not \( -path ./third_party -prune \) \
\( -type f -name '*.h' -or -name '*.cc' -or -name '*.proto' \) \
\) -exec clang-format -i -style=file {} +
Expand All @@ -37,7 +37,7 @@ find . \
-not \( -path ./.git -prune \) -and \
-not \( -path ./bazel-cache -prune \) -and \
-not \( -path ./cargo-cache -prune \) -and \
-not \( -path ./target -prune \) -and \
-not \( -path '*/target' -prune \) -and \
-not \( -path ./third_party -prune \) \
\( -type f -name '*.md' -or -name '*.yaml' -or -name '*.toml' \) \
\) -exec prettier --write {} +
Expand All @@ -47,7 +47,7 @@ find . \
-not \( -path ./.git -prune \) -and \
-not \( -path ./bazel-cache -prune \) -and \
-not \( -path ./cargo-cache -prune \) -and \
-not \( -path ./target -prune \) -and \
-not \( -path '*/target' -prune \) -and \
-not \( -path ./third_party -prune \) \
\( -type f -name '*.md' \) \
\) -exec markdownlint --fix {} +
Expand Down
1 change: 0 additions & 1 deletion sdk/rust/oak_tests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ license = "Apache-2.0"

[dependencies]
byteorder = "*"
cargo_metadata = "*"
lazy_static = "*"
log = { version = "*", features = ["std"] }
oak = "=0.1.0"
Expand Down
22 changes: 14 additions & 8 deletions sdk/rust/oak_tests/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,32 +22,38 @@ use oak_abi::proto::oak::application::{
WebAssemblyConfiguration,
};
use prost::Message;
use std::{collections::HashMap, process::Command};
use std::{collections::HashMap, path::PathBuf, process::Command};
use tonic::transport::Certificate;

// TODO(#544): re-enable unit tests of SDK functionality

/// Uses cargo to compile a Rust manifest to Wasm bytes.
pub fn compile_rust_wasm(cargo_path: &str, module_name: &str) -> std::io::Result<Vec<u8>> {
let mut cmd = cargo_metadata::MetadataCommand::new();
cmd.manifest_path(cargo_path);
let metadata = cmd.exec().unwrap();
// Use a separate target dir for Wasm build artifacts. The precise name is not relevant, but it
// should end with `target` so that it gets automatically ignored by our `.gitignore`.
let target_dir = PathBuf::from("oak_tests/target");

Command::new("cargo")
.args(&[
"build",
&format!(
"--target-dir={}",
target_dir.to_str().expect("invalid target dir")
),
"--target=wasm32-unknown-unknown",
&format!("--manifest-path={}", cargo_path),
])
.env_remove("RUSTFLAGS")
.spawn()?
.wait()?;

let mut path = metadata.target_directory;
path.push("wasm32-unknown-unknown/debug");
path.push(module_name);
let mut module_path = target_dir;
module_path.push("wasm32-unknown-unknown/debug");
module_path.push(module_name);

std::fs::read(path)
info!("compiled Wasm module path: {:?}", module_path);

std::fs::read(module_path)
}

const DEFAULT_ENTRYPOINT_NAME: &str = "oak_main";
Expand Down

0 comments on commit 21fa650

Please sign in to comment.