Skip to content

Commit

Permalink
using base example from wasmcov to all examples directories
Browse files Browse the repository at this point in the history
  • Loading branch information
jrmncos committed Aug 16, 2024
1 parent be6ddd5 commit dbdeef1
Show file tree
Hide file tree
Showing 50 changed files with 350 additions and 220 deletions.
60 changes: 60 additions & 0 deletions .github/workflows/coverage-examples.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
name: Coverage
on:
push:
branches:
- master
pull_request:
env:
RUSTFLAGS: "-D warnings"

jobs:
coverage:
runs-on: ${{ matrix.platform }}
name: "${{ matrix.example }} - ${{ matrix.platform }}"
strategy:
matrix:
toolchain: [nightly-2024-07-01-aarch64-apple-darwin]
steps:
- uses: actions/checkout@v3
- name: Install Homebrew
run: |
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
- name: Install LLVM
run: brew install llvm
- name: Add LLVM to PATH
run: echo "/opt/homebrew/opt/llvm/bin" >> $GITHUB_PATH
- name: Llvm version
run: llvm-config --version
- name: Clang version
run: clang --version
- name: "${{ matrix.toolchain }} with rustfmt, and wasm32"
uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: ${{ matrix.toolchain }}
target: wasm32-unknown-unknown
- name: Set Default Toolchain
run: rustup override set ${{ matrix.toolchain }}
- name: Rust version
run: rustc --version --verbose
- name: Verify Rust Toolchain
run: rustup show
- name: Add rust-src component
run: rustup component add rust-src --toolchain ${{ matrix.toolchain }}
- name: Install wasmcov
run: cargo +${{ matrix.toolchain }} install wasmcov
- name: Create wasmcov directory
run: mkdir -p ${{ github.workspace }}/wasmcov
- name: Set WASMCOV_DIR environment variable
run: echo "WASMCOV_DIR=${{ github.workspace }}/wasmcov" >> $GITHUB_ENV
- name: Set up cache for near_sandbox directory
uses: actions/cache@v3
with:
path: ${{ github.workspace }}/wasmcov/near_sandbox
key: ${{ runner.os }}-near-sandbox-${{ github.sha }}
restore-keys: |
${{ runner.os }}-near-sandbox-${{ github.sha }}
- name: Build examples
env:
RUSTFLAGS: '-C link-arg=-s'
run: bash ./examples/build_wasm_test_all.sh ${{ matrix.toolchain }}
7 changes: 5 additions & 2 deletions examples/adder/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ crate-type = ["cdylib"]

[dependencies]
near-sdk = { path = "../../near-sdk" }
wasmcov = "0.2"
wasmcov = { version = "0.2", optional = true }

[dev-dependencies]
near-workspaces = "0.11.1"
Expand All @@ -19,10 +19,13 @@ near-abi = "0.4.0"
zstd = "0.13"
near-sdk = { path = "../../near-sdk", features = ["unit-testing"] }

[features]
wasmcov = ["dep:wasmcov"]

[profile.release]
codegen-units = 1
# Tell `rustc` to optimize for small code size.
opt-level = "z"
lto = false
lto = true
debug = false
panic = "abort"
21 changes: 10 additions & 11 deletions examples/adder/build.sh
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
#!/bin/bash
TARGET="${CARGO_TARGET_DIR:-../../target}"
set -e
cd "$(dirname $0)"

INCLUDE_COVERAGE="${1:-false}"

if [ "$INCLUDE_COVERAGE" = true ]; then
cargo-wasmcov build --wasmcov-dir /Users/jrmncos/forks/near-sdk-rs/examples/wasmcov -- --all --target wasm32-unknown-unknown --release
cp ../wasmcov/target/wasm32-unknown-unknown/release/adder.wasm ./res/
if [ -n "$WASMCOV_DIR" ]; then
TARGET="${WASMCOV_DIR}/target"
BUILD_COMMAND="cargo wasmcov build -- --features wasmcov"
else
cargo build --target wasm32-unknown-unknown --release
cp $TARGET/wasm32-unknown-unknown/release/adder.wasm ./res/
#wasm-opt -Oz --output ./res/status_message.wasm ./res/status_message.wasm
TARGET="${CARGO_TARGET_DIR:-../../target}"
BUILD_COMMAND="cargo build"
fi

set -e
cd "$(dirname $0)"
$BUILD_COMMAND --target wasm32-unknown-unknown --release
cp $TARGET/wasm32-unknown-unknown/release/adder.wasm ./res/
#wasm-opt -Oz --output ./res/status_message.wasm ./res/status_message.wasm
8 changes: 4 additions & 4 deletions examples/adder/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
use near_sdk::near;

#[cfg(all(feature = "wasmcov", target_family = "wasm"))]
wasmcov::near::add_coverage!();

#[near(serializers=[borsh, json])]
pub struct Pair(u32, u32);

Expand Down Expand Up @@ -88,7 +91,4 @@ mod tests {

Ok(())
}
}

