Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions Cargo.lock

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

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ multiple_crate_versions = "allow"

[workspace.dependencies]
smplx-macros = { path = "./crates/macros", version = "0.0.3" }
smplx-build = { path = "./crates/build", version = "0.0.3" }
smplx-build-internal = { path = "./crates/build", version = "0.0.3" }
smplx-build = { path = "./crates/smplx-build", version = "0.0.1" }
smplx-test = { path = "./crates/test", version = "0.0.3" }
smplx-regtest = { path = "./crates/regtest", version = "0.0.3" }
smplx-sdk = { path = "./crates/sdk", version = "0.0.3" }
Expand Down
2 changes: 1 addition & 1 deletion crates/build/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[package]
name = "smplx-build"
name = "smplx-build-internal"
version = "0.0.3"
description = "Simplex build command internal implementation"
license.workspace = true
Expand Down
2 changes: 1 addition & 1 deletion crates/cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ workspace = true
[dependencies]
smplx-regtest = { workspace = true }
smplx-test = { workspace = true }
smplx-build = { workspace = true }
smplx-build-internal = { workspace = true }
smplx-sdk = { workspace = true }

thiserror = { workspace = true }
Expand Down
2 changes: 1 addition & 1 deletion crates/cli/src/commands/build.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use smplx_build::{ArtifactsGenerator, ArtifactsResolver, BuildConfig};
use smplx_build_internal::{ArtifactsGenerator, ArtifactsResolver, BuildConfig};

use super::error::CommandError;

Expand Down
2 changes: 1 addition & 1 deletion crates/cli/src/commands/clean.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::{fmt::Display, fs, path::PathBuf};

use smplx_build::{ArtifactsResolver, BuildConfig};
use smplx_build_internal::{ArtifactsResolver, BuildConfig};

