-
Notifications
You must be signed in to change notification settings - Fork 5.9k
/
Copy pathcommon_cmdline.sh
180 lines (159 loc) · 5.8 KB
/
common_cmdline.sh
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
#!/usr/bin/env bash
# ------------------------------------------------------------------------------
# vim:ts=4:et
# This file is part of solidity.
#
# solidity is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# solidity is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with solidity. If not, see <http://www.gnu.org/licenses/>
#
# (c) 2016-2019 solidity contributors.
# ------------------------------------------------------------------------------
YULARGS=(--strict-assembly)
FULLARGS=(--optimize --combined-json "abi,asm,ast,bin,bin-runtime,devdoc,hashes,metadata,opcodes,srcmap,srcmap-runtime,userdoc")
OLDARGS=(--optimize --combined-json "abi,asm,ast,bin,bin-runtime,devdoc,interface,metadata,opcodes,srcmap,srcmap-runtime,userdoc")
function compileFull
{
local expected_exit_code=0
local expect_output='none'
case "$1" in
'--expect-errors')
expected_exit_code=1
expect_output='warnings-or-errors'
shift;
;;
'--expect-warnings')
expect_output='warnings-or-errors'
shift;
;;
'--ignore-warnings')
expect_output='any'
shift;
;;
esac
local args=("${FULLARGS[@]}")
if [[ $1 = '-v' ]]; then
if (echo "$2" | grep -Po '(?<=0.4.)\d+' >/dev/null); then
patch=$(echo "$2" | grep -Po '(?<=0.4.)\d+')
if (( patch < 22 )); then
args=("${OLDARGS[@]}")
fi
fi
shift 2
fi
local files=("$@")
local stderr_path; stderr_path=$(mktemp)
if [ "${files: -4}" == ".yul" ]
then
args=("${YULARGS[@]}")
fi
set +e
"$SOLC" "${args[@]}" "${files[@]}" >/dev/null 2>"$stderr_path"
local exit_code=$?
local errors; errors=$(grep -v -E \
-e 'Warning: This is a pre-release compiler version|Warning: Experimental features are turned on|pragma experimental ABIEncoderV2|^ +--> |^ +\||^[0-9]+ +\| ' \
-e '^No text representation found.$' < "$stderr_path"
)
set -e
rm "$stderr_path"
if [[
$exit_code != "$expected_exit_code" ||
$errors != "" && $expect_output == 'none' ||
$errors == "" && $expect_output != 'none' && $expect_output != 'any' && $expected_exit_code == 0
]]
then
printError "TEST FAILURE"
printError "Actual exit code: $exit_code"
printError "Expected exit code: $expected_exit_code"
printError "==== Output ===="
echo "$errors"
printError "== Output end =="
printError ""
case "$expect_output" in
'none') printError "No output was expected." ;;
'warnings-or-errors') printError "Expected warnings or errors." ;;
esac
printError ""
printError "While calling:"
echo "\"$SOLC\" ${args[*]} ${files[*]}"
printError "Inside directory:"
echo " $(pwd)"
printError "Input was:"
echo "${files[@]}"
false
fi
}
function singleContractOutputViaStandardJSON
{
(( $# == 4 )) || assertFail
local language="$1"
local selected_output="$2"
local extra_settings="$3"
local input_file="$4"
[[ $selected_output != "*" ]] || assertFail
json_output=$(
"$SOLC" --standard-json --allow-paths "$(basename "$input_file")" - <<EOF
{
"language": "${language}",
"sources": {"${input_file}": {"urls": ["${input_file}"]}},
"settings": {
"outputSelection": {"*": { "*": ["${selected_output}"]}},
${extra_settings}
}
}
EOF
)
local error_list output has_contract_level_outputs
error_list=$(echo "$json_output" | jq '.errors[] | select(.severity=="error")')
[[ $error_list == '' ]] || \
fail "Failed to compile ${input_file} via Standard JSON. Errors: ${error_list}"
has_contract_level_outputs=$(echo "$json_output" | jq 'has("contracts")')
[[ $has_contract_level_outputs == true ]] || \
fail "Standard JSON output ${selected_output} was ignored by the compiler."
output=$(echo "$json_output" | jq --raw-output ".contracts[\"${input_file}\"][].${selected_output}")
[[ $output != null ]] || \
fail "Compiler failed to produce the ${selected_output} output when compiling ${input_file} via Standard JSON."
echo "$output"
}
function stripCLIDecorations
{
sed -e '/^=======.*=======$/d' \
-e '/^Binary:$/d' \
-e '/^Binary of the runtime part:$/d' \
-e '/^Opcodes:$/d' \
-e '/^IR:$/d' \
-e '/^Optimized IR:$/d' \
-e '/^EVM assembly:$/d' \
-e '/^Yul Control Flow Graph:$/d' \
-e '/^JSON AST (compact format):$/d' \
-e '/^Function signatures:$/d' \
-e '/^Contract Storage Layout:$/d' \
-e '/^Developer Documentation$/d' \
-e '/^User Documentation$/d' \
-e '/^Contract JSON ABI$/d' \
-e '/^Metadata:$/d' \
-e '/^EVM$/d' \
-e '/^Pretty printed source:$/d' \
-e '/^Text representation:$/d' \
-e '/^Binary representation:$/d'
}
function stripEmptyLines
{
sed -e '/^\s*$/d'
}
# Calculates the total size of bytecode of one or more contracts in bytes.
# Expects the output from `solc --bin` on standard input.
function bytecode_size {
local bytecode_chars
bytecode_chars=$(stripCLIDecorations | stripEmptyLines | wc --chars)
echo $(( bytecode_chars / 2 ))
}