@@ -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
122136generate-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
130158prepare :
131159 rustup target add riscv64imac-unknown-none-elf
0 commit comments