#[cfg(target_family = "wasm")]
wasmcov::near::add_coverage!();
}
7 changes: 5 additions & 2 deletions examples/callback-results/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,21 @@ crate-type = ["cdylib"]

[dependencies]
near-sdk = { path = "../../near-sdk" }
wasmcov = "0.2"
wasmcov = { version = "0.2", optional = true }

[dev-dependencies]
near-workspaces = "0.11.1"
tokio = { version = "1.14", features = ["full"] }
anyhow = "1.0"
near-sdk = { path = "../../near-sdk", features = ["unit-testing"] }

[features]
wasmcov = ["dep:wasmcov"]

[profile.release]
codegen-units = 1
# Tell `rustc` to optimize for small code size.
opt-level = "z"
lto = false
lto = true
debug = false
panic = "abort"
22 changes: 11 additions & 11 deletions examples/callback-results/build.sh
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
#!/bin/bash
TARGET="${CARGO_TARGET_DIR:-../../target}"
set -e
cd "$(dirname $0)"

INCLUDE_COVERAGE="${1:-false}"

if [ "$INCLUDE_COVERAGE" = "true" ]; then
cargo-wasmcov build --wasmcov-dir /Users/jrmncos/forks/near-sdk-rs/examples/wasmcov -- --all --target wasm32-unknown-unknown --release
cp ../wasmcov/target/wasm32-unknown-unknown/release/callback_results.wasm ./res/
if [ -n "$WASMCOV_DIR" ]; then
TARGET="${WASMCOV_DIR}/target"
BUILD_COMMAND="cargo wasmcov build -- --features wasmcov"
else
cargo build --target wasm32-unknown-unknown --release
cp $TARGET/wasm32-unknown-unknown/release/callback_results.wasm ./res/
fi
TARGET="${CARGO_TARGET_DIR:-../../target}"
BUILD_COMMAND="cargo build"
fi

set -e
cd "$(dirname $0)"
$BUILD_COMMAND --target wasm32-unknown-unknown --release
cp $TARGET/wasm32-unknown-unknown/release/callback_results.wasm ./res/
8 changes: 4 additions & 4 deletions examples/callback-results/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use near_sdk::require;
use near_sdk::{env, near, Promise, PromiseError};

#[cfg(all(feature = "wasmcov", target_family = "wasm"))]
wasmcov::near::add_coverage!();

const A_VALUE: u8 = 8;

#[near(contract_state)]
Expand Down Expand Up @@ -122,7 +125,4 @@ mod tests {

Ok(())
}
}

#[cfg(target_family = "wasm")]
wasmcov::near::add_coverage!();
}
8 changes: 7 additions & 1 deletion examples/cross-contract-calls/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ version = "0.1.0"
authors = ["Near Inc <hello@nearprotocol.com>"]
edition = "2021"

[dependencies]
wasmcov = { version = "0.2", optional = true }

[dev-dependencies]
anyhow = "1.0"
near-sdk = { path = "../../near-sdk", features = ["default", "unit-testing"] }
Expand All @@ -18,9 +21,12 @@ cross-contract-low-level = { path = "./low-level" }
codegen-units = 1
# Tell `rustc` to optimize for small code size.
opt-level = "z"
lto = false
lto = true
debug = false
panic = "abort"

[features]
wasmcov = ["dep:wasmcov"]

[workspace]
members = ["high-level", "low-level"]
25 changes: 12 additions & 13 deletions examples/cross-contract-calls/build.sh
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
#!/bin/bash
TARGET="${CARGO_TARGET_DIR:-../../target}"
set -e
cd "$(dirname $0)"

INCLUDE_COVERAGE="${1:-false}"

if [ "$INCLUDE_COVERAGE" = "true" ]; then
cargo-wasmcov build --wasmcov-dir /Users/jrmncos/forks/near-sdk-rs/examples/wasmcov -- --all --target wasm32-unknown-unknown --release
cp ../wasmcov/target/wasm32-unknown-unknown/release/cross_contract_high_level.wasm ./res/
cp ../wasmcov/target/wasm32-unknown-unknown/release/cross_contract_low_level.wasm ./res/
if [ -n "$WASMCOV_DIR" ]; then
TARGET="${WASMCOV_DIR}/target"
BUILD_COMMAND="cargo wasmcov build -- --features wasmcov"
else
cargo build --all --target wasm32-unknown-unknown --release
cp $TARGET/wasm32-unknown-unknown/release/cross_contract_high_level.wasm ./res/
cp $TARGET/wasm32-unknown-unknown/release/cross_contract_low_level.wasm ./res/
fi
TARGET="${CARGO_TARGET_DIR:-../../target}"
BUILD_COMMAND="cargo build"
fi

set -e
cd "$(dirname $0)"
$BUILD_COMMAND --all --target wasm32-unknown-unknown --release
cp $TARGET/wasm32-unknown-unknown/release/cross_contract_high_level.wasm ./res/
cp $TARGET/wasm32-unknown-unknown/release/cross_contract_low_level.wasm ./res/
5 changes: 4 additions & 1 deletion examples/cross-contract-calls/high-level/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,7 @@ crate-type = ["cdylib"]

