make-bootstrap.scm 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020, 2021 Ludovic Courtès <ludo@gnu.org>
  3. ;;; Copyright © 2017, 2021 Efraim Flashner <efraim@flashner.co.il>
  4. ;;; Copyright © 2018 Tobias Geerinckx-Rice <me@tobias.gr>
  5. ;;; Copyright © 2018, 2019 Mark H Weaver <mhw@netris.org>
  6. ;;; Copyright © 2018, 2019, 2021 Jan (janneke) Nieuwenhuizen <janneke@gnu.org>
  7. ;;; Copyright © 2019, 2020 Marius Bakke <mbakke@fastmail.com>
  8. ;;; Copyright © 2020 Mathieu Othacehe <m.othacehe@gmail.com>
  9. ;;;
  10. ;;; This file is part of GNU Guix.
  11. ;;;
  12. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  13. ;;; under the terms of the GNU General Public License as published by
  14. ;;; the Free Software Foundation; either version 3 of the License, or (at
  15. ;;; your option) any later version.
  16. ;;;
  17. ;;; GNU Guix is distributed in the hope that it will be useful, but
  18. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  19. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. ;;; GNU General Public License for more details.
  21. ;;;
  22. ;;; You should have received a copy of the GNU General Public License
  23. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  24. (define-module (gnu packages make-bootstrap)
  25. #:use-module (guix gexp)
  26. #:use-module (guix utils)
  27. #:use-module (guix packages)
  28. #:use-module (guix memoization)
  29. #:use-module ((guix licenses) #:select (gpl3+))
  30. #:use-module (guix build-system trivial)
  31. #:use-module (guix build-system gnu)
  32. #:use-module ((gnu packages) #:select (search-patch search-patches))
  33. #:use-module (gnu packages base)
  34. #:use-module (gnu packages cross-base)
  35. #:use-module (gnu packages bash)
  36. #:use-module (gnu packages compression)
  37. #:use-module (gnu packages gawk)
  38. #:use-module (gnu packages gcc)
  39. #:use-module (gnu packages guile)
  40. #:use-module (gnu packages bdw-gc)
  41. #:use-module (gnu packages libunistring)
  42. #:use-module (gnu packages linux)
  43. #:use-module (gnu packages hurd)
  44. #:use-module (gnu packages mes)
  45. #:use-module (gnu packages multiprecision)
  46. #:use-module (ice-9 match)
  47. #:use-module (srfi srfi-1)
  48. #:export (%bootstrap-binaries-tarball
  49. %linux-libre-headers-bootstrap-tarball
  50. %binutils-bootstrap-tarball
  51. %glibc-bootstrap-tarball
  52. %gcc-bootstrap-tarball
  53. %guile-bootstrap-tarball
  54. %mescc-tools-bootstrap-tarball
  55. %mes-bootstrap-tarball
  56. %bootstrap-tarballs
  57. %guile-static-stripped))
  58. ;;; Commentary:
  59. ;;;
  60. ;;; This module provides tools to build tarballs of the "bootstrap binaries"
  61. ;;; used in (gnu packages bootstrap). These statically-linked binaries are
  62. ;;; taken for granted and used as the root of the whole bootstrap procedure.
  63. ;;;
  64. ;;; Code:
  65. (define glibc-for-bootstrap
  66. (mlambdaq (base)
  67. "Return a libc deriving from BASE whose `system' and `popen' functions looks
  68. for `sh' in $PATH, and without nscd, and with static NSS modules."
  69. (package
  70. (inherit base)
  71. (source (origin (inherit (package-source base))
  72. (patches (append (search-patches
  73. "glibc-bootstrap-system.patch"
  74. "glibc-static-nss.patch")
  75. (origin-patches (package-source base))))))
  76. (arguments
  77. (substitute-keyword-arguments (package-arguments base)
  78. ((#:configure-flags flags)
  79. ;; Arrange so that getaddrinfo & co. do not contact the nscd,
  80. ;; and can use statically-linked NSS modules.
  81. `(cons* "--disable-nscd" "--disable-build-nscd"
  82. "--enable-static-nss"
  83. ,flags))))
  84. ;; Remove the 'debug' output to allow bit-reproducible builds (when the
  85. ;; 'debug' output is used, ELF files end up with a .gnu_debuglink, which
  86. ;; includes a CRC of the corresponding debugging symbols; those symbols
  87. ;; contain store file names, so the CRC changes at every rebuild.)
  88. (outputs (delete "debug" (package-outputs base))))))
  89. (define gcc-for-bootstrap
  90. (mlambdaq (glibc)
  91. "Return a variant of GCC that uses the bootstrap variant of GLIBC."
  92. (package
  93. (inherit gcc-7)
  94. (outputs '("out")) ;all in one so libgcc_s is easily found
  95. (inputs
  96. `( ;; Distinguish the name so we can refer to it below.
  97. ("bootstrap-libc" ,(glibc-for-bootstrap glibc))
  98. ("libc:static" ,(glibc-for-bootstrap glibc) "static")
  99. ,@(package-inputs gcc-7))))))
  100. (define (package-with-relocatable-glibc p)
  101. "Return a variant of P that uses the libc as defined by
  102. `glibc-for-bootstrap'."
  103. (define (cross-bootstrap-libc target)
  104. (glibc-for-bootstrap
  105. ;; `cross-libc' already returns a cross libc, so clear
  106. ;; %CURRENT-TARGET-SYSTEM.
  107. (parameterize ((%current-target-system #f))
  108. (cross-libc target))))
  109. ;; Standard inputs with the above libc and corresponding GCC.
  110. (define (inputs)
  111. (if (%current-target-system) ; is this package cross built?
  112. `(("cross-libc"
  113. ,(cross-bootstrap-libc (%current-target-system)))
  114. ("cross-libc:static"
  115. ,(cross-bootstrap-libc (%current-target-system))
  116. "static"))
  117. '()))
  118. (define (native-inputs)
  119. (if (%current-target-system)
  120. (let* ((target (%current-target-system))
  121. (xgcc (cross-gcc
  122. target
  123. #:xbinutils (cross-binutils target)
  124. #:libc (cross-bootstrap-libc target))))
  125. `(("cross-gcc" ,(package
  126. (inherit xgcc)
  127. (search-paths
  128. ;; Ensure the cross libc headers appears on the
  129. ;; C++ system header search path.
  130. (cons (search-path-specification
  131. (variable "CROSS_CPLUS_INCLUDE_PATH")
  132. (files '("include")))
  133. (package-search-paths gcc-7)))))
  134. ("cross-binutils" ,(cross-binutils target))
  135. ,@(%final-inputs)))
  136. `(("libc" ,(glibc-for-bootstrap glibc))
  137. ("libc:static" ,(glibc-for-bootstrap glibc) "static")
  138. ("gcc" ,(gcc-for-bootstrap glibc))
  139. ,@(fold alist-delete (%final-inputs) '("libc" "gcc")))))
  140. (package-with-explicit-inputs p inputs
  141. (current-source-location)
  142. #:native-inputs native-inputs))
  143. (define %static-inputs
  144. ;; Packages that are to be used as %BOOTSTRAP-INPUTS.
  145. (let ((coreutils (package (inherit coreutils)
  146. (arguments
  147. `(#:configure-flags
  148. '("--disable-nls"
  149. "--disable-silent-rules"
  150. "--enable-no-install-program=stdbuf,libstdbuf.so"
  151. "CFLAGS=-Os -g0" ; smaller, please
  152. "LDFLAGS=-static -pthread"
  153. ;; Work around a cross-compilation bug whereby libcoreutils.a
  154. ;; would provide '__mktime_internal', which conflicts with the
  155. ;; one in libc.a.
  156. ,@(if (%current-target-system)
  157. `("gl_cv_func_working_mktime=yes")
  158. '()))
  159. #:tests? #f ; signal-related Gnulib tests fail
  160. ,@(package-arguments coreutils)))
  161. ;; Remove optional dependencies such as GMP. Keep Perl
  162. ;; except if it's missing (which is the case when
  163. ;; cross-compiling).
  164. (inputs (match (assoc "perl" (package-inputs coreutils))
  165. (#f '())
  166. (x (list x))))
  167. ;; Remove the 'debug' output (see above for the reason.)
  168. (outputs '("out"))))
  169. (bzip2 (package (inherit bzip2)
  170. (arguments
  171. (substitute-keyword-arguments (package-arguments bzip2)
  172. ((#:phases phases)
  173. `(modify-phases ,phases
  174. (add-before 'build 'dash-static
  175. (lambda _
  176. (substitute* "Makefile"
  177. (("^LDFLAGS[[:blank:]]*=.*$")
  178. "LDFLAGS = -static"))
  179. #t))))))))
  180. (xz (package (inherit xz)
  181. (outputs '("out"))
  182. (arguments
  183. `(#:strip-flags '("--strip-all")
  184. #:phases (modify-phases %standard-phases
  185. (add-before 'configure 'static-executable
  186. (lambda _
  187. ;; Ask Libtool for a static executable.
  188. (substitute* "src/xz/Makefile.in"
  189. (("^xz_LDADD =")
  190. "xz_LDADD = -all-static"))
  191. #t)))))))
  192. (gawk (package (inherit gawk)
  193. (source (origin (inherit (package-source gawk))
  194. (patches (cons (search-patch "gawk-shell.patch")
  195. (origin-patches
  196. (package-source gawk))))))
  197. (arguments
  198. `(;; Starting from gawk 4.1.0, some of the tests for the
  199. ;; plug-in mechanism just fail on static builds:
  200. ;;
  201. ;; ./fts.awk:1: error: can't open shared library `filefuncs' for reading (No such file or directory)
  202. #:tests? #f
  203. ,@(substitute-keyword-arguments (package-arguments gawk)
  204. ((#:phases phases)
  205. `(modify-phases ,phases
  206. (add-before 'configure 'no-export-dynamic
  207. (lambda _
  208. ;; Since we use `-static', remove
  209. ;; `-export-dynamic'.
  210. (substitute* "configure"
  211. (("-Wl,-export-dynamic") ""))
  212. #t)))))))
  213. (inputs (if (%current-target-system)
  214. `(("bash" ,static-bash))
  215. '()))))
  216. (tar (package (inherit tar)
  217. (arguments
  218. `(;; Work around a cross-compilation bug whereby libgnu.a would provide
  219. ;; '__mktime_internal', which conflicts with the one in libc.a.
  220. ,@(if (%current-target-system)
  221. `(#:configure-flags '("gl_cv_func_working_mktime=yes"))
  222. '())
  223. ,@(substitute-keyword-arguments (package-arguments tar)
  224. ((#:phases phases)
  225. `(modify-phases ,phases
  226. (replace 'set-shell-file-name
  227. (lambda _
  228. ;; Do not use "/bin/sh" to run programs; see
  229. ;; <http://lists.gnu.org/archive/html/guix-devel/2016-09/msg02272.html>.
  230. (substitute* "src/system.c"
  231. (("/bin/sh") "sh")
  232. (("execv ") "execvp "))
  233. #t)))))))))
  234. ;; We don't want to retain a reference to /gnu/store in the bootstrap
  235. ;; versions of egrep/fgrep, so we remove the custom phase added since
  236. ;; grep@2.25. The effect is 'egrep' and 'fgrep' look for 'grep' in
  237. ;; $PATH.
  238. (grep (package
  239. (inherit grep)
  240. (inputs '()) ;remove PCRE, which is optional
  241. (arguments
  242. (substitute-keyword-arguments (package-arguments grep)
  243. ((#:phases phases)
  244. `(modify-phases ,phases
  245. (delete 'fix-egrep-and-fgrep)))))))
  246. (finalize (compose static-package
  247. package-with-relocatable-glibc)))
  248. `(,@(map (match-lambda
  249. ((name package)
  250. (list name (finalize package))))
  251. `(("tar" ,tar)
  252. ("gzip" ,gzip)
  253. ("bzip2" ,bzip2)
  254. ("xz" ,xz)
  255. ("patch" ,patch)
  256. ("coreutils" ,coreutils)
  257. ("sed" ,sed)
  258. ("grep" ,grep)
  259. ("gawk" ,gawk)))
  260. ("bash" ,static-bash))))
  261. (define %static-binaries
  262. (package
  263. (name "static-binaries")
  264. (version "0")
  265. (build-system trivial-build-system)
  266. (source #f)
  267. (inputs %static-inputs)
  268. (arguments
  269. `(#:modules ((guix build utils))
  270. #:builder
  271. (begin
  272. (use-modules (ice-9 ftw)
  273. (ice-9 match)
  274. (srfi srfi-1)
  275. (srfi srfi-26)
  276. (guix build utils))
  277. (let ()
  278. (define (directory-contents dir)
  279. (map (cut string-append dir "/" <>)
  280. (scandir dir (negate (cut member <> '("." ".."))))))
  281. (define (copy-directory source destination)
  282. (for-each (lambda (file)
  283. (format #t "copying ~s...~%" file)
  284. (copy-file file
  285. (string-append destination "/"
  286. (basename file))))
  287. (directory-contents source)))
  288. (let* ((out (assoc-ref %outputs "out"))
  289. (bin (string-append out "/bin")))
  290. (mkdir-p bin)
  291. ;; Copy Coreutils binaries.
  292. (let* ((coreutils (assoc-ref %build-inputs "coreutils"))
  293. (source (string-append coreutils "/bin")))
  294. (copy-directory source bin))
  295. ;; For the other inputs, copy just one binary, which has the
  296. ;; same name as the input.
  297. (for-each (match-lambda
  298. ((name . dir)
  299. (let ((source (string-append dir "/bin/" name)))
  300. (format #t "copying ~s...~%" source)
  301. (copy-file source
  302. (string-append bin "/" name)))))
  303. (alist-delete "coreutils" %build-inputs))
  304. ;; But of course, there are exceptions to this rule.
  305. (let ((grep (assoc-ref %build-inputs "grep")))
  306. (install-file (string-append grep "/bin/fgrep") bin)
  307. (install-file (string-append grep "/bin/egrep") bin))
  308. ;; Clear references to the store path.
  309. (for-each remove-store-references
  310. (directory-contents bin))
  311. (with-directory-excursion bin
  312. ;; Programs such as Perl's build system want these aliases.
  313. (symlink "bash" "sh")
  314. (symlink "gawk" "awk"))
  315. #t)))))
  316. (synopsis "Statically-linked bootstrap binaries")
  317. (description
  318. "Binaries used to bootstrap the distribution.")
  319. (license gpl3+)
  320. (home-page #f)))
  321. (define %linux-libre-headers-stripped
  322. ;; The subset of Linux-Libre-Headers that we need.
  323. (package (inherit linux-libre-headers)
  324. (name (string-append (package-name linux-libre-headers) "-stripped"))
  325. (build-system trivial-build-system)
  326. (outputs '("out"))
  327. (arguments
  328. `(#:modules ((guix build utils)
  329. (guix build make-bootstrap))
  330. #:builder
  331. (begin
  332. (use-modules (guix build utils)
  333. (guix build make-bootstrap))
  334. (let* ((in (assoc-ref %build-inputs "linux-libre-headers"))
  335. (out (assoc-ref %outputs "out")))
  336. (copy-linux-headers out in)
  337. #t))))
  338. (inputs `(("linux-libre-headers" ,linux-libre-headers)))))
  339. (define %binutils-static
  340. ;; Statically-linked Binutils.
  341. (package (inherit binutils)
  342. (name "binutils-static")
  343. (arguments
  344. `(#:configure-flags (cons "--disable-gold"
  345. ,(match (memq #:configure-flags
  346. (package-arguments binutils))
  347. ((#:configure-flags flags _ ...)
  348. flags)))
  349. #:strip-flags '("--strip-all")
  350. #:phases (modify-phases %standard-phases
  351. (add-before 'configure 'all-static
  352. (lambda _
  353. ;; The `-all-static' libtool flag can only be passed
  354. ;; after `configure', since configure tests don't use
  355. ;; libtool, and only for executables built with libtool.
  356. (substitute* '("binutils/Makefile.in"
  357. "gas/Makefile.in"
  358. "ld/Makefile.in")
  359. (("^LDFLAGS =(.*)$" line)
  360. (string-append line
  361. "\nAM_LDFLAGS = -static -all-static\n")))
  362. #t)))))))
  363. (define %binutils-static-stripped
  364. ;; The subset of Binutils that we need.
  365. (package (inherit %binutils-static)
  366. (name (string-append (package-name %binutils-static) "-stripped"))
  367. (build-system trivial-build-system)
  368. (outputs '("out"))
  369. (arguments
  370. `(#:modules ((guix build utils))
  371. #:builder
  372. (begin
  373. (use-modules (guix build utils))
  374. (setvbuf (current-output-port)
  375. (cond-expand (guile-2.0 _IOLBF) (else 'line)))
  376. (let* ((in (assoc-ref %build-inputs "binutils"))
  377. (out (assoc-ref %outputs "out"))
  378. (bin (string-append out "/bin")))
  379. (mkdir-p bin)
  380. (for-each (lambda (file)
  381. (let ((target (string-append bin "/" file)))
  382. (format #t "copying `~a'...~%" file)
  383. (copy-file (string-append in "/bin/" file)
  384. target)
  385. (remove-store-references target)))
  386. '("ar" "as" "ld" "nm" "objcopy" "objdump"
  387. "ranlib" "readelf" "size" "strings" "strip"))
  388. #t))))
  389. (inputs `(("binutils" ,%binutils-static)))))
  390. (define (%glibc-stripped)
  391. ;; GNU libc's essential shared libraries, dynamic linker, and headers,
  392. ;; with all references to store directories stripped. As a result,
  393. ;; libc.so is unusable and need to be patched for proper relocation.
  394. (let ((glibc (glibc-for-bootstrap glibc)))
  395. (package (inherit glibc)
  396. (name "glibc-stripped")
  397. (build-system trivial-build-system)
  398. (arguments
  399. `(#:modules ((guix build utils)
  400. (guix build make-bootstrap))
  401. #:builder
  402. (begin
  403. (use-modules (guix build make-bootstrap))
  404. (make-stripped-libc (assoc-ref %outputs "out")
  405. (assoc-ref %build-inputs "libc")
  406. (assoc-ref %build-inputs "kernel-headers")))))
  407. (inputs `(("kernel-headers"
  408. ,(if (or (and (%current-target-system)
  409. (target-hurd? (%current-target-system)))
  410. (string-suffix? "-hurd" (%current-system)))
  411. gnumach-headers
  412. linux-libre-headers))
  413. ("libc" ,(let ((target (%current-target-system)))
  414. (if target
  415. (glibc-for-bootstrap
  416. (parameterize ((%current-target-system #f))
  417. (cross-libc target)))
  418. glibc)))))
  419. (native-inputs '())
  420. (propagated-inputs '())
  421. ;; Only one output.
  422. (outputs '("out")))))
  423. (define %gcc-static
  424. ;; A statically-linked GCC, with stripped-down functionality.
  425. (package-with-relocatable-glibc
  426. (package (inherit gcc-7)
  427. (name "gcc-static")
  428. (outputs '("out")) ; all in one
  429. (arguments
  430. (substitute-keyword-arguments (package-arguments gcc-7)
  431. ((#:modules modules %gnu-build-system-modules)
  432. `((srfi srfi-1)
  433. (srfi srfi-26)
  434. (ice-9 regex)
  435. ,@modules))
  436. ((#:guile _) #f)
  437. ((#:implicit-inputs? _) #t)
  438. ((#:configure-flags flags)
  439. `(append (list
  440. ;; We don't need a full bootstrap here.
  441. "--disable-bootstrap"
  442. ;; Make sure '-static' is passed where it matters.
  443. "--with-stage1-ldflags=-static"
  444. ;; GCC 4.8+ requires a C++ compiler and library.
  445. "--enable-languages=c,c++"
  446. ;; Make sure gcc-nm doesn't require liblto_plugin.so.
  447. "--disable-lto"
  448. "--disable-shared"
  449. "--disable-plugin"
  450. "--disable-libmudflap"
  451. "--disable-libatomic"
  452. "--disable-libsanitizer"
  453. "--disable-libitm"
  454. "--disable-libgomp"
  455. "--disable-libcilkrts"
  456. "--disable-libvtv"
  457. "--disable-libssp"
  458. "--disable-libquadmath")
  459. (remove (cut string-match "--(.*plugin|enable-languages)" <>)
  460. ,flags)))
  461. ((#:phases phases)
  462. `(modify-phases ,phases
  463. (add-after 'pre-configure 'remove-lgcc_s
  464. (lambda _
  465. ;; Remove the '-lgcc_s' added to GNU_USER_TARGET_LIB_SPEC in
  466. ;; the 'pre-configure phase of our main gcc package, because
  467. ;; that shared library is not present in this static gcc. See
  468. ;; <https://lists.gnu.org/archive/html/guix-devel/2015-01/msg00008.html>.
  469. (substitute* (cons "gcc/config/rs6000/sysv4.h"
  470. (find-files "gcc/config"
  471. "^gnu-user.*\\.h$"))
  472. ((" -lgcc_s}}") "}}"))
  473. #t))))))
  474. (inputs
  475. `(("zlib:static" ,zlib "static")
  476. ("isl:static" ,isl "static")
  477. ,@(package-inputs gcc-7)))
  478. (native-inputs
  479. (if (%current-target-system)
  480. `(;; When doing a Canadian cross, we need GMP/MPFR/MPC both
  481. ;; as target inputs and as native inputs; the latter is
  482. ;; needed when building build-time tools ('genconstants',
  483. ;; etc.) Failing to do that leads to misdetections of
  484. ;; declarations by 'gcc/configure', and eventually to
  485. ;; duplicate declarations as reported in
  486. ;; <https://gcc.gnu.org/bugzilla/show_bug.cgi?id=59217>.
  487. ("gmp-native" ,gmp)
  488. ("mpfr-native" ,mpfr)
  489. ("mpc-native" ,mpc)
  490. ,@(package-native-inputs gcc-7))
  491. (package-native-inputs gcc-7))))))
  492. (define %gcc-stripped
  493. ;; The subset of GCC files needed for bootstrap.
  494. (package (inherit gcc-7)
  495. (name "gcc-stripped")
  496. (build-system trivial-build-system)
  497. (source #f)
  498. (outputs '("out")) ;only one output
  499. (arguments
  500. `(#:modules ((guix build utils))
  501. #:builder
  502. (begin
  503. (use-modules (srfi srfi-1)
  504. (srfi srfi-26)
  505. (guix build utils))
  506. (setvbuf (current-output-port)
  507. (cond-expand (guile-2.0 _IOLBF) (else 'line)))
  508. (let* ((out (assoc-ref %outputs "out"))
  509. (bindir (string-append out "/bin"))
  510. (libdir (string-append out "/lib"))
  511. (includedir (string-append out "/include"))
  512. (libexecdir (string-append out "/libexec"))
  513. (gcc (assoc-ref %build-inputs "gcc")))
  514. (copy-recursively (string-append gcc "/bin") bindir)
  515. (for-each remove-store-references
  516. (find-files bindir ".*"))
  517. (copy-recursively (string-append gcc "/lib") libdir)
  518. (for-each remove-store-references
  519. (remove (cut string-suffix? ".h" <>)
  520. (find-files libdir ".*")))
  521. (copy-recursively (string-append gcc "/libexec")
  522. libexecdir)
  523. (for-each remove-store-references
  524. (find-files libexecdir ".*"))
  525. ;; Starting from GCC 4.8, helper programs built natively
  526. ;; (‘genchecksum’, ‘gcc-nm’, etc.) rely on C++ headers.
  527. (copy-recursively (string-append gcc "/include/c++")
  528. (string-append includedir "/c++"))
  529. ;; For native builds, check whether the binaries actually work.
  530. ,@(if (%current-target-system)
  531. '()
  532. '((for-each (lambda (prog)
  533. (invoke (string-append gcc "/bin/" prog)
  534. "--version"))
  535. '("gcc" "g++" "cpp"))))
  536. #t))))
  537. (inputs `(("gcc" ,%gcc-static)))))
  538. ;; Two packages: first build static, bare minimum content.
  539. (define %mescc-tools-static
  540. ;; A statically linked MesCC Tools.
  541. (package
  542. (inherit mescc-tools)
  543. (name "mescc-tools-static")
  544. (arguments
  545. `(#:system "i686-linux"
  546. ,@(substitute-keyword-arguments (package-arguments mescc-tools)
  547. ((#:make-flags flags)
  548. `(cons "CC=gcc -static" ,flags)))))))
  549. ;; ... next remove store references.
  550. (define %mescc-tools-static-stripped
  551. ;; A statically linked Mescc Tools with store references removed, for
  552. ;; bootstrap.
  553. (package
  554. (inherit %mescc-tools-static)
  555. (name (string-append (package-name %mescc-tools-static) "-stripped"))
  556. (build-system trivial-build-system)
  557. (arguments
  558. `(#:modules ((guix build utils))
  559. #:builder
  560. (begin
  561. (use-modules (guix build utils))
  562. (let* ((in (assoc-ref %build-inputs "mescc-tools"))
  563. (out (assoc-ref %outputs "out"))
  564. (bin (string-append out "/bin")))
  565. (mkdir-p bin)
  566. (for-each (lambda (file)
  567. (let ((target (string-append bin "/" file)))
  568. (format #t "copying `~a'...~%" file)
  569. (copy-file (string-append in "/bin/" file)
  570. target)
  571. (remove-store-references target)))
  572. '( "M1" "blood-elf" "hex2"))
  573. #t))))
  574. (inputs `(("mescc-tools" ,%mescc-tools-static)))))
  575. ;; Two packages: first build static, bare minimum content.
  576. (define-public %mes-minimal
  577. ;; A minimal Mes without documentation.
  578. (package
  579. (inherit mes)
  580. (name "mes-minimal")
  581. (native-inputs
  582. `(("guile" ,guile-3.0)))
  583. (arguments
  584. `(#:system "i686-linux"
  585. #:strip-binaries? #f
  586. #:configure-flags '("--mes")
  587. #:phases
  588. (modify-phases %standard-phases
  589. (delete 'patch-shebangs)
  590. (add-after 'install 'strip-install
  591. (lambda _
  592. (let* ((out (assoc-ref %outputs "out"))
  593. (share (string-append out "/share")))
  594. (delete-file-recursively (string-append out "/lib/guile"))
  595. (delete-file-recursively (string-append share "/guile"))
  596. (for-each delete-file
  597. (find-files
  598. (string-append share "/mes/lib")
  599. "\\.(h|c)"))))))))))
  600. ;; next remove store references.
  601. (define %mes-minimal-stripped
  602. ;; A minimal Mes with store references removed, for bootstrap.
  603. (package
  604. (inherit %mes-minimal)
  605. (name (string-append (package-name %mes-minimal) "-stripped"))
  606. (build-system trivial-build-system)
  607. (arguments
  608. `(#:modules ((guix build utils))
  609. #:allowed-references ()
  610. #:builder
  611. (begin
  612. (use-modules (guix build utils))
  613. (let ((in (assoc-ref %build-inputs "mes"))
  614. (out (assoc-ref %outputs "out")))
  615. (copy-recursively in out)
  616. (for-each (lambda (dir)
  617. (for-each remove-store-references
  618. (find-files (string-append out "/" dir)
  619. ".*")))
  620. '("bin" "share/mes"))
  621. #t))))
  622. (inputs `(("mes" ,%mes-minimal)))))
  623. (define* (make-guile-static guile patches)
  624. (package-with-relocatable-glibc
  625. (static-package
  626. (package
  627. (inherit guile)
  628. (source
  629. (origin (inherit (package-source guile))
  630. (patches (append (map search-patch patches)
  631. (origin-patches (package-source guile))))))
  632. (name (string-append (package-name guile) "-static"))
  633. (synopsis "Statically-linked and relocatable Guile")
  634. ;; Remove the 'debug' output (see above for the reason.)
  635. (outputs (delete "debug" (package-outputs guile)))
  636. (inputs
  637. `(("libunistring:static" ,libunistring "static")
  638. ,@(package-inputs guile)))
  639. (propagated-inputs
  640. `(("bdw-gc" ,libgc/static-libs)
  641. ,@(alist-delete "bdw-gc"
  642. (package-propagated-inputs guile))))
  643. (arguments
  644. (substitute-keyword-arguments (package-arguments guile)
  645. ((#:configure-flags flags '())
  646. ;; When `configure' checks for ltdl availability, it
  647. ;; doesn't try to link using libtool, and thus fails
  648. ;; because of a missing -ldl. Work around that.
  649. ''("LDFLAGS=-ldl"
  650. "--enable-mini-gmp"))
  651. ((#:phases phases '%standard-phases)
  652. `(modify-phases ,phases
  653. ;; Do not record the absolute file name of 'sh' in
  654. ;; (ice-9 popen). This makes 'open-pipe' unusable in
  655. ;; a build chroot ('open-pipe*' is fine) but avoids
  656. ;; keeping a reference to Bash.
  657. (delete 'pre-configure)
  658. (add-before 'configure 'static-guile
  659. (lambda _
  660. (substitute* "libguile/Makefile.in"
  661. ;; Create a statically-linked `guile'
  662. ;; executable.
  663. (("^guile_LDFLAGS =")
  664. "guile_LDFLAGS = -all-static")
  665. ;; Add `-ldl' *after* libguile-2.0.la.
  666. (("^guile_LDADD =(.*)$" _ ldadd)
  667. (string-append "guile_LDADD = "
  668. (string-trim-right ldadd)
  669. " -ldl\n")))))))
  670. ((#:tests? _ #f)
  671. ;; There are uses of `dynamic-link' in
  672. ;; {foreign,coverage}.test that don't fly here.
  673. #f)
  674. ((#:parallel-build? _ #f)
  675. ;; Work around the fact that the Guile build system is
  676. ;; not deterministic when parallel-build is enabled.
  677. #f)))))))
  678. (define %guile-static
  679. ;; A statically-linked Guile that is relocatable--i.e., it can search
  680. ;; .scm and .go files relative to its installation directory, rather
  681. ;; than in hard-coded configure-time paths.
  682. (make-guile-static guile-2.0 '("guile-relocatable.patch"
  683. "guile-default-utf8.patch"
  684. "guile-linux-syscalls.patch")))
  685. (define* (make-guile-static-stripped static-guile)
  686. (package
  687. (inherit static-guile)
  688. (name (string-append (package-name static-guile) "-stripped"))
  689. (build-system trivial-build-system)
  690. (arguments
  691. ;; The end result should depend on nothing but itself.
  692. `(#:allowed-references ("out")
  693. #:modules ((guix build utils))
  694. #:builder
  695. (let ((version ,(version-major+minor (package-version static-guile))))
  696. (use-modules (guix build utils))
  697. (let* ((in (assoc-ref %build-inputs "guile"))
  698. (out (assoc-ref %outputs "out"))
  699. (guile1 (string-append in "/bin/guile"))
  700. (guile2 (string-append out "/bin/guile")))
  701. (mkdir-p (string-append out "/share/guile/" version))
  702. (copy-recursively (string-append in "/share/guile/" version)
  703. (string-append out "/share/guile/" version))
  704. (mkdir-p (string-append out "/lib/guile/" version "/ccache"))
  705. (copy-recursively (string-append in "/lib/guile/" version "/ccache")
  706. (string-append out "/lib/guile/" version "/ccache"))
  707. (mkdir (string-append out "/bin"))
  708. (copy-file guile1 guile2)
  709. ;; Verify that the relocated Guile works.
  710. ,@(if (%current-target-system)
  711. '()
  712. '((invoke guile2 "--version")))
  713. ;; Strip store references.
  714. (remove-store-references guile2)
  715. ;; Verify that the stripped Guile works. If it aborts, it could be
  716. ;; that it tries to open iconv descriptors and fails because libc's
  717. ;; iconv data isn't available (see `guile-default-utf8.patch'.)
  718. ,@(if (%current-target-system)
  719. '()
  720. '((invoke guile2 "--version")))
  721. #t))))
  722. (inputs `(("guile" ,static-guile)))
  723. (outputs '("out"))
  724. (synopsis "Minimal statically-linked and relocatable Guile")))
  725. (define %guile-static-stripped
  726. ;; A stripped static Guile 3.0 binary, for use in initrds
  727. ;; and during bootstrap.
  728. (make-guile-static-stripped
  729. (make-guile-static guile-3.0
  730. '("guile-2.2-default-utf8.patch"
  731. "guile-3.0-linux-syscalls.patch"
  732. "guile-3.0-relocatable.patch"))))
  733. (define (tarball-package pkg)
  734. "Return a package containing a tarball of PKG."
  735. (package (inherit pkg)
  736. (name (string-append (package-name pkg) "-tarball"))
  737. (build-system trivial-build-system)
  738. (native-inputs `(("tar" ,tar)
  739. ("xz" ,xz)))
  740. (inputs `(("input" ,pkg)))
  741. (arguments
  742. (let ((name (package-name pkg))
  743. (version (package-version pkg)))
  744. `(#:modules ((guix build utils))
  745. #:builder
  746. (begin
  747. (use-modules (guix build utils))
  748. (let ((out (assoc-ref %outputs "out"))
  749. (input (assoc-ref %build-inputs "input"))
  750. (tar (assoc-ref %build-inputs "tar"))
  751. (xz (assoc-ref %build-inputs "xz")))
  752. (mkdir out)
  753. (set-path-environment-variable "PATH" '("bin") (list tar xz))
  754. (with-directory-excursion input
  755. (invoke "tar" "cJvf"
  756. (string-append out "/"
  757. ,name "-" ,version
  758. "-"
  759. ,(or (%current-target-system)
  760. (%current-system))
  761. ".tar.xz")
  762. "."
  763. ;; avoid non-determinism in the archive
  764. "--sort=name" "--mtime=@0"
  765. "--owner=root:0" "--group=root:0")))))))))
  766. (define %bootstrap-binaries-tarball
  767. ;; A tarball with the statically-linked bootstrap binaries.
  768. (tarball-package %static-binaries))
  769. (define %linux-libre-headers-bootstrap-tarball
  770. ;; A tarball with the statically-linked Linux-Libre-Headers programs.
  771. (tarball-package %linux-libre-headers-stripped))
  772. (define %binutils-bootstrap-tarball
  773. ;; A tarball with the statically-linked Binutils programs.
  774. (tarball-package %binutils-static-stripped))
  775. (define (%glibc-bootstrap-tarball)
  776. ;; A tarball with GNU libc's shared libraries, dynamic linker, and headers.
  777. (tarball-package (%glibc-stripped)))
  778. (define %gcc-bootstrap-tarball
  779. ;; A tarball with a dynamic-linked GCC and its headers.
  780. (tarball-package %gcc-stripped))
  781. (define %guile-bootstrap-tarball
  782. ;; A tarball with the statically-linked, relocatable Guile.
  783. (tarball-package %guile-static-stripped))
  784. (define %mescc-tools-bootstrap-tarball
  785. ;; A tarball with statically-linked MesCC binary seed.
  786. (tarball-package %mescc-tools-static-stripped))
  787. (define %mes-bootstrap-tarball
  788. ;; A tarball with Mes binary seed.
  789. (tarball-package %mes-minimal-stripped))
  790. (define %bootstrap-tarballs
  791. ;; A single derivation containing all the bootstrap tarballs, for
  792. ;; convenience.
  793. (package
  794. (name "bootstrap-tarballs")
  795. (version "0")
  796. (source #f)
  797. (build-system trivial-build-system)
  798. (arguments
  799. (list #:modules '((guix build utils))
  800. #:builder
  801. #~(begin
  802. (use-modules (guix build utils)
  803. (ice-9 match)
  804. (srfi srfi-26))
  805. (define out #$output)
  806. (setvbuf (current-output-port)
  807. (cond-expand (guile-2.0 _IOLBF) (else 'line)))
  808. (mkdir out)
  809. (chdir out)
  810. (for-each (match-lambda
  811. ((name . directory)
  812. (for-each (lambda (file)
  813. (format #t "~a -> ~a~%" file out)
  814. (symlink file (basename file)))
  815. (find-files directory "\\.tar\\."))))
  816. %build-inputs))))
  817. (inputs `(("guile-tarball" ,%guile-bootstrap-tarball)
  818. ,@(match (or (%current-target-system) (%current-system))
  819. ((or "i686-linux" "x86_64-linux")
  820. `(("bootstrap-mescc-tools" ,%mescc-tools-bootstrap-tarball)
  821. ("bootstrap-mes" ,%mes-bootstrap-tarball)
  822. ("bootstrap-linux-libre-headers"
  823. ,%linux-libre-headers-bootstrap-tarball)))
  824. (_ `(("gcc-tarball" ,%gcc-bootstrap-tarball)
  825. ("binutils-tarball" ,%binutils-bootstrap-tarball)
  826. ("glibc-tarball" ,(%glibc-bootstrap-tarball))
  827. ("coreutils&co-tarball" ,%bootstrap-binaries-tarball))))))
  828. (synopsis "Tarballs containing all the bootstrap binaries")
  829. (description synopsis)
  830. (home-page #f)
  831. (license gpl3+)))
  832. ;;; make-bootstrap.scm ends here