ci.scm 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020 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 "." system)))
  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-7 gcc-8 gcc-9 gcc-10 glibc binutils
  133. gmp mpfr mpc coreutils findutils diffutils patch sed grep
  134. gawk gnu-gettext hello guile-2.0 guile-2.2 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 (packages-to-cross-build target)
  142. "Return the list of packages to cross-build for TARGET."
  143. ;; Don't cross-build the bootstrap tarballs for MinGW.
  144. (if (string-contains target "mingw")
  145. (drop-right %core-packages 6)
  146. %core-packages))
  147. (define %cross-targets
  148. '("mips64el-linux-gnu"
  149. "arm-linux-gnueabihf"
  150. "aarch64-linux-gnu"
  151. "powerpc-linux-gnu"
  152. "powerpc64le-linux-gnu"
  153. "riscv64-linux-gnu"
  154. "i586-pc-gnu" ;aka. GNU/Hurd
  155. "i686-w64-mingw32"
  156. "x86_64-w64-mingw32"))
  157. (define (cross-jobs store system)
  158. "Return a list of cross-compilation jobs for SYSTEM."
  159. (define (from-32-to-64? target)
  160. ;; Return true if SYSTEM is 32-bit and TARGET is 64-bit. This hack
  161. ;; prevents known-to-fail cross-builds from i686-linux or armhf-linux to
  162. ;; mips64el-linux-gnuabi64.
  163. (and (or (string-prefix? "i686-" system)
  164. (string-prefix? "i586-" system)
  165. (string-prefix? "armhf-" system))
  166. (string-contains target "64"))) ;x86_64, mips64el, aarch64, etc.
  167. (define (same? target)
  168. ;; Return true if SYSTEM and TARGET are the same thing. This is so we
  169. ;; don't try to cross-compile to 'mips64el-linux-gnu' from
  170. ;; 'mips64el-linux'.
  171. (or (string-contains target system)
  172. (and (string-prefix? "armhf" system) ;armhf-linux
  173. (string-prefix? "arm" target)))) ;arm-linux-gnueabihf
  174. (define (pointless? target)
  175. ;; Return #t if it makes no sense to cross-build to TARGET from SYSTEM.
  176. (match system
  177. ((or "x86_64-linux" "i686-linux")
  178. (if (string-contains target "mingw")
  179. (not (string=? "x86_64-linux" system))
  180. #f))
  181. (_
  182. ;; Don't try to cross-compile from non-Intel platforms: this isn't
  183. ;; very useful and these are often brittle configurations.
  184. #t)))
  185. (define (either proc1 proc2 proc3)
  186. (lambda (x)
  187. (or (proc1 x) (proc2 x) (proc3 x))))
  188. (append-map (lambda (target)
  189. (map (lambda (package)
  190. (package-cross-job store (job-name package)
  191. package target system))
  192. (packages-to-cross-build target)))
  193. (remove (either from-32-to-64? same? pointless?)
  194. %cross-targets)))
  195. (define* (guix-jobs store systems #:key source commit)
  196. "Return a list of jobs for Guix itself."
  197. (define build
  198. (primitive-load (string-append source "/build-aux/build-self.scm")))
  199. (map
  200. (lambda (system)
  201. (let ((name (string->symbol
  202. (string-append "guix." system)))
  203. (drv (run-with-store store
  204. (build source #:version commit #:system system
  205. #:pull-version 1
  206. #:guile-version "2.2"))))
  207. (derivation->job name drv)))
  208. systems))
  209. ;; Architectures that are able to build or cross-build Guix System images.
  210. ;; This does not mean that other architectures are not supported, only that
  211. ;; they are often not fast enough to support Guix System images building.
  212. (define %guix-system-supported-systems
  213. '("x86_64-linux" "i686-linux"))
  214. (define %guix-system-images
  215. (list hurd-barebones-qcow2-image
  216. pine64-barebones-raw-image
  217. pinebook-pro-barebones-raw-image
  218. novena-barebones-raw-image))
  219. (define (hours hours)
  220. (* 3600 hours))
  221. (define* (image->job store image
  222. #:key name system)
  223. "Return the job for IMAGE on SYSTEM. If NAME is passed, use it as job name,
  224. otherwise use the IMAGE name."
  225. (let* ((image-name (or name
  226. (symbol->string (image-name image))))
  227. (name (string-append image-name "." system))
  228. (drv (run-with-store store
  229. (mbegin %store-monad
  230. (set-guile-for-build (default-guile))
  231. (lower-object (system-image image))))))
  232. (parameterize ((%graft? #f))
  233. (derivation->job name drv))))
  234. (define (image-jobs store system)
  235. "Return a list of jobs that build images for SYSTEM."
  236. (define MiB
  237. (expt 2 20))
  238. (if (member system %guix-system-supported-systems)
  239. `(,(image->job store
  240. (image
  241. (inherit efi-disk-image)
  242. (operating-system installation-os))
  243. #:name "usb-image"
  244. #:system system)
  245. ,(image->job
  246. store
  247. (image
  248. (inherit (image-with-label
  249. iso9660-image
  250. (string-append "GUIX_" system "_"
  251. (if (> (string-length %guix-version) 7)
  252. (substring %guix-version 0 7)
  253. %guix-version))))
  254. (operating-system installation-os))
  255. #:name "iso9660-image"
  256. #:system system)
  257. ;; Only cross-compile Guix System images from x86_64-linux for now.
  258. ,@(if (string=? system "x86_64-linux")
  259. (map (cut image->job store <>
  260. #:system system)
  261. %guix-system-images)
  262. '()))
  263. '()))
  264. (define channel-build-system
  265. ;; Build system used to "convert" a channel instance to a package.
  266. (let* ((build (lambda* (store name inputs
  267. #:key source commit system
  268. #:allow-other-keys)
  269. (run-with-store store
  270. ;; SOURCE can be a lowerable object such as <local-file>
  271. ;; or a file name. Adjust accordingly.
  272. (mlet* %store-monad ((source (if (string? source)
  273. (return source)
  274. (lower-object source)))
  275. (instance
  276. -> (checkout->channel-instance
  277. source #:commit commit)))
  278. (channel-instances->derivation (list instance)))
  279. #:system system)))
  280. (lower (lambda* (name #:key system source commit
  281. #:allow-other-keys)
  282. (bag
  283. (name name)
  284. (system system)
  285. (build build)
  286. (arguments `(#:source ,source
  287. #:commit ,commit))))))
  288. (build-system (name 'channel)
  289. (description "Turn a channel instance into a package.")
  290. (lower lower))))
  291. (define* (channel-source->package source #:key commit)
  292. "Return a package for the given channel SOURCE, a lowerable object."
  293. (package
  294. (inherit guix)
  295. (version (string-append (package-version guix) "+"))
  296. (build-system channel-build-system)
  297. (arguments `(#:source ,source
  298. #:commit ,commit))
  299. (inputs '())
  300. (native-inputs '())
  301. (propagated-inputs '())))
  302. (define* (system-test-jobs store system
  303. #:key source commit)
  304. "Return a list of jobs for the system tests."
  305. (define (->job test)
  306. (let ((name (string-append "test." (system-test-name test)
  307. "." system))
  308. (drv (run-with-store store
  309. (mbegin %store-monad
  310. (set-current-system system)
  311. (set-grafting #f)
  312. (set-guile-for-build (default-guile))
  313. (system-test-value test)))))
  314. (derivation->job name drv)))
  315. (if (member system %guix-system-supported-systems)
  316. ;; Override the value of 'current-guix' used by system tests. Using a
  317. ;; channel instance makes tests that rely on 'current-guix' less
  318. ;; expensive. It also makes sure we get a valid Guix package when this
  319. ;; code is not running from a checkout.
  320. (parameterize ((current-guix-package
  321. (channel-source->package source #:commit commit)))
  322. (map ->job (all-system-tests)))
  323. '()))
  324. (define (tarball-jobs store system)
  325. "Return jobs to build the self-contained Guix binary tarball."
  326. (define (->job name drv)
  327. (let ((name (string-append name "." system)))
  328. (parameterize ((%graft? #f))
  329. (derivation->job name drv))))
  330. ;; XXX: Add a job for the stable Guix?
  331. (list
  332. (->job "binary-tarball"
  333. (run-with-store store
  334. (mbegin %store-monad
  335. (set-guile-for-build (default-guile))
  336. (>>= (profile-derivation (packages->manifest (list guix)))
  337. (lambda (profile)
  338. (self-contained-tarball "guix-binary" profile
  339. #:profile-name "current-guix"
  340. #:localstatedir? #t
  341. #:compressor
  342. (lookup-compressor "xz")))))
  343. #:system system))))
  344. (define job-name
  345. ;; Return the name of a package's job.
  346. package-name)
  347. (define package->job
  348. (let ((base-packages
  349. (delete-duplicates
  350. (append-map (match-lambda
  351. ((_ package _ ...)
  352. (match (package-transitive-inputs package)
  353. (((_ inputs _ ...) ...)
  354. inputs))))
  355. (%final-inputs)))))
  356. (lambda (store package system)
  357. "Return a job for PACKAGE on SYSTEM, or #f if this combination is not
  358. valid."
  359. (cond ((member package base-packages)
  360. (package-job store (string-append "base." (job-name package))
  361. package system))
  362. ((supported-package? package system)
  363. (let ((drv (package-derivation store package system
  364. #:graft? #f)))
  365. (and (substitutable-derivation? drv)
  366. (package-job store (job-name package)
  367. package system))))
  368. (else
  369. #f)))))
  370. (define (all-packages)
  371. "Return the list of packages to build."
  372. (define (adjust package result)
  373. (cond ((package-replacement package)
  374. ;; XXX: If PACKAGE and its replacement have the same name/version,
  375. ;; then both Cuirass jobs will have the same name, which
  376. ;; effectively means that the second one will be ignored. Thus,
  377. ;; return the replacement first.
  378. (cons* (package-replacement package) ;build both
  379. package
  380. result))
  381. ((package-superseded package)
  382. result) ;don't build it
  383. (else
  384. (cons package result))))
  385. (fold-packages adjust
  386. (fold adjust '() ;include base packages
  387. (match (%final-inputs)
  388. (((labels packages _ ...) ...)
  389. packages)))
  390. #:select? (const #t))) ;include hidden packages
  391. (define (arguments->manifests arguments channels)
  392. "Return the list of manifests extracted from ARGUMENTS."
  393. (map (lambda (manifest)
  394. (any (lambda (checkout)
  395. (let ((path (in-vicinity checkout manifest)))
  396. (and (file-exists? path)
  397. path)))
  398. (map channel-url channels)))
  399. arguments))
  400. (define (manifests->packages store manifests)
  401. "Return the list of packages found in MANIFESTS."
  402. (define (load-manifest manifest)
  403. (save-module-excursion
  404. (lambda ()
  405. (set-current-module (make-user-module '((guix profiles) (gnu))))
  406. (primitive-load manifest))))
  407. (delete-duplicates!
  408. (map manifest-entry-item
  409. (append-map (compose manifest-entries
  410. load-manifest)
  411. manifests))))
  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. (job (lambda (package)
  453. (package->job store package system))))
  454. (append
  455. (filter-map job all)
  456. (cross-jobs store system))))
  457. ('core
  458. ;; Build core packages only.
  459. (append
  460. (map (lambda (package)
  461. (package-job store (job-name package)
  462. package system))
  463. %core-packages)
  464. (cross-jobs store system)))
  465. ('guix
  466. ;; Build Guix modules only.
  467. (guix-jobs store systems
  468. #:source source
  469. #:commit commit))
  470. ('hello
  471. ;; Build hello package only.
  472. (let ((hello (specification->package "hello")))
  473. (list (package-job store (job-name hello)
  474. hello system))))
  475. ('images
  476. ;; Build Guix System images only.
  477. (image-jobs store system))
  478. ('system-tests
  479. ;; Build Guix System tests only.
  480. (system-test-jobs store system
  481. #:source source
  482. #:commit commit))
  483. ('tarball
  484. ;; Build Guix tarball only.
  485. (tarball-jobs store system))
  486. (('custom . modules)
  487. ;; Build custom modules jobs only.
  488. (append-map
  489. (lambda (module)
  490. (let ((proc (module-ref
  491. (resolve-interface module)
  492. 'cuirass-jobs)))
  493. (proc store arguments)))
  494. modules))
  495. (('channels . channels)
  496. ;; Build only the packages from CHANNELS.
  497. (let ((all (all-packages)))
  498. (filter-map
  499. (lambda (package)
  500. (any (lambda (channel)
  501. (and (member (channel-name channel) channels)
  502. (package->job store package system)))
  503. (package-channels package)))
  504. all)))
  505. (('packages . rest)
  506. ;; Build selected list of packages only.
  507. (let ((packages (map specification->package rest)))
  508. (map (lambda (package)
  509. (package-job store (job-name package)
  510. package system))
  511. packages)))
  512. (('manifests . rest)
  513. ;; Build packages in the list of manifests.
  514. (let* ((manifests (arguments->manifests rest channels))
  515. (packages (manifests->packages store manifests)))
  516. (map (lambda (package)
  517. (package-job store (job-name package)
  518. package system))
  519. packages)))
  520. (else
  521. (error "unknown subset" subset))))
  522. systems)))