use crate::commands::error::CleanError;
use crate::commands::error::CommandError;
Expand Down
2 changes: 1 addition & 1 deletion crates/cli/src/commands/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub enum CommandError {
Test(#[from] smplx_test::error::TestError),

#[error(transparent)]
Build(#[from] smplx_build::error::BuildError),
Build(#[from] smplx_build_internal::error::BuildError),

#[error(transparent)]
Init(#[from] InitError),
Expand Down
2 changes: 1 addition & 1 deletion crates/cli/src/config/core.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use serde::Deserialize;
use std::path::{Path, PathBuf};

use smplx_build::BuildConfig;
use smplx_build_internal::BuildConfig;
use smplx_regtest::RegtestConfig;
use smplx_test::TestConfig;

Expand Down
2 changes: 1 addition & 1 deletion crates/macros/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ proc-macro = true
workspace = true

[dependencies]
smplx-build = { workspace = true }
smplx-build-internal = { workspace = true }
smplx-test = { workspace = true }

syn = { version = "2.0.114", default-features = false, features = ["parsing", "proc-macro"] }
4 changes: 2 additions & 2 deletions crates/macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ use proc_macro::TokenStream;

#[proc_macro]
pub fn include_simf(tokenstream: TokenStream) -> TokenStream {
let input = syn::parse_macro_input!(tokenstream as smplx_build::macros::parse::SynFilePath);
let input = syn::parse_macro_input!(tokenstream as smplx_build_internal::macros::parse::SynFilePath);

match smplx_build::macros::expand(&input) {
match smplx_build_internal::macros::expand(&input) {
Ok(ts) => ts.into(),
Err(e) => e.to_compile_error().into(),
}
Expand Down
14 changes: 14 additions & 0 deletions crates/smplx-build/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[package]
name = "smplx-build"
version = "0.0.1"
description = "Build script helper for generating Simplex contract artifacts"
license.workspace = true
edition.workspace = true
repository = "https://github.com/BlockstreamResearch/smplx"
readme = "../../README.md"

[lints]
workspace = true

[dependencies]
smplx-build-internal = { workspace = true }
182 changes: 182 additions & 0 deletions crates/smplx-build/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
use std::path::{Path, PathBuf};

pub use smplx_build_internal::error::BuildError;
use smplx_build_internal::{ArtifactsGenerator, ArtifactsResolver, BuildConfig};

#[allow(clippy::needless_doctest_main)]
/// Builder for configuring and running Simplex artifact generation from `build.rs`.
///
/// Settings are resolved in priority order (highest wins):
/// 1. Values set directly on the builder (`.src_dir(...)`, etc.)
/// 2. Values loaded from a `Simplex.toml` config file (`.config(...)`)
/// 3. Built-in defaults (`simf/`, `**/*.simf`, `src/artifacts`)
///
/// # Examples
///
/// No config file — configure everything in `build.rs`:
/// ```no_run
/// fn main() {
/// smplx_build::Builder::new()
/// .src_dir("simf")
/// .out_dir("src/artifacts")
/// .simf_files(["**/*.simf"])
/// .generate()
/// .unwrap();
/// }
/// ```
///
/// Load from `Simplex.toml` but override the output directory:
/// ```no_run
/// fn main() {
/// smplx_build::Builder::new()
/// .config("Simplex.toml")
/// .out_dir("src/generated")
/// .generate()
/// .unwrap();
/// }
/// ```
pub struct Builder {
config_path: Option<PathBuf>,
src_dir: Option<String>,
out_dir: Option<String>,
simf_files: Option<Vec<String>>,
}

impl Builder {
pub fn new() -> Self {
Self {
config_path: None,
src_dir: None,
out_dir: None,
simf_files: None,
}
}

/// Load base settings from a `Simplex.toml` file.
/// Fields set directly on the builder take precedence over values in the file.
pub fn config(mut self, path: impl AsRef<Path>) -> Self {
self.config_path = Some(path.as_ref().to_path_buf());
self
}

/// Directory containing `.simf` source files (default: `simf`).
pub fn src_dir(mut self, dir: impl Into<String>) -> Self {
self.src_dir = Some(dir.into());
self
}

/// Directory where generated Rust artifacts are written (default: `src/artifacts`).
pub fn out_dir(mut self, dir: impl Into<String>) -> Self {
self.out_dir = Some(dir.into());
self
}

/// Glob patterns selecting which `.simf` files to compile (default: `["**/*.simf"]`).
/// Replaces the full list — call once with all patterns you need.
pub fn simf_files(mut self, patterns: impl IntoIterator<Item = impl Into<String>>) -> Self {
self.simf_files = Some(patterns.into_iter().map(Into::into).collect());
self
}

/// Run artifact generation.
///
/// Emits `cargo:rerun-if-changed` directives for the config file (if any)
/// and every resolved `.simf` input file.
pub fn generate(self) -> Result<(), BuildError> {
// Start from defaults, then overlay the config file (if provided).
let mut config = BuildConfig::default();

if let Some(ref path) = self.config_path {
if !path.exists() {
return Err(BuildError::Io(std::io::Error::new(
std::io::ErrorKind::NotFound,
format!("config file not found: {}", path.display()),
)));
}
config = BuildConfig::from_file(path)?;
println!("cargo:rerun-if-changed={}", path.display());
}

// Builder fields take highest precedence.
if let Some(src_dir) = self.src_dir {
config.src_dir = src_dir;
}
if let Some(out_dir) = self.out_dir {
config.out_dir = out_dir;
}
if let Some(simf_files) = self.simf_files {
config.simf_files = simf_files;
}

let out_dir = ArtifactsResolver::resolve_local_dir(&config.out_dir)?;
let src_dir = {
let p = ArtifactsResolver::resolve_local_dir(&config.src_dir)?;
// resolve_files_to_build canonicalizes paths (producing \\?\ UNC paths on
// Windows), so canonicalize src_dir too so strip_prefix works correctly.
if p.exists() { p.canonicalize()? } else { p }
};
let files = ArtifactsResolver::resolve_files_to_build(&config.src_dir, &config.simf_files)?;

for file in &files {
println!("cargo:rerun-if-changed={}", file.display());
}

ArtifactsGenerator::generate_artifacts(&out_dir, &src_dir, &files)?;

Ok(())
}
}

impl Default for Builder {
fn default() -> Self {
Self::new()
}
}

#[allow(clippy::needless_doctest_main)]
/// Generate Simplex contract artifacts from a `build.rs` script.
///
/// Reads the `[build]` section of `config_path` (a `Simplex.toml` file) and
/// generates Rust bindings for every `.simf` contract found. Equivalent to
/// `Builder::new().config(config_path).generate()`.
///
/// # Example
///
/// ```no_run
/// fn main() {
/// smplx_build::generate_artifacts("Simplex.toml").unwrap();
/// }
/// ```
pub fn generate_artifacts(config_path: impl AsRef<Path>) -> Result<(), BuildError> {
Builder::new().config(config_path).generate()
}

/// Convenience macro for use in `build.rs`.
///
/// Calls [`generate_artifacts`] and panics with a clear message on failure.
///
/// ```no_run
/// // uses "Simplex.toml" by default
/// fn main() {
/// smplx_build::generate_artifacts!();
/// }
/// ```
///
/// With explicit config
/// ```no_run
/// // explicit config path
/// fn main() {
/// smplx_build::generate_artifacts!("Simplex.toml");
/// }
/// ```
#[macro_export]
macro_rules! generate_artifacts {
($config:expr) => {
if let Err(e) = $crate::generate_artifacts($config) {
panic!("smplx-build: failed to generate artifacts: {}", e);
}
};
() => {
$crate::generate_artifacts!("Simplex.toml")
};
}
22 changes: 15 additions & 7 deletions examples/basic/Cargo.lock

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

3 changes: 3 additions & 0 deletions examples/basic/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ edition = "2024"
rust-version = "1.91.0"
version = "0.1.0"

[build-dependencies]
smplx-build = { path = "../../crates/smplx-build" }

[dependencies]
smplx-std = { path = "../../crates/simplex" }

Expand Down
3 changes: 3 additions & 0 deletions examples/basic/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fn main() {
smplx_build::generate_artifacts!();
}
Loading