status.scm 34 KB

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