ci.scm 22 KB

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