ci.scm 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622
  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. #:autoload (guix transformations) (tunable-package? tuned-package)
  31. #:use-module (guix channels)
  32. #:use-module (guix config)
  33. #:use-module (guix derivations)
  34. #:use-module (guix build-system)
  35. #:use-module (guix monads)
  36. #:use-module (guix gexp)
  37. #:use-module (guix ui)
  38. #:use-module ((guix licenses)
  39. #:select (gpl3+ license? license-name))
  40. #:use-module ((guix utils) #:select (%current-system))
  41. #:use-module ((guix scripts system) #:select (read-operating-system))
  42. #:use-module ((guix scripts pack)
  43. #:select (lookup-compressor self-contained-tarball))
  44. #:use-module (gnu bootloader)
  45. #:use-module (gnu bootloader u-boot)
  46. #:use-module (gnu image)
  47. #:use-module (gnu packages)
  48. #:use-module (gnu packages gcc)
  49. #:use-module (gnu packages base)
  50. #:use-module (gnu packages gawk)
  51. #:use-module (gnu packages guile)
  52. #:use-module (gnu packages gettext)
  53. #:use-module (gnu packages compression)
  54. #:use-module (gnu packages multiprecision)
  55. #:use-module (gnu packages make-bootstrap)
  56. #:use-module (gnu packages package-management)
  57. #:use-module (gnu system)
  58. #:use-module (gnu system image)
  59. #:use-module (gnu system vm)
  60. #:use-module (gnu system install)
  61. #:use-module (gnu system images hurd)
  62. #:use-module (gnu system images novena)
  63. #:use-module (gnu system images pine64)
  64. #:use-module (gnu system images pinebook-pro)
  65. #:use-module (gnu tests)
  66. #:use-module (srfi srfi-1)
  67. #:use-module (srfi srfi-26)
  68. #:use-module (ice-9 match)
  69. #:export (derivation->job
  70. image->job
  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 (suffix ""))
  105. "Return a job called JOB-NAME that builds PACKAGE on SYSTEM."
  106. (let ((job-name (string-append job-name "." system suffix)))
  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)))
  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-8 gcc-9 gcc-10 gcc-11 glibc binutils
  134. gmp mpfr mpc coreutils findutils diffutils patch sed grep
  135. gawk gnu-gettext hello guile-2.2 guile-3.0 zlib gzip xz guix
  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 (commencement-packages system)
  143. "Return the list of bootstrap packages from the commencement module for
  144. SYSTEM."
  145. ;; Only include packages supported on SYSTEM. For example, the Mes
  146. ;; bootstrap graph is currently not supported on ARM so it should be
  147. ;; excluded.
  148. (filter (lambda (obj)
  149. (and (package? obj)
  150. (supported-package? obj system)))
  151. (module-map (lambda (sym var)
  152. (variable-ref var))
  153. (resolve-module '(gnu packages commencement)))))
  154. (define (packages-to-cross-build target)
  155. "Return the list of packages to cross-build for TARGET."
  156. ;; Don't cross-build the bootstrap tarballs for MinGW.
  157. (if (string-contains target "mingw")
  158. (drop-right %core-packages 6)
  159. %core-packages))
  160. (define %cross-targets
  161. '("mips64el-linux-gnu"
  162. "arm-linux-gnueabihf"
  163. "aarch64-linux-gnu"
  164. "powerpc-linux-gnu"
  165. "powerpc64le-linux-gnu"
  166. "riscv64-linux-gnu"
  167. "i586-pc-gnu" ;aka. GNU/Hurd
  168. "i686-w64-mingw32"
  169. "x86_64-w64-mingw32"))
  170. (define (cross-jobs store system)
  171. "Return a list of cross-compilation jobs for SYSTEM."
  172. (define (from-32-to-64? target)
  173. ;; Return true if SYSTEM is 32-bit and TARGET is 64-bit. This hack
  174. ;; prevents known-to-fail cross-builds from i686-linux or armhf-linux to
  175. ;; mips64el-linux-gnuabi64.
  176. (and (or (string-prefix? "i686-" system)
  177. (string-prefix? "i586-" system)
  178. (string-prefix? "armhf-" system))
  179. (string-contains target "64"))) ;x86_64, mips64el, aarch64, etc.
  180. (define (same? target)
  181. ;; Return true if SYSTEM and TARGET are the same thing. This is so we
  182. ;; don't try to cross-compile to 'mips64el-linux-gnu' from
  183. ;; 'mips64el-linux'.
  184. (or (string-contains target system)
  185. (and (string-prefix? "armhf" system) ;armhf-linux
  186. (string-prefix? "arm" target)))) ;arm-linux-gnueabihf
  187. (define (pointless? target)
  188. ;; Return #t if it makes no sense to cross-build to TARGET from SYSTEM.
  189. (match system
  190. ((or "x86_64-linux" "i686-linux")
  191. (if (string-contains target "mingw")
  192. (not (string=? "x86_64-linux" system))
  193. #f))
  194. (_
  195. ;; Don't try to cross-compile from non-Intel platforms: this isn't
  196. ;; very useful and these are often brittle configurations.
  197. #t)))
  198. (define (either proc1 proc2 proc3)
  199. (lambda (x)
  200. (or (proc1 x) (proc2 x) (proc3 x))))
  201. (append-map (lambda (target)
  202. (map (lambda (package)
  203. (package-cross-job store (job-name package)
  204. package target system))
  205. (packages-to-cross-build target)))
  206. (remove (either from-32-to-64? same? pointless?)
  207. %cross-targets)))
  208. (define* (guix-jobs store systems #:key source commit)
  209. "Return a list of jobs for Guix itself."
  210. (define build
  211. (primitive-load (string-append source "/build-aux/build-self.scm")))
  212. (map
  213. (lambda (system)
  214. (let ((name (string->symbol
  215. (string-append "guix." system)))
  216. (drv (run-with-store store
  217. (build source #:version commit #:system system
  218. #:pull-version 1
  219. #:guile-version "2.2"))))
  220. (derivation->job name drv)))
  221. systems))
  222. ;; Architectures that are able to build or cross-build Guix System images.
  223. ;; This does not mean that other architectures are not supported, only that
  224. ;; they are often not fast enough to support Guix System images building.
  225. (define %guix-system-supported-systems
  226. '("x86_64-linux" "i686-linux"))
  227. (define %guix-system-images
  228. (list hurd-barebones-qcow2-image
  229. pine64-barebones-raw-image
  230. pinebook-pro-barebones-raw-image
  231. novena-barebones-raw-image))
  232. (define (hours hours)
  233. (* 3600 hours))
  234. (define* (image->job store image
  235. #:key name system)
  236. "Return the job for IMAGE on SYSTEM. If NAME is passed, use it as job name,
  237. otherwise use the IMAGE name."
  238. (let* ((image-name (or name
  239. (symbol->string (image-name image))))
  240. (name (string-append image-name "." system))
  241. (drv (run-with-store store
  242. (mbegin %store-monad
  243. (set-guile-for-build (default-guile))
  244. (lower-object (system-image image))))))
  245. (parameterize ((%graft? #f))
  246. (derivation->job name drv))))
  247. (define (image-jobs store system)
  248. "Return a list of jobs that build images for SYSTEM."
  249. (define MiB
  250. (expt 2 20))
  251. (if (member system %guix-system-supported-systems)
  252. `(,(image->job store
  253. (image
  254. (inherit efi-disk-image)
  255. (operating-system installation-os))
  256. #:name "usb-image"
  257. #:system system)
  258. ,(image->job
  259. store
  260. (image
  261. (inherit (image-with-label
  262. iso9660-image
  263. (string-append "GUIX_" system "_"
  264. (if (> (string-length %guix-version) 7)
  265. (substring %guix-version 0 7)
  266. %guix-version))))
  267. (operating-system installation-os))
  268. #:name "iso9660-image"
  269. #:system system)
  270. ;; Only cross-compile Guix System images from x86_64-linux for now.
  271. ,@(if (string=? system "x86_64-linux")
  272. (map (cut image->job store <>
  273. #:system system)
  274. %guix-system-images)
  275. '()))
  276. '()))
  277. (define channel-build-system
  278. ;; Build system used to "convert" a channel instance to a package.
  279. (let* ((build (lambda* (name inputs
  280. #:key source commit system
  281. #:allow-other-keys)
  282. (mlet* %store-monad ((source (if (string? source)
  283. (return source)
  284. (lower-object source)))
  285. (instance
  286. -> (checkout->channel-instance
  287. source #:commit commit)))
  288. (channel-instances->derivation (list instance)))))
  289. (lower (lambda* (name #:key system source commit
  290. #:allow-other-keys)
  291. (bag
  292. (name name)
  293. (system system)
  294. (build build)
  295. (arguments `(#:source ,source
  296. #:commit ,commit))))))
  297. (build-system (name 'channel)
  298. (description "Turn a channel instance into a package.")
  299. (lower lower))))
  300. (define* (channel-source->package source #:key commit)
  301. "Return a package for the given channel SOURCE, a lowerable object."
  302. (package
  303. (inherit guix)
  304. (version (string-append (package-version guix) "+"))
  305. (build-system channel-build-system)
  306. (arguments `(#:source ,source
  307. #:commit ,commit))
  308. (inputs '())
  309. (native-inputs '())
  310. (propagated-inputs '())))
  311. (define* (system-test-jobs store system
  312. #:key source commit)
  313. "Return a list of jobs for the system tests."
  314. (define (->job test)
  315. (let ((name (string-append "test." (system-test-name test)
  316. "." system))
  317. (drv (run-with-store store
  318. (mbegin %store-monad
  319. (set-current-system system)
  320. (set-grafting #f)
  321. (set-guile-for-build (default-guile))
  322. (system-test-value test)))))
  323. (derivation->job name drv)))
  324. (if (member system %guix-system-supported-systems)
  325. ;; Override the value of 'current-guix' used by system tests. Using a
  326. ;; channel instance makes tests that rely on 'current-guix' less
  327. ;; expensive. It also makes sure we get a valid Guix package when this
  328. ;; code is not running from a checkout.
  329. (parameterize ((current-guix-package
  330. (channel-source->package source #:commit commit)))
  331. (map ->job (all-system-tests)))
  332. '()))
  333. (define (tarball-jobs store system)
  334. "Return jobs to build the self-contained Guix binary tarball."
  335. (define (->job name drv)
  336. (let ((name (string-append name "." system)))
  337. (parameterize ((%graft? #f))
  338. (derivation->job name drv))))
  339. ;; XXX: Add a job for the stable Guix?
  340. (list
  341. (->job "binary-tarball"
  342. (run-with-store store
  343. (mbegin %store-monad
  344. (set-guile-for-build (default-guile))
  345. (>>= (profile-derivation (packages->manifest (list guix)))
  346. (lambda (profile)
  347. (self-contained-tarball "guix-binary" profile
  348. #:profile-name "current-guix"
  349. #:localstatedir? #t
  350. #:compressor
  351. (lookup-compressor "xz")))))
  352. #:system system))))
  353. (define job-name
  354. ;; Return the name of a package's job.
  355. package-name)
  356. (define package->job
  357. (let ((base-packages
  358. (delete-duplicates
  359. (append-map (match-lambda
  360. ((_ package _ ...)
  361. (match (package-transitive-inputs package)
  362. (((_ inputs _ ...) ...)
  363. inputs))))
  364. (%final-inputs)))))
  365. (lambda* (store package system #:key (suffix ""))
  366. "Return a job for PACKAGE on SYSTEM, or #f if this combination is not
  367. valid. Append SUFFIX to the job name."
  368. (cond ((member package base-packages)
  369. (package-job store (string-append "base." (job-name package))
  370. package system #:suffix suffix))
  371. ((supported-package? package system)
  372. (let ((drv (package-derivation store package system
  373. #:graft? #f)))
  374. (and (substitutable-derivation? drv)
  375. (package-job store (job-name package)
  376. package system #:suffix suffix))))
  377. (else
  378. #f)))))
  379. (define %x86-64-micro-architectures
  380. ;; Micro-architectures for which we build tuned variants.
  381. '("westmere" "ivybridge" "haswell" "skylake" "skylake-avx512"))
  382. (define (tuned-package-jobs store package system)
  383. "Return a list of jobs for PACKAGE tuned for SYSTEM's micro-architectures."
  384. (filter-map (lambda (micro-architecture)
  385. (define suffix
  386. (string-append "." micro-architecture))
  387. (package->job store
  388. (tuned-package package micro-architecture)
  389. system
  390. #:suffix suffix))
  391. (match system
  392. ("x86_64-linux" %x86-64-micro-architectures)
  393. (_ '()))))
  394. (define (all-packages)
  395. "Return the list of packages to build."
  396. (define (adjust package result)
  397. (cond ((package-replacement package)
  398. ;; XXX: If PACKAGE and its replacement have the same name/version,
  399. ;; then both Cuirass jobs will have the same name, which
  400. ;; effectively means that the second one will be ignored. Thus,
  401. ;; return the replacement first.
  402. (cons* (package-replacement package) ;build both
  403. package
  404. result))
  405. ((package-superseded package)
  406. result) ;don't build it
  407. (else
  408. (cons package result))))
  409. (fold-packages adjust
  410. (fold adjust '() ;include base packages
  411. (match (%final-inputs)
  412. (((labels packages _ ...) ...)
  413. packages)))
  414. #:select? (const #t))) ;include hidden packages
  415. (define (arguments->manifests arguments channels)
  416. "Return the list of manifests extracted from ARGUMENTS."
  417. (map (lambda (manifest)
  418. (any (lambda (checkout)
  419. (let ((path (in-vicinity checkout manifest)))
  420. (and (file-exists? path)
  421. path)))
  422. (map channel-url channels)))
  423. arguments))
  424. (define (manifests->jobs store manifests)
  425. "Return the list of jobs for the entries in MANIFESTS, a list of file
  426. names."
  427. (define (load-manifest manifest)
  428. (save-module-excursion
  429. (lambda ()
  430. (set-current-module (make-user-module '((guix profiles) (gnu))))
  431. (primitive-load manifest))))
  432. (define (manifest-entry-job-name entry)
  433. (string-append (manifest-entry-name entry) "-"
  434. (manifest-entry-version entry)))
  435. (define (manifest-entry->job entry)
  436. (let* ((obj (manifest-entry-item entry))
  437. (drv (parameterize ((%graft? #f))
  438. (run-with-store store
  439. (lower-object obj))))
  440. (max-silent-time (or (and (package? obj)
  441. (assoc-ref (package-properties obj)
  442. 'max-silent-time))
  443. 3600))
  444. (timeout (or (and (package? obj)
  445. (assoc-ref (package-properties obj) 'timeout))
  446. (* 5 3600))))
  447. (derivation->job (manifest-entry-job-name entry) drv
  448. #:max-silent-time max-silent-time
  449. #:timeout timeout)))
  450. (map manifest-entry->job
  451. (delete-duplicates
  452. (append-map (compose manifest-entries load-manifest)
  453. manifests)
  454. manifest-entry=?)))
  455. (define (arguments->systems arguments)
  456. "Return the systems list from ARGUMENTS."
  457. (match (assoc-ref arguments 'systems)
  458. (#f %cuirass-supported-systems)
  459. ((lst ...) lst)
  460. ((? string? str) (call-with-input-string str read))))
  461. ;;;
  462. ;;; Cuirass entry point.
  463. ;;;
  464. (define (cuirass-jobs store arguments)
  465. "Register Cuirass jobs."
  466. (define subset
  467. (assoc-ref arguments 'subset))
  468. (define systems
  469. (arguments->systems arguments))
  470. (define channels
  471. (let ((channels (assq-ref arguments 'channels)))
  472. (map sexp->channel channels)))
  473. (define guix
  474. (find guix-channel? channels))
  475. (define commit
  476. (channel-commit guix))
  477. (define source
  478. (channel-url guix))
  479. ;; Turn off grafts. Grafting is meant to happen on the user's machines.
  480. (parameterize ((%graft? #f))
  481. ;; Return one job for each package, except bootstrap packages.
  482. (append-map
  483. (lambda (system)
  484. (format (current-error-port)
  485. "evaluating for '~a' (heap size: ~a MiB)...~%"
  486. system
  487. (round
  488. (/ (assoc-ref (gc-stats) 'heap-size)
  489. (expt 2. 20))))
  490. (invalidate-derivation-caches!)
  491. (match subset
  492. ('all
  493. ;; Build everything, including replacements.
  494. (let ((all (all-packages))
  495. (jobs (lambda (package)
  496. (match (package->job store package system)
  497. (#f '())
  498. (main-job
  499. (cons main-job
  500. (if (tunable-package? package)
  501. (tuned-package-jobs store package system)
  502. '())))))))
  503. (append
  504. (append-map jobs all)
  505. (cross-jobs store system))))
  506. ('core
  507. ;; Build core packages only.
  508. (append
  509. (map (lambda (package)
  510. (package-job store (job-name package)
  511. package system))
  512. (append (commencement-packages system) %core-packages))
  513. (cross-jobs store system)))
  514. ('guix
  515. ;; Build Guix modules only.
  516. (guix-jobs store systems
  517. #:source source
  518. #:commit commit))
  519. ('hello
  520. ;; Build hello package only.
  521. (let ((hello (specification->package "hello")))
  522. (list (package-job store (job-name hello)
  523. hello system))))
  524. ('images
  525. ;; Build Guix System images only.
  526. (image-jobs store system))
  527. ('system-tests
  528. ;; Build Guix System tests only.
  529. (system-test-jobs store system
  530. #:source source
  531. #:commit commit))
  532. ('tarball
  533. ;; Build Guix tarball only.
  534. (tarball-jobs store system))
  535. (('custom . modules)
  536. ;; Build custom modules jobs only.
  537. (append-map
  538. (lambda (module)
  539. (let ((proc (module-ref
  540. (resolve-interface module)
  541. 'cuirass-jobs)))
  542. (proc store arguments)))
  543. modules))
  544. (('channels . channels)
  545. ;; Build only the packages from CHANNELS.
  546. (let ((all (all-packages)))
  547. (filter-map
  548. (lambda (package)
  549. (any (lambda (channel)
  550. (and (member (channel-name channel) channels)
  551. (package->job store package system)))
  552. (package-channels package)))
  553. all)))
  554. (('packages . rest)
  555. ;; Build selected list of packages only.
  556. (let ((packages (map specification->package rest)))
  557. (map (lambda (package)
  558. (package-job store (job-name package)
  559. package system))
  560. packages)))
  561. (('manifests . rest)
  562. ;; Build packages in the list of manifests.
  563. (let ((manifests (arguments->manifests rest channels)))
  564. (manifests->jobs store manifests)))
  565. (else
  566. (error "unknown subset" subset))))
  567. systems)))