graph.scm 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2015, 2016, 2017, 2018, 2019, 2020, 2021 Ludovic Courtès <ludo@gnu.org>
  3. ;;; Copyright © 2019 Simon Tournier <zimon.toutoune@gmail.com>
  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 scripts graph)
  20. #:use-module (guix ui)
  21. #:use-module (guix graph)
  22. #:use-module (guix grafts)
  23. #:use-module (guix scripts)
  24. #:use-module (guix packages)
  25. #:use-module (guix monads)
  26. #:use-module (guix store)
  27. #:use-module (guix gexp)
  28. #:use-module (guix derivations)
  29. #:use-module (guix memoization)
  30. #:use-module (guix modules)
  31. #:use-module ((guix build-system gnu) #:select (standard-packages))
  32. #:use-module (gnu packages)
  33. #:use-module (guix sets)
  34. #:use-module ((guix diagnostics)
  35. #:select (location-file formatted-message))
  36. #:use-module ((guix transformations)
  37. #:select (show-transformation-options-help
  38. options->transformation
  39. %transformation-options))
  40. #:use-module ((guix scripts build)
  41. #:select (%standard-build-options))
  42. #:use-module (srfi srfi-1)
  43. #:use-module (srfi srfi-26)
  44. #:use-module (srfi srfi-34)
  45. #:use-module (srfi srfi-35)
  46. #:use-module (srfi srfi-37)
  47. #:use-module (ice-9 format)
  48. #:use-module (ice-9 match)
  49. #:export (%package-node-type
  50. %reverse-package-node-type
  51. %bag-node-type
  52. %bag-with-origins-node-type
  53. %bag-emerged-node-type
  54. %reverse-bag-node-type
  55. %derivation-node-type
  56. %reference-node-type
  57. %referrer-node-type
  58. %module-node-type
  59. %node-types
  60. guix-graph))
  61. ;;;
  62. ;;; Package DAG.
  63. ;;;
  64. (define (node-full-name thing)
  65. "Return a human-readable name to denote THING, a package, origin, or file
  66. name."
  67. (cond ((package? thing)
  68. (package-full-name thing))
  69. ((origin? thing)
  70. (origin-actual-file-name thing))
  71. ((string? thing) ;file name
  72. (or (basename thing)
  73. (error "basename" thing)))
  74. (else
  75. (number->string (object-address thing) 16))))
  76. (define (package-node-edges package)
  77. "Return the list of dependencies of PACKAGE."
  78. (match (package-direct-inputs package)
  79. (((labels packages . outputs) ...)
  80. ;; Filter out origins and other non-package dependencies.
  81. (filter package? packages))))
  82. (define assert-package
  83. (match-lambda
  84. ((? package? package)
  85. package)
  86. (x
  87. (raise
  88. (formatted-message (G_ "~a: invalid argument (package name expected)")
  89. x)))))
  90. (define nodes-from-package
  91. ;; The default conversion method.
  92. (lift1 (compose list assert-package) %store-monad))
  93. (define %package-node-type
  94. ;; Type for the traversal of package nodes.
  95. (node-type
  96. (name "package")
  97. (description "the DAG of packages, excluding implicit inputs")
  98. (convert nodes-from-package)
  99. ;; We use package addresses as unique identifiers. This generally works
  100. ;; well, but for generated package objects, we could end up with two
  101. ;; packages that are not 'eq?', yet map to the same derivation (XXX).
  102. (identifier (lift1 object-address %store-monad))
  103. (label node-full-name)
  104. (edges (lift1 package-node-edges %store-monad))))
  105. ;;;
  106. ;;; Reverse package DAG.
  107. ;;;
  108. (define (all-packages) ;XXX: duplicated from (guix scripts refresh)
  109. "Return the list of all the distro's packages."
  110. (fold-packages (lambda (package result)
  111. ;; Ignore deprecated packages.
  112. (if (package-superseded package)
  113. result
  114. (cons package result)))
  115. '()
  116. #:select? (const #t))) ;include hidden packages
  117. (define %reverse-package-node-type
  118. ;; For this node type we first need to compute the list of packages and the
  119. ;; list of back-edges. Since we want to do it only once, we use the
  120. ;; promises below.
  121. (let* ((packages (delay (all-packages)))
  122. (back-edges (delay (run-with-store #f ;store not actually needed
  123. (node-back-edges %package-node-type
  124. (force packages))))))
  125. (node-type
  126. (inherit %package-node-type)
  127. (name "reverse-package")
  128. (description "the reverse DAG of packages")
  129. (edges (lift1 (force back-edges) %store-monad)))))
  130. ;;;
  131. ;;; Package DAG using bags.
  132. ;;;
  133. (define (bag-node-identifier thing)
  134. "Return a unique identifier for THING, which may be a package, origin, or a
  135. file name."
  136. ;; If THING is a file name (a string), we just return it; if it's a package
  137. ;; or origin, we return its address. That gives us the object graph, but
  138. ;; that may differ from the derivation graph (for instance,
  139. ;; 'package-with-bootstrap-guile' generates fresh package objects, and
  140. ;; several packages that are not 'eq?' may actually map to the same
  141. ;; derivation.) Thus, we lower THING and use its derivation file name as a
  142. ;; unique identifier.
  143. (with-monad %store-monad
  144. (if (string? thing)
  145. (return thing)
  146. (mlet %store-monad ((low (lower-object thing)))
  147. (return (if (derivation? low)
  148. (derivation-file-name low)
  149. low))))))
  150. (define (bag-node-edges thing)
  151. "Return the list of dependencies of THING, a package or origin.
  152. Dependencies may include packages, origin, and file names."
  153. (cond ((package? thing)
  154. (match (bag-direct-inputs (package->bag thing))
  155. (((labels things . outputs) ...)
  156. things)))
  157. ((origin? thing)
  158. (cons (or (origin-patch-guile thing) (default-guile))
  159. (if (or (pair? (origin-patches thing))
  160. (origin-snippet thing))
  161. (match (origin-patch-inputs thing)
  162. (#f '())
  163. (((labels dependencies _ ...) ...)
  164. (delete-duplicates dependencies eq?)))
  165. '())))
  166. (else
  167. '())))
  168. (define %bag-node-type
  169. ;; Type for the traversal of package nodes via the "bag" representation,
  170. ;; which includes implicit inputs.
  171. (node-type
  172. (name "bag")
  173. (description "the DAG of packages, including implicit inputs")
  174. (convert nodes-from-package)
  175. (identifier bag-node-identifier)
  176. (label node-full-name)
  177. (edges (lift1 (compose (cut filter package? <>) bag-node-edges)
  178. %store-monad))))
  179. (define %bag-with-origins-node-type
  180. (node-type
  181. (name "bag-with-origins")
  182. (description "the DAG of packages and origins, including implicit inputs")
  183. (convert nodes-from-package)
  184. (identifier bag-node-identifier)
  185. (label node-full-name)
  186. (edges (lift1 (lambda (thing)
  187. (filter (match-lambda
  188. ((? package?) #t)
  189. ((? origin?) #t)
  190. (_ #f))
  191. (bag-node-edges thing)))
  192. %store-monad))))
  193. (define standard-package-set
  194. (mlambda ()
  195. "Return the set of standard packages provided by GNU-BUILD-SYSTEM."
  196. (match (standard-packages)
  197. (((labels packages . output) ...)
  198. (list->setq packages)))))
  199. (define (bag-node-edges-sans-bootstrap thing)
  200. "Like 'bag-node-edges', but pretend that the standard packages of
  201. GNU-BUILD-SYSTEM have zero dependencies."
  202. (if (set-contains? (standard-package-set) thing)
  203. '()
  204. (bag-node-edges thing)))
  205. (define %bag-emerged-node-type
  206. ;; Like %BAG-NODE-TYPE, but without the bootstrap subset of the DAG.
  207. (node-type
  208. (name "bag-emerged")
  209. (description "same as 'bag', but without the bootstrap nodes")
  210. (convert nodes-from-package)
  211. (identifier bag-node-identifier)
  212. (label node-full-name)
  213. (edges (lift1 (compose (cut filter package? <>)
  214. bag-node-edges-sans-bootstrap)
  215. %store-monad))))
  216. (define %reverse-bag-node-type
  217. ;; Type for the reverse traversal of package nodes via the "bag"
  218. ;; representation, which includes implicit inputs.
  219. (let* ((packages (delay (package-closure (all-packages))))
  220. (back-edges (delay (run-with-store #f ;store not actually needed
  221. (node-back-edges %bag-node-type
  222. (force packages))))))
  223. (node-type
  224. (name "reverse-bag")
  225. (description "the reverse DAG of packages, including implicit inputs")
  226. (convert nodes-from-package)
  227. (identifier bag-node-identifier)
  228. (label node-full-name)
  229. (edges (lift1 (force back-edges) %store-monad)))))
  230. ;;;
  231. ;;; Derivation DAG.
  232. ;;;
  233. (define (derivation-dependencies obj)
  234. "Return the <derivation> objects and store items corresponding to the
  235. dependencies of OBJ, a <derivation> or store item."
  236. (if (derivation? obj)
  237. (append (map derivation-input-derivation (derivation-inputs obj))
  238. (derivation-sources obj))
  239. '()))
  240. (define (derivation-node-identifier node)
  241. "Return a unique identifier for NODE, which may be either a <derivation> or
  242. a plain store file."
  243. (if (derivation? node)
  244. (derivation-file-name node)
  245. node))
  246. (define (derivation-node-label node)
  247. "Return a label for NODE, a <derivation> object or plain store item."
  248. (store-path-package-name (match node
  249. ((? derivation? drv)
  250. (derivation-file-name drv))
  251. ((? string? file)
  252. file))))
  253. (define %derivation-node-type
  254. ;; DAG of derivations. Very accurate, very detailed, but usually too much
  255. ;; detailed.
  256. (node-type
  257. (name "derivation")
  258. (description "the DAG of derivations")
  259. (convert (match-lambda
  260. ((? package? package)
  261. (with-monad %store-monad
  262. (>>= (package->derivation package)
  263. (lift1 list %store-monad))))
  264. ((? derivation-path? item)
  265. (mbegin %store-monad
  266. ((store-lift add-temp-root) item)
  267. (return (list (read-derivation-from-file item)))))
  268. (x
  269. (raise
  270. (condition (&message (message "unsupported argument for \
  271. derivation graph")))))))
  272. (identifier (lift1 derivation-node-identifier %store-monad))
  273. (label derivation-node-label)
  274. (edges (lift1 derivation-dependencies %store-monad))))
  275. ;;;
  276. ;;; DAG of residual references (aka. run-time dependencies).
  277. ;;;
  278. (define intern
  279. (mlambda (str)
  280. "Intern STR, a string denoting a store item."
  281. ;; This is necessary for %REFERENCE-NODE-TYPE and %REFERRER-NODE-TYPE
  282. ;; because their nodes are strings but the (guix graph) traversal
  283. ;; procedures expect to be able to compare nodes with 'eq?'.
  284. str))
  285. (define ensure-store-items
  286. ;; Return a list of store items as a monadic value based on the given
  287. ;; argument, which may be a store item or a package.
  288. (match-lambda
  289. ((? package? package)
  290. ;; Return the output file names of PACKAGE.
  291. (mlet %store-monad ((drv (package->derivation package)))
  292. (return (match (derivation->output-paths drv)
  293. (((_ . file-names) ...)
  294. (map intern file-names))))))
  295. ((? store-path? item)
  296. (with-monad %store-monad
  297. (return (list (intern item)))))
  298. (x
  299. (raise
  300. (condition (&message (message "unsupported argument for \
  301. this type of graph")))))))
  302. (define (references* item)
  303. "Return as a monadic value the references of ITEM, based either on the
  304. information available in the local store or using information about
  305. substitutes."
  306. (lambda (store)
  307. (guard (c ((store-protocol-error? c)
  308. (match (substitutable-path-info store (list item))
  309. ((info)
  310. (values (map intern (substitutable-references info))
  311. store))
  312. (()
  313. (leave (G_ "references for '~a' are not known~%")
  314. item)))))
  315. (values (map intern (references store item)) store))))
  316. (define %reference-node-type
  317. (node-type
  318. (name "references")
  319. (description "the DAG of run-time dependencies (store references)")
  320. (convert ensure-store-items)
  321. (identifier (lift1 intern %store-monad))
  322. (label store-path-package-name)
  323. (edges references*)))
  324. (define non-derivation-referrers
  325. (let ((referrers (store-lift referrers)))
  326. (lambda (item)
  327. "Return the referrers of ITEM, except '.drv' files."
  328. (mlet %store-monad ((items (referrers item)))
  329. (return (map intern (remove derivation-path? items)))))))
  330. (define %referrer-node-type
  331. (node-type
  332. (name "referrers")
  333. (description "the DAG of referrers in the store")
  334. (convert ensure-store-items)
  335. (identifier (lift1 intern %store-monad))
  336. (label store-path-package-name)
  337. (edges non-derivation-referrers)))
  338. ;;;
  339. ;;; Scheme modules.
  340. ;;;
  341. (define (module-from-package package)
  342. (file-name->module-name (location-file (package-location package))))
  343. (define (source-module-dependencies* module)
  344. "Like 'source-module-dependencies' but filter out modules that are not
  345. package modules, while attempting to retain user package modules."
  346. (remove (match-lambda
  347. (('guix _ ...) #t)
  348. (('system _ ...) #t)
  349. (('language _ ...) #t)
  350. (('ice-9 _ ...) #t)
  351. (('srfi _ ...) #t)
  352. (_ #f))
  353. (source-module-dependencies module)))
  354. (define %module-node-type
  355. ;; Show the graph of package modules.
  356. (node-type
  357. (name "module")
  358. (description "the graph of package modules")
  359. (convert (lift1 (compose list module-from-package) %store-monad))
  360. (identifier (lift1 identity %store-monad))
  361. (label object->string)
  362. (edges (lift1 source-module-dependencies* %store-monad))))
  363. ;;;
  364. ;;; List of node types.
  365. ;;;
  366. (define %node-types
  367. ;; List of all the node types.
  368. (list %package-node-type
  369. %reverse-package-node-type
  370. %bag-node-type
  371. %bag-with-origins-node-type
  372. %bag-emerged-node-type
  373. %reverse-bag-node-type
  374. %derivation-node-type
  375. %reference-node-type
  376. %referrer-node-type
  377. %module-node-type))
  378. (define (lookup-node-type name)
  379. "Return the node type called NAME. Raise an error if it is not found."
  380. (or (find (lambda (type)
  381. (string=? (node-type-name type) name))
  382. %node-types)
  383. (leave (G_ "~a: unknown node type~%") name)))
  384. (define (lookup-backend name)
  385. "Return the graph backend called NAME. Raise an error if it is not found."
  386. (or (find (lambda (backend)
  387. (string=? (graph-backend-name backend) name))
  388. %graph-backends)
  389. (leave (G_ "~a: unknown backend~%") name)))
  390. (define (list-node-types)
  391. "Print the available node types along with their synopsis."
  392. (display (G_ "The available node types are:\n"))
  393. (newline)
  394. (for-each (lambda (type)
  395. (format #t " - ~a: ~a~%"
  396. (node-type-name type)
  397. (node-type-description type)))
  398. %node-types))
  399. (define (list-backends)
  400. "Print the available backends along with their synopsis."
  401. (display (G_ "The available backend types are:\n"))
  402. (newline)
  403. (for-each (lambda (backend)
  404. (format #t " - ~a: ~a~%"
  405. (graph-backend-name backend)
  406. (graph-backend-description backend)))
  407. %graph-backends))
  408. ;;;
  409. ;;; Displaying a path.
  410. ;;;
  411. (define (display-path node1 node2 type)
  412. "Display the shortest path from NODE1 to NODE2, of TYPE."
  413. (mlet %store-monad ((path (shortest-path node1 node2 type)))
  414. (define node-label
  415. (let ((label (node-type-label type)))
  416. ;; Special-case derivations and store items to print them in full,
  417. ;; contrary to what their 'node-type-label' normally does.
  418. (match-lambda
  419. ((? derivation? drv) (derivation-file-name drv))
  420. ((? string? str) str)
  421. (node (label node)))))
  422. (if path
  423. (format #t "~{~a~%~}" (map node-label path))
  424. (leave (G_ "no path from '~a' to '~a'~%")
  425. (node-label node1) (node-label node2)))
  426. (return #t)))
  427. ;;;
  428. ;;; Command-line options.
  429. ;;;
  430. (define %options
  431. (cons* (option '(#\t "type") #t #f
  432. (lambda (opt name arg result)
  433. (alist-cons 'node-type (lookup-node-type arg)
  434. result)))
  435. (option '("path") #f #f
  436. (lambda (opt name arg result)
  437. (alist-cons 'path? #t result)))
  438. (option '("list-types") #f #f
  439. (lambda (opt name arg result)
  440. (list-node-types)
  441. (exit 0)))
  442. (option '(#\b "backend") #t #f
  443. (lambda (opt name arg result)
  444. (alist-cons 'backend (lookup-backend arg)
  445. result)))
  446. (option '("list-backends") #f #f
  447. (lambda (opt name arg result)
  448. (list-backends)
  449. (exit 0)))
  450. (option '(#\e "expression") #t #f
  451. (lambda (opt name arg result)
  452. (alist-cons 'expression arg result)))
  453. (option '(#\s "system") #t #f
  454. (lambda (opt name arg result)
  455. (alist-cons 'system arg
  456. (alist-delete 'system result eq?))))
  457. (find (lambda (option)
  458. (member "load-path" (option-names option)))
  459. %standard-build-options)
  460. (option '(#\h "help") #f #f
  461. (lambda args
  462. (show-help)
  463. (exit 0)))
  464. (option '(#\V "version") #f #f
  465. (lambda args
  466. (show-version-and-exit "guix graph")))
  467. %transformation-options))
  468. (define (show-help)
  469. ;; TRANSLATORS: Here 'dot' is the name of a program; it must not be
  470. ;; translated.
  471. (display (G_ "Usage: guix graph PACKAGE...
  472. Emit a representation of the dependency graph of PACKAGE...\n"))
  473. (display (G_ "
  474. -b, --backend=TYPE produce a graph with the given backend TYPE"))
  475. (display (G_ "
  476. --list-backends list the available graph backends"))
  477. (display (G_ "
  478. -t, --type=TYPE represent nodes of the given TYPE"))
  479. (display (G_ "
  480. --list-types list the available graph types"))
  481. (display (G_ "
  482. --path display the shortest path between the given nodes"))
  483. (display (G_ "
  484. -e, --expression=EXPR consider the package EXPR evaluates to"))
  485. (display (G_ "
  486. -s, --system=SYSTEM consider the graph for SYSTEM--e.g., \"i686-linux\""))
  487. (newline)
  488. (display (G_ "
  489. -L, --load-path=DIR prepend DIR to the package module search path"))
  490. (newline)
  491. (show-transformation-options-help)
  492. (newline)
  493. (display (G_ "
  494. -h, --help display this help and exit"))
  495. (display (G_ "
  496. -V, --version display version information and exit"))
  497. (newline)
  498. (show-bug-report-information))
  499. (define %default-options
  500. `((node-type . ,%package-node-type)
  501. (backend . ,%graphviz-backend)
  502. (system . ,(%current-system))))
  503. ;;;
  504. ;;; Entry point.
  505. ;;;
  506. (define-command (guix-graph . args)
  507. (category packaging)
  508. (synopsis "view and query package dependency graphs")
  509. (with-error-handling
  510. (define opts
  511. (parse-command-line args %options
  512. (list %default-options)
  513. #:build-options? #f))
  514. (define backend
  515. (assoc-ref opts 'backend))
  516. (define type
  517. (assoc-ref opts 'node-type))
  518. (with-store store
  519. (let* ((transform (options->transformation opts))
  520. (items (filter-map (match-lambda
  521. (('argument . (? store-path? item))
  522. item)
  523. (('argument . spec)
  524. (transform
  525. (specification->package spec)))
  526. (('expression . exp)
  527. (transform
  528. (read/eval-package-expression exp)))
  529. (_ #f))
  530. opts)))
  531. (when (null? items)
  532. (warning (G_ "no arguments specified; creating an empty graph~%")))
  533. (run-with-store store
  534. ;; XXX: Since grafting can trigger unsolicited builds, disable it.
  535. (mlet %store-monad ((_ (set-grafting #f))
  536. (nodes (mapm %store-monad
  537. (node-type-convert type)
  538. (reverse items))))
  539. (if (assoc-ref opts 'path?)
  540. (match nodes
  541. (((node1 _ ...) (node2 _ ...))
  542. (display-path node1 node2 type))
  543. (_
  544. (leave (G_ "'--path' option requires exactly two \
  545. nodes (given ~a)~%")
  546. (length nodes))))
  547. (export-graph (concatenate nodes)
  548. (current-output-port)
  549. #:node-type type
  550. #:backend backend)))
  551. #:system (assq-ref opts 'system)))))
  552. #t)
  553. ;;; graph.scm ends here