ci.scm 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2012-2021 Ludovic Courtès <ludo@gnu.org>
  3. ;;; Copyright © 2017, 2020 Jan (janneke) Nieuwenhuizen <janneke@gnu.org>
  4. ;;; Copyright © 2018, 2019 Clément Lassieur <clement@lassieur.org>
  5. ;;; Copyright © 2020 Julien Lepiller <julien@lepiller.eu>
  6. ;;; Copyright © 2020, 2021 Mathieu Othacehe <othacehe@gnu.org>
  7. ;;;
  8. ;;; This file is part of GNU Guix.
  9. ;;;
  10. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  11. ;;; under the terms of the GNU General Public License as published by
  12. ;;; the Free Software Foundation; either version 3 of the License, or (at
  13. ;;; your option) any later version.
  14. ;;;
  15. ;;; GNU Guix is distributed in the hope that it will be useful, but
  16. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  17. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. ;;; GNU General Public License for more details.
  19. ;;;
  20. ;;; You should have received a copy of the GNU General Public License
  21. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  22. (define-module (gnu ci)
  23. #:use-module (guix channels)
  24. #:use-module (guix config)
  25. #:use-module (guix describe)
  26. #:use-module (guix store)
  27. #:use-module (guix grafts)
  28. #:use-module (guix profiles)
  29. #:use-module (guix packages)
  30. #:use-module (guix channels)
  31. #:use-module (guix config)
  32. #:use-module (guix derivations)
  33. #:use-module (guix build-system)
  34. #:use-module (guix monads)
  35. #:use-module (guix gexp)
  36. #:use-module (guix ui)
  37. #:use-module ((guix licenses)
  38. #:select (gpl3+ license? license-name))
  39. #:use-module ((guix utils) #:select (%current-system))
  40. #:use-module ((guix scripts system) #:select (read-operating-system))
  41. #:use-module ((guix scripts pack)
  42. #:select (lookup-compressor self-contained-tarball))
  43. #:use-module (gnu bootloader)
  44. #:use-module (gnu bootloader u-boot)
  45. #:use-module (gnu image)
  46. #:use-module (gnu packages)
  47. #:use-module (gnu packages gcc)
  48. #:use-module (gnu packages base)
  49. #:use-module (gnu packages gawk)
  50. #:use-module (gnu packages guile)
  51. #:use-module (gnu packages gettext)
  52. #:use-module (gnu packages compression)
  53. #:use-module (gnu packages multiprecision)
  54. #:use-module (gnu packages make-bootstrap)
  55. #:use-module (gnu packages package-management)
  56. #:use-module (gnu system)
  57. #:use-module (gnu system image)
  58. #:use-module (gnu system vm)
  59. #:use-module (gnu system install)
  60. #:use-module (gnu system images hurd)
  61. #:use-module (gnu system images novena)
  62. #:use-module (gnu system images pine64)
  63. #:use-module (gnu system images pinebook-pro)
  64. #:use-module (gnu tests)
  65. #:use-module (srfi srfi-1)
  66. #:use-module (srfi srfi-26)
  67. #:use-module (ice-9 match)
  68. #:export (derivation->job
  69. image->job
  70. %core-packages
  71. %cross-targets
  72. channel-source->package
  73. arguments->systems
  74. cuirass-jobs))
  75. ;;; Commentary:
  76. ;;;
  77. ;;; This file defines build jobs for Cuirass.
  78. ;;;
  79. ;;; Code:
  80. (define* (derivation->job name drv
  81. #:key
  82. (max-silent-time 3600)
  83. (timeout (* 5 3600)))
  84. "Return a Cuirass job called NAME and describing DRV.
  85. MAX-SILENT-TIME and TIMEOUT are build options passed to the daemon when
  86. building the derivation."
  87. `((#:job-name . ,name)
  88. (#:derivation . ,(derivation-file-name drv))
  89. (#:inputs . ,(map (compose derivation-file-name
  90. derivation-input-derivation)
  91. (derivation-inputs drv)))
  92. (#:outputs . ,(filter-map
  93. (lambda (res)
  94. (match res
  95. ((name . path)
  96. `(,name . ,path))))
  97. (derivation->output-paths drv)))
  98. (#:nix-name . ,(derivation-name drv))
  99. (#:system . ,(derivation-system drv))
  100. (#:max-silent-time . ,max-silent-time)
  101. (#:timeout . ,timeout)))
  102. (define* (package-job store job-name package system
  103. #:key cross? target)
  104. "Return a job called JOB-NAME that builds PACKAGE on SYSTEM."
  105. (let ((job-name (string-append job-name "." system)))
  106. (parameterize ((%graft? #f))
  107. (let* ((drv (if cross?
  108. (package-cross-derivation store package target system
  109. #:graft? #f)
  110. (package-derivation store package system
  111. #:graft? #f)))
  112. (max-silent-time (or (assoc-ref (package-properties package)
  113. 'max-silent-time)
  114. 3600))
  115. (timeout (or (assoc-ref (package-properties package)
  116. 'timeout)
  117. 72000)))
  118. (derivation->job job-name drv
  119. #:max-silent-time max-silent-time
  120. #:timeout timeout)))))
  121. (define (package-cross-job store job-name package target system)
  122. "Return a job called TARGET.JOB-NAME that cross-builds PACKAGE for TARGET on
  123. SYSTEM."
  124. (let ((name (string-append target "." job-name)))
  125. (package-job store name package system
  126. #:cross? #t
  127. #:target target)))
  128. (define %core-packages
  129. ;; Note: Don't put the '-final' package variants because (1) that's
  130. ;; implicit, and (2) they cannot be cross-built (due to the explicit input
  131. ;; chain.)
  132. (list gcc-8 gcc-9 gcc-10 gcc-11 glibc binutils
  133. gmp mpfr mpc coreutils findutils diffutils patch sed grep
  134. gawk gnu-gettext hello guile-2.2 guile-3.0 zlib gzip xz
  135. %bootstrap-binaries-tarball
  136. %binutils-bootstrap-tarball
  137. (%glibc-bootstrap-tarball)
  138. %gcc-bootstrap-tarball
  139. %guile-bootstrap-tarball
  140. %bootstrap-tarballs))
  141. (define (commencement-packages system)
  142. "Return the list of bootstrap packages from the commencement module for
  143. SYSTEM."
  144. ;; Only include packages supported on SYSTEM. For example, the Mes
  145. ;; bootstrap graph is currently not supported on ARM so it should be
  146. ;; excluded.
  147. (filter (lambda (obj)
  148. (and (package? obj)
  149. (supported-package? obj system)))
  150. (module-map (lambda (sym var)
  151. (variable-ref var))
  152. (resolve-module '(gnu packages commencement)))))
  153. (define (packages-to-cross-build target)
  154. "Return the list of packages to cross-build for TARGET."
  155. ;; Don't cross-build the bootstrap tarballs for MinGW.
  156. (if (string-contains target "mingw")
  157. (drop-right %core-packages 6)
  158. %core-packages))
  159. (define %cross-targets
  160. '("mips64el-linux-gnu"
  161. "arm-linux-gnueabihf"
  162. "aarch64-linux-gnu"
  163. "powerpc-linux-gnu"
  164. "powerpc64le-linux-gnu"
  165. "riscv64-linux-gnu"
  166. "i586-pc-gnu" ;aka. GNU/Hurd
  167. "i686-w64-mingw32"
  168. "x86_64-w64-mingw32"))
  169. (define (cross-jobs store system)
  170. "Return a list of cross-compilation jobs for SYSTEM."
  171. (define (from-32-to-64? target)
  172. ;; Return true if SYSTEM is 32-bit and TARGET is 64-bit. This hack
  173. ;; prevents known-to-fail cross-builds from i686-linux or armhf-linux to
  174. ;; mips64el-linux-gnuabi64.
  175. (and (or (string-prefix? "i686-" system)
  176. (string-prefix? "i586-" system)
  177. (string-prefix? "armhf-" system))
  178. (string-contains target "64"))) ;x86_64, mips64el, aarch64, etc.
  179. (define (same? target)
  180. ;; Return true if SYSTEM and TARGET are the same thing. This is so we
  181. ;; don't try to cross-compile to 'mips64el-linux-gnu' from
  182. ;; 'mips64el-linux'.
  183. (or (string-contains target system)
  184. (and (string-prefix? "armhf" system) ;armhf-linux
  185. (string-prefix? "arm" target)))) ;arm-linux-gnueabihf
  186. (define (pointless? target)
  187. ;; Return #t if it makes no sense to cross-build to TARGET from SYSTEM.
  188. (match system
  189. ((or "x86_64-linux" "i686-linux")
  190. (if (string-contains target "mingw")
  191. (not (string=? "x86_64-linux" system))
  192. #f))
  193. (_
  194. ;; Don't try to cross-compile from non-Intel platforms: this isn't
  195. ;; very useful and these are often brittle configurations.
  196. #t)))
  197. (define (either proc1 proc2 proc3)
  198. (lambda (x)
  199. (or (proc1 x) (proc2 x) (proc3 x))))
  200. (append-map (lambda (target)
  201. (map (lambda (package)
  202. (package-cross-job store (job-name package)
  203. package target system))
  204. (packages-to-cross-build target)))
  205. (remove (either from-32-to-64? same? pointless?)
  206. %cross-targets)))
  207. (define* (guix-jobs store systems #:key source commit)
  208. "Return a list of jobs for Guix itself."
  209. (define build
  210. (primitive-load (string-append source "/build-aux/build-self.scm")))
  211. (map
  212. (lambda (system)
  213. (let ((name (string->symbol
  214. (string-append "guix." system)))
  215. (drv (run-with-store store
  216. (build source #:version commit #:system system
  217. #:pull-version 1
  218. #:guile-version "2.2"))))
  219. (derivation->job name drv)))
  220. systems))
  221. ;; Architectures that are able to build or cross-build Guix System images.
  222. ;; This does not mean that other architectures are not supported, only that
  223. ;; they are often not fast enough to support Guix System images building.
  224. (define %guix-system-supported-systems
  225. '("x86_64-linux" "i686-linux"))
  226. (define %guix-system-images
  227. (list hurd-barebones-qcow2-image
  228. pine64-barebones-raw-image
  229. pinebook-pro-barebones-raw-image
  230. novena-barebones-raw-image))
  231. (define (hours hours)
  232. (* 3600 hours))
  233. (define* (image->job store image
  234. #:key name system)
  235. "Return the job for IMAGE on SYSTEM. If NAME is passed, use it as job name,
  236. otherwise use the IMAGE name."
  237. (let* ((image-name (or name
  238. (symbol->string (image-name image))))
  239. (name (string-append image-name "." system))
  240. (drv (run-with-store store
  241. (mbegin %store-monad
  242. (set-guile-for-build (default-guile))
  243. (lower-object (system-image image))))))
  244. (parameterize ((%graft? #f))
  245. (derivation->job name drv))))
  246. (define (image-jobs store system)
  247. "Return a list of jobs that build images for SYSTEM."
  248. (define MiB
  249. (expt 2 20))
  250. (if (member system %guix-system-supported-systems)
  251. `(,(image->job store
  252. (image
  253. (inherit efi-disk-image)
  254. (operating-system installation-os))
  255. #:name "usb-image"
  256. #:system system)
  257. ,(image->job
  258. store
  259. (image
  260. (inherit (image-with-label
  261. iso9660-image
  262. (string-append "GUIX_" system "_"
  263. (if (> (string-length %guix-version) 7)
  264. (substring %guix-version 0 7)
  265. %guix-version))))
  266. (operating-system installation-os))
  267. #:name "iso9660-image"
  268. #:system system)
  269. ;; Only cross-compile Guix System images from x86_64-linux for now.
  270. ,@(if (string=? system "x86_64-linux")
  271. (map (cut image->job store <>
  272. #:system system)
  273. %guix-system-images)
  274. '()))
  275. '()))
  276. (define channel-build-system
  277. ;; Build system used to "convert" a channel instance to a package.
  278. (let* ((build (lambda* (name inputs
  279. #:key source commit system
  280. #:allow-other-keys)
  281. (mlet* %store-monad ((source (if (string? source)
  282. (return source)
  283. (lower-object source)))
  284. (instance
  285. -> (checkout->channel-instance
  286. source #:commit commit)))
  287. (channel-instances->derivation (list instance)))))
  288. (lower (lambda* (name #:key system source commit
  289. #:allow-other-keys)
  290. (bag
  291. (name name)
  292. (system system)
  293. (build build)
  294. (arguments `(#:source ,source
  295. #:commit ,commit))))))
  296. (build-system (name 'channel)
  297. (description "Turn a channel instance into a package.")
  298. (lower lower))))
  299. (define* (channel-source->package source #:key commit)
  300. "Return a package for the given channel SOURCE, a lowerable object."
  301. (package
  302. (inherit guix)
  303. (version (string-append (package-version guix) "+"))
  304. (build-system channel-build-system)
  305. (arguments `(#:source ,source
  306. #:commit ,commit))
  307. (inputs '())
  308. (native-inputs '())
  309. (propagated-inputs '())))
  310. (define* (system-test-jobs store system
  311. #:key source commit)
  312. "Return a list of jobs for the system tests."
  313. (define (->job test)
  314. (let ((name (string-append "test." (system-test-name test)
  315. "." system))
  316. (drv (run-with-store store
  317. (mbegin %store-monad
  318. (set-current-system system)
  319. (set-grafting #f)
  320. (set-guile-for-build (default-guile))
  321. (system-test-value test)))))
  322. (derivation->job name drv)))
  323. (if (member system %guix-system-supported-systems)
  324. ;; Override the value of 'current-guix' used by system tests. Using a
  325. ;; channel instance makes tests that rely on 'current-guix' less
  326. ;; expensive. It also makes sure we get a valid Guix package when this
  327. ;; code is not running from a checkout.
  328. (parameterize ((current-guix-package
  329. (channel-source->package source #:commit commit)))
  330. (map ->job (all-system-tests)))
  331. '()))
  332. (define (tarball-jobs store system)
  333. "Return jobs to build the self-contained Guix binary tarball."
  334. (define (->job name drv)
  335. (let ((name (string-append name "." system)))
  336. (parameterize ((%graft? #f))
  337. (derivation->job name drv))))
  338. ;; XXX: Add a job for the stable Guix?
  339. (list
  340. (->job "binary-tarball"
  341. (run-with-store store
  342. (mbegin %store-monad
  343. (set-guile-for-build (default-guile))
  344. (>>= (profile-derivation (packages->manifest (list guix)))
  345. (lambda (profile)
  346. (self-contained-tarball "guix-binary" profile
  347. #:profile-name "current-guix"
  348. #:localstatedir? #t
  349. #:compressor
  350. (lookup-compressor "xz")))))
  351. #:system system))))
  352. (define job-name
  353. ;; Return the name of a package's job.
  354. package-name)
  355. (define package->job
  356. (let ((base-packages
  357. (delete-duplicates
  358. (append-map (match-lambda
  359. ((_ package _ ...)
  360. (match (package-transitive-inputs package)
  361. (((_ inputs _ ...) ...)
  362. inputs))))
  363. (%final-inputs)))))
  364. (lambda (store package system)
  365. "Return a job for PACKAGE on SYSTEM, or #f if this combination is not
  366. valid."
  367. (cond ((member package base-packages)
  368. (package-job store (string-append "base." (job-name package))
  369. package system))
  370. ((supported-package? package system)
  371. (let ((drv (package-derivation store package system
  372. #:graft? #f)))
  373. (and (substitutable-derivation? drv)
  374. (package-job store (job-name package)
  375. package system))))
  376. (else
  377. #f)))))
  378. (define (all-packages)
  379. "Return the list of packages to build."
  380. (define (adjust package result)
  381. (cond ((package-replacement package)
  382. ;; XXX: If PACKAGE and its replacement have the same name/version,
  383. ;; then both Cuirass jobs will have the same name, which
  384. ;; effectively means that the second one will be ignored. Thus,
  385. ;; return the replacement first.
  386. (cons* (package-replacement package) ;build both
  387. package
  388. result))
  389. ((package-superseded package)
  390. result) ;don't build it
  391. (else
  392. (cons package result))))
  393. (fold-packages adjust
  394. (fold adjust '() ;include base packages
  395. (match (%final-inputs)
  396. (((labels packages _ ...) ...)
  397. packages)))
  398. #:select? (const #t))) ;include hidden packages
  399. (define (arguments->manifests arguments channels)
  400. "Return the list of manifests extracted from ARGUMENTS."
  401. (map (lambda (manifest)
  402. (any (lambda (checkout)
  403. (let ((path (in-vicinity checkout manifest)))
  404. (and (file-exists? path)
  405. path)))
  406. (map channel-url channels)))
  407. arguments))
  408. (define (manifests->jobs store manifests)
  409. "Return the list of jobs for the entries in MANIFESTS, a list of file
  410. names."
  411. (define (load-manifest manifest)
  412. (save-module-excursion
  413. (lambda ()
  414. (set-current-module (make-user-module '((guix profiles) (gnu))))
  415. (primitive-load manifest))))
  416. (define (manifest-entry-job-name entry)
  417. (string-append (manifest-entry-name entry) "-"
  418. (manifest-entry-version entry)))
  419. (define (manifest-entry->job entry)
  420. (let* ((obj (manifest-entry-item entry))
  421. (drv (parameterize ((%graft? #f))
  422. (run-with-store store
  423. (lower-object obj))))
  424. (max-silent-time (or (and (package? obj)
  425. (assoc-ref (package-properties obj)
  426. 'max-silent-time))
  427. 3600))
  428. (timeout (or (and (package? obj)
  429. (assoc-ref (package-properties obj) 'timeout))
  430. (* 5 3600))))
  431. (derivation->job (manifest-entry-job-name entry) drv
  432. #:max-silent-time max-silent-time
  433. #:timeout timeout)))
  434. (map manifest-entry->job
  435. (delete-duplicates
  436. (append-map (compose manifest-entries load-manifest)
  437. manifests)
  438. manifest-entry=?)))
  439. (define (arguments->systems arguments)
  440. "Return the systems list from ARGUMENTS."
  441. (match (assoc-ref arguments 'systems)
  442. (#f %cuirass-supported-systems)
  443. ((lst ...) lst)
  444. ((? string? str) (call-with-input-string str read))))
  445. ;;;
  446. ;;; Cuirass entry point.
  447. ;;;
  448. (define (cuirass-jobs store arguments)
  449. "Register Cuirass jobs."
  450. (define subset
  451. (assoc-ref arguments 'subset))
  452. (define systems
  453. (arguments->systems arguments))
  454. (define channels
  455. (let ((channels (assq-ref arguments 'channels)))
  456. (map sexp->channel channels)))
  457. (define guix
  458. (find guix-channel? channels))
  459. (define commit
  460. (channel-commit guix))
  461. (define source
  462. (channel-url guix))
  463. ;; Turn off grafts. Grafting is meant to happen on the user's machines.
  464. (parameterize ((%graft? #f))
  465. ;; Return one job for each package, except bootstrap packages.
  466. (append-map
  467. (lambda (system)
  468. (format (current-error-port)
  469. "evaluating for '~a' (heap size: ~a MiB)...~%"
  470. system
  471. (round
  472. (/ (assoc-ref (gc-stats) 'heap-size)
  473. (expt 2. 20))))
  474. (invalidate-derivation-caches!)
  475. (match subset
  476. ('all
  477. ;; Build everything, including replacements.
  478. (let ((all (all-packages))
  479. (job (lambda (package)
  480. (package->job store package system))))
  481. (append
  482. (filter-map job all)
  483. (cross-jobs store system))))
  484. ('core
  485. ;; Build core packages only.
  486. (append
  487. (map (lambda (package)
  488. (package-job store (job-name package)
  489. package system))
  490. (append (commencement-packages system) %core-packages))
  491. (cross-jobs store system)))
  492. ('guix
  493. ;; Build Guix modules only.
  494. (guix-jobs store systems
  495. #:source source
  496. #:commit commit))
  497. ('hello
  498. ;; Build hello package only.
  499. (let ((hello (specification->package "hello")))
  500. (list (package-job store (job-name hello)
  501. hello system))))
  502. ('images
  503. ;; Build Guix System images only.
  504. (image-jobs store system))
  505. ('system-tests
  506. ;; Build Guix System tests only.
  507. (system-test-jobs store system
  508. #:source source
  509. #:commit commit))
  510. ('tarball
  511. ;; Build Guix tarball only.
  512. (tarball-jobs store system))
  513. (('custom . modules)
  514. ;; Build custom modules jobs only.
  515. (append-map
  516. (lambda (module)
  517. (let ((proc (module-ref
  518. (resolve-interface module)
  519. 'cuirass-jobs)))
  520. (proc store arguments)))
  521. modules))
  522. (('channels . channels)
  523. ;; Build only the packages from CHANNELS.
  524. (let ((all (all-packages)))
  525. (filter-map
  526. (lambda (package)
  527. (any (lambda (channel)
  528. (and (member (channel-name channel) channels)
  529. (package->job store package system)))
  530. (package-channels package)))
  531. all)))
  532. (('packages . rest)
  533. ;; Build selected list of packages only.
  534. (let ((packages (map specification->package rest)))
  535. (map (lambda (package)
  536. (package-job store (job-name package)
  537. package system))
  538. packages)))
  539. (('manifests . rest)
  540. ;; Build packages in the list of manifests.
  541. (let ((manifests (arguments->manifests rest channels)))
  542. (manifests->jobs store manifests)))
  543. (else
  544. (error "unknown subset" subset))))
  545. systems)))