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. %bootstrap-packages
  71. %core-packages
  72. %cross-targets
  73. channel-source->package
  74. arguments->systems
  75. cuirass-jobs))
  76. ;;; Commentary:
  77. ;;;
  78. ;;; This file defines build jobs for Cuirass.
  79. ;;;
  80. ;;; Code:
  81. (define* (derivation->job name drv
  82. #:key
  83. (max-silent-time 3600)
  84. (timeout (* 5 3600)))
  85. "Return a Cuirass job called NAME and describing DRV.
  86. MAX-SILENT-TIME and TIMEOUT are build options passed to the daemon when
  87. building the derivation."
  88. `((#:job-name . ,name)
  89. (#:derivation . ,(derivation-file-name drv))
  90. (#:inputs . ,(map (compose derivation-file-name
  91. derivation-input-derivation)
  92. (derivation-inputs drv)))
  93. (#:outputs . ,(filter-map
  94. (lambda (res)
  95. (match res
  96. ((name . path)
  97. `(,name . ,path))))
  98. (derivation->output-paths drv)))
  99. (#:nix-name . ,(derivation-name drv))
  100. (#:system . ,(derivation-system drv))
  101. (#:max-silent-time . ,max-silent-time)
  102. (#:timeout . ,timeout)))
  103. (define* (package-job store job-name package system
  104. #:key cross? target)
  105. "Return a job called JOB-NAME that builds PACKAGE on SYSTEM."
  106. (let ((job-name (string-append job-name "." system)))
  107. (parameterize ((%graft? #f))
  108. (let* ((drv (if cross?
  109. (package-cross-derivation store package target system
  110. #:graft? #f)
  111. (package-derivation store package system
  112. #:graft? #f)))
  113. (max-silent-time (or (assoc-ref (package-properties package)
  114. 'max-silent-time)
  115. 3600))
  116. (timeout (or (assoc-ref (package-properties package)
  117. 'timeout)
  118. 72000)))
  119. (derivation->job job-name drv
  120. #:max-silent-time max-silent-time
  121. #:timeout timeout)))))
  122. (define (package-cross-job store job-name package target system)
  123. "Return a job called TARGET.JOB-NAME that cross-builds PACKAGE for TARGET on
  124. SYSTEM."
  125. (let ((name (string-append target "." job-name "." system)))
  126. (package-job store name package system
  127. #:cross? #t
  128. #:target target)))
  129. (define %core-packages
  130. ;; Note: Don't put the '-final' package variants because (1) that's
  131. ;; implicit, and (2) they cannot be cross-built (due to the explicit input
  132. ;; chain.)
  133. (list gcc-7 gcc-8 gcc-9 gcc-10 glibc binutils
  134. gmp mpfr mpc coreutils findutils diffutils patch sed grep
  135. gawk gnu-gettext hello guile-2.0 guile-2.2 zlib gzip xz
  136. %bootstrap-binaries-tarball
  137. %binutils-bootstrap-tarball
  138. (%glibc-bootstrap-tarball)
  139. %gcc-bootstrap-tarball
  140. %guile-bootstrap-tarball
  141. %bootstrap-tarballs))
  142. (define %bootstrap-packages
  143. ;; Return the list of bootstrap packages from the commencement module.
  144. (filter package?
  145. (module-map
  146. (lambda (sym var)
  147. (variable-ref var))
  148. (resolve-module '(gnu packages commencement)))))
  149. (define (packages-to-cross-build target)
  150. "Return the list of packages to cross-build for TARGET."
  151. ;; Don't cross-build the bootstrap tarballs for MinGW.
  152. (if (string-contains target "mingw")
  153. (drop-right %core-packages 6)
  154. %core-packages))
  155. (define %cross-targets
  156. '("mips64el-linux-gnu"
  157. "arm-linux-gnueabihf"
  158. "aarch64-linux-gnu"
  159. "powerpc-linux-gnu"
  160. "powerpc64le-linux-gnu"
  161. "riscv64-linux-gnu"
  162. "i586-pc-gnu" ;aka. GNU/Hurd
  163. "i686-w64-mingw32"
  164. "x86_64-w64-mingw32"))
  165. (define (cross-jobs store system)
  166. "Return a list of cross-compilation jobs for SYSTEM."
  167. (define (from-32-to-64? target)
  168. ;; Return true if SYSTEM is 32-bit and TARGET is 64-bit. This hack
  169. ;; prevents known-to-fail cross-builds from i686-linux or armhf-linux to
  170. ;; mips64el-linux-gnuabi64.
  171. (and (or (string-prefix? "i686-" system)
  172. (string-prefix? "i586-" system)
  173. (string-prefix? "armhf-" system))
  174. (string-contains target "64"))) ;x86_64, mips64el, aarch64, etc.
  175. (define (same? target)
  176. ;; Return true if SYSTEM and TARGET are the same thing. This is so we
  177. ;; don't try to cross-compile to 'mips64el-linux-gnu' from
  178. ;; 'mips64el-linux'.
  179. (or (string-contains target system)
  180. (and (string-prefix? "armhf" system) ;armhf-linux
  181. (string-prefix? "arm" target)))) ;arm-linux-gnueabihf
  182. (define (pointless? target)
  183. ;; Return #t if it makes no sense to cross-build to TARGET from SYSTEM.
  184. (match system
  185. ((or "x86_64-linux" "i686-linux")
  186. (if (string-contains target "mingw")
  187. (not (string=? "x86_64-linux" system))
  188. #f))
  189. (_
  190. ;; Don't try to cross-compile from non-Intel platforms: this isn't
  191. ;; very useful and these are often brittle configurations.
  192. #t)))
  193. (define (either proc1 proc2 proc3)
  194. (lambda (x)
  195. (or (proc1 x) (proc2 x) (proc3 x))))
  196. (append-map (lambda (target)
  197. (map (lambda (package)
  198. (package-cross-job store (job-name package)
  199. package target system))
  200. (packages-to-cross-build target)))
  201. (remove (either from-32-to-64? same? pointless?)
  202. %cross-targets)))
  203. (define* (guix-jobs store systems #:key source commit)
  204. "Return a list of jobs for Guix itself."
  205. (define build
  206. (primitive-load (string-append source "/build-aux/build-self.scm")))
  207. (map
  208. (lambda (system)
  209. (let ((name (string->symbol
  210. (string-append "guix." system)))
  211. (drv (run-with-store store
  212. (build source #:version commit #:system system
  213. #:pull-version 1
  214. #:guile-version "2.2"))))
  215. (derivation->job name drv)))
  216. systems))
  217. ;; Architectures that are able to build or cross-build Guix System images.
  218. ;; This does not mean that other architectures are not supported, only that
  219. ;; they are often not fast enough to support Guix System images building.
  220. (define %guix-system-supported-systems
  221. '("x86_64-linux" "i686-linux"))
  222. (define %guix-system-images
  223. (list hurd-barebones-qcow2-image
  224. pine64-barebones-raw-image
  225. pinebook-pro-barebones-raw-image
  226. novena-barebones-raw-image))
  227. (define (hours hours)
  228. (* 3600 hours))
  229. (define* (image->job store image
  230. #:key name system)
  231. "Return the job for IMAGE on SYSTEM. If NAME is passed, use it as job name,
  232. otherwise use the IMAGE name."
  233. (let* ((image-name (or name
  234. (symbol->string (image-name image))))
  235. (name (string-append image-name "." system))
  236. (drv (run-with-store store
  237. (mbegin %store-monad
  238. (set-guile-for-build (default-guile))
  239. (lower-object (system-image image))))))
  240. (parameterize ((%graft? #f))
  241. (derivation->job name drv))))
  242. (define (image-jobs store system)
  243. "Return a list of jobs that build images for SYSTEM."
  244. (define MiB
  245. (expt 2 20))
  246. (if (member system %guix-system-supported-systems)
  247. `(,(image->job store
  248. (image
  249. (inherit efi-disk-image)
  250. (operating-system installation-os))
  251. #:name "usb-image"
  252. #:system system)
  253. ,(image->job
  254. store
  255. (image
  256. (inherit (image-with-label
  257. iso9660-image
  258. (string-append "GUIX_" system "_"
  259. (if (> (string-length %guix-version) 7)
  260. (substring %guix-version 0 7)
  261. %guix-version))))
  262. (operating-system installation-os))
  263. #:name "iso9660-image"
  264. #:system system)
  265. ;; Only cross-compile Guix System images from x86_64-linux for now.
  266. ,@(if (string=? system "x86_64-linux")
  267. (map (cut image->job store <>
  268. #:system system)
  269. %guix-system-images)
  270. '()))
  271. '()))
  272. (define channel-build-system
  273. ;; Build system used to "convert" a channel instance to a package.
  274. (let* ((build (lambda* (store name inputs
  275. #:key source commit system
  276. #:allow-other-keys)
  277. (run-with-store store
  278. ;; SOURCE can be a lowerable object such as <local-file>
  279. ;; or a file name. Adjust accordingly.
  280. (mlet* %store-monad ((source (if (string? source)
  281. (return source)
  282. (lower-object source)))
  283. (instance
  284. -> (checkout->channel-instance
  285. source #:commit commit)))
  286. (channel-instances->derivation (list instance)))
  287. #:system system)))
  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 %bootstrap-packages %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)))