Skip to content

Commit

Permalink
build_script
Browse files Browse the repository at this point in the history
  • Loading branch information
lwshang committed Oct 27, 2023
1 parent 7a9ddfb commit b65e960
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 12 deletions.
7 changes: 7 additions & 0 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[workspace]
members = ["unit_struct"]
members = ["unit_struct", "build_script"]
resolver = "2"

[workspace.dependencies]
Expand Down
12 changes: 12 additions & 0 deletions build_script/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[package]
name = "build_script"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
ic-cdk.workspace = true

[lib]
crate-type = ["cdylib"]
35 changes: 35 additions & 0 deletions build_script/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
use std::path::Path;

static BINDING: &str = "trait Counter {
fn inc();
fn read() -> u64;
}
#[export_name = \"canister_update inc\"]
fn __canister_method_inc() {
ic_cdk::setup();
ic_cdk::spawn(async {
let _result = Canister::inc();
ic_cdk::api::call::reply(())
});
}
#[export_name = \"canister_query read\"]
fn __canister_method_read() {
ic_cdk::setup();
ic_cdk::spawn(async {
let result = Canister::read();
ic_cdk::api::call::reply((result,))
});
}";

fn main() {
println!("cargo:rerun-if-changed=../counter.did");
println!("cargo:rerun-if-changed=build.rs");
// ic_cdk_bindgen::generate_provider("../counter.did").unwrap()
// We can also introduce Config similar to
// https://docs.rs/prost-build/latest/prost_build/struct.Config.html#method.compile_protos
let out_dir = std::env::var("OUT_DIR").unwrap();
let path = Path::new(&out_dir).join("counter.rs");
std::fs::write(&path, BINDING).unwrap();
}
19 changes: 19 additions & 0 deletions build_script/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
include!(concat!(env!("OUT_DIR"), "/counter.rs"));

use std::cell::Cell;

thread_local! {
static COUNTER: Cell<u64> = Cell::new(0);
}

struct Canister;

impl Counter for Canister {
fn inc() {
COUNTER.set(COUNTER.get() + 1);
}

fn read() -> u64 {
COUNTER.get()
}
}
19 changes: 8 additions & 11 deletions dfx.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,14 @@
"build": "sh build.sh unit_struct",
"candid": "counter.did",
"wasm": "target/wasm32-unknown-unknown/release/unit_struct.wasm",
"shrink": true,
"metadata": [
{
"name": "candid:service",
"visibility": "public"
},
{
"name": "candid:args",
"visibility": "public"
}
]
"shrink": true
},
"build_script": {
"type": "custom",
"build": "sh build.sh build_script",
"candid": "counter.did",
"wasm": "target/wasm32-unknown-unknown/release/build_script.wasm",
"shrink": true
}
}
}

0 comments on commit b65e960

Please sign in to comment.