diff --git a/basics/checking-accounts/asm/Cargo.toml b/basics/checking-accounts/asm/Cargo.toml index 830cb8172..19349c560 100644 --- a/basics/checking-accounts/asm/Cargo.toml +++ b/basics/checking-accounts/asm/Cargo.toml @@ -1,4 +1,5 @@ [package] +build = "build.rs" name = "checking-account-asm-program" version = "0.1.0" edition = "2021" diff --git a/basics/checking-accounts/asm/build.rs b/basics/checking-accounts/asm/build.rs new file mode 100644 index 000000000..08ca44557 --- /dev/null +++ b/basics/checking-accounts/asm/build.rs @@ -0,0 +1,22 @@ +// The tests embed the assembled program with include_bytes!, which needs the +// file to exist at compile time. `sbpf build` produces it, and it is gitignored, +// so a fresh checkout has no binary and the test module cannot compile. Set a +// cfg when the binary is present and let the tests key off it: present, they +// compile and run; absent, they are left out and the crate still builds. +use std::path::Path; + +const BINARY: &str = "deploy/checking-account-asm-program.so"; + +fn main() { + // Declare the cfg so `-D warnings` builds do not fail on unexpected_cfgs. + println!("cargo::rustc-check-cfg=cfg(has_asm_binary)"); + // Re-run when the binary appears or changes, so the cfg never goes stale. + println!("cargo::rerun-if-changed={BINARY}"); + + if Path::new(BINARY).exists() { + println!("cargo::rustc-cfg=has_asm_binary"); + } else { + println!("cargo::warning=ASM binary not found at {BINARY}: tests skipped"); + println!("cargo::warning=Run `sbpf build` in this directory to generate it"); + } +} diff --git a/basics/checking-accounts/asm/src/lib.rs b/basics/checking-accounts/asm/src/lib.rs index ec4cb4200..fa9ad5bc3 100644 --- a/basics/checking-accounts/asm/src/lib.rs +++ b/basics/checking-accounts/asm/src/lib.rs @@ -1,4 +1,4 @@ -#[cfg(test)] +#[cfg(all(test, has_asm_binary))] mod tests { use litesvm::LiteSVM; diff --git a/basics/create-account/asm/Cargo.toml b/basics/create-account/asm/Cargo.toml index 8c695fa1a..b37de8565 100644 --- a/basics/create-account/asm/Cargo.toml +++ b/basics/create-account/asm/Cargo.toml @@ -1,4 +1,5 @@ [package] +build = "build.rs" name = "create-account-asm-program" version = "0.1.0" edition = "2021" diff --git a/basics/create-account/asm/build.rs b/basics/create-account/asm/build.rs new file mode 100644 index 000000000..1107f14d7 --- /dev/null +++ b/basics/create-account/asm/build.rs @@ -0,0 +1,22 @@ +// The tests embed the assembled program with include_bytes!, which needs the +// file to exist at compile time. `sbpf build` produces it, and it is gitignored, +// so a fresh checkout has no binary and the test module cannot compile. Set a +// cfg when the binary is present and let the tests key off it: present, they +// compile and run; absent, they are left out and the crate still builds. +use std::path::Path; + +const BINARY: &str = "deploy/create-account-asm-program.so"; + +fn main() { + // Declare the cfg so `-D warnings` builds do not fail on unexpected_cfgs. + println!("cargo::rustc-check-cfg=cfg(has_asm_binary)"); + // Re-run when the binary appears or changes, so the cfg never goes stale. + println!("cargo::rerun-if-changed={BINARY}"); + + if Path::new(BINARY).exists() { + println!("cargo::rustc-cfg=has_asm_binary"); + } else { + println!("cargo::warning=ASM binary not found at {BINARY}: tests skipped"); + println!("cargo::warning=Run `sbpf build` in this directory to generate it"); + } +} diff --git a/basics/create-account/asm/src/lib.rs b/basics/create-account/asm/src/lib.rs index 84f30fadc..1851dd4ea 100644 --- a/basics/create-account/asm/src/lib.rs +++ b/basics/create-account/asm/src/lib.rs @@ -1,4 +1,4 @@ -#[cfg(test)] +#[cfg(all(test, has_asm_binary))] mod tests { use litesvm::LiteSVM; diff --git a/basics/hello-solana/asm/Cargo.toml b/basics/hello-solana/asm/Cargo.toml index 35b63c384..996ab6b67 100644 --- a/basics/hello-solana/asm/Cargo.toml +++ b/basics/hello-solana/asm/Cargo.toml @@ -1,4 +1,5 @@ [package] +build = "build.rs" name = "hello-solana-asm-program" version = "0.1.0" edition = "2021" diff --git a/basics/hello-solana/asm/build.rs b/basics/hello-solana/asm/build.rs new file mode 100644 index 000000000..a106a3f03 --- /dev/null +++ b/basics/hello-solana/asm/build.rs @@ -0,0 +1,22 @@ +// The tests embed the assembled program with include_bytes!, which needs the +// file to exist at compile time. `sbpf build` produces it, and it is gitignored, +// so a fresh checkout has no binary and the test module cannot compile. Set a +// cfg when the binary is present and let the tests key off it: present, they +// compile and run; absent, they are left out and the crate still builds. +use std::path::Path; + +const BINARY: &str = "deploy/hello-solana-asm-program.so"; + +fn main() { + // Declare the cfg so `-D warnings` builds do not fail on unexpected_cfgs. + println!("cargo::rustc-check-cfg=cfg(has_asm_binary)"); + // Re-run when the binary appears or changes, so the cfg never goes stale. + println!("cargo::rerun-if-changed={BINARY}"); + + if Path::new(BINARY).exists() { + println!("cargo::rustc-cfg=has_asm_binary"); + } else { + println!("cargo::warning=ASM binary not found at {BINARY}: tests skipped"); + println!("cargo::warning=Run `sbpf build` in this directory to generate it"); + } +} diff --git a/basics/hello-solana/asm/src/lib.rs b/basics/hello-solana/asm/src/lib.rs index 3057aa1bd..10e0b3fe4 100644 --- a/basics/hello-solana/asm/src/lib.rs +++ b/basics/hello-solana/asm/src/lib.rs @@ -1,4 +1,4 @@ -#[cfg(test)] +#[cfg(all(test, has_asm_binary))] mod tests { use litesvm::LiteSVM; use solana_instruction::{AccountMeta, Instruction}; diff --git a/basics/transfer-sol/asm/Cargo.toml b/basics/transfer-sol/asm/Cargo.toml index 100a04284..3f04e319d 100644 --- a/basics/transfer-sol/asm/Cargo.toml +++ b/basics/transfer-sol/asm/Cargo.toml @@ -2,6 +2,7 @@ name = "asm" version = "0.1.0" edition = "2021" +build = "build.rs" [dependencies] diff --git a/basics/transfer-sol/asm/build.rs b/basics/transfer-sol/asm/build.rs new file mode 100644 index 000000000..8bc071d1c --- /dev/null +++ b/basics/transfer-sol/asm/build.rs @@ -0,0 +1,22 @@ +// The tests embed the assembled program with include_bytes!, which needs the +// file to exist at compile time. `sbpf build` produces it, and it is gitignored, +// so a fresh checkout has no binary and the test module cannot compile. Set a +// cfg when the binary is present and let the tests key off it: present, they +// compile and run; absent, they are left out and the crate still builds. +use std::path::Path; + +const BINARY: &str = "deploy/transfer-sol-cpi.so"; + +fn main() { + // Declare the cfg so `-D warnings` builds do not fail on unexpected_cfgs. + println!("cargo::rustc-check-cfg=cfg(has_asm_binary)"); + // Re-run when the binary appears or changes, so the cfg never goes stale. + println!("cargo::rerun-if-changed={BINARY}"); + + if Path::new(BINARY).exists() { + println!("cargo::rustc-cfg=has_asm_binary"); + } else { + println!("cargo::warning=ASM binary not found at {BINARY}: tests skipped"); + println!("cargo::warning=Run `sbpf build` in this directory to generate it"); + } +} diff --git a/basics/transfer-sol/asm/src/lib.rs b/basics/transfer-sol/asm/src/lib.rs index 3a7c8e1f2..3a4344bbd 100644 --- a/basics/transfer-sol/asm/src/lib.rs +++ b/basics/transfer-sol/asm/src/lib.rs @@ -1,4 +1,4 @@ -#[cfg(test)] +#[cfg(all(test, has_asm_binary))] mod tests { use litesvm::LiteSVM; use solana_instruction::{AccountMeta, Instruction};