ci.scm 22 KB

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