1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019 Ludovic Courtès <ludo@gnu.org>
3 ;;; Copyright © 2014, 2015, 2018 Mark H Weaver <mhw@netris.org>
4 ;;; Copyright © 2017 Efraim Flashner <efraim@flashner.co.il>
5 ;;; Copyright © 2019 Carl Dong <contact@carldong.me>
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 bootstrap)
23 #:use-module (guix licenses)
24 #:use-module (gnu packages)
25 #:use-module (guix packages)
26 #:use-module (guix download)
27 #:use-module (guix build-system)
28 #:use-module (guix build-system gnu)
29 #:use-module (guix build-system trivial)
30 #:use-module ((guix store)
31 #:select (run-with-store add-to-store add-text-to-store))
32 #:use-module ((guix derivations)
33 #:select (derivation derivation-input derivation->output-path))
34 #:use-module ((guix utils) #:select (gnu-triplet->nix-system))
35 #:use-module (guix memoization)
36 #:use-module (srfi srfi-1)
37 #:use-module (srfi srfi-26)
38 #:use-module (ice-9 match)
39 #:export (bootstrap-origin
40 package-with-bootstrap-guile
43 bootstrap-guile-origin
46 %bootstrap-coreutils&co
54 ;;; Pre-built packages that are used to bootstrap the
55 ;;; distribution--i.e., to build all the core packages from scratch.
62 ;;; Helper procedures.
65 (define (bootstrap-origin source)
66 "Return a variant of SOURCE, an <origin> instance, whose method uses
67 %BOOTSTRAP-GUILE to do its job."
69 (lambda* (url hash-algo hash
70 #:optional name #:key system)
71 (fetch url hash-algo hash name
72 #:guile %bootstrap-guile
75 (define %bootstrap-patch-inputs
76 ;; Packages used when an <origin> has a non-empty 'patches' field.
77 `(("tar" ,%bootstrap-coreutils&co)
78 ("xz" ,%bootstrap-coreutils&co)
79 ("bzip2" ,%bootstrap-coreutils&co)
80 ("gzip" ,%bootstrap-coreutils&co)
81 ("patch" ,%bootstrap-coreutils&co)))
83 (let ((orig-method (origin-method source)))
84 (origin (inherit source)
85 (method (cond ((eq? orig-method url-fetch)
88 (patch-guile %bootstrap-guile)
89 (patch-inputs %bootstrap-patch-inputs)
91 ;; Patches can be origins as well, so process them.
92 (patches (map (match-lambda
94 (bootstrap-origin patch))
96 (origin-patches source))))))
98 (define* (package-from-tarball name source program-to-test description
100 "Return a package that correspond to the extraction of SOURCE.
101 PROGRAM-TO-TEST is a program to run after extraction of SOURCE, to check
102 whether everything is alright. If SNIPPET is provided, it is evaluated after
103 extracting SOURCE. SNIPPET should raise an exception to signal an error; its
104 return value is ignored."
108 (build-system trivial-build-system)
110 `(#:guile ,%bootstrap-guile
111 #:modules ((guix build utils))
113 (let ((out (assoc-ref %outputs "out"))
114 (tar (assoc-ref %build-inputs "tar"))
115 (xz (assoc-ref %build-inputs "xz"))
116 (tarball (assoc-ref %build-inputs "tarball")))
117 (use-modules (guix build utils))
120 (copy-file tarball "binaries.tar.xz")
121 (invoke xz "-d" "binaries.tar.xz")
122 (let ((builddir (getcwd)))
123 (with-directory-excursion out
125 (string-append builddir "/binaries.tar"))
126 ,@(if snippet (list snippet) '())
127 (invoke (string-append "bin/" ,program-to-test)
130 `(("tar" ,(search-bootstrap-binary "tar" (%current-system)))
131 ("xz" ,(search-bootstrap-binary "xz" (%current-system)))
132 ("tarball" ,(bootstrap-origin (source (%current-system))))))
134 (synopsis description)
135 (description description)
139 (define package-with-bootstrap-guile
141 "Return a variant of P such that all its origins are fetched with
143 (define rewritten-input
145 ((name (? origin? o))
146 `(,name ,(bootstrap-origin o)))
147 ((name (? package? p) sub-drvs ...)
148 `(,name ,(package-with-bootstrap-guile p) ,@sub-drvs))
152 (source (match (package-source p)
153 ((? origin? o) (bootstrap-origin o))
155 (inputs (map rewritten-input
157 (native-inputs (map rewritten-input
158 (package-native-inputs p)))
159 (propagated-inputs (map rewritten-input
160 (package-propagated-inputs p)))
161 (replacement (and=> (package-replacement p)
162 package-with-bootstrap-guile)))))
164 (define* (glibc-dynamic-linker
165 #:optional (system (or (and=> (%current-target-system)
166 gnu-triplet->nix-system)
168 "Return the name of Glibc's dynamic linker for SYSTEM."
169 ;; See the 'SYSDEP_KNOWN_INTERPRETER_NAMES' cpp macro in libc.
170 (cond ((string=? system "x86_64-linux") "/lib/ld-linux-x86-64.so.2")
171 ((string=? system "i686-linux") "/lib/ld-linux.so.2")
172 ((string=? system "armhf-linux") "/lib/ld-linux-armhf.so.3")
173 ((string=? system "mips64el-linux") "/lib/ld.so.1")
174 ((string=? system "i586-gnu") "/lib/ld.so.1")
175 ((string=? system "i686-gnu") "/lib/ld.so.1")
176 ((string=? system "aarch64-linux") "/lib/ld-linux-aarch64.so.1")
177 ((string=? system "powerpc-linux") "/lib/ld.so.1")
178 ((string=? system "powerpc64le-linux") "/lib/ld64.so.2")
179 ((string=? system "alpha-linux") "/lib/ld-linux.so.2")
180 ((string=? system "s390x-linux") "/lib/ld64.so.1")
181 ((string=? system "riscv64-linux") "/lib/ld-linux-riscv64-lp64d.so.1")
183 ;; XXX: This one is used bare-bones, without a libc, so add a case
184 ;; here just so we can keep going.
185 ((string=? system "arm-elf") "no-ld.so")
186 ((string=? system "arm-eabi") "no-ld.so")
187 ((string=? system "xtensa-elf") "no-ld.so")
188 ((string=? system "avr") "no-ld.so")
189 ((string=? system "propeller-elf") "no-ld.so")
190 ((string=? system "i686-mingw") "no-ld.so")
191 ((string=? system "x86_64-mingw") "no-ld.so")
192 ((string=? system "vc4-elf") "no-ld.so")
194 (else (error "dynamic linker name not known for this system"
199 ;;; Bootstrap packages.
202 (define %bootstrap-base-urls
203 ;; This is where the initial binaries come from.
204 '("https://alpha.gnu.org/gnu/guix/bootstrap"
205 "http://alpha.gnu.org/gnu/guix/bootstrap"
206 "ftp://alpha.gnu.org/gnu/guix/bootstrap"
207 "http://www.fdn.fr/~lcourtes/software/guix/packages"
208 "http://flashner.co.il/guix/bootstrap"))
210 (define (bootstrap-guile-url-path system)
211 "Return the URI for FILE."
212 (string-append "/" system
215 "/20170217/guile-2.0.14.tar.xz")
217 "/20150101/guile-2.0.11.tar.xz")
219 "/20131110/guile-2.0.9.tar.xz"))))
221 (define (bootstrap-guile-hash system)
222 "Return the SHA256 hash of the Guile bootstrap tarball for SYSTEM."
225 (base32 "1w2p5zyrglzzniqgvyn1b55vprfzhgk8vzbzkkbdgl5248si0yq3"))
227 (base32 "0im800m30abgh7msh331pcbjvb4n02smz5cfzf1srv0kpx3csmxp"))
229 (base32 "0fzp93lvi0hn54acc0fpvhc7bvl0yc853k62l958cihk03q80ilr"))
231 (base32 "1mi3brl7l58aww34rawhvja84xc7l1b4hmwdmc36fp9q9mfx0lg5"))
233 (base32 "1giy2aprjmn5fp9c4s9r125fljw4wv6ixy5739i5bffw4jgr0f9r"))))
235 (define (bootstrap-guile-origin system)
236 "Return an <origin> object for the Guile tarball of SYSTEM."
239 (uri (map (cute string-append <> (bootstrap-guile-url-path system))
240 %bootstrap-base-urls))
241 (sha256 (bootstrap-guile-hash system))))
243 (define (download-bootstrap-guile store system)
244 "Return a derivation that downloads the bootstrap Guile tarball for SYSTEM."
245 (let* ((path (bootstrap-guile-url-path system))
246 (base (basename path))
247 (urls (map (cut string-append <> path) %bootstrap-base-urls)))
248 (run-with-store store
249 (url-fetch urls 'sha256 (bootstrap-guile-hash system)
252 (define* (raw-build store name inputs
253 #:key outputs system search-paths
255 (define (->store file)
256 (add-to-store store file #t "sha256"
257 (or (search-bootstrap-binary file
259 (error "bootstrap binary not found"
262 (let* ((tar (->store "tar"))
264 (mkdir (->store "mkdir"))
265 (bash (->store "bash"))
266 (guile (download-bootstrap-guile store system))
267 ;; The following code, run by the bootstrap guile after it is
268 ;; unpacked, creates a wrapper for itself to set its load path.
269 ;; This replaces the previous non-portable method based on
270 ;; reading the /proc/self/exe symlink.
273 (use-modules (ice-9 match))
274 (match (command-line)
276 (let ((bin-dir (string-append out "/bin"))
277 (guile (string-append out "/bin/guile"))
278 (guile-real (string-append out "/bin/.guile-real"))
279 ;; We must avoid using a bare dollar sign in this code,
280 ;; because it would be interpreted by the shell.
281 (dollar (string (integer->char 36))))
282 (chmod bin-dir #o755)
283 (rename-file guile guile-real)
284 (call-with-output-file guile
288 export GUILE_SYSTEM_PATH=~a/share/guile/2.0
289 export GUILE_SYSTEM_COMPILED_PATH=~a/lib/guile/2.0/ccache
290 exec -a \"~a0\" ~a \"~a@\"\n"
291 bash out out dollar guile-real dollar)))
293 (chmod bin-dir #o555))))))
295 (add-text-to-store store
296 "build-bootstrap-guile.sh"
298 echo \"unpacking bootstrap Guile to '$out'...\"
301 ~a -dc < $GUILE_TARBALL | ~a xv
303 # Use the bootstrap guile to create its own wrapper to set the load path.
304 GUILE_SYSTEM_PATH=$out/share/guile/2.0 \
305 GUILE_SYSTEM_COMPILED_PATH=$out/lib/guile/2.0/ccache \
306 $out/bin/guile -c ~s $out ~a
309 $out/bin/guile --version~%"
311 (format #f "~s" make-guile-wrapper)
313 (list mkdir xz tar bash))))
314 (derivation store name
317 #:inputs (list (derivation-input guile))
318 #:sources (list bash builder)
319 #:env-vars `(("GUILE_TARBALL"
320 . ,(derivation->output-path guile))))))
322 (define* (make-raw-bag name
323 #:key source inputs native-inputs outputs
328 (build-inputs inputs)
331 (define %bootstrap-guile
332 ;; The Guile used to run the build scripts of the initial derivations.
333 ;; It is just unpacked from a tarball containing a pre-built binary.
334 ;; This is typically built using %GUILE-BOOTSTRAP-TARBALL below.
336 ;; XXX: Would need libc's `libnss_files2.so' for proper `getaddrinfo'
337 ;; support (for /etc/services).
338 (let ((raw (build-system
340 (description "Raw build system with direct store access")
341 (lower make-raw-bag))))
343 (name "guile-bootstrap")
347 (synopsis "Bootstrap Guile")
348 (description "Pre-built Guile for bootstrapping purposes.")
352 (define %bootstrap-coreutils&co
353 (package-from-tarball "bootstrap-binaries"
357 (uri (map (cut string-append <> "/" system
360 "/20150101/static-binaries.tar.xz")
362 "/20170217/static-binaries.tar.xz")
364 "/20131110/static-binaries.tar.xz")))
365 %bootstrap-base-urls))
370 "0c533p9dhczzcsa1117gmfq3pc8w362g4mx84ik36srpr7cx2bg4"))
373 "0s5b3jb315n13m1k8095l0a5hfrsz8g0fv1b6riyc5hnxqyphlak"))
376 "0gf0fn2kbpxkjixkmx5f4z6hv6qpmgixl69zgg74dbsfdfj8jdv5"))
379 "18dfiq6c6xhsdpbidigw6480wh0vdgsxqq3xindq4lpdgqlccpfh"))
382 "072y4wyfsj1bs80r6vbybbafy8ya4vfy7qj25dklwk97m6g71753"))))))
383 "fgrep" ; the program to test
384 "Bootstrap binaries of Coreutils, Awk, etc."
386 '(let ((path (list (string-append (getcwd) "/bin"))))
388 (patch-shebang "bin/egrep" path)
389 (patch-shebang "bin/fgrep" path)
390 ;; Starting with grep@2.25 'egrep' and 'fgrep' are shell files
391 ;; that call 'grep'. If the bootstrap 'egrep' and 'fgrep'
392 ;; are not binaries then patch them to execute 'grep' via its
393 ;; absolute file name instead of searching for it in $PATH.
394 (if (not (elf-file? "bin/egrep"))
395 (substitute* '("bin/egrep" "bin/fgrep")
396 (("^exec grep") (string-append (getcwd) "/bin/grep"))))
397 (chmod "bin" #o555))))
399 (define %bootstrap-binutils
400 (package-from-tarball "binutils-bootstrap"
404 (uri (map (cut string-append <> "/" system
407 "/20150101/binutils-2.25.tar.xz")
409 "/20170217/binutils-2.27.tar.xz")
411 "/20131110/binutils-2.23.2.tar.xz")))
412 %bootstrap-base-urls))
417 "1j5yivz7zkjqfsfmxzrrrffwyayjqyfxgpi89df0w4qziqs2dg20"))
420 "14jgwf9gscd7l2pnz610b1zia06dvcm2qyzvni31b8zpgmcai2v9"))
423 "1v7dj6bzn6m36f20gw31l99xaabq4xrhrx3gwqkhhig0mdlmr69q"))
426 "111s7ilfiby033rczc71797xrmaa3qlv179wdvsaq132pd51xv3n"))
429 "1x8kkhcxmfyzg1ddpz2pxs6fbdl6412r7x0nzbmi5n7mj8zw2gy7"))))))
430 "ld" ; the program to test
431 "Bootstrap binaries of the GNU Binutils"))
433 (define %bootstrap-glibc
436 (name "glibc-bootstrap")
439 (build-system trivial-build-system)
441 `(#:guile ,%bootstrap-guile
442 #:modules ((guix build utils))
444 (let ((out (assoc-ref %outputs "out"))
445 (tar (assoc-ref %build-inputs "tar"))
446 (xz (assoc-ref %build-inputs "xz"))
447 (tarball (assoc-ref %build-inputs "tarball")))
448 (use-modules (guix build utils))
451 (copy-file tarball "binaries.tar.xz")
452 (invoke xz "-d" "binaries.tar.xz")
453 (let ((builddir (getcwd)))
454 (with-directory-excursion out
456 (string-append builddir
460 ;; Patch libc.so so it refers to the right path.
461 (substitute* "lib/libc.so"
462 (("/[^ ]+/lib/(libc|ld)" _ prefix)
463 (string-append out "/lib/" prefix)))
467 `(("tar" ,(search-bootstrap-binary "tar" (%current-system)))
468 ("xz" ,(search-bootstrap-binary "xz" (%current-system)))
469 ("tarball" ,(bootstrap-origin
472 (uri (map (cut string-append <> "/" (%current-system)
473 (match (%current-system)
475 "/20150101/glibc-2.20.tar.xz")
477 "/20170217/glibc-2.25.tar.xz")
479 "/20131110/glibc-2.18.tar.xz")))
480 %bootstrap-base-urls))
482 (match (%current-system)
485 "0jlqrgavvnplj1b083s20jj9iddr4lzfvwybw5xrcis9spbfzk7v"))
488 "1hgrccw1zqdc7lvgivwa54d9l3zsim5pqm0dykxg0z522h6gr05w"))
491 "18cmgvpllqfpn6khsmivqib7ys8ymnq0hdzi3qp24prik0ykz8gn"))
494 "07nx3x8598i2924rjnlrncg6rm61c9bmcczbbcpbx0fb742nvv5c"))
497 "0k97a3whzx3apsi9n2cbsrr79ad6lh00klxph9hw4fqyp1abkdsg")))))))))
498 (synopsis "Bootstrap binaries and headers of the GNU C Library")
499 (description synopsis)
503 (define %bootstrap-gcc
504 ;; The initial GCC. Uses binaries from a tarball typically built by
505 ;; %GCC-BOOTSTRAP-TARBALL.
507 (name "gcc-bootstrap")
510 (build-system trivial-build-system)
512 `(#:guile ,%bootstrap-guile
513 #:modules ((guix build utils))
515 (let ((out (assoc-ref %outputs "out"))
516 (tar (assoc-ref %build-inputs "tar"))
517 (xz (assoc-ref %build-inputs "xz"))
518 (bash (assoc-ref %build-inputs "bash"))
519 (libc (assoc-ref %build-inputs "libc"))
520 (tarball (assoc-ref %build-inputs "tarball")))
521 (use-modules (guix build utils)
525 (copy-file tarball "binaries.tar.xz")
526 (invoke xz "-d" "binaries.tar.xz")
527 (let ((builddir (getcwd))
528 (bindir (string-append out "/bin")))
529 (with-directory-excursion out
531 (string-append builddir "/binaries.tar")))
533 (with-directory-excursion bindir
535 (rename-file "gcc" ".gcc-wrapped")
536 (call-with-output-file "gcc"
539 exec ~a/bin/.gcc-wrapped -B~a/lib \
540 -Wl,-rpath -Wl,~a/lib \
541 -Wl,-dynamic-linker -Wl,~a/~a \"$@\"~%"
544 ,(glibc-dynamic-linker))))
549 `(("tar" ,(search-bootstrap-binary "tar" (%current-system)))
550 ("xz" ,(search-bootstrap-binary "xz" (%current-system)))
551 ("bash" ,(search-bootstrap-binary "bash" (%current-system)))
552 ("libc" ,%bootstrap-glibc)
553 ("tarball" ,(bootstrap-origin
556 (uri (map (cut string-append <> "/" (%current-system)
557 (match (%current-system)
559 "/20150101/gcc-4.8.4.tar.xz")
561 "/20170217/gcc-5.4.0.tar.xz")
563 "/20131110/gcc-4.8.2.tar.xz")))
564 %bootstrap-base-urls))
566 (match (%current-system)
569 "17ga4m6195n4fnbzdkmik834znkhs53nkypp6557pl1ps7dgqbls"))
572 "150c1arrf2k8vfy6dpxh59vcgs4p1bgiz2av5m19dynpks7rjnyw"))
575 "0ghz825yzp43fxw53kd6afm8nkz16f7dxi9xi40bfwc8x3nbbr8v"))
578 "1ar3vdzyqbfm0z36kmvazvfswxhcihlacl2dzdjgiq25cqnq9ih1"))
581 "1m5miqkyng45l745n0sfafdpjkqv9225xf44jqkygwsipj2cv9ks")))))))))
583 (list (search-path-specification
585 (files '("include")))
586 (search-path-specification
587 (variable "LIBRARY_PATH")
588 (files '("lib" "lib64")))))
589 (synopsis "Bootstrap binaries of the GNU Compiler Collection")
590 (description synopsis)
594 (define %bootstrap-inputs
595 ;; The initial, pre-built inputs. From now on, we can start building our
597 `(("libc" ,%bootstrap-glibc)
598 ("gcc" ,%bootstrap-gcc)
599 ("binutils" ,%bootstrap-binutils)
600 ("coreutils&co" ,%bootstrap-coreutils&co)
602 ;; In gnu-build-system.scm, we rely on the availability of Bash.
603 ("bash" ,%bootstrap-coreutils&co)))
605 ;;; bootstrap.scm ends here