/
eosiocpp.in
executable file
·204 lines (181 loc) · 7.08 KB
/
eosiocpp.in
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#!/bin/bash
EOSIO_BIN_INSTALL_DIR=`dirname $0`
if [ "${EOSIO_BIN_INSTALL_DIR}" == "." ]; then
EOSIO_BIN_INSTALL_DIR=`pwd`
fi
EOSIO_INSTALL_DIR=`dirname ${EOSIO_BIN_INSTALL_DIR}`
if [ -x "@CMAKE_BINARY_DIR@/programs/eosio-abigen/eosio-abigen" ]; then
ABIGEN="@CMAKE_BINARY_DIR@/programs/eosio-abigen/eosio-abigen"
elif [ -x "${EOSIO_INSTALL_DIR}/bin/eosio-abigen" ]; then
ABIGEN=${EOSIO_INSTALL_DIR}/bin/eosio-abigen
fi
if [ -x "@CMAKE_BINARY_DIR@/externals/binaryen/bin/eosio-s2wasm" ]; then
EOSIO_S2WASM="@CMAKE_BINARY_DIR@/externals/binaryen/bin/eosio-s2wasm"
elif [ -x "${EOSIO_INSTALL_DIR}/bin/eosio-s2wasm" ]; then
EOSIO_S2WASM="${EOSIO_INSTALL_DIR}/bin/eosio-s2wasm"
else
echo "eosio-s2wasm not found either built or installed"
exit 12
fi
if [ -x "@CMAKE_BINARY_DIR@/libraries/wasm-jit/Source/Programs/eosio-wast2wasm" ]; then
EOSIO_WAST2WASM="@CMAKE_BINARY_DIR@/libraries/wasm-jit/Source/Programs/eosio-wast2wasm"
elif [ -x "${EOSIO_INSTALL_DIR}/bin/eosio-wast2wasm" ]; then
EOSIO_WAST2WASM="${EOSIO_INSTALL_DIR}/bin/eosio-wast2wasm"
else
echo "eosio-wast2wasm not found either built or installed"
exit 14
fi
BOOST_INCLUDE_DIR=@Boost_INCLUDE_DIR@
function copy_skeleton {
set -e
cp -r "${EOSIO_INSTALL_DIR}/share/eosio/skeleton/." $newname
for file in $(find ./$newname -name 'skeleton.*')
do
newfile=`echo $file | sed 's/skeleton\./'"$newname"'./'`
# echo mv "${file}" "${newfile}"
mv "${file}" "${newfile}"
exp=s/skeleton/${newname}/g
# echo sed -i ${exp} ${newfile}
sed ${exp} ${newfile} > ${newfile}1
mv ${newfile}1 ${newfile}
done
echo "created $newname from skeleton"
set +e
}
function build_contract {
set -e
workdir=`mktemp -d`
if [[ ${VERBOSE} == "1" ]]; then
PRINT_CMDS="set -x"
fi
($PRINT_CMDS; mkdir $workdir/built)
for file in $@; do
name=`basename $file`
filePath=`dirname $file`
($PRINT_CMDS; @WASM_CLANG@ -emit-llvm -O3 --std=c++14 --target=wasm32 -nostdinc \
-DBOOST_DISABLE_ASSERTS -DBOOST_EXCEPTION_DISABLE \
-nostdlib -nostdlibinc -ffreestanding -nostdlib \
-fno-threadsafe-statics -fno-rtti -fno-exceptions \
-I @CMAKE_BINARY_DIR@/contracts \
-I @CMAKE_SOURCE_DIR@/contracts \
-I @CMAKE_SOURCE_DIR@/contracts/libc++/upstream/include \
-I @CMAKE_SOURCE_DIR@/contracts/musl/upstream/include \
-I @CMAKE_SOURCE_DIR@/externals/magic_get/include \
-I ${EOSIO_INSTALL_DIR}/include \
-I${EOSIO_INSTALL_DIR}/include/libc++/upstream/include \
-I${EOSIO_INSTALL_DIR}/include/musl/upstream/include \
-I${BOOST_INCLUDE_DIR} \
-I $filePath \
${EOSIOCPP_CFLAGS} \
-c $file -o $workdir/built/$name
)
done
declare -a possible_libs=("@CMAKE_BINARY_DIR@/contracts/eosiolib/eosiolib.bc"
"@CMAKE_BINARY_DIR@/contracts/libc++/libc++.bc"
"@CMAKE_BINARY_DIR@/contracts/musl/libc.bc"
"${EOSIO_INSTALL_DIR}/usr/share/eosio/contractsdk/lib/eosiolib.bc"
"${EOSIO_INSTALL_DIR}/usr/share/eosio/contractsdk/lib/libc++.bc"
"${EOSIO_INSTALL_DIR}/usr/share/eosio/contractsdk/lib/libc.bc")
declare libs=""
for lib in "${possible_libs[@]}"; do
if [ -f "${lib}" ]; then
libs="${libs} ${lib}"
fi
done
($PRINT_CMDS; @WASM_LLVM_LINK@ -only-needed -o $workdir/linked.bc $workdir/built/* ${libs})
($PRINT_CMDS; @WASM_LLC@ -thread-model=single --asm-verbose=false -o $workdir/assembly.s $workdir/linked.bc)
($PRINT_CMDS; ${EOSIO_S2WASM} -o $outname -s 16384 $workdir/assembly.s)
($PRINT_CMDS; ${EOSIO_WAST2WASM} $outname ${outname%.*}.wasm -n)
($PRINT_CMDS; rm -rf $workdir)
set +e
}
function generate_abi {
if [[ ! -e "$1" ]]; then
echo "You must specify a file"
exit 1
fi
context_folder=$(cd "$(dirname "$1")" ; pwd -P)
${ABIGEN} -extra-arg=-c -extra-arg=--std=c++14 -extra-arg=--target=wasm32 \
-extra-arg=-nostdinc -extra-arg=-nostdinc++ -extra-arg=-DABIGEN \
-extra-arg=-I@CMAKE_SOURCE_DIR@/contracts \
-extra-arg=-I@CMAKE_BINARY_DIR@/contracts \
-extra-arg=-I@CMAKE_SOURCE_DIR@/contracts/libc++/upstream/include \
-extra-arg=-I@CMAKE_SOURCE_DIR@/contracts/musl/upstream/include \
-extra-arg=-I@CMAKE_SOURCE_DIR@/externals/magic_get/include \
-extra-arg=-I${EOSIO_INSTALL_DIR}/include/libc++/upstream/include \
-extra-arg=-I${EOSIO_INSTALL_DIR}/include/musl/upstream/include \
-extra-arg=-I${BOOST_INCLUDE_DIR} \
-extra-arg=${EOSIOCPP_CFLAGS} \
-extra-arg=-I${EOSIO_INSTALL_DIR}/include -extra-arg=-I$context_folder \
-extra-arg=-fparse-all-comments -destination-file=${outname} -verbose=0 \
-context=$context_folder $1 --
if [ "$?" -ne 0 ]; then
exit 1
fi
echo "Generated ${outname} ..."
}
function print_help {
echo "Usage: $0 -o output.wast contract.cpp [other.cpp ...]"
echo " OR"
echo " $0 -n mycontract"
echo " OR"
echo " $0 -g contract.abi types.hpp"
echo
echo "Options:"
echo " -n | --newcontract [name]"
echo " Create a new contract in the [name] folder, based on the example contract"
echo " OR"
echo " -o | --outname [output.wast] [input.cpp ...]"
echo " Generate the wast output file based on input cpp files"
echo " The wasm output will also be created as output.wasm"
echo " OR"
echo " -g | --genabi contract.abi types.hpp"
echo " Generate the ABI specification file [EXPERIMENTAL]"
}
function print_deprecation_notice {
echo -e "\033[0;33mWARNING: this tool is deprecated and will be removed in a future release\033[0m" 1>&2
echo -e "\033[0;33mPlease consider using the EOSIO.CDT (https://github.com/EOSIO/eosio.cdt/)\033[0m" 1>&2
}
command=""
while [[ $# -gt 1 ]]
do
key="$1"
case $key in
-h|--help)
print_help
break;
;;
-n|--newcontract)
newname="$2"
command="newcontract"
shift 2
break
;;
-o|--outname)
outname="$2"
command="outname"
shift 2
break
;;
-g|--genabi)
outname="$2"
command="genabi"
shift 2
;;
*)
echo "Unrecognized option: $1"
exit 1
;;
esac
done
print_deprecation_notice
if [[ "outname" == "$command" ]]; then
build_contract $@
elif [[ "newcontract" == "$command" ]]; then
copy_skeleton
elif [[ "genabi" == "$command" ]]; then
generate_abi $@
else
print_help
exit 1
fi