make-bootstrap.scm 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909
  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
  193. (inherit gawk)
  194. (source (origin
  195. (inherit (package-source gawk))
  196. (modules '((guix build utils)))
  197. (snippet
  198. ;; Do not build 'getopt.c' since that leads to a
  199. ;; link failure due to duplicate symbols with
  200. ;; 'libc.a'.
  201. '(substitute* "support/Makefile.in"
  202. (("getopt\\.\\$\\(OBJEXT\\)") "")))
  203. (patches (cons (search-patch "gawk-shell.patch")
  204. (origin-patches
  205. (package-source gawk))))))
  206. (arguments
  207. `(;; Starting from gawk 4.1.0, some of the tests for the
  208. ;; plug-in mechanism just fail on static builds:
  209. ;;
  210. ;; ./fts.awk:1: error: can't open shared library `filefuncs' for reading (No such file or directory)
  211. ;;
  212. ;; Therefore disable extensions support.
  213. #:configure-flags (list "--disable-extensions")
  214. ,@(substitute-keyword-arguments (package-arguments gawk)
  215. ((#:phases phases)
  216. `(modify-phases ,phases
  217. (add-before 'configure 'no-export-dynamic
  218. (lambda _
  219. ;; Since we use `-static', remove
  220. ;; `-export-dynamic'.
  221. (substitute* "configure"
  222. (("-Wl,-export-dynamic") ""))
  223. #t)))))))
  224. (inputs (if (%current-target-system)
  225. `(("bash" ,static-bash))
  226. '()))))
  227. (tar (package (inherit tar)
  228. (arguments
  229. `(;; Work around a cross-compilation bug whereby libgnu.a would provide
  230. ;; '__mktime_internal', which conflicts with the one in libc.a.
  231. ,@(if (%current-target-system)
  232. `(#:configure-flags '("gl_cv_func_working_mktime=yes"))
  233. '())
  234. ,@(substitute-keyword-arguments (package-arguments tar)
  235. ((#:phases phases)
  236. `(modify-phases ,phases
  237. (replace 'set-shell-file-name
  238. (lambda _
  239. ;; Do not use "/bin/sh" to run programs; see
  240. ;; <http://lists.gnu.org/archive/html/guix-devel/2016-09/msg02272.html>.
  241. (substitute* "src/system.c"
  242. (("/bin/sh") "sh")
  243. (("execv ") "execvp "))
  244. #t)))))))))
  245. ;; We don't want to retain a reference to /gnu/store in the bootstrap
  246. ;; versions of egrep/fgrep, so we remove the custom phase added since
  247. ;; grep@2.25. The effect is 'egrep' and 'fgrep' look for 'grep' in
  248. ;; $PATH.
  249. (grep (package
  250. (inherit grep)
  251. (inputs '()) ;remove PCRE, which is optional
  252. (arguments
  253. (substitute-keyword-arguments (package-arguments grep)
  254. ((#:phases phases)
  255. `(modify-phases ,phases
  256. (delete 'fix-egrep-and-fgrep)))))))
  257. (finalize (compose static-package
  258. package-with-relocatable-glibc)))
  259. (append (map finalize
  260. (list tar gzip bzip2 xz patch coreutils sed grep gawk))
  261. (list static-bash))))
  262. (define %static-binaries
  263. (package
  264. (name "static-binaries")
  265. (version "0")
  266. (build-system trivial-build-system)
  267. (source #f)
  268. (inputs %static-inputs)
  269. (arguments
  270. `(#:modules ((guix build utils))
  271. #:builder
  272. (begin
  273. (use-modules (ice-9 ftw)
  274. (ice-9 match)
  275. (srfi srfi-1)
  276. (srfi srfi-26)
  277. (guix build utils))
  278. (let ()
  279. (define (directory-contents dir)
  280. (map (cut string-append dir "/" <>)
  281. (scandir dir (negate (cut member <> '("." ".."))))))
  282. (define (copy-directory source destination)
  283. (for-each (lambda (file)
  284. (format #t "copying ~s...~%" file)
  285. (copy-file file
  286. (string-append destination "/"
  287. (basename file))))
  288. (directory-contents source)))
  289. (let* ((out (assoc-ref %outputs "out"))
  290. (bin (string-append out "/bin")))
  291. (mkdir-p bin)
  292. ;; Copy Coreutils binaries.
  293. (let* ((coreutils (assoc-ref %build-inputs "coreutils"))
  294. (source (string-append coreutils "/bin")))
  295. (copy-directory source bin))
  296. ;; For the other inputs, copy just one binary, which has the
  297. ;; same name as the input.
  298. (for-each (match-lambda
  299. ((name . dir)
  300. (let* ((name (if (string-prefix? "bash" name)
  301. "bash"
  302. name))
  303. (source (string-append dir "/bin/" name)))
  304. (format #t "copying ~s...~%" source)
  305. (copy-file source
  306. (string-append bin "/" name)))))
  307. (alist-delete "coreutils" %build-inputs))
  308. ;; But of course, there are exceptions to this rule.
  309. (let ((grep (assoc-ref %build-inputs "grep")))
  310. (install-file (string-append grep "/bin/fgrep") bin)
  311. (install-file (string-append grep "/bin/egrep") bin))
  312. ;; Clear references to the store path.
  313. (for-each remove-store-references
  314. (directory-contents bin))
  315. (with-directory-excursion bin
  316. ;; Programs such as Perl's build system want these aliases.
  317. (symlink "bash" "sh")
  318. (symlink "gawk" "awk"))
  319. #t)))))
  320. (synopsis "Statically-linked bootstrap binaries")
  321. (description
  322. "Binaries used to bootstrap the distribution.")
  323. (license gpl3+)
  324. (home-page #f)))
  325. (define %linux-libre-headers-stripped
  326. ;; The subset of Linux-Libre-Headers that we need.
  327. (package (inherit linux-libre-headers)
  328. (name (string-append (package-name linux-libre-headers) "-stripped"))
  329. (build-system trivial-build-system)
  330. (outputs '("out"))
  331. (arguments
  332. `(#:modules ((guix build utils)
  333. (guix build make-bootstrap))
  334. #:builder
  335. (begin
  336. (use-modules (guix build utils)
  337. (guix build make-bootstrap))
  338. (let* ((in (assoc-ref %build-inputs "linux-libre-headers"))
  339. (out (assoc-ref %outputs "out")))
  340. (copy-linux-headers out in)
  341. #t))))
  342. (inputs (list linux-libre-headers))))
  343. (define %binutils-static
  344. ;; Statically-linked Binutils.
  345. (package (inherit binutils)
  346. (name "binutils-static")
  347. (arguments
  348. `(#:configure-flags (cons "--disable-gold"
  349. ,(match (memq #:configure-flags
  350. (package-arguments binutils))
  351. ((#:configure-flags flags _ ...)
  352. flags)))
  353. #:strip-flags '("--strip-all")
  354. #:phases (modify-phases %standard-phases
  355. (add-before 'configure 'all-static
  356. (lambda _
  357. ;; The `-all-static' libtool flag can only be passed
  358. ;; after `configure', since configure tests don't use
  359. ;; libtool, and only for executables built with libtool.
  360. (substitute* '("binutils/Makefile.in"
  361. "gas/Makefile.in"
  362. "ld/Makefile.in")
  363. (("^LDFLAGS =(.*)$" line)
  364. (string-append line
  365. "\nAM_LDFLAGS = -static -all-static\n")))
  366. #t)))))))
  367. (define %binutils-static-stripped
  368. ;; The subset of Binutils that we need.
  369. (package
  370. (inherit %binutils-static)
  371. (name (string-append (package-name %binutils-static) "-stripped"))
  372. (build-system trivial-build-system)
  373. (outputs '("out"))
  374. (arguments
  375. (list #:modules '((guix build utils))
  376. #:builder
  377. #~(begin
  378. (use-modules (guix build utils))
  379. (setvbuf (current-output-port)
  380. (cond-expand (guile-2.0 _IOLBF) (else 'line)))
  381. (let* ((in #$%binutils-static)
  382. (out #$output)
  383. (bin (string-append out "/bin")))
  384. (mkdir-p bin)
  385. (for-each (lambda (file)
  386. (let ((target (string-append bin "/" file)))
  387. (format #t "copying `~a'...~%" file)
  388. (copy-file (string-append in "/bin/" file)
  389. target)
  390. (remove-store-references target)))
  391. '("ar" "as" "ld" "nm" "objcopy" "objdump"
  392. "ranlib" "readelf" "size" "strings" "strip"))))))))
  393. (define (%glibc-stripped)
  394. ;; GNU libc's essential shared libraries, dynamic linker, and headers,
  395. ;; with all references to store directories stripped. As a result,
  396. ;; libc.so is unusable and need to be patched for proper relocation.
  397. (let ((glibc (glibc-for-bootstrap glibc)))
  398. (package (inherit glibc)
  399. (name "glibc-stripped")
  400. (build-system trivial-build-system)
  401. (arguments
  402. `(#:modules ((guix build utils)
  403. (guix build make-bootstrap))
  404. #:builder
  405. (begin
  406. (use-modules (guix build make-bootstrap))
  407. (make-stripped-libc (assoc-ref %outputs "out")
  408. (assoc-ref %build-inputs "libc")
  409. (assoc-ref %build-inputs "kernel-headers")))))
  410. (inputs `(("kernel-headers"
  411. ,(if (or (and (%current-target-system)
  412. (target-hurd? (%current-target-system)))
  413. (string-suffix? "-hurd" (%current-system)))
  414. gnumach-headers
  415. linux-libre-headers))
  416. ("libc" ,(let ((target (%current-target-system)))
  417. (if target
  418. (glibc-for-bootstrap
  419. (parameterize ((%current-target-system #f))
  420. (cross-libc target)))
  421. glibc)))))
  422. (native-inputs '())
  423. (propagated-inputs '())
  424. ;; Only one output.
  425. (outputs '("out")))))
  426. (define %gcc-static
  427. ;; A statically-linked GCC, with stripped-down functionality.
  428. (package-with-relocatable-glibc
  429. (package (inherit gcc-7)
  430. (name "gcc-static")
  431. (outputs '("out")) ; all in one
  432. (arguments
  433. (substitute-keyword-arguments (package-arguments gcc-7)
  434. ((#:modules modules %gnu-build-system-modules)
  435. `((srfi srfi-1)
  436. (srfi srfi-26)
  437. (ice-9 regex)
  438. ,@modules))
  439. ((#:guile _) #f)
  440. ((#:implicit-inputs? _) #t)
  441. ((#:configure-flags flags)
  442. `(append (list
  443. ;; We don't need a full bootstrap here.
  444. "--disable-bootstrap"
  445. ;; Make sure '-static' is passed where it matters.
  446. "--with-stage1-ldflags=-static"
  447. ;; GCC 4.8+ requires a C++ compiler and library.
  448. "--enable-languages=c,c++"
  449. ;; Make sure gcc-nm doesn't require liblto_plugin.so.
  450. "--disable-lto"
  451. "--disable-shared"
  452. "--disable-plugin"
  453. "--disable-libmudflap"
  454. "--disable-libatomic"
  455. "--disable-libsanitizer"
  456. "--disable-libitm"
  457. "--disable-libgomp"
  458. "--disable-libcilkrts"
  459. "--disable-libvtv"
  460. "--disable-libssp"
  461. "--disable-libquadmath")
  462. (remove (cut string-match "--(.*plugin|enable-languages)" <>)
  463. ,flags)))
  464. ((#:phases phases)
  465. `(modify-phases ,phases
  466. (add-after 'pre-configure 'remove-lgcc_s
  467. (lambda _
  468. ;; Remove the '-lgcc_s' added to GNU_USER_TARGET_LIB_SPEC in
  469. ;; the 'pre-configure phase of our main gcc package, because
  470. ;; that shared library is not present in this static gcc. See
  471. ;; <https://lists.gnu.org/archive/html/guix-devel/2015-01/msg00008.html>.
  472. (substitute* (cons "gcc/config/rs6000/sysv4.h"
  473. (find-files "gcc/config"
  474. "^gnu-user.*\\.h$"))
  475. ((" -lgcc_s}}") "}}"))
  476. #t))))))
  477. (inputs
  478. `(("zlib:static" ,zlib "static")
  479. ("isl:static" ,isl "static")
  480. ,@(package-inputs gcc-7)))
  481. (native-inputs
  482. (if (%current-target-system)
  483. `(;; When doing a Canadian cross, we need GMP/MPFR/MPC both
  484. ;; as target inputs and as native inputs; the latter is
  485. ;; needed when building build-time tools ('genconstants',
  486. ;; etc.) Failing to do that leads to misdetections of
  487. ;; declarations by 'gcc/configure', and eventually to
  488. ;; duplicate declarations as reported in
  489. ;; <https://gcc.gnu.org/bugzilla/show_bug.cgi?id=59217>.
  490. ("gmp-native" ,gmp)
  491. ("mpfr-native" ,mpfr)
  492. ("mpc-native" ,mpc)
  493. ,@(package-native-inputs gcc-7))
  494. (package-native-inputs gcc-7))))))
  495. (define %gcc-stripped
  496. ;; The subset of GCC files needed for bootstrap.
  497. (package
  498. (inherit gcc-7)
  499. (name "gcc-stripped")
  500. (build-system trivial-build-system)
  501. (source #f)
  502. (outputs '("out")) ;only one output
  503. (arguments
  504. (list #:modules '((guix build utils))
  505. #:builder
  506. #~(begin
  507. (use-modules (srfi srfi-1)
  508. (srfi srfi-26)
  509. (guix build utils))
  510. (setvbuf (current-output-port)
  511. (cond-expand (guile-2.0 _IOLBF) (else 'line)))
  512. (let* ((out #$output)
  513. (bindir (string-append out "/bin"))
  514. (libdir (string-append out "/lib"))
  515. (includedir (string-append out "/include"))
  516. (libexecdir (string-append out "/libexec"))
  517. (gcc #$%gcc-static))
  518. (copy-recursively (string-append gcc "/bin") bindir)
  519. (for-each remove-store-references
  520. (find-files bindir ".*"))
  521. (copy-recursively (string-append gcc "/lib") libdir)
  522. (for-each remove-store-references
  523. (remove (cut string-suffix? ".h" <>)
  524. (find-files libdir ".*")))
  525. (copy-recursively (string-append gcc "/libexec")
  526. libexecdir)
  527. (for-each remove-store-references
  528. (find-files libexecdir ".*"))
  529. ;; Starting from GCC 4.8, helper programs built natively
  530. ;; (‘genchecksum’, ‘gcc-nm’, etc.) rely on C++ headers.
  531. (copy-recursively (string-append gcc "/include/c++")
  532. (string-append includedir "/c++"))
  533. ;; For native builds, check whether the binaries actually work.
  534. #$@(if (%current-target-system)
  535. '()
  536. '((for-each (lambda (prog)
  537. (invoke (string-append gcc "/bin/" prog)
  538. "--version"))
  539. '("gcc" "g++" "cpp"))))))))))
  540. ;; Two packages: first build static, bare minimum content.
  541. (define %mescc-tools-static
  542. ;; A statically linked MesCC Tools.
  543. (package
  544. (inherit mescc-tools)
  545. (name "mescc-tools-static")
  546. (arguments
  547. `(#:system "i686-linux"
  548. ,@(substitute-keyword-arguments (package-arguments mescc-tools)
  549. ((#:make-flags flags)
  550. `(cons "CC=gcc -static" ,flags)))))))
  551. ;; ... next remove store references.
  552. (define %mescc-tools-static-stripped
  553. ;; A statically linked Mescc Tools with store references removed, for
  554. ;; bootstrap.
  555. (package
  556. (inherit %mescc-tools-static)
  557. (name (string-append (package-name %mescc-tools-static) "-stripped"))
  558. (build-system trivial-build-system)
  559. (arguments
  560. (list #:modules '((guix build utils))
  561. #:builder
  562. #~(begin
  563. (use-modules (guix build utils))
  564. (let* ((in #$%mescc-tools-static)
  565. (out #$output)
  566. (bin (string-append out "/bin")))
  567. (mkdir-p bin)
  568. (for-each (lambda (file)
  569. (let ((target (string-append bin "/" file)))
  570. (format #t "copying `~a'...~%" file)
  571. (copy-file (string-append in "/bin/" file)
  572. target)
  573. (remove-store-references target)))
  574. '( "M1" "blood-elf" "hex2"))))))))
  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 (list guile-3.0))
  582. (arguments
  583. `(#:system "i686-linux"
  584. #:strip-binaries? #f
  585. #:configure-flags '("--mes")
  586. #:phases
  587. (modify-phases %standard-phases
  588. (delete 'patch-shebangs)
  589. (add-after 'install 'strip-install
  590. (lambda _
  591. (let* ((out (assoc-ref %outputs "out"))
  592. (share (string-append out "/share")))
  593. (delete-file-recursively (string-append out "/lib/guile"))
  594. (delete-file-recursively (string-append share "/guile"))
  595. (for-each delete-file
  596. (find-files
  597. (string-append share "/mes/lib")
  598. "\\.(h|c)"))))))))))
  599. ;; next remove store references.
  600. (define %mes-minimal-stripped
  601. ;; A minimal Mes with store references removed, for bootstrap.
  602. (package
  603. (inherit %mes-minimal)
  604. (name (string-append (package-name %mes-minimal) "-stripped"))
  605. (build-system trivial-build-system)
  606. (arguments
  607. (list #:modules '((guix build utils))
  608. #:allowed-references '()
  609. #:builder
  610. #~(begin
  611. (use-modules (guix build utils))
  612. (let ((in #$%mes-minimal)
  613. (out #$output))
  614. (copy-recursively in out)
  615. (for-each (lambda (dir)
  616. (for-each remove-store-references
  617. (find-files (string-append out "/" dir)
  618. ".*")))
  619. '("bin" "share/mes"))))))))
  620. (define* (make-guile-static guile patches)
  621. (package-with-relocatable-glibc
  622. (static-package
  623. (package
  624. (inherit guile)
  625. (source
  626. (origin (inherit (package-source guile))
  627. (patches (append (map search-patch patches)
  628. (origin-patches (package-source guile))))))
  629. (name (string-append (package-name guile) "-static"))
  630. (synopsis "Statically-linked and relocatable Guile")
  631. ;; Remove the 'debug' output (see above for the reason.)
  632. (outputs (delete "debug" (package-outputs guile)))
  633. (inputs
  634. (modify-inputs (package-inputs guile)
  635. (prepend `(,libunistring "static"))))
  636. (propagated-inputs
  637. (modify-inputs (package-propagated-inputs guile)
  638. (replace "bdw-gc" libgc/static-libs)))
  639. (arguments
  640. (substitute-keyword-arguments (package-arguments guile)
  641. ((#:configure-flags flags '())
  642. ;; When `configure' checks for ltdl availability, it
  643. ;; doesn't try to link using libtool, and thus fails
  644. ;; because of a missing -ldl. Work around that.
  645. `(list "LDFLAGS=-ldl" "--enable-mini-gmp"
  646. ,@(if (hurd-target?)
  647. '("--disable-jit")
  648. '())))
  649. ((#:phases phases '%standard-phases)
  650. `(modify-phases ,phases
  651. ;; Do not record the absolute file name of 'sh' in
  652. ;; (ice-9 popen). This makes 'open-pipe' unusable in
  653. ;; a build chroot ('open-pipe*' is fine) but avoids
  654. ;; keeping a reference to Bash.
  655. (delete 'pre-configure)
  656. (add-before 'configure 'static-guile
  657. (lambda _
  658. (substitute* "libguile/Makefile.in"
  659. ;; Create a statically-linked `guile'
  660. ;; executable.
  661. (("^guile_LDFLAGS =")
  662. "guile_LDFLAGS = -all-static")
  663. ;; Add `-ldl' *after* libguile-2.0.la.
  664. (("^guile_LDADD =(.*)$" _ ldadd)
  665. (string-append "guile_LDADD = "
  666. (string-trim-right ldadd)
  667. " -ldl\n")))))))
  668. ((#:tests? _ #f)
  669. ;; There are uses of `dynamic-link' in
  670. ;; {foreign,coverage}.test that don't fly here.
  671. #f)
  672. ((#:parallel-build? _ #f)
  673. ;; Work around the fact that the Guile build system is
  674. ;; not deterministic when parallel-build is enabled.
  675. #f)))))))
  676. (define %guile-static
  677. ;; A statically-linked Guile that is relocatable--i.e., it can search
  678. ;; .scm and .go files relative to its installation directory, rather
  679. ;; than in hard-coded configure-time paths.
  680. (make-guile-static guile-2.0 '("guile-relocatable.patch"
  681. "guile-default-utf8.patch"
  682. "guile-linux-syscalls.patch")))
  683. (define* (make-guile-static-stripped static-guile)
  684. (package
  685. (inherit static-guile)
  686. (name (string-append (package-name static-guile) "-stripped"))
  687. (build-system trivial-build-system)
  688. (arguments
  689. ;; The end result should depend on nothing but itself.
  690. (list #:allowed-references '("out")
  691. #:modules '((guix build utils))
  692. #:builder
  693. #~(let ((version #$(version-major+minor (package-version static-guile))))
  694. (use-modules (guix build utils))
  695. (let* ((in #$static-guile)
  696. (out #$output)
  697. (guile1 (string-append in "/bin/guile"))
  698. (guile2 (string-append out "/bin/guile")))
  699. (mkdir-p (string-append out "/share/guile/" version))
  700. (copy-recursively (string-append in "/share/guile/" version)
  701. (string-append out "/share/guile/" version))
  702. (mkdir-p (string-append out "/lib/guile/" version "/ccache"))
  703. (copy-recursively (string-append in "/lib/guile/" version "/ccache")
  704. (string-append out "/lib/guile/" version "/ccache"))
  705. (mkdir (string-append out "/bin"))
  706. (copy-file guile1 guile2)
  707. ;; Verify that the relocated Guile works.
  708. #$@(if (%current-target-system)
  709. '()
  710. '((invoke guile2 "--version")))
  711. ;; Strip store references.
  712. (remove-store-references guile2)
  713. ;; Verify that the stripped Guile works. If it aborts, it could be
  714. ;; that it tries to open iconv descriptors and fails because libc's
  715. ;; iconv data isn't available (see `guile-default-utf8.patch'.)
  716. #$@(if (%current-target-system)
  717. '()
  718. '((invoke guile2 "--version")))))))
  719. (outputs '("out"))
  720. (synopsis "Minimal statically-linked and relocatable Guile")))
  721. (define %guile-static-stripped
  722. ;; A stripped static Guile 3.0 binary, for use in initrds
  723. ;; and during bootstrap.
  724. (make-guile-static-stripped
  725. (make-guile-static guile-3.0
  726. '("guile-2.2-default-utf8.patch"
  727. "guile-3.0-linux-syscalls.patch"
  728. "guile-3.0-relocatable.patch"))))
  729. (define (tarball-package pkg)
  730. "Return a package containing a tarball of PKG."
  731. (package
  732. (inherit pkg)
  733. (name (string-append (package-name pkg) "-tarball"))
  734. (build-system trivial-build-system)
  735. (native-inputs (list tar xz))
  736. (arguments
  737. (list #:modules '((guix build utils))
  738. #:builder
  739. #~(begin
  740. (use-modules (guix build utils))
  741. (let ((out #$output)
  742. (input #$pkg)
  743. (tar #+(this-package-native-input "tar"))
  744. (xz #+(this-package-native-input "xz")))
  745. (mkdir out)
  746. (set-path-environment-variable "PATH" '("bin") (list tar xz))
  747. (with-directory-excursion input
  748. (invoke "tar" "cJvf"
  749. (string-append out "/"
  750. #$(package-name pkg) "-"
  751. #$(package-version pkg)
  752. "-"
  753. #$(or (%current-target-system)
  754. (%current-system))
  755. ".tar.xz")
  756. "."
  757. ;; avoid non-determinism in the archive
  758. "--sort=name" "--mtime=@0"
  759. "--owner=root:0" "--group=root:0"))))))))
  760. (define %bootstrap-binaries-tarball
  761. ;; A tarball with the statically-linked bootstrap binaries.
  762. (tarball-package %static-binaries))
  763. (define %linux-libre-headers-bootstrap-tarball
  764. ;; A tarball with the statically-linked Linux-Libre-Headers programs.
  765. (tarball-package %linux-libre-headers-stripped))
  766. (define %binutils-bootstrap-tarball
  767. ;; A tarball with the statically-linked Binutils programs.
  768. (tarball-package %binutils-static-stripped))
  769. (define (%glibc-bootstrap-tarball)
  770. ;; A tarball with GNU libc's shared libraries, dynamic linker, and headers.
  771. (tarball-package (%glibc-stripped)))
  772. (define %gcc-bootstrap-tarball
  773. ;; A tarball with a dynamic-linked GCC and its headers.
  774. (tarball-package %gcc-stripped))
  775. (define %guile-bootstrap-tarball
  776. ;; A tarball with the statically-linked, relocatable Guile.
  777. (tarball-package %guile-static-stripped))
  778. (define %mescc-tools-bootstrap-tarball
  779. ;; A tarball with statically-linked MesCC binary seed.
  780. (tarball-package %mescc-tools-static-stripped))
  781. (define %mes-bootstrap-tarball
  782. ;; A tarball with Mes binary seed.
  783. (tarball-package %mes-minimal-stripped))
  784. (define %bootstrap-tarballs
  785. ;; A single derivation containing all the bootstrap tarballs, for
  786. ;; convenience.
  787. (package
  788. (name "bootstrap-tarballs")
  789. (version "0")
  790. (source #f)
  791. (build-system trivial-build-system)
  792. (arguments
  793. (list #:modules '((guix build utils))
  794. #:builder
  795. #~(begin
  796. (use-modules (guix build utils)
  797. (ice-9 match)
  798. (srfi srfi-26))
  799. (define out #$output)
  800. (setvbuf (current-output-port)
  801. (cond-expand (guile-2.0 _IOLBF) (else 'line)))
  802. (mkdir out)
  803. (chdir out)
  804. (for-each (match-lambda
  805. ((name . directory)
  806. (for-each (lambda (file)
  807. (format #t "~a -> ~a~%" file out)
  808. (symlink file (basename file)))
  809. (find-files directory "\\.tar\\."))))
  810. %build-inputs))))
  811. (inputs
  812. (append (list %guile-bootstrap-tarball)
  813. (match (or (%current-target-system) (%current-system))
  814. ((or "i686-linux" "x86_64-linux")
  815. (list %mescc-tools-bootstrap-tarball
  816. %mes-bootstrap-tarball
  817. %linux-libre-headers-bootstrap-tarball))
  818. (_
  819. (list %gcc-bootstrap-tarball
  820. %binutils-bootstrap-tarball
  821. (%glibc-bootstrap-tarball)
  822. %bootstrap-binaries-tarball)))))
  823. (synopsis "Tarballs containing all the bootstrap binaries")
  824. (description synopsis)
  825. (home-page #f)
  826. (license gpl3+)))
  827. ;;; make-bootstrap.scm ends here