@@ -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
115129generate-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
123151prepare :
124152 rustup target add riscv64imac-unknown-none-elf
0 commit comments