aboutsummaryrefslogtreecommitdiff
path: root/reproduce/software/make/basic.mk
blob: 2c7401e5b5a6bcf46ba2f62ef50e9188b1256c78 (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
# Build the VERY BASIC project software before higher-level ones. Assuming
# minimal/generic Make and Shell.
#
# ------------------------------------------------------------------------
#                      !!!!! IMPORTANT NOTES !!!!!
#
# This Makefile will be run by the initial `./project configure' script. It
# is not included into the project afterwards.
#
# This Makefile builds low-level and basic tools that are necessary in any
# project like like GNU Tar, GNU Bash, GNU Make, GCC and etc. But before
# control reaches here, the 'configure.sh' script has already built the
# extremely low-level tools: Lzip (a compressing program), GNU Make (to be
# able to run this Makefile with a fixed version), Dash (a minimalist
# POSIX-compatible shell) and Flock (to allow locking files, and
# serializing when necessary: downloading during the software building
# phase). Thanks to GNU Make and Dash, we can assume a fixed structure in
# this Makefile. However, the 'PATH's in this Makefile still include the
# host's paths because we will be using the hosts tools (gradually
# decreasing) to build our own tools.
#
# ------------------------------------------------------------------------
#
# Copyright (C) 2018-2020 Mohammad Akhlaghi <mohammad@akhlaghi.org>
# Copyright (C) 2019-2020 Raul Infante-Sainz <infantesainz@gmail.com>
#
# This Makefile is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This Makefile is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this Makefile.  If not, see <http://www.gnu.org/licenses/>.

# Top level environment
include reproduce/software/config/LOCAL.conf
include reproduce/software/make/build-rules.mk
include reproduce/software/config/versions.conf
include reproduce/software/config/checksums.conf

# The optional URLs of software. Note that these may need the software
# version, so it is important that they be loaded after 'versions.conf'.
include reproduce/software/config/urls.conf

# Basic directories
lockdir = $(BDIR)/locks
tdir    = $(BDIR)/software/tarballs
ddir    = $(BDIR)/software/build-tmp
idir    = $(BDIR)/software/installed
ibdir   = $(BDIR)/software/installed/bin
ildir   = $(BDIR)/software/installed/lib
ibidir  = $(BDIR)/software/installed/version-info/proglib

# Ultimate Makefile target.
targets-proglib = low-level-links gcc-$(gcc-version)
all: $(foreach p, $(targets-proglib), $(ibidir)/$(p))

# Define the shell environment
# ----------------------------
#
# We build GNU Bash here in 'basic.mk'. So here we must must assume DASH
# shell that was built before calling this Makefile:
# http://gondor.apana.org.au/~herbert/dash. DASH is a minimalist POSIX
# shell, so it doesn't have startup options like '--noprofile --norc'. But
# from its manual, to load startup files, Dash actually requires that it be
# called with a '-' before it (for example '-dash'), so it shouldn't be
# loading any startup files if it was interpretted properly.
#
# As we build more programs, we want to use this project's built programs
# and libraries, not the host's, so in all PATH-related environments, our
# own build-directory comes first.
.ONESHELL:
.SHELLFLAGS := -e -c
export CCACHE_DISABLE := 1
export SHELL := $(ibdir)/dash
export PATH := $(ibdir):$(PATH)
export PKG_CONFIG_PATH := $(ildir)/pkgconfig
export PKG_CONFIG_LIBDIR := $(ildir)/pkgconfig
export CPPFLAGS := -I$(idir)/include $(CPPFLAGS)
export LDFLAGS := $(rpath_command) -L$(ildir) $(LDFLAGS)

# This is the "basic" tools where we are relying on the host operating
# system, but are slowly populating our basic software envirnoment. To run
# (system or template) programs, `LD_LIBRARY_PATH' is necessary, so here,
# we'll first tell the programs to look into any possible pre-defined
# `LD_LIBRARY_PATH', then we'll add our own newly installed libraries.  We
# will also make sure that there is no "current directory" in it (by
# removing a starting or trailing `:' and any occurance of `::'.
export LD_LIBRARY_PATH := $(shell echo $(LD_LIBRARY_PATH):$(ildir) \
                                  | sed -e's/::/:/g' -e's/^://' -e's/:$$//')

# RPATH is automatically written in macOS, so `DYLD_LIBRARY_PATH' is
# ultimately redundant. But on some systems, even having a single value
# causes crashs (see bug #56682). So we'll just give it no value at all.
export DYLD_LIBRARY_PATH :=

# Servers to use as backup. Maneage already has some fixed servers that can
# be used to download software tarballs. They are in a configuation
# file. But we give precedence to the "user" backup servers.
#
# One important "user" server (which the user doesn't actually give, but is
# found at configuration time in 'configure.sh') is Zenodo (see the
# description in 'configure.sh' for more on why this depends on
# configuration time).
#
# Afer putting everything together, we use the first server as the
# reference for all software if their '-url' variable isn't defined (in
# 'reproduce/software/config/urls.conf').
downloadwrapper = ./reproduce/analysis/bash/download-multi-try
maneage_backup_urls := $(shell awk '!/^#/{printf "%s ", $$1}' \
                               reproduce/software/config/servers-backup.conf)
backupservers_all = $(user_backup_urls) $(maneage_backup_urls)
topbackupserver = $(word 1, $(backupservers_all))
backupservers = $(filter-out $(topbackupserver),$(backupservers_all))










# Low-level (not built) programs
# ------------------------------
#
# For the time being, some components of the project aren't being built on
# some systems (primarily on proprietary operating systems). So we are
# simply making a symbolic link to the system's programs/libraries in the
# build directory.
#
# The logical position of this rule is irrelevant in this Makefile (because
# programs being built here have full access to the system's PATH
# already). This is done for the high-level programs installed in
# 'high-level.mk', 'xorg.mk' or 'python.mk'. So this step is done after
# building our own GNU Grep (which is the highest-level program used in
# 'makelink') to have trustable elements.
#
# About ccache: ccache acts like a wrapper over the C compiler and is made
# to avoid/speed-up compiling of identical files in a system (it is
# commonly used on large servers). It actually makes `gcc' or `g++' a
# symbolic link to itself so it can control them internally. So, for our
# purpose here, it is very annoying and can cause many complications. We
# thus remove any part of PATH of that has `ccache' in it before making
# symbolic links to the programs we are not building ourselves.
#
# The double quotations after the starting 'export PATH' are necessary in
# case the user's PATH has space-characters in it.
syspath := $(PATH)
makelink = origpath="$$PATH"; \
	export PATH="$$(echo $(syspath) \
	                     | tr : '\n' \
	                     | grep -v ccache \
	                     | tr '\n' :)"; \
	if type $(1) > /dev/null 2> /dev/null; then \
	  if [ x$(3) = x ]; then \
	    ln -sf "$$(which $(1))" $(ibdir)/$(1); \
	  else \
	    ln -sf "$$(which $(1))" $(ibdir)/$(3); \
	  fi; \
	else \
	  if [ "x$(strip $(2))" = xmandatory ]; then \
	    echo "'$(1)' is necessary for higher-level tools."; \
	    echo "Please install it for the configuration to continue."; \
	    exit 1; \
	  fi; \
	fi; \
	export PATH="$$origpath"

$(ibdir) $(ildir):; mkdir $@
$(ibidir)/low-level-links: $(ibidir)/grep-$(grep-version) \
                           | $(ibdir) $(ildir)

        # Not-installed (but necessary in some cases) compilers.
        #  Clang is necessary for CMake.
	$(call makelink,clang)
	$(call makelink,clang++)

        # Mac OS specific
	$(call makelink,mig)
	$(call makelink,xcrun)
	$(call makelink,sysctl)
	$(call makelink,sw_vers)
	$(call makelink,dsymutil)
	$(call makelink,install_name_tool)

        # On Mac OS, libtool is different compared to GNU Libtool. The
        # libtool we'll build in the high-level dependencies has the
        # executable name `glibtool'.
	$(call makelink,libtool)

        # Necessary libraries:
        #   Libdl (for dynamic loading libraries at runtime)
        #   POSIX Threads library for multi-threaded programs.
	for l in dl pthread; do
	  if [ -f /usr/lib/lib$$l.a ]; then
	    ln -sf /usr/lib/lib$$l.* $(ildir)/
	  fi
	done

        # We want this to be empty (so it doesn't interefere with the other
        # files in `ibidir'.
	touch $@










# Level 1 (MOST BASIC): Compression programs
# ------------------------------------------
#
# The first set of programs to be built are those that we need to unpack
# the source code tarballs of each program. We have already installed Lzip
# before calling 'basic.mk', so it is present and working. Hence we first
# build the Lzipped tarball of Gzip, then use our own Gzip to unpack the
# tarballs of the other compression programs. Once all the compression
# programs/libraries are complete, we build our own GNU Tar and continue
# with other software.
$(lockdir): | $(BDIR); mkdir $@
$(ibidir)/gzip-$(gzip-version): | $(ibdir) $(ildir) $(lockdir)
	tarball=gzip-$(gzip-version).tar.lz
	$(call import-source, $(gzip-url), $(gzip-checksum))
	$(call gbuild, gzip-$(gzip-version), static, , V=1)
	echo "GNU Gzip $(gzip-version)" > $@

$(ibidir)/xz-$(xz-version): $(ibidir)/gzip-$(gzip-version)
	tarball=xz-$(xz-version).tar.gz
	$(call import-source, $(xz-url), $(xz-checksum))
	$(call gbuild, xz-$(xz-version), static)
	echo "XZ Utils $(xz-version)" > $@

$(ibidir)/bzip2-$(bzip2-version): $(ibidir)/gzip-$(gzip-version)

        # Download the tarball.
	tarball=bzip2-$(bzip2-version).tar.gz
	$(call import-source, $(bzip2-url), $(bzip2-checksum))

        # Bzip2 doesn't have a `./configure' script, and its Makefile
        # doesn't build a shared library. So we can't use the `gbuild'
        # function here and we need to take some extra steps (inspired
        # from the GNU/Linux from Scratch (LFS) guide for Bzip2):
        #   1) The `sed' call is for relative installed symbolic links.
        #   2) The special Makefile-libbz2_so builds shared libraries.
        #
        # NOTE: the major version number appears in the final symbolic
        # link.
	tdir=bzip2-$(bzip2-version)
	if [ $(static_build) = yes ]; then
	  makecommand="make LDFLAGS=-static"
	  makeshared="echo no-shared"
	else
	  makecommand="make"
	  if [ x$(on_mac_os) = xyes ]; then
	    makeshared="echo no-shared"
	  else
	    makeshared="make -f Makefile-libbz2_so"
	  fi
	fi
	cd $(ddir)
	rm -rf $$tdir
	tar xf $(tdir)/$$tarball
	cd $$tdir
	sed -e 's@\(ln -s -f \)$$(PREFIX)/bin/@\1@' Makefile \
	    > Makefile.sed
	mv Makefile.sed Makefile
	$$makeshared CC=cc
	cp -a libbz2* $(ildir)/
	make clean
	$$makecommand CC=cc
	make install PREFIX=$(idir)
	cd ..
	rm -rf $$tdir
	cd $(ildir)
	ln -fs libbz2.so.1.0 libbz2.so
	echo "Bzip2 $(bzip2-version)" > $@

$(ibidir)/unzip-$(unzip-version): $(ibidir)/gzip-$(gzip-version)
	tarball=unzip-$(unzip-version).tar.gz
	v=$$(echo $(unzip-version) | sed -e's/\.//')
	$(call import-source, $(unzip-url), $(unzip-checksum))
	$(call gbuild, unzip$$v, static,, \
	               -f unix/Makefile generic \
	               CFLAGS="-DBIG_MEM -DMMAP",,pwd, \
	               -f unix/Makefile generic \
	               BINDIR=$(ibdir) MANDIR=$(idir)/man/man1 )
	echo "Unzip $(unzip-version)" > $@

$(ibidir)/zip-$(zip-version): $(ibidir)/gzip-$(gzip-version)
	tarball=zip-$(zip-version).tar.gz
	v=$$(echo $(zip-version) | sed -e's/\.//')
	$(call import-source, $(zip-url), $(zip-checksum))
	$(call gbuild, zip$$v, static,, \
	               -f unix/Makefile generic \
	               CFLAGS="-DBIG_MEM -DMMAP",,pwd, \
	               -f unix/Makefile generic \
	               BINDIR=$(ibdir) MANDIR=$(idir)/man/man1 )
	echo "Zip $(zip-version)" > $@

# Some programs (like Wget and CMake) that use zlib need it to be dynamic
# so they use our custom build. So we won't force a static-only build.
#
# Note for a static-only build: Zlib's `./configure' doesn't use Autoconf's
# configure script, it just accepts a direct `--static' option.
$(ibidir)/zlib-$(zlib-version): $(ibidir)/gzip-$(gzip-version)
	tarball=zlib-$(zlib-version).tar.gz
	$(call import-source, $(zlib-url), $(zlib-checksum))
	$(call gbuild, zlib-$(zlib-version))
	echo "Zlib $(zlib-version)" > $@

# GNU Tar: When built statically, tar gives a segmentation fault on
# unpacking Bash. So we'll build it dynamically. Note that technically, zip
# and unzip aren't dependencies of Tar, but for a clean build, we'll set
# Tar to be the last compression-related software (the first-set of
# software to be built).
$(ibidir)/tar-$(tar-version): \
              $(ibidir)/xz-$(xz-version) \
              $(ibidir)/zip-$(zip-version) \
              $(ibidir)/gzip-$(gzip-version) \
              $(ibidir)/zlib-$(zlib-version) \
              $(ibidir)/bzip2-$(bzip2-version) \
              $(ibidir)/unzip-$(unzip-version)
        # Since all later programs depend on Tar, the configuration will be
        # stuck here, only making Tar. So its more efficient to built it on
        # multiple threads (when the user's Make doesn't pass down the
        # number of threads).
	tarball=tar-$(tar-version).tar.gz
	$(call import-source, $(tar-url), $(tar-checksum))
	$(call gbuild, tar-$(tar-version), , , -j$(numthreads) V=1)
	echo "GNU Tar $(tar-version)" > $@










# Level 2 (necessary for linking)
#
# Patchelf is necessary for some software on GNU/Linux systems, its job is
# to manually insert RPATH into the dynamically-linked executable. Since
# all the other software depend on Pathelf, to avoid manually repeating as
# a prerequisite (and forgetting in others causing bugs), we'll put it as a
# dependancy of 'tar'.
$(ibidir)/patchelf-$(patchelf-version): $(ibidir)/tar-$(tar-version)
	tarball=patchelf-$(patchelf-version).tar.gz
	$(call import-source, $(patchelf-url), $(patchelf-checksum))
	if [ x$(on_mac_os) = xyes ]; then
	  echo "" > $@
	else
	  $(call gbuild, patchelf-$(patchelf-version))
	  echo "PatchELF $(patchelf-version)" > $@
	fi










# Level 3 (THIRD MOST BASIC): Bash
# --------------------------------
#
# GNU Make and GNU Bash are the second layer that we'll need to build the
# basic dependencies.
#
# Unfortunately Make needs dynamic linking in two instances: when loading
# objects (dynamically linked libraries), or when using the `getpwnam'
# function (for tilde expansion). The first can be disabled with
# `--disable-load', but unfortunately I don't know any way to fix the
# second. So, we'll have to build it dynamically for now.
$(ibidir)/ncurses-$(ncurses-version): $(ibidir)/patchelf-$(patchelf-version)
	tarball=ncurses-$(ncurses-version).tar.gz
	$(call import-source, $(ncurses-url), $(ncurses-checksum))

        # Delete the library that will be installed (so we can make sure
        # the build process completed afterwards and reset the links).
	rm -f $(ildir)/libncursesw*

        # Delete the (possibly existing) low-level programs that depend on
        # `readline', and thus `ncurses'. Since these programs are actually
        # used during the building of `ncurses', we need to delete them so
        # the build process doesn't use the project's Bash and AWK, but the
        # host's.
	rm -f $(ibdir)/bash* $(ibdir)/awk* $(ibdir)/gawk*

        # Standard build process.
	$(call gbuild, ncurses-$(ncurses-version), static, \
	               --with-shared --enable-rpath --without-normal \
	               --without-debug --with-cxx-binding \
	               --with-cxx-shared --enable-widec --enable-pc-files \
	               --with-pkg-config=$(ildir)/pkgconfig, -j$(numthreads))

        # Unfortunately there are many problems with `ncurses' using
        # "normal" (or 8-bit) characters. The standard way that will work
        # is to build it with wide character mode as you see above in the
        # configuration (or the `w' prefix you see below). Also, most
        # programs (and in particular Bash and AWK), first look for other
        # (mostly obsolete) libraries like tinfo, which define the same
        # symbols. The links below address both situations: we need to fool
        # higher-level packages to find this library even if they aren't
        # explicitly mentioning its name correctly (as a value to `-l' at
        # link time in their configure scripts).
        #
        # This part is taken from the Arch GNU/Linux build script[1], then
        # extended to Mac thanks to Homebrew's script [2].
        #
        # [1] https://git.archlinux.org/svntogit/packages.git/tree/trunk/PKGBUILD?h=packages/ncurses
        # [2] https://github.com/Homebrew/homebrew-core/blob/master/Formula/ncurses.rb
        #
        # Since we can't have comments, in the connected script, here is a
        # summary:
        #
        #   1. We find the actual suffix of the library, from the file that
        #      is not a symbolic link (starting with `-' in the output of
        #      `ls -l').
        #
        #   2. We make symbolic links to all the "ncurses", "ncurses++",
        #      "form", "panel" and "menu" libraries to point to their
        #      "wide" (character) library.
        #
        #   3. We make symbolic links to the "tic" and "tinfo" libraries to
        #      point to the same `libncursesw' library.
        #
        #   4. Some programs link with "curses" (not "ncurses", notice the
        #      starting "n"), so we'll also make links for these to point
        #      to the `libncursesw' library.
        #
        #   5. A link is made to also be able to include files from the
        #      `ncurses' headers.
	if [ x$(on_mac_os) = xyes ]; then so="dylib"; else so="so"; fi
	if [ -f $(ildir)/libncursesw.$$so ]; then

	  sov=$$(ls -l $(ildir)/libncursesw* \
	               | awk '/^-/{print $$NF}' \
	               | sed -e's|'$(ildir)/libncursesw.'||')

	  cd "$(ildir)"
	  for lib in ncurses ncurses++ form panel menu; do
	    ln -fs lib$$lib"w".$$sov     lib$$lib.$$so
	    ln -fs $(ildir)/pkgconfig/"$$lib"w.pc pkgconfig/$$lib.pc
	  done
	  for lib in tic tinfo; do
	    ln -fs libncursesw.$$sov     lib$$lib.$$so
	    ln -fs libncursesw.$$sov     lib$$lib.$$sov
	    ln -fs $(ildir)/pkgconfig/ncursesw.pc pkgconfig/$$lib.pc
	  done
	  ln -fs libncursesw.$$sov libcurses.$$so
	  ln -fs libncursesw.$$sov libcursesw.$$sov
	  ln -fs $(ildir)/pkgconfig/ncursesw.pc pkgconfig/curses.pc
	  ln -fs $(ildir)/pkgconfig/ncursesw.pc pkgconfig/cursesw.pc

	  ln -fs $(idir)/include/ncursesw $(idir)/include/ncurses
	  echo "GNU NCURSES $(ncurses-version)" > $@
	else
	  exit 1
	fi

$(ibidir)/readline-$(readline-version): \
                   $(ibidir)/ncurses-$(ncurses-version)
	tarball=readline-$(readline-version).tar.gz
	$(call import-source, $(readline-url), $(readline-checksum))
	$(call gbuild, readline-$(readline-version), static, \
	               --with-curses --disable-install-examples, \
	               SHLIB_LIBS="-lncursesw" -j$(numthreads))
	echo "GNU Readline $(readline-version)" > $@


# IMPORTANT: Even though we have enabled `rpath', Bash doesn't write the
# absolute adddress of the libraries it depends on! Therefore, if we
# configure Bash with `--with-installed-readline' (so the installed version
# of Readline, that we build below as a prerequisite or AWK, is used) and
# you run `ldd $(ibdir)/bash' on the resulting binary, it will say that it
# is linking with the system's `readline'. But if you run that same command
# within a rule in this project, you'll see that it is indeed linking with
# our own built readline.
#
# Unfortunately Bash doesn't maintain a Git repository and minor fixes are
# released as patches. Therefore we'll need to make our own fully-working
# and updated tarball to build the proper version of Bash. You download and
# apply them to the original tarball and make a new one with the following
# series of commands (just replace `NUMBER' with the total number of
# patches that you want to apply).
#
#   $ number=NUMBER
#   $ tar xf bash-5.0.tar.gz
#   $ cd bash-5.0
#   $ for i in $(seq 1 $number); do \
#       pname=bash50-$(printf "%03d" $i); \
#       wget http://ftp.gnu.org/gnu/bash/bash-5.0-patches/$pname -O ../$pname;\
#       patch -p0 -i ../$pname; \
#     done
#   $ cd ..
#   $ mv bash-5.0 bash-5.0.$number
#   $ tar cf bash-5.0.$number.tar bash-5.0.$number
#   $ lzip --best bash-5.0.$number.tar
#   $ rm -rf bash50-* bash-5.0.$number bash-5.0.tar.gz
$(ibidir)/bash-$(bash-version): \
               $(ibidir)/gettext-$(gettext-version) \
               $(ibidir)/readline-$(readline-version)

        # Download the tarball.
	tarball=bash-$(bash-version).tar.lz
	$(call import-source, $(bash-url), $(bash-checksum))

        # Delete the (possibly) existing Bash executable in the project,
        # let it use the default shell of the host.
	rm -f $(ibdir)/bash

        # Bash has many `--enable' features which are already enabled by
        # default. As described in the manual, they are mainly useful when
        # you disable them all with `--enable-minimal-config' and enable a
        # subset using the `--enable' options.
	if [ "x$(static_build)" = xyes ]; then stopt="--enable-static-link"
	else                                   stopt=""
	fi;
	export CFLAGS="$$CFLAGS \
	               -DDEFAULT_PATH_VALUE='\"$(ibdir)\"' \
	               -DSTANDARD_UTILS_PATH='\"$(ibdir)\"'  \
	               -DSYS_BASHRC='\"$(BASH_ENV)\"' "
	$(call gbuild, bash-$(bash-version),, $$stopt \
	               --with-installed-readline=$(ildir) \
	               --with-curses=yes, \
	               -j$(numthreads))

        # Atleast on GNU/Linux systems, Bash doesn't include RPATH by
        # default. So, we have to manually include it, currently we are
        # only doing this on GNU/Linux systems (using the `patchelf'
        # program).
	if [ -f $(ibdir)/patchelf ]; then
	  $(ibdir)/patchelf --set-rpath $(ildir) $(ibdir)/bash;
	fi

        # To be generic, some systems use the `sh' command to call the
        # shell. By convention, `sh' is just a symbolic link to the
        # preferred shell executable. So we'll define `$(ibdir)/sh' as a
        # symbolic link to the Bash that we just built and installed.
        #
        # Just to be sure that the installation step above went well,
        # before making the link, we'll see if the file actually exists
        # there.
	ln -fs $(ibdir)/bash $(ibdir)/sh
	echo "GNU Bash $(bash-version)" > $@





# The `-shared' flag will cause problems while building Perl on macOS, so
# we'll only use this configuration option when we are GNU/Linux
# systems. However, since the whole option must be used (which includes `='
# and empty space), its easier to define the variable as a Make variable
# outside the recipe, not as a shell variable inside it.
ifeq ($(on_mac_os),yes)
perl-conflddlflags =
else
perl-conflddlflags = -Dlddlflags="-shared $$LDFLAGS"
endif
$(ibidir)/perl-$(perl-version): $(ibidir)/patchelf-$(patchelf-version)
	tarball=perl-$(perl-version).tar.gz
	$(call import-source, $(perl-url), $(perl-checksum))
	major_version=$$(echo $(perl-version) \
	                     | sed -e's/\./ /g' \
	                     | awk '{printf("%d", $$1)}')
	base_version=$$(echo $(perl-version) \
	                     | sed -e's/\./ /g' \
	                     | awk '{printf("%d.%d", $$1, $$2)}')
	cd $(ddir)
	rm -rf perl-$(perl-version)
	tar xf $(tdir)/$$tarball
	cd perl-$(perl-version)
	./Configure -des \
	            -Dusethreads \
	            -Duseshrplib \
	            -Dprefix=$(idir) \
	            -Dvendorprefix=$(idir) \
	            -Dprivlib=$(idir)/share/perl$$major_version/core_perl \
	            -Darchlib=$(idir)/lib/perl$$major_version/$$base_version/core_perl \
	            -Dsitelib=$(idir)/share/perl$$major_version/site_perl \
	            -Dsitearch=$(idir)/lib/perl$$major_version/$$basever/site_perl \
	            -Dvendorlib=$(idir)/share/perl$$major_version/vendor_perl \
	            -Dvendorarch=$(idir)/lib/perl$$major_version/$$base_version/vendor_perl \
	            -Dscriptdir=$(idir)/bin/core_perl \
	            -Dsitescript=$(idir)/bin/site_perl \
	            -Dvendorscript=$(idir)/bin/vendor_perl \
	            -Dinc_version_list=none \
	            -Dman1ext=1perl \
	            -Dman3ext=3perl \
	            -Dcccdlflags='-fPIC' \
	            $(perl-conflddlflags) \
	            -Dldflags="$$LDFLAGS"
	make -j$(numthreads)
	make install
	cd ..
	rm -rf perl-$(perl-version)
	cd $$topdir
	echo "Perl $(perl-version)" > $@





# Coreutils
# ---------
#
# For some reason, Coreutils doesn't include `rpath' in its installed
# executables (even though it says that by default its included and that
# even when calling `--enable-rpath=yes'). So we have to manually add
# `rpath' to Coreutils' executables after the standard build is
# complete.
#
# One problem is that Coreutils installs many very basic executables which
# might be in used by other programs. So we must make sure that when
# Coreutils is being built, no other program is being built in
# parallel. The solution to the many executables it installs is to make a
# fake installation (with `DESTDIR'), and get a list of the contents of the
# directory to find the names.
#
# The echo after the PatchELF loop is to avoid a crash if the last
# file that PatchELF encounters is not usable (and it returns with
# an error).
#
# Coreutils uses Perl to create man pages!
$(ibidir)/coreutils-$(coreutils-version): \
                    $(ibidir)/bash-$(bash-version) \
                    $(ibidir)/perl-$(perl-version) \
                    $(ibidir)/openssl-$(openssl-version)

        # Import, unpack and enter the source directory.
	tarball=coreutils-$(coreutils-version).tar.xz
	$(call import-source, $(coreutils-url), $(coreutils-checksum))
	cd $(ddir)
	rm -rf coreutils-$(coreutils-version)
	tar xf $(tdir)/$$tarball
	cd coreutils-$(coreutils-version)

        # Set the configure script to use our shell, note that we can't
        # assume GNU SED here yet (it installs after Coreutils).
	sed -e's|\#\! /bin/sh|\#\! $(ibdir)/bash|' \
	    -e's|\#\!/bin/sh|\#\! $(ibdir)/bash|' \
	    configure > configure-tmp
	mv configure-tmp configure
	chmod +x configure

        # Configure, build and install Coreutils.
	./configure --prefix=$(idir) SHELL=$(ibdir)/bash  \
	            LDFLAGS="$(LDFLAGS)" CPPFLAGS="$(CPPFLAGS)" \
	            --disable-silent-rules --with-openssl=yes
	make SHELL=$(ibdir)/bash -j$(numthreads)
	make SHELL=$(ibdir)/bash install

        # Fix RPATH if necessary.
	if [ -f $(ibdir)/patchelf ]; then
	  make SHELL=$(ibdir)/bash install DESTDIR=junkinst
	  instprogs=$$(ls junkinst/$(ibdir))
	  for f in $$instprogs; do
	    $(ibdir)/patchelf --set-rpath $(ildir) $(ibdir)/$$f
	  done
	  echo "PatchELF applied to all programs."
	fi

        # Come back up to the unpacking directory, delete the source
        # directory and write the final target.
	cd ..
	rm -rf coreutils-$(coreutils-version)
	echo "GNU Coreutils $(coreutils-version)" > $@

# OpenSSL
#
# Until we find a nice and generic way to create an updated CA file in the
# project, the certificates will be available in a file for this project
# along with the other tarballs.
$(idir)/etc:; mkdir $@
$(idir)/etc/ssl: | $(idir)/etc; mkdir $@
$(ibidir)/openssl-$(openssl-version): $(ibidir)/perl-$(perl-version) \
                  | $(idir)/etc/ssl

        # First download the certificates and copy them into the
        # installation directory.
	tarball=cert.pem
	$(call import-source, $(cert-url), $(cert-checksum))
	cp $(tdir)/cert.pem $(idir)/etc/ssl/cert.pem

        # Now download the OpenSSL tarball.
	tarball=openssl-$(openssl-version).tar.gz
	$(call import-source, $(openssl-url), $(openssl-checksum))

        # According to OpenSSL's Wiki (link bellow), it can't automatically
        # detect Mac OS's structure. It will need some help. So we'll use
        # the `on_mac_os' Make variable that we defined in the configure
        # script and help it with some extra configuration options and an
        # environment variable.
        #
        # https://wiki.openssl.org/index.php/Compilation_and_Installation
	if [ x$(on_mac_os) = xyes ]; then
	  export KERNEL_BITS=64
	  copt="shared no-ssl2 no-ssl3 enable-ec_nistp_64_gcc_128"
	fi
	$(call gbuild, openssl-$(openssl-version), , \
	               zlib \
	               $$copt \
	               $(rpath_command) \
	               --openssldir=$(idir)/etc/ssl \
	               --with-zlib-lib=$(ildir) \
	               --with-zlib-include=$(idir)/include, \
	               -j$(numthreads), , ./config )

        # Manually insert RPATH inside the OpenSSL library.
	if [ -f $(ibdir)/patchelf ]; then
	   patchelf --set-rpath $(ildir) $(ildir)/libssl.so; \
	fi

        # Bug 58263 (https://savannah.nongnu.org/bugs/?58263): In OpenSSL
        # Version 1.1.1a (also checked in 1.1.1g), `openssl/ec.h' fails to
        # include `openssl/openconf.h' on some OSs. The SED hack below
        # inserts a hardwired element of `openssl/openconf.h' that is
        # needed to include sections of code `f` that are deprecated in
        # 1.2.0, but not yet in 1.1.1. This problem may be solved in
        # version 1.2.x, so please check again in that bug.
	mv -v $(idir)/include/openssl/ec.h $(idir)/include/openssl/ec.h.orig
	sed -e 's,\(# include .openssl/opensslconf\.h.\),\1\n#ifndef DEPRECATEDIN_1_2_0\n#define DEPRECATEDIN_1_2_0(f)   f;\n#endif\n,' \
	   $(idir)/include/openssl/ec.h.orig > $(idir)/include/openssl/ec.h

        # Build the final target.
	echo "OpenSSL $(openssl-version)" > $@




# Downloaders
# -----------

# cURL
#
# cURL can optionally link with many different network-related libraries on
# the host system that we are not yet building in the template. Many of
# these are not relevant to most science projects, so we are explicitly
# using `--without-XXX' or `--disable-XXX' so cURL doesn't link with
# them. Note that if it does link with them, the configuration will crash
# when the library is updated/changed by the host, and the whole purpose of
# this project is avoid dependency on the host as much as possible.
$(ibidir)/curl-$(curl-version): $(ibidir)/coreutils-$(coreutils-version)

	tarball=curl-$(curl-version).tar.gz
	$(call import-source, $(curl-url), $(curl-checksum))

	$(call gbuild, curl-$(curl-version), , \
	               LIBS="-pthread" \
	               --with-zlib=$(ildir) \
	               --with-ssl=$(idir) \
	               --without-mesalink \
	               --with-ca-fallback \
	               --without-librtmp \
	               --without-libidn2 \
	               --without-wolfssl \
	               --without-brotli \
	               --without-gnutls \
	               --without-cyassl \
	               --without-libpsl \
	               --without-axtls \
	               --disable-ldaps \
	               --disable-ldap \
	               --without-nss, V=1)

	if [ -f $(ibdir)/patchelf ]; then
	   $(ibdir)/patchelf --set-rpath $(ildir) $(ildir)/libcurl.so
	fi
	echo "cURL $(curl-version)" > $@


# GNU Wget
#
# Note that on some systems (for example GNU/Linux) Wget needs to explicity
# link with `libdl', but on others (for example Mac OS) it doesn't. We
# check this at configure time and define the `needs_ldl' variable.
#
# Also note that since Wget needs to load outside libraries dynamically, it
# gives a segmentation fault when built statically.
#
# There are many network related libraries that we are currently not
# building as part of this project. So to avoid too much dependency on the
# host system (especially a crash when these libraries are updated on the
# host), they are disabled here.
$(ibidir)/wget-$(wget-version): \
               $(ibidir)/libiconv-$(libiconv-version) \
               $(ibidir)/coreutils-$(coreutils-version)

        # Download the tarball.
	tarball=wget-$(wget-version).tar.lz
	$(call import-source, $(wget-url), $(wget-checksum))

        # We need to explicitly disable `libiconv', because of the
        # `pkg-config' and `libiconv' problem.
	libs="-pthread"
	if [ x$(needs_ldl) = xyes ]; then libs="$$libs -ldl"; fi
	$(call gbuild, wget-$(wget-version), , \
	               LIBS="$$LIBS $$libs" \
	               --with-libssl-prefix=$(idir) \
	               --without-libiconv-prefix \
	               --with-ssl=openssl \
	               --with-openssl=yes \
	               --without-metalink \
	               --without-libuuid \
	               --without-libpsl \
	               --without-libidn \
	               --disable-pcre2 \
	               --disable-pcre \
	               --disable-iri, V=1 -j$(numthreads))
	echo "GNU Wget $(wget-version)" > $@







# Basic command-line tools and their dependencies
# -----------------------------------------------
#
# These are basic programs which are commonly necessary in the build
# process of the higher-level programs and libraries. Note that during the
# building of those higher-level programs (after this Makefile finishes),
# there is no access to the system's PATH.
$(ibidir)/diffutils-$(diffutils-version): \
                    $(ibidir)/coreutils-$(coreutils-version)
	tarball=diffutils-$(diffutils-version).tar.xz
	$(call import-source, $(diffutils-url), $(diffutils-checksum))
	$(call gbuild, diffutils-$(diffutils-version), static,,V=1)
	echo "GNU Diffutils $(diffutils-version)" > $@

$(ibidir)/file-$(file-version): $(ibidir)/coreutils-$(coreutils-version)
	tarball=file-$(file-version).tar.gz
	$(call import-source, $(file-url), $(file-checksum))
	$(call gbuild, file-$(file-version), static,,V=1)
	echo "File $(file-version)" > $@

$(ibidir)/findutils-$(findutils-version): \
                    $(ibidir)/coreutils-$(coreutils-version)
	tarball=findutils-$(findutils-version).tar.xz
	$(call import-source, $(findutils-url), $(findutils-checksum))
	$(call gbuild, findutils-$(findutils-version), static,,V=1)
	echo "GNU Findutils $(findutils-version)" > $@

$(ibidir)/gawk-$(gawk-version): \
               $(ibidir)/gmp-$(gmp-version) \
               $(ibidir)/mpfr-$(mpfr-version) \
               $(ibidir)/coreutils-$(coreutils-version)

        # Download the tarball.
	tarball=gawk-$(gawk-version).tar.lz
	$(call import-source, $(gawk-url), $(gawk-checksum))

        # AWK doesn't include RPATH by default, so we'll have to manually
        # include it using the `patchelf' program (which was a dependency
        # of Bash). Just note that AWK produces two executables (for
        # example `gawk-4.2.1' and `gawk') and a symbolic link `awk' to one
        # of those executables.
	$(call gbuild, gawk-$(gawk-version), static, \
	               --with-readline=$(idir))

        # Correct the RPATH on systems that have installed patchelf.
	if [ -f $(ibdir)/patchelf ]; then
	  if [ -f $(ibdir)/gawk ]; then
	    $(ibdir)/patchelf --set-rpath $(ildir) $(ibdir)/gawk
	  fi
	  if [ -f $(ibdir)/gawk-$(gawk-version) ]; then
	    $(ibdir)/patchelf --set-rpath $(ildir) \
	                      $(ibdir)/gawk-$(gawk-version);
	  fi
	fi

        # Build final target.
	echo "GNU AWK $(gawk-version)" > $@

$(ibidir)/libiconv-$(libiconv-version): \
                   $(ibidir)/pkg-config-$(pkgconfig-version)
	tarball=libiconv-$(libiconv-version).tar.gz
	$(call import-source, $(libiconv-url), $(libiconv-checksum))
	$(call gbuild, libiconv-$(libiconv-version), static)
	echo "GNU libiconv $(libiconv-version)" > $@

$(ibidir)/libunistring-$(libunistring-version): \
                       $(ibidir)/libiconv-$(libiconv-version)
	tarball=libunistring-$(libunistring-version).tar.xz
	$(call import-source, $(libunistring-url), $(libunistring-checksum))
	$(call gbuild, libunistring-$(libunistring-version), static,, \
	               -j$(numthreads))
	echo "GNU libunistring $(libunistring-version)" > $@

$(ibidir)/libxml2-$(libxml2-version): $(ibidir)/patchelf-$(patchelf-version)
        # The libxml2 tarball also contains Python bindings which are built
        # and installed to a system directory by default. If you don't need
        # the Python bindings, the easiest solution is to compile without
        # Python support: `./configure --without-python'. If you really need
        # the Python bindings, use `--with-python-install-dir=DIR' instead.
	tarball=libxml2-$(libxml2-version).tar.gz
	$(call import-source, $(libxml2-url), $(libxml2-checksum))
	$(call gbuild, libxml2-$(libxml2-version), static, \
	               --without-python, V=1)
	echo "Libxml2 $(libxml2-version)" > $@

$(ibidir)/gettext-$(gettext-version): \
                  $(ibidir)/m4-$(m4-version) \
                  $(ibidir)/libxml2-$(libxml2-version) \
                  $(ibidir)/ncurses-$(ncurses-version) \
                  $(ibidir)/libiconv-$(libiconv-version) \
                  $(ibidir)/libunistring-$(libunistring-version)
	tarball=gettext-$(gettext-version).tar.xz
	$(call import-source, $(gettext-url), $(gettext-checksum))
	$(call gbuild, gettext-$(gettext-version), static,, \
	               V=1 -j$(numthreads))
	echo "GNU gettext $(gettext-version)" > $@

$(ibidir)/git-$(git-version): \
              $(ibidir)/curl-$(curl-version) \
              $(ibidir)/gettext-$(gettext-version) \
              $(ibidir)/libiconv-$(libiconv-version)
	tarball=git-$(git-version).tar.xz
	if [ x$(on_mac_os) = xyes ]; then
	  export LDFLAGS="$$LDFLAGS -lcharset"
	fi
	$(call import-source, $(git-url), $(git-checksum))
	$(call gbuild, git-$(git-version), static, \
	               --without-tcltk --with-shell=$(ibdir)/bash \
	               --with-iconv=$(idir), V=1 -j$(numthreads))
	echo "Git $(git-version)" > $@

$(ibidir)/gmp-$(gmp-version): \
              $(ibidir)/m4-$(m4-version) \
              $(ibidir)/coreutils-$(coreutils-version)
	tarball=gmp-$(gmp-version).tar.lz
	$(call import-source, $(gmp-url), $(gmp-checksum))
	$(call gbuild, gmp-$(gmp-version), static, \
	               --enable-cxx --enable-fat, \
	               -j$(numthreads) ,make check)
	echo "GNU Multiple Precision Arithmetic Library $(gmp-version)" > $@

# On Mac OS, libtool does different things, so to avoid confusion, we'll
# prefix GNU's libtool executables with `glibtool'.
$(ibidir)/libtool-$(libtool-version): $(ibidir)/m4-$(m4-version)
	tarball=libtool-$(libtool-version).tar.xz
	$(call import-source, $(libtool-url), $(libtool-checksum))
	$(call gbuild, libtool-$(libtool-version), static, \
                       --program-prefix=g, V=1 -j$(numthreads))
	ln -s $(ibdir)/glibtoolize $(ibdir)/libtoolize
	echo "GNU Libtool $(libtool-version)" > $@

$(ibidir)/grep-$(grep-version): $(ibidir)/coreutils-$(coreutils-version)
	tarball=grep-$(grep-version).tar.xz
	$(call import-source, $(grep-url), $(grep-checksum))
	$(call gbuild, grep-$(grep-version), static,,V=1)
	echo "GNU Grep $(grep-version)" > $@

$(ibidir)/libbsd-$(libbsd-version): $(ibidir)/coreutils-$(coreutils-version)
	tarball=libbsd-$(libbsd-version).tar.xz
	$(call import-source, $(libbsd-url), $(libbsd-checksum))
	if [ x$(on_mac_os) = xyes ]; then
	  echo "" > $@
	else
	  $(call gbuild, libbsd-$(libbsd-version), static,,V=1)
	  echo "Libbsd $(libbsd-version)" > $@
	fi

# We need to apply a patch to the M4 source to be used properly on macOS.
# The patch [1] was inspired by Homebrew's build instructions [1].
#
# [1] https://raw.githubusercontent.com/macports/macports-ports/edf0ee1e2cf/devel/m4/files/secure_snprintf.patch
# [2] https://github.com/Homebrew/homebrew-core/blob/master/Formula/m4.rb
$(ibidir)/m4-$(m4-version): $(ibidir)/texinfo-$(texinfo-version)
	tarball=m4-$(m4-version).tar.gz
	$(call import-source, $(m4-url), $(m4-checksum))
	cd $(ddir)
	unpackdir=m4-$(m4-version)
	rm -rf $$unpackdir
	tar xf $(tdir)/$$tarball
	mv m4-* $$unpackdir
	cd $$unpackdir
	if [ x$(on_mac_os) = xyes ]; then
	   sed 's|if !(((__GLIBC__ > 2|if !defined(__APPLE__) \&\& !(((__GLIBC__ > 2|' \
	       lib/vasnprintf.c > lib/vasnprintf_edited.c
	   mv lib/vasnprintf_edited.c lib/vasnprintf.c
	fi
	./configure --prefix=$(idir) LDFLAGS="$(LDFLAGS)" \
	            CPPFLAGS="$(CPPFLAGS)"
	make V=1 -j$(numthreads)
	make V=1 install
	cd ..
	rm -rf $$unpackdir
	echo "GNU M4 $(m4-version)" > $@

# Metastore is used (through a Git hook) to restore the source modification
# dates of files after a Git checkout. Another Git hook saves all file
# metadata just before a commit (to allow restoration after a
# checkout). Since this project is managed in Makefiles, file modification
# dates are critical to not having to redo the whole analysis after
# checking out between branches.
#
# Note that we aren't using the standard version of Metastore, but a fork
# of it that is maintained in this repository:
#    https://gitlab.com/makhlaghi/metastore-fork
#
# Note that the prerequisites `coreutils', `gawk' and `sed' are not
# metastore oficial dependencies, but they are necessaries to run our steps
# before and after the installation.
#
# Libbsd is not necessary on macOS systems, because macOS is already a
# BSD-based distribution. But on GNU/Linux systems, it is necessary.
$(ibidir)/metastore-$(metastore-version): \
                    $(ibidir)/sed-$(sed-version) \
                    $(ibidir)/git-$(git-version) \
                    $(ibidir)/gawk-$(gawk-version) \
                    $(ibidir)/libbsd-$(libbsd-version) \
                    $(ibidir)/coreutils-$(coreutils-version)

        # Download the tarball.
	tarball=metastore-$(metastore-version).tar.gz
	$(call import-source, $(metastore-url), $(metastore-checksum))

        # Metastore doesn't have any `./configure' script. So we'll just
        # call `pwd' as a place-holder for the `./configure' command.
        #
        # File attributes are also not available on some systems, since the
        # main purpose here is modification dates (and not attributes),
        # we'll also set the `NO_XATTR' flag.
        #
        # After installing Metastore, write the relevant hooks into this
        # system's Git hooks, while setting the system-specific
        # directories/files.
        #
        # Note that the metastore -O and -G options used in this template
        # are currently only available in a fork of `metastore' hosted at:
        # https://github.com/mohammad-akhlaghi/metastore
        #
        # Checking for presence of `.git'. When the project source is
        # downloaded from a non-Git source (for example from arXiv), there
        # is no `.git' directory to work with. So until we find a better
        # solution, avoid the step to to add the Git hooks.
	current_dir=$$(pwd); \
	$(call gbuild, metastore-$(metastore-version), static,, \
	               NO_XATTR=1 V=1,,pwd,PREFIX=$(idir))

        # Correct RPATH when necessary.
	if [ -f $(ibdir)/patchelf ]; then
	  $(ibdir)/patchelf --set-rpath $(ildir) $(ibdir)/metastore
	fi

        # If this project is being built in a directory version controlled
        # by Git, copy the hooks into the Git configuation.
	if [ -f $(ibdir)/metastore ]; then
	  if [ -d .git ]; then
	    user=$$(whoami)
	    group=$$(groups | awk '{print $$1}')
	    cd $$current_dir
	    for f in pre-commit post-checkout; do
	       sed -e's|@USER[@]|'$$user'|g' \
	           -e's|@GROUP[@]|'$$group'|g' \
	           -e's|@BINDIR[@]|$(ibdir)|g' \
	           -e's|@TOP_PROJECT_DIR[@]|'$$current_dir'|g' \
	           reproduce/software/shell/git-$$f > .git/hooks/$$f
	       chmod +x .git/hooks/$$f
	    done
	  fi
	  echo "Metastore (forked) $(metastore-version)" > $@
	else
	  echo; echo; echo
	  echo "*****************"
	  echo "metastore couldn't be installed!"
	  echo
	  echo "Its used for preserving timestamps on Git commits."
	  echo "Its useful for development, not simple running of "
	  echo "the project. So we won't stop the configuration "
	  echo "because it wasn't built."
	  echo "*****************"
	  echo "" > $@
	fi

$(ibidir)/mpfr-$(mpfr-version): $(ibidir)/gmp-$(gmp-version)
	tarball=mpfr-$(mpfr-version).tar.xz
	$(call import-source, $(mpfr-url), $(mpfr-checksum))
	$(call gbuild, mpfr-$(mpfr-version), static, , , make check)
	echo "GNU Multiple Precision Floating-Point Reliably $(mpfr-version)" > $@

$(ibidir)/pkg-config-$(pkgconfig-version): $(ibidir)/patchelf-$(patchelf-version)

        # Download the tarball.
	tarball=pkg-config-$(pkgconfig-version).tar.gz
	$(call import-source, $(pkgconfig-url), $(pkgconfig-checksum))

        # An existing `libiconv' can cause a conflict with `pkg-config',
        # this is why `libiconv' depends on `pkg-config'. On a clean build,
        # `pkg-config' is built first. But when we don't have a clean build
        # (and `libiconv' exists) there will be a problem. So before
        # re-building `pkg-config', we'll remove any installation of
        # `libiconv'.
	rm -f $(ildir)/libiconv* $(idir)/include/iconv.h

        # Some Mac OS systems may have a version of the GNU C Compiler
        # (GCC) installed that doesn't support some necessary features of
        # building Glib (as part of pkg-config). So to be safe, for Mac
        # systems, we'll make sure it will use LLVM's Clang.
	if [ x$(on_mac_os) = xyes ]; then export compiler="CC=clang"
	else                              export compiler=""
	fi
	$(call gbuild, pkg-config-$(pkgconfig-version), static, \
	               $$compiler --with-internal-glib \
	               --with-pc-path=$(ildir)/pkgconfig, V=1)
	echo "pkg-config $(pkgconfig-version)" > $@

$(ibidir)/sed-$(sed-version): $(ibidir)/coreutils-$(coreutils-version)
	tarball=sed-$(sed-version).tar.xz
	$(call import-source, $(sed-url), $(sed-checksum))
	$(call gbuild, sed-$(sed-version), static,,V=1)
	echo "GNU Sed $(sed-version)" > $@

$(ibidir)/texinfo-$(texinfo-version): \
                  $(ibidir)/perl-$(perl-version)
	tarball=texinfo-$(texinfo-version).tar.xz
	$(call import-source, $(texinfo-url), $(texinfo-checksum))
	$(call gbuild, texinfo-$(texinfo-version), static)
	if [ -f $(ibdir)/patchelf ]; then
	  $(ibdir)/patchelf --set-rpath $(ildir) $(ibdir)/info
	  $(ibdir)/patchelf --set-rpath $(ildir) $(ibdir)/install-info
	fi
	echo "GNU Texinfo $(texinfo-version)" > $@

$(ibidir)/which-$(which-version): $(ibidir)/coreutils-$(coreutils-version)
	tarball=/which-$(which-version).tar.gz
	$(call import-source, $(which-url), $(which-checksum))
	$(call gbuild, which-$(which-version), static)
	echo "GNU Which $(which-version)" > $@










# GCC and its prerequisites
# -------------------------
$(ibidir)/isl-$(isl-version): $(ibidir)/gmp-$(gmp-version)
	tarball=isl-$(isl-version).tar.bz2
	$(call import-source, $(isl-url), $(isl-checksum))
	if [ $(host_cc) = 1 ]; then
	  echo "" > $@
	else
	  $(call gbuild, isl-$(isl-version), static, , \
	                 V=1 -j$(numthreads))
	  echo "GNU Integer Set Library $(isl-version)" > $@
	fi

$(ibidir)/mpc-$(mpc-version): $(ibidir)/mpfr-$(mpfr-version)
	tarball=mpc-$(mpc-version).tar.gz
	$(call import-source, $(mpc-url), $(mpc-checksum))
	if [ $(host_cc) = 1 ]; then
	  echo "" > $@
	else
	  $(call gbuild, mpc-$(mpc-version), static, , \
	                 -j$(numthreads), make check)
	  echo "GNU Multiple Precision Complex library" > $@
	fi

# The installation of Binutils can cause problems during the build of other
# programs (http://savannah.nongnu.org/bugs/?56294). Therefore, we'll set
# all other basic programs as Binutils prerequisite and GCC (the final
# basic target) ultimately just depends on Binutils.
$(ibidir)/binutils-$(binutils-version): \
                   $(ibidir)/sed-$(sed-version) \
                   $(ibidir)/isl-$(isl-version) \
                   $(ibidir)/mpc-$(mpc-version) \
                   $(ibidir)/wget-$(wget-version) \
                   $(ibidir)/grep-$(grep-version) \
                   $(ibidir)/file-$(file-version) \
                   $(ibidir)/gawk-$(gawk-version) \
                   $(ibidir)/which-$(which-version) \
                   $(ibidir)/libtool-$(libtool-version) \
                   $(ibidir)/metastore-$(metastore-version) \
                   $(ibidir)/findutils-$(findutils-version) \
                   $(ibidir)/diffutils-$(diffutils-version) \
                   $(ibidir)/coreutils-$(coreutils-version)

        # Download the tarball.
	tarball=binutils-$(binutils-version).tar.lz
	$(call import-source, $(binutils-url), $(binutils-checksum))

        # Binutils' assembler (`as') and linker (`ld') will conflict with
        # other compilers. So if we don't build our own compiler, we'll use
        # the host opertating system's equivalents by just making links.
	if [ x$(on_mac_os) = xyes ]; then
	  $(call makelink,as)
	  $(call makelink,ar)
	  $(call makelink,ld)
	  $(call makelink,nm)
	  $(call makelink,ps)
	  $(call makelink,ranlib)
	  echo "" > $@
	else

          # Build binutils with the standard 'gbuild' function.
	  $(call gbuild, binutils-$(binutils-version), static, \
	                 --with-lib-path=$(sys_library_path), \
	                 -j$(numthreads) )

          # The `ld' linker of Binutils needs several `*crt*.o' file to
          # run. On some systems these object files aren't installed in
          # standard places. We defined `LIBRARY_PATH' and that fixed the
          # problem for many systems. However, some software (for example
          # ImageMagick) over-write `LIBRARY_PATH', therefore there is no
          # other way than to put a link to these necessary files in our
          # local build directory. IMPORTANT NOTE: later, when we build the
          # GNU C Library in the project, we should remove this step.
	  if ! [ x"$(sys_library_path)" = x ]; then
	    for f in $(sys_library_path)/*crt*.o; do
	      b=$$($(ibdir)/basename $$f)
	      ln -s $$f $(ildir)/$$b
	    done
	  fi

          # Write the final target.
	  echo "GNU Binutils $(binutils-version)" > $@
	fi

# We are having issues with `libiberty' (part of GCC) on Mac. So for now,
# GCC won't be built there. Since almost no natural science paper's
# processing depends so strongly on the compiler used, for now, this isn't
# a bad assumption, but we are indeed searching for a solution.
#
# Based on the GCC manual, the GCC build can benefit from a GNU
# environment. So, we'll build GCC after building all the basic tools that
# are often used in a configure and build scripts of GCC components.
#
# Objective C and Objective C++ is necessary for installing `matplotlib'.
#
# We are currently having problems installing GCC on macOS, so for the time
# being, if the project is being run on a macOS, we'll just set a link.
$(ibidir)/gcc-$(gcc-version): $(ibidir)/binutils-$(binutils-version)

        # Function to let the users know what to do if build fails.
	error_message() {
	    echo; echo
	    echo "_________________________________________________"
	    echo "!!!!!!!!       Warning from Maneage      !!!!!!!!"
	    echo
	    echo "Unfortunately building of GCC failed on this system!"
	    echo "Can you please copy the last ~500 lines above and post it"
	    echo "as a bug here (as an attached file):"
	    echo "  https://sv.nongnu.org/support/?func=additem&group=reproduce"
	    echo
	    echo "In the meantime, please re-configure Maneage with '--host-cc'"
	    echo "like below so it uses your own C compiler for building the"
	    echo "high-level software ('-e' is to use the existing configuration):"
	    echo
	    echo "  ./project configure -e --host-cc"
	    echo
	    echo "__________ SEE NOTE FROM MANEAGE ABOVE __________"
	    echo; exit 1
	}

        # Download the tarball.
	tarball=gcc-$(gcc-version).tar.xz
	$(call import-source, $(gcc-url), $(gcc-checksum))

        # GCC builds is own libraries in '$(idir)/lib64'. But all other
        # libraries are in '$(idir)/lib'. Since this project is only for a
        # single architecture, we can trick GCC into building its libraries
        # in '$(idir)/lib' by defining the '$(idir)/lib64' as a symbolic
        # link to '$(idir)/lib'.
	if [ $(host_cc) = 1 ]; then

          # Make sure we don't have any of the program we want to link to
          # in the '.local/bin' directory.
	  rm -f $(ibdir)/cc
	  rm -f $(ibdir)/c++
	  rm -f $(ibdir)/gcc
	  rm -f $(ibdir)/cpp
	  rm -f $(ibdir)/strip;
	  rm -f $(ibdir)/gfortran;

          # Put links to the host's tools in '.local/bin'.
	  $(call makelink,cc)
	  $(call makelink,cpp)
	  $(call makelink,cc,,gcc)
	  $(call makelink,gfortran)
	  $(call makelink,c++,,g++)
	  $(call makelink,strip,mandatory)
	  ccinfo=$$(gcc --version | awk 'NR==1')
	  echo "C compiler (""$$ccinfo"")" > $@

	else

          # We are building GCC, so to avoid any previous build in
          # '.local/bin', we'll delete all the files that GCC builds from
          # there.
	  current_dir=$$(pwd)
	  rm -f $(ibdir)/gcc* $(ibdir)/g++ $(ibdir)/gfortran $(ibdir)/gcov*
	  rm -rf $(ildir)/gcc $(ildir)/libcc* $(ildir)/libgcc*
	  rm -rf $(ildir)/libgfortran* $(ildir)/libstdc* rm $(idir)/x86_64*

          # We don't want '.local/lib' and '.local/lib64' to be separate.
	  ln -fs $(ildir) $(idir)/lib64

          # By default we'll build GCC in the RAM to avoid building so many
          # files and possibly harming the hard-drive or SSD. But if the
          # RAM doesn't have enough space, we can't use it.
	  in_ram=$$(df $(ddir) \
	               | awk 'NR==2{print ($$4>10000000) ? "yes" : "no"}'); \
	  if [ $$in_ram = "yes" ]; then odir=$(ddir)
	  else
	    odir=$(BDIR)/software/build-tmp-gcc
	    if [ -d $$odir ]; then rm -rf $$odir; fi
	    mkdir $$odir
	  fi

          # Go into the proper directory, unpack GCC and prepare the
          # 'build' directory inside it for all the built files.
	  cd $$odir
	  rm -rf gcc-$(gcc-version)
	  tar xf $(tdir)/$$tarball
	  if [ $$odir != $(ddir) ]; then
	    ln -s $$odir/gcc-$(gcc-version) $(ddir)/gcc-$(gcc-version)
	  fi
	  cd gcc-$(gcc-version)
	  mkdir build
	  cd build

          # Configure, build and install GCC, if any of three steps fails,
          # the error message will be printed.
	  if ! ../configure SHELL=$(ibdir)/bash \
	                    --prefix=$(idir) \
	                    --with-mpc=$(idir) \
	                    --with-gmp=$(idir) \
	                    --with-isl=$(idir) \
	                    --with-mpfr=$(idir) \
	                    --with-local-prefix=$(idir) \
	                    --with-build-time-tools=$(idir) \
	                    --enable-lto \
	                    --enable-shared \
	                    --enable-cet=auto \
	                    --enable-default-pie \
	                    --enable-default-ssp \
	                    --enable-decimal-float \
	                    --enable-threads=posix \
	                    --enable-languages=c,c++,fortran,objc,obj-c++ \
	                    --disable-nls \
	                    --disable-libada \
	                    --disable-multilib \
	                    --disable-multiarch; then error_message; fi
	  if ! make SHELL=$(ibdir)/bash -j$(numthreads); then error_message; fi
	  if ! make SHELL=$(ibdir)/bash install; then error_message; fi

          # We need to manually fix the RPATH inside GCC's libraries, the
          # programs built by GCC already have RPATH.
	  tempname=$$odir/gcc-$(gcc-version)/build/rpath-temp-copy
	  if [ -f $(ibdir)/patchelf ]; then

            # Go over all the installed GCC libraries (its executables are
            # fine!).
	    for f in $$(find $(idir)/libexec/gcc -type f) $(ildir)/libstdc++*; do

              # Make sure this is a static library, copy it to a temporary
              # name (to avoid any possible usage of the file while it is
              # being corrected), and add RPATH inside of it and put the
              # corrected file back in its place. In the case of the
              # standard C++ library, we also need to manually insert a
              # linking to libiconv.
	      if file $$f | grep -q "dynamically linked"; then
	        cp $$f $$tempname
	        patchelf --set-rpath $(ildir) $$tempname
	        echo "$$f: added rpath"
	        if echo $$f | grep -q "libstdc++"; then
	          patchelf --add-needed $(ildir)/libiconv.so $$tempname
	          echo "$$f: linked with libiconv"
	        fi
	        mv $$tempname $$f
	      fi
	    done
	  fi

          # Come back up to the un-packing directory and delete the GCC
          # source directory.
	  cd ../..
	  rm -rf gcc-$(gcc-version)
	  cd $$current_dir
	  if [ "$$odir" != "$(ddir)" ]; then
	    rm -rf $$odir;
	    rm $(ddir)/gcc-$(gcc-version);
	  fi

          # Set 'cc' to point to 'gcc'.
	  ln -sf $(ibdir)/gcc $(ibdir)/cc

          # Write the final target.
	  echo "GNU Compiler Collection (GCC) $(gcc-version)" > $@
	fi