status.scm 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2017, 2018, 2019 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-hook
  440. (let ((hook-type (assq-ref properties 'hook)))
  441. (or (and=> (hook-message hook-type)
  442. (lambda (msg)
  443. (format port (info msg))))
  444. (format port (info (G_ "running profile hook of type '~a'..."))
  445. hook-type))))
  446. (_
  447. (format port (info (G_ "building ~a...")) drv))))
  448. (newline port))
  449. (('build-succeeded drv . _)
  450. (erase-current-line*) ;erase spinner or progress bar
  451. (when (or print-log? (not (extended-build-trace-supported?)))
  452. (format port (success (G_ "successfully built ~a")) drv)
  453. (newline port))
  454. (match (build-status-building status)
  455. (() #t)
  456. (ongoing ;when max-jobs > 1
  457. (format port
  458. (N_ "The following build is still in progress:~%~{ ~a~%~}~%"
  459. "The following builds are still in progress:~%~{ ~a~%~}~%"
  460. (length ongoing))
  461. (map build-derivation ongoing)))))
  462. (('build-failed drv . _)
  463. (erase-current-line*) ;erase spinner or progress bar
  464. (format port (failure (G_ "build of ~a failed")) drv)
  465. (newline port)
  466. (match (derivation-log-file drv)
  467. (#f
  468. (format port (failure (G_ "Could not find build log for '~a'."))
  469. drv))
  470. (log
  471. (format port (info (G_ "View build log at '~a'.")) log)))
  472. (newline port))
  473. (('substituter-started item _ ...)
  474. (erase-current-line*)
  475. (when (or print-log? (not (extended-build-trace-supported?)))
  476. (format port (info (G_ "substituting ~a...")) item)
  477. (newline port)))
  478. (('download-started item uri _ ...)
  479. (erase-current-line*)
  480. (format port (info (G_ "downloading from ~a...")) uri)
  481. (newline port))
  482. (('download-progress item uri
  483. (= string->number size)
  484. (= string->number transferred))
  485. ;; Print a progress bar, but only if there's only one on-going
  486. ;; job--otherwise the output would be intermingled.
  487. (when (= 1 (simultaneous-jobs status))
  488. (match (find (matching-download item)
  489. (build-status-downloading status))
  490. (#f #f) ;shouldn't happen!
  491. (download
  492. ;; XXX: It would be nice to memoize the abbreviation.
  493. (let ((uri (if (string-contains uri "/nar/")
  494. (nar-uri-abbreviation uri)
  495. (basename uri))))
  496. (display-download-progress uri size
  497. #:start-time
  498. (download-start download)
  499. #:transferred transferred))))))
  500. (('substituter-succeeded item _ ...)
  501. ;; If there are no jobs running, we already reported download completion
  502. ;; so there's nothing left to do.
  503. (unless (and (zero? (simultaneous-jobs status))
  504. (extended-build-trace-supported?))
  505. (format port (success (G_ "substitution of ~a complete")) item)
  506. (newline port)))
  507. (('substituter-failed item _ ...)
  508. (format port (failure (G_ "substitution of ~a failed")) item)
  509. (newline port))
  510. (('hash-mismatch item algo expected actual _ ...)
  511. ;; TRANSLATORS: The final string looks like "sha256 hash mismatch for
  512. ;; /gnu/store/…-sth:", where "sha256" is the hash algorithm.
  513. (format port (failure (G_ "~a hash mismatch for ~a:")) algo item)
  514. (newline port)
  515. (format port (info (G_ "\
  516. expected hash: ~a
  517. actual hash: ~a~%"))
  518. expected actual))
  519. (('build-remote drv host _ ...)
  520. (format port (info (G_ "offloading build of ~a to '~a'")) drv host)
  521. (newline port))
  522. (('build-log pid line)
  523. (if (multiplexed-output-supported?)
  524. (if (not pid)
  525. (begin
  526. ;; LINE comes from the daemon, not from builders. Let it
  527. ;; through.
  528. (display line port)
  529. (force-output port))
  530. (print-log-line pid line))
  531. (cond ((string-prefix? "substitute: " line)
  532. ;; The daemon prefixes early messages coming with 'guix
  533. ;; substitute' with "substitute:". These are useful ("updating
  534. ;; substitutes from URL"), so let them through.
  535. (display line port)
  536. (force-output port))
  537. ((string-prefix? "waiting for locks" line)
  538. ;; This is when a derivation is already being built and we're just
  539. ;; waiting for the build to complete.
  540. (display (info (string-trim-right line)) port)
  541. (newline))
  542. (else
  543. (print-log-line pid line)))))
  544. (_
  545. event)))
  546. (define* (print-build-event/quiet event old-status status
  547. #:optional
  548. (port (current-error-port))
  549. #:key
  550. (colorize? (color-output? port)))
  551. (print-build-event event old-status status port
  552. #:colorize? colorize?
  553. #:print-log? #f))
  554. (define* (build-status-updater #:optional (on-change (const #t)))
  555. "Return a procedure that can be passed to 'build-event-output-port'. That
  556. procedure computes the new build status upon each event and calls ON-CHANGE:
  557. (ON-CHANGE event status new-status)
  558. ON-CHANGE can display the build status, build events, etc."
  559. (lambda (event status)
  560. (let ((new (compute-status event status)))
  561. (on-change event status new)
  562. new)))
  563. ;;;
  564. ;;; Build port.
  565. ;;;
  566. (define (maybe-utf8->string bv)
  567. "Attempt to decode BV as UTF-8 string and return it. Gracefully handle the
  568. case where BV does not contain only valid UTF-8."
  569. (catch 'decoding-error
  570. (lambda ()
  571. (utf8->string bv))
  572. (lambda _
  573. ;; This is the sledgehammer but it's the only safe way we have to
  574. ;; properly handle this. It's expensive but it's rarely needed.
  575. (let ((port (open-bytevector-input-port bv)))
  576. (set-port-encoding! port "UTF-8")
  577. (set-port-conversion-strategy! port 'substitute)
  578. (let ((str (read-string port)))
  579. (close-port port)
  580. str)))))
  581. (define (bytevector-index bv number offset count)
  582. "Search for NUMBER in BV starting from OFFSET and reading up to COUNT bytes;
  583. return the offset where NUMBER first occurs or #f if it could not be found."
  584. (let loop ((offset offset)
  585. (count count))
  586. (cond ((zero? count) #f)
  587. ((= (bytevector-u8-ref bv offset) number) offset)
  588. (else (loop (+ 1 offset) (- count 1))))))
  589. (define (split-lines str)
  590. "Split STR into lines in a way that preserves newline characters."
  591. (let loop ((str str)
  592. (result '()))
  593. (if (string-null? str)
  594. (reverse result)
  595. (match (string-index str #\newline)
  596. (#f
  597. (loop "" (cons str result)))
  598. (index
  599. (loop (string-drop str (+ index 1))
  600. (cons (string-take str (+ index 1)) result)))))))
  601. (define* (build-event-output-port proc #:optional (seed (build-status)))
  602. "Return an output port for use as 'current-build-output-port' that calls
  603. PROC with its current state value, initialized with SEED, on every build
  604. event. Build events passed to PROC are tuples corresponding to the \"build
  605. traces\" produced by the daemon:
  606. (build-started \"/gnu/store/...-foo.drv\" ...)
  607. (substituter-started \"/gnu/store/...-foo\" ...)
  608. and so on.
  609. The second return value is a thunk to retrieve the current state."
  610. (define %fragments
  611. ;; Line fragments received so far.
  612. '())
  613. (define %state
  614. ;; Current state for PROC.
  615. seed)
  616. ;; When true, this represents the current state while reading a
  617. ;; "@ build-log" trace: the current builder PID, the previously-read
  618. ;; bytevectors, and the number of bytes that remain to be read.
  619. (define %build-output-pid #f)
  620. (define %build-output '())
  621. (define %build-output-left #f)
  622. (define (process-line line)
  623. (cond ((string-prefix? "@ " line)
  624. ;; Note: Drop the trailing \n, and use 'string-split' to preserve
  625. ;; spaces (the log file part of 'build-started' events can be the
  626. ;; empty string.)
  627. (match (string-split (string-drop (string-drop-right line 1) 2)
  628. #\space)
  629. (("build-log" (= string->number pid) (= string->number len))
  630. (set! %build-output-pid pid)
  631. (set! %build-output '())
  632. (set! %build-output-left len))
  633. (((= string->symbol event-name) args ...)
  634. (set! %state
  635. (proc (cons event-name args)
  636. %state)))))
  637. (else
  638. (set! %state (proc (list 'build-log #f line)
  639. %state)))))
  640. (define (process-build-output pid output)
  641. ;; Transform OUTPUT in 'build-log' events or download events as generated
  642. ;; by extended build traces.
  643. (define (line->event line)
  644. (match (and (string-prefix? "@ " line)
  645. (string-tokenize (string-drop line 2)))
  646. ((type . args)
  647. (if (or (string-prefix? "download-" type)
  648. (string=? "build-remote" type))
  649. (cons (string->symbol type) args)
  650. `(build-log ,pid ,line)))
  651. (_
  652. `(build-log ,pid ,line))))
  653. (let* ((lines (split-lines output))
  654. (events (map line->event lines)))
  655. (set! %state (fold proc %state events))))
  656. (define (bytevector-range bv offset count)
  657. (let ((ptr (bytevector->pointer bv offset)))
  658. (pointer->bytevector ptr count)))
  659. (define (write! bv offset count)
  660. (if %build-output-pid
  661. (let ((keep (min count %build-output-left)))
  662. (set! %build-output
  663. (let ((bv* (make-bytevector keep)))
  664. (bytevector-copy! bv offset bv* 0 keep)
  665. (cons bv* %build-output)))
  666. (set! %build-output-left
  667. (- %build-output-left keep))
  668. (when (zero? %build-output-left)
  669. (process-build-output %build-output-pid
  670. (string-concatenate-reverse
  671. (map maybe-utf8->string %build-output))) ;XXX
  672. (set! %build-output '())
  673. (set! %build-output-pid #f))
  674. keep)
  675. (match (bytevector-index bv (char->integer #\newline)
  676. offset count)
  677. ((? integer? cr)
  678. (let* ((tail (maybe-utf8->string
  679. (bytevector-range bv offset (- cr -1 offset))))
  680. (line (string-concatenate-reverse
  681. (cons tail %fragments))))
  682. (process-line line)
  683. (set! %fragments '())
  684. (- cr -1 offset)))
  685. (#f
  686. (unless (zero? count)
  687. (let ((str (maybe-utf8->string
  688. (bytevector-range bv offset count))))
  689. (set! %fragments (cons str %fragments))))
  690. count))))
  691. (define port
  692. (make-custom-binary-output-port "filtering-input-port"
  693. write!
  694. #f #f
  695. #f))
  696. ;; The build port actually receives Unicode strings.
  697. (set-port-encoding! port "UTF-8")
  698. (setvbuf port 'line)
  699. (values port (lambda () %state)))
  700. (define (call-with-status-report on-event thunk)
  701. (parameterize ((current-terminal-columns (terminal-columns))
  702. (current-build-output-port
  703. (build-event-output-port (build-status-updater on-event))))
  704. (thunk)))
  705. (define-syntax-rule (with-status-report on-event exp ...)
  706. "Set up build status reporting to the user using the ON-EVENT procedure;
  707. evaluate EXP... in that context."
  708. (call-with-status-report on-event (lambda () exp ...)))
  709. (define (logger-for-level level)
  710. "Return the logging procedure that corresponds to LEVEL."
  711. (cond ((<= level 0) (const #t))
  712. ((= level 1) print-build-event/quiet)
  713. (else print-build-event)))
  714. (define (call-with-status-verbosity level thunk)
  715. (call-with-status-report (logger-for-level level) thunk))
  716. (define-syntax-rule (with-status-verbosity level exp ...)
  717. "Set up build status reporting to the user at the given LEVEL: 0 means
  718. silent, 1 means quiet, 2 means verbose. Evaluate EXP... in that context."
  719. (call-with-status-verbosity level (lambda () exp ...)))