ci.scm 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020 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 (%core-packages
  69. %cross-targets
  70. channel-source->package
  71. cuirass-jobs))
  72. ;;; Commentary:
  73. ;;;
  74. ;;; This file defines build jobs for Cuirass.
  75. ;;;
  76. ;;; Code:
  77. (define* (derivation->job name drv
  78. #:key
  79. period
  80. (max-silent-time 3600)
  81. (timeout 3600))
  82. "Return a Cuirass job called NAME and describing DRV. PERIOD is the minimal
  83. duration that must separate two evaluations of the same job. If PERIOD is
  84. false, then the job will be evaluated as soon as possible.
  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. (#:outputs . ,(filter-map
  90. (lambda (res)
  91. (match res
  92. ((name . path)
  93. `(,name . ,path))))
  94. (derivation->output-paths drv)))
  95. (#:nix-name . ,(derivation-name drv))
  96. (#:system . ,(derivation-system drv))
  97. (#:period . ,period)
  98. (#:max-silent-time . ,max-silent-time)
  99. (#:timeout . ,timeout)))
  100. (define* (package-job store job-name package system
  101. #:key cross? target)
  102. "Return a job called JOB-NAME that builds PACKAGE on SYSTEM."
  103. (let ((job-name (string-append job-name "." system)))
  104. (parameterize ((%graft? #f))
  105. (let* ((drv (if cross?
  106. (package-cross-derivation store package target system
  107. #:graft? #f)
  108. (package-derivation store package system
  109. #:graft? #f)))
  110. (max-silent-time (or (assoc-ref (package-properties package)
  111. 'max-silent-time)
  112. 3600))
  113. (timeout (or (assoc-ref (package-properties package)
  114. 'timeout)
  115. 72000)))
  116. (derivation->job job-name drv
  117. #:max-silent-time max-silent-time
  118. #:timeout timeout)))))
  119. (define (package-cross-job store job-name package target system)
  120. "Return a job called TARGET.JOB-NAME that cross-builds PACKAGE for TARGET on
  121. SYSTEM."
  122. (let ((name (string-append target "." job-name "." system)))
  123. (package-job store name package system
  124. #:cross? #t
  125. #:target target)))
  126. (define %core-packages
  127. ;; Note: Don't put the '-final' package variants because (1) that's
  128. ;; implicit, and (2) they cannot be cross-built (due to the explicit input
  129. ;; chain.)
  130. (list gcc-7 gcc-8 gcc-9 gcc-10 glibc binutils
  131. gmp mpfr mpc coreutils findutils diffutils patch sed grep
  132. gawk gnu-gettext hello guile-2.0 guile-2.2 zlib gzip xz
  133. %bootstrap-binaries-tarball
  134. %binutils-bootstrap-tarball
  135. (%glibc-bootstrap-tarball)
  136. %gcc-bootstrap-tarball
  137. %guile-bootstrap-tarball
  138. %bootstrap-tarballs))
  139. (define (packages-to-cross-build target)
  140. "Return the list of packages to cross-build for TARGET."
  141. ;; Don't cross-build the bootstrap tarballs for MinGW.
  142. (if (string-contains target "mingw")
  143. (drop-right %core-packages 6)
  144. %core-packages))
  145. (define %cross-targets
  146. '("mips64el-linux-gnu"
  147. "arm-linux-gnueabihf"
  148. "aarch64-linux-gnu"
  149. "powerpc-linux-gnu"
  150. "riscv64-linux-gnu"
  151. "i586-pc-gnu" ;aka. GNU/Hurd
  152. "i686-w64-mingw32"
  153. "x86_64-w64-mingw32"))
  154. (define (cross-jobs store system)
  155. "Return a list of cross-compilation jobs for SYSTEM."
  156. (define (from-32-to-64? target)
  157. ;; Return true if SYSTEM is 32-bit and TARGET is 64-bit. This hack
  158. ;; prevents known-to-fail cross-builds from i686-linux or armhf-linux to
  159. ;; mips64el-linux-gnuabi64.
  160. (and (or (string-prefix? "i686-" system)
  161. (string-prefix? "i586-" system)
  162. (string-prefix? "armhf-" system))
  163. (string-contains target "64"))) ;x86_64, mips64el, aarch64, etc.
  164. (define (same? target)
  165. ;; Return true if SYSTEM and TARGET are the same thing. This is so we
  166. ;; don't try to cross-compile to 'mips64el-linux-gnu' from
  167. ;; 'mips64el-linux'.
  168. (or (string-contains target system)
  169. (and (string-prefix? "armhf" system) ;armhf-linux
  170. (string-prefix? "arm" target)))) ;arm-linux-gnueabihf
  171. (define (pointless? target)
  172. ;; Return #t if it makes no sense to cross-build to TARGET from SYSTEM.
  173. (match system
  174. ((or "x86_64-linux" "i686-linux")
  175. (if (string-contains target "mingw")
  176. (not (string=? "x86_64-linux" system))
  177. #f))
  178. (_
  179. ;; Don't try to cross-compile from non-Intel platforms: this isn't
  180. ;; very useful and these are often brittle configurations.
  181. #t)))
  182. (define (either proc1 proc2 proc3)
  183. (lambda (x)
  184. (or (proc1 x) (proc2 x) (proc3 x))))
  185. (append-map (lambda (target)
  186. (map (lambda (package)
  187. (package-cross-job store (job-name package)
  188. package target system))
  189. (packages-to-cross-build target)))
  190. (remove (either from-32-to-64? same? pointless?)
  191. %cross-targets)))
  192. (define* (guix-jobs store systems #:key source commit)
  193. "Return a list of jobs for Guix itself."
  194. (define build
  195. (primitive-load (string-append source "/build-aux/build-self.scm")))
  196. (map
  197. (lambda (system)
  198. (let ((name (string->symbol
  199. (string-append "guix." system)))
  200. (drv (run-with-store store
  201. (build source #:version commit #:system system
  202. #:pull-version 1
  203. #:guile-version "2.2"))))
  204. (derivation->job name drv)))
  205. systems))
  206. ;; Architectures that are able to build or cross-build Guix System images.
  207. ;; This does not mean that other architectures are not supported, only that
  208. ;; they are often not fast enough to support Guix System images building.
  209. (define %guix-system-supported-systems
  210. '("x86_64-linux" "i686-linux"))
  211. (define %guix-system-images
  212. (list hurd-barebones-qcow2-image
  213. pine64-barebones-raw-image
  214. pinebook-pro-barebones-raw-image
  215. novena-barebones-raw-image))
  216. (define (hours hours)
  217. (* 3600 hours))
  218. (define (image-jobs store system)
  219. "Return a list of jobs that build images for SYSTEM. Those jobs are
  220. expensive in storage and I/O operations, hence their periodicity is limited by
  221. passing the PERIOD argument."
  222. (define (->job name drv)
  223. (let ((name (string-append name "." system)))
  224. (parameterize ((%graft? #f))
  225. (derivation->job name drv
  226. #:period (hours 48)))))
  227. (define (build-image image)
  228. (run-with-store store
  229. (mbegin %store-monad
  230. (set-guile-for-build (default-guile))
  231. (lower-object (system-image image)))))
  232. (define MiB
  233. (expt 2 20))
  234. (if (member system %guix-system-supported-systems)
  235. `(,(->job "usb-image"
  236. (build-image
  237. (image
  238. (inherit efi-disk-image)
  239. (operating-system installation-os))))
  240. ,(->job "iso9660-image"
  241. (build-image
  242. (image
  243. (inherit (image-with-label
  244. iso9660-image
  245. (string-append "GUIX_" system "_"
  246. (if (> (string-length %guix-version) 7)
  247. (substring %guix-version 0 7)
  248. %guix-version))))
  249. (operating-system installation-os))))
  250. ;; Only cross-compile Guix System images from x86_64-linux for now.
  251. ,@(if (string=? system "x86_64-linux")
  252. (map (lambda (image)
  253. (->job (symbol->string (image-name image))
  254. (build-image image)))
  255. %guix-system-images)
  256. '()))
  257. '()))
  258. (define channel-build-system
  259. ;; Build system used to "convert" a channel instance to a package.
  260. (let* ((build (lambda* (store name inputs
  261. #:key source commit system
  262. #:allow-other-keys)
  263. (run-with-store store
  264. ;; SOURCE can be a lowerable object such as <local-file>
  265. ;; or a file name. Adjust accordingly.
  266. (mlet* %store-monad ((source (if (string? source)
  267. (return source)
  268. (lower-object source)))
  269. (instance
  270. -> (checkout->channel-instance
  271. source #:commit commit)))
  272. (channel-instances->derivation (list instance)))
  273. #:system system)))
  274. (lower (lambda* (name #:key system source commit
  275. #:allow-other-keys)
  276. (bag
  277. (name name)
  278. (system system)
  279. (build build)
  280. (arguments `(#:source ,source
  281. #:commit ,commit))))))
  282. (build-system (name 'channel)
  283. (description "Turn a channel instance into a package.")
  284. (lower lower))))
  285. (define* (channel-source->package source #:key commit)
  286. "Return a package for the given channel SOURCE, a lowerable object."
  287. (package
  288. (inherit guix)
  289. (version (string-append (package-version guix) "+"))
  290. (build-system channel-build-system)
  291. (arguments `(#:source ,source
  292. #:commit ,commit))
  293. (inputs '())
  294. (native-inputs '())
  295. (propagated-inputs '())))
  296. (define* (system-test-jobs store system
  297. #:key source commit)
  298. "Return a list of jobs for the system tests."
  299. (define (->job test)
  300. (parameterize ((current-guix-package
  301. (channel-source->package source #:commit commit)))
  302. (let ((name (string-append "test." (system-test-name test)
  303. "." system))
  304. (drv (run-with-store store
  305. (mbegin %store-monad
  306. (set-current-system system)
  307. (set-grafting #f)
  308. (set-guile-for-build (default-guile))
  309. (system-test-value test)))))
  310. ;; Those tests are extremely expensive in I/O operations and storage
  311. ;; size, use the "period" attribute to run them with a period of at
  312. ;; least 48 hours.
  313. (derivation->job name drv
  314. #:period (hours 24)))))
  315. (if (member system %guix-system-supported-systems)
  316. ;; Override the value of 'current-guix' used by system tests. Using a
  317. ;; channel instance makes tests that rely on 'current-guix' less
  318. ;; expensive. It also makes sure we get a valid Guix package when this
  319. ;; code is not running from a checkout.
  320. (map ->job (all-system-tests))
  321. '()))
  322. (define (tarball-jobs store system)
  323. "Return jobs to build the self-contained Guix binary tarball."
  324. (define (->job name drv)
  325. (let ((name (string-append name "." system)))
  326. (parameterize ((%graft? #f))
  327. (derivation->job name drv
  328. #:period (hours 24)))))
  329. ;; XXX: Add a job for the stable Guix?
  330. (list
  331. (->job "binary-tarball"
  332. (run-with-store store
  333. (mbegin %store-monad
  334. (set-guile-for-build (default-guile))
  335. (>>= (profile-derivation (packages->manifest (list guix)))
  336. (lambda (profile)
  337. (self-contained-tarball "guix-binary" profile
  338. #:localstatedir? #t
  339. #:compressor
  340. (lookup-compressor "xz")))))
  341. #:system system))))
  342. (define job-name
  343. ;; Return the name of a package's job.
  344. package-name)
  345. (define package->job
  346. (let ((base-packages
  347. (delete-duplicates
  348. (append-map (match-lambda
  349. ((_ package _ ...)
  350. (match (package-transitive-inputs package)
  351. (((_ inputs _ ...) ...)
  352. inputs))))
  353. (%final-inputs)))))
  354. (lambda (store package system)
  355. "Return a job for PACKAGE on SYSTEM, or #f if this combination is not
  356. valid."
  357. (cond ((member package base-packages)
  358. (package-job store (string-append "base." (job-name package))
  359. package system))
  360. ((supported-package? package system)
  361. (let ((drv (package-derivation store package system
  362. #:graft? #f)))
  363. (and (substitutable-derivation? drv)
  364. (package-job store (job-name package)
  365. package system))))
  366. (else
  367. #f)))))
  368. (define (all-packages)
  369. "Return the list of packages to build."
  370. (define (adjust package result)
  371. (cond ((package-replacement package)
  372. ;; XXX: If PACKAGE and its replacement have the same name/version,
  373. ;; then both Cuirass jobs will have the same name, which
  374. ;; effectively means that the second one will be ignored. Thus,
  375. ;; return the replacement first.
  376. (cons* (package-replacement package) ;build both
  377. package
  378. result))
  379. ((package-superseded package)
  380. result) ;don't build it
  381. (else
  382. (cons package result))))
  383. (fold-packages adjust
  384. (fold adjust '() ;include base packages
  385. (match (%final-inputs)
  386. (((labels packages _ ...) ...)
  387. packages)))
  388. #:select? (const #t))) ;include hidden packages
  389. (define (arguments->manifests arguments channels)
  390. "Return the list of manifests extracted from ARGUMENTS."
  391. (map (lambda (manifest)
  392. (any (lambda (checkout)
  393. (let ((path (in-vicinity checkout manifest)))
  394. (and (file-exists? path)
  395. path)))
  396. (map channel-url channels)))
  397. arguments))
  398. (define (manifests->packages store manifests)
  399. "Return the list of packages found in MANIFESTS."
  400. (define (load-manifest manifest)
  401. (save-module-excursion
  402. (lambda ()
  403. (set-current-module (make-user-module '((guix profiles) (gnu))))
  404. (primitive-load manifest))))
  405. (delete-duplicates!
  406. (map manifest-entry-item
  407. (append-map (compose manifest-entries
  408. load-manifest)
  409. manifests))))
  410. ;;;
  411. ;;; Cuirass entry point.
  412. ;;;
  413. (define (cuirass-jobs store arguments)
  414. "Register Cuirass jobs."
  415. (define subset
  416. (assoc-ref arguments 'subset))
  417. (define systems
  418. (match (assoc-ref arguments 'systems)
  419. (#f %cuirass-supported-systems)
  420. ((lst ...) lst)
  421. ((? string? str) (call-with-input-string str read))))
  422. (define channels
  423. (let ((channels (assq-ref arguments 'channels)))
  424. (map sexp->channel channels)))
  425. (define guix
  426. (find guix-channel? channels))
  427. (define commit
  428. (channel-commit guix))
  429. (define source
  430. (channel-url guix))
  431. ;; Turn off grafts. Grafting is meant to happen on the user's machines.
  432. (parameterize ((%graft? #f))
  433. ;; Return one job for each package, except bootstrap packages.
  434. (append-map
  435. (lambda (system)
  436. (format (current-error-port)
  437. "evaluating for '~a' (heap size: ~a MiB)...~%"
  438. system
  439. (round
  440. (/ (assoc-ref (gc-stats) 'heap-size)
  441. (expt 2. 20))))
  442. (invalidate-derivation-caches!)
  443. (match subset
  444. ('all
  445. ;; Build everything, including replacements.
  446. (let ((all (all-packages))
  447. (job (lambda (package)
  448. (package->job store package system))))
  449. (append
  450. (filter-map job all)
  451. (image-jobs store system)
  452. (system-test-jobs store system
  453. #:source source
  454. #:commit commit)
  455. (tarball-jobs store system)
  456. (cross-jobs store system))))
  457. ('core
  458. ;; Build core packages only.
  459. (append
  460. (map (lambda (package)
  461. (package-job store (job-name package)
  462. package system))
  463. %core-packages)
  464. (cross-jobs store system)))
  465. ('guix
  466. ;; Build Guix modules only.
  467. (guix-jobs store systems
  468. #:source source
  469. #:commit commit))
  470. ('hello
  471. ;; Build hello package only.
  472. (let ((hello (specification->package "hello")))
  473. (list (package-job store (job-name hello)
  474. hello system))))
  475. (('channels . channels)
  476. ;; Build only the packages from CHANNELS.
  477. (let ((all (all-packages)))
  478. (filter-map
  479. (lambda (package)
  480. (any (lambda (channel)
  481. (and (member (channel-name channel) channels)
  482. (package->job store package system)))
  483. (package-channels package)))
  484. all)))
  485. (('packages . rest)
  486. ;; Build selected list of packages only.
  487. (let ((packages (map specification->package rest)))
  488. (map (lambda (package)
  489. (package-job store (job-name package)
  490. package system))
  491. packages)))
  492. (('manifests . rest)
  493. ;; Build packages in the list of manifests.
  494. (let* ((manifests (arguments->manifests rest channels))
  495. (packages (manifests->packages store manifests)))
  496. (map (lambda (package)
  497. (package-job store (job-name package)
  498. package system))
  499. packages)))
  500. (else
  501. (error "unknown subset" subset))))
  502. systems)))