Skip to content

Commit 257a2a6

Browse files
committed
Add basic templates
0 parents  commit 257a2a6

File tree

12 files changed

+377
-0
lines changed

12 files changed

+377
-0
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# ckb-script-templates
2+
3+
This repository keeps a series of CKB script templates that can be inflated via [cargo-generate](https://github.com/cargo-generate/cargo-generate). Those templates enable a native development flow on mainstream Linux, macOS and Windows machines using stock version of latest stable Rust & clang C compiler.

cargo-generate.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[template]
2+
sub_templates = [
3+
"contract",
4+
"workspace",
5+
]

contract/Cargo.toml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[package]
2+
name = "{{project-name}}"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
[dependencies]
7+
ckb-std = "0.15.0"

contract/Makefile

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
# We cannot use $(shell pwd), which will return unix path format on Windows,
2+
# making it hard to use.
3+
cur_dir = $(dir $(abspath $(lastword $(MAKEFILE_LIST))))
4+
5+
TOP := $(cur_dir)
6+
# RUSTFLAGS that are likely to be tweaked by developers. For example,
7+
# while we enable debug logs by default here, some might want to strip them
8+
# for minimal code size / consumed cycles.
9+
CUSTOM_RUSTFLAGS := --cfg debug_assertions
10+
# RUSTFLAGS that are less likely to be tweaked by developers. Most likely
11+
# one would want to keep the default values here.
12+
FULL_RUSTFLAGS := -C target-feature=+zba,+zbb,+zbc,+zbs $(CUSTOM_RUSTFLAGS)
13+
# Additional cargo args to append here. For example, one can use
14+
# make test CARGO_ARGS="-- --nocapture" so as to inspect data emitted to
15+
# stdout in unit tests
16+
CARGO_ARGS :=
17+
MODE := release
18+
# Tweak this to change the clang version to use for building C code. By default
19+
# we use a bash script with somes heuristics to find clang in current system.
20+
CLANG := $(shell $(TOP)/scripts/find_clang)
21+
# When this is set to some value, the generated binaries will be copied over
22+
BUILD_DIR :=
23+
# Generated binaries to copy. By convention, a Rust crate's directory name will
24+
# likely match the crate name, which is also the name of the final binary.
25+
# However if this is not the case, you can tweak this variable. As the name hints,
26+
# more than one binary is supported here.
27+
BINARIES := $(notdir $(shell pwd))
28+
29+
ifeq (release,$(MODE))
30+
MODE_ARGS := --release
31+
endif
32+
33+
default: build test
34+
35+
build:
36+
RUSTFLAGS="$(FULL_RUSTFLAGS)" TARGET_CC="$(CLANG)" \
37+
cargo build --target=riscv64imac-unknown-none-elf $(MODE_ARGS) $(CARGO_ARGS)
38+
@set -eu; \
39+
if [ "x$(BUILD_DIR)" != "x" ]; then \
40+
for binary in $(BINARIES); do \
41+
echo "Copying binary $$binary to build directory"; \
42+
cp $(TOP)/target/riscv64imac-unknown-none-elf/$(MODE)/$$binary $(TOP)/$(BUILD_DIR); \
43+
done \
44+
fi
45+
46+
# test, check, clippy and fmt here are provided for completeness,
47+
# there is nothing wrong invoking cargo directly instead of make.
48+
test:
49+
cargo test $(CARGO_ARGS)
50+
51+
check:
52+
cargo check $(CARGO_ARGS)
53+
54+
clippy:
55+
cargo clippy $(CARGO_ARGS)
56+
57+
fmt:
58+
cargo fmt $(CARGO_ARGS)
59+
60+
# Arbitrary cargo command is supported here. For example:
61+
#
62+
# make cargo CARGO_CMD=expand CARGO_ARGS="--ugly"
63+
#
64+
# Invokes:
65+
# cargo expand --ugly
66+
CARGO_CMD :=
67+
cargo:
68+
cargo $(CARGO_CMD) $(CARGO_ARGS)
69+
70+
clean:
71+
cargo clean
72+
73+
prepare:
74+
rustup target add riscv64imac-unknown-none-elf
75+
76+
.PHONY: build test check clippy fmt cargo clean prepare

contract/src/main.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#![no_std]
2+
#![cfg_attr(not(test), no_main)]
3+
4+
#[cfg(test)]
5+
extern crate alloc;
6+
7+
#[cfg(not(test))]
8+
use ckb_std::default_alloc;
9+
#[cfg(not(test))]
10+
ckb_std::entry!(program_entry);
11+
#[cfg(not(test))]
12+
default_alloc!();
13+
14+
pub fn program_entry() -> i8 {
15+
ckb_std::debug!("This is a sample contract!");
16+
17+
0
18+
}

workspace/.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/build
2+
/target
3+
/tests/failed_txs

workspace/Cargo.toml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
[workspace]
2+
resolver = "2"
3+
4+
members = [
5+
"crates/big-cell-fetcher",
6+
"crates/big-witness-hasher",
7+
"contracts/atomics-without-a",
8+
"contracts/legacy-c-dependency",
9+
"contracts/loads-of-hashes",
10+
"contracts/minimal-log",
11+
"contracts/stack-reorder",
12+
"tests",
13+
]
14+
15+
[profile.release]
16+
overflow-checks = true
17+
strip = true
18+
codegen-units = 1

workspace/Makefile

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
# We cannot use $(shell pwd), which will return unix path format on Windows,
2+
# making it hard to use.
3+
cur_dir = $(dir $(abspath $(lastword $(MAKEFILE_LIST))))
4+
5+
TOP := $(cur_dir)
6+
# RUSTFLAGS that are likely to be tweaked by developers. For example,
7+
# while we enable debug logs by default here, some might want to strip them
8+
# for minimal code size / consumed cycles.
9+
CUSTOM_RUSTFLAGS := --cfg debug_assertions
10+
# Additional cargo args to append here. For example, one can use
11+
# make test CARGO_ARGS="-- --nocapture" so as to inspect data emitted to
12+
# stdout in unit tests
13+
CARGO_ARGS :=
14+
MODE := release
15+
# Tweak this to change the clang version to use for building C code. By default
16+
# we use a bash script with somes heuristics to find clang in current system.
17+
CLANG := $(shell $(TOP)/scripts/find_clang)
18+
# When this is set, a single contract will be built instead of all contracts
19+
CONTRACT :=
20+
# By default, we would clean build/{release,debug} folder first, in case old
21+
# contracts are mixed together with new ones, if for some reason you want to
22+
# revert this behavior, you can change this to anything other than true
23+
CLEAN_BUILD_DIR_FIRST := true
24+
BUILD_DIR := build/$(MODE)
25+
26+
# Pass setups to child make processes
27+
export CUSTOM_RUSTFLAGS
28+
export TOP
29+
export CARGO_ARGS
30+
export MODE
31+
export CLANG
32+
export BUILD_DIR
33+
34+
default: build test
35+
36+
build:
37+
@if [ "x$(CLEAN_BUILD_DIR_FIRST)" = "xtrue" ]; then \
38+
echo "Cleaning $(BUILD_DIR) directory..."; \
39+
rm -rf $(BUILD_DIR); \
40+
fi
41+
mkdir -p $(BUILD_DIR)
42+
@set -eu; \
43+
if [ "x$(CONTRACT)" = "x" ]; then \
44+
for contract in $(wildcard contracts/*); do \
45+
make -e -C $$contract build; \
46+
done; \
47+
else \
48+
make -e -C contracts/$(CONTRACT) build; \
49+
fi
50+
51+
# Run a single make task for a specific contract. For example:
52+
#
53+
# make run CONTRACT=stack-reorder TASK=adjust_stack_size STACK_SIZE=0x200000
54+
TASK :=
55+
run:
56+
make -e -C contracts/$(CONTRACT) $(TASK)
57+
58+
# test, check, clippy and fmt here are provided for completeness,
59+
# there is nothing wrong invoking cargo directly instead of make.
60+
test:
61+
cargo test $(CARGO_ARGS)
62+
63+
check:
64+
cargo check $(CARGO_ARGS)
65+
66+
clippy:
67+
cargo clippy $(CARGO_ARGS)
68+
69+
fmt:
70+
cargo fmt $(CARGO_ARGS)
71+
72+
# Arbitrary cargo command is supported here. For example:
73+
#
74+
# make cargo CARGO_CMD=expand CARGO_ARGS="--ugly"
75+
#
76+
# Invokes:
77+
# cargo expand --ugly
78+
CARGO_CMD :=
79+
cargo:
80+
cargo $(CARGO_CMD) $(CARGO_ARGS)
81+
82+
clean:
83+
rm -rf build
84+
cargo clean
85+
86+
prepare:
87+
rustup target add riscv64imac-unknown-none-elf
88+
89+
# Generate checksum info for reproducible build
90+
CHECKSUM_FILE := build/checksums-$(MODE).txt
91+
checksum: build
92+
sha256sum build/$(MODE)/* > $(CHECKSUM_FILE)
93+
94+
.PHONY: build test check clippy fmt cargo clean prepare checksum

workspace/scripts/find_clang

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#!/usr/bin/env bash
2+
#
3+
# An utility script used to find a binary of clang 16+
4+
5+
if [[ -n "${CLANG}" ]]; then
6+
echo "${CLANG}"
7+
exit 0
8+
fi
9+
10+
CANDIDATES=("clang" "clang-16" "clang-17")
11+
12+
BREW_PREFIX=$(brew --prefix 2> /dev/null)
13+
if [[ -n "${BREW_PREFIX}" ]]; then
14+
CANDIDATES+=(
15+
"${BREW_PREFIX}/opt/llvm/bin/clang"
16+
"${BREW_PREFIX}/opt/llvm@16/bin/clang"
17+
"${BREW_PREFIX}/opt/llvm@17/bin/clang"
18+
)
19+
fi
20+
21+
for candidate in ${CANDIDATES[@]}; do
22+
OUTPUT=$($candidate -dumpversion 2> /dev/null | cut -d'.' -f 1)
23+
24+
if [[ $((OUTPUT)) -ge 16 ]]; then
25+
echo "$candidate"
26+
exit 0
27+
fi
28+
done
29+
30+
>&2 echo "Cannot find clang of version 16+!"
31+
exit 1

workspace/tests/Cargo.toml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[package]
2+
name = "tests"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
[dependencies]
7+
ckb-testtool = "0.10.1"
8+
serde_json = "1.0"

0 commit comments

Comments
 (0)