Skip to content

Commit 8e782b4

Browse files
committed
Refactore generate native-simulator
1 parent 7833ead commit 8e782b4

File tree

4 files changed

+32
-113
lines changed

4 files changed

+32
-113
lines changed

native-simulator/Cargo.toml

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,3 @@ ckb-std = { version = "0.15.1", features = ["simulator"] }
1010

1111
[lib]
1212
crate-type = ["cdylib"]
13-
14-
[workspace]
15-
16-
# TODO: Will be deleted after release
17-
[replace]
18-
"ckb-std:0.15.3" = { git = "https://github.com/nervosnetwork/ckb-std.git", branch = "master" }

native-simulator/GlobalMakefile

Lines changed: 0 additions & 28 deletions
This file was deleted.

workspace/Makefile

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,20 @@ build:
4848
$(MAKE) -e -C contracts/$(CONTRACT) build; \
4949
fi
5050

51+
build-simulator:
52+
@set -eu;\
53+
if [ ! -d native-simulators ] || [ -z "$$(ls -A native-simulators)" ]; then \
54+
echo "There is no native-simulator to compile."; \
55+
echo "If necessary, please use:"; \
56+
echo "make generate-native-simulator CRATE=<existing contract>"; \
57+
else \
58+
mkdir -p $(BUILD_DIR); \
59+
for sim in $(wildcard native-simulators/*); do \
60+
cd $$sim && cargo build && cd ../..; \
61+
done; \
62+
cp target/debug/*_sim.* $(BUILD_DIR); \
63+
fi
64+
5165
# Run a single make task for a specific contract. For example:
5266
#
5367
# make run CONTRACT=stack-reorder TASK=adjust_stack_size STACK_SIZE=0x200000
@@ -114,11 +128,25 @@ generate:
114128

115129
generate-native-simulator:
116130
@set -eu; \
117-
if [ "x$(CRATE)" = "x" ]; then \
118-
scripts/generate-native-simulator _ $(TEMPLATE_TYPE) $(TEMPLATE_REPO); \
131+
cargo generate $(TEMPLATE_TYPE) $(TEMPLATE_REPO) native-simulator \
132+
-n $(CRATE)-sim \
133+
--destination native-simulators \
134+
-d contract_name=$(CRATE) \
135+
-d contract_crate_name=`echo "$(CRATE)" | tr '-' '_'`; \
136+
mv native-simulators/$(CRATE)-sim/src/contract-lib.rs contracts/$(CRATE)/src/lib.rs; \
137+
FILE=contracts/$(CRATE)/Cargo.toml; \
138+
if grep -q "\\[features\\]" "$$FILE"; then \
139+
sed -i '/\[features\]/a\\simulator = \[\"ckb-std/simulator\"\]' $$FILE; \
119140
else \
120-
scripts/generate-native-simulator $(CRATE) $(TEMPLATE_TYPE) $(TEMPLATE_REPO); \
121-
fi
141+
echo "\\n[features]\\nsimulator = [\"ckb-std/simulator\"]\\n" >> $$FILE ; \
142+
fi; \
143+
FILE=contracts/$(CRATE)/src/main.rs; \
144+
sed -i 's/#!\[no_std\]/#!\[cfg_attr(not(feature = "simulator"), no_std)\]/' $$FILE; \
145+
sed -i 's/#!\[cfg_attr(not(test), no_main)\]/#!\[cfg_attr(not(any(feature = "simulator", test)), no_main)\]/' $$FILE; \
146+
sed -i 's/#\[cfg(test)\]/#\[cfg(any(feature = "simulator", test))\]/' $$FILE; \
147+
sed -i 's/#\[cfg(not(test))\]/#\[cfg(not(any(feature = "simulator", test)))\]/' $$FILE; \
148+
sed '/@@INSERTION_POINT@@/s/$$/\n "native-simulators\/$(CRATE)-sim",/' Cargo.toml > Cargo.toml.new; \
149+
mv Cargo.toml.new Cargo.toml;
122150

123151
prepare:
124152
rustup target add riscv64imac-unknown-none-elf

workspace/scripts/generate-native-simulator

Lines changed: 0 additions & 75 deletions
This file was deleted.

0 commit comments

Comments
 (0)