status.scm 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2017, 2018, 2019, 2020, 2021 Ludovic Courtès <ludo@gnu.org>
  3. ;;; Copyright © 2018, 2019 Ricardo Wurmus <rekado@elephly.net>
  4. ;;;
  5. ;;; This file is part of GNU Guix.
  6. ;;;
  7. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  8. ;;; under the terms of the GNU General Public License as published by
  9. ;;; the Free Software Foundation; either version 3 of the License, or (at
  10. ;;; your option) any later version.
  11. ;;;
  12. ;;; GNU Guix is distributed in the hope that it will be useful, but
  13. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. ;;; GNU General Public License for more details.
  16. ;;;
  17. ;;; You should have received a copy of the GNU General Public License
  18. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  19. (define-module (guix status)
  20. #:use-module (guix records)
  21. #:use-module (guix i18n)
  22. #:use-module (guix colors)
  23. #:use-module (guix progress)
  24. #:autoload (guix build syscalls) (terminal-columns)
  25. #:autoload (guix build download) (nar-uri-abbreviation)
  26. #:use-module (guix store)
  27. #:use-module (guix derivations)
  28. #:use-module (guix memoization)
  29. #:use-module (srfi srfi-1)
  30. #:use-module (srfi srfi-9)
  31. #:use-module (srfi srfi-9 gnu)
  32. #:use-module (srfi srfi-19)
  33. #:use-module (srfi srfi-26)
  34. #:use-module (ice-9 regex)
  35. #:use-module (ice-9 match)
  36. #:use-module (ice-9 format)
  37. #:use-module (ice-9 binary-ports)
  38. #:autoload (ice-9 rdelim) (read-string)
  39. #:use-module (rnrs bytevectors)
  40. #:use-module ((system foreign)
  41. #:select (bytevector->pointer pointer->bytevector))
  42. #:export (build-event-output-port
  43. compute-status
  44. build-status
  45. build-status?
  46. build-status-building
  47. build-status-downloading
  48. build-status-builds-completed
  49. build-status-downloads-completed
  50. build?
  51. build
  52. build-derivation
  53. build-system
  54. build-log-file
  55. build-phase
  56. build-completion
  57. download?
  58. download
  59. download-item
  60. download-uri
  61. download-size
  62. download-start
  63. download-end
  64. download-transferred
  65. build-status-updater
  66. print-build-event
  67. print-build-event/quiet
  68. print-build-status
  69. with-status-report
  70. with-status-verbosity))
  71. ;;; Commentary:
  72. ;;;
  73. ;;; This module provides facilities to track the status of ongoing builds and
  74. ;;; downloads in a given session, as well as tools to report about the current
  75. ;;; status to user interfaces. It does so by analyzing the output of
  76. ;;; 'current-build-output-port'. The build status is maintained in a
  77. ;;; <build-status> record.
  78. ;;;
  79. ;;; Code:
  80. ;;;
  81. ;;; Build status tracking.
  82. ;;;
  83. ;; Builds and substitutions performed by the daemon.
  84. (define-record-type* <build-status> build-status make-build-status
  85. build-status?
  86. (building build-status-building ;list of <build>
  87. (default '()))
  88. (downloading build-status-downloading ;list of <download>
  89. (default '()))
  90. (builds-completed build-status-builds-completed ;list of <build>
  91. (default '()))
  92. (downloads-completed build-status-downloads-completed ;list of <download>
  93. (default '())))
  94. ;; On-going or completed build.
  95. (define-immutable-record-type <build>
  96. (%build derivation id system log-file phase completion)
  97. build?
  98. (derivation build-derivation) ;string (.drv file name)
  99. (id build-id) ;#f | integer
  100. (system build-system) ;string
  101. (log-file build-log-file) ;#f | string
  102. (phase build-phase ;#f | symbol
  103. set-build-phase)
  104. (completion build-completion ;#f | integer (percentage)
  105. set-build-completion))
  106. (define* (build derivation system #:key id log-file phase completion)
  107. "Return a new build."
  108. (%build derivation id system log-file phase completion))
  109. ;; On-going or completed downloads. Downloads can be stem from substitutes
  110. ;; and from "builtin:download" fixed-output derivations.
  111. (define-record-type <download>
  112. (%download item uri size start end transferred)
  113. download?
  114. (item download-item) ;store item
  115. (uri download-uri) ;string | #f
  116. (size download-size) ;integer | #f
  117. (start download-start) ;<time>
  118. (end download-end) ;#f | <time>
  119. (transferred download-transferred)) ;integer
  120. (define* (download item uri
  121. #:key size
  122. (start (current-time time-monotonic)) end
  123. (transferred 0))
  124. "Return a new download."
  125. (%download item uri size start end transferred))
  126. (define (matching-build drv)
  127. "Return a predicate that matches builds of DRV."
  128. (lambda (build)
  129. (string=? drv (build-derivation build))))
  130. (define (matching-download item)
  131. "Return a predicate that matches downloads of ITEM."
  132. (lambda (download)
  133. (string=? item (download-item download))))
  134. (define %phase-start-rx
  135. ;; Match the "starting phase" message emitted by 'gnu-build-system'.
  136. (make-regexp "^starting phase [`']([^']+)'"))
  137. (define %percentage-line-rx
  138. ;; Things like CMake write lines like "[ 10%] gcc -c …". This regexp
  139. ;; matches them.
  140. (make-regexp "^[[:space:]]*\\[ *([0-9]+)%\\]"))
  141. (define %fraction-line-rx
  142. ;; The 'compiled-modules' derivations and Ninja produce reports like
  143. ;; "[ 1/32]" at the beginning of each line, while GHC prints "[ 6 of 45]".
  144. ;; This regexp matches these.
  145. (make-regexp "^[[:space:]]*\\[ *([0-9]+) *(/|of) *([0-9]+)\\]"))
  146. (define (update-build status id line)
  147. "Update STATUS based on LINE, a build output line for ID that might contain
  148. a completion indication."
  149. (define (find-build)
  150. (find (lambda (build)
  151. (and (build-id build)
  152. (= (build-id build) id)))
  153. (build-status-building status)))
  154. (define (update %)
  155. (let ((build (find-build)))
  156. (build-status
  157. (inherit status)
  158. (building (cons (set-build-completion build %)
  159. (delq build (build-status-building status)))))))
  160. (cond ((string-any #\nul line)
  161. ;; Don't try to match a regexp here.
  162. status)
  163. ((regexp-exec %percentage-line-rx line)
  164. =>
  165. (lambda (match)
  166. (let ((% (string->number (match:substring match 1))))
  167. (update %))))
  168. ((regexp-exec %fraction-line-rx line)
  169. =>
  170. (lambda (match)
  171. (let ((done (string->number (match:substring match 1)))
  172. (total (string->number (match:substring match 3))))
  173. (update (* 100. (/ done total))))))
  174. ((regexp-exec %phase-start-rx line)
  175. =>
  176. (lambda (match)
  177. (let ((phase (match:substring match 1))
  178. (build (find-build)))
  179. (if build
  180. (build-status
  181. (inherit status)
  182. (building
  183. (cons (set-build-phase (set-build-completion build #f)
  184. (string->symbol phase))
  185. (delq build (build-status-building status)))))
  186. status))))
  187. (else
  188. status)))
  189. (define* (compute-status event status
  190. #:key
  191. (current-time current-time)
  192. (derivation-path->output-path
  193. derivation-path->output-path))
  194. "Given EVENT, a tuple like (build-started \"/gnu/store/...-foo.drv\" ...),
  195. compute a new status based on STATUS."
  196. (match event
  197. (('build-started drv "-" system log-file . rest)
  198. (let ((build (build drv system
  199. #:id (match rest
  200. ((pid . _) (string->number pid))
  201. (_ #f))
  202. #:log-file (if (string-null? log-file)
  203. #f
  204. log-file))))
  205. (build-status
  206. (inherit status)
  207. (building (cons build (build-status-building status))))))
  208. (((or 'build-succeeded 'build-failed) drv _ ...)
  209. (let ((build (find (matching-build drv)
  210. (build-status-building status))))
  211. ;; If BUILD is #f, this may be because DRV corresponds to a
  212. ;; fixed-output derivation that is listed as a download.
  213. (if build
  214. (build-status
  215. (inherit status)
  216. (building (delq build (build-status-building status)))
  217. (builds-completed
  218. (cons build (build-status-builds-completed status))))
  219. status)))
  220. ;; Note: Ignore 'substituter-started' and 'substituter-succeeded' because
  221. ;; they're not as informative as 'download-started' and
  222. ;; 'download-succeeded'.
  223. (('download-started item uri (= string->number size))
  224. ;; This is presumably a fixed-output derivation so move it from
  225. ;; 'building' to 'downloading'. XXX: This doesn't work in 'check' mode
  226. ;; because ITEM is different from DRV's output.
  227. (build-status
  228. (inherit status)
  229. (building (remove (lambda (build)
  230. (let ((drv (build-derivation build)))
  231. (equal? (false-if-exception
  232. (derivation-path->output-path drv))
  233. item)))
  234. (build-status-building status)))
  235. (downloading (cons (download item uri #:size size
  236. #:start (current-time time-monotonic))
  237. (build-status-downloading status)))))
  238. (('download-succeeded item uri (= string->number size))
  239. (let ((current (find (matching-download item)
  240. (build-status-downloading status))))
  241. (build-status
  242. (inherit status)
  243. (downloading (delq current (build-status-downloading status)))
  244. (downloads-completed
  245. (cons (download item uri
  246. #:size size
  247. #:start (download-start current)
  248. #:transferred size
  249. #:end (current-time time-monotonic))
  250. (build-status-downloads-completed status))))))
  251. (('substituter-succeeded item _ ...)
  252. (match (find (matching-download item)
  253. (build-status-downloading status))
  254. (#f
  255. ;; Presumably we already got a 'download-succeeded' event for ITEM,
  256. ;; everything is fine.
  257. status)
  258. (current
  259. ;; Maybe the build process didn't emit a 'download-succeeded' event
  260. ;; for ITEM, so remove CURRENT from the queue now.
  261. (build-status
  262. (inherit status)
  263. (downloading (delq current (build-status-downloading status)))
  264. (downloads-completed
  265. (cons (download item (download-uri current)
  266. #:size (download-size current)
  267. #:start (download-start current)
  268. #:transferred (download-size current)
  269. #:end (current-time time-monotonic))
  270. (build-status-downloads-completed status)))))))
  271. (('download-progress item uri
  272. (= string->number size)
  273. (= string->number transferred))
  274. (let ((downloads (remove (matching-download item)
  275. (build-status-downloading status)))
  276. (current (find (matching-download item)
  277. (build-status-downloading status))))
  278. (build-status
  279. (inherit status)
  280. (downloading (cons (download item uri
  281. #:size size
  282. #:start
  283. (or (and current
  284. (download-start current))
  285. (current-time time-monotonic))
  286. #:transferred transferred)
  287. downloads)))))
  288. (('build-log (? integer? pid) line)
  289. (update-build status pid line))
  290. (_
  291. status)))
  292. (define (simultaneous-jobs status)
  293. "Return the number of on-going builds and downloads for STATUS."
  294. (+ (length (build-status-building status))
  295. (length (build-status-downloading status))))
  296. ;;;
  297. ;;; Rendering.
  298. ;;;
  299. (define (extended-build-trace-supported?)
  300. "Return true if the currently used store is known to support \"extended
  301. build traces\" such as \"@ download-progress\" traces."
  302. ;; Support for extended build traces was added in protocol version #x162.
  303. (and (current-store-protocol-version)
  304. (>= (current-store-protocol-version) #x162)))
  305. (define (multiplexed-output-supported?)
  306. "Return true if the daemon supports \"multiplexed output\"--i.e., \"@
  307. build-log\" traces."
  308. (and (current-store-protocol-version)
  309. (>= (current-store-protocol-version) #x163)))
  310. (define spin!
  311. (let ((steps (circular-list "\\" "|" "/" "-")))
  312. (lambda (phase port)
  313. "Display a spinner on PORT. If PHASE is true, display it as a hint of
  314. the current build phase."
  315. (when (isatty?* port)
  316. (match steps
  317. ((first . rest)
  318. (set! steps rest)
  319. (display "\r\x1b[K" port)
  320. (display first port)
  321. (when phase
  322. (display " " port)
  323. ;; TRANSLATORS: The word "phase" here denotes a "build phase";
  324. ;; "~a" is a placeholder for the untranslated name of the current
  325. ;; build phase--e.g., 'configure' or 'build'.
  326. (format port (G_ "'~a' phase") phase))
  327. (force-output port)))))))
  328. (define colorize-log-line
  329. ;; Take a string and return a possibly colorized string according to the
  330. ;; rules below.
  331. (color-rules
  332. ("^(phase)(.*)(succeeded after)(.*)(seconds)(.*)"
  333. GREEN BOLD GREEN RESET GREEN BLUE)
  334. ("^(phase)(.*)(failed after)(.*)(seconds)(.*)"
  335. RED BLUE RED BLUE RED BLUE)
  336. ("^(.*)(error|fail|failed|\\<FAIL|FAILED)([[:blank:]]*)(:)(.*)"
  337. RESET RED BOLD BOLD BOLD)
  338. ("^(.*)(warning)([[:blank:]]*)(:)(.*)"
  339. RESET MAGENTA BOLD BOLD BOLD)))
  340. (define (hook-message hook-type)
  341. "Return a human-readable string for the profile hook type HOOK-TYPE."
  342. (match hook-type
  343. ('info-dir
  344. (G_ "building directory of Info manuals..."))
  345. ('ghc-package-cache
  346. (G_ "building GHC package cache..."))
  347. ('ca-certificate-bundle
  348. (G_ "building CA certificate bundle..."))
  349. ('emacs-subdirs
  350. (G_ "listing Emacs sub-directories..."))
  351. ('glib-schemas
  352. (G_ "generating GLib schema cache..."))
  353. ('gtk-icon-themes
  354. (G_ "creating GTK+ icon theme cache..."))
  355. ('gtk-im-modules
  356. (G_ "building cache files for GTK+ input methods..."))
  357. ('xdg-desktop-database
  358. (G_ "building XDG desktop file cache..."))
  359. ('xdg-mime-database
  360. (G_ "building XDG MIME database..."))
  361. ('fonts-dir
  362. (G_ "building fonts directory..."))
  363. ('texlive-configuration
  364. (G_ "building TeX Live configuration..."))
  365. ('manual-database
  366. (G_ "building database for manual pages..."))
  367. ('package-cache ;package cache generated by 'guix pull'
  368. (G_ "building package cache..."))
  369. (_ #f)))
  370. (define* (print-build-event event old-status status
  371. #:optional (port (current-error-port))
  372. #:key
  373. (colorize? (color-output? port))
  374. (print-urls? #t)
  375. (print-log? #t))
  376. "Print information about EVENT and STATUS to PORT. When COLORIZE? is true,
  377. produce colorful output. When PRINT-LOG? is true, display the build log in
  378. addition to build events. When PRINT-URLS? is true, display the URL of
  379. substitutes being downloaded."
  380. (define info
  381. (if colorize?
  382. (cute colorize-string <> (color BOLD))
  383. identity))
  384. (define success
  385. (if colorize?
  386. (cute colorize-string <> (color GREEN BOLD))
  387. identity))
  388. (define failure
  389. (if colorize?
  390. (cute colorize-string <> (color RED BOLD))
  391. identity))
  392. (define tty?
  393. (isatty?* port))
  394. (define (report-build-progress phase %)
  395. (let ((% (min (max % 0) 100))) ;sanitize
  396. (erase-current-line port)
  397. (let* ((prefix (format #f "~3d% ~@['~a' ~]"
  398. (inexact->exact (round %))
  399. (case phase
  400. ((build) #f) ;not useful to display it
  401. (else phase))))
  402. (length (string-length prefix)))
  403. (display prefix port)
  404. (display (progress-bar % (- (current-terminal-columns) length))
  405. port))
  406. (force-output port)))
  407. (define print-log-line
  408. (if print-log?
  409. (if colorize?
  410. (lambda (id line)
  411. (display (colorize-log-line line) port))
  412. (lambda (id line)
  413. (display line port)))
  414. (lambda (id line)
  415. (match (build-status-building status)
  416. ((build) ;single job
  417. (match (build-completion build)
  418. ((? number? %)
  419. (report-build-progress (build-phase build) %))
  420. (_
  421. (spin! (build-phase build) port))))
  422. (_
  423. (spin! #f port))))))
  424. (define erase-current-line*
  425. (if (and (not print-log?) (isatty?* port))
  426. (lambda ()
  427. (erase-current-line port)
  428. (force-output port))
  429. (const #t)))
  430. (match event
  431. (('build-started drv . _)
  432. (erase-current-line*)
  433. (let ((properties (derivation-properties
  434. (read-derivation-from-file drv))))
  435. (match (assq-ref properties 'type)
  436. ('graft
  437. (let ((count (match (assq-ref properties 'graft)
  438. (#f 0)
  439. (lst (or (assq-ref lst 'count) 0)))))
  440. (format port (info (N_ "applying ~a graft for ~a ..."
  441. "applying ~a grafts for ~a ..."
  442. count))
  443. count drv)))
  444. ('profile
  445. (let ((count (match (assq-ref properties 'profile)
  446. (#f 0)
  447. (lst (or (assq-ref lst 'count) 0)))))
  448. (format port (info (N_ "building profile with ~a package..."
  449. "building profile with ~a packages..."
  450. count))
  451. count)))
  452. ('profile-hook
  453. (let ((hook-type (assq-ref properties 'hook)))
  454. (or (and=> (hook-message hook-type)
  455. (lambda (msg)
  456. (format port (info msg))))
  457. (format port (info (G_ "running profile hook of type '~a'..."))
  458. hook-type))))
  459. (_
  460. (format port (info (G_ "building ~a...")) drv))))
  461. (newline port))
  462. (('build-succeeded drv . _)
  463. (erase-current-line*) ;erase spinner or progress bar
  464. (when (or print-log? (not (extended-build-trace-supported?)))
  465. (format port (success (G_ "successfully built ~a")) drv)
  466. (newline port))
  467. (match (build-status-building status)
  468. (() #t)
  469. (ongoing ;when max-jobs > 1
  470. (format port
  471. (N_ "The following build is still in progress:~%~{ ~a~%~}~%"
  472. "The following builds are still in progress:~%~{ ~a~%~}~%"
  473. (length ongoing))
  474. (map build-derivation ongoing)))))
  475. (('build-failed drv . _)
  476. (erase-current-line*) ;erase spinner or progress bar
  477. (format port (failure (G_ "build of ~a failed")) drv)
  478. (newline port)
  479. (match (derivation-log-file drv)
  480. (#f
  481. (format port (failure (G_ "Could not find build log for '~a'."))
  482. drv))
  483. (log
  484. (format port (info (G_ "View build log at '~a'.")) log)))
  485. (newline port))
  486. (('substituter-started item _ ...)
  487. (erase-current-line*)
  488. (when (or print-log? (not (extended-build-trace-supported?)))
  489. (format port (info (G_ "substituting ~a...")) item)
  490. (newline port)))
  491. (('download-started item uri _ ...)
  492. (when print-urls?
  493. (erase-current-line*)
  494. (format port (info (G_ "downloading from ~a ...")) uri)
  495. (newline port)))
  496. (('download-progress item uri
  497. (= string->number size)
  498. (= string->number transferred))
  499. ;; Print a progress bar, but only if there's only one on-going
  500. ;; job--otherwise the output would be intermingled.
  501. (when (= 1 (simultaneous-jobs status))
  502. (match (find (matching-download item)
  503. (build-status-downloading status))
  504. (#f #f) ;shouldn't happen!
  505. (download
  506. ;; XXX: It would be nice to memoize the abbreviation.
  507. (let ((uri (if (string-contains uri "/nar/")
  508. (nar-uri-abbreviation uri)
  509. (basename uri))))
  510. (display-download-progress uri size
  511. #:tty? tty?
  512. #:start-time
  513. (download-start download)
  514. #:transferred transferred))))))
  515. (('substituter-succeeded item _ ...)
  516. (when (extended-build-trace-supported?)
  517. ;; If there are no jobs running, we already reported download completion
  518. ;; so there's nothing left to do.
  519. (unless (zero? (simultaneous-jobs status))
  520. (format port (success (G_ "substitution of ~a complete")) item)
  521. (newline port))
  522. (when (and print-urls? (zero? (simultaneous-jobs status)))
  523. ;; Leave a blank line after the "downloading ..." line and the
  524. ;; progress bar (that's three lines in total).
  525. (newline port))))
  526. (('substituter-failed item _ ...)
  527. (format port (failure (G_ "substitution of ~a failed")) item)
  528. (newline port))
  529. (('hash-mismatch item algo expected actual _ ...)
  530. ;; TRANSLATORS: The final string looks like "sha256 hash mismatch for
  531. ;; /gnu/store/…-sth:", where "sha256" is the hash algorithm.
  532. (format port (failure (G_ "~a hash mismatch for ~a:")) algo item)
  533. (newline port)
  534. (format port (info (G_ "\
  535. expected hash: ~a
  536. actual hash: ~a~%"))
  537. expected actual))
  538. (('build-remote drv host _ ...)
  539. (format port (info (G_ "offloading build of ~a to '~a'")) drv host)
  540. (newline port))
  541. (('build-log pid line)
  542. (if (multiplexed-output-supported?)
  543. (if (not pid)
  544. (begin
  545. ;; LINE comes from the daemon, not from builders. Let it
  546. ;; through.
  547. (display line port)
  548. (force-output port))
  549. (print-log-line pid line))
  550. (cond ((string-prefix? "substitute: " line)
  551. ;; The daemon prefixes early messages coming with 'guix
  552. ;; substitute' with "substitute:". These are useful ("updating
  553. ;; substitutes from URL"), so let them through.
  554. (display line port)
  555. (force-output port))
  556. ((string-prefix? "waiting for locks" line)
  557. ;; This is when a derivation is already being built and we're just
  558. ;; waiting for the build to complete.
  559. (display (info (string-trim-right line)) port)
  560. (newline))
  561. (else
  562. (print-log-line pid line)))))
  563. (_
  564. event)))
  565. (define* (print-build-event/quiet event old-status status
  566. #:optional
  567. (port (current-error-port))
  568. #:key
  569. (colorize? (color-output? port)))
  570. (print-build-event event old-status status port
  571. #:colorize? colorize?
  572. #:print-urls? #f
  573. #:print-log? #f))
  574. (define* (print-build-event/quiet-with-urls event old-status status
  575. #:optional
  576. (port (current-error-port))
  577. #:key
  578. (colorize? (color-output? port)))
  579. (print-build-event event old-status status port
  580. #:colorize? colorize?
  581. #:print-urls? #t ;show download URLs
  582. #:print-log? #f))
  583. (define* (build-status-updater #:optional (on-change (const #t)))
  584. "Return a procedure that can be passed to 'build-event-output-port'. That
  585. procedure computes the new build status upon each event and calls ON-CHANGE:
  586. (ON-CHANGE event status new-status)
  587. ON-CHANGE can display the build status, build events, etc."
  588. (lambda (event status)
  589. (let ((new (compute-status event status)))
  590. (on-change event status new)
  591. new)))
  592. ;;;
  593. ;;; Build port.
  594. ;;;
  595. (define (maybe-utf8->string bv)
  596. "Attempt to decode BV as UTF-8 string and return it. Gracefully handle the
  597. case where BV does not contain only valid UTF-8."
  598. (catch 'decoding-error
  599. (lambda ()
  600. (utf8->string bv))
  601. (lambda _
  602. ;; This is the sledgehammer but it's the only safe way we have to
  603. ;; properly handle this. It's expensive but it's rarely needed.
  604. (let ((port (open-bytevector-input-port bv)))
  605. (set-port-encoding! port "UTF-8")
  606. (set-port-conversion-strategy! port 'substitute)
  607. (let ((str (read-string port)))
  608. (close-port port)
  609. str)))))
  610. (define (bytevector-index bv number offset count)
  611. "Search for NUMBER in BV starting from OFFSET and reading up to COUNT bytes;
  612. return the offset where NUMBER first occurs or #f if it could not be found."
  613. (let loop ((offset offset)
  614. (count count))
  615. (cond ((zero? count) #f)
  616. ((= (bytevector-u8-ref bv offset) number) offset)
  617. (else (loop (+ 1 offset) (- count 1))))))
  618. (define (split-lines str)
  619. "Split STR into lines in a way that preserves newline characters."
  620. (let loop ((str str)
  621. (result '()))
  622. (if (string-null? str)
  623. (reverse result)
  624. (match (string-index str #\newline)
  625. (#f
  626. (loop "" (cons str result)))
  627. (index
  628. (loop (string-drop str (+ index 1))
  629. (cons (string-take str (+ index 1)) result)))))))
  630. (define* (build-event-output-port proc #:optional (seed (build-status)))
  631. "Return an output port for use as 'current-build-output-port' that calls
  632. PROC with its current state value, initialized with SEED, on every build
  633. event. Build events passed to PROC are tuples corresponding to the \"build
  634. traces\" produced by the daemon:
  635. (build-started \"/gnu/store/...-foo.drv\" ...)
  636. (substituter-started \"/gnu/store/...-foo\" ...)
  637. and so on.
  638. The second return value is a thunk to retrieve the current state."
  639. (define %fragments
  640. ;; Line fragments received so far.
  641. '())
  642. (define %state
  643. ;; Current state for PROC.
  644. seed)
  645. ;; When true, this represents the current state while reading a
  646. ;; "@ build-log" trace: the current builder PID, the previously-read
  647. ;; bytevectors, and the number of bytes that remain to be read.
  648. (define %build-output-pid #f)
  649. (define %build-output '())
  650. (define %build-output-left #f)
  651. (define (process-line line)
  652. (cond ((string-prefix? "@ " line)
  653. ;; Note: Drop the trailing \n, and use 'string-split' to preserve
  654. ;; spaces (the log file part of 'build-started' events can be the
  655. ;; empty string.)
  656. (match (string-split (string-drop (string-drop-right line 1) 2)
  657. #\space)
  658. (("build-log" (= string->number pid) (= string->number len))
  659. (set! %build-output-pid pid)
  660. (set! %build-output '())
  661. (set! %build-output-left len))
  662. (((= string->symbol event-name) args ...)
  663. (set! %state
  664. (proc (cons event-name args)
  665. %state)))))
  666. (else
  667. (set! %state (proc (list 'build-log #f line)
  668. %state)))))
  669. (define (process-build-output pid output)
  670. ;; Transform OUTPUT in 'build-log' events or download events as generated
  671. ;; by extended build traces.
  672. (define (line->event line)
  673. (match (and (string-prefix? "@ " line)
  674. (string-tokenize (string-drop line 2)))
  675. ((type . args)
  676. (if (or (string-prefix? "download-" type)
  677. (string=? "build-remote" type))
  678. (cons (string->symbol type) args)
  679. `(build-log ,pid ,line)))
  680. (_
  681. `(build-log ,pid ,line))))
  682. (let* ((lines (split-lines output))
  683. (events (map line->event lines)))
  684. (set! %state (fold proc %state events))))
  685. (define (bytevector-range bv offset count)
  686. (let ((ptr (bytevector->pointer bv offset)))
  687. (pointer->bytevector ptr count)))
  688. (define (write! bv offset count)
  689. (if %build-output-pid
  690. (let ((keep (min count %build-output-left)))
  691. (set! %build-output
  692. (let ((bv* (make-bytevector keep)))
  693. (bytevector-copy! bv offset bv* 0 keep)
  694. (cons bv* %build-output)))
  695. (set! %build-output-left
  696. (- %build-output-left keep))
  697. (when (zero? %build-output-left)
  698. (process-build-output %build-output-pid
  699. (string-concatenate-reverse
  700. (map maybe-utf8->string %build-output))) ;XXX
  701. (set! %build-output '())
  702. (set! %build-output-pid #f))
  703. keep)
  704. (match (bytevector-index bv (char->integer #\newline)
  705. offset count)
  706. ((? integer? cr)
  707. (let* ((tail (maybe-utf8->string
  708. (bytevector-range bv offset (- cr -1 offset))))
  709. (line (string-concatenate-reverse
  710. (cons tail %fragments))))
  711. (process-line line)
  712. (set! %fragments '())
  713. (- cr -1 offset)))
  714. (#f
  715. (unless (zero? count)
  716. (let ((str (maybe-utf8->string
  717. (bytevector-range bv offset count))))
  718. (set! %fragments (cons str %fragments))))
  719. count))))
  720. (define port
  721. (make-custom-binary-output-port "filtering-input-port"
  722. write!
  723. #f #f
  724. #f))
  725. ;; The build port actually receives Unicode strings.
  726. (set-port-encoding! port "UTF-8")
  727. (setvbuf port 'line)
  728. (values port (lambda () %state)))
  729. (define (call-with-status-report on-event thunk)
  730. (parameterize ((current-terminal-columns (terminal-columns))
  731. (current-build-output-port
  732. (build-event-output-port (build-status-updater on-event))))
  733. (thunk)))
  734. (define-syntax-rule (with-status-report on-event exp ...)
  735. "Set up build status reporting to the user using the ON-EVENT procedure;
  736. evaluate EXP... in that context."
  737. (call-with-status-report on-event (lambda () exp ...)))
  738. (define (logger-for-level level)
  739. "Return the logging procedure that corresponds to LEVEL."
  740. (cond ((<= level 0) (const #t))
  741. ((= level 1) print-build-event/quiet)
  742. ((= level 2) print-build-event/quiet-with-urls)
  743. (else print-build-event)))
  744. (define (call-with-status-verbosity level thunk)
  745. (call-with-status-report (logger-for-level level) thunk))
  746. (define-syntax-rule (with-status-verbosity level exp ...)
  747. "Set up build status reporting to the user at the given LEVEL: 0 means
  748. silent, 1 means quiet, 2 means verbose. Evaluate EXP... in that context."
  749. (call-with-status-verbosity level (lambda () exp ...)))