Skip to content

Commit

Permalink
Merge pull request #14 from joii2020/main
Browse files Browse the repository at this point in the history
Add Template: Native Simulator Debug
  • Loading branch information
joii2020 authored Nov 12, 2024
2 parents c4a147d + b6d79b9 commit a9d8dd7
Show file tree
Hide file tree
Showing 23 changed files with 1,900 additions and 27 deletions.
2 changes: 1 addition & 1 deletion atomics-contract/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ version = "0.1.0"
edition = "2021"

[dependencies]
ckb-std = "0.15.1"
ckb-std = { version = "0.16.3", default-features = false, features = ["allocator", "calc-hash", "ckb-types", "libc"] }
log = { version = "0.4.20", default-features = false }

[build-dependencies]
Expand Down
2 changes: 1 addition & 1 deletion atomics-contract/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ TOP := $(cur_dir)
# RUSTFLAGS that are likely to be tweaked by developers. For example,
# while we enable debug logs by default here, some might want to strip them
# for minimal code size / consumed cycles.
CUSTOM_RUSTFLAGS := --cfg debug_assertions
CUSTOM_RUSTFLAGS := -C debug-assertions
# RUSTFLAGS that are less likely to be tweaked by developers. Most likely
# one would want to keep the default values here.
FULL_RUSTFLAGS := -C target-feature=+zba,+zbb,+zbc,+zbs,-a $(CUSTOM_RUSTFLAGS)
Expand Down
2 changes: 1 addition & 1 deletion atomics-contract/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#![no_std]
#![cfg_attr(not(test), no_std)]
#![cfg_attr(not(test), no_main)]

