graph.scm 21 KB

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