Skip to content

Commit c58c16b

Browse files
committed
Refactore generate native-simulator
1 parent 662b3cc commit c58c16b

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
@@ -55,6 +55,20 @@ build:
5555
$(MAKE) -e -C contracts/$(CONTRACT) build; \
5656
fi
5757

58+
build-simulator:
59+
@set -eu;\
60+
if [ ! -d native-simulators ] || [ -z "$$(ls -A native-simulators)" ]; then \
61+
echo "There is no native-simulator to compile."; \
62+
echo "If necessary, please use:"; \
63+
echo "make generate-native-simulator CRATE=<existing contract>"; \
64+
else \
65+
mkdir -p $(BUILD_DIR); \
66+
for sim in $(wildcard native-simulators/*); do \
67+
cd $$sim && cargo build && cd ../..; \
68+
done; \
69+
cp target/debug/*_sim.* $(BUILD_DIR); \
70+
fi
71+
5872
# Run a single make task for a specific contract. For example:
5973
#
6074
# make run CONTRACT=stack-reorder TASK=adjust_stack_size STACK_SIZE=0x200000
@@ -121,11 +135,25 @@ generate:
121135

122136
generate-native-simulator:
123137
@set -eu; \
124-
if [ "x$(CRATE)" = "x" ]; then \
125-
scripts/generate-native-simulator _ $(TEMPLATE_TYPE) $(TEMPLATE_REPO); \
138+
cargo generate $(TEMPLATE_TYPE) $(TEMPLATE_REPO) native-simulator \
139+
-n $(CRATE)-sim \
140+
--destination native-simulators \
141+
-d contract_name=$(CRATE) \
142+
-d contract_crate_name=`echo "$(CRATE)" | tr '-' '_'`; \
143+
mv native-simulators/$(CRATE)-sim/src/contract-lib.rs contracts/$(CRATE)/src/lib.rs; \
144+
FILE=contracts/$(CRATE)/Cargo.toml; \
145+
if grep -q "\\[features\\]" "$$FILE"; then \
146+
sed -i '/\[features\]/a\\simulator = \[\"ckb-std/simulator\"\]' $$FILE; \
126147
else \
127-
scripts/generate-native-simulator $(CRATE) $(TEMPLATE_TYPE) $(TEMPLATE_REPO); \
128-
fi
148+
echo "\\n[features]\\nsimulator = [\"ckb-std/simulator\"]\\n" >> $$FILE ; \
149+
fi; \
150+
FILE=contracts/$(CRATE)/src/main.rs; \
151+
sed -i 's/#!\[no_std\]/#!\[cfg_attr(not(feature = "simulator"), no_std)\]/' $$FILE; \
152+
sed -i 's/#!\[cfg_attr(not(test), no_main)\]/#!\[cfg_attr(not(any(feature = "simulator", test)), no_main)\]/' $$FILE; \
153+
sed -i 's/#\[cfg(test)\]/#\[cfg(any(feature = "simulator", test))\]/' $$FILE; \
154+
sed -i 's/#\[cfg(not(test))\]/#\[cfg(not(any(feature = "simulator", test)))\]/' $$FILE; \
155+
sed '/@@INSERTION_POINT@@/s/$$/\n "native-simulators\/$(CRATE)-sim",/' Cargo.toml > Cargo.toml.new; \
156+
mv Cargo.toml.new Cargo.toml;
129157

130158
prepare:
131159
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)