ci.scm 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569
  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. ;;;
  7. ;;; This file is part of GNU Guix.
  8. ;;;
  9. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  10. ;;; under the terms of the GNU General Public License as published by
  11. ;;; the Free Software Foundation; either version 3 of the License, or (at
  12. ;;; your option) any later version.
  13. ;;;
  14. ;;; GNU Guix is distributed in the hope that it will be useful, but
  15. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  16. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. ;;; GNU General Public License for more details.
  18. ;;;
  19. ;;; You should have received a copy of the GNU General Public License
  20. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  21. (define-module (gnu ci)
  22. #:use-module (guix config)
  23. #:use-module (guix store)
  24. #:use-module (guix grafts)
  25. #:use-module (guix profiles)
  26. #:use-module (guix packages)
  27. #:use-module (guix channels)
  28. #:use-module (guix config)
  29. #:use-module (guix derivations)
  30. #:use-module (guix build-system)
  31. #:use-module (guix monads)
  32. #:use-module (guix gexp)
  33. #:use-module (guix ui)
  34. #:use-module ((guix licenses)
  35. #:select (gpl3+ license? license-name))
  36. #:use-module ((guix utils) #:select (%current-system))
  37. #:use-module ((guix scripts system) #:select (read-operating-system))
  38. #:use-module ((guix scripts pack)
  39. #:select (lookup-compressor self-contained-tarball))
  40. #:use-module (gnu bootloader)
  41. #:use-module (gnu bootloader u-boot)
  42. #:use-module (gnu image)
  43. #:use-module (gnu packages)
  44. #:use-module (gnu packages gcc)
  45. #:use-module (gnu packages base)
  46. #:use-module (gnu packages gawk)
  47. #:use-module (gnu packages guile)
  48. #:use-module (gnu packages gettext)
  49. #:use-module (gnu packages compression)
  50. #:use-module (gnu packages multiprecision)
  51. #:use-module (gnu packages make-bootstrap)
  52. #:use-module (gnu packages package-management)
  53. #:use-module (gnu system)
  54. #:use-module (gnu system image)
  55. #:use-module (gnu system vm)
  56. #:use-module (gnu system install)
  57. #:use-module (gnu system images hurd)
  58. #:use-module (gnu system images pine64)
  59. #:use-module (gnu tests)
  60. #:use-module (srfi srfi-1)
  61. #:use-module (srfi srfi-26)
  62. #:use-module (ice-9 match)
  63. #:export (%cross-targets
  64. channel-source->package
  65. hydra-jobs))
  66. ;;; Commentary:
  67. ;;;
  68. ;;; This file defines build jobs for the Hydra and Cuirass continuation
  69. ;;; integration tools.
  70. ;;;
  71. ;;; Code:
  72. (define* (package->alist store package system
  73. #:optional (package-derivation package-derivation))
  74. "Convert PACKAGE to an alist suitable for Hydra."
  75. (parameterize ((%graft? #f))
  76. (let ((drv (package-derivation store package system
  77. #:graft? #f)))
  78. `((derivation . ,(derivation-file-name drv))
  79. (log . ,(log-file store (derivation-file-name drv)))
  80. (outputs . ,(filter-map (lambda (res)
  81. (match res
  82. ((name . path)
  83. `(,name . ,path))))
  84. (derivation->output-paths drv)))
  85. (nix-name . ,(derivation-name drv))
  86. (system . ,(derivation-system drv))
  87. (description . ,(package-synopsis package))
  88. (long-description . ,(package-description package))
  89. ;; XXX: Hydra ignores licenses that are not a <license> structure or a
  90. ;; list thereof.
  91. (license . ,(let loop ((license (package-license package)))
  92. (match license
  93. ((? license?)
  94. (license-name license))
  95. ((lst ...)
  96. (map loop license)))))
  97. (home-page . ,(package-home-page package))
  98. (maintainers . ("bug-guix@gnu.org"))
  99. (max-silent-time . ,(or (assoc-ref (package-properties package)
  100. 'max-silent-time)
  101. 3600)) ;1 hour by default
  102. (timeout . ,(or (assoc-ref (package-properties package) 'timeout)
  103. 72000)))))) ;20 hours by default
  104. (define (package-job store job-name package system)
  105. "Return a job called JOB-NAME that builds PACKAGE on SYSTEM."
  106. (let ((job-name (symbol-append job-name (string->symbol ".")
  107. (string->symbol system))))
  108. `(,job-name . ,(cut package->alist store package system))))
  109. (define (package-cross-job store job-name package target system)
  110. "Return a job called TARGET.JOB-NAME that cross-builds PACKAGE for TARGET on
  111. SYSTEM."
  112. `(,(symbol-append (string->symbol target) (string->symbol ".") job-name
  113. (string->symbol ".") (string->symbol system)) .
  114. ,(cute package->alist store package system
  115. (lambda* (store package system #:key graft?)
  116. (package-cross-derivation store package target system
  117. #:graft? graft?)))))
  118. (define %core-packages
  119. ;; Note: Don't put the '-final' package variants because (1) that's
  120. ;; implicit, and (2) they cannot be cross-built (due to the explicit input
  121. ;; chain.)
  122. (list gcc-7 gcc-8 gcc-9 gcc-10 glibc binutils
  123. gmp mpfr mpc coreutils findutils diffutils patch sed grep
  124. gawk gnu-gettext hello guile-2.0 guile-2.2 zlib gzip xz
  125. %bootstrap-binaries-tarball
  126. %binutils-bootstrap-tarball
  127. (%glibc-bootstrap-tarball)
  128. %gcc-bootstrap-tarball
  129. %guile-bootstrap-tarball
  130. %bootstrap-tarballs))
  131. (define (packages-to-cross-build target)
  132. "Return the list of packages to cross-build for TARGET."
  133. ;; Don't cross-build the bootstrap tarballs for MinGW.
  134. (if (string-contains target "mingw")
  135. (drop-right %core-packages 6)
  136. %core-packages))
  137. (define %cross-targets
  138. '("mips64el-linux-gnu"
  139. "arm-linux-gnueabihf"
  140. "aarch64-linux-gnu"
  141. "powerpc-linux-gnu"
  142. "riscv64-linux-gnu"
  143. "i586-pc-gnu" ;aka. GNU/Hurd
  144. "i686-w64-mingw32"
  145. "x86_64-w64-mingw32"))
  146. (define (cross-jobs store system)
  147. "Return a list of cross-compilation jobs for SYSTEM."
  148. (define (from-32-to-64? target)
  149. ;; Return true if SYSTEM is 32-bit and TARGET is 64-bit. This hack
  150. ;; prevents known-to-fail cross-builds from i686-linux or armhf-linux to
  151. ;; mips64el-linux-gnuabi64.
  152. (and (or (string-prefix? "i686-" system)
  153. (string-prefix? "i586-" system)
  154. (string-prefix? "armhf-" system))
  155. (string-contains target "64"))) ;x86_64, mips64el, aarch64, etc.
  156. (define (same? target)
  157. ;; Return true if SYSTEM and TARGET are the same thing. This is so we
  158. ;; don't try to cross-compile to 'mips64el-linux-gnu' from
  159. ;; 'mips64el-linux'.
  160. (or (string-contains target system)
  161. (and (string-prefix? "armhf" system) ;armhf-linux
  162. (string-prefix? "arm" target)))) ;arm-linux-gnueabihf
  163. (define (pointless? target)
  164. ;; Return #t if it makes no sense to cross-build to TARGET from SYSTEM.
  165. (match system
  166. ((or "x86_64-linux" "i686-linux")
  167. (if (string-contains target "mingw")
  168. (not (string=? "x86_64-linux" system))
  169. #f))
  170. (_
  171. ;; Don't try to cross-compile from non-Intel platforms: this isn't
  172. ;; very useful and these are often brittle configurations.
  173. #t)))
  174. (define (either proc1 proc2 proc3)
  175. (lambda (x)
  176. (or (proc1 x) (proc2 x) (proc3 x))))
  177. (append-map (lambda (target)
  178. (map (lambda (package)
  179. (package-cross-job store (job-name package)
  180. package target system))
  181. (packages-to-cross-build target)))
  182. (remove (either from-32-to-64? same? pointless?)
  183. %cross-targets)))
  184. ;; Architectures that are able to build or cross-build Guix System images.
  185. ;; This does not mean that other architectures are not supported, only that
  186. ;; they are often not fast enough to support Guix System images building.
  187. (define %guix-system-supported-systems
  188. '("x86_64-linux" "i686-linux"))
  189. (define %guix-system-images
  190. (list hurd-barebones-qcow2-image
  191. pine64-barebones-raw-image))
  192. (define (image-jobs store system)
  193. "Return a list of jobs that build images for SYSTEM."
  194. (define (->alist drv)
  195. `((derivation . ,(derivation-file-name drv))
  196. (log . ,(log-file store (derivation-file-name drv)))
  197. (outputs . ,(filter-map (lambda (res)
  198. (match res
  199. ((name . path)
  200. `(,name . ,path))))
  201. (derivation->output-paths drv)))
  202. (nix-name . ,(derivation-name drv))
  203. (system . ,(derivation-system drv))
  204. (description . "Stand-alone image of the GNU system")
  205. (long-description . "This is a demo stand-alone image of the GNU
  206. system.")
  207. (license . ,(license-name gpl3+))
  208. (max-silent-time . 600)
  209. (timeout . 3600)
  210. (home-page . ,%guix-home-page-url)
  211. (maintainers . ("bug-guix@gnu.org"))))
  212. (define (->job name drv)
  213. (let ((name (symbol-append name (string->symbol ".")
  214. (string->symbol system))))
  215. `(,name . ,(lambda ()
  216. (parameterize ((%graft? #f))
  217. (->alist drv))))))
  218. (define (build-image image)
  219. (run-with-store store
  220. (mbegin %store-monad
  221. (set-guile-for-build (default-guile))
  222. (lower-object (system-image image)))))
  223. (define MiB
  224. (expt 2 20))
  225. (if (member system %guix-system-supported-systems)
  226. `(,(->job 'usb-image
  227. (build-image
  228. (image
  229. (inherit efi-disk-image)
  230. (operating-system installation-os))))
  231. ,(->job 'iso9660-image
  232. (build-image
  233. (image
  234. (inherit (image-with-label
  235. iso9660-image
  236. (string-append "GUIX_" system "_"
  237. (if (> (string-length %guix-version) 7)
  238. (substring %guix-version 0 7)
  239. %guix-version))))
  240. (operating-system installation-os))))
  241. ;; Only cross-compile Guix System images from x86_64-linux for now.
  242. ,@(if (string=? system "x86_64-linux")
  243. (map (lambda (image)
  244. (->job (image-name image) (build-image image)))
  245. %guix-system-images)
  246. '()))
  247. '()))
  248. (define channel-build-system
  249. ;; Build system used to "convert" a channel instance to a package.
  250. (let* ((build (lambda* (store name inputs
  251. #:key source commit system
  252. #:allow-other-keys)
  253. (run-with-store store
  254. ;; SOURCE can be a lowerable object such as <local-file>
  255. ;; or a file name. Adjust accordingly.
  256. (mlet* %store-monad ((source (if (string? source)
  257. (return source)
  258. (lower-object source)))
  259. (instance
  260. -> (checkout->channel-instance
  261. source #:commit commit)))
  262. (channel-instances->derivation (list instance)))
  263. #:system system)))
  264. (lower (lambda* (name #:key system source commit
  265. #:allow-other-keys)
  266. (bag
  267. (name name)
  268. (system system)
  269. (build build)
  270. (arguments `(#:source ,source
  271. #:commit ,commit))))))
  272. (build-system (name 'channel)
  273. (description "Turn a channel instance into a package.")
  274. (lower lower))))
  275. (define* (channel-source->package source #:key commit)
  276. "Return a package for the given channel SOURCE, a lowerable object."
  277. (package
  278. (inherit guix)
  279. (version (string-append (package-version guix) "+"))
  280. (build-system channel-build-system)
  281. (arguments `(#:source ,source
  282. #:commit ,commit))
  283. (inputs '())
  284. (native-inputs '())
  285. (propagated-inputs '())))
  286. (define* (system-test-jobs store system
  287. #:key source commit)
  288. "Return a list of jobs for the system tests."
  289. (define (test->thunk test)
  290. (lambda ()
  291. (define drv
  292. (run-with-store store
  293. (mbegin %store-monad
  294. (set-current-system system)
  295. (set-grafting #f)
  296. (set-guile-for-build (default-guile))
  297. (system-test-value test))))
  298. `((derivation . ,(derivation-file-name drv))
  299. (log . ,(log-file store (derivation-file-name drv)))
  300. (outputs . ,(filter-map (lambda (res)
  301. (match res
  302. ((name . path)
  303. `(,name . ,path))))
  304. (derivation->output-paths drv)))
  305. (nix-name . ,(derivation-name drv))
  306. (system . ,(derivation-system drv))
  307. (description . ,(format #f "Guix '~a' system test"
  308. (system-test-name test)))
  309. (long-description . ,(system-test-description test))
  310. (license . ,(license-name gpl3+))
  311. (max-silent-time . 600)
  312. (timeout . 3600)
  313. (home-page . ,%guix-home-page-url)
  314. (maintainers . ("bug-guix@gnu.org")))))
  315. (define (->job test)
  316. (let ((name (string->symbol
  317. (string-append "test." (system-test-name test)
  318. "." system))))
  319. (cons name (test->thunk test))))
  320. (if (member system %guix-system-supported-systems)
  321. ;; Override the value of 'current-guix' used by system tests. Using a
  322. ;; channel instance makes tests that rely on 'current-guix' less
  323. ;; expensive. It also makes sure we get a valid Guix package when this
  324. ;; code is not running from a checkout.
  325. (parameterize ((current-guix-package
  326. (channel-source->package source #:commit commit)))
  327. (map ->job (all-system-tests)))
  328. '()))
  329. (define (tarball-jobs store system)
  330. "Return Hydra jobs to build the self-contained Guix binary tarball."
  331. (define (->alist drv)
  332. `((derivation . ,(derivation-file-name drv))
  333. (log . ,(log-file store (derivation-file-name drv)))
  334. (outputs . ,(filter-map (lambda (res)
  335. (match res
  336. ((name . path)
  337. `(,name . ,path))))
  338. (derivation->output-paths drv)))
  339. (nix-name . ,(derivation-name drv))
  340. (system . ,(derivation-system drv))
  341. (description . "Stand-alone binary Guix tarball")
  342. (long-description . "This is a tarball containing binaries of Guix and
  343. all its dependencies, and ready to be installed on \"foreign\" distributions.")
  344. (license . ,(license-name gpl3+))
  345. (home-page . ,%guix-home-page-url)
  346. (maintainers . ("bug-guix@gnu.org"))))
  347. (define (->job name drv)
  348. (let ((name (symbol-append name (string->symbol ".")
  349. (string->symbol system))))
  350. `(,name . ,(lambda ()
  351. (parameterize ((%graft? #f))
  352. (->alist drv))))))
  353. ;; XXX: Add a job for the stable Guix?
  354. (list (->job 'binary-tarball
  355. (run-with-store store
  356. (mbegin %store-monad
  357. (set-guile-for-build (default-guile))
  358. (>>= (profile-derivation (packages->manifest (list guix)))
  359. (lambda (profile)
  360. (self-contained-tarball "guix-binary" profile
  361. #:localstatedir? #t
  362. #:compressor
  363. (lookup-compressor "xz")))))
  364. #:system system))))
  365. (define job-name
  366. ;; Return the name of a package's job.
  367. (compose string->symbol
  368. (cut package-full-name <> "-")))
  369. (define package->job
  370. (let ((base-packages
  371. (delete-duplicates
  372. (append-map (match-lambda
  373. ((_ package _ ...)
  374. (match (package-transitive-inputs package)
  375. (((_ inputs _ ...) ...)
  376. inputs))))
  377. (%final-inputs)))))
  378. (lambda (store package system)
  379. "Return a job for PACKAGE on SYSTEM, or #f if this combination is not
  380. valid."
  381. (cond ((member package base-packages)
  382. (package-job store (symbol-append 'base. (job-name package))
  383. package system))
  384. ((supported-package? package system)
  385. (let ((drv (package-derivation store package system
  386. #:graft? #f)))
  387. (and (substitutable-derivation? drv)
  388. (package-job store (job-name package)
  389. package system))))
  390. (else
  391. #f)))))
  392. (define (all-packages)
  393. "Return the list of packages to build."
  394. (define (adjust package result)
  395. (cond ((package-replacement package)
  396. ;; XXX: If PACKAGE and its replacement have the same name/version,
  397. ;; then both Cuirass jobs will have the same name, which
  398. ;; effectively means that the second one will be ignored. Thus,
  399. ;; return the replacement first.
  400. (cons* (package-replacement package) ;build both
  401. package
  402. result))
  403. ((package-superseded package)
  404. result) ;don't build it
  405. (else
  406. (cons package result))))
  407. (fold-packages adjust
  408. (fold adjust '() ;include base packages
  409. (match (%final-inputs)
  410. (((labels packages _ ...) ...)
  411. packages)))
  412. #:select? (const #t))) ;include hidden packages
  413. (define (arguments->manifests arguments)
  414. "Return the list of manifests extracted from ARGUMENTS."
  415. (map (match-lambda
  416. ((input-name . relative-path)
  417. (let* ((checkout (assq-ref arguments (string->symbol input-name)))
  418. (base (assq-ref checkout 'file-name)))
  419. (in-vicinity base relative-path))))
  420. (assq-ref arguments 'manifests)))
  421. (define (manifests->packages store manifests)
  422. "Return the list of packages found in MANIFESTS."
  423. (define (load-manifest manifest)
  424. (save-module-excursion
  425. (lambda ()
  426. (set-current-module (make-user-module '((guix profiles) (gnu))))
  427. (primitive-load manifest))))
  428. (delete-duplicates!
  429. (map manifest-entry-item
  430. (append-map (compose manifest-entries
  431. load-manifest)
  432. manifests))))
  433. (define (find-current-checkout arguments)
  434. "Find the first checkout of ARGUMENTS that provided the current file.
  435. Return #f if no such checkout is found."
  436. (let ((current-root
  437. (canonicalize-path
  438. (string-append (dirname (current-filename)) "/.."))))
  439. (find (lambda (argument)
  440. (and=> (assq-ref argument 'file-name)
  441. (lambda (name)
  442. (string=? name current-root)))) arguments)))
  443. ;;;
  444. ;;; Hydra entry point.
  445. ;;;
  446. (define (hydra-jobs store arguments)
  447. "Return Hydra jobs."
  448. (define subset
  449. (match (assoc-ref arguments 'subset)
  450. ("core" 'core) ; only build core packages
  451. ("hello" 'hello) ; only build hello
  452. (((? string?) (? string?) ...) 'list) ; only build selected list of packages
  453. ("manifests" 'manifests) ; only build packages in the list of manifests
  454. (_ 'all))) ; build everything
  455. (define systems
  456. (match (assoc-ref arguments 'systems)
  457. (#f %hydra-supported-systems)
  458. ((lst ...) lst)
  459. ((? string? str) (call-with-input-string str read))))
  460. (define checkout
  461. (or (find-current-checkout arguments)
  462. (assq-ref arguments 'superior-guix-checkout)))
  463. (define commit
  464. (assq-ref checkout 'revision))
  465. (define source
  466. (assq-ref checkout 'file-name))
  467. ;; Turn off grafts. Grafting is meant to happen on the user's machines.
  468. (parameterize ((%graft? #f))
  469. ;; Return one job for each package, except bootstrap packages.
  470. (append-map (lambda (system)
  471. (format (current-error-port)
  472. "evaluating for '~a' (heap size: ~a MiB)...~%"
  473. system
  474. (round
  475. (/ (assoc-ref (gc-stats) 'heap-size)
  476. (expt 2. 20))))
  477. (invalidate-derivation-caches!)
  478. (case subset
  479. ((all)
  480. ;; Build everything, including replacements.
  481. (let ((all (all-packages))
  482. (job (lambda (package)
  483. (package->job store package
  484. system))))
  485. (append (filter-map job all)
  486. (image-jobs store system)
  487. (system-test-jobs store system
  488. #:source source
  489. #:commit commit)
  490. (tarball-jobs store system)
  491. (cross-jobs store system))))
  492. ((core)
  493. ;; Build core packages only.
  494. (append (map (lambda (package)
  495. (package-job store (job-name package)
  496. package system))
  497. %core-packages)
  498. (cross-jobs store system)))
  499. ((hello)
  500. ;; Build hello package only.
  501. (let ((hello (specification->package "hello")))
  502. (list (package-job store (job-name hello) hello system))))
  503. ((list)
  504. ;; Build selected list of packages only.
  505. (let* ((names (assoc-ref arguments 'subset))
  506. (packages (map specification->package names)))
  507. (map (lambda (package)
  508. (package-job store (job-name package)
  509. package system))
  510. packages)))
  511. ((manifests)
  512. ;; Build packages in the list of manifests.
  513. (let* ((manifests (arguments->manifests arguments))
  514. (packages (manifests->packages store manifests)))
  515. (map (lambda (package)
  516. (package-job store (job-name package)
  517. package system))
  518. packages)))
  519. (else
  520. (error "unknown subset" subset))))
  521. systems)))