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