summary refs log tree commit diff
path: root/gnu/packages/gcc.scm
blob: e3ce0069f011b53707fc297bf207c4884071e9d1 (plain) (blame)
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
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
;;; GNU Guix --- Functional package management for GNU
;;; Copyright © 2012-2023 Ludovic Courtès <ludo@gnu.org>
;;; Copyright © 2014, 2015, 2018 Mark H Weaver <mhw@netris.org>
;;; Copyright © 2014, 2015, 2016, 2017, 2019, 2021 Ricardo Wurmus <rekado@elephly.net>
;;; Copyright © 2015, 2023 Andreas Enge <andreas@enge.fr>
;;; Copyright © 2015-2018, 2020-2023 Efraim Flashner <efraim@flashner.co.il>
;;; Copyright © 2016 Carlos Sánchez de La Lama <csanchezdll@gmail.com>
;;; Copyright © 2018 Tobias Geerinckx-Rice <me@tobias.gr>
;;; Copyright © 2018, 2020, 2022 Marius Bakke <marius@gnu.org>
;;; Copyright © 2020 Joseph LaFreniere <joseph@lafreniere.xyz>
;;; Copyright © 2020 Guy Fleury Iteriteka <gfleury@disroot.org>
;;; Copyright © 2020 Simon Tournier <zimon.toutoune@gmail.com>
;;; Copyright © 2021 Chris Marusich <cmmarusich@gmail.com>
;;; Copyright © 2021 Sarah Morgensen <iskarian@mgsn.dev>
;;; Copyright © 2022 Greg Hogan <code@greghogan.com>
;;;
;;; This file is part of GNU Guix.
;;;
;;; GNU Guix 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.
;;;
;;; GNU Guix 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 GNU Guix.  If not, see <http://www.gnu.org/licenses/>.

