ci.scm 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584
  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, 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 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* (store name inputs
  279. #:key source commit system
  280. #:allow-other-keys)
  281. (run-with-store store
  282. ;; SOURCE can be a lowerable object such as <local-file>
  283. ;; or a file name. Adjust accordingly.
  284. (mlet* %store-monad ((source (if (string? source)
  285. (return source)
  286. (lower-object source)))
  287. (instance
  288. -> (checkout->channel-instance
  289. source #:commit commit)))
  290. (channel-instances->derivation (list instance)))
  291. #:system system)))
  292. (lower (lambda* (name #:key system source commit
  293. #:allow-other-keys)
  294. (bag
  295. (name name)
  296. (system system)
  297. (build build)
  298. (arguments `(#:source ,source
  299. #:commit ,commit))))))
  300. (build-system (name 'channel)
  301. (description "Turn a channel instance into a package.")
  302. (lower lower))))
  303. (define* (channel-source->package source #:key commit)
  304. "Return a package for the given channel SOURCE, a lowerable object."
  305. (package
  306. (inherit guix)
  307. (version (string-append (package-version guix) "+"))
  308. (build-system channel-build-system)
  309. (arguments `(#:source ,source
  310. #:commit ,commit))
  311. (inputs '())
  312. (native-inputs '())
  313. (propagated-inputs '())))
  314. (define* (system-test-jobs store system
  315. #:key source commit)
  316. "Return a list of jobs for the system tests."
  317. (define (->job test)
  318. (let ((name (string-append "test." (system-test-name test)
  319. "." system))
  320. (drv (run-with-store store
  321. (mbegin %store-monad
  322. (set-current-system system)
  323. (set-grafting #f)
  324. (set-guile-for-build (default-guile))
  325. (system-test-value test)))))
  326. (derivation->job name drv)))
  327. (if (member system %guix-system-supported-systems)
  328. ;; Override the value of 'current-guix' used by system tests. Using a
  329. ;; channel instance makes tests that rely on 'current-guix' less
  330. ;; expensive. It also makes sure we get a valid Guix package when this
  331. ;; code is not running from a checkout.
  332. (parameterize ((current-guix-package
  333. (channel-source->package source #:commit commit)))
  334. (map ->job (all-system-tests)))
  335. '()))
  336. (define (tarball-jobs store system)
  337. "Return jobs to build the self-contained Guix binary tarball."
  338. (define (->job name drv)
  339. (let ((name (string-append name "." system)))
  340. (parameterize ((%graft? #f))
  341. (derivation->job name drv))))
  342. ;; XXX: Add a job for the stable Guix?
  343. (list
  344. (->job "binary-tarball"
  345. (run-with-store store
  346. (mbegin %store-monad
  347. (set-guile-for-build (default-guile))
  348. (>>= (profile-derivation (packages->manifest (list guix)))
  349. (lambda (profile)
  350. (self-contained-tarball "guix-binary" profile
  351. #:profile-name "current-guix"
  352. #:localstatedir? #t
  353. #:compressor
  354. (lookup-compressor "xz")))))
  355. #:system system))))
  356. (define job-name
  357. ;; Return the name of a package's job.
  358. package-name)
  359. (define package->job
  360. (let ((base-packages
  361. (delete-duplicates
  362. (append-map (match-lambda
  363. ((_ package _ ...)
  364. (match (package-transitive-inputs package)
  365. (((_ inputs _ ...) ...)
  366. inputs))))
  367. (%final-inputs)))))
  368. (lambda (store package system)
  369. "Return a job for PACKAGE on SYSTEM, or #f if this combination is not
  370. valid."
  371. (cond ((member package base-packages)
  372. (package-job store (string-append "base." (job-name package))
  373. package system))
  374. ((supported-package? package system)
  375. (let ((drv (package-derivation store package system
  376. #:graft? #f)))
  377. (and (substitutable-derivation? drv)
  378. (package-job store (job-name package)
  379. package system))))
  380. (else
  381. #f)))))
  382. (define (all-packages)
  383. "Return the list of packages to build."
  384. (define (adjust package result)
  385. (cond ((package-replacement package)
  386. ;; XXX: If PACKAGE and its replacement have the same name/version,
  387. ;; then both Cuirass jobs will have the same name, which
  388. ;; effectively means that the second one will be ignored. Thus,
  389. ;; return the replacement first.
  390. (cons* (package-replacement package) ;build both
  391. package
  392. result))
  393. ((package-superseded package)
  394. result) ;don't build it
  395. (else
  396. (cons package result))))
  397. (fold-packages adjust
  398. (fold adjust '() ;include base packages
  399. (match (%final-inputs)
  400. (((labels packages _ ...) ...)
  401. packages)))
  402. #:select? (const #t))) ;include hidden packages
  403. (define (arguments->manifests arguments channels)
  404. "Return the list of manifests extracted from ARGUMENTS."
  405. (map (lambda (manifest)
  406. (any (lambda (checkout)
  407. (let ((path (in-vicinity checkout manifest)))
  408. (and (file-exists? path)
  409. path)))
  410. (map channel-url channels)))
  411. arguments))
  412. (define (manifests->packages store manifests)
  413. "Return the list of packages found in MANIFESTS."
  414. (define (load-manifest manifest)
  415. (save-module-excursion
  416. (lambda ()
  417. (set-current-module (make-user-module '((guix profiles) (gnu))))
  418. (primitive-load manifest))))
  419. (delete-duplicates!
  420. (map manifest-entry-item
  421. (append-map (compose manifest-entries
  422. load-manifest)
  423. manifests))))
  424. (define (arguments->systems arguments)
  425. "Return the systems list from ARGUMENTS."
  426. (match (assoc-ref arguments 'systems)
  427. (#f %cuirass-supported-systems)
  428. ((lst ...) lst)
  429. ((? string? str) (call-with-input-string str read))))
  430. ;;;
  431. ;;; Cuirass entry point.
  432. ;;;
  433. (define (cuirass-jobs store arguments)
  434. "Register Cuirass jobs."
  435. (define subset
  436. (assoc-ref arguments 'subset))
  437. (define systems
  438. (arguments->systems arguments))
  439. (define channels
  440. (let ((channels (assq-ref arguments 'channels)))
  441. (map sexp->channel channels)))
  442. (define guix
  443. (find guix-channel? channels))
  444. (define commit
  445. (channel-commit guix))
  446. (define source
  447. (channel-url guix))
  448. ;; Turn off grafts. Grafting is meant to happen on the user's machines.
  449. (parameterize ((%graft? #f))
  450. ;; Return one job for each package, except bootstrap packages.
  451. (append-map
  452. (lambda (system)
  453. (format (current-error-port)
  454. "evaluating for '~a' (heap size: ~a MiB)...~%"
  455. system
  456. (round
  457. (/ (assoc-ref (gc-stats) 'heap-size)
  458. (expt 2. 20))))
  459. (invalidate-derivation-caches!)
  460. (match subset
  461. ('all
  462. ;; Build everything, including replacements.
  463. (let ((all (all-packages))
  464. (job (lambda (package)
  465. (package->job store package system))))
  466. (append
  467. (filter-map job all)
  468. (cross-jobs store system))))
  469. ('core
  470. ;; Build core packages only.
  471. (append
  472. (map (lambda (package)
  473. (package-job store (job-name package)
  474. package system))
  475. (append (commencement-packages system) %core-packages))
  476. (cross-jobs store system)))
  477. ('guix
  478. ;; Build Guix modules only.
  479. (guix-jobs store systems
  480. #:source source
  481. #:commit commit))
  482. ('hello
  483. ;; Build hello package only.
  484. (let ((hello (specification->package "hello")))
  485. (list (package-job store (job-name hello)
  486. hello system))))
  487. ('images
  488. ;; Build Guix System images only.
  489. (image-jobs store system))
  490. ('system-tests
  491. ;; Build Guix System tests only.
  492. (system-test-jobs store system
  493. #:source source
  494. #:commit commit))
  495. ('tarball
  496. ;; Build Guix tarball only.
  497. (tarball-jobs store system))
  498. (('custom . modules)
  499. ;; Build custom modules jobs only.
  500. (append-map
  501. (lambda (module)
  502. (let ((proc (module-ref
  503. (resolve-interface module)
  504. 'cuirass-jobs)))
  505. (proc store arguments)))
  506. modules))
  507. (('channels . channels)
  508. ;; Build only the packages from CHANNELS.
  509. (let ((all (all-packages)))
  510. (filter-map
  511. (lambda (package)
  512. (any (lambda (channel)
  513. (and (member (channel-name channel) channels)
  514. (package->job store package system)))
  515. (package-channels package)))
  516. all)))
  517. (('packages . rest)
  518. ;; Build selected list of packages only.
  519. (let ((packages (map specification->package rest)))
  520. (map (lambda (package)
  521. (package-job store (job-name package)
  522. package system))
  523. packages)))
  524. (('manifests . rest)
  525. ;; Build packages in the list of manifests.
  526. (let* ((manifests (arguments->manifests rest channels))
  527. (packages (manifests->packages store manifests)))
  528. (map (lambda (package)
  529. (package-job store (job-name package)
  530. package system))
  531. packages)))
  532. (else
  533. (error "unknown subset" subset))))
  534. systems)))