/
crossplatform-functions.sh
executable file
·3076 lines (2880 loc) · 146 KB
/
crossplatform-functions.sh
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
# ----------------------------
# Copyright 2021 Diskuv, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ----------------------------
#
# @jonahbeckford: 2021-09-07
# - This file is licensed differently than the rest of the Diskuv OCaml distribution.
# Keep the Apache License in this file since this file is part of the reproducible
# build files.
#
######################################
# crossplatform-functions.sh
#
# Meant to be `source`-d.
#
# Can be run within a container or outside of a container.
#
export SHARE_OCAML_OPAM_REPO_RELPATH=share/dkml/repro
export SHARE_REPRODUCIBLE_BUILD_RELPATH=share/dkml/repro
export SHARE_FUNCTIONS_RELPATH=share/dkml/functions
# Prefer dash if it is there because it is average 4x faster than bash and should
# be much more secure. Otherwise /bin/sh which should always be a POSIX
# compatible shell.
#
# Output:
# - env:DKML_POSIX_SHELL - The path to the POSIX shell. Only set if it wasn't already
# set.
# - env:DKML_HOST_POSIX_SHELL - The host's path to the POSIX shell. Only set if it wasn't already
# set. On a Windows host (Cygwin/MSYS2) this will be a Windows path; on Unix this will be a Unix
# path.
# References:
# - https://unix.stackexchange.com/questions/148035/is-dash-or-some-other-shell-faster-than-bash
autodetect_posix_shell() {
export DKML_POSIX_SHELL
export DKML_HOST_POSIX_SHELL
if [ -n "${DKML_POSIX_SHELL:-}" ] && [ -n "${DKML_HOST_POSIX_SHELL}" ]; then
return
# On MSYS2 especially, binaries look like they exist simultaneously in /usr/bin and /bin but
# only if you are inside MSYS2. The binaries in /bin are in fact a mount of /usr/bin.
# This is a critical problem for `opam exec -- /bin/dash.exe` which will fail because Opam cannot
# see the mount.
elif [ -e /usr/bin/dash.exe ]; then
DKML_POSIX_SHELL=/usr/bin/dash.exe
elif [ -e /usr/bin/dash ]; then
DKML_POSIX_SHELL=/usr/bin/dash
elif [ -e /bin/dash.exe ]; then
DKML_POSIX_SHELL=/bin/dash.exe
elif [ -e /bin/dash ]; then
DKML_POSIX_SHELL=/bin/dash
elif [ -e /bin/sh.exe ]; then
DKML_POSIX_SHELL=/bin/sh.exe
else
DKML_POSIX_SHELL=/bin/sh
fi
if [ -x /usr/bin/cygpath ]; then
DKML_HOST_POSIX_SHELL=$(/usr/bin/cygpath -aw "$DKML_POSIX_SHELL")
else
DKML_HOST_POSIX_SHELL="$DKML_POSIX_SHELL"
fi
}
# Set the parent directory of DiskuvOCamlHome.
#
# Always defined, even on Unix. It is your responsibility to check if it exists.
#
# Outputs:
# - env:DKMLPARENTHOME_BUILDHOST
set_dkmlparenthomedir() {
if [ -n "${LOCALAPPDATA:-}" ]; then
DKMLPARENTHOME_BUILDHOST="$LOCALAPPDATA\\Programs\\DiskuvOCaml"
else
# shellcheck disable=SC2034
DKMLPARENTHOME_BUILDHOST="${XDG_DATA_HOME:-$HOME/.local/share}/diskuv-ocaml"
fi
}
# Detects Diskuv OCaml and sets its variables.
#
# If the environment variables already exist they are not overwritten.
# Setting these variables is useful for example _during_ a deployment, where the
# version of dkmlvars.sh in the filesystem is either pre-deployment (too old) or not present.
#
# Inputs:
# - env:DiskuvOCamlVarsVersion - optional
# - env:DiskuvOCamlHome - optional
# - env:DiskuvOCamlBinaryPaths - optional
# - env:DiskuvOCamlDeploymentId - optional
# - env:DiskuvOCamlVersion - optional
# - env:DiskuvOCamlMSYS2Dir - optional
# Outputs:
# - env:DKMLPARENTHOME_BUILDHOST
# - env:DKMLVERSION - set if Diskuv OCaml installed. The installed version number
# - env:DKMLHOME_BUILDHOST - set if Diskuv OCaml installed. Path will be in Windows (semicolon separated) or Unix (colon separated) format
# - env:DKMLHOME_UNIX - set if Diskuv OCaml installed. Path will be in Unix (colon separated) format
# - env:DKMLBINPATHS_BUILDHOST - set if Diskuv OCaml installed. Paths will be in Windows (semicolon separated) or Unix (colon separated) format
# - env:DKMLBINPATHS_UNIX - set if Diskuv OCaml installed. Paths will be in Unix (colon separated) format
# - env:DKMLMSYS2DIR_BUILDHOST - set if DiskuvO Caml installed MSYS2. Directory will be in Windows format
# Return Code:
# - 1 if DiskuvOCaml is not installed
autodetect_dkmlvars() {
autodetect_dkmlvars_DiskuvOCamlVarsVersion_Override=${DiskuvOCamlVarsVersion:-}
autodetect_dkmlvars_DiskuvOCamlHome_Override=${DiskuvOCamlHome:-}
autodetect_dkmlvars_DiskuvOCamlBinaryPaths_Override=${DiskuvOCamlBinaryPaths:-}
autodetect_dkmlvars_DiskuvOCamlDeploymentId_Override=${DiskuvOCamlDeploymentId:-}
autodetect_dkmlvars_DiskuvOCamlVersion_Override=${DiskuvOCamlVersion:-}
autodetect_dkmlvars_DiskuvOCamlMSYS2Dir_Override=${DiskuvOCamlMSYS2Dir:-}
set_dkmlparenthomedir
# Init output vars
DKMLVERSION=
DKMLHOME_UNIX=
DKMLHOME_BUILDHOST=
DKMLBINPATHS_UNIX=
DKMLBINPATHS_BUILDHOST=
DKMLMSYS2DIR_BUILDHOST=
if is_unixy_windows_build_machine; then
if [ -e "$DKMLPARENTHOME_BUILDHOST\\dkmlvars.sh" ]; then
if [ -x /usr/bin/cygpath ]; then
autodetect_dkmlvars_VARSSCRIPT=$(/usr/bin/cygpath -a "$DKMLPARENTHOME_BUILDHOST\\dkmlvars.sh")
# shellcheck disable=SC1090
. "$autodetect_dkmlvars_VARSSCRIPT"
else
# shellcheck disable=SC1090
. "$DKMLPARENTHOME_BUILDHOST\\dkmlvars.sh"
fi
fi
else
if [ -e "$DKMLPARENTHOME_BUILDHOST/dkmlvars.sh" ]; then
# shellcheck disable=SC1091
. "$DKMLPARENTHOME_BUILDHOST/dkmlvars.sh"
fi
fi
# Overrides
if [ -n "${autodetect_dkmlvars_DiskuvOCamlVarsVersion_Override:-}" ]; then DiskuvOCamlVarsVersion="$autodetect_dkmlvars_DiskuvOCamlVarsVersion_Override"; fi
if [ -n "${autodetect_dkmlvars_DiskuvOCamlHome_Override:-}" ]; then DiskuvOCamlHome="$autodetect_dkmlvars_DiskuvOCamlHome_Override"; fi
if [ -n "${autodetect_dkmlvars_DiskuvOCamlBinaryPaths_Override:-}" ]; then DiskuvOCamlBinaryPaths="$autodetect_dkmlvars_DiskuvOCamlBinaryPaths_Override"; fi
if [ -n "${autodetect_dkmlvars_DiskuvOCamlDeploymentId_Override:-}" ]; then DiskuvOCamlDeploymentId="$autodetect_dkmlvars_DiskuvOCamlDeploymentId_Override"; fi
if [ -n "${autodetect_dkmlvars_DiskuvOCamlVersion_Override:-}" ]; then DiskuvOCamlVersion="$autodetect_dkmlvars_DiskuvOCamlVersion_Override"; fi
if [ -n "${autodetect_dkmlvars_DiskuvOCamlMSYS2Dir_Override:-}" ]; then DiskuvOCamlMSYS2Dir="$autodetect_dkmlvars_DiskuvOCamlMSYS2Dir_Override"; fi
# Check if any vars are still unset (we do the Windows-specific DiskuvOCamlMSYS2Dir a bit later)
if [ -z "${DiskuvOCamlVarsVersion:-}" ]; then return 1; fi
if [ -z "${DiskuvOCamlHome:-}" ]; then return 1; fi
if [ -z "${DiskuvOCamlBinaryPaths:-}" ]; then return 1; fi
if [ -z "${DiskuvOCamlDeploymentId:-}" ]; then return 1; fi
if [ -z "${DiskuvOCamlVersion:-}" ]; then return 1; fi
# Validate DiskuvOCamlVarsVersion. Can be v1 or v2 since only the .sexp file changed in v2.
if [ ! "$DiskuvOCamlVarsVersion" = "1" ] && [ ! "$DiskuvOCamlVarsVersion" = "2" ]; then
printf "FATAL: Only able to read Diskuv OCaml variables version '1' and '2'. Instead Diskuv OCaml variables for %s were on version '%s'\n" "$DiskuvOCamlHome" "$DiskuvOCamlVarsVersion" >&2
exit 107
fi
# shellcheck disable=SC2034
DKMLVERSION="$DiskuvOCamlVersion"
# Unixize DiskuvOCamlHome
if [ -x /usr/bin/cygpath ]; then
DKMLHOME_UNIX=$(/usr/bin/cygpath -au "$DiskuvOCamlHome")
DKMLHOME_BUILDHOST=$(/usr/bin/cygpath -aw "$DiskuvOCamlHome")
else
DKMLHOME_UNIX="$DiskuvOCamlHome"
# shellcheck disable=SC2034
DKMLHOME_BUILDHOST="$DiskuvOCamlHome"
fi
# Unixize DiskuvOCamlMSYS2Dir
if is_unixy_windows_build_machine; then
if [ -z "${DiskuvOCamlMSYS2Dir:-}" ]; then return 1; fi
if [ -x /usr/bin/cygpath ]; then
DKMLMSYS2DIR_BUILDHOST=$(/usr/bin/cygpath -aw "$DiskuvOCamlMSYS2Dir")
else
DKMLMSYS2DIR_BUILDHOST="$DiskuvOCamlMSYS2Dir"
fi
else
# shellcheck disable=SC2034
DKMLMSYS2DIR_BUILDHOST=
fi
# Pathize DiskuvOCamlBinaryPaths
if [ -x /usr/bin/cygpath ]; then
# Going from Windows to Unix is safe. Going from Unix to Windows is safe.
# But Windows to Windows has garbled output from cygpath.
DKMLBINPATHS_UNIX=$(/usr/bin/cygpath --path "$DiskuvOCamlBinaryPaths")
DKMLBINPATHS_BUILDHOST=$(/usr/bin/cygpath -w --path "$DKMLBINPATHS_UNIX")
else
DKMLBINPATHS_UNIX="$DiskuvOCamlBinaryPaths"
# shellcheck disable=SC2034
DKMLBINPATHS_BUILDHOST="$DiskuvOCamlBinaryPaths"
fi
return 0
}
# Set OCAMLHOME and OPAMHOME if part of DKML system installation.
autodetect_ocaml_and_opam_home() {
# Set DKMLHOME_UNIX
autodetect_dkmlvars || true
# Set OCAMLHOME and OPAMHOME from DKMLHOME
OCAMLHOME=
OPAMHOME=
if [ -n "$DKMLHOME_UNIX" ]; then
if [ -x "$DKMLHOME_UNIX/usr/bin/ocaml" ] || [ -x "$DKMLHOME_UNIX/usr/bin/ocaml.exe" ]; then
OCAMLHOME=$DKMLHOME_UNIX
elif [ -x "$DKMLHOME_UNIX/bin/ocaml" ] || [ -x "$DKMLHOME_UNIX/bin/ocaml.exe" ]; then
# shellcheck disable=SC2034
OCAMLHOME=$DKMLHOME_UNIX
fi
if [ -x "$DKMLHOME_UNIX/usr/bin/opam" ] || [ -x "$DKMLHOME_UNIX/usr/bin/opam.exe" ]; then
OPAMHOME=$DKMLHOME_UNIX
elif [ -x "$DKMLHOME_UNIX/bin/opam" ] || [ -x "$DKMLHOME_UNIX/bin/opam.exe" ]; then
# shellcheck disable=SC2034
OPAMHOME=$DKMLHOME_UNIX
fi
fi
}
__autodetect_system_path_push_git() {
# Add Git at beginning of PATH
autodetect_system_path_GITEXE=$(command -v git || true)
if [ -n "$autodetect_system_path_GITEXE" ]; then
autodetect_system_path_GITDIR=$(PATH=/usr/bin:/bin dirname "$autodetect_system_path_GITEXE")
case "$autodetect_system_path_GITDIR" in
/usr/bin|/bin)
# __autodetect_system_path_push_usr_bin is responsible for
# /usr/bin and /bin
;;
*)
# Handle Scoop which places bash.exe in the same directory as
# git.exe.
# Confer: https://github.com/diskuv/dkml-installer-ocaml/issues/34
# Example:
# ~/scoop/shims/git.exe
# ~/scoop/shims/bash.exe
# but need the better directory:
# ~/scoop/apps/git/current/cmd/git.exe
# <no bash.exe!>
autodetect_system_path_GITGRANDDIR=$(PATH=/usr/bin:/bin dirname "$autodetect_system_path_GITDIR")
if [ -x "$autodetect_system_path_GITGRANDDIR/apps/git/current/cmd/git.exe" ]; then
autodetect_system_path_GITDIR="$autodetect_system_path_GITGRANDDIR/apps/git/current/cmd"
fi
# Add to DKML_SYSTEM_PATH
if [ -n "${DKML_SYSTEM_PATH:-}" ]; then
DKML_SYSTEM_PATH="$autodetect_system_path_GITDIR:$DKML_SYSTEM_PATH"
else
DKML_SYSTEM_PATH="$autodetect_system_path_GITDIR"
fi
esac
fi
}
__autodetect_system_path_push_usr_bin() {
__autodetect_system_path_push_usr_bin_PATH=
if is_cygwin_build_machine; then
__autodetect_system_path_push_usr_bin_PATH=/usr/bin:/bin
elif is_msys2_msys_build_machine; then
# /bin is a mount (essentially a symlink) to /usr/bin on MSYS2
__autodetect_system_path_push_usr_bin_PATH=/usr/bin
else
__autodetect_system_path_push_usr_bin_PATH=/usr/bin:/bin
fi
if [ -n "${DKML_SYSTEM_PATH:-}" ]; then
DKML_SYSTEM_PATH="$__autodetect_system_path_push_usr_bin_PATH:$DKML_SYSTEM_PATH"
else
DKML_SYSTEM_PATH="$__autodetect_system_path_push_usr_bin_PATH"
fi
}
__autodetect_system_path_helper() {
__autodetect_system_path_helper_ORDER=$1
shift
export DKML_SYSTEM_PATH
if [ -x /usr/bin/cygpath ]; then
autodetect_system_path_SYSDIR=$(/usr/bin/cygpath --sysdir)
autodetect_system_path_WINDIR=$(/usr/bin/cygpath --windir)
# folder 38 = C:\Program Files typically
autodetect_system_path_PROGRAMFILES=$(/usr/bin/cygpath --folder 38)
fi
if is_cygwin_build_machine || is_msys2_msys_build_machine; then
DKML_SYSTEM_PATH=$autodetect_system_path_PROGRAMFILES/PowerShell/7:$autodetect_system_path_SYSDIR:$autodetect_system_path_WINDIR:$autodetect_system_path_SYSDIR/Wbem:$autodetect_system_path_SYSDIR/WindowsPowerShell/v1.0:$autodetect_system_path_SYSDIR/OpenSSH
else
DKML_SYSTEM_PATH=
# RHEL has a Developer Toolset which should be in the path for things
# like the latest GCC compilers. It is used, for example, by dockcross Linux.
# Confer: https://access.redhat.com/documentation/en-us/red_hat_developer_toolset/12/html/12.0_release_notes/index
if [ -e /opt/rh ]; then
for __autodetect_system_path_helper_I in 19 18 17 16 15 14 13 12 11 10 9 8 7 6 4; do
if [ -d /opt/rh/devtoolset-$__autodetect_system_path_helper_I/root/usr/bin ]; then
DKML_SYSTEM_PATH=/opt/rh/devtoolset-$__autodetect_system_path_helper_I/root/usr/bin
break
fi
done
fi
fi
case "$__autodetect_system_path_helper_ORDER" in
USR_BIN_FIRST)
__autodetect_system_path_push_git
__autodetect_system_path_push_usr_bin
;;
*)
__autodetect_system_path_push_usr_bin
__autodetect_system_path_push_git
esac
# Set DKMLHOME_UNIX if available
autodetect_dkmlvars || true
# Add $DKMLHOME_UNIX/bin at beginning of PATH
if [ -n "${DKMLHOME_UNIX:-}" ] && [ -d "$DKMLHOME_UNIX/bin" ]; then
DKML_SYSTEM_PATH="$DKMLHOME_UNIX/bin:$DKML_SYSTEM_PATH"
fi
}
# Get a path that has system binaries, and Git, and nothing else.
#
# Purpose: Use whenever you have something meant to be reproducible.
#
# On Windows this includes the Cygwin/MSYS2 paths, the but also Windows directories
# like C:\Windows\System32 and C:\Windows\System32\OpenSSH and Powershell directories and
# also the essential binaries in $env:DiskuvOCamlHome\bin. The general binaries in $env:DiskuvOCamlHome\usr\bin are not
# included.
#
# Output:
# env:DKML_SYSTEM_PATH - A PATH containing only system directories like /usr/bin.
# The path will be in Unix format (so a path on Windows MSYS2 could be /c/Windows/System32)
autodetect_system_path() {
__autodetect_system_path_helper USR_BIN_FIRST
}
# Get a path that has system binaries, and Git, and nothing else.
#
# Purpose: Use whenever you have something meant to be reproducible.
# This function places Git before /usr/bin in the PATH. This is rarely a good idea
# because, on Windows, package managers like Chocolately and Scoop place
# git.exe and bash.exe in the same directory; that means their bash.exe can
# easily conflict with whatever shell (ex. MSYS2 bash) that runs this script.
#
# The only situation where it makes sense is if you know you must use the
# system git (ex. Git for Windows). In that situation you don't want any
# private git (ex. MSYS2 from Diskuv OCaml) to be used if present (that would
# happen if you used `autodetect_system_path`).
#
# On Windows this includes the Cygwin/MSYS2 paths, the but also Windows directories
# like C:\Windows\System32 and C:\Windows\System32\OpenSSH and Powershell directories and
# also the essential binaries in $env:DiskuvOCamlHome\bin. The general binaries in $env:DiskuvOCamlHome\usr\bin are not
# included.
#
# Output:
# env:DKML_SYSTEM_PATH - A PATH containing only system directories like /usr/bin.
# The path will be in Unix format (so a path on Windows MSYS2 could be /c/Windows/System32)
autodetect_system_path_with_git_before_usr_bin() {
__autodetect_system_path_helper GIT_FIRST
}
# Get standard locations of Unix system binaries like `/usr/bin/mv` (or `/bin/mv`).
#
# Will not return anything in `/usr/local/bin` or `/usr/sbin`. Use when you do not
# know whether the PATH has been set correctly, or when you do not know if the
# system binary exists.
#
# At some point in the future, this function will error out if the required system binaries
# do not exist. Most system binaries are common to all Unix/Linux/macOS installations but
# some (like `comm`) may need to be installed for proper functioning of DKML.
#
# Outputs:
# - env:DKMLSYS_MV - Location of `mv`
# - env:DKMLSYS_CHMOD - Location of `chmod`
# - env:DKMLSYS_UNAME - Location of `uname`
# - env:DKMLSYS_ENV - Location of `env`
# - env:DKMLSYS_AWK - Location of `awk`
# - env:DKMLSYS_SED - Location of `sed`
# - env:DKMLSYS_COMM - Location of `comm`
# - env:DKMLSYS_INSTALL - Location of `install`
# - env:DKMLSYS_RM - Location of `rm`
# - env:DKMLSYS_SORT - Location of `sort`
# - env:DKMLSYS_CAT - Location of `cat`
# - env:DKMLSYS_STAT - Location of `stat`
# - env:DKMLSYS_GREP - Location of `grep`
# - env:DKMLSYS_CURL - Location of `curl`
# - env:DKMLSYS_TR - Location of `tr`
autodetect_system_binaries() {
if [ -z "${DKMLSYS_MV:-}" ]; then
if [ -x /usr/bin/mv ]; then
DKMLSYS_MV=/usr/bin/mv
else
DKMLSYS_MV=/bin/mv
fi
fi
if [ -z "${DKMLSYS_CHMOD:-}" ]; then
if [ -x /usr/bin/chmod ]; then
DKMLSYS_CHMOD=/usr/bin/chmod
else
DKMLSYS_CHMOD=/bin/chmod
fi
fi
if [ -z "${DKMLSYS_UNAME:-}" ]; then
if [ -x /usr/bin/uname ]; then
DKMLSYS_UNAME=/usr/bin/uname
else
DKMLSYS_UNAME=/bin/uname
fi
fi
if [ -z "${DKMLSYS_ENV:-}" ]; then
if [ -x /usr/bin/env ]; then
DKMLSYS_ENV=/usr/bin/env
else
DKMLSYS_ENV=/bin/env
fi
fi
if [ -z "${DKMLSYS_AWK:-}" ]; then
if [ -x /usr/bin/awk ]; then
DKMLSYS_AWK=/usr/bin/awk
else
DKMLSYS_AWK=/bin/awk
fi
fi
if [ -z "${DKMLSYS_SED:-}" ]; then
if [ -x /usr/bin/sed ]; then
DKMLSYS_SED=/usr/bin/sed
else
DKMLSYS_SED=/bin/sed
fi
fi
if [ -z "${DKMLSYS_COMM:-}" ]; then
if [ -x /usr/bin/comm ]; then
DKMLSYS_COMM=/usr/bin/comm
else
DKMLSYS_COMM=/bin/comm
fi
fi
if [ -z "${DKMLSYS_INSTALL:-}" ]; then
if [ -x /usr/bin/install ]; then
DKMLSYS_INSTALL=/usr/bin/install
else
DKMLSYS_INSTALL=/bin/install
fi
fi
if [ -z "${DKMLSYS_RM:-}" ]; then
if [ -x /usr/bin/rm ]; then
DKMLSYS_RM=/usr/bin/rm
else
DKMLSYS_RM=/bin/rm
fi
fi
if [ -z "${DKMLSYS_SORT:-}" ]; then
if [ -x /usr/bin/sort ]; then
DKMLSYS_SORT=/usr/bin/sort
else
DKMLSYS_SORT=/bin/sort
fi
fi
if [ -z "${DKMLSYS_CAT:-}" ]; then
if [ -x /usr/bin/cat ]; then
DKMLSYS_CAT=/usr/bin/cat
else
DKMLSYS_CAT=/bin/cat
fi
fi
if [ -z "${DKMLSYS_STAT:-}" ]; then
if [ -x /usr/bin/stat ]; then
DKMLSYS_STAT=/usr/bin/stat
else
DKMLSYS_STAT=/bin/stat
fi
fi
if [ -z "${DKMLSYS_GREP:-}" ]; then
if [ -x /usr/bin/grep ]; then
DKMLSYS_GREP=/usr/bin/grep
else
DKMLSYS_GREP=/bin/grep
fi
fi
if [ -z "${DKMLSYS_CURL:-}" ]; then
if [ -x /usr/bin/curl ]; then
DKMLSYS_CURL=/usr/bin/curl
else
DKMLSYS_CURL=/bin/curl
fi
fi
if [ -z "${DKMLSYS_TR:-}" ]; then
if [ -x /usr/bin/tr ]; then
DKMLSYS_TR=/usr/bin/tr
else
DKMLSYS_TR=/bin/tr
fi
fi
export DKMLSYS_MV DKMLSYS_CHMOD DKMLSYS_UNAME DKMLSYS_ENV DKMLSYS_AWK DKMLSYS_SED DKMLSYS_COMM DKMLSYS_INSTALL
export DKMLSYS_RM DKMLSYS_SORT DKMLSYS_CAT DKMLSYS_STAT DKMLSYS_GREP DKMLSYS_CURL DKMLSYS_TR
}
# Is a Windows build machine if we are in a MSYS2 or Cygwin environment.
#
# Better alternatives
# -------------------
#
# 1. If you are checking to see if you should do a cygpath, then just guard it
# like so:
# if [ -x /usr/bin/cygpath ]; then
# do_something $(/usr/bin/cygpath ...) ...
# fi
# This clearly guards what you are about to do (cygpath) with what you will
# need (cygpath).
# 2. is_arg_windows_platform
is_unixy_windows_build_machine() {
if is_msys2_msys_build_machine || is_cygwin_build_machine; then
return 0
fi
return 1
}
# Is a MSYS2 environment with the MSYS or MINGW64 subsystem?
# * MSYS2 can also do MinGW 32-bit and 64-bit subsystems. Used by Diskuv OCaml
# * MINGW64 used by Git Bash (aka. GitHub Actions `shell: bash`)
# https://www.msys2.org/docs/environments/
is_msys2_msys_build_machine() {
if [ -e /usr/bin/msys-2.0.dll ] && {
[ "${MSYSTEM:-}" = "MSYS" ] || [ "${MSYSTEM:-}" = "MINGW64" ] || [ "${MSYSTEM:-}" = "UCRT64" ] || [ "${MSYSTEM:-}" = "CLANG64" ] || [ "${MSYSTEM:-}" = "MINGW32" ] || [ "${MSYSTEM:-}" = "CLANG32" ] || [ "${MSYSTEM:-}" = "CLANGARM64" ]
}; then
return 0
fi
return 1
}
is_cygwin_build_machine() {
if [ -e /usr/bin/cygwin1.dll ]; then
return 0
fi
return 1
}
# Inputs:
# - $1 - The PLATFORM
is_arg_windows_platform() {
case "$1" in
windows_x86) return 0;;
windows_x86_64) return 0;;
windows_arm32) return 0;;
windows_arm64) return 0;;
dev) if is_unixy_windows_build_machine; then return 0; else return 1; fi ;;
*) return 1;;
esac
}
# Linux and Android are Linux based platforms
# Inputs:
# - $1 - The PLATFORM
# Outputs:
# - BUILDHOST_ARCH
is_arg_linux_based_platform() {
autodetect_buildhost_arch
case "$1" in
linux_*) return 0;;
android_*) return 0;;
dev)
case "$BUILDHOST_ARCH" in
linux_*) return 0;;
android_*) return 0;;
*) return 1;;
esac
;;
*) return 1;;
esac
}
# macOS and iOS are Darwin based platforms
# Inputs:
# - $1 - The PLATFORM
# Outputs:
# - BUILDHOST_ARCH
is_arg_darwin_based_platform() {
autodetect_buildhost_arch
case "$1" in
darwin_*) return 0;;
dev)
case "$BUILDHOST_ARCH" in
darwin_*) return 0;;
*) return 1;;
esac
;;
*) return 1;;
esac
}
# Install files that will always be in a reproducible build.
#
# Inputs:
# env:DEPLOYDIR_UNIX - The deployment directory
# env:BOOTSTRAPNAME - Examples include: 110co
# env:DKMLDIR - The directory with .dkmlroot
install_reproducible_common() {
# Set DKMLSYS_*
autodetect_system_binaries
install_reproducible_common_BOOTSTRAPDIR=$DEPLOYDIR_UNIX/$SHARE_REPRODUCIBLE_BUILD_RELPATH/$BOOTSTRAPNAME
"$DKMLSYS_INSTALL" -d "$install_reproducible_common_BOOTSTRAPDIR"
install_reproducible_file .dkmlroot
install_reproducible_file vendor/drc/all/emptytop/dune-project
install_reproducible_file vendor/drc/unix/crossplatform-functions.sh
install_reproducible_file vendor/drc/unix/_common_tool.sh
}
# Install any non-common files that go into your reproducible build.
# All installed files will have the executable bit set.
#
# Inputs:
# env:DEPLOYDIR_UNIX - The deployment directory
# env:BOOTSTRAPNAME - Examples include: 110co
# env:DKMLDIR - The directory with .dkmlroot
# $1 - The path of the script that will be installed.
# It will be deployed relative to $DEPLOYDIR_UNIX and it
# must be specified as an existing relative path to $DKMLDIR.
install_reproducible_file() {
# Set DKMLSYS_*
autodetect_system_binaries
_install_reproducible_file_RELFILE="$1"
shift
_install_reproducible_file_RELDIR=$(dirname "$_install_reproducible_file_RELFILE")
_install_reproducible_file_BOOTSTRAPDIR=$DEPLOYDIR_UNIX/$SHARE_REPRODUCIBLE_BUILD_RELPATH/$BOOTSTRAPNAME
"$DKMLSYS_INSTALL" -d "$_install_reproducible_file_BOOTSTRAPDIR"/"$_install_reproducible_file_RELDIR"/
# When we rerun a setup script from within
# the reproducible target directory we may be installing on top of ourselves; that is, installing with
# the source and destination files being the same file.
# shellcheck disable=SC3013
if [ /dev/null -ef /dev/null ] 2>/dev/null; then
# This script accepts the -ef operator
if [ ! "$DKMLDIR"/"$_install_reproducible_file_RELFILE" -ef "$_install_reproducible_file_BOOTSTRAPDIR"/"$_install_reproducible_file_RELFILE" ]; then
"$DKMLSYS_INSTALL" "$DKMLDIR"/"$_install_reproducible_file_RELFILE" "$_install_reproducible_file_BOOTSTRAPDIR"/"$_install_reproducible_file_RELDIR"/
fi
else
# Sigh; portable scripts are not required to have a [ f1 -ef f2 ] operator. So we compare inodes (assuming `stat` supports `-c`)
install_reproducible_file_STAT1=$("$DKMLSYS_STAT" -c '%i' "$DKMLDIR"/"$_install_reproducible_file_RELFILE")
if [ -e "$_install_reproducible_file_BOOTSTRAPDIR"/"$_install_reproducible_file_RELFILE" ]; then
install_reproducible_file_STAT2=$("$DKMLSYS_STAT" -c '%i' "$_install_reproducible_file_BOOTSTRAPDIR"/"$_install_reproducible_file_RELFILE")
else
install_reproducible_file_STAT2=
fi
if [ ! "$install_reproducible_file_STAT1" = "$install_reproducible_file_STAT2" ]; then
"$DKMLSYS_INSTALL" "$DKMLDIR"/"$_install_reproducible_file_RELFILE" "$_install_reproducible_file_BOOTSTRAPDIR"/"$_install_reproducible_file_RELDIR"/
fi
fi
}
# Install any deterministically generated files that go into your
# reproducible build.
#
# Inputs:
# env:DEPLOYDIR_UNIX - The deployment directory
# env:BOOTSTRAPNAME - Examples include: 110co
# env:DKMLDIR - The directory with .dkmlroot
# $1 - The path to the generated script.
# $2 - The location of the script that will be installed.
# It must be specified relative to $DEPLOYDIR_UNIX.
install_reproducible_generated_file() {
# Set DKMLSYS_*
autodetect_system_binaries
install_reproducible_generated_file_SRCFILE="$1"
shift
install_reproducible_generated_file_RELFILE="$1"
shift
install_reproducible_generated_file_RELDIR=$(dirname "$install_reproducible_generated_file_RELFILE")
install_reproducible_generated_file_BOOTSTRAPDIR=$DEPLOYDIR_UNIX/$SHARE_REPRODUCIBLE_BUILD_RELPATH/$BOOTSTRAPNAME
"$DKMLSYS_INSTALL" -d "$install_reproducible_generated_file_BOOTSTRAPDIR"/"$install_reproducible_generated_file_RELDIR"/
"$DKMLSYS_RM" -f "$install_reproducible_generated_file_BOOTSTRAPDIR"/"$install_reproducible_generated_file_RELFILE" # ensure if exists it is a regular file or link but not a directory
"$DKMLSYS_INSTALL" "$install_reproducible_generated_file_SRCFILE" "$install_reproducible_generated_file_BOOTSTRAPDIR"/"$install_reproducible_generated_file_RELFILE"
}
# Install a README.md file that go into your reproducible build.
#
# The @@BOOTSTRAPDIR_UNIX@@ is a macro you can use inside the Markdown file
# which will be replaced with the relative path to the BOOTSTRAPNAME folder;
# it will have a trailing slash.
#
# Inputs:
# env:DEPLOYDIR_UNIX - The deployment directory
# env:BOOTSTRAPNAME - Examples include: 110co
# env:DKMLDIR - The directory with .dkmlroot
# $1 - The path of the .md file that will be installed.
# It will be deployed as 'README.md' in the bootstrap folder of $DEPLOYDIR_UNIX and it
# must be specified as an existing relative path to $DKMLDIR.
install_reproducible_readme() {
# Set DKMLSYS_*
autodetect_system_binaries
install_reproducible_readme_RELFILE="$1"
shift
# Needs to be in the standard location (so can be reproduced again)
install_reproducible_file "$install_reproducible_readme_RELFILE"
# Also place as a standalone README at the top of the reproducible tree
install_reproducible_readme_BOOTSTRAPDIR=$DEPLOYDIR_UNIX/$SHARE_REPRODUCIBLE_BUILD_RELPATH/$BOOTSTRAPNAME
"$DKMLSYS_INSTALL" -d "$install_reproducible_readme_BOOTSTRAPDIR"
"$DKMLSYS_SED" "s,@@BOOTSTRAPDIR_UNIX@@,$SHARE_REPRODUCIBLE_BUILD_RELPATH/$BOOTSTRAPNAME/,g" "$DKMLDIR"/"$install_reproducible_readme_RELFILE" > "$install_reproducible_readme_BOOTSTRAPDIR"/README.md
}
# Changes the suffix of a string and print to the standard output.
# change_suffix TEXT OLD_SUFFIX NEW_SUFFIX
#
# This function can handle old and suffixes containing:
# * A-Za-z0-9
# * commas (,)
# * dashes (-)
# * underscores (_)
# * periods (.)
# * ampersands (@)
#
# Other characters may work, but they are not officially supported by this function.
change_suffix() {
change_suffix_TEXT="$1"
shift
change_suffix_OLD_SUFFIX="$1"
shift
change_suffix_NEW_SUFFIX="$1"
shift
# Set DKMLSYS_*
autodetect_system_binaries
printf "%s" "$change_suffix_TEXT" | "$DKMLSYS_AWK" -v REPLACE="$change_suffix_NEW_SUFFIX" "{ gsub(/$change_suffix_OLD_SUFFIX/,REPLACE); print }"
}
# Replaces all occurrences of the search term with a replacement string, and print to the standard output.
# replace_all TEXT SEARCH REPLACE
#
# This function can handle SEARCH text containing:
# * A-Za-z0-9
# * commas (,)
# * dashes (-)
# * underscores (_)
# * periods (.)
# * ampersands (@)
#
# Other characters may work, but they are not officially supported by this function.
#
# Any characters can be used in TEXT and REPLACE.
replace_all() {
# Set DKMLSYS_*
autodetect_system_binaries
replace_all_TEXT="$1"
shift
replace_all_SEARCH="$1"
shift
replace_all_REPLACE="$1"
shift
replace_all_REPLACE=$(printf "%s" "$replace_all_REPLACE" | "$DKMLSYS_SED" 's#\\#\\\\#g') # escape all backslashes for awk
printf "%s" "$replace_all_TEXT" | "$DKMLSYS_AWK" -v REPLACE="$replace_all_REPLACE" "{ gsub(/$replace_all_SEARCH/,REPLACE); print }"
}
# Install a script that can re-install necessary system packages.
#
# Inputs:
# env:DEPLOYDIR_UNIX - The deployment directory
# env:BOOTSTRAPNAME - Examples include: 110co
# env:DKMLDIR - The directory with .dkmlroot
# env:DKML_REPRODUCIBLE_SYSTEM_BREWFILE - Optional Brewfile of all system packages. If the package manager
# is Homebrew, and this is specified, then instead of
# querying Homebrew with 'brew bundle dump' this Brewfile contains the result already.
# The file produced by 'brew bundle dump' is named 'Brewfile'.
# $1 - The path of the script that will be created, relative to $DEPLOYDIR_UNIX.
# Must end with `.sh`.
# $@ - All remaining arguments are how to invoke the run script ($1).
install_reproducible_system_packages() {
# Set DKMLSYS_*
autodetect_system_binaries
# Set BUILDHOST_ARCH
autodetect_buildhost_arch
install_reproducible_system_packages_SCRIPTFILE="$1"
shift
install_reproducible_system_packages_PACKAGEFILE=$(change_suffix "$install_reproducible_system_packages_SCRIPTFILE" .sh .packagelist.txt)
if [ "$install_reproducible_system_packages_PACKAGEFILE" = "$install_reproducible_system_packages_SCRIPTFILE" ]; then
printf "%s" "FATAL: The run script $install_reproducible_system_packages_SCRIPTFILE must end with .sh" >&2
exit 1
fi
install_reproducible_system_packages_SCRIPTDIR=$(dirname "$install_reproducible_system_packages_SCRIPTFILE")
install_reproducible_system_packages_BOOTSTRAPRELDIR=$SHARE_REPRODUCIBLE_BUILD_RELPATH/$BOOTSTRAPNAME
install_reproducible_system_packages_BOOTSTRAPDIR=$DEPLOYDIR_UNIX/$install_reproducible_system_packages_BOOTSTRAPRELDIR
"$DKMLSYS_INSTALL" -d "$install_reproducible_system_packages_BOOTSTRAPDIR"/"$install_reproducible_system_packages_SCRIPTDIR"/
if is_msys2_msys_build_machine && [ -x /git-bash.exe ]; then
# Git Bash
true > "$install_reproducible_system_packages_BOOTSTRAPDIR"/"$install_reproducible_system_packages_PACKAGEFILE"
printf "#!/bin/sh\necho Install Git for Windows from https://git-scm.com/download/win which gives you Git Bash. Git Bash should be used to run the remaining scripts\n" > "$install_reproducible_system_packages_BOOTSTRAPDIR"/"$install_reproducible_system_packages_SCRIPTFILE"
elif [ -x /usr/bin/pacman ] || [ -x /usr/sbin/pacman ]; then
# Works on MSYS2 (bin) and ArchLinux (sbin)
# https://wiki.archlinux.org/title/Pacman/Tips_and_tricks#List_of_installed_packages
if [ -x /usr/bin/pacman ]; then
pacmanexe=/usr/bin/pacman
else
pacmanexe=/usr/sbin/pacman
fi
"$pacmanexe" -Qqet > "$install_reproducible_system_packages_BOOTSTRAPDIR"/"$install_reproducible_system_packages_PACKAGEFILE"
printf "#!/bin/sh\nexec %s -S \"\$@\" --needed - < '%s'\n" "$pacmanexe" "$install_reproducible_system_packages_BOOTSTRAPRELDIR/$install_reproducible_system_packages_PACKAGEFILE" > "$install_reproducible_system_packages_BOOTSTRAPDIR"/"$install_reproducible_system_packages_SCRIPTFILE"
elif is_cygwin_build_machine; then
cygcheck.exe -c -d > "$install_reproducible_system_packages_BOOTSTRAPDIR"/"$install_reproducible_system_packages_PACKAGEFILE"
{
printf "%s\n" "#!/bin/sh"
printf "%s\n" "if [ ! -e /usr/local/bin/cyg-get ]; then wget -O /usr/local/bin/cyg-get 'https://gitlab.com/cogline.v3/cygwin/-/raw/2049faf4b565af81937d952292f8ae5008d38765/cyg-get?inline=false'; fi"
printf "%s\n" "if [ ! -x /usr/local/bin/cyg-get ]; then chmod +x /usr/local/bin/cyg-get; fi"
printf "readarray -t pkgs < <(awk 'display==1{print \$1} \$1==\"Package\"{display=1}' '%s')\n" "$install_reproducible_system_packages_BOOTSTRAPRELDIR/$install_reproducible_system_packages_PACKAGEFILE"
# shellcheck disable=SC2016
printf "%s\n" 'set -x ; /usr/local/bin/cyg-get install ${pkgs[@]}'
} > "$install_reproducible_system_packages_BOOTSTRAPDIR"/"$install_reproducible_system_packages_SCRIPTFILE"
elif is_arg_darwin_based_platform "$BUILDHOST_ARCH"; then
# Use a Brewfile.lock.json as the package manifest.
# However, when `brew` is not available (ex. Xcode runs CMake with a PATH that excludes homebrew) it is likely
# that no brew installed packages are available either. So if CMake succeeds then no brew commands were needed!
if command -v brew >/dev/null; then
# Brew exists and its installed packages can be used in the rest of the reproducible scripts.
if [ -n "${DKML_REPRODUCIBLE_SYSTEM_BREWFILE:-}" ] && [ -e "${DKML_REPRODUCIBLE_SYSTEM_BREWFILE}" ]; then
$DKMLSYS_INSTALL \
"$DKML_REPRODUCIBLE_SYSTEM_BREWFILE" \
"$install_reproducible_system_packages_BOOTSTRAPDIR/$install_reproducible_system_packages_PACKAGEFILE"
# For troubleshooting and a bit of security, place a comment saying the Brewfile was provided not queried
printf "\n# This Brewfile was provided to install_reproducible_system_packages() rather than queried.\n# It is possible that this Brewfile was out of date with the system Brew bottles and taps.\n" >> "$install_reproducible_system_packages_BOOTSTRAPDIR/$install_reproducible_system_packages_PACKAGEFILE"
else
install_reproducible_system_packages_OLDDIR=$PWD
if ! cd "$install_reproducible_system_packages_BOOTSTRAPDIR"/"$install_reproducible_system_packages_SCRIPTDIR"; then echo "FATAL: Could not cd to script directory" >&2; exit 107; fi
# Read-only Homebrew without any fancy interactive display
$DKMLSYS_ENV \
HOMEBREW_NO_AUTO_UPDATE=1 \
HOMEBREW_NO_ANALYTICS=1 \
HOMEBREW_NO_COLOR=1 \
HOMEBREW_NO_EMOJI=1 \
brew bundle dump --force # creates a Brewfile in current directory
if ! cd "$install_reproducible_system_packages_OLDDIR"; then echo "FATAL: Could not cd to old directory" >&2; exit 107; fi
$DKMLSYS_MV \
"$install_reproducible_system_packages_BOOTSTRAPDIR"/"$install_reproducible_system_packages_SCRIPTDIR"/Brewfile \
"$install_reproducible_system_packages_BOOTSTRAPDIR/$install_reproducible_system_packages_PACKAGEFILE"
fi
{
printf "%s\n" "#!/bin/sh"
printf "set -x ; brew bundle install --file '%s'\n" "$install_reproducible_system_packages_BOOTSTRAPRELDIR/$install_reproducible_system_packages_PACKAGEFILE"
} > "$install_reproducible_system_packages_BOOTSTRAPDIR"/"$install_reproducible_system_packages_SCRIPTFILE"
else
# Brew and its installed packages are not available in the rest of the reproducible scripts.
{
printf "%s\n" "#!/bin/sh"
printf "# Brew was not used so nothing to install\n"
printf "true\n"
} > "$install_reproducible_system_packages_BOOTSTRAPDIR"/"$install_reproducible_system_packages_SCRIPTFILE"
fi
elif [ -x /usr/bin/dpkg ]; then
# Debian/Ubuntu package restoration
/usr/bin/dpkg --get-selections > "$install_reproducible_system_packages_BOOTSTRAPDIR"/"$install_reproducible_system_packages_PACKAGEFILE"
{
# Technique from https://unix.stackexchange.com/questions/176134/installing-packages-by-importing-the-list-with-dpkg-set-selections
printf "#!/bin/sh\n"
printf "sudo apt-cache dumpavail | sudo dpkg --merge-avail\n"
printf "sudo dpkg --set-selections < '%s'\n" "$install_reproducible_system_packages_BOOTSTRAPRELDIR/$install_reproducible_system_packages_PACKAGEFILE"
printf 'sudo apt-get dselect-upgrade\n'
} > "$install_reproducible_system_packages_BOOTSTRAPDIR"/"$install_reproducible_system_packages_SCRIPTFILE"
elif [ -x /usr/bin/rpm ] && [ -x /usr/bin/zypper ]; then
# OpenSUSE packages
/usr/bin/rpm -qa > "$install_reproducible_system_packages_BOOTSTRAPDIR"/"$install_reproducible_system_packages_PACKAGEFILE"
{
printf "#!/bin/sh\n"
printf "sudo /usr/bin/zypper install "
awk 'NF==1 {printf "%s ", $1}' "$install_reproducible_system_packages_BOOTSTRAPDIR"/"$install_reproducible_system_packages_PACKAGEFILE"
printf '\n'
} > "$install_reproducible_system_packages_BOOTSTRAPDIR"/"$install_reproducible_system_packages_SCRIPTFILE"
# truncate package list, since already embedded in script
true > "$install_reproducible_system_packages_BOOTSTRAPDIR"/"$install_reproducible_system_packages_PACKAGEFILE"
elif [ -x /usr/bin/rpm ] && [ -x /usr/bin/dnf ]; then
# newer Oracle Linux / Fedora packages
/usr/bin/rpm -qa > "$install_reproducible_system_packages_BOOTSTRAPDIR"/"$install_reproducible_system_packages_PACKAGEFILE"
{
printf "#!/bin/sh\n"
printf "sudo /usr/bin/dnf install "
awk 'NF==1 {printf "%s ", $1}' "$install_reproducible_system_packages_BOOTSTRAPDIR"/"$install_reproducible_system_packages_PACKAGEFILE"
printf ' -y\n'
} > "$install_reproducible_system_packages_BOOTSTRAPDIR"/"$install_reproducible_system_packages_SCRIPTFILE"
# truncate package list, since already embedded in script
true > "$install_reproducible_system_packages_BOOTSTRAPDIR"/"$install_reproducible_system_packages_PACKAGEFILE"
elif [ -x /usr/bin/rpm ] && [ -x /usr/bin/yum ]; then
# older Fedora packages
/usr/bin/rpm -qa > "$install_reproducible_system_packages_BOOTSTRAPDIR"/"$install_reproducible_system_packages_PACKAGEFILE"
{
printf "#!/bin/sh\n"
printf "sudo /usr/bin/yum -y install "
awk 'NF==1 {printf "%s ", $1}' "$install_reproducible_system_packages_BOOTSTRAPDIR"/"$install_reproducible_system_packages_PACKAGEFILE"
printf '\n'
} > "$install_reproducible_system_packages_BOOTSTRAPDIR"/"$install_reproducible_system_packages_SCRIPTFILE"
# truncate package list, since already embedded in script
true > "$install_reproducible_system_packages_BOOTSTRAPDIR"/"$install_reproducible_system_packages_PACKAGEFILE"
elif [ -x /sbin/apk ]; then
# Alpine packages
/sbin/apk info > "$install_reproducible_system_packages_BOOTSTRAPDIR"/"$install_reproducible_system_packages_PACKAGEFILE"
{
printf "#!/bin/sh\n"
printf "sudo /sbin/apk add "
awk 'NF==1 {printf "%s ", $1}' "$install_reproducible_system_packages_BOOTSTRAPDIR"/"$install_reproducible_system_packages_PACKAGEFILE"
printf '\n'
} > "$install_reproducible_system_packages_BOOTSTRAPDIR"/"$install_reproducible_system_packages_SCRIPTFILE"
# truncate package list, since already embedded in script
true > "$install_reproducible_system_packages_BOOTSTRAPDIR"/"$install_reproducible_system_packages_PACKAGEFILE"
elif [ -n "${DEFAULT_DOCKCROSS_IMAGE:-}" ] || [ -e /dockcross ]; then
true > "$install_reproducible_system_packages_BOOTSTRAPDIR"/"$install_reproducible_system_packages_PACKAGEFILE"
printf "#!/bin/sh\necho Run from inside the %s Docker container\n" "${DEFAULT_DOCKCROSS_IMAGE:-dockcross}" > "$install_reproducible_system_packages_BOOTSTRAPDIR"/"$install_reproducible_system_packages_SCRIPTFILE"
elif [ -x /bin/busybox ]; then
# minimal Alpine (often a minimal Docker container)
true > "$install_reproducible_system_packages_BOOTSTRAPDIR"/"$install_reproducible_system_packages_PACKAGEFILE"
printf "#!/bin/sh\necho Install Busybox to run the remaining scripts\n" > "$install_reproducible_system_packages_BOOTSTRAPDIR"/"$install_reproducible_system_packages_SCRIPTFILE"
else
printf "%s\n" "TODO: unsupported install_reproducible_system_packages platform" >&2
exit 1
fi
"$DKMLSYS_CHMOD" 755 "$install_reproducible_system_packages_BOOTSTRAPDIR"/"$install_reproducible_system_packages_SCRIPTFILE"
}
# Install a script that can relaunch itself in a relocated position.
#
# Inputs:
# env:DEPLOYDIR_UNIX - The deployment directory
# env:BOOTSTRAPNAME - Examples include: 110co
# env:DKMLDIR - The directory with .dkmlroot
# $1 - The path of the pre-existing script that should be run.
# It will be deployed relative to $DEPLOYDIR_UNIX and it
# must be specified as an existing relative path to $DKMLDIR.
# Must end with `.sh`.
# $@ - All remaining arguments are how to invoke the run script ($1).
install_reproducible_script_with_args() {
# Set DKMLSYS_*
autodetect_system_binaries
install_reproducible_script_with_args_SCRIPTFILE="$1"
shift
install_reproducible_script_with_args_RECREATEFILE=$(change_suffix "$install_reproducible_script_with_args_SCRIPTFILE" .sh -noargs.sh)
if [ "$install_reproducible_script_with_args_RECREATEFILE" = "$install_reproducible_script_with_args_SCRIPTFILE" ]; then
printf "%s\n" "FATAL: The run script $install_reproducible_script_with_args_SCRIPTFILE must end with .sh" >&2
exit 1
fi
install_reproducible_script_with_args_RECREATEDIR=$(dirname "$install_reproducible_script_with_args_SCRIPTFILE")
install_reproducible_script_with_args_BOOTSTRAPRELDIR=$SHARE_REPRODUCIBLE_BUILD_RELPATH/$BOOTSTRAPNAME
install_reproducible_script_with_args_BOOTSTRAPDIR=$DEPLOYDIR_UNIX/$install_reproducible_script_with_args_BOOTSTRAPRELDIR
install_reproducible_file "$install_reproducible_script_with_args_SCRIPTFILE"
"$DKMLSYS_INSTALL" -d "$install_reproducible_script_with_args_BOOTSTRAPDIR"/"$install_reproducible_script_with_args_RECREATEDIR"/
{
printf "#!/bin/sh\n"
printf "set -euf\n"
# shellcheck disable=SC2016
printf 'if [ "${DKML_BUILD_TRACE:-}" = ON ] && [ "${DKML_BUILD_TRACE_LEVEL:-0}" -ge 2 ]; then\n'
printf " exec env TOPDIR=\"\$PWD/%s/vendor/drc/all/emptytop\" bash -x %s " \
"$install_reproducible_script_with_args_BOOTSTRAPRELDIR" \
"$install_reproducible_script_with_args_BOOTSTRAPRELDIR/$install_reproducible_script_with_args_SCRIPTFILE"
escape_args_for_shell "$@"
printf "\n"
printf "else\n"
printf " exec env TOPDIR=\"\$PWD/%s/vendor/drc/all/emptytop\" %s " \
"$install_reproducible_script_with_args_BOOTSTRAPRELDIR" \
"$install_reproducible_script_with_args_BOOTSTRAPRELDIR/$install_reproducible_script_with_args_SCRIPTFILE"
escape_args_for_shell "$@"
printf "\n"
printf "fi\n"
} > "$install_reproducible_script_with_args_BOOTSTRAPDIR"/"$install_reproducible_script_with_args_RECREATEFILE"
"$DKMLSYS_CHMOD" 755 "$install_reproducible_script_with_args_BOOTSTRAPDIR"/"$install_reproducible_script_with_args_RECREATEFILE"
}
# Tries to find the ARCH (defined in TOPDIR/Makefile corresponding to the build machine).
# ARCH is also called the PLATFORM.
# For now only tested in Linux/Windows x86/x86_64 and Apple x86_64/arm64.
#
# This function uses `uname` probing which sometimes is often inaccurate
# during cross-compilation.
#
# Outputs:
# - env:BUILDHOST_ARCH will contain the correct ARCH
autodetect_buildhost_arch() {
# Set DKMLSYS_*
autodetect_system_binaries
autodetect_buildhost_arch_SYSTEM=$("$DKMLSYS_UNAME" -s)
autodetect_buildhost_arch_MACHINE=$("$DKMLSYS_UNAME" -m)
# list from https://en.wikipedia.org/wiki/Uname and https://stackoverflow.com/questions/45125516/possible-values-for-uname-m
case "${autodetect_buildhost_arch_SYSTEM}-${autodetect_buildhost_arch_MACHINE}" in