11#! /bin/bash
22set -e
33
4-
54# Color codes
65RED=' \033[0;31m'
76GREEN=' \033[0;32m'
@@ -10,17 +9,21 @@ NC='\033[0m' # No Color
109
1110# Usage/help message
1211if [[ " $1 " == " -h" || " $1 " == " --help" ]]; then
13- echo -e " ${YELLOW} Usage: bash scripts/build.sh [contract_name]${NC} "
12+ echo -e " ${YELLOW} Usage: bash scripts/build.sh [contract_name] [--production] ${NC} "
1413 echo " If contract_name is provided, only that contract will be built and optimized."
14+ echo " Use --production to use the production profile (max optimization, slower build)."
1515 exit 0
1616fi
1717
18- # Build all contracts
19- if [ -n " $1 " ]; then
20- echo -e " ${YELLOW} Building contract: $1 ${NC} "
21- else
22- echo -e " ${YELLOW} Building all contracts...${NC} "
23- fi
18+ PROFILE=" release"
19+ SPECIFIC_CONTRACT=" "
20+ for arg in " $@ " ; do
21+ if [ " $arg " == " --production" ]; then
22+ PROFILE=" production"
23+ elif [ -z " $SPECIFIC_CONTRACT " ]; then
24+ SPECIFIC_CONTRACT=" $arg "
25+ fi
26+ done
2427
2528# Check for required tools
2629if ! command -v cargo & > /dev/null; then
@@ -31,89 +34,99 @@ if ! command -v soroban &> /dev/null; then
3134 echo -e " ${RED} Error: 'soroban' CLI not found. Please install Soroban CLI: https://soroban.stellar.org/docs/getting-started/installation${NC} "
3235 exit 1
3336fi
34- if ! command -v wasm-opt & > /dev/null; then
35- echo -e " ${YELLOW} Warning: 'wasm-opt' not found. Fallback optimization will not be available. Install Binaryen: https://github.com/WebAssembly/binaryen${NC} "
36- fi
3737
3838# Print environment info
3939echo -e " ${YELLOW} Environment Info:${NC} "
4040echo -n " Rust: " && rustc --version || echo " Not installed"
4141echo -n " Cargo: " && cargo --version || echo " Not installed"
4242echo -n " Soroban: " && soroban --version || echo " Not installed"
43- echo -n " wasm-opt: " && wasm-opt --version || echo " Not installed"
44- # Create target directory if it doesn't exist
4543
46- mkdir -p target/wasm32-unknown-unknown/release
44+ # Dependency Caching (sccache)
45+ if command -v sccache & > /dev/null; then
46+ export RUSTC_WRAPPER=sccache
47+ echo -e " ${GREEN} sccache detected and enabled for faster recompilation.${NC} "
48+ fi
49+
50+ # Create target directory if it doesn't exist
51+ mkdir -p target/wasm32-unknown-unknown/$PROFILE
4752
4853# Logging setup
4954LOGFILE=" build.log"
5055echo " --- Build started at $( date) ---" > " $LOGFILE "
5156
52- # Build each contract
53- echo " Build completed successfully!"
57+ START_TOTAL=$( date +%s)
5458
59+ # Build contracts
60+ if [ -n " $SPECIFIC_CONTRACT " ]; then
61+ echo -e " ${YELLOW} Building contract: $SPECIFIC_CONTRACT (Profile: $PROFILE )...${NC} "
62+ cargo build --target wasm32-unknown-unknown --profile " $PROFILE " -p " $SPECIFIC_CONTRACT " 2>&1 | tee -a " $LOGFILE "
63+ else
64+ echo -e " ${YELLOW} Building all contracts in workspace (Profile: $PROFILE )...${NC} "
65+ # Exclude non-contract workspace members if necessary, but cargo build --workspace --target wasm32-unknown-unknown
66+ # will only build CDYLIB crates for that target anyway.
67+ cargo build --workspace --target wasm32-unknown-unknown --profile " $PROFILE " 2>&1 | tee -a " $LOGFILE "
68+ fi
69+
70+ BUILD_END=$( date +%s)
71+ echo -e " ${GREEN} Cargo build completed in $(( BUILD_END - START_TOTAL)) s${NC} "
72+
73+ # Optimize WASM files in parallel
74+ echo -e " ${YELLOW} Optimizing WASM files in parallel...${NC} "
75+ OPTIMIZE_START=$( date +%s)
5576
5677success_contracts=()
5778failed_contracts=()
5879
59- if [ -n " $1 " ]; then
60- contracts=(" contracts/$1 /" )
61- else
62- contracts=(contracts/* /)
63- fi
64-
65- for contract in " ${contracts[@]} " ; do
66- contract_name=$( basename " $contract " )
67- wasm_basename=" ${contract_name// -/ _} "
68- wasm_path=" target/wasm32-unknown-unknown/release/$contract_name .wasm"
69- if [ ! -f " $wasm_path " ]; then
70- wasm_path=" target/wasm32-unknown-unknown/release/$wasm_basename .wasm"
71- fi
72- optimized_wasm_path=" target/wasm32-unknown-unknown/release/$wasm_basename .optimized.wasm"
73- echo -e " ${YELLOW} Building $contract_name contract...${NC} "
74- start_time=$( date +%s)
75-
76- if cargo build --target wasm32-unknown-unknown --release -p " $contract_name " 2>&1 | tee -a " $LOGFILE " ; then
77- if [ -f " $wasm_path " ]; then
78- echo -e " ${YELLOW} Optimizing $( basename " $wasm_path " ) ...${NC} "
79- if soroban contract optimize --wasm " $wasm_path " --wasm-out " $optimized_wasm_path " 2>&1 | tee -a " $LOGFILE " ; then
80- echo -e " ${GREEN} Optimization succeeded for $contract_name ${NC} "
81- success_contracts+=(" $contract_name " )
82- else
83- echo -e " ${YELLOW} Warning: 'soroban contract optimize' failed for $contract_name . Attempting fallback with 'wasm-opt -Oz'...${NC} "
84- if command -v wasm-opt & > /dev/null; then
85- if wasm-opt -Oz " $wasm_path " -o " $optimized_wasm_path " 2>&1 | tee -a " $LOGFILE " ; then
86- echo -e " ${GREEN} Fallback optimization succeeded for $contract_name ${NC} "
87- success_contracts+=(" $contract_name " )
88- else
89- echo -e " ${RED} Error: Both 'soroban contract optimize' and 'wasm-opt -Oz' failed for $contract_name . Please check your WASM file and tool installations.${NC} "
90- failed_contracts+=(" $contract_name " )
91- fi
92- else
93- echo -e " ${RED} Error: 'wasm-opt' not found. Please install Binaryen (https://github.com/WebAssembly/binaryen) for fallback optimization.${NC} "
94- failed_contracts+=(" $contract_name " )
95- fi
80+ # Function to optimize a single contract
81+ optimize_contract () {
82+ local wasm_path=$1
83+ local contract_name=$( basename " $wasm_path " .wasm)
84+ local optimized_wasm_path=" target/wasm32-unknown-unknown/$PROFILE /$contract_name .optimized.wasm"
85+
86+ echo -e " ${YELLOW} Optimizing $contract_name ...${NC} "
87+ if soroban contract optimize --wasm " $wasm_path " --wasm-out " $optimized_wasm_path " >> " $LOGFILE " 2>&1 ; then
88+ echo -e " ${GREEN} ✓ $contract_name optimized${NC} "
89+ return 0
90+ else
91+ # Fallback to wasm-opt if available
92+ if command -v wasm-opt & > /dev/null; then
93+ if wasm-opt -Oz " $wasm_path " -o " $optimized_wasm_path " >> " $LOGFILE " 2>&1 ; then
94+ echo -e " ${GREEN} ✓ $contract_name optimized (via wasm-opt)${NC} "
95+ return 0
9696 fi
97- else
98- echo -e " ${RED} Error: WASM file not found for $contract_name after build.${NC} "
99- failed_contracts+=(" $contract_name " )
10097 fi
101- else
102- echo -e " ${RED} Error: Build failed for $contract_name ${NC} "
103- failed_contracts+=(" $contract_name " )
98+ echo -e " ${RED} ✗ $contract_name optimization failed${NC} "
99+ return 1
100+ fi
101+ }
102+
103+ # Find all generated WASM files (excluding already optimized ones)
104+ wasm_files=$( find target/wasm32-unknown-unknown/$PROFILE -maxdepth 1 -name " *.wasm" ! -name " *.optimized.wasm" )
105+
106+ # Run optimizations in parallel
107+ pids=()
108+ for wasm in $wasm_files ; do
109+ optimize_contract " $wasm " &
110+ pids+=($! )
111+ done
112+
113+ # Wait for all background jobs
114+ exit_code=0
115+ for pid in " ${pids[@]} " ; do
116+ if ! wait " $pid " ; then
117+ exit_code=1
104118 fi
105- end_time=$( date +%s)
106- duration=$(( end_time - start_time))
107- echo -e " ${YELLOW} Time taken for $contract_name : ${duration} s${NC} "
108119done
109120
110- # Print summary
111- echo -e " \n ${GREEN} Build completed! ${NC} "
112- if [ ${ # success_contracts[@]} -gt 0 ] ; then
113- echo -e " ${GREEN} Contracts built and optimized successfully: ${NC} ${success_contracts[*] }"
114- fi
115- if [ ${ # failed_contracts[@]} -gt 0 ] ; then
116- echo -e " ${RED} Contracts with errors: ${NC} ${failed_contracts[*]} "
117- echo " See $LOGFILE for details."
121+ OPTIMIZE_END= $( date +%s )
122+ TOTAL_END= $( date +%s )
123+
124+ echo -e " \n ${GREEN} Optimization completed in $(( OPTIMIZE_END - OPTIMIZE_START )) s ${NC }"
125+ echo -e " ${GREEN} Total time: $(( TOTAL_END - START_TOTAL )) s ${NC} "
126+
127+ if [ $exit_code -ne 0 ] ; then
128+ echo -e " ${RED} Some optimizations failed. See $LOGFILE for details.${NC} "
118129 exit 4
119130fi
131+
132+ echo -e " ${GREEN} Build and optimization successful!${NC} "
0 commit comments