ci.scm 21 KB

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