gnu: signify: Update to 26.
[guix.git] / gnu / packages / commencement.scm
blob4a41e2abf32458cada5236b82fc0990281562d01
1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2012, 2013, 2014, 2015, 2016, 2017, 2018 Ludovic Courtès <ludo@gnu.org>
3 ;;; Copyright © 2014 Andreas Enge <andreas@enge.fr>
4 ;;; Copyright © 2012 Nikita Karetnikov <nikita@karetnikov.org>
5 ;;; Copyright © 2014, 2015, 2017 Mark H Weaver <mhw@netris.org>
6 ;;; Copyright © 2017, 2018 Efraim Flashner <efraim@flashner.co.il>
7 ;;; Copyright © 2018 Tobias Geerinckx-Rice <me@tobias.gr>
8 ;;;
9 ;;; This file is part of GNU Guix.
10 ;;;
11 ;;; GNU Guix is free software; you can redistribute it and/or modify it
12 ;;; under the terms of the GNU General Public License as published by
13 ;;; the Free Software Foundation; either version 3 of the License, or (at
14 ;;; your option) any later version.
15 ;;;
16 ;;; GNU Guix is distributed in the hope that it will be useful, but
17 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 ;;; GNU General Public License for more details.
20 ;;;
21 ;;; You should have received a copy of the GNU General Public License
22 ;;; along with GNU Guix.  If not, see <http://www.gnu.org/licenses/>.
24 (define-module (gnu packages commencement)
25   #:use-module ((guix licenses)
26                 #:select (gpl3+ lgpl2.0+ public-domain))
27   #:use-module (gnu packages)
28   #:use-module (gnu packages bootstrap)
29   #:use-module (gnu packages base)
30   #:use-module (gnu packages bash)
31   #:use-module (gnu packages gcc)
32   #:use-module (gnu packages m4)
33   #:use-module (gnu packages code)
34   #:use-module (gnu packages file)
35   #:use-module (gnu packages gawk)
36   #:use-module (gnu packages bison)
37   #:use-module (gnu packages flex)
38   #:use-module (gnu packages guile)
39   #:use-module (gnu packages gettext)
40   #:use-module (gnu packages multiprecision)
41   #:use-module (gnu packages compression)
42   #:use-module (gnu packages perl)
43   #:use-module (gnu packages linux)
44   #:use-module (gnu packages hurd)
45   #:use-module (gnu packages texinfo)
46   #:use-module (gnu packages pkg-config)
47   #:use-module (guix packages)
48   #:use-module (guix download)
49   #:use-module (guix build-system gnu)
50   #:use-module (guix build-system trivial)
51   #:use-module (guix memoization)
52   #:use-module (guix utils)
53   #:use-module (srfi srfi-1)
54   #:use-module (srfi srfi-26)
55   #:use-module (ice-9 vlist)
56   #:use-module (ice-9 match)
57   #:use-module (ice-9 regex)
58   #:export (make-gcc-toolchain))
60 ;;; Commentary:
61 ;;;
62 ;;; This is the commencement, this is where things start.  Before the
63 ;;; commencement, of course, there's the 'bootstrap' module, which provides us
64 ;;; with the initial binaries.  This module uses those bootstrap binaries to
65 ;;; actually build up the whole tool chain that make up the implicit inputs of
66 ;;; 'gnu-build-system'.
67 ;;;
68 ;;; To avoid circular dependencies, this module should not be imported
69 ;;; directly from anywhere.
70 ;;;
71 ;;; Below, we frequently use "inherit" to create modified packages.  The
72 ;;; reason why we use "inherit" instead of "package/inherit" is because we do
73 ;;; not want these commencement packages to inherit grafts.  By definition,
74 ;;; these packages are not depended on at run time by any of the packages we
75 ;;; use.  Thus it does not make sense to inherit grafts.  Furthermore, those
76 ;;; grafts would often lead to extra overhead for users who would end up
77 ;;; downloading those "-boot0" packages just to build package replacements
78 ;;; that are in fact not going to be used.
79 ;;;
80 ;;; Code:
82 (define gnu-make-boot0
83   (package-with-bootstrap-guile
84    (package (inherit gnu-make)
85      (name "make-boot0")
86      (arguments
87       `(#:guile ,%bootstrap-guile
88         #:implicit-inputs? #f
89         #:tests? #f                  ; cannot run "make check"
90         ,@(substitute-keyword-arguments (package-arguments gnu-make)
91             ((#:phases phases)
92              `(modify-phases ,phases
93                 (replace 'build
94                   (lambda _
95                     (invoke "./build.sh")
96                     #t))
97                 (replace 'install
98                   (lambda* (#:key outputs #:allow-other-keys)
99                     (let* ((out (assoc-ref outputs "out"))
100                            (bin (string-append out "/bin")))
101                       (install-file "make" bin)
102                       #t))))))))
103      (native-inputs '())                          ; no need for 'pkg-config'
104      (inputs %bootstrap-inputs))))
106 (define diffutils-boot0
107   (package-with-bootstrap-guile
108    (let ((p (package-with-explicit-inputs diffutils
109                                           `(("make" ,gnu-make-boot0)
110                                             ,@%bootstrap-inputs)
111                                           #:guile %bootstrap-guile)))
112      (package (inherit p)
113        (name "diffutils-boot0")
114        (arguments `(#:tests? #f         ; the test suite needs diffutils
115                     ,@(package-arguments p)))))))
117 (define findutils-boot0
118   (package-with-bootstrap-guile
119    (package-with-explicit-inputs (package
120                                    (inherit findutils)
121                                    (name "findutils-boot0"))
122                                  `(("make" ,gnu-make-boot0)
123                                    ("diffutils" ,diffutils-boot0) ; for tests
124                                    ,@%bootstrap-inputs)
125                                  (current-source-location)
126                                  #:guile %bootstrap-guile)))
128 (define file-boot0
129   (package-with-bootstrap-guile
130    (package-with-explicit-inputs (package
131                                    (inherit file)
132                                    (name "file-boot0"))
133                                  `(("make" ,gnu-make-boot0)
134                                    ,@%bootstrap-inputs)
135                                  (current-source-location)
136                                  #:guile %bootstrap-guile)))
139 (define %boot0-inputs
140   `(("make" ,gnu-make-boot0)
141     ("diffutils" ,diffutils-boot0)
142     ("findutils" ,findutils-boot0)
143     ("file" ,file-boot0)
144     ,@%bootstrap-inputs))
146 (define* (boot-triplet #:optional (system (%current-system)))
147   ;; Return the triplet used to create the cross toolchain needed in the
148   ;; first bootstrapping stage.
149   (nix-system->gnu-triplet system "guix"))
151 ;; Following Linux From Scratch, build a cross-toolchain in stage 0.  That
152 ;; toolchain actually targets the same OS and arch, but it has the advantage
153 ;; of being independent of the libc and tools in %BOOTSTRAP-INPUTS, since
154 ;; GCC-BOOT0 (below) is built without any reference to the target libc.
156 (define binutils-boot0
157   (package-with-bootstrap-guile
158    (package (inherit binutils)
159      (name "binutils-cross-boot0")
160      (arguments
161       `(#:guile ,%bootstrap-guile
162         #:implicit-inputs? #f
164         #:modules ((guix build gnu-build-system)
165                    (guix build utils)
166                    (ice-9 ftw))                    ; for 'scandir'
167         #:phases (modify-phases %standard-phases
168                    (add-after 'install 'add-symlinks
169                      (lambda* (#:key outputs #:allow-other-keys)
170                        ;; The cross-gcc invokes 'as', 'ld', etc, without the
171                        ;; triplet prefix, so add symlinks.
172                        (let ((out (assoc-ref outputs "out"))
173                              (triplet-prefix (string-append ,(boot-triplet) "-")))
174                          (define (has-triplet-prefix? name)
175                            (string-prefix? triplet-prefix name))
176                          (define (remove-triplet-prefix name)
177                            (substring name (string-length triplet-prefix)))
178                          (with-directory-excursion (string-append out "/bin")
179                            (for-each (lambda (name)
180                                        (symlink name (remove-triplet-prefix name)))
181                                      (scandir "." has-triplet-prefix?)))
182                          #t))))
184         ,@(substitute-keyword-arguments (package-arguments binutils)
185             ((#:configure-flags cf)
186              `(cons ,(string-append "--target=" (boot-triplet))
187                     ,cf)))))
188      (inputs %boot0-inputs))))
190 ;; Use a "fixed" package source for this early libstdc++ variant so we can
191 ;; update GCC 4.9 without triggering a full rebuild.
192 (define gcc-for-libstdc++
193   (package
194     (inherit gcc-4.9)
195     (source (origin
196               (inherit (package-source gcc-4.9))
197               (patches (search-patches "gcc-4.9-libsanitizer-fix.patch"
198                                        "gcc-arm-bug-71399.patch"
199                                        "gcc-asan-missing-include.patch"
200                                        "gcc-libvtv-runpath.patch"
201                                        "gcc-fix-texi2pod.patch"))))))
203 (define libstdc++-boot0
204   ;; GCC's libcc1 is always built as a shared library (the top-level
205   ;; 'Makefile.def' forcefully adds --enable-shared) and thus needs to refer
206   ;; to libstdc++.so.  We cannot build libstdc++-5.3 because it relies on
207   ;; C++14 features missing in some of our bootstrap compilers.
208   (let ((lib (package-with-bootstrap-guile (make-libstdc++ gcc-for-libstdc++))))
209     (package
210       (inherit lib)
211       (name "libstdc++-boot0")
212       (arguments
213        `(#:guile ,%bootstrap-guile
214          #:implicit-inputs? #f
216          ;; XXX: libstdc++.so NEEDs ld.so for some reason.
217          #:validate-runpath? #f
219          ,@(package-arguments lib)))
220       (inputs %boot0-inputs)
221       (native-inputs '()))))
223 (define gcc-boot0
224   (package-with-bootstrap-guile
225    (package (inherit gcc)
226      (name "gcc-cross-boot0")
227      (arguments
228       `(#:guile ,%bootstrap-guile
229         #:implicit-inputs? #f
230         #:modules ((guix build gnu-build-system)
231                    (guix build utils)
232                    (ice-9 regex)
233                    (srfi srfi-1)
234                    (srfi srfi-26))
235         ,@(substitute-keyword-arguments (package-arguments gcc)
236             ((#:configure-flags flags)
237              `(append (list ,(string-append "--target=" (boot-triplet))
239                             ;; No libc yet.
240                             "--without-headers"
242                             ;; Disable features not needed at this stage.
243                             "--disable-shared"
244                             "--enable-languages=c,c++"
246                             ;; libstdc++ cannot be built at this stage
247                             ;; ("Link tests are not allowed after
248                             ;; GCC_NO_EXECUTABLES.").
249                             "--disable-libstdc++-v3"
251                             "--disable-threads"
252                             "--disable-libmudflap"
253                             "--disable-libatomic"
254                             "--disable-libsanitizer"
255                             "--disable-libitm"
256                             "--disable-libgomp"
257                             "--disable-libcilkrts"
258                             "--disable-libvtv"
259                             "--disable-libssp"
260                             "--disable-libquadmath"
261                             "--disable-decimal-float")
262                       (remove (cut string-match
263                                 "--(with-system-zlib|enable-languages.*)" <>)
264                               ,flags)))
265             ((#:phases phases)
266              `(modify-phases ,phases
267                 (add-after 'unpack 'unpack-gmp&co
268                   (lambda* (#:key inputs #:allow-other-keys)
269                     (let ((gmp  (assoc-ref %build-inputs "gmp-source"))
270                           (mpfr (assoc-ref %build-inputs "mpfr-source"))
271                           (mpc  (assoc-ref %build-inputs "mpc-source")))
273                       ;; To reduce the set of pre-built bootstrap inputs, build
274                       ;; GMP & co. from GCC.
275                       (for-each (lambda (source)
276                                   (invoke "tar" "xvf" source))
277                                 (list gmp mpfr mpc))
279                       ;; Create symlinks like `gmp' -> `gmp-x.y.z'.
280                       ,@(map (lambda (lib)
281                                ;; Drop trailing letters, as gmp-6.0.0a unpacks
282                                ;; into gmp-6.0.0.
283                                `(symlink ,(string-trim-right
284                                            (package-full-name lib "-")
285                                            char-set:letter)
286                                          ,(package-name lib)))
287                              (list gmp-6.0 mpfr mpc))
288                       #t)))
289                 (add-after 'install 'symlink-libgcc_eh
290                   (lambda* (#:key outputs #:allow-other-keys)
291                     (let ((out (assoc-ref outputs "lib")))
292                       ;; Glibc wants to link against libgcc_eh, so provide
293                       ;; it.
294                       (with-directory-excursion
295                           (string-append out "/lib/gcc/"
296                                          ,(boot-triplet)
297                                          "/" ,(package-version gcc))
298                         (symlink "libgcc.a" "libgcc_eh.a"))
299                       #t))))))))
301      (inputs `(("gmp-source" ,(package-source gmp-6.0))
302                ("mpfr-source" ,(package-source mpfr))
303                ("mpc-source" ,(package-source mpc))
304                ("binutils-cross" ,binutils-boot0)
306                ;; The libstdc++ that libcc1 links against.
307                ("libstdc++" ,libstdc++-boot0)
309                ;; Call it differently so that the builder can check whether
310                ;; the "libc" input is #f.
311                ("libc-native" ,@(assoc-ref %boot0-inputs "libc"))
312                ,@(alist-delete "libc" %boot0-inputs)))
314      ;; No need for the native-inputs to build the documentation at this stage.
315      (native-inputs `()))))
317 (define perl-boot0
318   (let ((perl (package
319                 (inherit perl)
320                 (name "perl-boot0")
321                 (arguments
322                  ;; At the very least, this must not depend on GCC & co.
323                  (let ((args `(#:disallowed-references
324                                ,(list %bootstrap-binutils))))
325                    (substitute-keyword-arguments (package-arguments perl)
326                      ((#:phases phases)
327                       `(modify-phases ,phases
328                          ;; Pthread support is missing in the bootstrap compiler
329                          ;; (broken spec file), so disable it.
330                          (add-before 'configure 'disable-pthreads
331                            (lambda _
332                              (substitute* "Configure"
333                                (("^libswanted=(.*)pthread" _ before)
334                                 (string-append "libswanted=" before)))
335                              #t))))
336                      ;; Do not configure with '-Dusethreads' since pthread
337                      ;; support is missing.
338                      ((#:configure-flags configure-flags)
339                       `(delete "-Dusethreads" ,configure-flags))))))))
340     (package-with-bootstrap-guile
341      (package-with-explicit-inputs perl
342                                    %boot0-inputs
343                                    (current-source-location)
344                                    #:guile %bootstrap-guile))))
346 (define bison-boot0
347   ;; This Bison is needed to build MiG so we need it early in the process.
348   ;; It is also needed to rebuild Bash's parser, which is modified by
349   ;; its CVE patches.  Remove it when it's no longer needed.
350   (let* ((m4    (package-with-bootstrap-guile
351                  (package-with-explicit-inputs m4 %boot0-inputs
352                                                (current-source-location)
353                                                #:guile %bootstrap-guile)))
354          (bison (package (inherit bison)
355                   (propagated-inputs `(("m4" ,m4)))
356                   (inputs '())                    ;remove Flex...
357                   (arguments
358                    '(#:tests? #f                  ;... and thus disable tests
360                      ;; Zero timestamps in liby.a; this must be done
361                      ;; explicitly here because the bootstrap Binutils don't
362                      ;; do that (default is "cru".)
363                      #:make-flags '("ARFLAGS=crD" "RANLIB=ranlib -D"
364                                     "V=1"))))))
365     (package
366       (inherit (package-with-bootstrap-guile
367                 (package-with-explicit-inputs bison %boot0-inputs
368                                               (current-source-location)
369                                               #:guile %bootstrap-guile)))
370       (native-inputs `(("perl" ,perl-boot0))))))
372 (define flex-boot0
373   ;; This Flex is needed to build MiG.
374   (let* ((flex (package (inherit flex)
375                  (native-inputs `(("bison" ,bison-boot0)))
376                  (propagated-inputs `(("m4" ,m4)))
377                  (inputs `(("indent" ,indent)))
378                  (arguments '(#:tests? #f)))))
379     (package-with-bootstrap-guile
380      (package-with-explicit-inputs flex %boot0-inputs
381                                    (current-source-location)
382                                    #:guile %bootstrap-guile))))
384 (define linux-libre-headers-boot0
385   (mlambda ()
386     "Return Linux-Libre header files for the bootstrap environment."
387     ;; Note: this is wrapped in a thunk to nicely handle circular dependencies
388     ;; between (gnu packages linux) and this module.  Additionally, memoize
389     ;; the result to play well with further memoization and code that relies
390     ;; on pointer identity; see <https://bugs.gnu.org/30155>.
391     (package-with-bootstrap-guile
392      (package (inherit linux-libre-headers)
393               (arguments `(#:guile ,%bootstrap-guile
394                            #:implicit-inputs? #f
395                            ,@(package-arguments linux-libre-headers)))
396               (native-inputs
397                `(("perl" ,perl-boot0)
398                  ,@%boot0-inputs))))))
400 (define gnumach-headers-boot0
401   (package-with-bootstrap-guile
402    (package-with-explicit-inputs gnumach-headers
403                                  %boot0-inputs
404                                  (current-source-location)
405                                  #:guile %bootstrap-guile)))
407 (define mig-boot0
408   (let* ((mig (package (inherit mig)
409                  (native-inputs `(("bison" ,bison-boot0)
410                                   ("flex" ,flex-boot0)))
411                  (inputs `(("flex" ,flex-boot0)))
412                  (arguments
413                   `(#:configure-flags
414                     `(,(string-append "LDFLAGS=-Wl,-rpath="
415                                       (assoc-ref %build-inputs "flex") "/lib/")))))))
416     (package-with-bootstrap-guile
417      (package-with-explicit-inputs mig %boot0-inputs
418                                    (current-source-location)
419                                    #:guile %bootstrap-guile))))
421 (define hurd-headers-boot0
422   (let ((hurd-headers (package (inherit hurd-headers)
423                         (native-inputs `(("mig" ,mig-boot0)))
424                         (inputs '()))))
425     (package-with-bootstrap-guile
426      (package-with-explicit-inputs hurd-headers %boot0-inputs
427                                    (current-source-location)
428                                    #:guile %bootstrap-guile))))
430 (define hurd-minimal-boot0
431   (let ((hurd-minimal (package (inherit hurd-minimal)
432                         (native-inputs `(("mig" ,mig-boot0)))
433                         (inputs '()))))
434     (package-with-bootstrap-guile
435      (package-with-explicit-inputs hurd-minimal %boot0-inputs
436                                    (current-source-location)
437                                    #:guile %bootstrap-guile))))
439 (define hurd-core-headers-boot0
440   (mlambda ()
441     "Return the Hurd and Mach headers as well as initial Hurd libraries for
442 the bootstrap environment."
443     (package-with-bootstrap-guile
444      (package (inherit hurd-core-headers)
445               (arguments `(#:guile ,%bootstrap-guile
446                            ,@(package-arguments hurd-core-headers)))
447               (inputs
448                `(("gnumach-headers" ,gnumach-headers-boot0)
449                  ("hurd-headers" ,hurd-headers-boot0)
450                  ("hurd-minimal" ,hurd-minimal-boot0)
451                  ,@%boot0-inputs))))))
453 (define* (kernel-headers-boot0 #:optional (system (%current-system)))
454   (match system
455     ("i586-gnu" (hurd-core-headers-boot0))
456     (_ (linux-libre-headers-boot0))))
458 (define texinfo-boot0
459   ;; Texinfo used to build libc's manual.
460   ;; We build without ncurses because it fails to build at this stage, and
461   ;; because we don't need the stand-alone Info reader.
462   ;; Also, use %BOOT0-INPUTS to avoid building Perl once more.
463   (let ((texinfo (package (inherit texinfo)
464                    (native-inputs '())
465                    (inputs `(("perl" ,perl-boot0)))
467                    ;; Some of Texinfo 6.1's tests would fail with "Couldn't
468                    ;; set UTF-8 character type in locale" but we don't have a
469                    ;; UTF-8 locale at this stage, so skip them.
470                    (arguments '(#:tests? #f)))))
471     (package-with-bootstrap-guile
472      (package-with-explicit-inputs texinfo %boot0-inputs
473                                    (current-source-location)
474                                    #:guile %bootstrap-guile))))
476 (define ld-wrapper-boot0
477   ;; We need this so binaries on Hurd will have libmachuser and libhurduser
478   ;; in their RUNPATH, otherwise validate-runpath will fail.
479   (make-ld-wrapper "ld-wrapper-boot0"
480                    #:target boot-triplet
481                    #:binutils binutils-boot0
482                    #:guile %bootstrap-guile
483                    #:bash (car (assoc-ref %boot0-inputs "bash"))))
485 (define %boot1-inputs
486   ;; 2nd stage inputs.
487   `(("gcc" ,gcc-boot0)
488     ("ld-wrapper-cross" ,ld-wrapper-boot0)
489     ("binutils-cross" ,binutils-boot0)
490     ,@(alist-delete "binutils" %boot0-inputs)))
492 (define glibc-final-with-bootstrap-bash
493   ;; The final libc, "cross-built".  If everything went well, the resulting
494   ;; store path has no dependencies.  Actually, the really-final libc is
495   ;; built just below; the only difference is that this one uses the
496   ;; bootstrap Bash.
497   (package-with-bootstrap-guile
498    (package (inherit glibc)
499      (name "glibc-intermediate")
500      (arguments
501       `(#:guile ,%bootstrap-guile
502         #:implicit-inputs? #f
504         ,@(substitute-keyword-arguments (package-arguments glibc)
505             ((#:configure-flags flags)
506              `(append (list ,(string-append "--host=" (boot-triplet))
507                             ,(string-append "--build="
508                                             (nix-system->gnu-triplet))
510                             ;; Build Sun/ONC RPC support.  In particular,
511                             ;; install rpc/*.h.
512                             "--enable-obsolete-rpc")
513                       ,flags))
514             ((#:phases phases)
515              `(modify-phases ,phases
516                 (add-before 'configure 'pre-configure
517                   (lambda* (#:key inputs #:allow-other-keys)
518                     ;; Don't clobber CPATH with the bootstrap libc.
519                     (setenv "NATIVE_CPATH" (getenv "CPATH"))
520                     (unsetenv "CPATH")
522                     ;; Tell 'libpthread' where to find 'libihash' on Hurd systems.
523                     ,@(if (hurd-triplet? (%current-system))
524                           `((substitute* "libpthread/Makefile"
525                               (("LDLIBS-pthread.so =.*")
526                                (string-append "LDLIBS-pthread.so = "
527                                               (assoc-ref %build-inputs "kernel-headers")
528                                               "/lib/libihash.a\n"))))
529                           '())
531                     ;; 'rpcgen' needs native libc headers to be built.
532                     (substitute* "sunrpc/Makefile"
533                       (("sunrpc-CPPFLAGS =.*" all)
534                        (string-append "CPATH = $(NATIVE_CPATH)\n"
535                                       "export CPATH\n"
536                                       all "\n")))
537                     #t)))))))
538      (propagated-inputs `(("kernel-headers" ,(kernel-headers-boot0))))
539      (native-inputs
540       `(("bison" ,bison-boot0)
541         ("texinfo" ,texinfo-boot0)
542         ("perl" ,perl-boot0)))
543      (inputs
544       `(;; The boot inputs.  That includes the bootstrap libc.  We don't want
545         ;; it in $CPATH, hence the 'pre-configure' phase above.
546         ,@%boot1-inputs
548         ;; A native MiG is needed to build Glibc on Hurd.
549         ,@(if (hurd-triplet? (%current-system))
550               `(("mig" ,mig-boot0))
551               '())
553         ;; A native GCC is needed to build `cross-rpcgen'.
554         ("native-gcc" ,@(assoc-ref %boot0-inputs "gcc"))
556         ;; Here, we use the bootstrap Bash, which is not satisfactory
557         ;; because we don't want to depend on bootstrap tools.
558         ("static-bash" ,@(assoc-ref %boot0-inputs "bash")))))))
560 (define (cross-gcc-wrapper gcc binutils glibc bash)
561   "Return a wrapper for the pseudo-cross toolchain GCC/BINUTILS/GLIBC
562 that makes it available under the native tool names."
563   (package (inherit gcc)
564     (name (string-append (package-name gcc) "-wrapped"))
565     (source #f)
566     (build-system trivial-build-system)
567     (outputs '("out"))
568     (arguments
569      `(#:guile ,%bootstrap-guile
570        #:modules ((guix build utils))
571        #:builder (begin
572                    (use-modules (guix build utils))
574                    (let* ((binutils (assoc-ref %build-inputs "binutils"))
575                           (gcc      (assoc-ref %build-inputs "gcc"))
576                           (libc     (assoc-ref %build-inputs "libc"))
577                           (bash     (assoc-ref %build-inputs "bash"))
578                           (out      (assoc-ref %outputs "out"))
579                           (bindir   (string-append out "/bin"))
580                           (triplet  ,(boot-triplet)))
581                      (define (wrap-program program)
582                        ;; GCC-BOOT0 is a libc-less cross-compiler, so it
583                        ;; needs to be told where to find the crt files and
584                        ;; the dynamic linker.
585                        (call-with-output-file program
586                          (lambda (p)
587                            (format p "#!~a/bin/bash
588 exec ~a/bin/~a-~a -B~a/lib -Wl,-dynamic-linker -Wl,~a/~a \"$@\"~%"
589                                    bash
590                                    gcc triplet program
591                                    libc libc
592                                    ,(glibc-dynamic-linker))))
594                        (chmod program #o555))
596                      (mkdir-p bindir)
597                      (with-directory-excursion bindir
598                        (for-each (lambda (tool)
599                                    (symlink (string-append binutils "/bin/"
600                                                            triplet "-" tool)
601                                             tool))
602                                  '("ar" "ranlib"))
603                        (for-each wrap-program '("gcc" "g++")))
605                      #t))))
606     (native-inputs
607      `(("binutils" ,binutils)
608        ("gcc" ,gcc)
609        ("libc" ,glibc)
610        ("bash" ,bash)))
611     (inputs '())))
613 (define static-bash-for-glibc
614   ;; A statically-linked Bash to be used by GLIBC-FINAL in system(3) & co.
615   (let* ((gcc  (cross-gcc-wrapper gcc-boot0 binutils-boot0
616                                   glibc-final-with-bootstrap-bash
617                                   (car (assoc-ref %boot1-inputs "bash"))))
618          (bash (package
619                  (inherit static-bash)
620                  (arguments
621                   (substitute-keyword-arguments
622                       (package-arguments static-bash)
623                     ((#:guile _ #f)
624                      '%bootstrap-guile)
625                     ((#:configure-flags flags '())
626                      ;; Add a '-L' flag so that the pseudo-cross-ld of
627                      ;; BINUTILS-BOOT0 can find libc.a.
628                      `(append ,flags
629                               (list (string-append "LDFLAGS=-static -L"
630                                                    (assoc-ref %build-inputs
631                                                               "libc:static")
632                                                    "/lib"))))))))
633          (inputs `(("gcc" ,gcc)
634                    ("libc" ,glibc-final-with-bootstrap-bash)
635                    ("libc:static" ,glibc-final-with-bootstrap-bash "static")
636                    ,@(fold alist-delete %boot1-inputs
637                            '("gcc" "libc")))))
638     (package-with-bootstrap-guile
639      (package-with-explicit-inputs bash inputs
640                                    (current-source-location)
641                                    #:guile %bootstrap-guile))))
643 (define gettext-boot0
644   ;; A minimal gettext used during bootstrap.
645   (let ((gettext-minimal
646          (package (inherit gettext-minimal)
647            (name "gettext-boot0")
648            (inputs '())                           ;zero dependencies
649            (arguments
650             (substitute-keyword-arguments
651                 `(#:tests? #f
652                   ,@(package-arguments gettext-minimal))
653               ((#:phases phases)
654                `(modify-phases ,phases
655                   ;; Build only the tools.
656                   (add-after 'unpack 'chdir
657                              (lambda _
658                                (chdir "gettext-tools")
659                                #t))
661                   ;; Some test programs require pthreads, which we don't have.
662                   (add-before 'configure 'no-test-programs
663                               (lambda _
664                                 (substitute* "tests/Makefile.in"
665                                   (("^PROGRAMS =.*$")
666                                    "PROGRAMS =\n"))
667                                 #t))
669                   ;; Don't try to link against libexpat.
670                   (delete 'link-expat)
671                   (delete 'patch-tests))))))))
672     (package-with-bootstrap-guile
673      (package-with-explicit-inputs gettext-minimal
674                                    %boot1-inputs
675                                    (current-source-location)
676                                    #:guile %bootstrap-guile))))
678 (define glibc-final
679   ;; The final glibc, which embeds the statically-linked Bash built above.
680   ;; Use 'package/inherit' so we get the 'replacement' of 'glibc', if any.
681   (let ((glibc (package-with-bootstrap-guile glibc)))
682     (package/inherit glibc
683       (name "glibc")
684       (inputs `(("static-bash" ,static-bash-for-glibc)
685                 ,@(alist-delete
686                    "static-bash"
687                    (package-inputs glibc-final-with-bootstrap-bash))))
689       ;; This time we need 'msgfmt' to install all the libc.mo files.
690       (native-inputs `(,@(package-native-inputs glibc-final-with-bootstrap-bash)
691                        ("gettext" ,gettext-boot0)))
693       (propagated-inputs
694        (package-propagated-inputs glibc-final-with-bootstrap-bash))
696       ;; The final libc only refers to itself, but the 'debug' output contains
697       ;; references to GCC-BOOT0 and to the Linux headers.  XXX: Would be great
698       ;; if 'allowed-references' were per-output.
699       (arguments
700        `(#:allowed-references
701          ,(cons* `(,gcc-boot0 "lib") (kernel-headers-boot0)
702                  static-bash-for-glibc
703                  (package-outputs glibc-final-with-bootstrap-bash))
705          ,@(package-arguments glibc-final-with-bootstrap-bash))))))
707 (define gcc-boot0-wrapped
708   ;; Make the cross-tools GCC-BOOT0 and BINUTILS-BOOT0 available under the
709   ;; non-cross names.
710   (cross-gcc-wrapper gcc-boot0 binutils-boot0 glibc-final
711                      (car (assoc-ref %boot1-inputs "bash"))))
713 (define %boot2-inputs
714   ;; 3rd stage inputs.
715   `(("libc" ,glibc-final)
716     ("libc:static" ,glibc-final "static")
717     ("gcc" ,gcc-boot0-wrapped)
718     ,@(fold alist-delete %boot1-inputs '("libc" "gcc"))))
720 (define binutils-final
721   (package-with-bootstrap-guile
722    (package (inherit binutils)
723      (arguments
724       `(#:guile ,%bootstrap-guile
725         #:implicit-inputs? #f
726         #:allowed-references ("out" ,glibc-final)
727         ,@(package-arguments binutils)))
728      (inputs %boot2-inputs))))
730 (define libstdc++
731   ;; Intermediate libstdc++ that will allow us to build the final GCC
732   ;; (remember that GCC-BOOT0 cannot build libstdc++.)
733   (let ((lib (package-with-bootstrap-guile (make-libstdc++ gcc))))
734     (package
735       (inherit lib)
736       (arguments
737        `(#:guile ,%bootstrap-guile
738          #:implicit-inputs? #f
739          #:allowed-references ("out")
741          ;; XXX: libstdc++.so NEEDs ld.so for some reason.
742          #:validate-runpath? #f
744          ;; All of the package arguments from 'make-libstdc++
745          ;; except for the configure-flags.
746          ,@(package-arguments lib)
747          #:configure-flags `("--disable-shared"
748                              "--disable-libstdcxx-threads"
749                              "--disable-libstdcxx-pch"
750                              ,(string-append "--with-gxx-include-dir="
751                                              (assoc-ref %outputs "out")
752                                              "/include"))))
753       (outputs '("out"))
754       (inputs %boot2-inputs)
755       (synopsis "GNU C++ standard library (intermediate)"))))
757 (define zlib-final
758   ;; Zlib used by GCC-FINAL.
759   (package-with-bootstrap-guile
760    (package
761      (inherit zlib)
762      (arguments
763       `(#:guile ,%bootstrap-guile
764         #:implicit-inputs? #f
765         #:allowed-references ("out" ,glibc-final)
766         ,@(package-arguments zlib)))
767      (inputs %boot2-inputs))))
769 (define ld-wrapper-boot3
770   ;; A linker wrapper that uses the bootstrap Guile.
771   (make-ld-wrapper "ld-wrapper-boot3"
772                    #:binutils binutils-final
773                    #:guile %bootstrap-guile
774                    #:bash (car (assoc-ref %boot2-inputs "bash"))))
776 (define gcc-final
777   ;; The final GCC.
778   (package (inherit gcc-boot0)
779     (name "gcc")
781     ;; XXX: Currently #:allowed-references applies to all the outputs but the
782     ;; "debug" output contains disallowed references, notably
783     ;; linux-libre-headers.  Disable the debugging output to work around that.
784     (outputs (delete "debug" (package-outputs gcc-boot0)))
786     (arguments
787      `(#:guile ,%bootstrap-guile
788        #:implicit-inputs? #f
790        #:allowed-references ("out" "lib" ,zlib-final
791                              ,glibc-final ,static-bash-for-glibc)
793        ;; Things like libasan.so and libstdc++.so NEED ld.so for some
794        ;; reason, but it is not in their RUNPATH.  This is a false
795        ;; positive, so turn it off.
796        #:validate-runpath? #f
798        ;; Build again GMP & co. within GCC's build process, because it's hard
799        ;; to do outside (because GCC-BOOT0 is a cross-compiler, and thus
800        ;; doesn't honor $LIBRARY_PATH, which breaks `gnu-build-system'.)
801        ,@(substitute-keyword-arguments (package-arguments gcc-boot0)
802            ((#:configure-flags boot-flags)
803             (let loop ((args (package-arguments gcc)))
804               (match args
805                 ((#:configure-flags normal-flags _ ...)
806                  normal-flags)
807                 ((_ rest ...)
808                  (loop rest)))))
809            ((#:make-flags flags)
810             ;; Since $LIBRARY_PATH is not honored, add the relevant flags.
811             `(let ((zlib (assoc-ref %build-inputs "zlib")))
812                (map (lambda (flag)
813                       (if (string-prefix? "LDFLAGS=" flag)
814                           (string-append flag " -L"
815                                          (assoc-ref %build-inputs "libstdc++")
816                                          "/lib -L" zlib "/lib -Wl,-rpath="
817                                          zlib "/lib")
818                           flag))
819                     ,flags)))
820            ((#:phases phases)
821             `(alist-delete 'symlink-libgcc_eh ,phases)))))
823     ;; This time we want Texinfo, so we get the manual.  Add
824     ;; STATIC-BASH-FOR-GLIBC so that it's used in the final shebangs of
825     ;; scripts such as 'mkheaders' and 'fixinc.sh' (XXX: who cares about these
826     ;; scripts?).
827     (native-inputs `(("texinfo" ,texinfo-boot0)
828                      ("perl" ,perl-boot0) ;for manpages
829                      ("static-bash" ,static-bash-for-glibc)
830                      ,@(package-native-inputs gcc-boot0)))
832     (inputs `(("gmp-source" ,(bootstrap-origin (package-source gmp-6.0)))
833               ("mpfr-source" ,(package-source mpfr))
834               ("mpc-source" ,(package-source mpc))
835               ("ld-wrapper" ,ld-wrapper-boot3)
836               ("binutils" ,binutils-final)
837               ("libstdc++" ,libstdc++)
838               ("zlib" ,zlib-final)
839               ,@%boot2-inputs))))
841 (define %boot3-inputs
842   ;; 4th stage inputs.
843   `(("gcc" ,gcc-final)
844     ("ld-wrapper" ,ld-wrapper-boot3)
845     ,@(alist-delete "gcc" %boot2-inputs)))
847 (define bash-final
848   ;; Link with `-static-libgcc' to make sure we don't retain a reference
849   ;; to the bootstrap GCC.  Use "bash-minimal" to avoid an extra dependency
850   ;; on Readline and ncurses.
851   (let ((bash (package
852                 (inherit bash-minimal)
853                 (arguments
854                  `(#:disallowed-references
855                    ,(assoc-ref %boot3-inputs "coreutils&co")
856                    ,@(package-arguments bash-minimal))))))
857     (package-with-bootstrap-guile
858      (package-with-explicit-inputs (static-libgcc-package bash)
859                                    %boot3-inputs
860                                    (current-source-location)
861                                    #:guile %bootstrap-guile))))
863 (define %boot4-inputs
864   ;; Now use the final Bash.
865   `(("bash" ,bash-final)
866     ,@(alist-delete "bash" %boot3-inputs)))
868 (define-public guile-final
869   ;; This package must be public because other modules refer to it.  However,
870   ;; mark it as hidden so that 'fold-packages' ignores it.
871   (package-with-bootstrap-guile
872    (package-with-explicit-inputs (hidden-package guile-2.2/fixed)
873                                  %boot4-inputs
874                                  (current-source-location)
875                                  #:guile %bootstrap-guile)))
877 (define glibc-utf8-locales-final
878   ;; Now that we have GUILE-FINAL, build the UTF-8 locales.  They are needed
879   ;; by the build processes afterwards so their 'scm_to_locale_string' works
880   ;; with the full range of Unicode codepoints (remember
881   ;; 'scm_to_locale_string' is called every time a string is passed to a C
882   ;; function.)
883   (package
884     (inherit glibc-utf8-locales)
885     (inputs `(("glibc" ,glibc-final)
886               ("gzip"
887                ,(package-with-explicit-inputs gzip %boot4-inputs
888                                               (current-source-location)
889                                               #:guile %bootstrap-guile))))))
891 (define-public ld-wrapper
892   ;; The final 'ld' wrapper, which uses the final Guile and Binutils.
893   (make-ld-wrapper "ld-wrapper"
894                    #:binutils binutils-final
895                    #:guile guile-final
896                    #:bash bash-final))
898 (define %boot5-inputs
899   ;; Now with UTF-8 locales.  Remember that the bootstrap binaries were built
900   ;; with an older libc, which cannot load the new locale format.  See
901   ;; <https://lists.gnu.org/archive/html/guix-devel/2015-08/msg00737.html>.
902   `(("locales" ,glibc-utf8-locales-final)
903     ,@%boot4-inputs))
905 (define gnu-make-final
906   ;; The final GNU Make, which uses the final Guile.
907   (package-with-bootstrap-guile
908    (package-with-explicit-inputs gnu-make
909                                  `(("guile" ,guile-final)
910                                    ,@%boot5-inputs)
911                                  (current-source-location))))
913 (define coreutils-final
914   ;; The final Coreutils.  Treat them specially because some packages, such as
915   ;; Findutils, keep a reference to the Coreutils they were built with.
916   (package-with-bootstrap-guile
917    (package-with-explicit-inputs coreutils
918                                  %boot5-inputs
919                                  (current-source-location)
921                                  ;; Use the final Guile, linked against the
922                                  ;; final libc with working iconv, so that
923                                  ;; 'substitute*' works well when touching
924                                  ;; test files in Gettext.
925                                  #:guile guile-final)))
927 (define grep-final
928   ;; The final grep.  Gzip holds a reference to it (via zgrep), so it must be
929   ;; built before gzip.
930   (let ((grep (package-with-bootstrap-guile
931                (package-with-explicit-inputs grep %boot5-inputs
932                                              (current-source-location)
933                                              #:guile guile-final))))
934     (package/inherit grep
935                      (inputs (alist-delete "pcre" (package-inputs grep)))
936                      (native-inputs `(("perl" ,perl-boot0))))))
938 (define %boot6-inputs
939   ;; Now use the final Coreutils.
940   `(("coreutils" ,coreutils-final)
941     ("grep" ,grep-final)
942     ,@%boot5-inputs))
944 (define sed-final
945   ;; The final sed.
946   (let ((sed (package-with-bootstrap-guile
947               (package-with-explicit-inputs sed %boot6-inputs
948                                             (current-source-location)
949                                             #:guile guile-final))))
950     (package/inherit sed (native-inputs `(("perl" ,perl-boot0))))))
952 (define-public %final-inputs
953   ;; Final derivations used as implicit inputs by 'gnu-build-system'.  We
954   ;; still use 'package-with-bootstrap-guile' so that the bootstrap tools are
955   ;; used for origins that have patches, thereby avoiding circular
956   ;; dependencies.
957   (let ((finalize (compose package-with-bootstrap-guile
958                            (cut package-with-explicit-inputs <> %boot6-inputs
959                                 (current-source-location)))))
960     `(,@(map (match-lambda
961               ((name package)
962                (list name (finalize package))))
963              `(("tar" ,tar)
964                ("gzip" ,gzip)
965                ("bzip2" ,bzip2)
966                ("xz" ,xz)
967                ("file" ,file)
968                ("diffutils" ,diffutils)
969                ("patch" ,patch)
970                ("findutils" ,findutils)
971                ("gawk" ,gawk)))
972       ("sed" ,sed-final)
973       ("grep" ,grep-final)
974       ("coreutils" ,coreutils-final)
975       ("make" ,gnu-make-final)
976       ("bash" ,bash-final)
977       ("ld-wrapper" ,ld-wrapper)
978       ("binutils" ,binutils-final)
979       ("gcc" ,gcc-final)
980       ("libc" ,glibc-final)
981       ("libc:static" ,glibc-final "static")
982       ("locales" ,glibc-utf8-locales-final))))
984 (define-public canonical-package
985   (let ((name->package (fold (lambda (input result)
986                                (match input
987                                  ((_ package . outputs)
988                                   (vhash-cons (package-full-name package)
989                                               package result))))
990                              vlist-null
991                              `(("guile" ,guile-final)
992                                ,@%final-inputs))))
993     (lambda (package)
994       "Return the 'canonical' variant of PACKAGE---i.e., if PACKAGE is one of
995 the implicit inputs of 'gnu-build-system', return that one, otherwise return
996 PACKAGE.
998 The goal is to avoid duplication in cases like GUILE-FINAL vs. GUILE-2.2,
999 COREUTILS-FINAL vs. COREUTILS, etc."
1000       ;; XXX: This doesn't handle dependencies of the final inputs, such as
1001       ;; libunistring, GMP, etc.
1002       (match (vhash-assoc (package-full-name package) name->package)
1003         ((_ . canon)
1004          ;; In general we want CANON, except if we're cross-compiling: CANON
1005          ;; uses explicit inputs, so it is "anchored" in the bootstrapped
1006          ;; process, with dependencies on things that cannot be
1007          ;; cross-compiled.
1008          (if (%current-target-system)
1009              package
1010              canon))
1011         (_ package)))))
1015 ;;; GCC toolchain.
1018 ;; Using the following procedure, a gcc toolchain targeting glibc-2.27 can be
1019 ;; instantiated like this:
1021 ;; (define-public gcc-glibc-2.27-toolchain
1022 ;;   (make-gcc-toolchain gcc glibc-2.27))
1024 (define* (make-gcc-toolchain gcc
1025                             #:optional
1026                             (libc #f))
1027   "Return a complete toolchain for GCC. If LIBC is specified, target that libc."
1028   (let ((gcc (if libc (make-gcc-libc gcc libc) gcc))
1029         (libc (if libc libc glibc-final)))
1030     (package
1031       (name (string-append (package-name gcc) "-toolchain"))
1032       (version (package-version gcc))
1033       (source #f)
1034       (build-system trivial-build-system)
1035       (arguments
1036        '(#:modules ((guix build union))
1037          #:builder (begin
1038                      (use-modules (ice-9 match)
1039                                   (srfi srfi-26)
1040                                   (guix build union))
1042                      (let ((out (assoc-ref %outputs "out")))
1044                        (match %build-inputs
1045                          (((names . directories) ...)
1046                           (union-build out directories)))
1048                        (union-build (assoc-ref %outputs "debug")
1049                                     (list (assoc-ref %build-inputs
1050                                                      "libc-debug")))
1051                        (union-build (assoc-ref %outputs "static")
1052                                     (list (assoc-ref %build-inputs
1053                                                      "libc-static")))
1054                        #t))))
1056       (native-search-paths (package-native-search-paths gcc))
1057       (search-paths (package-search-paths gcc))
1059       (license (package-license gcc))
1060       (synopsis "Complete GCC tool chain for C/C++ development")
1061       (description
1062        "This package provides a complete GCC tool chain for C/C++ development to
1063 be   installed in user profiles.  This includes GCC, as well as libc (headers
1064 an  d binaries, plus debugging symbols in the @code{debug} output), and Binutils.")
1065       (home-page "https://gcc.gnu.org/")
1066       (outputs '("out" "debug" "static"))
1068       ;; The main raison d'être of this "meta-package" is (1) to conveniently
1069       ;; install everything that we need, and (2) to make sure ld-wrapper comes
1070       ;; before Binutils' ld in the user's profile.
1071       (inputs `(("gcc" ,gcc)
1072                 ("ld-wrapper" ,(car (assoc-ref %final-inputs "ld-wrapper")))
1073                 ("binutils" ,binutils-final)
1074                 ("libc" ,libc)
1075                 ("libc-debug" ,libc "debug")
1076                 ("libc-static" ,libc "static"))))))
1078 (define-public gcc-toolchain-4.8
1079   (make-gcc-toolchain gcc-4.8))
1081 (define-public gcc-toolchain-4.9
1082   (make-gcc-toolchain gcc-4.9))
1084 (define-public gcc-toolchain
1085   (make-gcc-toolchain gcc-final))
1087 (define-public gcc-toolchain-5
1088   gcc-toolchain)
1090 (define-public gcc-toolchain-6
1091   (make-gcc-toolchain gcc-6))
1093 (define-public gcc-toolchain-7
1094   (make-gcc-toolchain gcc-7))
1096 (define-public gcc-toolchain-8
1097   (make-gcc-toolchain gcc-8))
1099 (define-public gcc-toolchain-9
1100   (make-gcc-toolchain gcc-9))
1102 ;;; commencement.scm ends here