1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2012, 2013, 2014, 2015, 2016, 2017 Ludovic Courtès <ludo@gnu.org>
3 ;;; Copyright © 2017 Efraim Flashner <efraim@flashner.co.il>
5 ;;; This file is part of GNU Guix.
7 ;;; GNU Guix is free software; you can redistribute it and/or modify it
8 ;;; under the terms of the GNU General Public License as published by
9 ;;; the Free Software Foundation; either version 3 of the License, or (at
10 ;;; your option) any later version.
12 ;;; GNU Guix is distributed in the hope that it will be useful, but
13 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
14 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 ;;; GNU General Public License for more details.
17 ;;; You should have received a copy of the GNU General Public License
18 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
20 (define-module (gnu packages make-bootstrap)
21 #:use-module (guix utils)
22 #:use-module (guix packages)
23 #:use-module (guix licenses)
24 #:use-module (guix build-system trivial)
25 #:use-module (guix build-system gnu)
26 #:use-module ((gnu packages) #:select (search-patch))
27 #:use-module (gnu packages base)
28 #:use-module (gnu packages cross-base)
29 #:use-module (gnu packages bash)
30 #:use-module (gnu packages compression)
31 #:use-module (gnu packages gawk)
32 #:use-module (gnu packages gcc)
33 #:use-module (gnu packages guile)
34 #:use-module (gnu packages bdw-gc)
35 #:use-module (gnu packages linux)
36 #:use-module (gnu packages hurd)
37 #:use-module (gnu packages multiprecision)
38 #:use-module (ice-9 match)
39 #:use-module (srfi srfi-1)
40 #:export (%bootstrap-binaries-tarball
41 %binutils-bootstrap-tarball
42 %glibc-bootstrap-tarball
43 %gcc-bootstrap-tarball
44 %guile-bootstrap-tarball
47 %guile-static-stripped))
51 ;;; This module provides tools to build tarballs of the "bootstrap binaries"
52 ;;; used in (gnu packages bootstrap). These statically-linked binaries are
53 ;;; taken for granted and used as the root of the whole bootstrap procedure.
57 (define* (glibc-for-bootstrap #:optional (base glibc))
58 "Return a libc deriving from BASE whose `system' and `popen' functions looks
59 for `sh' in $PATH, and without nscd, and with static NSS modules."
60 (package (inherit base)
61 (source (origin (inherit (package-source base))
62 (patches (cons (search-patch "glibc-bootstrap-system.patch")
63 (origin-patches (package-source base))))))
65 (substitute-keyword-arguments (package-arguments base)
66 ((#:configure-flags flags)
67 ;; Arrange so that getaddrinfo & co. do not contact the nscd,
68 ;; and can use statically-linked NSS modules.
69 `(cons* "--disable-nscd" "--disable-build-nscd"
73 ;; Remove the 'debug' output to allow bit-reproducible builds (when the
74 ;; 'debug' output is used, ELF files end up with a .gnu_debuglink, which
75 ;; includes a CRC of the corresponding debugging symbols; those symbols
76 ;; contain store file names, so the CRC changes at every rebuild.)
77 (outputs (delete "debug" (package-outputs base)))))
79 (define (package-with-relocatable-glibc p)
80 "Return a variant of P that uses the libc as defined by
81 `glibc-for-bootstrap'."
83 (define (cross-bootstrap-libc)
84 (let ((target (%current-target-system)))
86 ;; `cross-libc' already returns a cross libc, so clear
87 ;; %CURRENT-TARGET-SYSTEM.
88 (parameterize ((%current-target-system #f))
89 (cross-libc target)))))
91 ;; Standard inputs with the above libc and corresponding GCC.
94 (if (%current-target-system) ; is this package cross built?
95 `(("cross-libc" ,(cross-bootstrap-libc)))
98 (define (native-inputs)
99 (if (%current-target-system)
100 (let ((target (%current-target-system)))
101 `(("cross-gcc" ,(cross-gcc target
102 #:xbinutils (cross-binutils target)
103 #:libc (cross-bootstrap-libc)))
104 ("cross-binutils" ,(cross-binutils target))
106 `(("libc" ,(glibc-for-bootstrap))
107 ("gcc" ,(package (inherit gcc)
108 (outputs '("out")) ; all in one so libgcc_s is easily found
110 `(("libc",(glibc-for-bootstrap))
111 ,@(package-inputs gcc)))))
112 ,@(fold alist-delete (%final-inputs) '("libc" "gcc")))))
114 (package-with-explicit-inputs p inputs
115 (current-source-location)
116 #:native-inputs native-inputs))
118 (define %static-inputs
119 ;; Packages that are to be used as %BOOTSTRAP-INPUTS.
120 (let ((coreutils (package (inherit coreutils)
124 "--disable-silent-rules"
125 "--enable-no-install-program=stdbuf,libstdbuf.so"
126 "CFLAGS=-Os -g0" ; smaller, please
127 "LDFLAGS=-static -pthread")
128 #:tests? #f ; signal-related Gnulib tests fail
129 ,@(package-arguments coreutils)))
131 ;; Remove optional dependencies such as GMP. Keep Perl
132 ;; except if it's missing (which is the case when
134 (inputs (match (assoc "perl" (package-inputs coreutils))
138 ;; Remove the 'debug' output (see above for the reason.)
140 (bzip2 (package (inherit bzip2)
142 (substitute-keyword-arguments (package-arguments bzip2)
147 (substitute* "Makefile"
148 (("^LDFLAGS[[:blank:]]*=.*$")
149 "LDFLAGS = -static")))
151 (xz (package (inherit xz)
153 `(#:strip-flags '("--strip-all")
154 #:phases (alist-cons-before
155 'configure 'static-executable
157 ;; Ask Libtool for a static executable.
158 (substitute* "src/xz/Makefile.in"
160 "xz_LDADD = -all-static")))
161 %standard-phases)))))
162 (gawk (package (inherit gawk)
163 (source (origin (inherit (package-source gawk))
164 (patches (cons (search-patch "gawk-shell.patch")
166 (package-source gawk))))))
168 `(;; Starting from gawk 4.1.0, some of the tests for the
169 ;; plug-in mechanism just fail on static builds:
171 ;; ./fts.awk:1: error: can't open shared library `filefuncs' for reading (No such file or directory)
174 ,@(substitute-keyword-arguments (package-arguments gawk)
177 'configure 'no-export-dynamic
179 ;; Since we use `-static', remove
180 ;; `-export-dynamic'.
181 (substitute* "configure"
182 (("-Wl,-export-dynamic") "")))
184 (inputs (if (%current-target-system)
185 `(("bash" ,static-bash))
187 (tar (package (inherit tar)
189 '(#:phases (modify-phases %standard-phases
190 (add-before 'build 'set-shell-file-name
192 ;; Do not use "/bin/sh" to run programs; see
193 ;; <http://lists.gnu.org/archive/html/guix-devel/2016-09/msg02272.html>.
194 (substitute* "src/system.c"
196 (("execv ") "execvp "))
198 (finalize (compose static-package
199 package-with-relocatable-glibc)))
200 `(,@(map (match-lambda
202 (list name (finalize package))))
208 ("coreutils" ,coreutils)
210 ;; We don't want to retain a reference to /gnu/store in the
211 ;; bootstrap versions of egrep/fgrep, so we remove the custom
212 ;; phase added since grep@2.25. The effect is 'egrep' and
213 ;; 'fgrep' look for 'grep' in $PATH.
217 (substitute-keyword-arguments (package-arguments grep)
219 `(modify-phases ,phases
220 (delete 'fix-egrep-and-fgrep)))))))
222 ("bash" ,static-bash))))
224 (define %static-binaries
226 (name "static-binaries")
228 (build-system trivial-build-system)
230 (inputs %static-inputs)
232 `(#:modules ((guix build utils))
235 (use-modules (ice-9 ftw)
242 (define (directory-contents dir)
243 (map (cut string-append dir "/" <>)
244 (scandir dir (negate (cut member <> '("." ".."))))))
246 (define (copy-directory source destination)
247 (for-each (lambda (file)
248 (format #t "copying ~s...~%" file)
250 (string-append destination "/"
252 (directory-contents source)))
254 (let* ((out (assoc-ref %outputs "out"))
255 (bin (string-append out "/bin")))
258 ;; Copy Coreutils binaries.
259 (let* ((coreutils (assoc-ref %build-inputs "coreutils"))
260 (source (string-append coreutils "/bin")))
261 (copy-directory source bin))
263 ;; For the other inputs, copy just one binary, which has the
264 ;; same name as the input.
265 (for-each (match-lambda
267 (let ((source (string-append dir "/bin/" name)))
268 (format #t "copying ~s...~%" source)
270 (string-append bin "/" name)))))
271 (alist-delete "coreutils" %build-inputs))
273 ;; But of course, there are exceptions to this rule.
274 (let ((grep (assoc-ref %build-inputs "grep")))
275 (install-file (string-append grep "/bin/fgrep") bin)
276 (install-file (string-append grep "/bin/egrep") bin))
278 ;; Clear references to the store path.
279 (for-each remove-store-references
280 (directory-contents bin))
282 (with-directory-excursion bin
283 ;; Programs such as Perl's build system want these aliases.
284 (symlink "bash" "sh")
285 (symlink "gawk" "awk"))
288 (synopsis "Statically-linked bootstrap binaries")
290 "Binaries used to bootstrap the distribution.")
294 (define %binutils-static
295 ;; Statically-linked Binutils.
296 (package (inherit binutils)
297 (name "binutils-static")
299 `(#:configure-flags (cons "--disable-gold"
300 ,(match (memq #:configure-flags
301 (package-arguments binutils))
302 ((#:configure-flags flags _ ...)
304 #:strip-flags '("--strip-all")
305 #:phases (alist-cons-before
306 'configure 'all-static
308 ;; The `-all-static' libtool flag can only be passed
309 ;; after `configure', since configure tests don't use
310 ;; libtool, and only for executables built with libtool.
311 (substitute* '("binutils/Makefile.in"
314 (("^LDFLAGS =(.*)$" line)
316 "\nAM_LDFLAGS = -static -all-static\n"))))
317 %standard-phases)))))
319 (define %binutils-static-stripped
320 ;; The subset of Binutils that we need.
321 (package (inherit %binutils-static)
322 (name (string-append (package-name %binutils-static) "-stripped"))
323 (build-system trivial-build-system)
326 `(#:modules ((guix build utils))
329 (use-modules (guix build utils))
331 (setvbuf (current-output-port) _IOLBF)
332 (let* ((in (assoc-ref %build-inputs "binutils"))
333 (out (assoc-ref %outputs "out"))
334 (bin (string-append out "/bin")))
336 (for-each (lambda (file)
337 (let ((target (string-append bin "/" file)))
338 (format #t "copying `~a'...~%" file)
339 (copy-file (string-append in "/bin/" file)
341 (remove-store-references target)))
342 '("ar" "as" "ld" "nm" "objcopy" "objdump"
343 "ranlib" "readelf" "size" "strings" "strip"))
345 (inputs `(("binutils" ,%binutils-static)))))
347 (define (%glibc-stripped)
348 ;; GNU libc's essential shared libraries, dynamic linker, and headers,
349 ;; with all references to store directories stripped. As a result,
350 ;; libc.so is unusable and need to be patched for proper relocation.
351 (let ((glibc (glibc-for-bootstrap)))
352 (package (inherit glibc)
353 (name "glibc-stripped")
354 (build-system trivial-build-system)
356 `(#:modules ((guix build utils)
357 (guix build make-bootstrap))
360 (use-modules (guix build make-bootstrap))
361 (make-stripped-libc (assoc-ref %outputs "out")
362 (assoc-ref %build-inputs "libc")
363 (assoc-ref %build-inputs "kernel-headers")))))
364 (inputs `(("kernel-headers"
365 ,(if (or (and (%current-target-system)
366 (hurd-triplet? (%current-target-system)))
367 (string-suffix? "-hurd" (%current-system)))
369 linux-libre-headers))
370 ("libc" ,(let ((target (%current-target-system)))
373 (parameterize ((%current-target-system #f))
374 (cross-libc target)))
378 (outputs '("out")))))
381 ;; A statically-linked GCC, with stripped-down functionality.
382 (package-with-relocatable-glibc
383 (package (inherit gcc)
385 (outputs '("out")) ; all in one
387 `(#:modules ((guix build utils)
388 (guix build gnu-build-system)
392 ,@(substitute-keyword-arguments (package-arguments gcc)
394 ((#:implicit-inputs? _) #t)
395 ((#:configure-flags flags)
397 ;; We don't need a full bootstrap here.
398 "--disable-bootstrap"
400 ;; Make sure '-static' is passed where it matters.
401 "--with-stage1-ldflags=-static"
403 ;; GCC 4.8+ requires a C++ compiler and library.
404 "--enable-languages=c,c++"
406 ;; Make sure gcc-nm doesn't require liblto_plugin.so.
411 "--disable-libmudflap"
412 "--disable-libatomic"
413 "--disable-libsanitizer"
416 "--disable-libcilkrts"
419 "--disable-libquadmath")
420 (remove (cut string-match "--(.*plugin|enable-languages)" <>)
424 'pre-configure 'remove-lgcc_s
426 ;; Remove the '-lgcc_s' added to GNU_USER_TARGET_LIB_SPEC in
427 ;; the 'pre-configure phase of our main gcc package, because
428 ;; that shared library is not present in this static gcc. See
429 ;; <https://lists.gnu.org/archive/html/guix-devel/2015-01/msg00008.html>.
430 (substitute* (cons "gcc/config/rs6000/sysv4.h"
431 (find-files "gcc/config"
433 ((" -lgcc_s}}") "}}")))
436 (if (%current-target-system)
437 `(;; When doing a Canadian cross, we need GMP/MPFR/MPC both
438 ;; as target inputs and as native inputs; the latter is
439 ;; needed when building build-time tools ('genconstants',
440 ;; etc.) Failing to do that leads to misdetections of
441 ;; declarations by 'gcc/configure', and eventually to
442 ;; duplicate declarations as reported in
443 ;; <https://gcc.gnu.org/bugzilla/show_bug.cgi?id=59217>.
445 ("mpfr-native" ,mpfr)
447 ,@(package-native-inputs gcc))
448 (package-native-inputs gcc))))))
450 (define %gcc-stripped
451 ;; The subset of GCC files needed for bootstrap.
452 (package (inherit gcc)
453 (name "gcc-stripped")
454 (build-system trivial-build-system)
456 (outputs '("out")) ;only one output
458 `(#:modules ((guix build utils))
461 (use-modules (srfi srfi-1)
465 (setvbuf (current-output-port) _IOLBF)
466 (let* ((out (assoc-ref %outputs "out"))
467 (bindir (string-append out "/bin"))
468 (libdir (string-append out "/lib"))
469 (includedir (string-append out "/include"))
470 (libexecdir (string-append out "/libexec"))
471 (gcc (assoc-ref %build-inputs "gcc")))
472 (copy-recursively (string-append gcc "/bin") bindir)
473 (for-each remove-store-references
474 (find-files bindir ".*"))
476 (copy-recursively (string-append gcc "/lib") libdir)
477 (for-each remove-store-references
478 (remove (cut string-suffix? ".h" <>)
479 (find-files libdir ".*")))
481 (copy-recursively (string-append gcc "/libexec")
483 (for-each remove-store-references
484 (find-files libexecdir ".*"))
486 ;; Starting from GCC 4.8, helper programs built natively
487 ;; (‘genchecksum’, ‘gcc-nm’, etc.) rely on C++ headers.
488 (copy-recursively (string-append gcc "/include/c++")
489 (string-append includedir "/c++"))
491 ;; For native builds, check whether the binaries actually work.
492 ,(if (%current-target-system)
494 '(every (lambda (prog)
495 (zero? (system* (string-append gcc "/bin/" prog)
497 '("gcc" "g++" "cpp")))))))
498 (inputs `(("gcc" ,%gcc-static)))))
500 (define %guile-static
501 ;; A statically-linked Guile that is relocatable--i.e., it can search
502 ;; .scm and .go files relative to its installation directory, rather
503 ;; than in hard-coded configure-time paths.
504 (let* ((patches (cons* (search-patch "guile-relocatable.patch")
505 (search-patch "guile-2.2-default-utf8.patch")
506 (search-patch "guile-linux-syscalls.patch")
507 (origin-patches (package-source guile-2.2))))
508 (source (origin (inherit (package-source guile-2.2))
510 (guile (package (inherit guile-2.2)
511 (name (string-append (package-name guile-2.2) "-static"))
513 (synopsis "Statically-linked and relocatable Guile")
515 ;; Remove the 'debug' output (see above for the reason.)
516 (outputs (delete "debug" (package-outputs guile-2.2)))
520 ,@(alist-delete "bdw-gc"
521 (package-propagated-inputs guile-2.2))))
523 `(;; When `configure' checks for ltdl availability, it
524 ;; doesn't try to link using libtool, and thus fails
525 ;; because of a missing -ldl. Work around that.
526 #:configure-flags '("LDFLAGS=-ldl")
528 #:phases (alist-cons-before
529 'configure 'static-guile
531 (substitute* "libguile/Makefile.in"
532 ;; Create a statically-linked `guile'
534 (("^guile_LDFLAGS =")
535 "guile_LDFLAGS = -all-static")
537 ;; Add `-ldl' *after* libguile-2.2.la.
538 (("^guile_LDADD =(.*)$" _ ldadd)
539 (string-append "guile_LDADD = "
540 (string-trim-right ldadd)
544 ;; There are uses of `dynamic-link' in
545 ;; {foreign,coverage}.test that don't fly here.
547 (package-with-relocatable-glibc (static-package guile))))
549 (define %guile-static-stripped
550 ;; A stripped static Guile binary, for use during bootstrap.
551 (package (inherit %guile-static)
552 (name "guile-static-stripped")
553 (build-system trivial-build-system)
555 `(#:modules ((guix build utils))
558 (use-modules (guix build utils))
560 (let* ((in (assoc-ref %build-inputs "guile"))
561 (out (assoc-ref %outputs "out"))
562 (guile1 (string-append in "/bin/guile"))
563 (guile2 (string-append out "/bin/guile")))
564 (mkdir-p (string-append out "/share/guile/2.2"))
565 (copy-recursively (string-append in "/share/guile/2.2")
566 (string-append out "/share/guile/2.2"))
568 (mkdir-p (string-append out "/lib/guile/2.2/ccache"))
569 (copy-recursively (string-append in "/lib/guile/2.2/ccache")
570 (string-append out "/lib/guile/2.2/ccache"))
572 (mkdir (string-append out "/bin"))
573 (copy-file guile1 guile2)
575 ;; Does the relocated Guile work?
576 (and ,(if (%current-target-system)
578 '(zero? (system* guile2 "--version")))
580 ;; Strip store references.
581 (remove-store-references guile2)
583 ;; Does the stripped Guile work? If it aborts, it could be
584 ;; that it tries to open iconv descriptors and fails because
585 ;; libc's iconv data isn't available (see
586 ;; `guile-default-utf8.patch'.)
587 ,(if (%current-target-system)
589 '(zero? (system* guile2 "--version")))))))))
590 (inputs `(("guile" ,%guile-static)))
592 (synopsis "Minimal statically-linked and relocatable Guile")))
594 (define (tarball-package pkg)
595 "Return a package containing a tarball of PKG."
596 (package (inherit pkg)
597 (name (string-append (package-name pkg) "-tarball"))
598 (build-system trivial-build-system)
599 (native-inputs `(("tar" ,tar)
601 (inputs `(("input" ,pkg)))
603 (let ((name (package-name pkg))
604 (version (package-version pkg)))
605 `(#:modules ((guix build utils))
608 (use-modules (guix build utils))
609 (let ((out (assoc-ref %outputs "out"))
610 (input (assoc-ref %build-inputs "input"))
611 (tar (assoc-ref %build-inputs "tar"))
612 (xz (assoc-ref %build-inputs "xz")))
614 (set-path-environment-variable "PATH" '("bin") (list tar xz))
615 (with-directory-excursion input
616 (zero? (system* "tar" "cJvf"
617 (string-append out "/"
620 ,(or (%current-target-system)
624 ;; avoid non-determinism in the archive
625 "--sort=name" "--mtime=@0"
626 "--owner=root:0" "--group=root:0"))))))))))
628 (define %bootstrap-binaries-tarball
629 ;; A tarball with the statically-linked bootstrap binaries.
630 (tarball-package %static-binaries))
632 (define %binutils-bootstrap-tarball
633 ;; A tarball with the statically-linked Binutils programs.
634 (tarball-package %binutils-static-stripped))
636 (define (%glibc-bootstrap-tarball)
637 ;; A tarball with GNU libc's shared libraries, dynamic linker, and headers.
638 (tarball-package (%glibc-stripped)))
640 (define %gcc-bootstrap-tarball
641 ;; A tarball with a dynamic-linked GCC and its headers.
642 (tarball-package %gcc-stripped))
644 (define %guile-bootstrap-tarball
645 ;; A tarball with the statically-linked, relocatable Guile.
646 (tarball-package %guile-static-stripped))
648 (define %bootstrap-tarballs
649 ;; A single derivation containing all the bootstrap tarballs, for
652 (name "bootstrap-tarballs")
655 (build-system trivial-build-system)
657 `(#:modules ((guix build utils))
659 (let ((out (assoc-ref %outputs "out")))
660 (use-modules (guix build utils)
664 (setvbuf (current-output-port) _IOLBF)
667 (for-each (match-lambda
669 (for-each (lambda (file)
670 (format #t "~a -> ~a~%" file out)
671 (symlink file (basename file)))
672 (find-files directory "\\.tar\\."))))
675 (inputs `(("guile-tarball" ,%guile-bootstrap-tarball)
676 ("gcc-tarball" ,%gcc-bootstrap-tarball)
677 ("binutils-tarball" ,%binutils-bootstrap-tarball)
678 ("glibc-tarball" ,(%glibc-bootstrap-tarball))
679 ("coreutils&co-tarball" ,%bootstrap-binaries-tarball)))
680 (synopsis "Tarballs containing all the bootstrap binaries")
681 (description synopsis)
685 ;;; make-bootstrap.scm ends here