(define-module (gnu packages gcc)
  #:use-module ((guix licenses)
                #:select (gpl3+ gpl2+ lgpl2.1+ lgpl2.0+ fdl1.3+))
  #:use-module (gnu packages)
  #:use-module (gnu packages bootstrap)
  #:use-module (gnu packages compression)
  #:use-module (gnu packages multiprecision)
  #:use-module (gnu packages texinfo)
  #:use-module (gnu packages dejagnu)
  #:use-module (gnu packages documentation)
  #:use-module (gnu packages xml)
  #:use-module (gnu packages docbook)
  #:use-module (gnu packages graphviz)
  #:use-module (gnu packages elf)
  #:use-module (gnu packages perl)
  #:use-module (guix packages)
  #:use-module (guix download)
  #:use-module (guix build-system gnu)
  #:use-module (guix build-system trivial)
  #:use-module (guix gexp)
  #:use-module (guix utils)
  #:use-module (srfi srfi-1)
  #:use-module (ice-9 match)
  #:use-module (ice-9 regex))

(define %gcc-infrastructure
  ;; Base URL for GCC's infrastructure.
  "mirror://gcc/infrastructure/")

(define (gcc-configure-flags-for-triplet target)
  "Return a list of additional GCC `configure' flags for TARGET, a GNU triplet.

The purpose of this procedure is to translate extended GNU triplets---e.g.,
where the OS part is overloaded to denote a specific ABI---into GCC
`configure' options.  We take extended GNU triplets that glibc recognizes."
  (cond ((string-match "^mips64el.*gnuabin?64$" target)
         ;; Triplets recognized by glibc as denoting the N64 ABI; see
         ;; ports/sysdeps/mips/preconfigure.
         '("--with-abi=64"))

        ((string-match "^arm.*-gnueabihf$" target)
         '("--with-arch=armv7-a"
           "--with-float=hard"
           "--with-mode=thumb"
           "--with-fpu=neon"))

        ((and (string-suffix? "-gnu" target)
              (not (string-contains target "-linux")))
         ;; Cross-compilation of libcilkrts in GCC 5.5.0 to GNU/Hurd fails
         ;; with:
         ;;   libcilkrts/runtime/os-unix.c:388:2: error: #error "Unknown architecture"
         ;; Cilk has been removed from GCC 8 anyway.
         '("--disable-libcilkrts"))

        ;; glibc needs the 128-bit long double type on these architectures.
        ((or (string-prefix? "powerpc64le-" target)
             (string-prefix? "powerpc-" target))
         '("--with-long-double-128"))

        ;; GCC 11.3.0's <libgcov.h> includes <sys/mman.h>, which MinGW lacks:
        ;; <https://bugs.gentoo.org/show_bug.cgi?id=843989>.
        ((target-mingw? target)
         '("--disable-gcov"))

        (else
         ;; TODO: Add `arm.*-gnueabi', etc.
         '())))

(define-public gcc-4.7
  (let* ((stripped? #t)      ;whether to strip the compiler, not the libraries
         (maybe-target-tools
          (lambda ()
            ;; Return the `_FOR_TARGET' variables that are needed when
            ;; cross-compiling GCC.
            (let ((target (%current-target-system)))
              (if target
                  (map (lambda (var tool)
                         (string-append (string-append var "_FOR_TARGET")
                                        "=" target "-" tool))
                       '("CC"  "CXX" "LD" "AR" "NM" "OBJDUMP" "RANLIB" "STRIP")
                       '("gcc" "g++" "ld" "ar" "nm" "objdump" "ranlib" "strip"))
                  '()))))
         (libdir
          (let ((base '(or (assoc-ref outputs "lib")
                           (assoc-ref outputs "out"))))
            (lambda ()
              ;; Return the directory that contains lib/libgcc_s.so et al.
              (if (%current-target-system)
                  `(string-append ,base "/" ,(%current-target-system))
                  base))))
         (configure-flags
          (lambda ()
            ;; This is terrible.  Since we have two levels of quasiquotation,
            ;; we have to do this convoluted thing just so we can insert the
            ;; contents of (maybe-target-tools).
            (list 'quasiquote
                  (append
                   '("--enable-plugin"
                     "--enable-languages=c,c++"
                     "--disable-multilib"
                     "--with-system-zlib"

                     ;; No pre-compiled libstdc++ headers, to save space.
                     "--disable-libstdcxx-pch"

                     "--with-local-prefix=/no-gcc-local-prefix"

                     ;; With a separate "lib" output, the build system
                     ;; incorrectly guesses GPLUSPLUS_INCLUDE_DIR, so force
                     ;; it.  (Don't use a versioned sub-directory, that's
                     ;; unnecessary.)
                     ,(string-append "--with-gxx-include-dir="
                                     (assoc-ref %outputs "out")
                                     "/include/c++")

                     ,(let ((libc (assoc-ref %build-inputs "libc")))
                        (if libc
                            (string-append "--with-native-system-header-dir=" libc
                                           "/include")
                            "--without-headers")))

                   ;; Pass the right options for the target triplet.
                   (let ((triplet
                          (or (%current-target-system)
                              (nix-system->gnu-triplet (%current-system)))))
                     (gcc-configure-flags-for-triplet triplet))

                   (maybe-target-tools))))))
    (hidden-package
     (package
       (name "gcc")
       (version "4.7.4")
       (source (origin
                 (method url-fetch)
                 (uri (string-append "mirror://gnu/gcc/gcc-"
                                     version "/gcc-" version ".tar.bz2"))
                 (sha256
                  (base32
                   "10k2k71kxgay283ylbbhhs51cl55zn2q38vj5pk4k950qdnirrlj"))
                 (patches (search-patches "gcc-4-compile-with-gcc-5.patch"
                                          "gcc-fix-texi2pod.patch"))))
       (build-system gnu-build-system)

       ;; Separate out the run-time support libraries because all the
       ;; dynamic-linked objects depend on it.
       (outputs '("out"                    ;commands, etc. (60+ MiB)
                  "lib"                    ;libgcc_s, libgomp, etc. (15+ MiB)
                  "debug"))                ;debug symbols of run-time libraries

       (inputs (list gmp mpfr mpc elfutils zlib))

       ;; GCC < 5 is one of the few packages that doesn't ship .info files.
       ;; Newer texinfos fail to build the manual, so we use an older one.
       (native-inputs (list perl ;for manpages
                            texinfo-5))

       (arguments
        `(#:out-of-source? #t
          #:configure-flags ,(let ((flags (configure-flags))
                                   (version (package-version this-package)))
                               ;; GCC 4.9 and 5.0 requires C++11 but GCC
                               ;; 11.3.0 defaults to C++17, which is partly
                               ;; incompatible.  Force C++11.
                               (if (or (version-prefix? "4.9" version)
                                       (version-prefix? "5" version))
                                   `(cons "CXX=g++ -std=c++11" ,flags)
                                   flags))

          #:make-flags
          ;; None of the flags below are needed when doing a Canadian cross.
          ;; TODO: Simplify this.
          ,(if (%current-target-system)
               (if stripped?
                   ''("CFLAGS=-g0 -O2")
                   ''())
               `(let* ((libc        (assoc-ref %build-inputs "libc"))
                       (libc-native (or (assoc-ref %build-inputs "libc-native")
                                        libc)))
                  `(,@(if libc
                          (list (string-append "LDFLAGS_FOR_TARGET="
                                               "-B" libc "/lib "
                                               "-Wl,-dynamic-linker "
                                               "-Wl," libc
                                               ,(glibc-dynamic-linker)))
                          '())

                    ;; Native programs like 'genhooks' also need that right.
                    ,(string-append "LDFLAGS="
                                    "-Wl,-rpath=" libc-native "/lib "
                                    "-Wl,-dynamic-linker "
                                    "-Wl," libc-native ,(glibc-dynamic-linker))
                    ,(string-append "BOOT_CFLAGS=-O2 "
                                    ,(if stripped? "-g0" "-g")))))

          #:tests? #f

          #:phases
          (modify-phases %standard-phases
            (add-before 'configure 'pre-configure
              (lambda* (#:key inputs outputs #:allow-other-keys)
                (let ((libdir ,(libdir))
                      (libc   (assoc-ref inputs "libc")))
                  (when libc
                    ;; The following is not performed for `--without-headers'
                    ;; cross-compiler builds.

                    ;; Join multi-line definitions of GLIBC_DYNAMIC_LINKER* into a
                    ;; single line, to allow the next step to work properly.
                    (for-each
                     (lambda (x)
                       (substitute* (find-files "gcc/config"
                                                "^(linux|gnu|sysv4)(64|-elf|-eabi)?\\.h$")
                         (("(#define (GLIBC|GNU_USER)_DYNAMIC_LINKER.*)\\\\\n$" _ line)
                          line)))
                     '(1 2 3))

                    ;; Fix the dynamic linker's file name.
                    (substitute* (find-files "gcc/config"
                                             "^(linux|gnu|sysv4)(64|-elf|-eabi)?\\.h$")
                      (("#define (GLIBC|GNU_USER)_DYNAMIC_LINKER([^ \t]*).*$"
                        _ gnu-user suffix)
                       (format #f "#define ~a_DYNAMIC_LINKER~a \"~a\"~%"
                               gnu-user suffix
                               (string-append libc ,(glibc-dynamic-linker)))))

                    ;; Tell where to find libstdc++, libc, and `?crt*.o', except
                    ;; `crt{begin,end}.o', which come with GCC.
                    (substitute* (find-files "gcc/config"
                                             "^gnu-user.*\\.h$")
                      (("#define GNU_USER_TARGET_LIB_SPEC (.*)$" _ suffix)
                       ;; Help libgcc_s.so be found (see also below.)  Always use
                       ;; '-lgcc_s' so that libgcc_s.so is always found by those
                       ;; programs that use 'pthread_cancel' (glibc dlopens
                       ;; libgcc_s.so when pthread_cancel support is needed, but
                       ;; having it in the application's RUNPATH isn't enough; see
                       ;; <http://sourceware.org/ml/libc-help/2013-11/msg00023.html>.)
                       ;;
                       ;; NOTE: The '-lgcc_s' added below needs to be removed in a
                       ;; later phase of %gcc-static.  If you change the string
                       ;; below, make sure to update the relevant code in
                       ;; %gcc-static package as needed.
                       (format #f "#define GNU_USER_TARGET_LIB_SPEC \
\"-L~a/lib %{!static:-rpath=~a/lib %{!static-libgcc:-rpath=~a/lib -lgcc_s}} \" ~a"
                               libc libc libdir suffix))
                      (("#define GNU_USER_TARGET_STARTFILE_SPEC.*$" line)
                       (format #f "#define STANDARD_STARTFILE_PREFIX_1 \"~a/lib\"
#define STANDARD_STARTFILE_PREFIX_2 \"\"
~a"
                               libc line)))

                    ;; The rs6000 (a.k.a. powerpc) config in GCC does not use
                    ;; GNU_USER_* defines.  Do the above for this case.
                    (substitute*
                        "gcc/config/rs6000/sysv4.h"
                      (("#define LIB_LINUX_SPEC (.*)$" _ suffix)
                       (format #f "#define LIB_LINUX_SPEC \
\"-L~a/lib %{!static:-rpath=~a/lib %{!static-libgcc:-rpath=~a/lib -lgcc_s}} \" ~a"
                               libc libc libdir suffix))
                      (("#define	STARTFILE_LINUX_SPEC.*$" line)
                       (format #f "#define STANDARD_STARTFILE_PREFIX_1 \"~a/lib\"
#define STANDARD_STARTFILE_PREFIX_2 \"\"
~a"
                               libc line))))

                  (when (file-exists? "gcc/config/rs6000")
                    ;; Force powerpc libdir to be /lib and not /lib64
                    (substitute* (find-files "gcc/config/rs6000")
                      (("/lib64") "/lib")))

                  ;; Don't retain a dependency on the build-time sed.
                  (substitute* "fixincludes/fixincl.x"
                    (("static char const sed_cmd_z\\[\\] =.*;")
                     "static char const sed_cmd_z[] = \"sed\";"))

                  ;; Aarch64 support didn't land in GCC until the 4.8 series.
                  (when (file-exists? "gcc/config/aarch64")
                    ;; Force Aarch64 libdir to be /lib and not /lib64
                    (substitute* "gcc/config/aarch64/t-aarch64-linux"
                      (("lib64") "lib")))

                  ;; The STARTFILE_PREFIX_SPEC prevents gcc from finding the
                  ;; gcc:lib output, which causes ld to not find -lgcc_s.
                  (when (file-exists? "gcc/config/riscv")
                    (substitute* '("gcc/config/riscv/linux.h"
                                   "gcc/config/riscv/riscv.h")  ; GCC < 10
                      (("define STARTFILE_PREFIX_SPEC")
                      "define __STARTFILE_PREFIX_SPEC")))

                  (when (file-exists? "libbacktrace")
                    ;; GCC 4.8+ comes with libbacktrace.  By default it builds
                    ;; with -Werror, which fails with a -Wcast-qual error in glibc
                    ;; 2.21's stdlib-bsearch.h.  Remove -Werror.
                    (substitute* "libbacktrace/configure"
                      (("WARN_FLAGS=(.*)-Werror" _ flags)
                       (string-append "WARN_FLAGS=" flags)))

                    (when (file-exists? "libsanitizer/libbacktrace")
                      ;; Same in libsanitizer's bundled copy (!) found in 4.9+.
                      (substitute* "libsanitizer/libbacktrace/Makefile.in"
                        (("-Werror")
                         ""))))

                  ;; Add a RUNPATH to libstdc++.so so that it finds libgcc_s.
                  ;; See <https://gcc.gnu.org/bugzilla/show_bug.cgi?id=32354>
                  ;; and <http://bugs.gnu.org/20358>.
                  (substitute* "libstdc++-v3/src/Makefile.in"
                    (("^OPT_LDFLAGS = ")
                     "OPT_LDFLAGS = -Wl,-rpath=$(libdir) "))

                  ;; Move libstdc++*-gdb.py to the "lib" output to avoid a
                  ;; circularity between "out" and "lib".  (Note:
                  ;; --with-python-dir is useless because it imposes $(prefix) as
                  ;; the parent directory.)
                  (substitute* "libstdc++-v3/python/Makefile.in"
                    (("pythondir = .*$")
                     (string-append "pythondir = " libdir "/share"
                                    "/gcc-$(gcc_version)/python\n")))

                  ;; Avoid another circularity between the outputs: this #define
                  ;; ends up in auto-host.h in the "lib" output, referring to
                  ;; "out".  (This variable is used to augment cpp's search path,
                  ;; but there's nothing useful to look for here.)
                  (substitute* "gcc/config.in"
                    (("PREFIX_INCLUDE_DIR")
                     "PREFIX_INCLUDE_DIR_isnt_necessary_here")))))

            (add-after 'configure 'post-configure
              (lambda _
                ;; Don't store configure flags, to avoid retaining references to
                ;; build-time dependencies---e.g., `--with-ppl=/gnu/store/xxx'.
                (substitute* "Makefile"
                  (("^TOPLEVEL_CONFIGURE_ARGUMENTS=(.*)$" _ rest)
                   "TOPLEVEL_CONFIGURE_ARGUMENTS=\n")))))))

       (native-search-paths
        ;; Use the language-specific variables rather than 'CPATH' because they
        ;; are equivalent to '-isystem' whereas 'CPATH' is equivalent to '-I'.
        ;; The intent is to allow headers that are in the search path to be
        ;; treated as "system headers" (headers exempt from warnings) just like
        ;; the typical /usr/include headers on an FHS system.
        (list (search-path-specification
               (variable "C_INCLUDE_PATH")
               (files '("include")))
              (search-path-specification
               (variable "CPLUS_INCLUDE_PATH")
               ;; Add 'include/c++' here so that <cstdlib>'s "#include_next
               ;; <stdlib.h>" finds GCC's <stdlib.h>, not libc's.
               (files '("include/c++" "include")))
              (search-path-specification
               (variable "LIBRARY_PATH")
               (files '("lib" "lib64")))))

       (properties `((gcc-libc . ,(assoc-ref inputs "libc"))))
       (synopsis "GNU Compiler Collection")
       (description
        "GCC is the GNU Compiler Collection.  It provides compiler front-ends
for several languages, including C, C++, Objective-C, Fortran, Java, Ada, and
Go.  It also includes runtime support libraries for these languages.")
       (license gpl3+)
       (supported-systems (delete "aarch64-linux" %supported-systems))
       (home-page "https://gcc.gnu.org/")))))

(define-public gcc-4.8
  (package (inherit gcc-4.7)
    (version "4.8.5")
    (source (origin
              (method url-fetch)
              (uri (string-append "mirror://gnu/gcc/gcc-"
                                  version "/gcc-" version ".tar.bz2"))
              (sha256
               (base32
                "08yggr18v373a1ihj0rg2vd6psnic42b518xcgp3r9k81xz1xyr2"))
              (patches (search-patches "gcc-arm-link-spec-fix.patch"
                                       "gcc-4.8-libsanitizer-fix.patch"
                                       "gcc-asan-missing-include.patch"
                                       "gcc-fix-texi2pod.patch"))
              (modules '((guix build utils)))
              ;; This is required for building with glibc-2.26.
              ;; https://gcc.gnu.org/bugzilla/show_bug.cgi?id=81712
              (snippet
               '(for-each
                 (lambda (dir)
                   (substitute* (string-append "libgcc/config/"
                                               dir "/linux-unwind.h")
                     (("struct ucontext") "ucontext_t")))
                 '("aarch64" "alpha" "bfin" "i386" "m68k"
                   "pa" "sh" "tilepro" "xtensa")))))
    (supported-systems %supported-systems)
    (inputs
     (modify-inputs (package-inputs gcc-4.7)
       (prepend isl-0.11 cloog)))))

(define-public gcc-4.9
  (package (inherit gcc-4.8)
    (version "4.9.4")
    (source (origin
              (method url-fetch)
              (uri (string-append "mirror://gnu/gcc/gcc-"
                                  version "/gcc-" version ".tar.bz2"))
              (sha256
               (base32
                "14l06m7nvcvb0igkbip58x59w3nq6315k6jcz3wr9ch1rn9d44bc"))
              (patches (search-patches "gcc-4.9-libsanitizer-fix.patch"
                                       "gcc-4.9-libsanitizer-ustat.patch"
                                       "gcc-4.9-libsanitizer-mode-size.patch"
                                       "gcc-arm-bug-71399.patch"
                                       "gcc-asan-missing-include.patch"
                                       "gcc-libvtv-runpath.patch"
                                       "gcc-fix-texi2pod.patch"))
              (modules '((guix build utils)))
              ;; This is required for building with glibc-2.26.
              ;; https://gcc.gnu.org/bugzilla/show_bug.cgi?id=81712
              (snippet
               '(for-each
                 (lambda (dir)
                   (substitute* (string-append "libgcc/config/"
                                               dir "/linux-unwind.h")
                     (("struct ucontext") "ucontext_t")))
                 '("aarch64" "alpha" "bfin" "i386" "m68k" "nios2"
                   "pa" "sh" "tilepro" "xtensa")))))
    ;; Override inherited texinfo-5 with latest version.
    (native-inputs (list perl ;for manpages
                         texinfo))
    (arguments
     ;; Since 'arguments' is a function of the package's version, define
     ;; 'parent' such that the 'arguments' thunk gets to see the right
     ;; version.
     (let ((parent (package
                     (inherit gcc-4.8)
                     (version (package-version this-package)))))
       (if (%current-target-system)
           (package-arguments parent)
           ;; For native builds of GCC 4.9 and GCC 5, the C++ include path needs
           ;; to be adjusted so it does not interfere with GCC's own build processes.
           (substitute-keyword-arguments (package-arguments parent)
             ((#:modules modules %gnu-build-system-modules)
              `((srfi srfi-1)
                ,@modules))
             ((#:phases phases)
              `(modify-phases ,phases
                 (add-after 'set-paths 'adjust-CPLUS_INCLUDE_PATH
                   (lambda* (#:key inputs #:allow-other-keys)
                     (let ((libc (assoc-ref inputs "libc"))
                           (gcc (assoc-ref inputs  "gcc")))
                       (setenv "CPLUS_INCLUDE_PATH"
                               (string-join (fold delete
                                                  (string-split (getenv "CPLUS_INCLUDE_PATH")
                                                                #\:)
                                                  (list (string-append libc "/include")
                                                        (string-append gcc "/include/c++")))
                                            ":"))
                       (format #t
                               "environment variable `CPLUS_INCLUDE_PATH' changed to ~a~%"
                               (getenv "CPLUS_INCLUDE_PATH")))))))))))))

(define gcc-canadian-cross-objdump-snippet
  ;; Fix 'libcc1/configure' error when cross-compiling GCC.  Without that,
  ;; 'libcc1/configure' wrongfully determines that '-rdynamic' support is
  ;; missing because $gcc_cv_objdump is empty:
  ;;
  ;;   https://gcc.gnu.org/bugzilla/show_bug.cgi?id=67590
  ;;   http://cgit.openembedded.org/openembedded-core/commit/?id=f6e47aa9b12f9ab61530c40e0343f451699d9077
  #~(substitute* "libcc1/configure"
      (("\\$gcc_cv_objdump -T")
       "$OBJDUMP_FOR_TARGET -T")))

