Skip to content

Commit 1d6d331

Browse files
committed
Add atomics-contract template
1 parent 53ce068 commit 1d6d331

File tree

5 files changed

+143
-0
lines changed

5 files changed

+143
-0
lines changed

atomics-contract/.gitignore

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

atomics-contract/Cargo.toml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[package]
2+
name = "{{project-name}}"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
[dependencies]
7+
ckb-std = "0.15.0"
8+
log = { version = "0.4.20", default-features = false }
9+
10+
[build-dependencies]
11+
cc = "1.0"

atomics-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,-a $(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

atomics-contract/build.rs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
fn main() {
2+
let top = match std::env::var("TOP") {
3+
Ok(top) => top,
4+
Err(_) => "../..".to_string(),
5+
};
6+
7+
println!("cargo:rerun-if-changed={}/deps/lib-dummy-atomics/atomics.c", top);
8+
9+
let target_arch = std::env::var("CARGO_CFG_TARGET_ARCH").unwrap();
10+
if target_arch == "riscv64" {
11+
let mut build = cc::Build::new();
12+
assert!(
13+
build.get_compiler().is_like_clang(),
14+
"Clang must be used as the compiler!"
15+
);
16+
build
17+
.file(format!("{}/deps/lib-dummy-atomics/atomics.c", top))
18+
.static_flag(true)
19+
.include(format!("{}/deps/ckb-c-stdlib", top))
20+
.include(format!("{}/deps/ckb-c-stdlib/libc", top))
21+
.no_default_flags(true)
22+
.flag("--target=riscv64")
23+
.flag("-march=rv64imc_zba_zbb_zbc_zbs")
24+
.flag("-O3")
25+
.flag("-nostdinc")
26+
.flag("-nostdlib")
27+
.flag("-fvisibility=hidden")
28+
.flag("-fdata-sections")
29+
.flag("-ffunction-sections")
30+
.flag("-Wall")
31+
.flag("-Werror")
32+
.flag("-Wno-unused-parameter")
33+
.define("CKB_DECLARATION_ONLY", None)
34+
.compile("dummy-atomics");
35+
}
36+
}

atomics-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+
log::error!("Just a log line");
16+
17+
0
18+
}

0 commit comments

Comments
 (0)