-
Notifications
You must be signed in to change notification settings - Fork 5
/
typeart-wrapper.in
executable file
·310 lines (279 loc) · 6.82 KB
/
typeart-wrapper.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
#!/bin/bash
#
# TypeART library
#
# Copyright (c) 2017-2022 TypeART Authors
# Distributed under the BSD 3-Clause license.
# (See accompanying file LICENSE.txt or copy at
# https://opensource.org/licenses/BSD-3-Clause)
#
# Project home: https://github.com/tudasc/TypeART
#
# SPDX-License-Identifier: BSD-3-Clause
#
function global_init() {
local -r typeart_use_rel_path=@TYPEART_RELOCATABLE@
if [ "$typeart_use_rel_path" == 0 ]; then
local -r typeart_bin_dir="@TYPEART_BINARY_DIR@"
local -r typeart_lib_dir="@TYPEART_RT_DIR@"
local -r typeart_include_dir="@TYPEART_INCLUDE_DIRS@"
local -r typeart_pass="@TYPEART_PASS_DIR@/$<TARGET_FILE_NAME:typeart::TransformPass>"
else
# shellcheck disable=SC2155
local -r typeart_bin_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
# shellcheck disable=SC2155
local -r typeart_install_dir="$(dirname "${typeart_bin_dir}")"
local -r typeart_lib_dir="${typeart_install_dir}/@CMAKE_INSTALL_LIBDIR@"
local -r typeart_include_dir="-I${typeart_install_dir}/@CMAKE_INSTALL_INCLUDEDIR@/@PROJECT_NAME@"
local -r typeart_pass="${typeart_lib_dir}/$<TARGET_FILE_NAME:typeart::TransformPass>"
fi
readonly compiler="@TYPEART_COMPILER@"
readonly opt_tool="@TYPEART_OPT@"
readonly llc_tool="@TYPEART_LLC@"
readonly typeart_includes="${typeart_include_dir}"
readonly typeart_ldflags="-L${typeart_lib_dir}/ \
-Wl,-rpath,${typeart_lib_dir}/ \
-l$<TARGET_FILE_BASE_NAME:typeart::Runtime>"
readonly typeart_san_flags="@TYPEART_SAN_FLAGS@"
# shellcheck disable=SC2027
readonly typeart_plugin="-load "${typeart_pass}" -typeart"
readonly typeart_stack_mode_args="-typeart-heap=false -typeart-stack -typeart-stats @TYPEART_CALLFILTER@"
readonly typeart_heap_mode_args="-typeart-heap=true -typeart-stats"
}
function is_wrapper_disabled() {
case "${TYPEART_WRAPPER}" in
off | OFF | 0 | false | FALSE)
return 1
;;
esac
return 0
}
function is_linking() {
for arg in "$@"; do
case "$arg" in
-c | -S | -E)
return 0
;;
esac
done
return 1
}
function has_source() {
for arg in "$@"; do
local extension_of_arg="${arg##*.}"
case "$extension_of_arg" in
cpp | cxx | cc | c)
return 1
;;
esac
done
return 0
}
function skip_typeart_compile() {
# -E inline header; -M list (all) headers; -MM list file deps
for arg in "$@"; do
case "$arg" in
-E | -M | -MM)
return 1
;;
esac
done
return 0
}
function try_extract_source() {
# $1 == flag (source file); $2 == shift value
local -r extension="${1##*.}"
local -r shift_val="$2"
case "$extension" in
cpp | cxx | cc | c)
source_file="$1"
found_src_file=1
return "$shift_val"
;;
*)
return 1
;;
esac
}
function handle_source_flag() {
if [ -n "$2" ]; then
try_extract_source "$2" 2
else
try_extract_source "$1" 1
fi
return $?
}
function try_extract_object() {
# $1 == flag (obj file); $2 == shift value
local -r extension="${1##*.}"
local -r shift_val="$2"
case "$extension" in
o)
object_file="$1"
found_obj_file=1
return "$shift_val"
;;
*)
return 1
;;
esac
}
function handle_object_flag() {
if [ -n "$2" ]; then
try_extract_object "$2" 2
else
try_extract_object "$1" 1
fi
return $?
}
function handle_binary() {
if [ -n "$2" ]; then
exe_file="$2"
found_exe_file=1
fi
return 2
}
# shellcheck disable=SC2034
function parse_cmd_line() {
found_src_file=0
found_obj_file=0
found_exe_file=0
found_fpic=0
skip_typeart=0
typeart_to_asm=0
exe_file=""
source_file=""
object_file=""
asm_file=""
ta_more_args=""
optimize=""
while (("$#")); do
case "$1" in
-O?)
optimize=$1
shift
;;
-MT)
if [ -n "$2" ]; then
ta_more_args+=" $1 $2"
shift 2
else
ta_more_args+=" $1"
shift 1
fi
;;
-c | -S)
# -S compile to asm
if [ "$1" == "-S" ]; then
typeart_to_asm=1
fi
handle_source_flag "$1" "$2"
shift $?
;;
*.s)
asm_file="$1"
shift 1
;;
*.cpp | *.cxx | *.cc | *.c)
handle_source_flag "$1"
shift $?
;;
-o)
# shellcheck disable=SC2154
if [ "$linking" == 1 ]; then
handle_binary "$1" "$2"
else
handle_object_flag "$1" "$2"
fi
shift $?
;;
*.o)
if [ "$linking" == 0 ]; then
handle_object_flag "$1"
shift $?
else
# when linking, we don't care about object files
ta_more_args="$ta_more_args $1"
shift
fi
;;
-fPIC)
# llc requires special flag
found_fpic=1
ta_more_args="$ta_more_args $1"
shift
;;
*) # preserve other arguments
ta_more_args="$ta_more_args $1"
shift
;;
esac
done
# set other positional arguments in their proper place
eval set -- "ta_more_args"
if [ -z "${optimize}" ]; then
optimize=-O0
fi
}
function main_link() {
# shellcheck disable=SC2086 disable=SC2068
$compiler ${typeart_includes} ${typeart_ldflags} ${typeart_san_flags} $@
}
function main_compile() {
if [ -z "${object_file}" ]; then
# if no object file is specified, use filename(source_file).o
object_file="${source_file%.*}".o
fi
if [ "$typeart_to_asm" == 1 ] && [ -z "${asm_file}" ]; then
# if no asm file is specified, use filename(source_file).s
local asm_file="${source_file%.*}".s
fi
local out_file="${object_file}"
local llc_flags="--filetype=obj"
if [ "$typeart_to_asm" == 1 ]; then
local llc_flags="--filetype=asm"
local out_file="${asm_file}"
fi
if [ "$found_fpic" == 1 ]; then
local llc_flags="$llc_flags --relocation-model=pic"
fi
# shellcheck disable=SC2086
$compiler ${ta_more_args} ${typeart_includes} ${typeart_san_flags} \
-O1 -Xclang -disable-llvm-passes -S -emit-llvm "${source_file}" -o - |
$opt_tool ${typeart_plugin} ${typeart_heap_mode_args} |
$opt_tool ${optimize} -S |
$opt_tool ${typeart_plugin} ${typeart_stack_mode_args} |
$llc_tool -x=ir ${llc_flags} -o "${out_file}"
}
function main_in() {
global_init
is_wrapper_disabled
readonly typeart_disabled=$?
skip_typeart_compile "$@"
if [ "$?" == 1 ] || [ "$typeart_disabled" == 1 ]; then
# shellcheck disable=SC2068
$compiler $@
return 0
fi
is_linking "$@"
local -r linking=$?
has_source "$@"
local -r with_source=$?
if [ "$linking" == 1 ] && [ "$with_source" == 1 ]; then
parse_cmd_line "$@"
main_compile "$@"
if [ "$found_exe_file" == 1 ]; then
ta_more_args+=" -o ${exe_file}"
fi
main_link "$ta_more_args" "${object_file}"
if [ -f "${object_file}" ]; then
rm "${object_file}"
fi
elif [ "$linking" == 1 ]; then
main_link "$@"
else
parse_cmd_line "$@"
main_compile "$@"
fi
}
main_in "$@"