(define-public gcc-5
  ;; Note: GCC >= 5 ships with .info files but 'make install' fails to install
  ;; them in a VPATH build.
  (package (inherit gcc-4.9)
    (version "5.5.0")
    (source (origin
              (method url-fetch)
              (uri (string-append "mirror://gnu/gcc/gcc-"
                                  version "/gcc-" version ".tar.xz"))
              (sha256
               (base32
                "11zd1hgzkli3b2v70qsm2hyqppngd4616qc96lmm9zl2kl9yl32k"))
              (patches (search-patches "gcc-arm-bug-71399.patch"
                                       "gcc-libsanitizer-ustat.patch"
                                       "gcc-strmov-store-file-names.patch"
                                       "gcc-5.0-libvtv-runpath.patch"
                                       "gcc-5-source-date-epoch-1.patch"
                                       "gcc-5-source-date-epoch-2.patch"
                                       "gcc-6-libsanitizer-mode-size.patch"
                                       "gcc-fix-texi2pod.patch"
                                       "gcc-5-hurd.patch"
                                       ;; See https://gcc.gnu.org/bugzilla/show_bug.cgi?id=86162
                                       "gcc-5-fix-powerpc64le-build.patch"))
              (modules '((guix build utils)))
              (snippet gcc-canadian-cross-objdump-snippet)))
    (inputs
     (modify-inputs (package-inputs gcc-4.7)
       (prepend ;; GCC5 needs <isl/band.h> which is removed in later versions.
                isl-0.18)))))

(define-public gcc-6
  (package
    (inherit gcc-5)
    (version "6.5.0")
    (source (origin
              (method url-fetch)
              (uri (string-append "mirror://gnu/gcc/gcc-"
                                  version "/gcc-" version ".tar.xz"))
              (sha256
               (base32
                "0i89fksfp6wr1xg9l8296aslcymv2idn60ip31wr9s4pwin7kwby"))
              (patches (search-patches "gcc-strmov-store-file-names.patch"
                                       "gcc-6-libsanitizer-mode-size.patch"
                                       "gcc-6-source-date-epoch-1.patch"
                                       "gcc-6-source-date-epoch-2.patch"
                                       "gcc-5.0-libvtv-runpath.patch"))))

    ;; GCC 4.9 and 5 has a workaround that is not needed for GCC 6 and later.
    (arguments (package-arguments gcc-4.8))

    (inputs
     `(("isl" ,isl)

       ;; XXX: This gross hack allows us to have libstdc++'s <bits/c++config.h>
       ;; in the search path, thereby avoiding misconfiguration of libstdc++:
       ;; <https://bugs.gnu.org/42392>.
       ("libstdc++" ,libstdc++-headers)

       ,@(package-inputs gcc-4.7)))))

(define %gcc-7.5-aarch64-micro-architectures
  ;; Suitable '-march' values for GCC 7.5.
  ;; TODO: Allow dynamically adding feature flags.
  '("armv8-a" "armv8.1-a" "armv8.2-a" "armv8.3-a"))

(define %gcc-7.5-armhf-micro-architectures
  ;; Suitable '-march' values for GCC 7.5.
  ;; TODO: Allow dynamically adding feature flags.
  '("armv7" "armv7-a" "armv7-m" "armv7-r" "armv7e-m" "armv7ve"
    "armv8-a" "armv8-a+crc" "armv8.1-a" "armv8.1-a+crc"
    "armv8-m.base" "armv8-m.main" "armv8-m.main+dsp"
    "iwmmxt" "iwmmxt2"))

(define %gcc-7.5-x86_64-micro-architectures
  ;; Suitable '-march' values for GCC 7.5 (info "(gcc) x86 Options").
  '("core2" "nehalem" "westmere" "sandybridge" "ivybridge"
    "haswell" "broadwell" "skylake" "bonnell" "silvermont"
    "knl" "skylake-avx512"

    "k8" "k8-sse3" "barcelona"
    "bdver1" "bdver2" "bdver3" "bdver4"
    "znver1"
    "btver1" "btver2" "geode"))

(define %gcc-10-aarch64-micro-architectures
  ;; Suitable '-march' values for GCC 10.
  ;; TODO: Allow dynamically adding feature flags.
  (append %gcc-7.5-aarch64-micro-architectures
          '("armv8.4-a" "armv8.5-a" "armv8.6-a")))

(define %gcc-10-armhf-micro-architectures
  ;; Suitable '-march' values for GCC 10.
  ;; TODO: Allow dynamically adding feature flags.
  (append %gcc-7.5-armhf-micro-architectures
          '("armv8.2-a" "armv8.3-a" "armv8.4-a" "armv8.5-a" "armv8.6-a"
            "armv8-r" "armv8.1-m.main")))

(define %gcc-10-x86_64-micro-architectures
  ;; Suitable '-march' values for GCC 10.
  (append %gcc-7.5-x86_64-micro-architectures
      '("goldmont" "goldmont-plus" "tremont"
        "knm" "cannonlake" "icelake-client" "icelake-server"
        "cascadelake" "cooperlake" "tigerlake"

        "znver2" "znver3")))

(define %gcc-11-aarch64-micro-architectures
  ;; Suitable '-march' values for GCC 11.
  %gcc-10-aarch64-micro-architectures)            ;unchanged

(define %gcc-11-armhf-micro-architectures
  %gcc-10-armhf-micro-architectures)

(define %gcc-11-x86_64-micro-architectures
  ;; Suitable '-march' values for GCC 11.
  (append %gcc-10-x86_64-micro-architectures
          '("sapphirerapids" "alterlake" "rocketlake" ;Intel

            "btver1" "btver2")))                  ;AMD

(define-public gcc-7
  (package
    (inherit gcc-6)
    (version "7.5.0")
    (source (origin
              (method url-fetch)
              (uri (string-append "mirror://gnu/gcc/gcc-"
                                  version "/gcc-" version ".tar.xz"))
              (sha256
               (base32
                "0qg6kqc5l72hpnj4vr6l0p69qav0rh4anlkk3y55540zy3klc6dq"))
              (patches (search-patches "gcc-strmov-store-file-names.patch"
                                       "gcc-7-libsanitizer-mode-size.patch"
                                       "gcc-5.0-libvtv-runpath.patch"))))
    (description
     "GCC is the GNU Compiler Collection.  It provides compiler front-ends
for several languages, including C, C++, Objective-C, Fortran, Ada, and Go.
It also includes runtime support libraries for these languages.")
    (properties
     `((compiler-cpu-architectures
        ("aarch64" ,@%gcc-7.5-aarch64-micro-architectures)
        ("armhf" ,@%gcc-7.5-armhf-micro-architectures)
        ("x86_64" ,@%gcc-7.5-x86_64-micro-architectures))
       ,@(package-properties gcc-6)))))

(define-public gcc-8
  (package
    (inherit gcc-7)
    (version "8.5.0")
    (source (origin
              (method url-fetch)
              (uri (string-append "mirror://gnu/gcc/gcc-"
                                  version "/gcc-" version ".tar.xz"))
              (sha256
               (base32
                "0l7d4m9jx124xsk6xardchgy2k5j5l2b15q322k31f0va4d8826k"))
              (patches (search-patches "gcc-8-strmov-store-file-names.patch"
                                       "gcc-5.0-libvtv-runpath.patch"
                                       "gcc-8-sort-libtool-find-output.patch"))
              (modules '((guix build utils)))
              (snippet gcc-canadian-cross-objdump-snippet)))))

(define-public gcc-9
  (package
   (inherit gcc-8)
   (version "9.5.0")
   (source (origin
            (method url-fetch)
            (uri (string-append "mirror://gnu/gcc/gcc-"
                                version "/gcc-" version ".tar.xz"))
            (sha256
             (base32
              "13ygjmd938m0wmy946pxdhz9i1wq7z4w10l6pvidak0xxxj9yxi7"))
            (patches (search-patches "gcc-9-strmov-store-file-names.patch"
                                     "gcc-9-asan-fix-limits-include.patch"
                                     "gcc-5.0-libvtv-runpath.patch"))
            (modules '((guix build utils)))
            (snippet gcc-canadian-cross-objdump-snippet)))))

(define-public gcc-10
  (package
   (inherit gcc-8)
   (version "10.4.0")
   (source (origin
            (method url-fetch)
            (uri (string-append "mirror://gnu/gcc/gcc-"
                                version "/gcc-" version ".tar.xz"))
            (sha256
             (base32
              "1wg4xdizkksmwi66mvv2v4pk3ja8x64m7v9gzhykzd3wrmdpsaf9"))
            (patches (search-patches "gcc-9-strmov-store-file-names.patch"
                                     "gcc-5.0-libvtv-runpath.patch"
                                     "gcc-10-tree-sra-union-handling.patch"))
            (modules '((guix build utils)))
            (snippet gcc-canadian-cross-objdump-snippet)))
   (properties
    `((compiler-cpu-architectures
       ("aarch64" ,@%gcc-10-aarch64-micro-architectures)
       ("armhf" ,@%gcc-10-armhf-micro-architectures)
       ("x86_64" ,@%gcc-10-x86_64-micro-architectures))
      ,@(package-properties gcc-8)))))

(define-public gcc-11
  (package
   (inherit gcc-8)
   (version "11.3.0")
   (source (origin
            (method url-fetch)
            (uri (string-append "mirror://gnu/gcc/gcc-"
                                version "/gcc-" version ".tar.xz"))
            (sha256
             (base32
              "0fdclcwf728wbq52vphfcjywzhpsjp3kifzj3pib3xcihs0z4z5l"))
            (patches (search-patches "gcc-9-strmov-store-file-names.patch"
                                     "gcc-5.0-libvtv-runpath.patch"
                                     "gcc-10-tree-sra-union-handling.patch"))
            (modules '((guix build utils)))
            (snippet gcc-canadian-cross-objdump-snippet)))
   (arguments
    (substitute-keyword-arguments (package-arguments gcc-8)
      ((#:phases phases #~%standard-phases)
       (if (target-hurd?)
           #~(modify-phases #$phases
               (add-after 'unpack 'patch-hurd-libpthread
                 (lambda _
                   (define patch
                     #$(local-file
                        (search-patch "gcc-11-libstdc++-hurd-libpthread.patch")))
                   (invoke "patch" "--force" "-p1" "-i" patch))))
           phases))))
   (properties
    `((compiler-cpu-architectures
       ("aarch64" ,@%gcc-11-aarch64-micro-architectures)
       ("armhf" ,@%gcc-11-armhf-micro-architectures)
       ("x86_64" ,@%gcc-11-x86_64-micro-architectures))
      ,@(package-properties gcc-8)))))

(define-public gcc-12
  (package
    (inherit gcc-11)
    ;; Note: 'compiler-cpu-architectures' is unchanged compared to GCC 11.
    (version "12.2.0")
    (source (origin
              (method url-fetch)
              (uri (string-append "mirror://gnu/gcc/gcc-"
                                  version "/gcc-" version ".tar.xz"))
              (sha256
               (base32
                "1zrhca90c7hqnjz3jgr1vl675q3h5lrd92b5ggi00jjryffcyjg5"))
              (patches (search-patches "gcc-12-strmov-store-file-names.patch"
                                       "gcc-5.0-libvtv-runpath.patch"))
              (modules '((guix build utils)))
              (snippet gcc-canadian-cross-objdump-snippet)))))


;; Note: When changing the default gcc version, update
;;       the gcc-toolchain-* definitions.
(define-public gcc gcc-11)


;;;
;;; Historical version.
;;;

(define-public gcc-2.95
  ;; Note: 'gcc-core-mesboot0' in commencement.scm provides 2.95 as well, but
  ;; with additional tricks to support compilation with TinyCC and Mes-libc.
  (package
    (inherit gcc)
    (version "2.95.3")
    (source (origin
              (method url-fetch)
              (uri (string-append "mirror://gnu/gcc/gcc-2.95.3/gcc-core-"
                                  version ".tar.gz"))
              (sha256
               (base32
                "1xvfy4pqhrd5v2cv8lzf63iqg92k09g6z9n2ah6ndd4h17k1x0an"))
              (modules '((guix build utils)))
              (snippet
               '(begin
                  ;; Do not build the bundled Texinfo.
                  (delete-file-recursively "texinfo")
                  (substitute* "configure"
                    (("host_tools=(.*)texinfo" _ before)
                     (string-append "host_tools=" before)))
                  ;; Fix building on arm* with gcc-4+
                  (substitute* "gcc/config/arm/arm.c"
                    (("arm_prog_mode") "arm_prgmode"))))))
    (supported-systems (fold delete %supported-systems
                             '("powerpc64le-linux" "riscv64-linux")))
    (native-inputs (list texinfo dejagnu))
    (inputs '())
    (propagated-inputs '())
    (outputs '("out"))
    (arguments
     (let ((matching-system
             (match (%current-system)
               ;; This package predates our 64-bit architectures.
               ;; Force a 32-bit build targeting a similar architecture.
               ("aarch64-linux"
                "armhf-linux")
               ("x86_64-linux"
                "i686-linux")
               (_
                (%current-system)))))
       (list #:system matching-system
             #:configure-flags #~'("--disable-werror")

             #:phases
             #~(modify-phases %standard-phases
                 (add-before 'configure 'set-dynamic-linker-file-name
                   (lambda* (#:key inputs #:allow-other-keys)
                     ;; Tell GCC what the real loader file name is.
                     (substitute* '("gcc/config/alpha/linux-elf.h"
                                    "gcc/config/m68k/linux.h"
                                    "gcc/config/mips/linux.h"
                                    "gcc/config/rs6000/linux.h")
                       (("/lib/ld\\.so\\.1")
                        (search-input-file
                          inputs #$(glibc-dynamic-linker matching-system))))
                     (substitute* "gcc/config/i386/gnu.h"
                       (("/lib/ld\\.so")
                        (search-input-file
                          inputs #$(glibc-dynamic-linker matching-system))))
                     (substitute* '("gcc/config/alpha/linux-elf.h"
                                    "gcc/config/arm/linux-elf.h"
                                    "gcc/config/i386/linux.h"
                                    "gcc/config/m68k/linux.h"
                                    "gcc/config/sparc/linux.h"
                                    "gcc/config/sparc/linux64.h")
                       (("/lib(64)?/ld-linux\\.so\\.[12]")
                        (search-input-file
                          inputs #$(glibc-dynamic-linker matching-system))))))
                 (replace 'configure
                   (lambda* (#:key outputs build configure-flags
                             #:allow-other-keys)
                     ;; It's an old 'configure' script so it needs some help.
                     (setenv "CONFIG_SHELL" (which "sh"))
                     (apply invoke "./configure"
                            (string-append "--prefix=" #$output)
                            (string-append "--build=" build)
                            (string-append "--host=" build)
                            configure-flags)))
                 (add-before 'configure 'remove-bundled-texinfo
                   (lambda _
                     ;; Go ahead despite the many warnings.
                     (substitute* '("Makefile.in" "gcc/Makefile.in")
                       (("^MAKEINFOFLAGS =.*")
                        "MAKEINFOFLAGS = --force\n"))))))))
    (native-search-paths
     ;; This package supports nothing but the C language.
     (list (search-path-specification
            (variable "C_INCLUDE_PATH")
            (files '("include")))
           (search-path-specification
            (variable "LIBRARY_PATH")
            (files '("lib")))))))


(define-public (make-libstdc++ gcc)
  "Return a libstdc++ package based on GCC.  The primary use case is when
using compilers other than GCC."
  (package
    (inherit gcc)
    (name "libstdc++")
    (arguments
     `(#:out-of-source? #t
       #:modules ((srfi srfi-1)
                  (srfi srfi-26)
                  ,@%gnu-build-system-modules)
       #:phases
       (modify-phases %standard-phases
         ,@(if (version>=? (package-version gcc) "11")
               `((add-after 'unpack 'hide-gcc-headers
                   (lambda* (#:key native-inputs inputs #:allow-other-keys)
                     (let ((gcc (assoc-ref (or native-inputs inputs)
                                           ,(if (%current-target-system)
                                                "cross-gcc"
                                                "gcc"))))
                       ;; Fix a regression in GCC 11 where the GCC headers
                       ;; shadows glibc headers when building libstdc++.  An
                       ;; upstream fix was added in GCC 11.3.0, but it only
                       ;; hides system include directories, not those on
                       ;; CPLUS_INCLUDE_PATH.  See discussion at
                       ;; <https://gcc.gnu.org/bugzilla/show_bug.cgi?id=100017>
                       ;; and the similar adjustment in GCC-FINAL.
                       (substitute* "libstdc++-v3/src/c++17/Makefile.in"
                         (("AM_CXXFLAGS = ")
                          (string-append ,(if (%current-target-system)
                                              "CROSS_CPLUS_INCLUDE_PATH = "
                                              "CPLUS_INCLUDE_PATH = ")
                                         (string-join
                                          (remove (cut string-prefix? gcc <>)
                                                  (string-split
                                                   (getenv
                                                    ,(if (%current-target-system)
                                                         "CROSS_CPLUS_INCLUDE_PATH"
                                                         "CPLUS_INCLUDE_PATH"))
                                                   #\:))
                                          ":")
                                         "\nAM_CXXFLAGS = ")))))))
               '())
         ,@(let ((version (package-version gcc)))
                (if (and (target-ppc64le?)
                    (version>=? version "11")
                    (not (version>=? version "12")))
               `((add-after 'unpack 'patch-powerpc
                   (lambda* (#:key inputs #:allow-other-keys)
                     (invoke "patch" "--force" "-p1" "-i"
                             (assoc-ref inputs "powerpc64le-patch")))))
               '()))
         ;; Force rs6000 (i.e., powerpc) libdir to be /lib and not /lib64.
         (add-before 'chdir 'fix-rs6000-libdir
           (lambda _
             (when (file-exists? "gcc/config/rs6000")
               (substitute* (find-files "gcc/config/rs6000")
                 (("/lib64") "/lib")))))
         (add-before 'configure 'chdir
           (lambda _
             (chdir "libstdc++-v3"))))

       #:configure-flags `("--disable-libstdcxx-pch"
                           ,(string-append "--with-gxx-include-dir="
                                           (assoc-ref %outputs "out")
                                           "/include"))))
    (outputs '("out" "debug"))
    (inputs '())
    (native-inputs
     `(,@(if (and (target-ppc64le?)
                  (let ((version (package-version gcc)))
                    (and
                     (version>=? version "11")
                     (not (version>=? version "12")))))
             `(("powerpc64le-patch" ,(search-patch "gcc-11-libstdc++-powerpc.patch")))
             '())))
    (propagated-inputs '())
    (synopsis "GNU C++ standard library")))

(define libstdc++
  ;; Libstdc++ matching the default GCC.
  (make-libstdc++ gcc))

(define libstdc++-headers
  ;; XXX: This package is for internal use to work around
  ;; <https://bugs.gnu.org/42392> (see above).  The main difference compared
  ;; to the libstdc++ headers that come with 'gcc' is that <bits/c++config.h>
  ;; is right under include/c++ and not under
  ;; include/c++/x86_64-unknown-linux-gnu (aka. GPLUSPLUS_TOOL_INCLUDE_DIR).
  (package
    (inherit libstdc++)
    (name "libstdc++-headers")
    (outputs '("out"))
    (build-system trivial-build-system)
    (arguments
     '(#:builder (let* ((out       (assoc-ref %outputs "out"))
                        (libstdc++ (assoc-ref %build-inputs "libstdc++")))
                   (mkdir out)
                   (mkdir (string-append out "/include"))
                   (symlink (string-append libstdc++ "/include")
                            (string-append out "/include/c++")))))
    (inputs `(("libstdc++" ,libstdc++)))
    (synopsis "Headers of GNU libstdc++")))

(define-public libstdc++-4.9
  (make-libstdc++ gcc-4.9))

(define (make-libiberty gcc)
  "Return a libiberty package based on GCC."
  (package
    (inherit gcc)
    (name "libiberty")
    (arguments
     `(#:out-of-source? #t
       #:phases
       (modify-phases %standard-phases
         (add-before 'configure 'chdir
           (lambda _
             (chdir "libiberty")))
         (replace 'install
           (lambda* (#:key outputs #:allow-other-keys)
             (let* ((out     (assoc-ref outputs "out"))
                    (lib     (string-append out "/lib/"))
                    (include (string-append out "/include/")))
               (install-file "libiberty.a" lib)
               (install-file "../include/libiberty.h" include)))))))
    (inputs '())
    (outputs '("out"))
    (native-inputs '())
    (propagated-inputs '())
    (properties '())
    (synopsis "Collection of subroutines used by various GNU programs")))

(define-public libiberty
  (make-libiberty gcc))

(define* (custom-gcc gcc name languages
                     #:optional
                     (search-paths (package-native-search-paths gcc))
                     #:key (separate-lib-output? #t))
  "Return a custom version of GCC that supports LANGUAGES.  Use SEARCH-PATHS
as the 'native-search-paths' field."
  (package (inherit gcc)
    (name name)
    (outputs (if separate-lib-output?
                 (package-outputs gcc)
                 (delete "lib" (package-outputs gcc))))
    (native-search-paths search-paths)
    (properties (alist-delete 'hidden? (package-properties gcc)))
    (arguments
     (substitute-keyword-arguments (package-arguments gcc)
       ((#:modules modules %gnu-build-system-modules)
        `(,@modules
          (srfi srfi-1)
          (srfi srfi-26)
          (ice-9 regex)))
       ((#:configure-flags flags)
        `(cons (string-append "--enable-languages="
                              ,(string-join languages ","))
               (remove (cut string-match "--enable-languages.*" <>)
                       ,flags)))
       ((#:phases phases)
        `(modify-phases ,phases
           (add-after 'install 'remove-broken-or-conflicting-files
             (lambda* (#:key outputs #:allow-other-keys)
               (for-each
                delete-file
                (find-files (string-append (assoc-ref outputs "out") "/bin")
                            ".*(c\\+\\+|cpp|g\\+\\+|gcov|gcc|lto)(-.*)?$"))))))))))

(define %generic-search-paths
  ;; This is the language-neutral search path for GCC.  Entries in $CPATH are
  ;; not considered "system headers", which means GCC can raise warnings for
  ;; issues in those headers.  'CPATH' is the only one that works for
  ;; front-ends not in the C family.
  (list (search-path-specification
         (variable "CPATH")
         (files '("include")))
        (search-path-specification
         (variable "LIBRARY_PATH")
         (files '("lib" "lib64")))))

(define-public gfortran
  (hidden-package
   (custom-gcc gcc
               "gfortran" '("fortran")
               %generic-search-paths)))

(define-public gfortran-7
  (hidden-package
   (custom-gcc gcc-7
               "gfortran" '("fortran")
               %generic-search-paths)))

(define-public gdc-10
  (hidden-package
   (custom-gcc gcc-10 "gdc" '("d")
               %generic-search-paths)))

(define-public gdc-11
  (hidden-package
   (custom-gcc gcc-11 "gdc" '("d")
               %generic-search-paths)))

(define-public (make-libgccjit gcc)
  (package
    (inherit gcc)
    (name "libgccjit")
    (outputs (delete "lib" (package-outputs gcc)))
    (properties (alist-delete 'hidden? (package-properties gcc)))
    (arguments
     (substitute-keyword-arguments (package-arguments gcc)
       ((#:modules _ '())
        '((guix build gnu-build-system)
          (guix build utils)
          (ice-9 regex)
          (srfi srfi-1)
          (srfi srfi-26)))
       ((#:configure-flags flags)
        #~(cons* "--disable-bootstrap"
                 "--disable-libatomic"
                 "--disable-libgomp"
                 "--disable-libquadmath"
                 "--disable-libssp"
                 "--enable-host-shared"
                 "--enable-checking=release"
                 "--enable-languages=jit"
                 (remove (cut string-match "--enable-languages.*" <>)
                         #$flags)))
       ((#:phases phases)
        #~(modify-phases #$phases
            (add-after 'install 'remove-broken-or-conflicting-files
              (lambda* (#:key outputs #:allow-other-keys)
                (for-each delete-file
                          (find-files
                           (string-append (assoc-ref outputs "out") "/bin")
                           ".*(c\\+\\+|cpp|g\\+\\+|gcov|gcc|gcc-.*)"))))))))
    (inputs (modify-inputs (package-inputs gcc)
              (delete "libstdc++")))
    (native-inputs (modify-inputs (package-native-inputs gcc)
                     (prepend gcc)))
    (synopsis "GCC library generating machine code on-the-fly at runtime")
    (description
     "This package is part of the GNU Compiler Collection and provides an
embeddable library for generating machine code on-the-fly at runtime.  This
shared library can then be dynamically-linked into bytecode interpreters and
other such programs that want to generate machine code on-the-fly at run-time.
It can also be used for ahead-of-time code generation for building standalone
compilers.  The just-in-time (jit) part of the name is now something of a
misnomer.")))

(define-public libgccjit-9 (make-libgccjit gcc-9))
(define-public libgccjit-10 (make-libgccjit gcc-10))
(define-public libgccjit-11 (make-libgccjit gcc-11))
(define-public libgccjit-12 (make-libgccjit gcc-12))

(define-public libgccjit libgccjit-10)

(define (make-gccgo gcc)
  "Return a gccgo package based on GCC."
  (let ((gccgo (custom-gcc gcc "gccgo" '("go") %generic-search-paths)))
    (package
      (inherit gccgo)
      (synopsis "Go frontend to GCC")
      (description
        "This package is part of the GNU Compiler Collection and
provides the GNU compiler for the Go programming language.")
      (arguments
       (substitute-keyword-arguments (package-arguments gccgo)
         ((#:phases phases)
          `(modify-phases ,phases
             (add-after 'install 'wrap-go-with-tool-path
               (lambda* (#:key outputs #:allow-other-keys)
                 (let* ((out (assoc-ref outputs "out"))
                        (exedir (string-append out "/libexec/gcc"))
                        (tooldir (dirname (car (find-files exedir "^cgo$")))))
                   (wrap-program (string-append out "/bin/go")
                     `("GCCGOTOOLDIR" =
                       (,(string-append "${GCCGOTOOLDIR:-" tooldir "}")))
                     `("GOROOT" =
                       (,(string-append "${GOROOT:-" out "}")))))))
             (add-before 'configure 'fix-gotools-runpath
               (lambda _
                 (substitute* "gotools/Makefile.in"
                   (("AM_LDFLAGS =" all)
                    (string-append all " -Wl,-rpath=$(libdir) ")))))
             (add-before 'configure 'remove-tool-reference-from-libgo
               (lambda _
                 (substitute* "libgo/Makefile.in"
                   (("(GccgoToolDir = \\\")[^\\\"]+" _ start)
                    (string-append start "/nonexistent"))
                   ,@(if (version>=? (package-version gccgo) "12.0")
                       '((("(defaultGOROOT = `)[^`]+" _ start)
                          (string-append start "/nonexistent")))
                       '((("(DefaultGoroot = \\\")[^\\\"]+" _ start)
                          (string-append start "/nonexistent"))))
                   (("(defaultGOROOTValue.*?return `)[^`]+" _ start)
                    (string-append start "/nonexistent"))))))))))))

(define-public gccgo-4.9
  (custom-gcc (package
                (inherit gcc-4.9)
                (synopsis "Go frontend to GCC")
                (description
                 "This package is part of the GNU Compiler Collection and
provides the GNU compiler for the Go programming language."))
              "gccgo" '("go")
              %generic-search-paths
              ;; Suppress the separate "lib" output, because otherwise the
              ;; "lib" and "out" outputs would refer to each other, creating
              ;; a cyclic dependency.  <http://debbugs.gnu.org/18101>
              #:separate-lib-output? #f))

(define-public gccgo-10
  (make-gccgo gcc-10))

(define-public gccgo-11
  (make-gccgo gcc-11))

(define-public gccgo-12
  (make-gccgo gcc-12))

(define %objc-search-paths
  (list (search-path-specification
         (variable "OBJC_INCLUDE_PATH")
         (files '("include")))
        (search-path-specification
         (variable "LIBRARY_PATH")
         (files '("lib" "lib64")))))

(define-public gcc-objc-4.8
  (custom-gcc gcc-4.8 "gcc-objc" '("objc")
              %objc-search-paths))

(define-public gcc-objc-4.9
  (custom-gcc gcc-4.9 "gcc-objc" '("objc")
              %objc-search-paths))

(define-public gcc-objc-5
  (custom-gcc gcc-5 "gcc-objc" '("objc")
              %objc-search-paths))

(define-public gcc-objc-6
  (custom-gcc gcc-6 "gcc-objc" '("objc")
              %objc-search-paths))

(define-public gcc-objc-7
  (custom-gcc gcc-7 "gcc-objc" '("objc")
              %objc-search-paths))

(define-public gcc-objc-8
  (custom-gcc gcc-8 "gcc-objc" '("objc")
              %objc-search-paths))

(define-public gcc-objc-9
  (custom-gcc gcc-9 "gcc-objc" '("objc")
              %objc-search-paths))

(define-public gcc-objc-10
  (custom-gcc gcc-10 "gcc-objc" '("objc")
              %objc-search-paths))

(define-public gcc-objc-11
  (custom-gcc gcc-11 "gcc-objc" '("objc")
              %objc-search-paths))

(define-public gcc-objc-12
  (custom-gcc gcc-12 "gcc-objc" '("objc")
              %objc-search-paths))

(define-public gcc-objc gcc-objc-11)

(define %objc++-search-paths
  (list (search-path-specification
         (variable "OBJCPLUS_INCLUDE_PATH")
         (files '("include")))
        (search-path-specification
         (variable "LIBRARY_PATH")
         (files '("lib" "lib64")))))

(define-public gcc-objc++-4.8
  (custom-gcc gcc-4.8 "gcc-objc++" '("obj-c++")
              %objc++-search-paths))

(define-public gcc-objc++-4.9
  (custom-gcc gcc-4.9 "gcc-objc++" '("obj-c++")
              %objc++-search-paths))

(define-public gcc-objc++-5
  (custom-gcc gcc-5 "gcc-objc++" '("obj-c++")
              %objc++-search-paths))

(define-public gcc-objc++-6
  (custom-gcc gcc-6 "gcc-objc++" '("obj-c++")
              %objc++-search-paths))

(define-public gcc-objc++-7
  (custom-gcc gcc-7 "gcc-objc++" '("obj-c++")
              %objc++-search-paths))

(define-public gcc-objc++-8
  (custom-gcc gcc-8 "gcc-objc++" '("obj-c++")
              %objc++-search-paths))

(define-public gcc-objc++-9
  (custom-gcc gcc-9 "gcc-objc++" '("obj-c++")
              %objc++-search-paths))

(define-public gcc-objc++-10
  (custom-gcc gcc-10 "gcc-objc++" '("obj-c++")
              %objc++-search-paths))

(define-public gcc-objc++-11
  (custom-gcc gcc-11 "gcc-objc++" '("obj-c++")
              %objc++-search-paths))

(define-public gcc-objc++-12
  (custom-gcc gcc-12 "gcc-objc++" '("obj-c++")
              %objc++-search-paths))

(define-public gcc-objc++ gcc-objc++-11)

(define (make-libstdc++-doc gcc)
  "Return a package with the libstdc++ documentation for GCC."
  (package
    (inherit gcc)
    (name "libstdc++-doc")
    (version (package-version gcc))
    (synopsis "GNU libstdc++ documentation")
    (outputs '("out"))
    (native-inputs (list doxygen
                         texinfo
                         libxml2
                         libxslt
                         docbook-xml
                         docbook-xsl
                         graphviz)) ;for 'dot', invoked by 'doxygen'
    (inputs '())
    (propagated-inputs '())
    (arguments
     '(#:out-of-source? #t
       #:tests? #f                                ;it's just documentation
       #:phases (modify-phases %standard-phases
                  (add-before 'configure 'chdir
                              (lambda _
                                (chdir "libstdc++-v3")))
                  (add-before 'configure 'set-xsl-directory
                              (lambda* (#:key inputs #:allow-other-keys)
                                (let ((docbook (assoc-ref inputs "docbook-xsl")))
                                  (substitute* (find-files "doc"
                                                           "^Makefile\\.in$")
                                    (("@XSL_STYLE_DIR@")
                                     (string-append
                                      docbook "/xml/xsl/"
                                      (strip-store-file-name docbook)))))))
                  (replace 'build
                           (lambda _
                             ;; XXX: There's also a 'doc-info' target, but it
                             ;; relies on docbook2X, which itself relies on
                             ;; DocBook 4.1.2, which is not really usable
                             ;; (lacks a catalog.xml.)
                             (invoke "make"
                                     "doc-html"
                                     "doc-man")))
                  (replace 'install
                           (lambda* (#:key outputs #:allow-other-keys)
                             (let ((out (assoc-ref outputs "out")))
                               (invoke "make"
                                       "doc-install-html"
                                       "doc-install-man")))))))
    (properties (alist-delete 'hidden? (package-properties gcc)))))

(define-public libstdc++-doc-5
  (make-libstdc++-doc gcc-5))

(define-public libstdc++-doc-9
  (make-libstdc++-doc gcc-9))

(define-public isl
  (package
    (name "isl")
    (version "0.24")
    (source (origin
             (method url-fetch)
             (uri (list (string-append "mirror://sourceforge/libisl/isl-"
                                       version ".tar.xz")
                        (string-append %gcc-infrastructure
                                       "isl-" version ".tar.xz")))
             (sha256
              (base32
               "1bgbk6n93qqn7w8v21kxf4x6dc3z0ypqrzvgfd46nhagak60ac84"))))
    (build-system gnu-build-system)
    (outputs '("out" "static"))
    (arguments
     '(#:phases (modify-phases %standard-phases
                  (add-after 'install 'move-static-library
                    (lambda* (#:key outputs #:allow-other-keys)
                      (let* ((out (assoc-ref outputs "out"))
                             (static (assoc-ref outputs "static"))
                             (source (string-append out "/lib/libisl.a"))
                             (target (string-append static "/lib/libisl.a")))
                        (mkdir-p (dirname target))
                        (link source target)
                        (delete-file source)

                        ;; Remove reference to libisl.a from the .la file so
                        ;; libtool looks for it in the usual locations.
                        (substitute* (string-append out "/lib/libisl.la")
                          (("^old_library=.*")
                           "old_library=''\n"))))))))
    (inputs (list gmp))
    (home-page "https://libisl.sourceforge.io/") ;https://repo.or.cz/w/isl.git
    (properties `((release-monitoring-url . ,home-page)))
    (synopsis
     "Manipulating sets and relations of integer points \
bounded by linear constraints")
    (description
     "isl is a library for manipulating sets and relations of integer points
bounded by linear constraints.  Supported operations on sets include
intersection, union, set difference, emptiness check, convex hull, (integer)
affine hull, integer projection, computing the lexicographic minimum using
parametric integer programming, coalescing and parametric vertex
enumeration.  It also includes an ILP solver based on generalized basis
reduction, transitive closures on maps (which may encode infinite graphs),
dependence analysis and bounds on piecewise step-polynomials.")
    (license lgpl2.1+)))

(define-public isl-0.18
  (package
    (inherit isl)
    (version "0.18")
    (source (origin
              (method url-fetch)
              (uri (list (string-append "mirror://sourceforge/libisl/isl-"
                                        version ".tar.bz2")
                         (string-append %gcc-infrastructure
                                        "isl-" version ".tar.bz2")))
              (sha256
               (base32
                "06ybml6llhi4i56q90jnimbcgk1lpcdwhy9nxdxra2hxz3bhz2vb"))))))

(define-public isl-0.11
  (package
    (inherit isl)
    (name "isl")
    (version "0.11.1")
    (source (origin
             (method url-fetch)
             (uri (list (string-append "mirror://sourceforge/libisl/isl-"
                                       version ".tar.bz2")
                        (string-append %gcc-infrastructure
                                       "isl-" version ".tar.bz2")))
             (sha256
              (base32
               "13d9cqa5rzhbjq0xf0b2dyxag7pqa72xj9dhsa03m8ccr1a4npq9"))
             (patches (search-patches "isl-0.11.1-aarch64-support.patch"))))))

(define-public cloog
  (package
    (name "cloog")
    (version "0.18.0")
    (source
     (origin
      (method url-fetch)
      (uri (list (string-append
                  "http://www.bastoul.net/cloog/pages/download/count.php3?url=cloog-"
                  version
                  ".tar.gz")
                 (string-append %gcc-infrastructure
                                name "-" version ".tar.gz")))
      (sha256
       (base32
        "0a12rwfwp22zd0nlld0xyql11cj390rrq1prw35yjsw8wzfshjhw"))
      (file-name (string-append name "-" version ".tar.gz"))))
    (build-system gnu-build-system)
    (inputs (list gmp isl-0.11))
    (arguments '(#:configure-flags '("--with-isl=system")))
    (home-page "http://www.cloog.org/")
    (synopsis "Library to generate code for scanning Z-polyhedra")
    (description
     "CLooG is a free software library to generate code for scanning
Z-polyhedra.  That is, it finds a code (e.g., in C, FORTRAN...) that
reaches each integral point of one or more parameterized polyhedra.
CLooG has been originally written to solve the code generation problem
for optimizing compilers based on the polytope model.  Nevertheless it
is used now in various area e.g., to build control automata for
high-level synthesis or to find the best polynomial approximation of a
function.  CLooG may help in any situation where scanning polyhedra
matters.  While the user has full control on generated code quality,
CLooG is designed to avoid control overhead and to produce a very
effective code.")
    (license gpl2+)))

(define-public gnu-c-manual
  (package
    (name "gnu-c-manual")
    (version "0.2.5")
    (source (origin
              (method url-fetch)
              (uri (string-append "mirror://gnu/gnu-c-manual/gnu-c-manual-"
                                  version ".tar.gz"))
              (sha256
               (base32
                "1sfsj9256w18qzylgag2h5h377aq8in8929svblfnj9svfriqcys"))))
    (build-system gnu-build-system)
    (native-inputs (list texinfo))
    (arguments
     '(#:phases (modify-phases %standard-phases
                  (delete 'configure)
                  (delete 'check)
                  (replace 'build
                           (lambda _
                             (invoke "make"
                                     "gnu-c-manual.info"
                                     "gnu-c-manual.html")))
                  (replace 'install
                           (lambda* (#:key outputs #:allow-other-keys)
                             (let* ((out (assoc-ref outputs "out"))
                                    (info (string-append out "/share/info"))
                                    (html (string-append
                                           out "/share/doc/gnu-c-manual")))
                               (mkdir-p info)
                               (mkdir-p html)

                               (for-each (lambda (file)
                                           (copy-file file
                                                      (string-append info "/"
                                                                     file)))
                                         (find-files "." "\\.info(-[0-9])?$"))
                               (for-each (lambda (file)
                                           (copy-file file
                                                      (string-append html "/"
                                                                     file)))
                                         (find-files "." "\\.html$"))))))))
    (synopsis "Reference manual for the C programming language")
    (description
     "This is a reference manual for the C programming language, as
implemented by the GNU C Compiler (gcc).  As a reference, it is not intended
to be a tutorial of the language.  Rather, it outlines all of the constructs
of the language.  Library functions are not included.")
    (home-page "https://www.gnu.org/software/gnu-c-manual/")
    (license fdl1.3+)))