status.scm 31 KB

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