-
Notifications
You must be signed in to change notification settings - Fork 18
/
build
executable file
·1231 lines (1108 loc) · 34.2 KB
/
build
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
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/bin/sh
#
# Type `build -h` for usage and see https://github.com/romkatv/zsh-bin
# for documentation.
set -ue
if [ -n "${BASH_VERSION:-}" ]; then
if [ -z "${BASH_VERSION##[0-4].*}" ]; then
>&2 echo "[error] bash version < 5 is not supported; try another interpreter"
exit 1
fi
fi
if [ -n "${ZSH_VERSION:-}" ]; then
emulate sh -o err_exit -o no_unset
fi
usage="$(cat <<\END
Usage: build [-m ARCH] [-c CPU] [-i IMAGE] [-d CMD]
Creates an archive containing statically-linked, hermetic,
relocatable Zsh 5.8. Installation of Zsh from the archive
does not require libc, terminfo, ncurses or root access.
As long as the target machine has a compatible CPU and
kernel, it will work.
Options:
-m ARCH `uname -m` from the target machine; defaults to `uname -m`
from the local machine
-c CPU generate machine instructions for CPU of this type; this
value gets passed as `-march` (or `-mcpu` for ppc64le) to gcc;
inferred from ARCH if not set explicitly
-i IMAGE docker image used for building zsh; inferred from ARCH
if not set explicitly
-d CMD use this command instead of 'docker'; it must understand
the same command line arguments
END
)"
build="$(cat <<\END
outdir="$(pwd)"
unset TERMINFO TERMINFO_DIRS
if command -v mktemp >/dev/null 2>&1; then
workdir="$(mktemp -d "${TMPDIR:-/tmp}"/zsh-bin.XXXXXXXXXX)"
else
workdir="${TMPDIR:-/tmp}/zsh-bin.$$"
mkdir -- "$workdir"
fi
cd -- "$workdir"
workdir="$(pwd)"
narg() { echo $#; }
if [ "$(narg $workdir)" != 1 -o -z "${workdir##*:*}" ]; then
>&2 echo "[error] cannot build in this directory: $workdir"
exit 1
fi
cleanup() {
trap - INT QUIT TERM EXIT ILL PIPE
cd /
if ! rm -rf -- "$workdir"; then
sleep 5
rm -rf -- "$workdir"
fi
}
trap cleanup INT QUIT TERM EXIT ILL PIPE
libiconv_version=1.16
icmake_version=9.03.01
yodl_version=4.02.01
ncurses_version=6.2
kitty_version=0.25.2
termite_version=15
kakoune_version=2020.09.01
foot_version=1.7
zsh_doc_version=5.0.1
ncurses_url=https://ftp.gnu.org/pub/gnu/ncurses/ncurses-$ncurses_version.tar.gz
kitty_url=https://raw.githubusercontent.com/kovidgoyal/kitty/v$kitty_version/terminfo/kitty.terminfo
termite_url=https://raw.githubusercontent.com/thestinger/termite/v$termite_version/termite.terminfo
kakoune_url=https://raw.githubusercontent.com/mawww/kakoune/v$kakoune_version/contrib/tmux-256color.terminfo
foot_url=https://codeberg.org/dnkl/foot/raw/branch/releases/$foot_version/foot.info
libiconv_url=https://ftp.gnu.org/pub/gnu/libiconv/libiconv-$libiconv_version.tar.gz
icmake_url=https://gitlab.com/fbb-git/icmake/-/archive/$icmake_version/icmake-$icmake_version.tar.gz
yodl_url=https://gitlab.com/fbb-git/yodl/-/archive/$yodl_version/yodl-$yodl_version.tar.gz
zsh_url=https://github.com/zsh-users/zsh/archive/zsh-5.8.tar.gz
zsh_doc_url=https://github.com/romkatv/zsh-bin/releases/download/v$zsh_doc_version/zsh-5.8-linux-x86_64.tar.gz
ncurses_sha256=30306e0c76e0f9f1f0de987cf1c82a5c21e1ce6568b9227f7da5b71cbea86c9d
kitty_sha256=bdcac2c8cbae7bf2e2843e500ded9f550a6a8b3e66a14565b0156d8301dfc6aa
termite_sha256=771ecbde374ccefc40f06fb8ee0191bac1a81ab1f8f46b88288d1a9092770529
kakoune_sha256=b1a0c61f6ce3d123878c501c01183aac5cf951ed718eb1c73949eb86b00678be
foot_sha256=59769813ed6545c8176af291f49e281860ac70c30ceb5f72f307ec7ce9c992f4
libiconv_sha256=e6a1b1b589654277ee790cce3734f07876ac4ccfaecbee8afa0b649cf529cc04
icmake_sha256=7c5c091f58f576da580238c5e3636e2038d9ecf5efb6562ae7e402910d9036e6
yodl_sha256=0118efd6f05ddeed4910bb5e280a37a4d8e5c2f42fb2009c840b3d5c70f700f8
zsh_sha256=db6cdfadac8d3d1f85c55c3644620cf5a0f65bf01ca26a58ff06f041bf159a5d
zsh_doc_sha256=e1310318c6d0a14e595f71f2f393bf915758f834e50a5d101a98e246b7a137cf
cpus="$(getconf _NPROCESSORS_ONLN)" || cpus="$(sysctl -n hw.ncpu)" || cpus=8
fetch() {
local url="$1"
local sha256="$2"
local filename="${url##*/}"
local base="${filename%.tar.gz}"
printf '%s %s\n' "$sha256" "$filename" >"$base".sha256
if ! cp -- "$outdir/src/$filename" . 2>/dev/null || ! shasum -b -a 256 -c "$base".sha256; then
rm -f -- "$filename"
wget -- "$url"
shasum -b -a 256 -c "$base".sha256
mkdir -p -- "$outdir"/src
cp -f -- "$filename" "$outdir"/src/tmp."$filename"
mv -f -- "$outdir"/src/tmp."$filename" "$outdir"/src/"$filename"
fi
case "$filename" in
*.tar.gz)
tar -xzf "$filename"
;;
esac
}
ncurses_configure=
case "$ZSH_BIN_KERNEL" in
linux)
apk update
apk add \
autoconf \
bash \
binutils \
file \
g++ \
gcc \
gdbm-dev \
groff \
make \
man \
musl-dev \
pcre-dev \
perl-utils \
rsync \
tar \
texinfo \
util-linux
cd -- "$workdir"
fetch "$libiconv_url" "$libiconv_sha256"
cd libiconv-"$libiconv_version"
./configure \
--prefix=/usr \
--disable-dependency-tracking \
--enable-static \
--disable-shared \
--enable-extra-encodings \
--disable-rpath \
--enable-relocatable
make -j "$cpus" install
;;
freebsd)
pkg install -y \
autoconf \
binutils \
gcc \
gmake \
groff \
libiconv \
perl5 \
pcre \
rsync \
texinfo \
yodl
;;
darwin)
if ! command -v make >/dev/null 2>&1 || ! command -v gcc >/dev/null 2>&1; then
>&2 echo "[error] please run 'xcode-select --install' and retry"
exit 1
fi
if command -v port >/dev/null 2>&1; then
sudo port -N install autoconf libiconv pcre wget
elif command -v brew >/dev/null 2>&1; then
for formula in autoconf libiconv pcre wget; do
if brew ls --version "$formula" >/dev/null; then
brew upgrade "$formula"
else
brew install "$formula"
fi
done
else
>&2 echo "[error] please install MacPorts or Homebrew and retry"
exit 1
fi
mkdir -- "$workdir"/zsh_doc
cd -- "$workdir"/zsh_doc
fetch "$zsh_doc_url" "$zsh_doc_sha256"
;;
msys*|mingw*)
ncurses_configure='--with-pcre2'
pacman -Syu --noconfirm
pacman -S --needed --noconfirm \
autoconf \
binutils \
gcc \
groff \
libiconv-devel \
make \
man \
perl \
pcre-devel \
rsync \
texinfo \
yodl
;;
cygwin*)
for cmd in autoconf bash colcrt gcc g++ groff ld make makeinfo shasum tar wget; do
if ! command -v "$cmd" >/dev/null 2>&1; then
>&2 echo "[error] command not found: $cmd"
>&2 echo ""
>&2 echo "Make sure the following Cygwin packages are installed:"
>&2 echo ""
>&2 echo " autoconf"
>&2 echo " bash"
>&2 echo " binutils"
>&2 echo " gcc-core"
>&2 echo " gcc-g++"
>&2 echo " groff"
>&2 echo " libiconv-devel"
>&2 echo " make"
>&2 echo " perl"
>&2 echo " tar"
>&2 echo " texinfo"
>&2 echo " rsync"
>&2 echo " util-linux"
>&2 echo " wget"
exit 1
fi
done
for file in /usr/lib/libiconv.a; do
if [ ! -e "$file" ]; then
>&2 echo "[error] not found: $file"
exit 1
fi
done
;;
*)
>&2 echo "[internal error] unhandled kernel: $ZSH_BIN_KERNEL"
exit 1
;;
esac
if [ ! -e "$workdir"/zsh_doc ] && ! command -v yodl >/dev/null 2>&1; then
if ! command -v icmake >/dev/null 2>&1; then
cd -- "$workdir"
fetch "$icmake_url" "$icmake_sha256"
cd icmake-"$icmake_version"/icmake
./icm_prepare /
./icm_bootstrap x
./icm_install all
fi
cd -- "$workdir"
fetch "$yodl_url" "$yodl_sha256"
cd yodl-"$yodl_version"
mkdir fake-bin
touch fake-bin/tput
chmod +x fake-bin/tput
fake_bin_dir="$(pwd)"/fake-bin
cd yodl
PATH="$fake_bin_dir:$PATH" ./build programs
PATH="$fake_bin_dir:$PATH" ./build macros
PATH="$fake_bin_dir:$PATH" ./build install programs
PATH="$fake_bin_dir:$PATH" ./build install macros
fi
cd -- "$workdir"
fetch "$ncurses_url" "$ncurses_sha256"
cd ncurses-"$ncurses_version"
mkdir fake-bin
echo 'false' >fake-bin/ldconfig
chmod +x fake-bin/ldconfig
fake_bin_dir="$(pwd)"/fake-bin
PATH="$fake_bin_dir:$PATH" ./configure \
--prefix "$workdir"/ncurses \
--disable-pc-files \
--disable-mixed-case \
--disable-rpath \
--disable-bsdpad \
--disable-termcap \
--disable-rpath-hack \
--enable-root-environ \
--enable-widec \
--without-ada \
--without-manpages \
--without-tack \
--without-tests \
--without-pc-suffix \
--without-pkg-config-libdir \
--without-shared \
--without-debug \
--without-cxx-shared \
--without-gmp \
--without-dlsym \
--without-pcre2 \
--with-terminfo-dirs="/etc/terminfo:/usr/share/terminfo:/lib/terminfo:/usr/lib/terminfo" \
$ncurses_configure
if command -v gmake >/dev/null 2>/dev/null; then
gmake -j "$cpus" install
else
make -j "$cpus" install
fi
cd -- "$workdir"
fetch "$kitty_url" "$kitty_sha256"
ncurses/bin/tic -x -o ncurses/share/terminfo kitty.terminfo
cd -- "$workdir"
fetch "$termite_url" "$termite_sha256"
ncurses/bin/tic -x -o ncurses/share/terminfo termite.terminfo
cd -- "$workdir"
fetch "$kakoune_url" "$kakoune_sha256"
ncurses/bin/tic -x -o ncurses/share/terminfo tmux-256color.terminfo
cd -- "$workdir"
fetch "$foot_url" "$foot_sha256"
ncurses/bin/tic -x -o ncurses/share/terminfo foot.info
cp ncurses/share/terminfo/66/foot ncurses/share/terminfo/66/foot-extra
cp ncurses/share/terminfo/66/foot+base ncurses/share/terminfo/66/foot+base-extra
cp ncurses/share/terminfo/66/foot-direct ncurses/share/terminfo/66/foot-direct-extra
cd -- "$workdir"
fetch "$zsh_url" "$zsh_sha256"
cd zsh-zsh-5.8
patch -u aczsh.m4 <<-\END
--- aczsh.m4 2020-12-21 19:06:04.847363893 +0100
+++ aczsh.m4.new 2020-12-21 19:05:51.975418771 +0100
@@ -118,6 +118,7 @@
AC_TRY_COMMAND($CC -c $CFLAGS $CPPFLAGS $DLCFLAGS conftest2.c 1>&AC_FD_CC) &&
AC_TRY_COMMAND($DLLD -o conftest2.$DL_EXT $LDFLAGS $DLLDFLAGS conftest2.o $LIBS 1>&AC_FD_CC); then
AC_TRY_RUN([
+#include <stdlib.h>
#ifdef HPUX10DYNAMIC
#include <dl.h>
#define RTLD_LAZY BIND_DEFERRED
@@ -199,6 +200,7 @@
AC_TRY_COMMAND($CC -c $CFLAGS $CPPFLAGS $DLCFLAGS conftest2.c 1>&AC_FD_CC) &&
AC_TRY_COMMAND($DLLD -o conftest2.$DL_EXT $LDFLAGS $DLLDFLAGS conftest2.o $LIBS 1>&AC_FD_CC); then
AC_TRY_RUN([
+#include <stdlib.h>
#ifdef HPUX10DYNAMIC
#include <dl.h>
#define RTLD_LAZY BIND_DEFERRED
@@ -274,6 +276,7 @@
AC_TRY_COMMAND($CC -c $CFLAGS $CPPFLAGS $DLCFLAGS conftest2.c 1>&AC_FD_CC) &&
AC_TRY_COMMAND($DLLD -o conftest2.$DL_EXT $LDFLAGS $DLLDFLAGS conftest2.o $LIBS 1>&AC_FD_CC); then
AC_TRY_RUN([
+#include <stdlib.h>
#ifdef HPUX10DYNAMIC
#include <dl.h>
#define RTLD_LAZY BIND_DEFERRED
@@ -343,6 +346,7 @@
save_ldflags=$LDFLAGS
LDFLAGS="$LDFLAGS $EXTRA_LDFLAGS"
AC_TRY_RUN([
+#include <stdlib.h>
#ifdef HPUX10DYNAMIC
#include <dl.h>
#define RTLD_LAZY BIND_DEFERRED
@@ -416,6 +420,7 @@
save_ldflags=$LDFLAGS
LDFLAGS="$LDFLAGS $EXTRA_LDFLAGS -s"
AC_TRY_RUN([
+#include <stdlib.h>
#ifdef HPUX10DYNAMIC
#include <dl.h>
#define RTLD_LAZY BIND_DEFERRED
@@ -483,6 +488,7 @@
if AC_TRY_COMMAND($CC -c $CFLAGS $CPPFLAGS $DLCFLAGS conftest1.c 1>&AC_FD_CC) &&
AC_TRY_COMMAND($DLLD -o conftest1.$DL_EXT $LDFLAGS $DLLDFLAGS -s conftest1.o $LIBS 1>&AC_FD_CC); then
AC_TRY_RUN([
+#include <stdlib.h>
#ifdef HPUX10DYNAMIC
#include <dl.h>
#define RTLD_LAZY BIND_DEFERRED
END
patch -u configure.ac <<-\END
--- configure.ac 2020-12-21 19:06:04.851363875 +0100
+++ configure.ac.new 2020-12-21 19:05:59.199387974 +0100
@@ -1394,6 +1394,10 @@
AC_CACHE_CHECK(if tgetent accepts NULL,
zsh_cv_func_tgetent_accepts_null,
[AC_RUN_IFELSE([AC_LANG_SOURCE([[
+#include <fcntl.h>
+#include <stdlib.h>
+int tgetent(char *, char *);
+char *tgetstr(char *, char **);
main()
{
char buf[4096];
@@ -1418,6 +1422,10 @@
AC_CACHE_CHECK(if tgetent returns 0 on success,
zsh_cv_func_tgetent_zero_success,
[AC_RUN_IFELSE([AC_LANG_SOURCE([[
+#include <fcntl.h>
+#include <stdlib.h>
+int tgetent(char *, char*);
+char *tgetstr(char *, char **);
main()
{
char buf[4096];
@@ -1855,6 +1863,7 @@
#include <sys/time.h>
#endif
#include <sys/resource.h>
+#include <stdlib.h>
main(){struct rlimit r;exit(sizeof(r.rlim_cur) <= sizeof(long));}]])],[zsh_cv_rlim_t_is_longer=yes],[zsh_cv_rlim_t_is_longer=no],[zsh_cv_rlim_t_is_longer=yes])])
if test x$zsh_cv_rlim_t_is_longer = xyes; then
AC_CACHE_CHECK(if rlim_t is a quad,
@@ -1865,6 +1874,7 @@
#endif
#include <stdio.h>
#include <sys/resource.h>
+#include <stdlib.h>
main() {
struct rlimit r;
char buf[20];
@@ -1887,6 +1897,7 @@
#include <sys/time.h>
#endif
#include <sys/resource.h>
+#include <stdlib.h>
main(){struct rlimit r;r.rlim_cur=-1;exit(r.rlim_cur<0);}]])],[zsh_cv_type_rlim_t_is_unsigned=yes],[zsh_cv_type_rlim_t_is_unsigned=no],[zsh_cv_type_rlim_t_is_unsigned=no])])
if test x$zsh_cv_type_rlim_t_is_unsigned = xyes; then
AC_DEFINE(RLIM_T_IS_UNSIGNED)
@@ -2258,6 +2269,9 @@
[AC_RUN_IFELSE([AC_LANG_SOURCE([[
#include <fcntl.h>
#include <signal.h>
+#include <unistd.h>
+#include <stdlib.h>
+#include <sys/stat.h>
main()
{
char c;
@@ -2299,6 +2313,7 @@
[AC_RUN_IFELSE([AC_LANG_SOURCE([[
#include <unistd.h>
#include <fcntl.h>
+#include <stdlib.h>
main()
{
int ret;
@@ -2331,6 +2346,7 @@
#include <unistd.h>
#include <signal.h>
#include <errno.h>
+#include <stdlib.h>
main()
{
int pid = (getpid() + 10000) & 0xffffff;
@@ -2356,6 +2372,7 @@
[AC_RUN_IFELSE([AC_LANG_SOURCE([[
#include <signal.h>
#include <unistd.h>
+#include <stdlib.h>
int child=0;
void handler(sig)
int sig;
@@ -2407,6 +2424,7 @@
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>
+#include <stdlib.h>
main() {
int fd;
int ret;
@@ -2450,6 +2468,10 @@
zsh_cv_sys_getpwnam_faked,
[AC_RUN_IFELSE([AC_LANG_SOURCE([[
#include <pwd.h>
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+#include <unistd.h>
main() {
struct passwd *pw1, *pw2;
char buf[1024], name[1024];
@@ -2777,6 +2799,7 @@
[AC_RUN_IFELSE([AC_LANG_SOURCE([[/* Test for whether ELF binaries are produced */
#include <fcntl.h>
#include <stdlib.h>
+#include <unistd.h>
main(argc, argv)
int argc;
char *argv[];
@@ -2930,6 +2953,7 @@
AC_TRY_COMMAND($DLLD $LDFLAGS $DLLDFLAGS -o conftest.$DL_EXT conftest.o 1>&AS_MESSAGE_LOG_FD) &&
AC_RUN_IFELSE([AC_LANG_SOURCE([[
#include <stdio.h>
+#include <stdlib.h>
#ifdef HPUX10DYNAMIC
#include <dl.h>
#define RTLD_LAZY BIND_DEFERRED
END
case "$ZSH_BIN_CPU" in
powerpc64le) archflag="-mcpu";;
*) archflag="-march";;
esac
name=zsh-5.8-"$ZSH_BIN_KERNEL"-"$ZSH_BIN_ARCH"
export CFLAGS="-Wall -Wmissing-prototypes $archflag=$ZSH_BIN_CPU -DNDEBUG -O2 -flto -fno-strict-aliasing"
export LDFLAGS=
export EXELDFLAGS=
export LIBS=
CFLAGS="$CFLAGS -I$workdir/ncurses/include"
LDFLAGS="$LDFLAGS -L$workdir/ncurses/lib"
case "$ZSH_BIN_KERNEL" in
linux|freebsd|msys*|mingw*)
EXELDFLAGS="$EXELDFLAGS -static"
LIBS="$LIBS -lpcre"
;;
darwin)
mkdir -- "$workdir"/lib
if [ -e /opt/local/lib/libiconv.a -a -e /opt/local/lib/libpcre.a ]; then
ln -s -- /opt/local/lib/libiconv.a "$workdir"/lib
ln -s -- /opt/local/lib/libpcre.a "$workdir"/lib
CFLAGS="$CFLAGS -I/opt/local/include -I/opt/local/include"
else
brew_prefix="$(command brew --prefix)"
opt="$brew_prefix"/opt
ln -s -- "$opt"/libiconv/lib/libiconv.a "$workdir"/lib
ln -s -- "$opt"/pcre/lib/libpcre.a "$workdir"/lib
CFLAGS="$CFLAGS -I$opt/libiconv/include -I$opt/pcre/include"
fi
LDFLAGS="$LDFLAGS -L$workdir/lib"
LIBS="$LIBS -lpcre"
;;
cygwin*)
EXELDFLAGS="$EXELDFLAGS -static"
;;
*)
>&2 echo "[internal error] unhandled kernel: $ZSH_BIN_KERNEL"
exit 1
;;
esac
export DLLDFLAGS="-shared"
export LDFLAGS="$CFLAGS $LDFLAGS"
sed_i() {
[ $# -gt 1 ]
local script="$1"
while [ $# != 1 ]; do
shift
local file="$1"
sed "$script" "$file" >"$file".tmp
mv -- "$file".tmp "$file"
done
}
sed_i 's/armv\*/armv* | arm64/' config.sub
host="$(./config.guess)"
if [ -z "$host" -o -n "${host##*?-?*}" ]; then
>&2 echo "[error] unexpected output from ./config.guess: $host"
exit 1
fi
./Util/preconfig
./configure \
--prefix="$workdir/$name" \
--disable-etcdir \
--disable-zshenv \
--disable-zshrc \
--disable-zlogin \
--disable-zprofile \
--disable-zlogout \
--disable-site-fndir \
--disable-site-scriptdir \
--enable-cap \
--enable-unicode9 \
--with-tcsetpgrp \
--disable-dynamic \
--host="$ZSH_BIN_ARCH-${host#*-}" \
--enable-custom-patchlevel=zsh-5.8-0-g77d203f
sed_i 's/link=no/link=static/' config.modules
case "$ZSH_BIN_KERNEL" in
linux);;
freebsd)
sed_i 's/attr.mdd link=static/attr.mdd link=no/' config.modules
sed_i 's/db_gdbm.mdd link=static/db_gdbm.mdd link=no/' config.modules
;;
darwin)
sed_i 's/db_gdbm.mdd link=static/db_gdbm.mdd link=no/' config.modules
;;
msys*|mingw*)
sed_i 's/db_gdbm.mdd link=static/db_gdbm.mdd link=no/' config.modules
;;
cygwin*)
sed_i 's/db_gdbm.mdd link=static/db_gdbm.mdd link=no/' config.modules
sed_i 's/pcre.mdd link=static/pcre.mdd link=no/' config.modules
;;
*)
>&2 echo "[internal error] unhandled kernel: $ZSH_BIN_KERNEL"
exit 1
;;
esac
magic=iLWDLaG9dUlsxzEQp10k
sed_i '46i\
#include <math.h>' Src/params.c
sed_i 's|mv -f zshpaths.h.tmp zshpaths.h|cp -f ../zshpaths.h ./|' Src/zsh.mdd
sed_i 's/\(\$(LN_S).*\);/( cd -- $(DESTDIR)$(runhelpdir) \&\& \1; );/' Doc/Makefile.in
sed_i 's/tgetent/tgetent_with_env/g' Src/init.c
patch -u Src/init.c <<-\END
--- Src/init.c 2020-12-22 14:41:00.524162742 +0100
+++ Src/init.c.new 2020-12-22 14:49:40.535136789 +0100
@@ -869,6 +869,10 @@
return 1;
}
+#if defined(__APPLE__)
+static void init_paths_from_exe_path(void);
+#endif
+
/* Initialize lots of global variables and hash tables */
/**/
@@ -905,6 +909,10 @@
#endif
int close_fds[10], tmppipe[2];
+#if defined(__APPLE__)
+ init_paths_from_exe_path();
+#endif
+
/*
* Workaround a problem with NIS (in one guise or another) which
* grabs file descriptors and keeps them for future reference.
END
cat >>Src/init.c <<-\END
volatile char tagged_script_dir[sizeof(SCRIPT_DIR_TAG) + 4096] = {
SCRIPT_DIR_TAG "/usr/share/zsh/5.8/scripts"
};
volatile char tagged_fpath_dir[sizeof(FPATH_DIR_TAG) + 4096] = {
FPATH_DIR_TAG "/usr/share/zsh/5.8/functions"
};
volatile char tagged_terminfo_dir[sizeof(TERMINFO_DIR_TAG) + 4096] = {
TERMINFO_DIR_TAG "/usr/share/terminfo"
};
extern int tgetent_with_env(char *termbuf, char *term) {
int res;
int ev_len;
char **ev;
char *orig;
char *patched;
orig = getenv("TERMINFO_DIRS");
if (orig) {
orig -= strlen("TERMINFO_DIRS") + 1;
patched = tricat(orig, ":", TERMINFO_DIR);
putenv(patched); /* ignore ENOMEM */
} else {
patched = bicat("TERMINFO_DIRS=", TERMINFO_DIR);
ev = environ;
ev_len = arrlen(environ);
environ = (char **)zalloc(sizeof(char *) * (ev_len + 2));
memcpy(environ, ev, sizeof(char *) * ev_len);
environ[ev_len] = patched;
environ[ev_len + 1] = NULL;
}
res = tgetent(termbuf, term);
if (orig) {
putenv(orig); /* ignore ENOMEM */
} else {
zfree(environ, sizeof(char *) * (ev_len + 2));
environ = ev;
}
zsfree(patched);
return res;
}
#if defined(__APPLE__)
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <libproc.h>
#include <unistd.h>
static void init_paths_from_exe_path(void) {
int n, i;
pid_t pid;
char exepath[PROC_PIDPATHINFO_MAXSIZE];
pid = getpid();
if (proc_pidpath(pid, exepath, sizeof(exepath)) < 0) {
perror("proc_pidpath");
exit(1);
}
n = strlen(exepath);
for (i = 0; i != 2; --n) {
if (n == 0) {
fprintf(stderr, "zsh: invalid exe path: %s\n", exepath);
exit(1);
}
if (exepath[n-1] == '/') ++i;
}
memcpy((char*)tagged_script_dir + strlen(SCRIPT_DIR_TAG), exepath, n);
memcpy((char*)tagged_fpath_dir + strlen(FPATH_DIR_TAG), exepath, n);
memcpy((char*)tagged_terminfo_dir + strlen(TERMINFO_DIR_TAG), exepath, n);
strcpy((char*)tagged_script_dir + strlen(SCRIPT_DIR_TAG) + n, "/share/zsh/5.8/scripts");
strcpy((char*)tagged_fpath_dir + strlen(FPATH_DIR_TAG) + n, "/share/zsh/5.8/functions");
strcpy((char*)tagged_terminfo_dir + strlen(TERMINFO_DIR_TAG) + n, "/share/terminfo");
}
#endif
END
cat >zshpaths.h <<-\END
#define MODULE_DIR "/dev/null"
#define SCRIPT_DIR_TAG ":@ZSH_BIN_MAGIC@:script:"
#define FPATH_DIR_TAG ":@ZSH_BIN_MAGIC@:fpath:"
#define TERMINFO_DIR_TAG ":@ZSH_BIN_MAGIC@:terminfo:"
extern volatile char tagged_script_dir[sizeof(SCRIPT_DIR_TAG) + 4096];
extern volatile char tagged_fpath_dir[sizeof(FPATH_DIR_TAG) + 4096];
extern volatile char tagged_terminfo_dir[sizeof(TERMINFO_DIR_TAG) + 4096];
#define SCRIPT_DIR ((const char *)(tagged_script_dir + sizeof(SCRIPT_DIR_TAG) - 1))
#define FPATH_DIR ((const char *)(tagged_fpath_dir + sizeof(FPATH_DIR_TAG) - 1))
#define TERMINFO_DIR ((const char *)(tagged_terminfo_dir + sizeof(TERMINFO_DIR_TAG) - 1))
extern int tgetent_with_env(char *, char *);
END
sed_i "s/@ZSH_BIN_MAGIC@/$magic/g" zshpaths.h
if [ -e "$workdir"/zsh_doc ]; then
make -j "$cpus" install.bin install.modules install.fns
else
make -j "$cpus" install install.info
fi
rsync -a --copy-links --copy-dirlinks -- "$workdir/$name"/ "$workdir/$name".tmp/
rm -r -- "$workdir/$name"
mv -- "$workdir/$name".tmp "$workdir/$name"
cd -- "$workdir/$name"
strip bin/zsh
rm -- bin/zsh-5.8
rsync -a --copy-links --copy-dirlinks -- "$workdir"/ncurses/share/terminfo share/
for dir in share/terminfo/*; do
mv -- "$dir" "$dir".zsh-bin
done
if [ -e "$workdir"/zsh_doc ]; then
cp -r -- "$workdir"/zsh_doc/share/man "$workdir"/zsh_doc/share/info share/
cp -r -- "$workdir"/zsh_doc/share/zsh/5.8/help share/zsh/5.8/
fi
cp -r -- share/man share/info ./
sed_i 's/local HELPDIR=.*/[[ -n "${HELPDIR:-}" ]] || local HELPDIR="$(<<\\'"$magic"'\
'"$magic"'\
)"/' \
share/zsh/5.8/functions/run-help \
share/zsh/5.8/functions/_run-help
cat >share/zsh/5.8/scripts/relocate <<-\END
#!/bin/sh
set -ue
magic='@ZSH_BIN_MAGIC@'
fpath_pos=@ZSH_BIN_FPATH_POS@
script_pos=@ZSH_BIN_SCRIPT_POS@
terminfo_pos=@ZSH_BIN_TERMINFO_POS@
do_patch_bin=@ZSH_BIN_DO_PATCH_BIN@
usage="$(cat <<-\USAGE
Usage: relocate [-s SRC] [-d DST]
Modifies hard-coded paths within Zsh residing in directory SRC
so that they point to directory DST.
The source directory must exist but the destination directory
does not have to.
Options:
-s SRC directory where Zsh is currently installed; this is the
directory with 'bin' and 'usr' as subdirectories; if not
specified, this directory is automatically derived from
path to 'relocate'
-d DST directory from which Zsh will be used; this is the
directory with 'bin' and 'usr' as subdirectories; if not
specified, this directory is automatically derived from
path to 'relocate'
USAGE
)"
src=
dst=
while getopts ':s:d:h' opt "$@"; do
case "$opt" in
h)
printf '%s\n' "$usage"
exit
;;
s)
if [ -n "$src" ]; then
>&2 echo "[error] duplicate option: -$opt"
exit 1
fi
if [ -z "$OPTARG" ]; then
>&2 echo "[error] incorrect value of -$opt: $OPTARG"
exit 1
fi
src="$OPTARG"
;;
d)
if [ -n "$dst" ]; then
>&2 echo "[error] duplicate option: -$opt"
exit 1
fi
if [ -z "$OPTARG" ]; then
>&2 echo "[error] incorrect value of -$opt: $OPTARG"
exit 1
fi
dst="$OPTARG"
;;
\?) >&2 echo "[error] invalid option: -$OPTARG" ; exit 1;;
:) >&2 echo "[error] missing required argument: -$OPTARG"; exit 1;;
*) >&2 echo "[internal error] unhandled option: -$opt" ; exit 1;;
esac
done
if [ "$OPTIND" -le $# ]; then
>&2 echo "[error] unexpected positional argument"
exit 1
fi
if [ -z "$src" ]; then
src="$(dirname -- "$0")"/../../../..
fi
absdir() {
( [ -d "$1" ] && cd -- "$1" && pwd )
}
if ! src="$(absdir "$src")"; then
>&2 echo "[error] not a directory: $src"
exit 1
fi
if [ -n "$dst" ]; then
if [ -n "${dst##/*}" ]; then
>&2 echo "[error] path not absolute: $dst"
exit 1
fi
else
dst="$(dirname -- "$0")"/../../../..
if ! dst="$(absdir "$dst")"; then
>&2 echo "[error] not a directory: $dst"
exit 1
fi
fi
zsh="$src"/bin/zsh
runhelp="$src"/share/zsh/5.8/functions/run-help
runhelpcomp="$src"/share/zsh/5.8/functions/_run-help
if [ ! -x "$zsh" ]; then
>&2 echo "[error] not an executable file: $zsh"
exit 1
fi
if ! grep -qF 'local HELPDIR' -- "$runhelp"; then
>&2 echo "[error] cannot relocate zsh from this directory: $src"
exit 1
fi
if ! grep -qF 'local HELPDIR' -- "$runhelpcomp"; then
>&2 echo "[error] cannot relocate zsh from this directory: $src"
exit 1
fi
dst="${dst%/}/"
# 4096 minus 23 for "share/zsh/5.8/functions"
if [ ${#dst} -gt 4073 ]; then
>&2 echo "[error] directory name too long: $dst"
exit 1
fi
if [ -z "${dst##*$magic*}" ]; then
>&2 echo "[error] cannot relocate to this directory: $dst"
exit 1
fi
cp -pf -- "$zsh" "$zsh".tmp
patch_help() {
local data
data="$(cat -- "$1")"
local prefix="${data%%$magic*}"
local suffix="${data##*$magic}"
if [ "$prefix" = "$data" -o "$suffix" = "$data" ]; then
>&2 echo "[error] not a relocatable zsh directory: $src"
exit 1
fi
local dir="$dst"share/zsh/5.8/help
printf '%s\n%s\n%s\n' "$prefix$magic" "$dir" "$magic$suffix" >"$1".tmp
}
patch_bin() {
"$do_patch_bin" || return 0
local header_len=$((1 + ${#magic} + 1 + ${#2} + 1))
local header
if ! header="$(dd if="$zsh" bs=1 skip="$1" count="$header_len" 2>/dev/null)"; then
header="$(dd if="$zsh" bs=1 skip="$1" count="$header_len")"
fi
if [ "$header" != ":$magic:$2:" ]; then
>&2 echo "[error] not a relocatable zsh binary: $zsh"
exit 1
fi
local pos=$(($1 + header_len))
local dir="${dst}$3"
local err
if ! err="$(dd if=/dev/zero of="$zsh".tmp bs=1 seek="$pos" count=4096 conv=notrunc 2>&1)"; then
>&2 printf '%s\n' "$err"
exit 1
fi
if ! err="$(printf '%s' "$dir" |
dd of="$zsh".tmp bs=1 seek="$pos" count=${#dir} conv=notrunc 2>&1)"; then
>&2 printf '%s\n' "$err"
exit 1
fi
}
patch_help "$runhelp"
patch_help "$runhelpcomp"
patch_bin "$fpath_pos" fpath share/zsh/5.8/functions
patch_bin "$script_pos" script share/zsh/5.8/scripts
patch_bin "$terminfo_pos" terminfo share/terminfo
if "$do_patch_bin"; then
if ! fpath="$(unset FPATH && "$zsh".tmp -c 'print -r -- $fpath[1]')" ||
[ "${fpath#$dst}" = "$fpath" ]; then
>&2 echo "[error] failed to relocate zsh"
exit 1
fi
fi
mv -f -- "$zsh".tmp "$zsh"
mv -f -- "$runhelp".tmp "$runhelp"
mv -f -- "$runhelpcomp".tmp "$runhelpcomp"
END
sed_i "s/@ZSH_BIN_MAGIC@/$magic/g" share/zsh/5.8/scripts/relocate
if [ "$ZSH_BIN_KERNEL" = darwin ]; then
sed_i 's/@ZSH_BIN_DO_PATCH_BIN@/false/g' share/zsh/5.8/scripts/relocate
else
sed_i 's/@ZSH_BIN_DO_PATCH_BIN@/true/g' share/zsh/5.8/scripts/relocate
fi
embed_pos() {
local cmd='
bin=$(LC_ALL=C tr -c "[:alnum:]:" " " <$0)
parts=("${(@ps:$1:)bin}")
(( $#parts == 2 ))
print -r -- $#parts[1]'
local pos
pos="$(bin/zsh -fuec "$cmd" bin/zsh ":$magic:$1:")"
printf '%s' "$pos" | grep -qxE '[0-9]+'