forked from skiplang/skip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sk
executable file
·358 lines (299 loc) · 8.49 KB
/
sk
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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
#!/bin/bash
#
# Copyright (c) Facebook, Inc. and its affiliates.
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.
#
installDir=$(dirname "${BASH_SOURCE[0]}")
clangpp=clang++-6.0
preamble="$installDir/../lib/skip/preamble.ll"
standalone="$installDir/../lib/skip/sk_standalone.cpp.o"
prelude="$installDir/../lib/skip/prelude"
thirdPartyLibDir="$installDir/../lib/skip"
linuxGnuLibDir="$installDir/../lib/x86_64-linux-gnu"
skipRuntimeLib="$installDir/../lib/skip/libskip_runtime.a"
skip_server="$installDir/../bin/skip_server"
if [ -z "$1" ]; then
command="run"
root="$PWD"
else
if [ "$1" == "--args" ]; then
command="run"
root="$PWD"
shift
else
command="$1"
shift
if [ -z "$1" ]; then
root="$PWD"
else
if [ "$1" == "--args" ]; then
root="$PWD"
else
root="$1"
fi
shift
fi
fi
fi
root=`realpath "$root"`
if [ "$1" == "--args" ]; then
shift
fi
case $command in
init|start|stop|status|build|check|run|restart)
;;
*)
>&2 echo "Usage:"
>&2 echo "\$ sk (init|start|stop|status|build|check|run|restart) dirname";
exit 1
;;
esac
project="$root/skip.project.json"
skdir_escape_z="${root//z/zZ}"
tmpdir=$(dirname $(mktemp -u))
skdir="$tmpdir/.skserver_${skdir_escape_z//\//z}"
errfile="$skdir/errors"
if ! [ -d "$root" ]; then
>&2 echo "*********************************************"
>&2 echo "ERROR: '$root' is not a valid directory name!"
>&2 echo "*********************************************"
exit 1
fi
if [ $command == "init" ]; then
echo "{" > "$project"
echo " \"skipVersion\": \"1.0\"," >> "$project"
echo " \"programUnits\": {" >> "$project"
echo " \"a.out\": {" >> "$project"
echo " \"kind\": \"Program\"," >> "$project"
echo " \"sources\": [\".\"]," >> "$project"
echo " \"references\": [{ \"path\": \"$prelude\" }]" >> "$project"
echo " }" >> "$project"
echo " }" >> "$project"
echo "}" >> "$project"
exit 0
fi
runAfterBuild=""
if [ $command == "run" ]; then
if [ -f "$skdir/bin.out" ]; then
if [[ $(find "$root" -name "*.sk" -newer "$skdir/bin.out") ]]; then
runAfterBuild=1
command="build"
else
exec "$skdir/bin.out" "$@"
fi
else
runAfterBuild=1
command="build"
fi
fi
if ! [ -f "$project" ]; then
>&2 echo "*********************************************"
>&2 echo "ERROR: $project doesn't exist!"
>&2 echo "*********************************************"
>&2 echo "You can create a default project file with:"
>&2 echo "\$ sk init $root"
exit 7
fi
stop() {
kill -9 `cat "$skdir/pids" 2> /dev/null` 2> /dev/null
rm -Rf "$skdir"
}
if [ $command == "stop" ]; then
stop
echo "Server stopped"
exit 0
fi
start() {
skip_server_pid=`ps xa | grep skip_server | grep "$root" | awk '{ print $1 }'`
if [ "$skip_server_pid" ]; then
>&2 echo "Another server is already running"
exit 2
fi
fifo_command="$skdir/fifo_command"
fifo_response="$skdir/fifo_response"
if ! [ -e "$skdir" ] ; then
mkdir "$skdir"
fi
if ! [ -p "$fifo_command" ]; then
rm -f "$fifo_command"
mkfifo "$fifo_command"
fi
if ! [ -p "$fifo_response" ]; then
rm -f "$fifo_response"
mkfifo "$fifo_response"
fi
nohup sleep 86400 > "$fifo_command" < "$fifo_response" 2> /dev/null &
echo $! > "$skdir/pids"
if ! [ -d "$root" ] ; then
echo "$root is not a directory"
exit 1
fi
# This is a scheme to create a unique temporary file name derived
# from the directory named. We first replace the 'z' with "zZ" and
# then replace the '/' with z. This way, for example, the directory
# /tmp/myDirwithZinIt becomes ztmpzmyDirwithzZinIt.
# The idea is to create a valid filename given an absolute path.
skdir_escape_z="${root//z/zZ}"
tmpdir=$(dirname $(mktemp -u))
skdir="$tmpdir/.skserver_${skdir_escape_z//\//z}"
if ! [ -e "$skdir" ] ; then
>&2 echo "Missing directory $skdir"
exit 2
fi
# We use 2 fifos to talk to the skip_server.
fifo_command="$skdir/fifo_command"
fifo_response="$skdir/fifo_response"
if ! [ -p "$fifo_command" ]; then
>&2 echo "Missing: $fifo_command"
exit 2
fi
if ! [ -p "$fifo_response" ]; then
>&2 echo "Missing: $fifo_response"
exit 2
fi
find "$root" -name "*.sk" > "$skdir/skfiles"
read < "$fifo_response" &
readPid=$!
nohup "$skip_server" "$root" --export-function-as main=skip_main \
--output "$skdir/out.ll" < "$fifo_command" > "$fifo_response" \
2> "$skdir/server_errors" &
echo $! >> "$skdir/pids"
sleep 1
skip_server_pid=`ps xawww | grep skip_server | grep "$root" | awk '{ print $1 }'`
if [ -z "$skip_server_pid" ]; then
>&2 echo "Could not start the server"
cat "$skdir/server_errors" 1>&2
kill -9 `cat "$skdir/pids" 2> /dev/null` 2> /dev/null
exit 9
fi
echo "Server started for: $root"
wait $readPid
echo "Ready"
}
if [ $command == "start" ]; then
start
exit 0
fi
restart() {
stop
start
echo "Server restarted for: $root"
}
# Let's check if the ".sk" files changed, if it's the case,
# restart the server.
if [ -f "$skdir/skfiles" ]; then
find "$root" -name "*.sk" > "$skdir/skfiles_new"
diff -q "$skdir/skfiles" "$skdir/skfiles_new" > /dev/null
if [ $? -ne 0 ]; then
echo "Detected a file configuration change"
restart
fi
fi
if [ $command == "restart" ]; then
restart
exit 0
fi
skip_server_pid=`ps xawww | grep skip_server | grep "$root" | awk '{ print $1 }'`
if [ -z "$skip_server_pid" ]; then
if [ $command == "status" ]; then
echo "No server running"
exit 0
fi
echo "No server running, starting one now!"
start
fi
if [ $command == "status" ]; then
echo "The server is running"
exit 0
fi
read < "$skdir/fifo_response" &
readPid=$!
echo $command > "$skdir/fifo_command"
wait $readPid
if cat "$errfile" | head -n 1 | grep --quiet "No error"; then
if [ $command == "check" ]; then
cat "$errfile";
exit 0
fi
else
cat "$errfile" >&2;
rm -f "$skdir/bin.out"
exit 2
fi
if [ $command == "build" ]; then
rm -f "$skdir/bin.out"
# If built is called before the initialization of the server is over,
# we could be in a situation where the .ll file is not yet ready.
# This can also happen if another process was asking for a "check"
# and so the server was responding to that other process.
# These races are rare enough that we are just going to wait for
# the file to be written.
while [ ! -f "$skdir/out.ll" ];
do
sleep 1
done;
# Adding the preamble to the .ll file
cat "$preamble" "$skdir/out.ll" > "$skdir/preamble_and_out.ll"
system=$(uname -s)
if ! [ -f "$root/.ccopts" ]; then
ccopts=""
else
ccopts=`cat "$root/.ccopts"`
fi
"$clangpp" -O2 -g -c "$skdir/preamble_and_out.ll" -o "$skdir/preamble_and_out.o"
if [ "$system" == "Linux" ]; then
# Linking it all together
"$clangpp" -s -o "$skdir/bin.out" -g \
"$skdir/preamble_and_out.o" "$standalone" \
-L"$thirdPartyLibDir/" \
"$skipRuntimeLib" \
-fPIC -DPIC \
-lrt \
-Wl,--gc-sections \
-DUSE_JEMALLOC -msse4.2 -std=c++14 \
-O3 -fno-omit-frame-pointer -mno-omit-leaf-frame-pointer \
-fdata-sections -ffunction-sections \
-lglog -lgflags \
-licuuc -licui18n -licuio -licutu -licudata -ljemalloc_pic \
-lpcre -lboost_system \
-lboost_filesystem -lboost_chrono -lboost_date_time \
-levent \
-lpthread \
-lunwind \
-lboost_thread \
-lboost_context \
-lboost_atomic \
-ldl \
$ccopts
else
if [ "$system" == "Darwin" ]; then
"$clangpp" -o "$skdir/bin.out" -g \
"$skdir/preamble_and_out.o" "$standalone" \
-L"$thirdPartyLibDir/" \
"$skipRuntimeLib" \
-fPIC -DPIC -Wl,-rpath,"$linuxGnuLibDir" \
-lboost_thread-mt \
-lboost_context-mt \
-lboost_atomic-mt \
-DUSE_JEMALLOC -msse4.2 -std=c++14 \
-O3 -fno-omit-frame-pointer -mno-omit-leaf-frame-pointer \
-fdata-sections -ffunction-sections \
-lglog -lgflags \
-licuuc -licui18n -licuio -licutu -licudata -ljemalloc_pic \
-lpcre -lboost_system \
-lboost_filesystem -lboost_chrono -lboost_date_time \
-levent -ldl \
-lpthread \
$ccopts
else
2>&1 echo "Unknown system"
exit 2
fi
fi
echo "Written: $skdir/bin.out"
fi
if [ $runAfterBuild ]; then
exec "$skdir/bin.out" "$@"
fi