#[cfg(test)]
Expand Down
1 change: 1 addition & 0 deletions cargo-generate.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ sub_templates = [
"contract",
"atomics-contract",
"stack-reorder-contract",
"native-simulator",
# Dependency crate templates
"c-wrapper-crate",
"x64-simulator-crate",
Expand Down
5 changes: 4 additions & 1 deletion contract/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,7 @@ version = "0.1.0"
edition = "2021"

[dependencies]
ckb-std = "0.15.1"
ckb-std = "0.16.3"

[features]
native-simulator = ["ckb-std/native-simulator"]
5 changes: 4 additions & 1 deletion contract/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ TOP := $(cur_dir)
# RUSTFLAGS that are likely to be tweaked by developers. For example,
# while we enable debug logs by default here, some might want to strip them
# for minimal code size / consumed cycles.
CUSTOM_RUSTFLAGS := --cfg debug_assertions
CUSTOM_RUSTFLAGS := -C debug-assertions
# RUSTFLAGS that are less likely to be tweaked by developers. Most likely
# one would want to keep the default values here.
FULL_RUSTFLAGS := -C target-feature=+zba,+zbb,+zbc,+zbs,-a $(CUSTOM_RUSTFLAGS)
Expand All @@ -19,6 +19,7 @@ MODE := release
# we use a bash script with somes heuristics to find clang in current system.
CLANG := $(shell $(TOP)/scripts/find_clang)
AR := $(subst clang,llvm-ar,$(CLANG))
OBJCOPY := $(subst clang,llvm-objcopy,$(CLANG))
# When this is set to some value, the generated binaries will be copied over
BUILD_DIR :=
# Generated binaries to copy. By convention, a Rust crate's directory name will
Expand All @@ -41,6 +42,8 @@ build:
for binary in $(BINARIES); do \
echo "Copying binary $$binary to build directory"; \
cp $(TOP)/target/riscv64imac-unknown-none-elf/$(MODE)/$$binary $(TOP)/$(BUILD_DIR); \
cp $(TOP)/$(BUILD_DIR)/$$binary $(TOP)/$(BUILD_DIR)/$$binary.debug; \
$(OBJCOPY) --strip-debug --strip-all $(TOP)/$(BUILD_DIR)/$$binary; \
done \
fi

Expand Down
7 changes: 7 additions & 0 deletions contract/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#![cfg_attr(not(feature = "native-simulator"), no_std)]
#![allow(special_module_name)]
#![allow(unused_attributes)]
#[cfg(feature = "native-simulator")]
mod main;
#[cfg(feature = "native-simulator")]
pub use main::program_entry;
12 changes: 5 additions & 7 deletions contract/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
#![no_std]
#![cfg_attr(not(any(feature = "native-simulator", test)), no_std)]
#![cfg_attr(not(test), no_main)]

#[cfg(test)]
#[cfg(any(feature = "native-simulator", test))]
extern crate alloc;

#[cfg(not(test))]
use ckb_std::default_alloc;
#[cfg(not(test))]
#[cfg(not(any(feature = "native-simulator", test)))]
ckb_std::entry!(program_entry);
#[cfg(not(test))]
default_alloc!();
#[cfg(not(any(feature = "native-simulator", test)))]
ckb_std::default_alloc!();

pub fn program_entry() -> i8 {
ckb_std::debug!("This is a sample contract!");
Expand Down
2 changes: 2 additions & 0 deletions native-simulator/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/build
/target
12 changes: 12 additions & 0 deletions native-simulator/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[package]
name = "{{project-name}}"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
{{project-name | append: "@@SIMULATOR_PLACEHOLDER@@" | remove: "-sim@@SIMULATOR_PLACEHOLDER@@"}} = { path = "../../contracts/{{project-name | append: "@@SIMULATOR_PLACEHOLDER@@" | remove: "-sim@@SIMULATOR_PLACEHOLDER@@"}}", features = ["native-simulator"] }
ckb-std = { version = "0.16.3", features = ["native-simulator"] }

[lib]
crate-type = ["cdylib"]
10 changes: 10 additions & 0 deletions native-simulator/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# {{project-name}}

TODO: Write this readme

*This template is used to provide native simulator for a particular contract, and is not designed to be used on its own.*

*This project was bootstrapped with [ckb-script-templates].*


[ckb-script-templates]: https://github.com/cryptape/ckb-script-templates
2 changes: 2 additions & 0 deletions native-simulator/cargo-generate.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[template]
cargo_generate_version = ">=0.16.0"
1 change: 1 addition & 0 deletions native-simulator/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ckb_std::entry_simulator!({{project-name | append: "@@SIMULATOR_PLACEHOLDER@@" | remove: "-sim@@SIMULATOR_PLACEHOLDER@@" | replace: "-", "_"}}::program_entry);
2 changes: 1 addition & 1 deletion stack-reorder-contract/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ version = "0.1.0"
edition = "2021"

[dependencies]
ckb-std = "0.15.1"
ckb-std = "0.16.3"

[build-dependencies]
cc = "1.0"
2 changes: 1 addition & 1 deletion stack-reorder-contract/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ TOP := $(cur_dir)
# RUSTFLAGS that are likely to be tweaked by developers. For example,
# while we enable debug logs by default here, some might want to strip them
# for minimal code size / consumed cycles.
CUSTOM_RUSTFLAGS := --cfg debug_assertions
CUSTOM_RUSTFLAGS := -C debug-assertions
# RUSTFLAGS that are less likely to be tweaked by developers. Most likely
# one would want to keep the default values here.
FULL_RUSTFLAGS := -C target-feature=+zba,+zbb,+zbc,+zbs,-a $(CUSTOM_RUSTFLAGS) \
Expand Down
3 changes: 2 additions & 1 deletion stack-reorder-contract/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#![no_std]
#![cfg_attr(not(test), no_std)]
#![cfg_attr(not(test), no_main)]

#[cfg(test)]
Expand All @@ -11,6 +11,7 @@ ckb_std::entry!(program_entry);
#[cfg(not(test))]
default_alloc!();

#[allow(unused_variables, unused_assignments)]
pub fn program_entry() -> i8 {
let mut x: u64;
unsafe {
Expand Down
4 changes: 2 additions & 2 deletions standalone-contract/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ version = "0.1.0"
edition = "2021"

[dependencies]
ckb-std = "0.15.1"
ckb-std = "0.16.3"

[dev-dependencies]
ckb-testtool = "0.10.2"
ckb-testtool = "0.14.0"
serde_json = "1.0"
2 changes: 1 addition & 1 deletion standalone-contract/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ TOP := $(cur_dir)
# RUSTFLAGS that are likely to be tweaked by developers. For example,
# while we enable debug logs by default here, some might want to strip them
# for minimal code size / consumed cycles.
CUSTOM_RUSTFLAGS := --cfg debug_assertions
CUSTOM_RUSTFLAGS := -C debug-assertions
# RUSTFLAGS that are less likely to be tweaked by developers. Most likely
# one would want to keep the default values here.
FULL_RUSTFLAGS := -C target-feature=+zba,+zbb,+zbc,+zbs,-a $(CUSTOM_RUSTFLAGS)
Expand Down
Loading

0 comments on commit a9d8dd7

Please sign in to comment.