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 © 2017 Efraim Flashner <efraim@flashner.co.il>
4 ;;; Copyright © 2018 Tobias Geerinckx-Rice <me@tobias.gr>
5 ;;; Copyright © 2018 Mark H Weaver <mhw@netris.org>
7 ;;; This file is part of GNU Guix.
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.
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.
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/>.
22 (define-module (gnu packages make-bootstrap)
23 #:use-module (guix utils)
24 #:use-module (guix packages)
25 #:use-module ((guix licenses) #:select (gpl3+))
26 #:use-module (guix build-system trivial)
27 #:use-module (guix build-system gnu)
28 #:use-module ((gnu packages) #:select (search-patch))
29 #:use-module (gnu packages base)
30 #:use-module (gnu packages cross-base)
31 #:use-module (gnu packages bash)
32 #:use-module (gnu packages compression)
33 #:use-module (gnu packages gawk)
34 #:use-module (gnu packages gcc)
35 #:use-module (gnu packages guile)
36 #:use-module (gnu packages bdw-gc)
37 #:use-module (gnu packages libunistring)
38 #:use-module (gnu packages linux)
39 #:use-module (gnu packages hurd)
40 #:use-module (gnu packages multiprecision)
41 #:use-module (ice-9 match)
42 #:use-module (srfi srfi-1)
43 #:export (%bootstrap-binaries-tarball
44 %binutils-bootstrap-tarball
45 %glibc-bootstrap-tarball
46 %gcc-bootstrap-tarball
47 %guile-bootstrap-tarball
50 %guile-static-stripped))
54 ;;; This module provides tools to build tarballs of the "bootstrap binaries"
55 ;;; used in (gnu packages bootstrap). These statically-linked binaries are
56 ;;; taken for granted and used as the root of the whole bootstrap procedure.
60 (define* (glibc-for-bootstrap #:optional (base glibc))
61 "Return a libc deriving from BASE whose `system' and `popen' functions looks
62 for `sh' in $PATH, and without nscd, and with static NSS modules."
63 (package (inherit base)
64 (source (origin (inherit (package-source base))
65 (patches (cons (search-patch "glibc-bootstrap-system.patch")
66 (origin-patches (package-source base))))))
68 (substitute-keyword-arguments (package-arguments base)
69 ((#:configure-flags flags)
70 ;; Arrange so that getaddrinfo & co. do not contact the nscd,
71 ;; and can use statically-linked NSS modules.
72 `(cons* "--disable-nscd" "--disable-build-nscd"
76 ;; Remove the 'debug' output to allow bit-reproducible builds (when the
77 ;; 'debug' output is used, ELF files end up with a .gnu_debuglink, which
78 ;; includes a CRC of the corresponding debugging symbols; those symbols
79 ;; contain store file names, so the CRC changes at every rebuild.)
80 (outputs (delete "debug" (package-outputs base)))))
82 (define (package-with-relocatable-glibc p)
83 "Return a variant of P that uses the libc as defined by
84 `glibc-for-bootstrap'."
86 (define (cross-bootstrap-libc)
87 (let ((target (%current-target-system)))
89 ;; `cross-libc' already returns a cross libc, so clear
90 ;; %CURRENT-TARGET-SYSTEM.
91 (parameterize ((%current-target-system #f))
92 (cross-libc target)))))
94 ;; Standard inputs with the above libc and corresponding GCC.
97 (if (%current-target-system) ; is this package cross built?
98 `(("cross-libc" ,(cross-bootstrap-libc)))
101 (define (native-inputs)
102 (if (%current-target-system)
103 (let ((target (%current-target-system)))
104 `(("cross-gcc" ,(cross-gcc target
105 #:xbinutils (cross-binutils target)
106 #:libc (cross-bootstrap-libc)))
107 ("cross-binutils" ,(cross-binutils target))
109 `(("libc" ,(glibc-for-bootstrap))
110 ("libc:static" ,(glibc-for-bootstrap) "static")
111 ("gcc" ,(package (inherit gcc)
112 (outputs '("out")) ; all in one so libgcc_s is easily found
114 `(("libc" ,(glibc-for-bootstrap))
115 ("libc:static" ,(glibc-for-bootstrap) "static")
116 ,@(package-inputs gcc)))))
117 ,@(fold alist-delete (%final-inputs) '("libc" "gcc")))))
119 (package-with-explicit-inputs p inputs
120 (current-source-location)
121 #:native-inputs native-inputs))
123 (define %static-inputs
124 ;; Packages that are to be used as %BOOTSTRAP-INPUTS.
125 (let ((coreutils (package (inherit coreutils)
129 "--disable-silent-rules"
130 "--enable-no-install-program=stdbuf,libstdbuf.so"
131 "CFLAGS=-Os -g0" ; smaller, please
132 "LDFLAGS=-static -pthread")
133 #:tests? #f ; signal-related Gnulib tests fail
134 ,@(package-arguments coreutils)))
136 ;; Remove optional dependencies such as GMP. Keep Perl
137 ;; except if it's missing (which is the case when
139 (inputs (match (assoc "perl" (package-inputs coreutils))
143 ;; Remove the 'debug' output (see above for the reason.)
145 (bzip2 (package (inherit bzip2)
147 (substitute-keyword-arguments (package-arguments bzip2)
149 `(modify-phases ,phases
150 (add-before 'build 'dash-static
152 (substitute* "Makefile"
153 (("^LDFLAGS[[:blank:]]*=.*$")
154 "LDFLAGS = -static"))
156 (xz (package (inherit xz)
158 `(#:strip-flags '("--strip-all")
159 #:phases (modify-phases %standard-phases
160 (add-before 'configure 'static-executable
162 ;; Ask Libtool for a static executable.
163 (substitute* "src/xz/Makefile.in"
165 "xz_LDADD = -all-static"))
167 (gawk (package (inherit gawk)
168 (source (origin (inherit (package-source gawk))
169 (patches (cons (search-patch "gawk-shell.patch")
171 (package-source gawk))))))
173 `(;; Starting from gawk 4.1.0, some of the tests for the
174 ;; plug-in mechanism just fail on static builds:
176 ;; ./fts.awk:1: error: can't open shared library `filefuncs' for reading (No such file or directory)
179 ,@(substitute-keyword-arguments (package-arguments gawk)
181 `(modify-phases ,phases
182 (add-before 'configure 'no-export-dynamic
184 ;; Since we use `-static', remove
185 ;; `-export-dynamic'.
186 (substitute* "configure"
187 (("-Wl,-export-dynamic") ""))
189 (inputs (if (%current-target-system)
190 `(("bash" ,static-bash))
192 (tar (package (inherit tar)
194 (substitute-keyword-arguments (package-arguments tar)
196 `(modify-phases ,phases
197 (replace 'set-shell-file-name
199 ;; Do not use "/bin/sh" to run programs; see
200 ;; <http://lists.gnu.org/archive/html/guix-devel/2016-09/msg02272.html>.
201 (substitute* "src/system.c"
203 (("execv ") "execvp "))
205 ;; We don't want to retain a reference to /gnu/store in the bootstrap
206 ;; versions of egrep/fgrep, so we remove the custom phase added since
207 ;; grep@2.25. The effect is 'egrep' and 'fgrep' look for 'grep' in
211 (inputs '()) ;remove PCRE, which is optional
213 (substitute-keyword-arguments (package-arguments grep)
215 `(modify-phases ,phases
216 (delete 'fix-egrep-and-fgrep)))))))
217 (finalize (compose static-package
218 package-with-relocatable-glibc)))
219 `(,@(map (match-lambda
221 (list name (finalize package))))
227 ("coreutils" ,coreutils)
231 ("bash" ,static-bash))))
233 (define %static-binaries
235 (name "static-binaries")
237 (build-system trivial-build-system)
239 (inputs %static-inputs)
241 `(#:modules ((guix build utils))
244 (use-modules (ice-9 ftw)
251 (define (directory-contents dir)
252 (map (cut string-append dir "/" <>)
253 (scandir dir (negate (cut member <> '("." ".."))))))
255 (define (copy-directory source destination)
256 (for-each (lambda (file)
257 (format #t "copying ~s...~%" file)
259 (string-append destination "/"
261 (directory-contents source)))
263 (let* ((out (assoc-ref %outputs "out"))
264 (bin (string-append out "/bin")))
267 ;; Copy Coreutils binaries.
268 (let* ((coreutils (assoc-ref %build-inputs "coreutils"))
269 (source (string-append coreutils "/bin")))
270 (copy-directory source bin))
272 ;; For the other inputs, copy just one binary, which has the
273 ;; same name as the input.
274 (for-each (match-lambda
276 (let ((source (string-append dir "/bin/" name)))
277 (format #t "copying ~s...~%" source)
279 (string-append bin "/" name)))))
280 (alist-delete "coreutils" %build-inputs))
282 ;; But of course, there are exceptions to this rule.
283 (let ((grep (assoc-ref %build-inputs "grep")))
284 (install-file (string-append grep "/bin/fgrep") bin)
285 (install-file (string-append grep "/bin/egrep") bin))
287 ;; Clear references to the store path.
288 (for-each remove-store-references
289 (directory-contents bin))
291 (with-directory-excursion bin
292 ;; Programs such as Perl's build system want these aliases.
293 (symlink "bash" "sh")
294 (symlink "gawk" "awk"))
297 (synopsis "Statically-linked bootstrap binaries")
299 "Binaries used to bootstrap the distribution.")
303 (define %binutils-static
304 ;; Statically-linked Binutils.
305 (package (inherit binutils)
306 (name "binutils-static")
308 `(#:configure-flags (cons "--disable-gold"
309 ,(match (memq #:configure-flags
310 (package-arguments binutils))
311 ((#:configure-flags flags _ ...)
313 #:strip-flags '("--strip-all")
314 #:phases (modify-phases %standard-phases
315 (add-before 'configure 'all-static
317 ;; The `-all-static' libtool flag can only be passed
318 ;; after `configure', since configure tests don't use
319 ;; libtool, and only for executables built with libtool.
320 (substitute* '("binutils/Makefile.in"
323 (("^LDFLAGS =(.*)$" line)
325 "\nAM_LDFLAGS = -static -all-static\n")))
328 (define %binutils-static-stripped
329 ;; The subset of Binutils that we need.
330 (package (inherit %binutils-static)
331 (name (string-append (package-name %binutils-static) "-stripped"))
332 (build-system trivial-build-system)
335 `(#:modules ((guix build utils))
338 (use-modules (guix build utils))
340 (setvbuf (current-output-port) _IOLBF)
341 (let* ((in (assoc-ref %build-inputs "binutils"))
342 (out (assoc-ref %outputs "out"))
343 (bin (string-append out "/bin")))
345 (for-each (lambda (file)
346 (let ((target (string-append bin "/" file)))
347 (format #t "copying `~a'...~%" file)
348 (copy-file (string-append in "/bin/" file)
350 (remove-store-references target)))
351 '("ar" "as" "ld" "nm" "objcopy" "objdump"
352 "ranlib" "readelf" "size" "strings" "strip"))
354 (inputs `(("binutils" ,%binutils-static)))))
356 (define (%glibc-stripped)
357 ;; GNU libc's essential shared libraries, dynamic linker, and headers,
358 ;; with all references to store directories stripped. As a result,
359 ;; libc.so is unusable and need to be patched for proper relocation.
360 (let ((glibc (glibc-for-bootstrap)))
361 (package (inherit glibc)
362 (name "glibc-stripped")
363 (build-system trivial-build-system)
365 `(#:modules ((guix build utils)
366 (guix build make-bootstrap))
369 (use-modules (guix build make-bootstrap))
370 (make-stripped-libc (assoc-ref %outputs "out")
371 (assoc-ref %build-inputs "libc")
372 (assoc-ref %build-inputs "kernel-headers")))))
373 (inputs `(("kernel-headers"
374 ,(if (or (and (%current-target-system)
375 (hurd-triplet? (%current-target-system)))
376 (string-suffix? "-hurd" (%current-system)))
378 linux-libre-headers))
379 ("libc" ,(let ((target (%current-target-system)))
382 (parameterize ((%current-target-system #f))
383 (cross-libc target)))
387 (outputs '("out")))))
390 ;; A statically-linked GCC, with stripped-down functionality.
391 (package-with-relocatable-glibc
392 (package (inherit gcc)
394 (outputs '("out")) ; all in one
396 `(#:modules ((guix build utils)
397 (guix build gnu-build-system)
401 ,@(substitute-keyword-arguments (package-arguments gcc)
403 ((#:implicit-inputs? _) #t)
404 ((#:configure-flags flags)
406 ;; We don't need a full bootstrap here.
407 "--disable-bootstrap"
409 ;; Make sure '-static' is passed where it matters.
410 "--with-stage1-ldflags=-static"
412 ;; GCC 4.8+ requires a C++ compiler and library.
413 "--enable-languages=c,c++"
415 ;; Make sure gcc-nm doesn't require liblto_plugin.so.
420 "--disable-libmudflap"
421 "--disable-libatomic"
422 "--disable-libsanitizer"
425 "--disable-libcilkrts"
428 "--disable-libquadmath")
429 (remove (cut string-match "--(.*plugin|enable-languages)" <>)
432 `(modify-phases ,phases
433 (add-after 'pre-configure 'remove-lgcc_s
435 ;; Remove the '-lgcc_s' added to GNU_USER_TARGET_LIB_SPEC in
436 ;; the 'pre-configure phase of our main gcc package, because
437 ;; that shared library is not present in this static gcc. See
438 ;; <https://lists.gnu.org/archive/html/guix-devel/2015-01/msg00008.html>.
439 (substitute* (cons "gcc/config/rs6000/sysv4.h"
440 (find-files "gcc/config"
442 ((" -lgcc_s}}") "}}"))
445 `(("zlib:static" ,zlib "static")
446 ,@(package-inputs gcc)))
448 (if (%current-target-system)
449 `(;; When doing a Canadian cross, we need GMP/MPFR/MPC both
450 ;; as target inputs and as native inputs; the latter is
451 ;; needed when building build-time tools ('genconstants',
452 ;; etc.) Failing to do that leads to misdetections of
453 ;; declarations by 'gcc/configure', and eventually to
454 ;; duplicate declarations as reported in
455 ;; <https://gcc.gnu.org/bugzilla/show_bug.cgi?id=59217>.
457 ("mpfr-native" ,mpfr)
459 ,@(package-native-inputs gcc))
460 (package-native-inputs gcc))))))
462 (define %gcc-stripped
463 ;; The subset of GCC files needed for bootstrap.
464 (package (inherit gcc)
465 (name "gcc-stripped")
466 (build-system trivial-build-system)
468 (outputs '("out")) ;only one output
470 `(#:modules ((guix build utils))
473 (use-modules (srfi srfi-1)
477 (setvbuf (current-output-port) _IOLBF)
478 (let* ((out (assoc-ref %outputs "out"))
479 (bindir (string-append out "/bin"))
480 (libdir (string-append out "/lib"))
481 (includedir (string-append out "/include"))
482 (libexecdir (string-append out "/libexec"))
483 (gcc (assoc-ref %build-inputs "gcc")))
484 (copy-recursively (string-append gcc "/bin") bindir)
485 (for-each remove-store-references
486 (find-files bindir ".*"))
488 (copy-recursively (string-append gcc "/lib") libdir)
489 (for-each remove-store-references
490 (remove (cut string-suffix? ".h" <>)
491 (find-files libdir ".*")))
493 (copy-recursively (string-append gcc "/libexec")
495 (for-each remove-store-references
496 (find-files libexecdir ".*"))
498 ;; Starting from GCC 4.8, helper programs built natively
499 ;; (‘genchecksum’, ‘gcc-nm’, etc.) rely on C++ headers.
500 (copy-recursively (string-append gcc "/include/c++")
501 (string-append includedir "/c++"))
503 ;; For native builds, check whether the binaries actually work.
504 ,@(if (%current-target-system)
506 '((for-each (lambda (prog)
507 (invoke (string-append gcc "/bin/" prog)
509 '("gcc" "g++" "cpp"))))
512 (inputs `(("gcc" ,%gcc-static)))))
514 (define %guile-static
515 ;; A statically-linked Guile that is relocatable--i.e., it can search
516 ;; .scm and .go files relative to its installation directory, rather
517 ;; than in hard-coded configure-time paths.
518 (let* ((patches (cons* (search-patch "guile-relocatable.patch")
519 (search-patch "guile-2.2-default-utf8.patch")
520 (search-patch "guile-linux-syscalls.patch")
521 (origin-patches (package-source guile-2.2))))
522 (source (origin (inherit (package-source guile-2.2))
524 (guile (package (inherit guile-2.2)
525 (name (string-append (package-name guile-2.2) "-static"))
527 (synopsis "Statically-linked and relocatable Guile")
529 ;; Remove the 'debug' output (see above for the reason.)
530 (outputs (delete "debug" (package-outputs guile-2.2)))
533 `(("libunistring:static" ,libunistring "static")
534 ,@(package-inputs guile-2.2)))
538 ,@(alist-delete "bdw-gc"
539 (package-propagated-inputs guile-2.2))))
541 (substitute-keyword-arguments (package-arguments guile-2.2)
542 ((#:configure-flags flags '())
543 ;; When `configure' checks for ltdl availability, it
544 ;; doesn't try to link using libtool, and thus fails
545 ;; because of a missing -ldl. Work around that.
547 ((#:phases phases '%standard-phases)
548 `(modify-phases ,phases
550 ;; Do not record the absolute file name of 'sh' in
551 ;; (ice-9 popen). This makes 'open-pipe' unusable in
552 ;; a build chroot ('open-pipe*' is fine) but avoids
553 ;; keeping a reference to Bash.
554 (delete 'pre-configure)
556 (add-before 'configure 'static-guile
558 (substitute* "libguile/Makefile.in"
559 ;; Create a statically-linked `guile'
561 (("^guile_LDFLAGS =")
562 "guile_LDFLAGS = -all-static")
564 ;; Add `-ldl' *after* libguile-2.2.la.
565 (("^guile_LDADD =(.*)$" _ ldadd)
566 (string-append "guile_LDADD = "
567 (string-trim-right ldadd)
570 ;; There are uses of `dynamic-link' in
571 ;; {foreign,coverage}.test that don't fly here.
573 (package-with-relocatable-glibc (static-package guile))))
575 (define %guile-static-stripped
576 ;; A stripped static Guile binary, for use during bootstrap.
577 (package (inherit %guile-static)
578 (name "guile-static-stripped")
579 (build-system trivial-build-system)
581 ;; The end result should depend on nothing but itself.
582 `(#:allowed-references ("out")
583 #:modules ((guix build utils))
586 (use-modules (guix build utils))
588 (let* ((in (assoc-ref %build-inputs "guile"))
589 (out (assoc-ref %outputs "out"))
590 (guile1 (string-append in "/bin/guile"))
591 (guile2 (string-append out "/bin/guile")))
592 (mkdir-p (string-append out "/share/guile/2.2"))
593 (copy-recursively (string-append in "/share/guile/2.2")
594 (string-append out "/share/guile/2.2"))
596 (mkdir-p (string-append out "/lib/guile/2.2/ccache"))
597 (copy-recursively (string-append in "/lib/guile/2.2/ccache")
598 (string-append out "/lib/guile/2.2/ccache"))
600 (mkdir (string-append out "/bin"))
601 (copy-file guile1 guile2)
603 ;; Verify that the relocated Guile works.
604 ,@(if (%current-target-system)
606 '((invoke guile2 "--version")))
608 ;; Strip store references.
609 (remove-store-references guile2)
611 ;; Verify that the stripped Guile works. If it aborts, it could be
612 ;; that it tries to open iconv descriptors and fails because libc's
613 ;; iconv data isn't available (see `guile-default-utf8.patch'.)
614 ,@(if (%current-target-system)
616 '((invoke guile2 "--version")))
619 (inputs `(("guile" ,%guile-static)))
621 (synopsis "Minimal statically-linked and relocatable Guile")))
623 (define (tarball-package pkg)
624 "Return a package containing a tarball of PKG."
625 (package (inherit pkg)
626 (name (string-append (package-name pkg) "-tarball"))
627 (build-system trivial-build-system)
628 (native-inputs `(("tar" ,tar)
630 (inputs `(("input" ,pkg)))
632 (let ((name (package-name pkg))
633 (version (package-version pkg)))
634 `(#:modules ((guix build utils))
637 (use-modules (guix build utils))
638 (let ((out (assoc-ref %outputs "out"))
639 (input (assoc-ref %build-inputs "input"))
640 (tar (assoc-ref %build-inputs "tar"))
641 (xz (assoc-ref %build-inputs "xz")))
643 (set-path-environment-variable "PATH" '("bin") (list tar xz))
644 (with-directory-excursion input
646 (string-append out "/"
649 ,(or (%current-target-system)
653 ;; avoid non-determinism in the archive
654 "--sort=name" "--mtime=@0"
655 "--owner=root:0" "--group=root:0")))))))))
657 (define %bootstrap-binaries-tarball
658 ;; A tarball with the statically-linked bootstrap binaries.
659 (tarball-package %static-binaries))
661 (define %binutils-bootstrap-tarball
662 ;; A tarball with the statically-linked Binutils programs.
663 (tarball-package %binutils-static-stripped))
665 (define (%glibc-bootstrap-tarball)
666 ;; A tarball with GNU libc's shared libraries, dynamic linker, and headers.
667 (tarball-package (%glibc-stripped)))
669 (define %gcc-bootstrap-tarball
670 ;; A tarball with a dynamic-linked GCC and its headers.
671 (tarball-package %gcc-stripped))
673 (define %guile-bootstrap-tarball
674 ;; A tarball with the statically-linked, relocatable Guile.
675 (tarball-package %guile-static-stripped))
677 (define %bootstrap-tarballs
678 ;; A single derivation containing all the bootstrap tarballs, for
681 (name "bootstrap-tarballs")
684 (build-system trivial-build-system)
686 `(#:modules ((guix build utils))
688 (let ((out (assoc-ref %outputs "out")))
689 (use-modules (guix build utils)
693 (setvbuf (current-output-port) _IOLBF)
696 (for-each (match-lambda
698 (for-each (lambda (file)
699 (format #t "~a -> ~a~%" file out)
700 (symlink file (basename file)))
701 (find-files directory "\\.tar\\."))))
704 (inputs `(("guile-tarball" ,%guile-bootstrap-tarball)
705 ("gcc-tarball" ,%gcc-bootstrap-tarball)
706 ("binutils-tarball" ,%binutils-bootstrap-tarball)
707 ("glibc-tarball" ,(%glibc-bootstrap-tarball))
708 ("coreutils&co-tarball" ,%bootstrap-binaries-tarball)))
709 (synopsis "Tarballs containing all the bootstrap binaries")
710 (description synopsis)
714 ;;; make-bootstrap.scm ends here