aboutsummaryrefslogtreecommitdiff
path: root/Makefile.in
blob: f87770432a326cfa465c213a514b7ca1d67c0977 (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
srcdir := @abs_top_srcdir@
builddir := @abs_top_builddir@
INSTALL_DIR := @prefix@

GCC_SRCDIR := @with_gcc_src@
BINUTILS_SRCDIR := @with_binutils_src@
NEWLIB_SRCDIR := @with_newlib_src@
GLIBC_SRCDIR := @with_glibc_src@
MUSL_SRCDIR := @with_musl_src@
LINUX_HEADERS_SRCDIR := @with_linux_headers_src@
GDB_SRCDIR := @with_gdb_src@
QEMU_SRCDIR := @with_qemu_src@
SPIKE_SRCDIR := @with_spike_src@
PK_SRCDIR := @with_pk_src@
LLVM_SRCDIR := @with_llvm_src@
DEJAGNU_SRCDIR := @with_dejagnu_src@
DEBUG_INFO := @debug_info@
DEJAGNU_SRCDIR := @with_dejagnu_src@

SIM ?= @WITH_SIM@

ifeq ($(srcdir)/gcc,$(GCC_SRCDIR))
# We need a relative source dir for the gcc configure, to make msys2 mingw64
# builds work.  Mayberelsrcdir is relative if a relative path was used to run
# configure, otherwise absolute, so we have to check.
mayberelsrcdir := @top_srcdir@
gccsrcdir := $(shell case $(mayberelsrcdir) in \
		  ([\\/]* | ?:[\\/]*)  echo $(mayberelsrcdir)/gcc ;; \
		  (*)  echo ../$(mayberelsrcdir)/gcc ;; \
		esac)
else
gccsrcdir := $(abspath $(GCC_SRCDIR))
endif

WITH_ARCH ?= @WITH_ARCH@
WITH_ABI ?= @WITH_ABI@
WITH_TUNE ?= @WITH_TUNE@
WITH_ISA_SPEC ?= @WITH_ISA_SPEC@
SYSROOT := $(INSTALL_DIR)/sysroot
ENABLE_LIBSANITIZER ?= @enable_libsanitizer@
QEMU_TARGETS ?= @qemu_targets@

SHELL := /bin/sh
AWK := @GAWK@
SED := @GSED@
PATH := $(INSTALL_DIR)/bin:$(PATH)

# Check to see if we need wrapper scripts for awk/sed (which point to
# gawk/gsed on platforms where these aren't the default), otherwise
# don't override these as the wrappers don't always work.
ifneq (@GSED@,/bin/sed)
	PATH := $(builddir)/scripts/wrapper/sed:$(PATH)
endif
ifneq (@GAWK@,/usr/bin/gawk)
	PATH := $(builddir)/scripts/wrapper/awk:$(PATH)
endif

export PATH AWK SED

MULTILIB_FLAGS := @multilib_flags@
MULTILIB_GEN := @multilib_gen@
ifeq ($(MULTILIB_GEN),)
NEWLIB_MULTILIB_NAMES := @newlib_multilib_names@
GCC_MULTILIB_FLAGS := $(MULTILIB_FLAGS)
else
NEWLIB_MULTILIB_NAMES := $(shell echo "$(MULTILIB_GEN)" | $(SED) 's/;/\n/g' | $(SED) '/^$$/d' | $(AWK) '{split($$0,a,"-"); printf "%s-%s ", a[1],a[2]}')
GCC_MULTILIB_FLAGS := $(MULTILIB_FLAGS) --with-multilib-generator="$(MULTILIB_GEN)"
endif
GLIBC_MULTILIB_NAMES := @glibc_multilib_names@
GCC_CHECKING_FLAGS := @gcc_checking@

EXTRA_MULTILIB_TEST := @extra_multilib_test@

XLEN := $(shell echo $(WITH_ARCH) | tr A-Z a-z | sed 's/.*rv\([0-9]*\).*/\1/')
ifneq ($(XLEN),32)
	XLEN := 64
endif

make_tuple = riscv$(1)-unknown-$(2)
LINUX_TUPLE  ?= $(call make_tuple,$(XLEN),linux-gnu)
NEWLIB_TUPLE ?= $(call make_tuple,$(XLEN),elf)
MUSL_TUPLE ?= $(call make_tuple,$(XLEN),linux-musl)

CFLAGS_FOR_TARGET := $(CFLAGS_FOR_TARGET_EXTRA) $(DEBUG_INFO) @target_cflags@ @cmodel@
CXXFLAGS_FOR_TARGET := $(CXXFLAGS_FOR_TARGET_EXTRA) $(DEBUG_INFO) @target_cxxflags@ @cmodel@
ASFLAGS_FOR_TARGET := $(ASFLAGS_FOR_TARGET_EXTRA) $(DEBUG_INFO) @cmodel@
# --with-expat is required to enable XML support used by OpenOCD.
BINUTILS_TARGET_FLAGS := --with-expat=yes $(BINUTILS_TARGET_FLAGS_EXTRA)
BINUTILS_NATIVE_FLAGS := $(BINUTILS_NATIVE_FLAGS_EXTRA)
GDB_TARGET_FLAGS := --with-expat=yes $(GDB_TARGET_FLAGS_EXTRA)
GDB_NATIVE_FLAGS := $(GDB_NATIVE_FLAGS_EXTRA)

GLIBC_TARGET_FLAGS := $(GLIBC_TARGET_FLAGS_EXTRA)
GLIBC_CC_FOR_TARGET ?= $(LINUX_TUPLE)-gcc
GLIBC_CXX_FOR_TARGET ?= $(LINUX_TUPLE)-g++
GLIBC_TARGET_BOARDS ?= $(shell $(srcdir)/scripts/generate_target_board \
  --sim-name riscv-sim \
  --cmodel $(shell echo @cmodel@ | cut -d '=' -f2) \
  --build-arch-abi $(GLIBC_MULTILIB_NAMES) \
  --extra-test-arch-abi-flags-list "$(EXTRA_MULTILIB_TEST)")

NEWLIB_CC_FOR_TARGET ?= $(NEWLIB_TUPLE)-gcc
NEWLIB_CXX_FOR_TARGET ?= $(NEWLIB_TUPLE)-g++
NEWLIB_TARGET_BOARDS ?= $(shell $(srcdir)/scripts/generate_target_board \
  --sim-name riscv-sim \
  --cmodel $(shell echo @cmodel@ | cut -d '=' -f2) \
  --build-arch-abi $(NEWLIB_MULTILIB_NAMES) \
  --extra-test-arch-abi-flags-list "$(EXTRA_MULTILIB_TEST)")

NEWLIB_NANO_TARGET_BOARDS ?= $(shell $(srcdir)/scripts/generate_target_board \
  --sim-name riscv-sim-nano \
  --cmodel $(shell echo @cmodel@ | cut -d '=' -f2) \
  --build-arch-abi $(NEWLIB_MULTILIB_NAMES) \
  --extra-test-arch-abi-flags-list "$(EXTRA_MULTILIB_TEST)")
NEWLIB_CC_FOR_MULTILIB_INFO := $(NEWLIB_CC_FOR_TARGET)

MUSL_TARGET_FLAGS := $(MUSL_TARGET_FLAGS_EXTRA)
MUSL_CC_FOR_TARGET ?= $(MUSL_TUPLE)-gcc
MUSL_CXX_FOR_TARGET ?= $(MUSL_TUPLE)-g++

CONFIGURE_HOST   = @configure_host@
PREPARATION_STAMP:=stamps/check-write-permission

all: @default_target@
ifeq (@enable_host_gcc@,--enable-host-gcc)
PREPARATION_STAMP+= stamps/install-host-gcc
PATH := $(builddir)/install-host-gcc/bin:$(PATH)
GCC_CHECKING_FLAGS := $(GCC_CHECKING_FLAGS) --enable-werror-always
endif
newlib: stamps/build-gcc-newlib-stage2
linux: stamps/build-gcc-linux-stage2
ifneq (,$(findstring riscv32,$(MUSL_TUPLE)))
.PHONY: musl
musl:
	@echo "musl only supports 64bit builds." && exit 1
else
musl: stamps/build-gcc-musl-stage2
endif
ifeq (@enable_gdb@,--enable-gdb)
newlib: stamps/build-gdb-newlib
linux: stamps/build-gdb-linux
endif
linux-native: stamps/build-gcc-linux-native
ifeq (@enable_llvm@,--enable-llvm)
all: stamps/build-llvm-@default_target@
newlib: stamps/build-llvm-newlib
linux: stamps/build-llvm-linux
ifeq (@multilib_flags@,--enable-multilib)
$(error "Setting multilib flags for LLVM builds is not supported.")
endif
endif

.PHONY: build-binutils build-gdb build-gcc1 build-libc build-gcc2 build-qemu build-llvm
build-binutils: stamps/build-binutils-@default_target@
build-gdb: stamps/build-gdb-@default_target@
build-gcc%: stamps/build-gcc-@default_target@-stage%
ifeq (@default_target@,linux)
build-libc: $(addprefix stamps/build-glibc-linux-,$(GLIBC_MULTILIB_NAMES))
else
build-libc: stamps/build-newlib stamps/build-newlib-nano \
	stamps/merge-newlib-nano
endif
build-qemu: stamps/build-qemu
build-llvm: stamps/build-llvm-@default_target@

REGRESSION_TEST_LIST = gcc

.PHONY: check
check: check-@default_target@
.PHONY: check-linux check-newlib
check-linux: $(patsubst %,check-%-linux,$(REGRESSION_TEST_LIST))
check-newlib: $(patsubst %,check-%-newlib,$(REGRESSION_TEST_LIST))
check-newlib-nano: $(patsubst %,check-%-newlib-nano,$(REGRESSION_TEST_LIST))
.PHONY: check-gcc check-gcc-linux check-gcc-newlib check-gcc-newlib-nano
check-gcc: check-gcc-@default_target@
check-gcc-linux: stamps/check-gcc-linux
check-gcc-newlib: stamps/check-gcc-newlib
check-gcc-newlib-nano: stamps/check-gcc-newlib-nano
.PHONY: check-glibc-linux
check-glibc-linux: $(addprefix stamps/check-glibc-linux-,$(GLIBC_MULTILIB_NAMES))
.PHONY: check-dhrystone check-dhrystone-linux check-dhrystone-newlib
check-dhrystone: check-dhrystone-@default_target@
.PHONY: check-binutils check-binutils-linux check-binutils-newlib
check-binutils: check-binutils-@default_target@
check-binutils-linux: stamps/check-binutils-linux
check-binutils-newlib: stamps/check-binutils-newlib
check-binutils-newlib-nano: stamps/check-binutils-newlib-nano
.PHONY: check-gdb check-gdb-linux check-gdb-newlib
check-gdb: check-gdb-@default_target@
check-gdb-linux: stamps/check-gdb-linux
check-gdb-newlib: stamps/check-gdb-newlib
check-gdb-newlib-nano: stamps/check-gdb-newlib-nano

.PHONY: report
report: report-@default_target@
.PHONY: report-linux report-newlib report-newlib-nano
report-linux: $(patsubst %,report-%-linux,$(REGRESSION_TEST_LIST))
report-newlib: $(patsubst %,report-%-newlib,$(REGRESSION_TEST_LIST))
report-newlib-nano: $(patsubst %,report-%-newlib-nano,$(REGRESSION_TEST_LIST))
.PHONY: report-gcc
report-gcc: report-gcc-@default_target@
.PHONY: report-dhrystone
report-dhrystone: report-dhrystone-@default_target@
.PHONY: report-binutils
report-binutils: report-binutils-@default_target@
.PHONY: report-gdb
report-gdb: report-gdb-@default_target@

.PHONY: build-sim
ifeq ($(SIM),qemu)
SIM_PATH:=$(srcdir)/scripts/wrapper/qemu:$(srcdir)/scripts
SIM_PREPARE:=PATH="$(SIM_PATH):$(INSTALL_DIR)/bin:$(PATH)" RISC_V_SYSROOT="$(SYSROOT)"
SIM_STAMP:= stamps/build-qemu
else
ifeq ($(SIM),spike)
# Using spike simulator.
SIM_PATH:=$(srcdir)/scripts/wrapper/spike:$(srcdir)/scripts
SIM_PREPARE:=PATH="$(SIM_PATH):$(INSTALL_DIR)/bin:$(PATH)" PK_PATH="$(INSTALL_DIR)/$(NEWLIB_TUPLE)/bin/" ARCH_STR="$(WITH_ARCH)"
SIM_STAMP:= stamps/build-spike
ifneq (,$(findstring rv32,$(NEWLIB_MULTILIB_NAMES)))
SIM_STAMP+= stamps/build-pk32
endif
ifneq (,$(findstring rv64,$(NEWLIB_MULTILIB_NAMES)))
SIM_STAMP+= stamps/build-pk64
endif
else
ifeq ($(SIM),gdb)
# Using gdb simulator.
SIM_PATH:=$(INSTALL_DIR)/bin
SIM_PREPARE:=
else
$(error "Only support SIM=spike, SIM=gdb or SIM=qemu (default).")
endif
endif
endif

build-sim: $(SIM_STAMP)

stamps/check-write-permission:
	mkdir -p $(INSTALL_DIR)/.test || \
		(echo "Sorry, you don't have permission to write to" \
		 "'$(INSTALL_DIR)'. Please make sure that the location is " \
		 "writable or use --prefix to specify another destination.'" \
		 && exit 1)
	rm -r $(INSTALL_DIR)/.test
	mkdir -p $(dir $@) && touch $@

stamps/build-linux-headers:
	mkdir -p $(SYSROOT)/usr/
ifdef LINUX_HEADERS_SRCDIR
	cp -a $(LINUX_HEADERS_SRCDIR) $(SYSROOT)/usr/
else
	cp -a $(srcdir)/linux-headers/include $(SYSROOT)/usr/
endif
	mkdir -p $(dir $@) && touch $@

#
# Rule for auto init submodules
#

ifeq ($(findstring $(srcdir),$(GCC_SRCDIR)),$(srcdir))
GCC_SRC_GIT := $(GCC_SRCDIR)/.git
else
GCC_SRC_GIT :=
endif

ifeq ($(findstring $(srcdir),$(BINUTILS_SRCDIR)),$(srcdir))
BINUTILS_SRC_GIT := $(BINUTILS_SRCDIR)/.git
else
BINUTILS_SRC_GIT :=
endif

ifeq ($(findstring $(srcdir),$(GDB_SRCDIR)),$(srcdir))
GDB_SRC_GIT := $(GDB_SRCDIR)/.git
else
GDB_SRC_GIT :=
endif

ifeq ($(findstring $(srcdir),$(NEWLIB_SRCDIR)),$(srcdir))
NEWLIB_SRC_GIT := $(NEWLIB_SRCDIR)/.git
else
NEWLIB_SRC_GIT :=
endif

ifeq ($(findstring $(srcdir),$(GLIBC_SRCDIR)),$(srcdir))
GLIBC_SRC_GIT := $(GLIBC_SRCDIR)/.git
else
GLIBC_SRC_GIT :=
endif

ifeq ($(findstring $(srcdir),$(MUSL_SRCDIR)),$(srcdir))
MUSL_SRC_GIT := $(MUSL_SRCDIR)/.git
else
MUSL_SRC_GIT :=
endif

ifeq ($(findstring $(srcdir),$(QEMU_SRCDIR)),$(srcdir))
QEMU_SRC_GIT := $(QEMU_SRCDIR)/.git
else
QEMU_SRC_GIT :=
endif

ifeq ($(findstring $(srcdir),$(SPIKE_SRCDIR)),$(srcdir))
SPIKE_SRC_GIT := $(SPIKE_SRCDIR)/.git
PK_SRC_GIT := $(PK_SRCDIR)/.git
else
SPIKE_SRC_GIT :=
endif

ifeq ($(findstring $(srcdir),$(LLVM_SRCDIR)),$(srcdir))
LLVM_SRC_GIT := $(LLVM_SRCDIR)/.git
else
LLVM_SRC_GIT :=
endif

ifeq ($(findstring $(srcdir),$(DEJAGNU_SRCDIR)),$(srcdir))
DEJAGNU_SRC_GIT := $(DEJAGNU_SRCDIR)/.git
else
DEJAGNU_SRC_GIT :=
endif

ifneq ("$(wildcard $(GCC_SRCDIR)/.git)","")
GCCPKGVER := g$(shell git -C $(GCC_SRCDIR) describe --always --dirty --exclude '*')
else
GCCPKGVER :=
endif

$(srcdir)/%/.git:
	cd $(srcdir) && \
	flock `git rev-parse --git-dir`/config git submodule init $(dir $@) && \
	flock `git rev-parse --git-dir`/config git submodule update $(dir $@)

stamps/install-host-gcc: $(GCC_SRCDIR) $(GCC_SRC_GIT)
	if test -f $</contrib/download_prerequisites && test "@NEED_GCC_EXTERNAL_LIBRARIES@" = "true"; then cd $< && ./contrib/download_prerequisites; fi
	rm -rf $@ $(notdir $@)
	mkdir $(notdir $@)
	cd $(notdir $@) && $</configure \
		--prefix=$(builddir)/install-host-gcc \
		@with_system_zlib@ \
		--enable-languages=c,c++ \
		--disable-bootstrap \
		--disable-multilib
	$(MAKE) -C $(notdir $@)
	$(MAKE) -C $(notdir $@) install
	mkdir -p $(dir $@) && touch $@

#
# GLIBC
#

stamps/build-binutils-linux: $(BINUTILS_SRCDIR) $(BINUTILS_SRC_GIT) $(PREPARATION_STAMP)
	rm -rf $@ $(notdir $@)
	mkdir $(notdir $@)
# CC_FOR_TARGET is required for the ld testsuite.
	cd $(notdir $@) && CC_FOR_TARGET=$(GLIBC_CC_FOR_TARGET) $</configure \
		--target=$(LINUX_TUPLE) \
		$(CONFIGURE_HOST) \
		--prefix=$(INSTALL_DIR) \
		--with-sysroot=$(SYSROOT) \
		--enable-plugins \
		$(MULTILIB_FLAGS) \
		@with_guile@ \
		--disable-werror \
		--disable-nls \
		$(BINUTILS_TARGET_FLAGS) \
		--disable-gdb \
		--disable-sim \
		--disable-libdecnumber \
		--disable-readline \
		$(WITH_ISA_SPEC)
	$(MAKE) -C $(notdir $@)
	$(MAKE) -C $(notdir $@) install
	mkdir -p $(dir $@) && touch $@

stamps/build-gdb-linux: $(GDB_SRCDIR) $(GDB_SRC_GIT) $(PREPARATION_STAMP)
	rm -rf $@ $(notdir $@)
	mkdir $(notdir $@)
# CC_FOR_TARGET is required for the ld testsuite.
	cd $(notdir $@) && CC_FOR_TARGET=$(GLIBC_CC_FOR_TARGET) $</configure \
		--target=$(LINUX_TUPLE) \
		$(CONFIGURE_HOST) \
		--prefix=$(INSTALL_DIR) \
		--with-sysroot=$(SYSROOT) \
		$(MULTILIB_FLAGS) \
		@with_guile@ \
		--disable-werror \
		--disable-nls \
		$(GDB_TARGET_FLAGS) \
		--enable-gdb \
		--disable-gas \
		--disable-binutils \
		--disable-ld \
		--disable-gold \
		--disable-gprof
	$(MAKE) -C $(notdir $@)
	$(MAKE) -C $(notdir $@) install
	mkdir -p $(dir $@) && touch $@

stamps/build-glibc-linux-headers: $(GLIBC_SRCDIR) $(GLIBC_SRC_GIT) stamps/build-gcc-linux-stage1
	rm -rf $@ $(notdir $@)
	mkdir $(notdir $@)
	cd $(notdir $@) && CC="$(GLIBC_CC_FOR_TARGET)" $</configure \
		--host=$(LINUX_TUPLE) \
		--prefix=$(SYSROOT)/usr \
		--enable-shared \
		--with-headers=$(LINUX_HEADERS_SRCDIR) \
		--disable-multilib \
		--enable-kernel=3.0.0
	$(MAKE) -C $(notdir $@) install-headers
	mkdir -p $(dir $@) && touch $@

stamps/build-glibc-linux-%: $(GLIBC_SRCDIR) $(GLIBC_SRC_GIT) stamps/build-gcc-linux-stage1
ifeq ($(MULTILIB_FLAGS),--enable-multilib)
	$(eval $@_ARCH := $(word 4,$(subst -, ,$@)))
	$(eval $@_ABI := $(word 5,$(subst -, ,$@)))
else
	$(eval $@_ARCH := )
	$(eval $@_ABI := )
endif
	$(eval $@_LIBDIRSUFFIX := $(if $($@_ABI),$(shell echo $($@_ARCH) | sed 's/.*rv\([0-9]*\).*/\1/')/$($@_ABI),))
	$(eval $@_XLEN := $(if $($@_ABI),$(shell echo $($@_ARCH) | sed 's/.*rv\([0-9]*\).*/\1/'),$(XLEN)))
	$(eval $@_CFLAGS := $(if $($@_ABI),-march=$($@_ARCH) -mabi=$($@_ABI),))
	$(eval $@_LIBDIROPTS := $(if $@_LIBDIRSUFFIX,--libdir=/usr/lib$($@_LIBDIRSUFFIX) libc_cv_slibdir=/lib$($@_LIBDIRSUFFIX) libc_cv_rtlddir=/lib,))
	rm -rf $@ $(notdir $@)
	mkdir $(notdir $@)
	cd $(notdir $@) && \
		CC="$(GLIBC_CC_FOR_TARGET) $($@_CFLAGS)" \
		CXX="this-is-not-the-compiler-youre-looking-for" \
		CFLAGS="$(CFLAGS_FOR_TARGET) -O2 $($@_CFLAGS)" \
		CXXFLAGS="$(CXXFLAGS_FOR_TARGET) -O2 $($@_CFLAGS)" \
		ASFLAGS="$(ASFLAGS_FOR_TARGET) $($@_CFLAGS)" \
		$</configure \
		--host=$(call make_tuple,$($@_XLEN),linux-gnu) \
		--prefix=/usr \
		--disable-werror \
		--enable-shared \
		--enable-obsolete-rpc \
		--with-headers=$(LINUX_HEADERS_SRCDIR) \
		$(MULTILIB_FLAGS) \
		--enable-kernel=3.0.0 \
		$(GLIBC_TARGET_FLAGS) \
		$($@_LIBDIROPTS)
	$(MAKE) -C $(notdir $@)
	+flock $(SYSROOT)/.lock $(MAKE) -C $(notdir $@) install install_root=$(SYSROOT)
	mkdir -p $(dir $@) && touch $@

stamps/build-gcc-linux-stage1: $(GCC_SRCDIR) $(GCC_SRC_GIT) stamps/build-binutils-linux \
                               stamps/build-linux-headers
	if test -f $</contrib/download_prerequisites && test "@NEED_GCC_EXTERNAL_LIBRARIES@" = "true"; then cd $< && ./contrib/download_prerequisites; fi
	rm -rf $@ $(notdir $@)
	mkdir $(notdir $@)
	cd $(notdir $@) && $</configure \
		--target=$(LINUX_TUPLE) \
		$(CONFIGURE_HOST) \
		--prefix=$(INSTALL_DIR) \
		--with-sysroot=$(SYSROOT) \
		--with-newlib \
		--without-headers \
		--disable-shared \
		--disable-threads \
		@with_system_zlib@ \
		--enable-tls \
		--enable-languages=c \
		--disable-libatomic \
		--disable-libmudflap \
		--disable-libssp \
		--disable-libquadmath \
		--disable-libgomp \
		--disable-nls \
		--disable-bootstrap \
		--src=$(gccsrcdir) \
		$(GCC_CHECKING_FLAGS) \
		$(MULTILIB_FLAGS) \
		$(WITH_ABI) \
		$(WITH_ARCH) \
		$(WITH_TUNE) \
		$(WITH_ISA_SPEC) \
		$(GCC_EXTRA_CONFIGURE_FLAGS) \
		CFLAGS_FOR_TARGET="-O2 $(CFLAGS_FOR_TARGET)" \
		CXXFLAGS_FOR_TARGET="-O2 $(CXXFLAGS_FOR_TARGET)"
	$(MAKE) -C $(notdir $@) inhibit-libc=true all-gcc
	$(MAKE) -C $(notdir $@) inhibit-libc=true install-gcc
	$(MAKE) -C $(notdir $@) inhibit-libc=true all-target-libgcc
	$(MAKE) -C $(notdir $@) inhibit-libc=true install-target-libgcc
	mkdir -p $(dir $@) && touch $@

stamps/build-gcc-linux-stage2: $(GCC_SRCDIR) $(GCC_SRC_GIT) $(addprefix stamps/build-glibc-linux-,$(GLIBC_MULTILIB_NAMES)) \
                               stamps/build-glibc-linux-headers
	rm -rf $@ $(notdir $@)
	mkdir $(notdir $@)
	cd $(notdir $@) && $</configure \
		--target=$(LINUX_TUPLE) \
		$(CONFIGURE_HOST) \
		--prefix=$(INSTALL_DIR) \
		--with-sysroot=$(SYSROOT) \
		--with-pkgversion="$(GCCPKGVER)" \
		@with_system_zlib@ \
		--enable-shared \
		--enable-tls \
		--enable-languages=c,c++,fortran \
		--disable-libmudflap \
		--disable-libssp \
		--disable-libquadmath \
		$(ENABLE_LIBSANITIZER) \
		--disable-nls \
		--disable-bootstrap \
		--src=$(gccsrcdir) \
		$(GCC_CHECKING_FLAGS) \
		$(MULTILIB_FLAGS) \
		$(WITH_ABI) \
		$(WITH_ARCH) \
		$(WITH_TUNE) \
		$(WITH_ISA_SPEC) \
		$(GCC_EXTRA_CONFIGURE_FLAGS) \
		CFLAGS_FOR_TARGET="-O2 $(CFLAGS_FOR_TARGET)" \
		CXXFLAGS_FOR_TARGET="-O2 $(CXXFLAGS_FOR_TARGET)"
	$(MAKE) -C $(notdir $@)
	$(MAKE) -C $(notdir $@) install
	cp -a $(INSTALL_DIR)/$(LINUX_TUPLE)/lib* $(SYSROOT)
	mkdir -p $(dir $@) && touch $@

stamps/build-binutils-linux-native: $(BINUTILS_SRCDIR) $(BINUTILS_SRC_GIT) stamps/build-gcc-linux-stage2 $(PREPARATION_STAMP)
	rm -rf $@ $(notdir $@)
	mkdir $(notdir $@)
	cd $(notdir $@) && $</configure \
		--host=$(LINUX_TUPLE) \
		--target=$(LINUX_TUPLE) \
		$(CONFIGURE_HOST) \
		--prefix=$(INSTALL_DIR)/native \
		--enable-plugins \
		$(MULTILIB_FLAGS) \
		@with_guile@ \
		--disable-werror \
		--disable-nls \
		$(BINUTILS_NATIVE_FLAGS) \
		--disable-gdb \
		--disable-sim \
		--disable-libdecnumber \
		--disable-readline \
		$(WITH_ISA_SPEC)
	$(MAKE) -C $(notdir $@)
	$(MAKE) -C $(notdir $@) install
	mkdir -p $(dir $@) && touch $@

stamps/build-gcc-linux-native: $(GCC_SRCDIR) $(GCC_SRC_GIT) stamps/build-gcc-linux-stage2 stamps/build-binutils-linux-native
	if test -f $</contrib/download_prerequisites; then cd $< && ./contrib/download_prerequisites; fi
	rm -rf $@ $(notdir $@)
	mkdir $(notdir $@)
	cd $(notdir $@) && $</configure \
		--host=$(LINUX_TUPLE) \
		--target=$(LINUX_TUPLE) \
		$(CONFIGURE_HOST) \
		--prefix=$(INSTALL_DIR)/native \
		--without-system-zlib \
		--enable-shared \
		--enable-tls \
		--enable-languages=c,c++,fortran \
		--disable-libmudflap \
		--disable-libssp \
		--disable-libquadmath \
		--disable-nls \
		--disable-bootstrap \
                --with-native-system-header-dir=$(INSTALL_DIR)/native/include \
		$(GCC_CHECKING_FLAGS) \
		$(MULTILIB_FLAGS) \
		$(WITH_ABI) \
		$(WITH_ARCH) \
		$(WITH_TUNE) \
		$(WITH_ISA_SPEC) \
		$(GCC_EXTRA_CONFIGURE_FLAGS)
	$(MAKE) -C $(notdir $@)
	$(MAKE) -C $(notdir $@) install
	cp -a $(INSTALL_DIR)/$(LINUX_TUPLE)/lib* $(SYSROOT)
	mkdir -p $(dir $@) && touch $@

#
# NEWLIB
#

stamps/build-binutils-newlib: $(BINUTILS_SRCDIR) $(BINUTILS_SRC_GIT) $(PREPARATION_STAMP)
	rm -rf $@ $(notdir $@)
	mkdir $(notdir $@)
# CC_FOR_TARGET is required for the ld testsuite.
	cd $(notdir $@) && CC_FOR_TARGET=$(NEWLIB_CC_FOR_TARGET) $</configure \
		--target=$(NEWLIB_TUPLE) \
		$(CONFIGURE_HOST) \
		--prefix=$(INSTALL_DIR) \
		--enable-plugins \
		@with_guile@ \
		--disable-werror \
		$(BINUTILS_TARGET_FLAGS) \
		--disable-gdb \
		--disable-sim \
		--disable-libdecnumber \
		--disable-readline \
		$(WITH_ISA_SPEC)
	$(MAKE) -C $(notdir $@)
	$(MAKE) -C $(notdir $@) install
	mkdir -p $(dir $@) && touch $@

stamps/build-gdb-newlib: $(GDB_SRCDIR) $(GDB_SRC_GIT) $(PREPARATION_STAMP)
	rm -rf $@ $(notdir $@)
	mkdir $(notdir $@)
# CC_FOR_TARGET is required for the ld testsuite.
	cd $(notdir $@) && CC_FOR_TARGET=$(NEWLIB_CC_FOR_TARGET) $</configure \
		--target=$(NEWLIB_TUPLE) \
		$(CONFIGURE_HOST) \
		--prefix=$(INSTALL_DIR) \
		@with_guile@ \
		--disable-werror \
		$(GDB_TARGET_FLAGS) \
		--enable-gdb \
		--disable-gas \
		--disable-binutils \
		--disable-ld \
		--disable-gold \
		--disable-gprof
	$(MAKE) -C $(notdir $@)
	$(MAKE) -C $(notdir $@) install
	mkdir -p $(dir $@) && touch $@

stamps/build-gcc-newlib-stage1: $(GCC_SRCDIR) $(GCC_SRC_GIT) stamps/build-binutils-newlib
	if test -f $</contrib/download_prerequisites && test "@NEED_GCC_EXTERNAL_LIBRARIES@" = "true"; then cd $< && ./contrib/download_prerequisites; fi
	rm -rf $@ $(notdir $@)
	mkdir $(notdir $@)
	cd $(notdir $@) && $</configure \
		--target=$(NEWLIB_TUPLE) \
		$(CONFIGURE_HOST) \
		--prefix=$(INSTALL_DIR) \
		--disable-shared \
		--disable-threads \
		--disable-tls \
		--enable-languages=c,c++ \
		@with_system_zlib@ \
		--with-newlib \
		--with-sysroot=$(INSTALL_DIR)/$(NEWLIB_TUPLE) \
		--disable-libmudflap \
		--disable-libssp \
		--disable-libquadmath \
		--disable-libgomp \
		--disable-nls \
		--disable-tm-clone-registry \
		--src=$(gccsrcdir) \
		$(GCC_CHECKING_FLAGS) \
		$(GCC_MULTILIB_FLAGS) \
		$(WITH_ABI) \
		$(WITH_ARCH) \
		$(WITH_TUNE) \
		$(WITH_ISA_SPEC) \
		$(GCC_EXTRA_CONFIGURE_FLAGS) \
		CFLAGS_FOR_TARGET="-Os $(CFLAGS_FOR_TARGET)" \
		CXXFLAGS_FOR_TARGET="-Os $(CXXFLAGS_FOR_TARGET)"
	$(MAKE) -C $(notdir $@) all-gcc
	$(MAKE) -C $(notdir $@) install-gcc
	mkdir -p $(dir $@) && touch $@

stamps/build-newlib: $(NEWLIB_SRCDIR) $(NEWLIB_SRC_GIT) stamps/build-gcc-newlib-stage1
	rm -rf $@ $(notdir $@)
	mkdir $(notdir $@)
	cd $(notdir $@) && $</configure \
		--target=$(NEWLIB_TUPLE) \
		$(CONFIGURE_HOST) \
		--prefix=$(INSTALL_DIR) \
		--enable-newlib-io-long-double \
		--enable-newlib-io-long-long \
		--enable-newlib-io-c99-formats \
		--enable-newlib-register-fini \
		CFLAGS_FOR_TARGET="-O2 -D_POSIX_MODE -ffunction-sections -fdata-sections $(CFLAGS_FOR_TARGET)" \
		CXXFLAGS_FOR_TARGET="-O2 -D_POSIX_MODE -ffunction-sections -fdata-sections $(CXXFLAGS_FOR_TARGET)"
	$(MAKE) -C $(notdir $@)
	$(MAKE) -C $(notdir $@) install
	mkdir -p $(dir $@) && touch $@

stamps/build-newlib-nano: $(NEWLIB_SRCDIR) $(NEWLIB_SRC_GIT) stamps/build-gcc-newlib-stage1
	rm -rf $@ $(notdir $@)
	mkdir $(notdir $@)
	cd $(notdir $@) && $</configure \
		--target=$(NEWLIB_TUPLE) \
		$(CONFIGURE_HOST) \
		--prefix=$(builddir)/install-newlib-nano \
		--enable-newlib-reent-small \
		--disable-newlib-fvwrite-in-streamio \
		--disable-newlib-fseek-optimization \
		--disable-newlib-wide-orient \
		--enable-newlib-nano-malloc \
		--disable-newlib-unbuf-stream-opt \
		--enable-lite-exit \
		--enable-newlib-global-atexit \
		--enable-newlib-nano-formatted-io \
		--disable-newlib-supplied-syscalls \
		--disable-nls \
		CFLAGS_FOR_TARGET="-Os -ffunction-sections -fdata-sections $(CFLAGS_FOR_TARGET)" \
		CXXFLAGS_FOR_TARGET="-Os -ffunction-sections -fdata-sections $(CXXFLAGS_FOR_TARGET)"
	$(MAKE) -C $(notdir $@)
	$(MAKE) -C $(notdir $@) install
	mkdir -p $(dir $@) && touch $@

stamps/merge-newlib-nano: stamps/build-newlib-nano stamps/build-newlib
# Copy nano library files into newlib install dir.
	if [ -f $(INSTALL_DIR)/bin/$(NEWLIB_TUPLE)-gcc ] ; then \
		export NEWLIB_CC_FOR_MULTILIB_INFO="$(INSTALL_DIR)/bin/$(NEWLIB_TUPLE)-gcc"; \
	else \
		export NEWLIB_CC_FOR_MULTILIB_INFO="$(NEWLIB_CC_FOR_TARGET)"; \
	fi
	set -e; \
        for ml in `${NEWLIB_CC_FOR_MULTILIB_INFO} --print-multi-lib`; \
	do \
	    mld=`echo $${ml} | sed -e 's/;.*$$//'`; \
	    cp $(builddir)/install-newlib-nano/$(NEWLIB_TUPLE)/lib/$${mld}/libc.a \
		$(INSTALL_DIR)/$(NEWLIB_TUPLE)/lib/$${mld}/libc_nano.a; \
	    cp $(builddir)/install-newlib-nano/$(NEWLIB_TUPLE)/lib/$${mld}/libm.a \
		$(INSTALL_DIR)/$(NEWLIB_TUPLE)/lib/$${mld}/libm_nano.a; \
	    cp $(builddir)/install-newlib-nano/$(NEWLIB_TUPLE)/lib/$${mld}/libg.a \
		$(INSTALL_DIR)/$(NEWLIB_TUPLE)/lib/$${mld}/libg_nano.a; \
	    cp $(builddir)/install-newlib-nano/$(NEWLIB_TUPLE)/lib/$${mld}/libgloss.a\
		$(INSTALL_DIR)/$(NEWLIB_TUPLE)/lib/$${mld}/libgloss_nano.a; \
	    cp $(builddir)/install-newlib-nano/$(NEWLIB_TUPLE)/lib/$${mld}/crt0.o\
		$(INSTALL_DIR)/$(NEWLIB_TUPLE)/lib/$${mld}/crt0.o; \
	done
# Copy nano header files into newlib install dir.
	mkdir -p $(INSTALL_DIR)/$(NEWLIB_TUPLE)/include/newlib-nano; \
	cp $(builddir)/install-newlib-nano/$(NEWLIB_TUPLE)/include/newlib.h \
		$(INSTALL_DIR)/$(NEWLIB_TUPLE)/include/newlib-nano/newlib.h; \
	mkdir -p $(dir $@) && touch $@

stamps/build-gcc-newlib-stage2: $(GCC_SRCDIR) $(GCC_SRC_GIT) stamps/build-newlib \
		stamps/merge-newlib-nano
	rm -rf $@ $(notdir $@)
	mkdir $(notdir $@)
	cd $(notdir $@) && $</configure \
		--target=$(NEWLIB_TUPLE) \
		$(CONFIGURE_HOST) \
		--prefix=$(INSTALL_DIR) \
		--disable-shared \
		--disable-threads \
		--enable-languages=c,c++ \
		--with-pkgversion="$(GCCPKGVER)" \
		@with_system_zlib@ \
		--enable-tls \
		--with-newlib \
		--with-sysroot=$(INSTALL_DIR)/$(NEWLIB_TUPLE) \
		--with-native-system-header-dir=/include \
		--disable-libmudflap \
		--disable-libssp \
		--disable-libquadmath \
		--disable-libgomp \
		--disable-nls \
		--disable-tm-clone-registry \
		--src=$(gccsrcdir) \
		$(GCC_CHECKING_FLAGS) \
		$(GCC_MULTILIB_FLAGS) \
		$(WITH_ABI) \
		$(WITH_ARCH) \
		$(WITH_TUNE) \
		$(WITH_ISA_SPEC) \
		$(GCC_EXTRA_CONFIGURE_FLAGS) \
		CFLAGS_FOR_TARGET="-Os $(CFLAGS_FOR_TARGET)" \
		CXXFLAGS_FOR_TARGET="-Os $(CXXFLAGS_FOR_TARGET)"
	$(MAKE) -C $(notdir $@)
	$(MAKE) -C $(notdir $@) install
	mkdir -p $(dir $@) && touch $@

#
# MUSL
#

stamps/build-binutils-musl: $(BINUTILS_SRCDIR) $(BINUTILS_SRC_GIT) $(PREPARATION_STAMP)
	rm -rf $@ $(notdir $@)
	mkdir $(notdir $@)
# CC_FOR_TARGET is required for the ld testsuite.
	cd $(notdir $@) && CC_FOR_TARGET=$(MUSL_CC_FOR_TARGET) $</configure \
		--target=$(MUSL_TUPLE) \
		$(CONFIGURE_HOST) \
		--prefix=$(INSTALL_DIR) \
		--with-sysroot=$(SYSROOT) \
		--enable-plugins \
		$(MULTILIB_FLAGS) \
		@with_guile@ \
		--disable-werror \
		--disable-nls \
		$(BINUTILS_TARGET_FLAGS) \
		--disable-gdb \
		--disable-sim \
		--disable-libdecnumber \
		--disable-readline \
		$(WITH_ISA_SPEC)
	$(MAKE) -C $(notdir $@)
	$(MAKE) -C $(notdir $@) install
	mkdir -p $(dir $@) && touch $@

stamps/build-gcc-musl-stage1: $(GCC_SRCDIR) $(GCC_SRC_GIT) stamps/build-binutils-musl \
                               stamps/build-linux-headers
	if test -f $</contrib/download_prerequisites && test "@NEED_GCC_EXTERNAL_LIBRARIES@" = "true"; then cd $< && ./contrib/download_prerequisites; fi
	rm -rf $@ $(notdir $@)
	mkdir $(notdir $@)
	cd $(notdir $@) && $</configure \
		--target=$(MUSL_TUPLE) \
		$(CONFIGURE_HOST) \
		--prefix=$(INSTALL_DIR) \
		--without-headers \
		--disable-shared \
		--disable-threads \
		@with_system_zlib@ \
		--enable-tls \
		--enable-languages=c \
		--disable-libatomic \
		--disable-libmudflap \
		--disable-libssp \
		--disable-libquadmath \
		--disable-libgomp \
		--disable-nls \
		--disable-bootstrap \
		--src=$(gccsrcdir) \
		$(GCC_CHECKING_FLAGS) \
		--disable-multilib \
		$(WITH_ABI) \
		$(WITH_ARCH) \
		$(WITH_TUNE) \
		$(WITH_ISA_SPEC) \
		$(GCC_EXTRA_CONFIGURE_FLAGS) \
		CFLAGS_FOR_TARGET="-O2 $(CFLAGS_FOR_TARGET)" \
		CXXFLAGS_FOR_TARGET="-O2 $(CXXFLAGS_FOR_TARGET)"
	$(MAKE) -C $(notdir $@) inhibit-libc=true all-gcc
	$(MAKE) -C $(notdir $@) inhibit-libc=true install-gcc
	$(MAKE) -C $(notdir $@) inhibit-libc=true all-target-libgcc
	$(MAKE) -C $(notdir $@) inhibit-libc=true install-target-libgcc
	mkdir -p $(dir $@) && touch $@

stamps/build-musl-linux-headers: $(MUSL_SRCDIR) $(MUSL_SRC_GIT) stamps/build-gcc-musl-stage1
	rm -rf $@ $(notdir $@)
	mkdir $(notdir $@)
	cd $(notdir $@) && CC="$(MUSL_CC_FOR_TARGET)" $</configure \
		--host=$(MUSL_TUPLE) \
		--prefix=$(SYSROOT)/usr \
		--enable-shared \
		--with-headers=$(LINUX_HEADERS_SRCDIR) \
		--disable-multilib \
		--enable-kernel=3.0.0
	$(MAKE) -C $(notdir $@) install-headers
	mkdir -p $(dir $@) && touch $@

stamps/build-musl-linux: $(MUSL_SRCDIR) $(MUSL_SRC_GIT) stamps/build-gcc-musl-stage1
	rm -rf $@ $(notdir $@)
	mkdir $(notdir $@)
	cd $(notdir $@) && \
		CC="$(MUSL_CC_FOR_TARGET) $($@_CFLAGS)" \
		CXX="$(MUSL_CXX_FOR_TARGET) $($@_CFLAGS)" \
		CFLAGS="$(CFLAGS_FOR_TARGET) -O2 $($@_CFLAGS)" \
		CXXFLAGS="$(CXXFLAGS_FOR_TARGET) -O2 $($@_CFLAGS)" \
		ASFLAGS="$(ASFLAGS_FOR_TARGET) $($@_CFLAGS)" \
		$</configure \
		--host=$(MUSL_TUPLE) \
		--prefix=$(SYSROOT) \
		--disable-werror \
		--enable-shared \
		$(MUSL_TARGET_FLAGS)
	$(MAKE) -C $(notdir $@)
	+flock $(SYSROOT)/.lock $(MAKE) -C $(notdir $@) install
	mkdir -p $(dir $@) && touch $@

stamps/build-gcc-musl-stage2: $(GCC_SRCDIR) $(GCC_SRC_GIT) stamps/build-musl-linux \
                               stamps/build-musl-linux-headers
	rm -rf $@ $(notdir $@)
	mkdir $(notdir $@)
	# Disable libsanitizer for now
	# https://github.com/google/sanitizers/issues/1080
	cd $(notdir $@) && $</configure \
		--target=$(MUSL_TUPLE) \
		$(CONFIGURE_HOST) \
		--prefix=$(INSTALL_DIR) \
		--with-sysroot=$(SYSROOT) \
		@with_system_zlib@ \
		--enable-shared \
		--enable-tls \
		--enable-languages=c,c++ \
		--disable-libmudflap \
		--disable-libssp \
		--disable-libquadmath \
		--disable-libsanitizer \
		--disable-nls \
		--disable-bootstrap \
		--src=$(gccsrcdir) \
		$(GCC_CHECKING_FLAGS) \
		--disable-multilib \
		$(WITH_ABI) \
		$(WITH_ARCH) \
		$(WITH_TUNE) \
		$(WITH_ISA_SPEC) \
		$(GCC_EXTRA_CONFIGURE_FLAGS) \
		CFLAGS_FOR_TARGET="-O2 $(CFLAGS_FOR_TARGET)" \
		CXXFLAGS_FOR_TARGET="-O2 $(CXXFLAGS_FOR_TARGET)"
	$(MAKE) -C $(notdir $@)
	$(MAKE) -C $(notdir $@) install
	cp -a $(INSTALL_DIR)/$(MUSL_TUPLE)/lib* $(SYSROOT)
	mkdir -p $(dir $@) && touch $@

stamps/build-spike: $(SPIKE_SRCDIR) $(SPIKE_SRC_GIT) $(PREPARATION_STAMP)
	rm -rf $@ $(notdir $@)
	mkdir $(notdir $@)
	cd $(notdir $@) && $</configure \
		--prefix=$(INSTALL_DIR)
	$(MAKE) -C $(notdir $@)
	$(MAKE) -C $(notdir $@) install
	mkdir -p $(dir $@)
	date > $@

stamps/build-pk32: $(PK_SRCDIR) $(PK_SRC_GIT) stamps/build-gcc-newlib-stage2
	rm -rf $@ $(notdir $@)
	mkdir $(notdir $@)
	cd $(notdir $@) && $</configure \
		--prefix=$(INSTALL_DIR) \
		--host=$(NEWLIB_TUPLE) \
		--with-arch=rv32gc \
		--with-abi=ilp32f
	$(MAKE) -C $(notdir $@)
	cp $(notdir $@)/pk $(INSTALL_DIR)/$(NEWLIB_TUPLE)/bin/pk32
	mkdir -p $(dir $@)
	date > $@

stamps/build-pk64: $(PK_SRCDIR) $(PK_SRC_GIT) stamps/build-gcc-newlib-stage2
	rm -rf $@ $(notdir $@)
	mkdir $(notdir $@)
	cd $(notdir $@) && $</configure \
		--prefix=$(INSTALL_DIR) \
		--host=$(NEWLIB_TUPLE) \
		--with-arch=rv64gc \
		--with-abi=lp64d
	$(MAKE) -C $(notdir $@)
	cp $(notdir $@)/pk $(INSTALL_DIR)/$(NEWLIB_TUPLE)/bin/pk64
	mkdir -p $(dir $@)
	date > $@

stamps/build-qemu: $(QEMU_SRCDIR) $(QEMU_SRC_GIT) $(PREPARATION_STAMP)
	rm -rf $@ $(notdir $@)
	mkdir $(notdir $@)
	cd $(notdir $@) && $</configure \
		--prefix=$(INSTALL_DIR) \
		--target-list=$(QEMU_TARGETS) \
		--interp-prefix=$(INSTALL_DIR)/sysroot \
		--python=python3
	$(MAKE) -C $(notdir $@)
	$(MAKE) -C $(notdir $@) install
	mkdir -p $(dir $@)
	date > $@

stamps/build-llvm-linux: $(LLVM_SRCDIR) $(LLVM_SRC_GIT) $(BINUTILS_SRCDIR) $(BINUTILS_SRC_GIT) \
                         stamps/build-gcc-linux-stage2
	# We have the following situation:
	# - sysroot directory: $(INSTALL_DIR)/sysroot
	# - GCC install directory: $(INSTALL_DIR)
	# However, LLVM does not allow to set a GCC install prefix
	# (-DGCC_INSTALL_PREFIX) if a sysroot (-DDEFAULT_SYSROOT) is set
	# (the GCC install prefix will be ignored silently).
	# Without a proper sysroot path feature.h won't be found by clang.
	# Without a proper GCC install directory libgcc won't be found.
	# As a workaround we have to merge both paths:
	mkdir -p $(SYSROOT)/lib/
	ln -s -f $(INSTALL_DIR)/lib/gcc $(SYSROOT)/lib/gcc
	rm -rf $@ $(notdir $@)
	mkdir $(notdir $@)
	cd $(notdir $@) && ln -f -s $(SYSROOT) sysroot
	cd $(notdir $@) && \
	    cmake $(LLVM_SRCDIR)/llvm \
	    -DCMAKE_INSTALL_PREFIX=$(INSTALL_DIR) \
	    -DCMAKE_BUILD_TYPE=Release \
	    -DLLVM_TARGETS_TO_BUILD="RISCV" \
	    -DLLVM_ENABLE_PROJECTS="clang;lld" \
	    -DLLVM_ENABLE_RUNTIMES="compiler-rt;libcxx;libcxxabi;libunwind" \
	    -DLLVM_DEFAULT_TARGET_TRIPLE="$(LINUX_TUPLE)" \
	    -DDEFAULT_SYSROOT="../sysroot" \
	    -DLLVM_RUNTIME_TARGETS=$(call make_tuple,$(XLEN),linux-gnu) \
	    -DLLVM_INSTALL_TOOLCHAIN_ONLY=On \
	    -DLLVM_BINUTILS_INCDIR=$(BINUTILS_SRCDIR)/include \
	    -DLLVM_PARALLEL_LINK_JOBS=4
	$(MAKE) -C $(notdir $@)
	$(MAKE) -C $(notdir $@) install
	cp $(notdir $@)/lib/riscv$(XLEN)-unknown-linux-gnu/libc++* $(SYSROOT)/lib
	cp $(notdir $@)/lib/LLVMgold.so  $(INSTALL_DIR)/lib
	cd $(INSTALL_DIR)/bin && ln -s -f clang $(LINUX_TUPLE)-clang && ln -s -f clang++ $(LINUX_TUPLE)-clang++
	mkdir -p $(dir $@) && touch $@

stamps/build-llvm-newlib: $(LLVM_SRCDIR) $(LLVM_SRC_GIT) $(BINUTILS_SRCDIR) $(BINUTILS_SRC_GIT) \
                          stamps/build-gcc-newlib-stage2
	rm -rf $@ $(notdir $@)
	mkdir $(notdir $@)
	cd $(notdir $@) && \
	    cmake $(LLVM_SRCDIR)/llvm \
	    -DCMAKE_INSTALL_PREFIX=$(INSTALL_DIR) \
	    -DCMAKE_BUILD_TYPE=Release \
	    -DLLVM_TARGETS_TO_BUILD="RISCV" \
	    -DLLVM_ENABLE_PROJECTS="clang;lld" \
	    -DLLVM_DEFAULT_TARGET_TRIPLE="$(NEWLIB_TUPLE)" \
	    -DLLVM_INSTALL_TOOLCHAIN_ONLY=On \
	    -DLLVM_BINUTILS_INCDIR=$(BINUTILS_SRCDIR)/include \
	    -DLLVM_PARALLEL_LINK_JOBS=4
	$(MAKE) -C $(notdir $@)
	$(MAKE) -C $(notdir $@) install
	cp $(notdir $@)/lib/LLVMgold.so  $(INSTALL_DIR)/lib
	cd $(INSTALL_DIR)/bin && ln -s -f clang $(NEWLIB_TUPLE)-clang && \
	    ln -s -f clang++ $(NEWLIB_TUPLE)-clang++
	mkdir -p $(dir $@) && touch $@

stamps/build-dejagnu: $(DEJAGNU_SRCDIR) $(DEJAGNU_SRC_GIT) $(PREPARATION_STAMP)
	rm -rf $@ $(notdir $@)
	mkdir $(notdir $@)
	cd $(notdir $@) && $</configure \
		--prefix=$(INSTALL_DIR)
	$(MAKE) -C $(notdir $@)
	$(MAKE) -C $(notdir $@) install
	mkdir -p $(dir $@)
	date > $@

stamps/check-gcc-newlib: stamps/build-gcc-newlib-stage2 $(SIM_STAMP) stamps/build-dejagnu
	$(SIM_PREPARE) $(MAKE) -C build-gcc-newlib-stage2 check-gcc "RUNTESTFLAGS=$(RUNTESTFLAGS) --target_board='$(NEWLIB_TARGET_BOARDS)'"
	mkdir -p $(dir $@)
	date > $@

stamps/check-gcc-newlib-nano: stamps/build-gcc-newlib-stage2 $(SIM_STAMP) stamps/build-dejagnu
	$(SIM_PREPARE) $(MAKE) -C build-gcc-newlib-stage2 check-gcc "RUNTESTFLAGS=$(RUNTESTFLAGS) --target_board='$(NEWLIB_NANO_TARGET_BOARDS)'"
	mkdir -p $(dir $@)
	date > $@

stamps/check-gcc-linux: stamps/build-gcc-linux-stage2 $(SIM_STAMP) stamps/build-dejagnu
	$(SIM_PREPARE) $(MAKE) -C build-gcc-linux-stage2 check-gcc "RUNTESTFLAGS=$(RUNTESTFLAGS) --target_board='$(GLIBC_TARGET_BOARDS)'"
	mkdir -p $(dir $@)
	date > $@

stamps/check-glibc-linux-%: stamps/build-gcc-linux-stage2 $(SIM_STAMP) stamps/build-dejagnu \
		$(addprefix stamps/build-glibc-linux-,$(GLIBC_MULTILIB_NAMES))
	$(eval $@_BUILD_DIR := $(notdir $@))
	$(eval $@_BUILD_DIR := $(subst check-,build-,$($@_BUILD_DIR)))
	$(SIM_PREPARE) $(MAKE) -C $($@_BUILD_DIR) check
	mkdir -p $(dir $@)
	date > $@

.PHONY: check-dhrystone-newlib check-dhrystone-newlib-nano
check-dhrystone-newlib: $(patsubst %,stamps/check-dhrystone-newlib-%,$(NEWLIB_MULTILIB_NAMES))
check-dhrystone-newlib-nano: $(patsubst %,stamps/check-dhrystone-newlib-nano-%,$(NEWLIB_MULTILIB_NAMES))

stamps/check-dhrystone-newlib-%: \
		stamps/build-gcc-newlib-stage2 \
		$(SIM_STAMP) \
		$(wildcard $(srcdir)/test/benchmarks/dhrystone/*)
	$(eval $@_ARCH := $(word 4,$(subst -, ,$@)))
	$(eval $@_ABI := $(word 5,$(subst -, ,$@)))
	$(eval $@_XLEN := $(patsubst rv32%,32,$(patsubst rv64%,64,$($@_ARCH))))
	$(SIM_PREPARE) $(srcdir)/test/benchmarks/dhrystone/check -march=$($@_ARCH) -mabi=$($@_ABI) -cc=riscv$(XLEN)-unknown-elf-gcc -objdump=riscv$(XLEN)-unknown-elf-objdump -sim=riscv$($@_XLEN)-unknown-elf-run -out=$@ $(filter %.c,$^) || true

stamps/check-dhrystone-newlib-nano-%: \
		stamps/build-gcc-newlib-stage2 \
		$(SIM_STAMP) \
		$(wildcard $(srcdir)/test/benchmarks/dhrystone/*)
	$(eval $@_ARCH := $(word 5,$(subst -, ,$@)))
	$(eval $@_ABI := $(word 6,$(subst -, ,$@)))
	$(eval $@_XLEN := $(patsubst rv32%,32,$(patsubst rv64%,64,$($@_ARCH))))
	$(SIM_PREPARE) $(srcdir)/test/benchmarks/dhrystone/check -march=$($@_ARCH) -mabi=$($@_ABI) -specs=nano.specs -cc=riscv$(XLEN)-unknown-elf-gcc -objdump=riscv$(XLEN)-unknown-elf-objdump -sim=riscv$($@_XLEN)-unknown-elf-run -out=$@ $(filter %.c,$^) || true

.PHONY: check-dhrystone-linux
check-dhrystone-linux: $(patsubst %,stamps/check-dhrystone-linux-%,$(GLIBC_MULTILIB_NAMES))

stamps/check-dhrystone-linux-%: \
		stamps/build-gcc-linux-stage2 \
		$(SIM_STAMP) \
		$(wildcard $(srcdir)/test/benchmarks/dhrystone/*)
	$(eval $@_ARCH := $(word 4,$(subst -, ,$@)))
	$(eval $@_ABI := $(word 5,$(subst -, ,$@)))
	$(eval $@_XLEN := $(patsubst rv32%,32,$(patsubst rv64%,64,$($@_ARCH))))
	$(SIM_PREPARE) $(srcdir)/test/benchmarks/dhrystone/check -march=$($@_ARCH) -mabi=$($@_ABI) -cc=riscv$(XLEN)-unknown-elf-gcc -objdump=riscv$(XLEN)-unknown-elf-objdump -sim=riscv$($@_XLEN)-unknown-elf-run -out=$@ $(filter %.c,$^) || true

stamps/check-binutils-newlib: stamps/build-gcc-newlib-stage2 $(SIM_STAMP) stamps/build-dejagnu
	$(SIM_PREPARE) $(MAKE) -C build-binutils-newlib check-binutils check-gas check-ld -k "RUNTESTFLAGS=--target_board='$(NEWLIB_TARGET_BOARDS)'" || true
	date > $@

stamps/check-binutils-newlib-nano: stamps/build-gcc-newlib-stage2 $(SIM_STAMP) stamps/build-dejagnu
	$(SIM_PREPARE) $(MAKE) -C build-binutils-newlib check-binutils check-gas check-ld -k "RUNTESTFLAGS=--target_board='$(NEWLIB_NANO_TARGET_BOARDS)'" || true
	date > $@

stamps/check-binutils-linux: stamps/build-gcc-linux-stage2 $(SIM_STAMP) stamps/build-dejagnu
	$(SIM_PREPARE) $(MAKE) -C build-binutils-linux check-binutils check-gas check-ld -k "RUNTESTFLAGS=--target_board='$(GLIBC_TARGET_BOARDS)'" || true
	date > $@

stamps/check-gdb-newlib: stamps/build-gcc-newlib-stage2 stamps/build-gdb-newlib $(SIM_STAMP) stamps/build-dejagnu
	$(SIM_PREPARE) $(MAKE) -C build-gdb-newlib check-gdb -k "RUNTESTFLAGS=--target_board='$(NEWLIB_TARGET_BOARDS)'" || true
	date > $@

stamps/check-gdb-newlib-nano: stamps/build-gcc-newlib-stage2 stamps/build-gdb-newlib $(SIM_STAMP) stamps/build-dejagnu
	$(SIM_PREPARE) $(MAKE) -C build-gdb-newlib check-gdb -k "RUNTESTFLAGS=--target_board='$(NEWLIB_NANO_TARGET_BOARDS)'" || true
	date > $@

stamps/check-gdb-linux: stamps/build-gcc-linux-stage2 stamps/build-gdb-linux $(SIM_STAMP) stamps/build-dejagnu
	$(SIM_PREPARE) $(MAKE) -C build-gdb-linux check-gdb -k "RUNTESTFLAGS=--target_board='$(GLIBC_TARGET_BOARDS)'" || true
	date > $@

.PHONY: report-gcc-newlib report-gcc-newlib-nano
report-gcc-newlib: stamps/check-gcc-newlib
	$(srcdir)/scripts/testsuite-filter gcc newlib $(srcdir)/test/allowlist `find build-gcc-newlib-stage2/gcc/testsuite/ -name *.sum |paste -sd "," -`

report-gcc-newlib-nano: stamps/check-gcc-newlib-nano
	$(srcdir)/scripts/testsuite-filter gcc newlib-nano $(srcdir)/test/allowlist `find build-gcc-newlib-stage2/gcc/testsuite/ -name *.sum |paste -sd "," -`

.PHONY: report-gcc-linux
report-gcc-linux: stamps/check-gcc-linux
	$(srcdir)/scripts/testsuite-filter gcc glibc $(srcdir)/test/allowlist `find build-gcc-linux-stage2/gcc/testsuite/ -name *.sum |paste -sd "," -`

.PHONY: report-dhrystone-newlib report-dhrystone-newlib-nano
report-dhrystone-newlib: $(patsubst %,stamps/check-dhrystone-newlib-%,$(NEWLIB_MULTILIB_NAMES))
	if cat $^ | grep -v '^PASS'; then false; else true; fi
report-dhrystone-newlib-nano: $(patsubst %,stamps/check-dhrystone-newlib-nano-%,$(NEWLIB_MULTILIB_NAMES))
	if cat $^ | grep -v '^PASS'; then false; else true; fi

.PHONY: report-dhrystone-linux
report-dhrystone-linux: $(patsubst %,stamps/check-dhrystone-linux-%,$(GLIBC_MULTILIB_NAMES))
	if cat $^ | grep -v '^PASS'; then false; else true; fi

.PHONY: report-binutils-newlib report-binutils-newlib-nano
report-binutils-newlib: stamps/check-binutils-newlib
	$(srcdir)/scripts/testsuite-filter binutils newlib \
	    $(srcdir)/test/allowlist \
	    `find build-binutils-newlib/ -name *.sum |paste -sd "," -`

report-binutils-newlib-nano: stamps/check-binutils-newlib-nano
	$(srcdir)/scripts/testsuite-filter binutils newlib-nano \
	    $(srcdir)/test/allowlist \
	    `find build-binutils-newlib/ -name *.sum |paste -sd "," -`

.PHONY: report-binutils-linux
report-binutils-linux: stamps/check-binutils-linux
	$(srcdir)/scripts/testsuite-filter binutils glibc \
	    $(srcdir)/test/allowlist \
	    `find build-binutils-linux/ -name *.sum |paste -sd "," -`

clean:
	rm -rf build-* install-* stamps install-newlib-nano

.PHONY: report-gdb-newlib report-gdb-newlib-nano
report-gdb-newlib: stamps/check-gdb-newlib
	stat $(patsubst %,$(srcdir)/test/gdb-newlib/%.log,$(NEWLIB_MULTILIB_NAMES)) || exit 1
# Fail if there are blank lines in the log file used as input for grep below.
	if grep '^$$' $(patsubst %,$(srcdir)/test/gdb-newlib/%.log,$(NEWLIB_MULTILIB_NAMES)); then exit 1; fi
	if find build-gdb-newlib -iname '*.sum' | xargs grep ^FAIL | sort | grep -F -v $(patsubst %,--file=$(srcdir)/test/gdb-newlib/%.log,$(NEWLIB_MULTILIB_NAMES)); then false; else true; fi

report-gdb-newlib-nano: stamps/check-gdb-newlib-nano
	stat $(patsubst %,$(srcdir)/test/gdb-newlib/%.log,$(NEWLIB_MULTILIB_NAMES)) || exit 1
# Fail if there are blank lines in the log file used as input for grep below.
	if grep '^$$' $(patsubst %,$(srcdir)/test/gdb-newlib/%.log,$(NEWLIB_MULTILIB_NAMES)); then exit 1; fi
	if find build-gdb-newlib -iname '*.sum' | xargs grep ^FAIL | sort | grep -F -v $(patsubst %,--file=$(srcdir)/test/gdb-newlib/%.log,$(NEWLIB_MULTILIB_NAMES)); then false; else true; fi

.PHONY: report-gdb-linux
report-gdb-linux: stamps/check-gdb-linux
	stat $(patsubst %,$(srcdir)/test/gdb-linux/%.log,$(GLIBC_MULTILIB_NAMES)) || exit 1
# Fail if there are blank lines in the log file used as input for grep below.
	if grep '^$$' $(patsubst %,$(srcdir)/test/gdb-linux/%.log,$(GLIBC_MULTILIB_NAMES)); then exit 1; fi
	if find build-gdb-linux -iname '*.sum' | xargs grep ^FAIL | sort | grep -F -v $(patsubst %,--file=$(srcdir)/test/gdb-linux/%.log,$(GLIBC_MULTILIB_NAMES)); then false; else true; fi

distclean: clean
	rm -rf src

# All of the packages install themselves, so our install target does nothing.
install:

# Rebuilding Makefile.
Makefile: $(srcdir)/Makefile.in config.status
	CONFIG_FILES=$@ CONFIG_HEADERS= $(SHELL) ./config.status

config.status: $(srcdir)/configure
	CONFIG_SHELL="$(SHELL)" $(SHELL) ./config.status --recheck