[dependencies]
near-sdk = { path = "../../../near-sdk" , features = ["default", "unit-testing"] }
wasmcov = "0.2"
wasmcov = { version = "0.2", optional = true }

[features]
wasmcov = ["dep:wasmcov"]
6 changes: 3 additions & 3 deletions examples/cross-contract-calls/high-level/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use near_sdk::env;
use near_sdk::{log, near, PromiseOrValue};

#[cfg(all(feature = "wasmcov", target_family = "wasm"))]
wasmcov::near::add_coverage!();

#[derive(Default)]
#[near(contract_state)]
pub struct CrossContract {}
Expand Down Expand Up @@ -30,6 +33,3 @@ impl CrossContract {
result
}
}

#[cfg(target_family = "wasm")]
wasmcov::near::add_coverage!();
5 changes: 4 additions & 1 deletion examples/cross-contract-calls/low-level/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,7 @@ crate-type = ["cdylib"]

[dependencies]
near-sdk = { path = "../../../near-sdk" , features = ["default", "unit-testing"] }
wasmcov = "0.2"
wasmcov = { version = "0.2", optional = true }

[features]
wasmcov = ["dep:wasmcov"]
6 changes: 3 additions & 3 deletions examples/cross-contract-calls/low-level/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use near_sdk::serde_json;
use near_sdk::{env, near, require, Gas, NearToken, PromiseResult};

#[cfg(all(feature = "wasmcov", target_family = "wasm"))]
wasmcov::near::add_coverage!();

// Prepaid gas for a single (not inclusive of recursion) `factorial` call.
const FACTORIAL_CALL_GAS: Gas = Gas::from_tgas(20);

Expand Down Expand Up @@ -50,6 +53,3 @@ impl CrossContract {
env::value_return(&serde_json::to_vec(&(cur * n)).unwrap());
}
}

#[cfg(target_family = "wasm")]
wasmcov::near::add_coverage!();
8 changes: 7 additions & 1 deletion examples/factory-contract/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,24 @@ version = "0.1.0"
authors = ["Near Inc <hello@nearprotocol.com>"]
edition = "2021"

[dependencies]
wasmcov = { version = "0.2", optional = true }

[dev-dependencies]
anyhow = "1.0"
test-case = "2.0"
tokio = { version = "1.14", features = ["full"] }
near-workspaces = "0.11.1"
near-sdk = { path = "../../near-sdk", features = ["unit-testing"] }

[features]
wasmcov = ["dep:wasmcov"]

[profile.release]
codegen-units = 1
# Tell `rustc` to optimize for small code size.
opt-level = "z"
lto = false
lto = true
debug = false
panic = "abort"

Expand Down
24 changes: 12 additions & 12 deletions examples/factory-contract/build.sh
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
#!/bin/bash
TARGET="${CARGO_TARGET_DIR:-../../target}"

if [ -n "$WASMCOV_DIR" ]; then
TARGET="${WASMCOV_DIR}/target"
BUILD_COMMAND="cargo wasmcov build -- --features wasmcov"
else
TARGET="${CARGO_TARGET_DIR:-../../target}"
BUILD_COMMAND="cargo build"
fi

set -e
cd "$(dirname $0)"

INCLUDE_COVERAGE="${1:-false}"

if [ "$INCLUDE_COVERAGE" = "true" ]; then
cargo-wasmcov build --wasmcov-dir /Users/jrmncos/forks/near-sdk-rs/examples/wasmcov -- --all --target wasm32-unknown-unknown --release
cp ../wasmcov/target/wasm32-unknown-unknown/release/factory_contract_high_level.wasm ./res/
cp ../wasmcov/target/wasm32-unknown-unknown/release/factory_contract_low_level.wasm ./res/
else
cargo build --all --target wasm32-unknown-unknown --release
cp $TARGET/wasm32-unknown-unknown/release/factory_contract_high_level.wasm ./res/
cp $TARGET/wasm32-unknown-unknown/release/factory_contract_low_level.wasm ./res/
fi
$BUILD_COMMAND --all --target wasm32-unknown-unknown --release
cp $TARGET/wasm32-unknown-unknown/release/factory_contract_high_level.wasm ./res/
cp $TARGET/wasm32-unknown-unknown/release/factory_contract_low_level.wasm ./res/
5 changes: 4 additions & 1 deletion examples/factory-contract/high-level/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ crate-type = ["cdylib"]

[dependencies]
near-sdk = { path = "../../../near-sdk" }
wasmcov = "0.2"
wasmcov = { version = "0.2", optional = true }

[dev-dependencies]
near-sdk = { path = "../../../near-sdk", features = ["unit-testing"] }

[features]
wasmcov = ["dep:wasmcov"]
Loading

0 comments on commit dbdeef1

Please sign in to comment.