-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathhook
More file actions
executable file
·699 lines (657 loc) · 17.3 KB
/
Copy pathhook
File metadata and controls
executable file
·699 lines (657 loc) · 17.3 KB
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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
#!/usr/bin/env bash
# Compiler command emulator for a C Analyzer
# Author: Jaeho Shin <netj@ropas.snu.ac.kr>
# Created: 2004-11-05
Cmd=$(basename "$0")
# don't use our hook commands (in the same directory) from here on
rmPATH() {
PATH=${PATH/:$1:/:}
PATH=${PATH/#$1:/}
PATH=${PATH/:$1%/}
}
rmPATH "$(cd "$(dirname "$0")" && pwd)" "$(dirname "$0")"
# first, do the real job
"$Cmd" "$@" || exit $?
# and continue when successful
# stop here if ./configure is in progress
${SMAKECONFIGURE:-true} && exit 0
[ -d "$SMAKEDIR" ] && exec >>"$SMAKEDIR/.smake.out" 2>&1
## auxiliary pieces
# escape strings so it can be safely used in shell commands
esc() {
local a
for a in "$@"; do
a=${a//"'"/"'\\''"}
# XXX: wrap with ' only if $a contains unsafe chars
echo -n "'$a' "
done
}
# `add a 1 2 3 ...` means add 1, 2, 3, ... to array a
add() {
local a=$1
shift
eval $a'=("${'$a'[@]}" "$@")'
}
# check existence of option
hasopt() {
local opt="-$1"
shift
local found=false
local o
for o in "$@"; do
case "$o" in
$opt)
found=true
break
;;
esac
done
$found
}
# vocabularies for option handling
# assuming args, o, j are used
opt() {
local t addoptarg=false
case "$1" in
-*)
if [ "$o" = "$1" ]; then
# -o output
let j++
OPTARG=${args[$j]}
addoptarg=true
else
# -ooutput
OPTARG=${o#$1}
fi
shift
;;
esac
if $addoptarg; then
for t in "$@"; do
add $t "$o" "$OPTARG"
done
else
for t in "$@"; do
add $t "$o"
done
fi
}
# relative path of given path from base given (or $SMAKEROOT)
# XXX: Only paths that begins with $base will work. Not sure if we really
# need to find out the exact relative path with "../"s prepended.. I
# guess doing so will make files leak outside $SMAKEDIR.
relpath() {
local p=$1
local base=${2:-$SMAKEROOT}
case "$p" in
/*) ;;
*) p="$PWD/$p" ;;
esac
local d=$(dirname "$p")
local rd=$(cd "$d" 2>/dev/null && pwd)
p="${rd:-$d}/$(basename "$p")"
p=${p#$base}
echo "${p#/}"
}
# relpath -> abspath
realpath() {
case "$1" in
/*) echo "$1" ;;
*) echo "$PWD/$1" ;;
esac
}
# create parent directories of given paths
# so that we can create them directly (e.g. by redirection)
mkpath() {
local p
for p in "$@"; do
mkdir -p $(dirname "$p")
done
}
# look for lib*.{a,so} under each $@
findlib() {
local lp pfx ext l=$1
shift
for lp in "$@"; do
pfx="$lp/lib$l"
for ext in so a; do
lib="$pfx.$ext"
if [ -f "$lib" ]; then
echo "$lib"
return
fi
done
done
}
## naming conventions for tracking
# path of tag for tracking source
tagpath() {
local o=$1
echo "$SMAKEDIR/.tags/$(relpath "$o")"
}
# find path of corresponding source of object
srcpath() {
local o=$1
readlink "$(tagpath "$o")"
}
# suffix for source in preprocessed form
srcext() {
case "$1" in
c-header) echo .h ;;
c | cpp-output) echo .i ;;
c++ | c++-cpp-output) echo .ii ;;
objective-c | objc-cpp-output) echo .mi ;;
f77 | f77-cpp-input | ratfor) echo .f ;;
assembler | assembler-with-cpp) echo .s ;;
ada) echo .ads ;;
java) echo .java ;;
treelang) echo .treelang ;;
esac
}
# detect language of file based on its suffix
srctype() {
case "${1##*.}" in
c) echo c ;;
i) echo cpp-output ;;
ii) echo c++-cpp-output ;;
m) echo objective-c ;;
mi) echo objc-cpp-output ;;
h) echo c-header ;;
cc | cp | cxx | cpp | c++ | C) echo c++ ;;
f | for | FOR) echo f77-cpp-output ;;
F | fpp | FPP) echo f77 ;;
r) echo ratfor ;;
ads | adb) echo ada ;;
S) echo assembler ;;
s) echo assembler-with-cpp ;;
# java) echo java ;;
# treelang) echo treelang ;;
*) echo none ;;
esac
}
# create a srcpath for the given object of some type and tag it
mksrc() {
local o=$1 t=$2
local s="$SMAKEDIR/$(relpath "$o")$(srcext "$t")"
mkpath "$s"
echo "$s"
# create a tag
local tg=$(tagpath "$o")
mkpath "$tg"
ln -sf "$s" "$tg"
}
# remove source and tag if exists
rmsrc() {
local o=$1
local done=false
local tg=$(tagpath "$o")
if [ -L "$tg" ]; then
if [ -e "$tg" ]; then
rm -rf "$(readlink "$tg")"
fi
unlink "$tg"
done=true
fi
update_target "$o"
$done
}
# update the existence of target
update_target() {
local tgt=$(relpath "$1")
local files=$(find "$(srcpath "$1")" -type f 2>/dev/null | wc -l)
(
cd "$SMAKEDIR"
if [ ${files:-0} -gt 0 ]; then
{
cat .targets 2>/dev/null
echo "$tgt"
} | sort | uniq >.targets.tmp &&
mv -f .targets.tmp .targets
else
# remove from target list if no files exist
grep -v "^$tgt$" .targets >.targets.tmp &&
mv -f .targets.tmp .targets
fi
)
}
## logging
if [ -d "$SMAKEDIR" ]; then
output() {
echo "$@"
log "$@"
}
log() { echo "$@" >>"$SMAKEDIR/.smake.log"; }
else
output() { :; }
log() { echo "$@" >&2; }
fi
notfound() {
log "# XXX: $@ not found"
}
unsupported() {
log "# XXX: $@ not supported"
}
unknown() {
log "# XXX: $@ unknown"
}
## act like gcc
_cc() {
local output= link=true noop=false ext= lang=none
local args cpp cc as ld lpath objs
args=(_cc "$@") cpp=() cc=() as=() ld=() lpath=() objs=()
# parse options and files
local o j
for ((j = 1; $j <= $#; j++)); do
o=${!j}
[ -n "$o" ] || continue
case "$o" in
# Overall options
-c) link=false ext=.o ;;
-S) link=false ext=.s ;;
-E) link=false noop=true ;;
-v | -pipe | -pass-exit-codes) opt cc ;;
'--###' | --help | --target-help | \
-version | --version)
opt cc
noop=true
;;
-x*)
opt -x cpp cc
lang=$OPTARG
;;
-o*)
opt -o
output=$OPTARG
;;
# Directory
-B*) opt -B cc ;;
-I*) opt -I cpp ;;
-L*)
opt -L ld
add lpath "$OPTARG"
;;
-YP,*)
opt -YP, ld
add lpath "$OPTARG"
;; # SYSV
-specs=*) opt cc ;;
# Preprocessor
-A*) opt -A cpp ;;
-D*) opt -D cpp ;;
-U*) opt -U cpp ;;
-idirafter*) opt -idirafter cpp ;;
-include*) opt -include cpp ;;
-imacros*) opt -imacros cpp ;;
-iprefix*) opt -iprefix cpp ;;
-iwithprefix*) opt -iwithprefix cpp ;;
-iwithprefixbefore*) opt -iwithprefixbefore cpp ;;
-isystem*) opt -isystem cpp ;;
-M | -MM)
noop=true
opt cpp
;;
# we need no dependency files (*.d)
-MF*) opt -MF ;; # cpp ;;
-MT*) opt -MT ;; # cpp ;;
-MQ*) opt -MQ ;; # cpp ;;
-M[DGP] | -MMD) opt ;; # cpp ;;
-$ | -C | -CC | -H | -d[DIMN] | -nostdinc | -nostdinc++ | \
-P | -remap | -trigraphs | -undef | -Wp,*) opt cpp ;;
# C options
-ansi | -std=* | -traditional | -traditional-cpp) opt cpp cc ;;
-aux-info*) opt -aux-info cc ;;
# Assembler
-Wa,*) opt as ;;
# Linker
-nostartfiles | -nodefaultlibs | -nostdlib | -s | \
-static | -static-libgcc | -shared | -shared-libgcc | \
-symbolic | -Wl,*) opt ld ;;
-Xlinker*) opt -Xlinker ld ;;
-u*) opt -u ld ;;
-l*)
opt -l ld
add objs "none:-l$OPTARG"
;;
# feature, warning
-f* | -no-* | -pedantic | -pedantic-errors | -w | -W*) opt cc ;;
# Debugging
-d* | -dump* | -p | -g* | -pg | -print-* | -Q | -save-temps | -time) opt cc ;;
# Optimization
-O) opt cc ;;
-O*) opt -O cc ;;
--param*) opt --param cc ;;
# Target
-V*) opt -V cc ;;
-b*) opt -b cc ;;
# Machine dependent
-m* | -pthread | -EL | -EB | -threads | -nolibdld | \
-Q[yn] | -sim | -sim2) opt cc ;;
-G*) opt -G cc ;;
-Ym,*) opt -Ym, cc ;;
# unknown option
-*)
unknown "cc: $o: option"
opt cc
;;
# objects or sources
*)
case "$lang" in
# detect lang if none
none) add objs "$(srctype "$o"):$o" ;;
*) add objs "$lang:$o" ;;
esac
;;
esac
done
# have we got any object or source given?
[ ${#objs[@]} -gt 0 ] || noop=true
# isn't the output of this command useless?
[ "$output" = /dev/null ] && noop=true
# stop here if nothing to do
$noop && return
copy() {
ln -f -- "$1" "$2"
}
generate() {
# preprocess and save source
local o=$1 # input
local s=$2 # srcpath
local f=${o#*:} # input file (path relative to $PWD)
local t=${o%%:*} # its language
# skip null inputs (might wanted to test the installation)
[ "$f" = /dev/null ] && return
# TODO: Can we make $f relative to $SMAKEROOT?
# Since preprocessed files may contain ambiguous path..
# E.g. gcc -E a/x.c and gcc -E b/x.c is OK, but
# cd a; gcc -E x.c and cd b; gcc -E x.c will
# generate ambiguous line info. However, doing this isn't easy
# since we must deal all path related options. :(
case "$t" in
c-header) ;;
*cpp-output) cp -f "$f" "$s" ;;
c | c++)
rm -f "$s"
"$CC" -E "${cpp[@]}" "${cc[@]}" $(realpath "$f") -o "$s"
log "# cc: $s: preprocessed $f"
;;
*)
unsupported "$s: $f: source type"
;;
esac
}
if $link; then
output=${output:-a.out}
# gather sources that are being linked
log "# cc: $output: linking $(esc "${objs[@]}")"
local sout=$(mksrc "$output" target)
rm -rf "$sout"
mkpath "$sout/."
# object number handling
objnumfmt() {
local os=$(printf %x ${1:-0})
local ondigits=${#os}
[ $ondigits -lt 1 ] && ondigits=1
let ondigits++
echo "%0${ondigits}x"
}
local on=0
local onfmt=$(objnumfmt "${#objs[@]}")
numbering() {
local what=$1
shift
local numbered="$sout/$(printf $onfmt $on).$(basename "$what")"
let on++
"$@" "$numbered"
}
local o f t
for o in "${objs[@]}"; do
f=${o#*:} # path to actual file
t=${o%%:*} # its language
case "$t" in
none) # an object
case "$f" in
-l*)
# find library
local l=${f#-l}
f=$(findlib "$l" "${lpath[@]}")
if [ -f "$f" ]; then
log "# cc: -l$l: found at $f"
else
notfound "$output: -l$l: library"
continue
fi
;;
esac
# gather sources of objects
local s=$(srcpath "$f")
if [ -d "$s" ]; then # archive or relocatable (executable?)
local sl
for sl in "$s"/*; do
numbering "$sl" copy "$sl"
done
elif [ -f "$s" ]; then # single object
numbering "$s" copy "$s"
else
notfound "$output: $f: source"
fi
;;
*) # a source
# generate on-the-fly-object's source
numbering "$f$(srcext "$t")" generate "$o"
;;
esac
done
# register as target
update_target "$output"
else
# generate source for each object
local o f t s out
for o in "${objs[@]}"; do
f=${o#*:} # path to actual file
t=${o%%:*} # its language
s=$(srcpath "$f") # source of $f
out=${output:-$(basename "${f%.*}$ext")}
if [ -f "$s" ]; then
# if there is a source for $f already, then just copy it
# e.g. generating "a.s" by `gcc -S a.c`, and then
# "a.o" by `gcc -c a.s`: "a.o" needs to keep track of "a.c".
copy "$s" "$(mksrc "$out" $(srctype "$s"))"
else
generate "$o" "$(mksrc "$out" "$t")"
fi
done
fi
}
## act like assembler
_as() {
local args asms noop=false output=a.out
args=(_as "$@") asms=()
# gather options and files
local o j
for ((j = 1; $j <= $#; j++)); do
o=${!j}
[ -n "$o" ] || continue
case "$o" in
# TODO: sophisticated option detecting
-o*)
opt -o
output=$OPTARG
;;
*) [ -f "$o" ] && opt asms || unsupported "as: $o: option" ;;
esac
done
# have we got any asm given?
[ ${#asms[@]} -gt 0 ] || noop=true
# stop here if nothing to do
$noop && return
# if we have all source of asms
local all=true f s ss
ss=()
for f in "${asms[@]}"; do
s=$(srcpath "$f")
if ! [ -f "$s" ]; then
all=false
break
fi
add ss "$s"
done
if $all; then
# place them as a single source
local sout=$(mksrc "$output" $(srctype "${#ss[0]}"))
# XXX: assuming types of sources of asms are all the same
if [ ${#ss[@]} -eq 1 ]; then
ln -f -- "${ss[@]}" "$sout"
else
cat "${ss[@]}" >"$sout"
fi
log "# as: $sout: assembled ${ss[@]}"
else
notfound "as: $output: complete sources of asm"
fi
}
## act like linker
_ld() {
local args ccargs noop=false
args=(_as "$@") ccargs=()
# gather meaningful arguments for _cc
local o j
for ((j = 1; $j <= $#; j++)); do
o=${!j}
[ -n "$o" ] || continue
case "$o" in
-o*) opt -o ccargs ;;
-l*) opt -l ccargs ;;
-L*) opt -L ccargs ;;
-Y*)
opt -Y
o="-L$OPTARG"
opt ccargs
;;
-Tbss*) opt -Tbss ;;
-Ttext*) opt -Ttext ;;
-Tdata*) opt -Tdata ;;
-T*) opt -T ;; # ld script
# TODO: better handling of these multi-flavors of same option
-script=*) opt -script= ;;
-script*) opt -script ;;
--script=*) opt --script= ;;
--script*) opt --script ;;
# TODO: sophisticated option detecting
*) [ -f "$o" ] && opt ccargs || unsupported "ld: $o: option" ;;
esac
done
# stop here if nothing to do
$noop && return
# use _cc to handle the rest
_cc "${ccargs[@]}"
}
_ar() {
# maintain a list of sources that constitute the archive
has() { [ "$1" != "${1/$2//}" ]; }
if has $1 r || hasopt r "$@"; then
# XXX: Currently, only "ar ..r.. ..." style commands
# (i.e. replace members) are supported. :(
local a=$2
shift 2
local sout=$(mksrc "$a" archive)
mkpath "$sout/."
local o
for o in "$@"; do
local s=$(srcpath "$o")
if [ -d "$s" ]; then
# archiving another archive or relocatable? impossible!
# XXX: NO! actually one may archive an object which was linked
# incrementally :( TODO: copy $s/*. might need numbering,
# which will require checking of replacements.
log "# XXX: $a: not archiving $o"
elif [ -f "$s" ]; then
ln -f -- "$s" "$sout"
log "# ar: $a: adding $o"
else
notfound "$a: $o: source"
fi
done
update_target "$a"
else
unsupported "ar: mode $1"
fi
}
# record hooked command
output "cd $(esc "$PWD")"
output "$(esc $Cmd "$@")"
## command detection
case "$Cmd" in
# capture the rules for compiling, assemblying and linking
*cc | *g++) CC="gcc" _cc "$@" ;;
*clang*) CC="clang" _cc "$@" ;;
wllvm) CC="wllvm" _cc "$@" ;;
*as) _as "$@" ;;
*ld) _ld "$@" ;;
*ar) _ar "$@" ;;
cp | ln | mv | rm)
# gather options and arguments
opts=() files=()
endofopts=false
for o in "$@"; do
if $endofopts; then
files=("${files[@]}" "$o")
else
case "$o" in
--) endofopts=true ;;
# TODO: skip useless (e.g. -i) options?
-*) opts=("${opts[@]}" "$o") ;;
*) files=("${files[@]}" "$o") ;;
esac
fi
done
# bookkeeping by applying the same command to corresponding sources
case "$Cmd" in
rm) # remove (for all)
for f in "${files[@]}"; do
rmsrc "$f" && log "# bookkeep: rm $t $dest"
done
;;
*) # moving/copying around (pair-wise with last)
dest="${files[$((${#files[@]} - 1))]}"
# for each pair among files and destination
for src in "${files[@]}"; do
# $src and $dst are the actual files,
# while $s and $d are the corresponding sources
# TODO: what about .targets? consider this:
# gcc -o a.out <some files>; mv a.out exe1
# gcc -o a.out <more files>; mv a.out exe2
# does exe1, exe2 get listed?
dst=$dest
[ "$src" = "$dst" ] && continue
# (append its implied filename if $dst is directory)
[ -d "$dst" ] && dst="$dst/$(basename "$src")"
# we don't need the previous source of $dst
rmsrc "$dst"
# without the source of $src beyond this point is meaningless
s=$(srcpath "$src")
[ -e "$s" -o -L "$s" ] || continue
# handle some special cases
moreopts=(-f)
case "$Cmd" in
ln)
# prevent hard-linking a directory
[ -d "$s" ] && moreopts=("${moreopts[@]}" -s)
;;
cp)
# copy recursively if source is a directory
[ -d "$s" ] && moreopts=("${moreopts[@]}" -r)
;;
esac
# prepare a new source for $dst (same type as $s)
d=$(mksrc "$dst" $(srctype "$s"))
mkpath "$d"
# finally, apply the same command to the source pair
$Cmd "${moreopts[@]}" "${opts[@]}" -- "$s" "$d"
log "# bookkeep: $Cmd $s $d"
done
;;
esac
;;
esac
# don't disturb the make process
true