-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlib4bin
executable file
·860 lines (815 loc) · 48 KB
/
lib4bin
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
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
#!/usr/bin/env bash
# deps: apt update && apt install bash file binutils patchelf findutils grep sed coreutils strace -y
# deps: apk add bash file binutils patchelf findutils grep sed coreutils strace
# deps: dnf install bash file binutils patchelf findutils grep sed coreutils strace -y
# deps: pacman -Sy bash file binutils patchelf findutils grep sed coreutils strace --noconfirm
# deps: xbps-install -Sy bash file binutils patchelf findutils grep sed coreutils strace
RED='\033[1;91m'
BLUE='\033[1;94m'
GREEN='\033[1;92m'
YELLOW='\033[1;33m'
RESETCOLOR='\033[1;00m'
ONE_DIR=${ONE_DIR:=1}
DST_DIR="${DST_DIR:=.}"
CREATE_LINKS=${CREATE_LINKS:=1}
STRIP=${STRIP:=0}
VERBOSE=${VERBOSE:=0}
LIBS_ONLY=${LIBS_ONLY:=0}
QUIET_MODE=${QUIET_MODE:=0}
HARD_LINKS=${HARD_LINKS:=0}
WITH_HOOKS=${WITH_HOOKS:=0}
WITH_SHARUN=${WITH_SHARUN:=0}
WITH_WRAPPE=${WITH_WRAPPE:=0}
WRAPPE_CLVL=${WRAPPE_CLVL:=8}
PATCH_RPATH=${PATCH_RPATH:=0}
STRACE_MODE=${STRACE_MODE:=0}
STRACE_TIME=${STRACE_TIME:=5}
GEN_LIB_PATH=${GEN_LIB_PATH:=0}
ANY_EXECUTABLE=${ANY_EXECUTABLE:=0}
WRAPPE_CLEANUP=${WRAPPE_CLEANUP:=1}
PATCH_INTERPRETER=${PATCH_INTERPRETER:=0}
# BINARY_LIST=('/path/executable' '/path/executable')
GIT_SHARUN_RELEASES='https://github.com/VHSgunzo/sharun/releases'
GIT_WRAPPE_RELEASES='https://github.com/VHSgunzo/wrappe/releases'
usage() {
echo -e "[ Usage ]: lib4bin [OPTIONS] /path/executable -- [STRACE MODE EXEC ARGS]
[ Options ]:
-d, --dst-dir '/path' Destination directory (env: DST_DIR=/path)
-e, --strace-mode Use strace for get libs (env: STRACE_MODE=1)
-t, --strace-time 5 Specifies the time in seconds for strace mode (env: STRACE_TIME=5)
-g, --gen-lib-path Generate a lib.path file (env: GEN_LIB_PATH=1)
-h, --help Show this message
-i, --patch-interpreter Patch INTERPRETER to a relative path (env: PATCH_INTERPRETER=1)
-k, --with-hooks Pack additional files required for libraries (env: WITH_HOOKS=1)
-l, --libs-only Pack only libraries without executables (env: LIBS_ONLY=1)
-n, --not-one-dir Separate directories for each executable (env: ONE_DIR=0)
-p, --hard-links Pack sharun and create hard links (env: HARD_LINKS=1)
-q, --quiet-mode Show only errors (env: QUIET_MODE=1)
-r, --patch-rpath Patch RPATH to a relative path (env: PATCH_RPATH=1)
-s, --strip Strip binaries and libraries (env: STRIP=1)
-v, --verbose Verbose mode (env: VERBOSE=1)
-w, --with-sharun Pack sharun from PATH or env or download
(env: WITH_SHARUN=1, SHARUN=/path|URL, SHARUN_URL=URL, UPX_SHARUN=1)
-o, --with-wrappe Pack with wrappe from PATH or env or download
(env: WITH_WRAPPE=1, WRAPPE=/path|URL, WRAPPE_URL=URL)
-c, --wrappe-clvl 0-22 Specify the compression level for wrappe (env: WRAPPE_CLVL=0-22) (default: 8)
-x, --wrappe-exec name Specify the name of the wrappe packaged executable (env: WRAPPE_EXEC=name)
-m, --wrappe-args 'args' Specify the args for the wrappe packaged executable (env: WRAPPE_ARGS='args')
-u, --wrappe-no-cleanup Disable cleanup the wrappe unpack directory after exit (env: WRAPPE_CLEANUP=0)
It can also be set at runtime (env: STARTPE_CLEANUP=0)"
exit 1
}
error_msg() {
echo -e "${RED}[ ERROR ][$(date +"%Y.%m.%d %T")]: $@ $RESETCOLOR" 1>&2
return 1
}
info_msg() {
if [ "$QUIET_MODE" != 1 ]
then echo -e "${GREEN}[ INFO ][$(date +"%Y.%m.%d %T")]: $@ $RESETCOLOR" 1>&2
fi
}
skip_msg() {
if [ "$QUIET_MODE" != 1 ]
then echo -e "${YELLOW}[ SKIPPED ][$(date +"%Y.%m.%d %T")]: $@ $RESETCOLOR" 1>&2
fi
}
which_exe() { command -v "$@" ; }
is_exe_exist() { which_exe "$@" &>/dev/null ; }
check_deps() {
local ret=0
local binaries=(file patchelf find grep sed)
[ "$STRIP" != 1 ]||binaries+=(strip)
[ "$STRACE_MODE" != 1 ]||binaries+=(strace)
for bin in "${binaries[@]}"
do
if ! is_exe_exist $bin
then
error_msg "$BLUE[$bin]$YELLOW not found!"
ret=1
fi
done
if [ "$ret" != 0 ]
then
info_msg "You need to install ${BLUE}lib4bin${GREEN} dependencies: ${BLUE}file binutils patchelf findutils grep sed coreutils strace"
exit 1
fi
}
try_strip() {
if [ "$STRIP" == 1 ]
then
info_msg "$YELLOW[ STRIP ]: $BLUE[$1]"
strip -s -R .comment --strip-unneeded "$1"||exit 1
fi
}
try_set_rpath() {
if [ "$PATCH_RPATH" == 1 ]
then
info_msg "$YELLOW[ SET RPATH ]: $BLUE[$1]"
patchelf $pvarg --set-rpath '$ORIGIN/../lib:$ORIGIN/../lib32' "$1"||exit 1
else
local rpath="$(print_rpath "$1")"
if grep -qE '^/|:/'<<<"$rpath"
then
info_msg "$YELLOW[ REMOVE RPATH ]: $BLUE[$rpath]"
patchelf $pvarg --remove-rpath "$1"||exit 1
fi
fi
}
print_needed() { patchelf --print-needed "$1" 2>/dev/null ; }
print_rpath() { patchelf --print-rpath "$1" 2>/dev/null ; }
ldd_libs() {
ldd "$1" 2>/dev/null|grep -v 'error while loading shared libraries'|\
grep '/lib'|cut -d'>' -f2|sed 's| (.*)||g'|sed 's|^[[:space:]]*||g'
}
get_libs() {
unset libs libs4libs
if [ "$STRACE_MODE" == 1 ] && ! is_so
then
local libs_file="/tmp/libs.$$"
[[ "$(strace --help)" =~ always-show-pid ]] && \
STRACE_ARGS='--always-show-pid'||unset STRACE_ARGS
strace -f -e trace=openat,open $STRACE_ARGS -o "$libs_file" "$1" "${STRACE_CMD_ARGS[@]}" &>/dev/null &
sleep $STRACE_TIME
local pids="$(cut -d ' ' -f1<"$libs_file"|sort -u)"
kill $pids 2>/dev/null
local libs="$(echo -e "$(sed '/nvidia/d;/libcuda/d;/ENOENT/d;/unfinished/d'<"$libs_file"|\
grep -oP '".*lib.*\.so.*"'|sed -u 's|"||g')")\n"
rm -f "$libs_file"
fi
[ -n "$2" ] && local needed_libs="$2"||\
local needed_libs="$(print_needed "$1")"
local libs+="$(([ -z "$needed_libs" ]||\
grep -E "$(tr '\n' '|'<<<"$needed_libs"|sed 's|\||$\||g')libpthread.so.0$"<<<"$ALL_LIBS" ; \
ldd_libs "$1")|sort -u)"
[ -n "$IS_ELF32" ] && \
local libs="$(grep -vE '/lib/|/lib64/|/.*64-linux-gnu/'<<<"$libs")"||\
local libs="$(grep -vE '/lib32/|/i386-linux-gnu/|/arm-linux-gnu/'<<<"$libs")"
for lib in $libs
do local libs4libs="$(echo -e "$(ldd_libs "$lib")\n$libs4libs")"
done
echo -e "$libs\n$libs4libs"|sort -u|sed '/^$/d'
}
repath_needed_libs() {
[ -n "$2" ] && local needed_libs="$2"||\
local needed_libs="$(print_needed "$1")"
local patch_needed_libs="$(grep '^/'<<<"$needed_libs")"
if [ -n "$patch_needed_libs" ]
then
for lib in $patch_needed_libs
do
local relib="$(basename "$lib")"
info_msg "$YELLOW[ REPATH ]: $BLUE[$lib -> $relib]"
patchelf $pvarg --replace-needed "$lib" "$relib" "$1"||exit 1
done
fi
}
try_mkdir() {
if [ ! -d "$1" ]
then mkdir $varg -p "$1"||exit 1
fi
}
try_ln() {
if [ ! -L "$2" ]
then
[ "$VERBOSE" == 1 ] && \
echo -n "ln: "
ln $varg -sf "$1" "$2"||exit 1
fi
}
try_cp_exe() {
if [ ! -f "$2" ]
then
[ "$VERBOSE" == 1 ] && \
echo -n "cp: "
cp $varg -f "$1" "$2"||exit 1
chmod $varg 755 "$2"||exit 1
fi
}
try_cp() {
[ "$VERBOSE" == 1 ] && \
echo -n "cp: "
cp $varg -rf "$@"||exit 1
}
try_cd() {
[ "$VERBOSE" == 1 ] && \
echo "cd: '$1'"
cd "$1"||exit 1
}
find_so() { find "$@" -name '*.so' -o -name '*.so.*' 2>/dev/null ; }
is_so() { [ -n "$IS_SO" ] && [[ "${binary_name,,}" =~ .*(\.so$|\.so\..*) || "${binary_real_name,,}" =~ .*(\.so$|\.so\..*) ]] ; }
check_url_stat_code() {
set -o pipefail
if is_exe_exist curl
then curl -sL -o /dev/null -I -w "%{http_code}" "$@" 2>/dev/null
elif is_exe_exist wget
then wget --no-check-certificate --server-response \
--spider "$@"|& awk '/^ HTTP/{print$2}'|tail -1
else return 1
fi
}
get_sharun_git_url() {
echo "${GIT_SHARUN_RELEASES}/download/$(NO_ARIA2C=1 try_dl "${GIT_SHARUN_RELEASES}/latest" \
/dev/stdout 2>/dev/null|grep -m1 "Release v.*"|awk '{print$2}')/sharun-$(uname -m)$([ "$UPX_SHARUN" != 1 ]||echo -upx)"
}
get_wrappe_git_url() {
echo "${GIT_WRAPPE_RELEASES}/download/$(NO_ARIA2C=1 try_dl "${GIT_WRAPPE_RELEASES}/latest" \
/dev/stdout 2>/dev/null|grep -m1 "Release v.*"|awk '{print$2}')/wrappe-$(uname -m)"
}
is_url() {
[ ! -n "$1" ] && \
return 1
if [ -n "$2" ]
then [ "$(check_url_stat_code "$1")" == "$2" ]
else [ "$(check_url_stat_code "$1")" == "200" ]
fi
}
is_net_conn() {
if is_exe_exist nc
then nc -zw1 github.com 443 &>/dev/null
elif is_exe_exist curl
then curl -Ifs github.com &>/dev/null
elif is_exe_exist wget
then wget -q --spider github.com &>/dev/null
elif is_exe_exist ping
then ping -c 2 github.com &>/dev/null
else return 1
fi
}
try_dl() {
if is_net_conn
then
if [ -n "$1" ]
then
URL="$1"
if [ -n "$2" ]
then
if [ -d "$2" ]
then
FILEDIR="$2"
FILENAME="$(basename "$1")"
else
FILEDIR="$(dirname "$2")"
FILENAME="$(basename "$2")"
fi
else
FILEDIR="."
FILENAME="$(basename "$1")"
fi
if is_url "$URL"
then
WGET_ARGS=(-q --no-check-certificate -t 3 -T 5 -w 0.5 "$URL" -O "$FILEDIR/$FILENAME")
try_mkdir "$FILEDIR"
if [ "$NO_ARIA2C" != 1 ] && is_exe_exist aria2c
then
aria2c --no-conf -R -x 13 -s 13 --allow-overwrite -d "$FILEDIR" -o "$FILENAME" "$URL"
elif is_exe_exist curl
then
curl -R --progress-bar --insecure --fail -L "$URL" -o "$FILEDIR/$FILENAME"
elif is_exe_exist wget2
then
wget2 --force-progress "${WGET_ARGS[@]}"
elif is_exe_exist wget
then
wget --show-progress "${WGET_ARGS[@]}"
else
error_msg "Downloader not found!"
fi
else
error_msg "$FILENAME not found in $(echo "$URL"|awk -F/ '{print$3"/"$4}')"
fi
else
error_msg "Specify download URL!"
fi
else
error_msg "There is no internet connection!"
fi
return $?
}
parse_arg_options() {
[ "$1" == 'dash' ] && shift && \
local _ALLOW_DASH=1||unset _ALLOW_DASH
if [ -n "$3" ] && [[ "$_ALLOW_DASH" == 1 || "$3" != -* ]]
then eval "${1}='$3'"
else
error_msg "${YELLOW}Option ${BLUE}$2 ${YELLOW}requires a non-empty argument!\n"
usage
fi
}
while [[ "$#" -gt 0 ]]; do
case $1 in
-h|--help) usage ;;
-s|--strip) STRIP=1; shift ;;
-v|--verbose) VERBOSE=1; shift ;;
-n|--not-one-dir) ONE_DIR=0; shift ;;
-l|--libs-only) LIBS_ONLY=1; shift ;;
-k|--with-hooks) WITH_HOOKS=1; shift ;;
-q|--quiet-mode) QUIET_MODE=1; shift ;;
-p|--hard-links) HARD_LINKS=1; shift ;;
-e|--strace-mode) STRACE_MODE=1; shift ;;
-w|--with-sharun) WITH_SHARUN=1; shift ;;
-o|--with-wrappe) WITH_WRAPPE=1; shift ;;
-r|--patch-rpath) PATCH_RPATH=1; shift ;;
-g|--gen-lib-path) GEN_LIB_PATH=1; shift ;;
-u|--wrappe-no-cleanup) WRAPPE_CLEANUP=0; shift ;;
-i|--patch-interpreter) PATCH_INTERPRETER=1; shift ;;
-d|--dst-dir) parse_arg_options DST_DIR "$1" "$2"; shift 2 ;;
-t|--strace-time) parse_arg_options STRACE_TIME "$1" "$2"; shift 2 ;;
-x|--wrappe-exec) parse_arg_options WRAPPE_EXEC "$1" "$2"; shift 2 ;;
-c|--wrappe-clvl) parse_arg_options WRAPPE_CLVL "$1" "$2"; shift 2 ;;
-m|--wrappe-args) parse_arg_options dash WRAPPE_ARGS "$1" "$2"; shift 2 ;;
-*) error_msg "Unknown parameter: ${BLUE}$1\n"; usage ;;
*) break ;;
esac
done
[[ "$STRACE_TIME" =~ ^[0-9]+$ ]]||\
STRACE_TIME=5
[[ -n "$WRAPPE_EXEC" || -n "$WRAPPE_ARGS" ]] && \
WITH_WRAPPE=1
(( WRAPPE_CLVL >= 0 && WRAPPE_CLVL <= 22 ))||\
WRAPPE_CLVL=8
[ "$WITH_WRAPPE" == 1 ] && \
WITH_SHARUN=1
if [ "$2" == '--' ]
then
STRACE_MODE=1
BINARY_LIST=("$1"); shift 2
STRACE_CMD_ARGS=("$@")
fi
check_deps
if [ "$VERBOSE" == 1 ]
then
varg='-v'
pvarg='--debug'
else
unset varg pvarg
fi
if [ ! -n "$BINARY_LIST" ]
then
if [ -n "$1" ]
then
BINARY_LIST=("$@")
else
error_msg "Specify the executable or shared object!\n"
usage
fi
fi
ALL_LIBS="$(find_so \
/usr/lib /usr/libexec /usr/lib64 \
/usr/lib32 /lib /lib64 /lib32 \
|sort -u \
)"
binary_number=1
declare -A DST_DIRS
declare -A BINARIES
declare -A LIBRARIES
for binary in "${BINARY_LIST[@]}"
do
unset binary_real_name
if [ -L "$binary" ]
then
binary_src_pth="$(readlink -f "$binary")"
binary_real_name="$(basename "$binary_src_pth")"
else
binary_src_pth="$binary"
fi
if [[ "${BINARIES["$binary"]}" != 1 ]]
then
binary_name="$(basename "$binary")"
if [ "$ONE_DIR" == 1 ]
then
[ "$WITH_WRAPPE" == 1 ] && \
dst_dir="$DST_DIR/wrappe-$$"||\
dst_dir="$DST_DIR"
dst_dir_pth="${dst_dir}/shared"
sharun_bin_dir_pth="${dst_dir}/bin"
else
[ "$WITH_WRAPPE" == 1 ] && \
dst_dir="${DST_DIR}/${binary_name}/wrappe-$$"||\
dst_dir="${DST_DIR}/${binary_name}"
dst_dir_pth="${dst_dir}/shared"
sharun_bin_dir_pth="${dst_dir}/bin"
fi
[[ -f "$dst_dir_pth" || -L "$dst_dir_pth" ]] && \
dst_dir_pth="${dst_dir_pth}.dir"
DST_DIRS["$dst_dir_pth"]=
bin_dir_pth="${dst_dir_pth}/bin"
FILE_INFO="$(file "$binary_src_pth" 2>/dev/null)"
IS_ELF="$(grep -o 'ELF'<<<"$FILE_INFO")"
IS_STATIC="$(grep -o 'static'<<<"$FILE_INFO")"
IS_SCRIPT="$(grep -o 'script'<<<"$FILE_INFO")"
IS_ELF32="$(grep -q 'ELF 32-bit'<<<"$FILE_INFO")"
IS_SO="$(grep -o 'shared object'<<<"$FILE_INFO")"
IS_EXECUTABLE="$(grep -o 'executable'<<<"$FILE_INFO")"
IS_EXECUTABLE="${IS_EXECUTABLE:=$(([ -n "$IS_SO" ] && ! is_so) && echo executable)}"
info_msg "$YELLOW[ $binary_number ]: $BLUE[$binary_name] ${GREEN}..."
if [ "$HARD_LINKS" == 1 ] && [[ -n "$IS_SCRIPT" || -n "$IS_STATIC" ]]
then
hard_links=0
with_sharun=1
fi
hard_links=${HARD_LINKS:=0}
with_sharun=${WITH_SHARUN:=0}
if ([ -n "$IS_EXECUTABLE" ] || is_so)
then
needed_libs="$(print_needed "$binary_src_pth")"
LIBS="$(get_libs "$binary_src_pth" "$needed_libs")"
is_so && LIBS="$(echo -e "$LIBS\n$binary"|sort -u|sed '/^$/d')"
if [[ -n "$LIBS" && ! -n "$IS_SCRIPT" && ! -n "$IS_STATIC" ]]
then
INTERPRETER="$(basename "$(grep 'ld-linux'<<<"$LIBS"|cut -d'=' -f1|sed 's|\t||' )")"
[[ "$CREATE_LINKS" == 1 && "$hard_links" == 1 && ! -x "${dst_dir}/sharun" ]] && \
with_sharun=1
else
bin_dir_pth="$sharun_bin_dir_pth"
fi
if [ "$LIBS_ONLY" != 1 ] && ! is_so
then
if [[ "$with_sharun" == 1 && ! -x "${dst_dir}/sharun" ]]
then
TMP_SHARUN="/tmp/sharun-$(uname -m)$([ "$UPX_SHARUN" != 1 ]||echo -upx)"
SHARUN="${SHARUN:="$(readlink -f "$(which_exe sharun)")"}"
SHARUN="${SHARUN:="$TMP_SHARUN"}"
if [ ! -x "$SHARUN" ]
then
if [[ "${SHARUN,,}" =~ ^http ]]
then
SHARUN_URL="$SHARUN"
SHARUN="$TMP_SHARUN"
fi
SHARUN_URL="${SHARUN_URL:=$(get_sharun_git_url)}"
info_msg "Downloading sharun -> '$SHARUN'..."
info_msg "$SHARUN_URL"
if try_dl "$SHARUN_URL" "$SHARUN"
then chmod $varg +x "$SHARUN"
else
error_msg "Failed to download sharun!"
exit 1
fi
fi
if [ -x "$SHARUN" ]
then
try_mkdir "$dst_dir"
try_cp_exe "$SHARUN" "${dst_dir}/sharun"
else
error_msg "sharun not found!"
exit 1
fi
fi
try_mkdir "$bin_dir_pth"
[ -n "$binary_real_name" ] && \
binary_dst_pth="$bin_dir_pth/$binary_real_name"||\
binary_dst_pth="$bin_dir_pth/$binary_name"
try_cp_exe "$binary_src_pth" "$binary_dst_pth"
if [ -n "$IS_SCRIPT" ]
then
for intep in python bash sh perl ruby go
do
if grep -qo "^#!.*/bin/$intep" "$binary_dst_pth"
then
sed -i "1s|^#!.*/bin/$intep|#!/usr/bin/env $intep|" "$binary_dst_pth"
break
fi
done
fi
if [[ -n "$binary_real_name" && "$binary_name" != "$binary_real_name" ]]
then
(try_cd "$bin_dir_pth"
try_ln "$binary_real_name" "$binary_name")||exit 1
fi
if [[ "${BINARIES["$binary_dst_pth"]}" != 1 ]]
then
if [ -n "$IS_ELF" ]
then try_strip "$binary_dst_pth"
fi
if [[ -n "$LIBS" && ! -n "$IS_SCRIPT" && ! -n "$IS_STATIC" ]]
then
repath_needed_libs "$binary_dst_pth" "$needed_libs"
try_set_rpath "$binary_dst_pth"
fi
BINARIES["$binary_dst_pth"]=1
fi
if [ "$CREATE_LINKS" == 1 ]
then
try_mkdir "$sharun_bin_dir_pth"
[ "$hard_links" == 1 ] && \
unset ln_args||ln_args='-s'
(try_cd "$sharun_bin_dir_pth"
if [[ ! -e "$binary_name" && ! -L "$binary_name" ]]
then
[ "$VERBOSE" != 1 ]||echo -n "ln: "
ln $varg $ln_args ../sharun "$binary_name"||exit 1
fi
if [[ -n "$binary_real_name" && ! -e "$binary_real_name" && ! -L "$binary_real_name" ]]
then
[ "$VERBOSE" != 1 ]||echo -n "ln: "
ln $varg $ln_args ../sharun "$binary_real_name"||exit 1
fi)||exit 1
fi
fi
for lib_src_pth in $LIBS
do
if [[ "${LIBRARIES["$lib_src_pth"]}" != 1 ]]
then
unset lib_src_real_pth lib_src_real_name
if [ -L "$lib_src_pth" ]
then
lib_src_real_pth="$(readlink -f "$lib_src_pth")"
lib_src_real_name="$(basename "$lib_src_real_pth")"
lib_src_dirname_pth="$(dirname "$lib_src_real_pth")"
lib_info="$(file "$lib_src_real_pth" 2>/dev/null)"
else
lib_src_dirname_pth="$(dirname "$lib_src_pth")"
lib_info="$(file "$lib_src_pth" 2>/dev/null)"
fi
if [[ "$lib_info" =~ 'shared object' ]]
then
lib_src_dirname_pth="$(sed 's|^\.\.$||;s|^\.$||;s|\.\./|/|g;s|^/*|/|;s|^/$||;s|^/\.\.$||'<<<"$lib_src_dirname_pth")"
lib_src_name="$(basename "$lib_src_pth")"
grep -qE '/lib32|/i386-linux-gnu|/arm-linux-gnu'<<<"$lib_src_dirname_pth" && \
lib_dir="lib32"||lib_dir="lib"
lib_dst_dir_pth="${dst_dir_pth}/${lib_dir}$(sed 's|^/usr||;s|^/opt||;s|^/lib64||;s|^/lib32||;s|^/lib||;s|^/.*-linux-gnu||'<<<"$lib_src_dirname_pth")"
[ -n "$lib_src_real_name" ] && \
lib_dst_pth="$lib_dst_dir_pth/$lib_src_real_name"||\
lib_dst_pth="$lib_dst_dir_pth/$lib_src_name"
try_mkdir "$lib_dst_dir_pth"
try_cp_exe "$lib_src_pth" "$lib_dst_pth"
if [[ -n "$lib_src_real_name" && "$lib_src_name" != "$lib_src_real_name" ]]
then
(try_cd "$lib_dst_dir_pth"
try_ln "$lib_src_real_name" "$lib_src_name")||exit 1
fi
if [[ "${LIBRARIES["$lib_dst_pth"]}" != 1 ]]
then
if ! is_so && [ ! -L "${dst_dir}/${lib_dir}" ]
then
(try_cd "$dst_dir"
try_ln shared/$lib_dir $lib_dir)||exit 1
fi
repath_needed_libs "$lib_dst_pth"
try_strip "$lib_dst_pth"
if [[ ! "$lib_dst_pth" =~ "$INTERPRETER" && \
! "$lib_dst_pth" =~ ld-[0-9]\.[0-9]+\.so ]]
then try_set_rpath "$lib_dst_pth"
fi
if [ "$WITH_HOOKS" == 1 ]
then
case "$lib_dst_pth" in
*/gio/modules/*.so)
sys_giom_cache="${lib_src_pth/modules\/*\.so/modules\/giomodule.cache}"
dst_giom_dir="$(dirname "$lib_dst_pth")"
dst_giom_cache="$dst_giom_dir/giomodule.cache"
if [[ -f "$sys_giom_cache" && ! -f "$dst_giom_cache" ]]
then
info_msg "$YELLOW[ HOOK ]:$GREEN copy giomodule.cache..."
try_mkdir "$dst_giom_dir"
try_cp "$sys_giom_cache" "$dst_giom_cache"
fi ;;
*/libglib-*.so*)
glib="$(grep -o 'glib-.*\.so'<<<"$lib_src_name"|sed "s|\.so$||")"
sys_glib_schemas="/usr/share/$glib/schemas"
dst_glib_schemas="$dst_dir/share/$glib/schemas"
if [[ -d "$sys_glib_schemas" && ! -d "$dst_glib_schemas" ]]
then
info_msg "$YELLOW[ HOOK ]:$GREEN copy glib schemas..."
try_mkdir "$dst_glib_schemas"
try_cp -T "$sys_glib_schemas" "$dst_glib_schemas"
fi ;;
*/gdk-pixbuf-*/*/loaders/*.so)
sys_pixbufl_cache="${lib_src_pth/loaders\/*\.so/loaders.cache}"
dst_pixbufl_dir="$(dirname "$lib_dst_pth")"
dst_pixbufl_cache="${dst_pixbufl_dir}.cache"
if [[ -f "$sys_pixbufl_cache" && ! -f "$dst_pixbufl_cache" ]]
then
info_msg "$YELLOW[ HOOK ]:$GREEN copy gdk pixbuf loaders.cache..."
try_mkdir "$dst_pixbufl_dir"
try_cp "$sys_pixbufl_cache" "$dst_pixbufl_cache"
sed -i 's|/usr/lib/.*/loaders/||g' "$dst_pixbufl_cache"
fi ;;
*/gtk-*/*/immodules/*.so)
sys_gtkimm_cache="${lib_src_pth/immodules\/*\.so/immodules.cache}"
dst_gtkimm_dir="$(dirname "$lib_dst_pth")"
dst_gtkimm_cache="${dst_gtkimm_dir}.cache"
if [[ -f "$sys_gtkimm_cache" && ! -f "$dst_gtkimm_cache" ]]
then
info_msg "$YELLOW[ HOOK ]:$GREEN copy gtk immodules.cache..."
try_mkdir "$dst_gtkimm_dir"
try_cp "$sys_gtkimm_cache" "$dst_gtkimm_cache"
sed -i 's|/usr/lib/.*/immodules/||g' "$dst_gtkimm_cache"
fi ;;
*/libfontconfig.so*)
sys_fcfg='/etc/fonts/fonts.conf'
dst_fcfg="$dst_dir/etc/fonts/fonts.conf"
if [[ -f "$sys_fcfg" && ! -f "$dst_fcfg" ]]
then
info_msg "$YELLOW[ HOOK ]:$GREEN copy fonts.conf..."
try_mkdir "$dst_dir/etc/fonts"
try_cp "$sys_fcfg" "$dst_fcfg"
fi ;;
*/libxkbcommon*.so*)
sys_xcb_dir='/usr/share/X11/xkb'
dst_xcb_dir="$dst_dir/share/X11/xkb"
if [[ -d "$sys_xcb_dir" && ! -d "$dst_xcb_dir" ]]
then
info_msg "$YELLOW[ HOOK ]:$GREEN copy X11 xkb..."
try_mkdir "$dst_xcb_dir"
try_cp -T "$sys_xcb_dir" "$dst_xcb_dir"
fi ;;
*/libEGL_mesa.so*)
sys_glvnd_dir='/usr/share/glvnd/egl_vendor.d'
dst_glvnd_dir="$dst_dir/share/glvnd/egl_vendor.d"
if [[ -d "$sys_glvnd_dir" && ! -d "$dst_glvnd_dir" ]]
then
info_msg "$YELLOW[ HOOK ]:$GREEN copy EGL vendors..."
try_mkdir "$dst_glvnd_dir"
try_cp -T "$sys_glvnd_dir" "$dst_glvnd_dir"
fi ;;
*/libvulkan.so*)
sys_vk_icd_dir='/usr/share/vulkan/icd.d'
dst_vk_icd_dir="$dst_dir/share/vulkan/icd.d"
if [[ -d "$sys_vk_icd_dir" && ! -d "$dst_vk_icd_dir" ]]
then
info_msg "$YELLOW[ HOOK ]:$GREEN copy Vulkan ICD..."
try_mkdir "$dst_vk_icd_dir"
try_cp -T "$sys_vk_icd_dir" "$dst_vk_icd_dir"
sed -i 's|/usr/lib||g;s|/.*-linux-gnu||g;s|"/|"|g' "$dst_vk_icd_dir"/*
fi ;;
*/libwebkit*gtk-*.so*)
webkit="$(grep -Eo 'webkit(2)?gtk-[0-9]*\.[0-9]*'<<<"$lib_src_name")"
dst_webkit_lib_dir="$(dirname "$lib_dst_pth")/$webkit"
info_msg "$YELLOW[ HOOK ]:$GREEN hack webkitgtk to be portable..."
sed -i 's|/usr|././|g' "$lib_dst_pth"
for sys_webkit_lib_dir in "$(dirname "$lib_src_pth")/$webkit" \
"$(dirname "$lib_src_pth")exec/$webkit"
do
if [ -d "$sys_webkit_lib_dir" ] && \
[[ ! -d "$dst_webkit_lib_dir/injected-bundle" || \
! -f "$dst_webkit_lib_dir/WebKitWebProcess" ]]
then try_cp -T "$sys_webkit_lib_dir" "$dst_webkit_lib_dir"
fi
done
for bin_to_wrap in "$dst_webkit_lib_dir"/*
do
if [[ -f "$bin_to_wrap" && ! -L "$bin_to_wrap" ]] && \
! grep -q '.*\.so.*'<<<"$bin_to_wrap"
then
try_mkdir "$bin_dir_pth"
mv "$bin_to_wrap" "$bin_dir_pth"
ln -sr "$dst_dir/sharun" "$dst_webkit_lib_dir/$(basename "$bin_to_wrap")"
fi
done
if [ "$(basename "$(dirname "$lib_src_pth")")" != "$lib_dir" ]
then try_ln . "$dst_dir_pth/$lib_dir/$(basename "$(dirname "$lib_src_pth")")"
fi
if ! grep -q 'SHARUN_WORKING_DIR=${SHARUN_DIR}' "$dst_dir/.env" 2>/dev/null
then echo 'SHARUN_WORKING_DIR=${SHARUN_DIR}' >> "$dst_dir/.env"
fi ;;
*/libwebkit*gtkinjectedbundle.so)
sed -i 's|/usr|././|g' "$lib_dst_pth" ;;
*/libncursesw.so*|*/libcursesw.so*|*/libcurses.so*)
dst_terminfo_dir="$dst_dir/share/terminfo"
for terminfo_dir in '/etc' '/usr/share'
do
sys_terminfo_dir="$terminfo_dir/terminfo"
if [[ -d "$sys_terminfo_dir" && ! -d "$dst_terminfo_dir" ]]
then
info_msg "$YELLOW[ HOOK ]:$GREEN copy terminfo..."
try_mkdir "$dst_terminfo_dir"
try_cp -T "$sys_terminfo_dir" "$dst_terminfo_dir"
break
fi
done
sys_tabset_dir='/usr/share/tabset'
dst_tabset_dir="$dst_dir/share/tabset"
if [[ -d "$sys_tabset_dir" && ! -d "$dst_tabset_dir" ]]
then
info_msg "$YELLOW[ HOOK ]:$GREEN copy tabset..."
try_mkdir "$dst_tabset_dir"
try_cp -T "$sys_tabset_dir" "$dst_tabset_dir"
fi ;;
*/qt*/plugins/*.so)
qt_conf="$sharun_bin_dir_pth/qt.conf"
if [ ! -f "$qt_conf" ]
then
info_msg "$YELLOW[ HOOK ]:$GREEN create qt.conf..."
qt="$(grep -Eo 'qt([0-9])?'<<<"$lib_src_pth")"
echo -e \
"[Paths]\nPrefix = ../shared/${lib_dir}/${qt}\nPlugins = plugins\nImports = qml\nQml2Imports = qml" \
> "$qt_conf"
fi ;;
*/libmagic.so*)
dst_magic_file="$dst_dir/share/file/misc/magic.mgc"
for magic_file_dir in 'misc' 'file/misc'
do
sys_magic_file="/usr/share/$magic_file_dir/magic.mgc"
if [[ -f "$sys_magic_file" && ! -f "$dst_magic_file" ]]
then
info_msg "$YELLOW[ HOOK ]:$GREEN copy magic file..."
try_mkdir "$(dirname "$dst_magic_file")"
try_cp "$sys_magic_file" "$dst_magic_file"
break
fi
done ;;
esac
fi
LIBRARIES["$lib_dst_pth"]=1
fi
else
skip_msg "$BLUE[$lib_src_pth]$YELLOW not shared object!"
fi
LIBRARIES["$lib_src_pth"]=1
fi
done
if [[ -n "$INTERPRETER" && "$PATCH_INTERPRETER" == 1 && "${LIBRARIES["${INTERPRETER}_patched"]}" != 1 ]]
then
if [ "$LIBS_ONLY" != 1 ] && ! is_so
then
(try_cd "$bin_dir_pth"
if [ -f "../$lib_dir/$INTERPRETER" ]
then
info_msg "$YELLOW[ SET INTERPRETER ]: $BLUE[$bin_dir_pth/$binary_name -> ../$lib_dir/$INTERPRETER]"
patchelf $pvarg --set-interpreter "../$lib_dir/$INTERPRETER" "$binary_name"||exit 1
fi)||exit 1
fi
interpreter_pth="${dst_dir}/shared/$lib_dir/$INTERPRETER"
info_msg "$YELLOW[ PATCH INTERPRETER ]: $BLUE[$interpreter_pth]"
sed -i 's|/usr|/xxx|g;s|/lib|/XXX|g;s|/etc|/EEE|g' "$interpreter_pth"||exit 1
LIBRARIES["${INTERPRETER}_patched"]=1
fi
info_msg "[ DONE ]"
binary_number=$(( $binary_number + 1 ))
else
skip_msg "$BLUE[$binary]$YELLOW not executable or shared object!"
fi
BINARIES["$binary"]=1
fi
done
unset BINARIES LIBRARIES
if [ "$WITH_WRAPPE" == 1 ]
then
TMP_WRAPPE="/tmp/wrappe-$(uname -m)"
WRAPPE="${WRAPPE:="$(readlink -f "$(which_exe wrappe)")"}"
WRAPPE="${WRAPPE:="$TMP_WRAPPE"}"
if [ ! -x "$WRAPPE" ]
then
if [[ "${WRAPPE,,}" =~ ^http ]]
then
WRAPPE_URL="$WRAPPE"
WRAPPE="$TMP_WRAPPE"
fi
WRAPPE_URL="${WRAPPE_URL:=$(get_wrappe_git_url)}"
info_msg "Downloading wrappe -> '$WRAPPE'..."
info_msg "$WRAPPE_URL"
if try_dl "$WRAPPE_URL" "$WRAPPE"
then chmod $varg +x "$WRAPPE"
else
error_msg "Failed to download wrappe!"
exit 1
fi
fi
if [ ! -x "$WRAPPE" ]
then
error_msg "wrappe not found!"
exit 1
fi
fi
for dst_dir in "${!DST_DIRS[@]}"
do
if [ "$GEN_LIB_PATH" == 1 ]
then
(for lib_dir in lib lib32
do
lib_dir="${dst_dir}/${lib_dir}"
if [ -d "$lib_dir" ]
then
info_msg "$YELLOW[ GEN LIB PATH ]: $BLUE[${lib_dir}/lib.path]" && \
find_so "$lib_dir"|xargs -I {} dirname "{}"|\
sort -u|sed "s|${lib_dir}|+|g" > "${lib_dir}/lib.path"||exit 1
fi
done)||exit 1
fi
src_dir="${dst_dir%/shared}"
if [[ "$WITH_WRAPPE" == 1 && -x "$src_dir/sharun" ]]
then
src_bins=($(find "$src_dir/bin/" -not -type d -executable -printf "%f\n"))
if [ -n "$src_bins" ]
then
if [ "${#src_bins[@]}" == 1 ]
then wrappe_exec="$src_dir/bin/$src_bins"
else
if [ -n "$WRAPPE_EXEC" ]
then wrappe_exec="$src_dir/bin/$WRAPPE_EXEC"
else wrappe_exec="$src_dir/sharun"
fi
fi
wexec_name="${wrappe_exec##*/}"
[ -L "$wrappe_exec" ] && wrappe_args="$wexec_name $WRAPPE_ARGS"
info_msg "$YELLOW[ PACKING WITH WRAPPE ]..."
if "$WRAPPE" --unpack-target temp --unpack-directory .wrappe \
$([ "$WRAPPE_CLEANUP" == 0 ]||echo '--cleanup') \
--show-information none --compression "$WRAPPE_CLVL" "$src_dir" \
"$wrappe_exec" "$(dirname "$src_dir")/$wexec_name" -- $wrappe_args
then rm -rf "$src_dir"
fi
fi
fi
done