1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2012, 2013, 2014, 2015, 2016, 2017 Ludovic Courtès <ludo@gnu.org>
3 ;;; Copyright © 2014, 2015 Mark H Weaver <mhw@netris.org>
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 bootstrap)
21 #:use-module (guix licenses)
22 #:use-module (gnu packages)
23 #:use-module (guix packages)
24 #:use-module (guix download)
25 #:use-module (guix build-system)
26 #:use-module (guix build-system gnu)
27 #:use-module (guix build-system trivial)
28 #:use-module ((guix store) #:select (add-to-store add-text-to-store))
29 #:use-module ((guix derivations) #:select (derivation))
30 #:use-module ((guix utils) #:select (gnu-triplet->nix-system))
31 #:use-module (guix memoization)
32 #:use-module (srfi srfi-1)
33 #:use-module (srfi srfi-26)
34 #:use-module (ice-9 match)
35 #:export (bootstrap-origin
36 package-with-bootstrap-guile
40 %bootstrap-coreutils&co
48 ;;; Pre-built packages that are used to bootstrap the
49 ;;; distribution--i.e., to build all the core packages from scratch.
56 ;;; Helper procedures.
59 (define (bootstrap-origin source)
60 "Return a variant of SOURCE, an <origin> instance, whose method uses
61 %BOOTSTRAP-GUILE to do its job."
63 (lambda* (url hash-algo hash
64 #:optional name #:key system)
65 (fetch url hash-algo hash name
66 #:guile %bootstrap-guile
69 (define %bootstrap-patch-inputs
70 ;; Packages used when an <origin> has a non-empty 'patches' field.
71 `(("tar" ,%bootstrap-coreutils&co)
72 ("xz" ,%bootstrap-coreutils&co)
73 ("bzip2" ,%bootstrap-coreutils&co)
74 ("gzip" ,%bootstrap-coreutils&co)
75 ("patch" ,%bootstrap-coreutils&co)))
77 (let ((orig-method (origin-method source)))
78 (origin (inherit source)
79 (method (cond ((eq? orig-method url-fetch)
82 (patch-guile %bootstrap-guile)
83 (patch-inputs %bootstrap-patch-inputs)
85 ;; Patches can be origins as well, so process them.
86 (patches (map (match-lambda
88 (bootstrap-origin patch))
90 (origin-patches source))))))
92 (define* (package-from-tarball name source program-to-test description
94 "Return a package that correspond to the extraction of SOURCE.
95 PROGRAM-TO-TEST is a program to run after extraction of SOURCE, to
96 check whether everything is alright. If SNIPPET is provided, it is
97 evaluated after extracting SOURCE. SNIPPET should return true if
98 successful, or false to signal an error."
102 (build-system trivial-build-system)
104 `(#:guile ,%bootstrap-guile
105 #:modules ((guix build utils))
107 (let ((out (assoc-ref %outputs "out"))
108 (tar (assoc-ref %build-inputs "tar"))
109 (xz (assoc-ref %build-inputs "xz"))
110 (tarball (assoc-ref %build-inputs "tarball")))
111 (use-modules (guix build utils))
114 (copy-file tarball "binaries.tar.xz")
115 (system* xz "-d" "binaries.tar.xz")
116 (let ((builddir (getcwd)))
117 (with-directory-excursion out
118 (and (zero? (system* tar "xvf"
119 (string-append builddir "/binaries.tar")))
120 ,@(if snippet (list snippet) '())
121 (zero? (system* (string-append "bin/" ,program-to-test)
124 `(("tar" ,(search-bootstrap-binary "tar" (%current-system)))
125 ("xz" ,(search-bootstrap-binary "xz" (%current-system)))
126 ("tarball" ,(bootstrap-origin (source (%current-system))))))
128 (synopsis description)
129 (description description)
133 (define package-with-bootstrap-guile
135 "Return a variant of P such that all its origins are fetched with
137 (define rewritten-input
139 ((name (? origin? o))
140 `(,name ,(bootstrap-origin o)))
141 ((name (? package? p) sub-drvs ...)
142 `(,name ,(package-with-bootstrap-guile p) ,@sub-drvs))
146 (source (match (package-source p)
147 ((? origin? o) (bootstrap-origin o))
149 (inputs (map rewritten-input
151 (native-inputs (map rewritten-input
152 (package-native-inputs p)))
153 (propagated-inputs (map rewritten-input
154 (package-propagated-inputs p)))
155 (replacement (and=> (package-replacement p)
156 package-with-bootstrap-guile)))))
158 (define* (glibc-dynamic-linker
159 #:optional (system (or (and=> (%current-target-system)
160 gnu-triplet->nix-system)
162 "Return the name of Glibc's dynamic linker for SYSTEM."
163 (cond ((string=? system "x86_64-linux") "/lib/ld-linux-x86-64.so.2")
164 ((string=? system "i686-linux") "/lib/ld-linux.so.2")
165 ((string=? system "armhf-linux") "/lib/ld-linux-armhf.so.3")
166 ((string=? system "mips64el-linux") "/lib/ld.so.1")
167 ((string=? system "i586-gnu") "/lib/ld.so.1")
168 ((string=? system "i686-gnu") "/lib/ld.so.1")
169 ((string=? system "aarch64-linux") "/lib/ld-linux-aarch64.so.1")
171 ;; XXX: This one is used bare-bones, without a libc, so add a case
172 ;; here just so we can keep going.
173 ((string=? system "arm-eabi") "no-ld.so")
174 ((string=? system "xtensa-elf") "no-ld.so")
175 ((string=? system "avr") "no-ld.so")
176 ((string=? system "propeller-elf") "no-ld.so")
177 ((string=? system "i686-mingw") "no-ld.so")
179 (else (error "dynamic linker name not known for this system"
184 ;;; Bootstrap packages.
187 (define* (raw-build store name inputs
188 #:key outputs system search-paths
190 (define (->store file)
191 (add-to-store store file #t "sha256"
192 (or (search-bootstrap-binary file
194 (error "bootstrap binary not found"
197 (let* ((tar (->store "tar"))
199 (mkdir (->store "mkdir"))
200 (bash (->store "bash"))
201 (guile (->store (match system
203 "guile-2.0.11.tar.xz")
205 "guile-2.0.9.tar.xz"))))
206 ;; The following code, run by the bootstrap guile after it is
207 ;; unpacked, creates a wrapper for itself to set its load path.
208 ;; This replaces the previous non-portable method based on
209 ;; reading the /proc/self/exe symlink.
212 (use-modules (ice-9 match))
213 (match (command-line)
215 (let ((bin-dir (string-append out "/bin"))
216 (guile (string-append out "/bin/guile"))
217 (guile-real (string-append out "/bin/.guile-real"))
218 ;; We must avoid using a bare dollar sign in this code,
219 ;; because it would be interpreted by the shell.
220 (dollar (string (integer->char 36))))
221 (chmod bin-dir #o755)
222 (rename-file guile guile-real)
223 (call-with-output-file guile
227 export GUILE_SYSTEM_PATH=~a/share/guile/2.0
228 export GUILE_SYSTEM_COMPILED_PATH=~a/lib/guile/2.0/ccache
229 exec -a \"~a0\" ~a \"~a@\"\n"
230 bash out out dollar guile-real dollar)))
232 (chmod bin-dir #o555))))))
234 (add-text-to-store store
235 "build-bootstrap-guile.sh"
237 echo \"unpacking bootstrap Guile to '$out'...\"
242 # Use the bootstrap guile to create its own wrapper to set the load path.
243 GUILE_SYSTEM_PATH=$out/share/guile/2.0 \
244 GUILE_SYSTEM_COMPILED_PATH=$out/lib/guile/2.0/ccache \
245 $out/bin/guile -c ~s $out ~a
248 $out/bin/guile --version~%"
250 (format #f "~s" make-guile-wrapper)
252 (list mkdir xz guile tar bash))))
253 (derivation store name
256 #:inputs `((,bash) (,builder)))))
258 (define* (make-raw-bag name
259 #:key source inputs native-inputs outputs
264 (build-inputs inputs)
267 (define %bootstrap-guile
268 ;; The Guile used to run the build scripts of the initial derivations.
269 ;; It is just unpacked from a tarball containing a pre-built binary.
270 ;; This is typically built using %GUILE-BOOTSTRAP-TARBALL below.
272 ;; XXX: Would need libc's `libnss_files2.so' for proper `getaddrinfo'
273 ;; support (for /etc/services).
274 (let ((raw (build-system
276 (description "Raw build system with direct store access")
277 (lower make-raw-bag))))
279 (name "guile-bootstrap")
283 (synopsis "Bootstrap Guile")
284 (description "Pre-built Guile for bootstrapping purposes.")
288 (define %bootstrap-base-urls
289 ;; This is where the initial binaries come from.
290 '("ftp://alpha.gnu.org/gnu/guix/bootstrap"
291 "http://alpha.gnu.org/gnu/guix/bootstrap"
292 "http://www.fdn.fr/~lcourtes/software/guix/packages"))
294 (define %bootstrap-coreutils&co
295 (package-from-tarball "bootstrap-binaries"
299 (uri (map (cut string-append <> "/" system
302 "/20150101/static-binaries.tar.xz")
304 "/20131110/static-binaries.tar.xz")))
305 %bootstrap-base-urls))
310 "0c533p9dhczzcsa1117gmfq3pc8w362g4mx84ik36srpr7cx2bg4"))
313 "0s5b3jb315n13m1k8095l0a5hfrsz8g0fv1b6riyc5hnxqyphlak"))
316 "0gf0fn2kbpxkjixkmx5f4z6hv6qpmgixl69zgg74dbsfdfj8jdv5"))
319 "072y4wyfsj1bs80r6vbybbafy8ya4vfy7qj25dklwk97m6g71753"))))))
320 "fgrep" ; the program to test
321 "Bootstrap binaries of Coreutils, Awk, etc."
323 '(let ((path (list (string-append (getcwd) "/bin"))))
325 (patch-shebang "bin/egrep" path)
326 (patch-shebang "bin/fgrep" path)
330 (define %bootstrap-binutils
331 (package-from-tarball "binutils-bootstrap"
335 (uri (map (cut string-append <> "/" system
338 "/20150101/binutils-2.25.tar.xz")
340 "/20131110/binutils-2.23.2.tar.xz")))
341 %bootstrap-base-urls))
346 "1j5yivz7zkjqfsfmxzrrrffwyayjqyfxgpi89df0w4qziqs2dg20"))
349 "14jgwf9gscd7l2pnz610b1zia06dvcm2qyzvni31b8zpgmcai2v9"))
352 "1v7dj6bzn6m36f20gw31l99xaabq4xrhrx3gwqkhhig0mdlmr69q"))
355 "1x8kkhcxmfyzg1ddpz2pxs6fbdl6412r7x0nzbmi5n7mj8zw2gy7"))))))
356 "ld" ; the program to test
357 "Bootstrap binaries of the GNU Binutils"))
359 (define %bootstrap-glibc
362 (name "glibc-bootstrap")
365 (build-system trivial-build-system)
367 `(#:guile ,%bootstrap-guile
368 #:modules ((guix build utils))
370 (let ((out (assoc-ref %outputs "out"))
371 (tar (assoc-ref %build-inputs "tar"))
372 (xz (assoc-ref %build-inputs "xz"))
373 (tarball (assoc-ref %build-inputs "tarball")))
374 (use-modules (guix build utils))
377 (copy-file tarball "binaries.tar.xz")
378 (system* xz "-d" "binaries.tar.xz")
379 (let ((builddir (getcwd)))
380 (with-directory-excursion out
382 (string-append builddir
386 ;; Patch libc.so so it refers to the right path.
387 (substitute* "lib/libc.so"
388 (("/[^ ]+/lib/(libc|ld)" _ prefix)
389 (string-append out "/lib/" prefix))))))))
391 `(("tar" ,(search-bootstrap-binary "tar" (%current-system)))
392 ("xz" ,(search-bootstrap-binary "xz" (%current-system)))
393 ("tarball" ,(bootstrap-origin
396 (uri (map (cut string-append <> "/" (%current-system)
397 (match (%current-system)
399 "/20150101/glibc-2.20.tar.xz")
401 "/20131110/glibc-2.18.tar.xz")))
402 %bootstrap-base-urls))
404 (match (%current-system)
407 "0jlqrgavvnplj1b083s20jj9iddr4lzfvwybw5xrcis9spbfzk7v"))
410 "1hgrccw1zqdc7lvgivwa54d9l3zsim5pqm0dykxg0z522h6gr05w"))
413 "18cmgvpllqfpn6khsmivqib7ys8ymnq0hdzi3qp24prik0ykz8gn"))
416 "0k97a3whzx3apsi9n2cbsrr79ad6lh00klxph9hw4fqyp1abkdsg")))))))))
417 (synopsis "Bootstrap binaries and headers of the GNU C Library")
418 (description synopsis)
422 (define %bootstrap-gcc
423 ;; The initial GCC. Uses binaries from a tarball typically built by
424 ;; %GCC-BOOTSTRAP-TARBALL.
426 (name "gcc-bootstrap")
429 (build-system trivial-build-system)
431 `(#:guile ,%bootstrap-guile
432 #:modules ((guix build utils))
434 (let ((out (assoc-ref %outputs "out"))
435 (tar (assoc-ref %build-inputs "tar"))
436 (xz (assoc-ref %build-inputs "xz"))
437 (bash (assoc-ref %build-inputs "bash"))
438 (libc (assoc-ref %build-inputs "libc"))
439 (tarball (assoc-ref %build-inputs "tarball")))
440 (use-modules (guix build utils)
444 (copy-file tarball "binaries.tar.xz")
445 (system* xz "-d" "binaries.tar.xz")
446 (let ((builddir (getcwd))
447 (bindir (string-append out "/bin")))
448 (with-directory-excursion out
450 (string-append builddir "/binaries.tar")))
452 (with-directory-excursion bindir
454 (rename-file "gcc" ".gcc-wrapped")
455 (call-with-output-file "gcc"
458 exec ~a/bin/.gcc-wrapped -B~a/lib \
459 -Wl,-rpath -Wl,~a/lib \
460 -Wl,-dynamic-linker -Wl,~a/~a \"$@\"~%"
463 ,(glibc-dynamic-linker))))
465 (chmod "gcc" #o555))))))
467 `(("tar" ,(search-bootstrap-binary "tar" (%current-system)))
468 ("xz" ,(search-bootstrap-binary "xz" (%current-system)))
469 ("bash" ,(search-bootstrap-binary "bash" (%current-system)))
470 ("libc" ,%bootstrap-glibc)
471 ("tarball" ,(bootstrap-origin
474 (uri (map (cut string-append <> "/" (%current-system)
475 (match (%current-system)
477 "/20150101/gcc-4.8.4.tar.xz")
479 "/20131110/gcc-4.8.2.tar.xz")))
480 %bootstrap-base-urls))
482 (match (%current-system)
485 "17ga4m6195n4fnbzdkmik834znkhs53nkypp6557pl1ps7dgqbls"))
488 "150c1arrf2k8vfy6dpxh59vcgs4p1bgiz2av5m19dynpks7rjnyw"))
491 "0ghz825yzp43fxw53kd6afm8nkz16f7dxi9xi40bfwc8x3nbbr8v"))
494 "1m5miqkyng45l745n0sfafdpjkqv9225xf44jqkygwsipj2cv9ks")))))))))
496 (list (search-path-specification
498 (files '("include")))
499 (search-path-specification
500 (variable "LIBRARY_PATH")
501 (files '("lib" "lib64")))))
502 (synopsis "Bootstrap binaries of the GNU Compiler Collection")
503 (description synopsis)
507 (define %bootstrap-inputs
508 ;; The initial, pre-built inputs. From now on, we can start building our
510 `(("libc" ,%bootstrap-glibc)
511 ("gcc" ,%bootstrap-gcc)
512 ("binutils" ,%bootstrap-binutils)
513 ("coreutils&co" ,%bootstrap-coreutils&co)
515 ;; In gnu-build-system.scm, we rely on the availability of Bash.
516 ("bash" ,%bootstrap-coreutils&co)))
518 ;;; bootstrap.scm ends here