packages.scm 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019 Ludovic Courtès <ludo@gnu.org>
  3. ;;; Copyright © 2013 Mark H Weaver <mhw@netris.org>
  4. ;;; Copyright © 2014 Eric Bavier <bavier@member.fsf.org>
  5. ;;; Copyright © 2016, 2017 Alex Kost <alezost@gmail.com>
  6. ;;; Copyright © 2016 Mathieu Lirzin <mthl@gnu.org>
  7. ;;;
  8. ;;; This file is part of GNU Guix.
  9. ;;;
  10. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  11. ;;; under the terms of the GNU General Public License as published by
  12. ;;; the Free Software Foundation; either version 3 of the License, or (at
  13. ;;; your option) any later version.
  14. ;;;
  15. ;;; GNU Guix is distributed in the hope that it will be useful, but
  16. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  17. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. ;;; GNU General Public License for more details.
  19. ;;;
  20. ;;; You should have received a copy of the GNU General Public License
  21. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  22. (define-module (gnu packages)
  23. #:use-module (guix packages)
  24. #:use-module (guix ui)
  25. #:use-module (guix utils)
  26. #:use-module (guix discovery)
  27. #:use-module (guix memoization)
  28. #:use-module ((guix build utils)
  29. #:select ((package-name->name+version
  30. . hyphen-separated-name->name+version)
  31. mkdir-p))
  32. #:autoload (guix profiles) (packages->manifest)
  33. #:use-module (guix describe)
  34. #:use-module (ice-9 vlist)
  35. #:use-module (ice-9 match)
  36. #:autoload (ice-9 binary-ports) (put-bytevector)
  37. #:autoload (system base compile) (compile)
  38. #:use-module (srfi srfi-1)
  39. #:use-module (srfi srfi-11)
  40. #:use-module (srfi srfi-26)
  41. #:use-module (srfi srfi-34)
  42. #:use-module (srfi srfi-35)
  43. #:use-module (srfi srfi-39)
  44. #:export (search-patch
  45. search-patches
  46. search-auxiliary-file
  47. search-bootstrap-binary
  48. %patch-path
  49. %auxiliary-files-path
  50. %bootstrap-binaries-path
  51. %package-module-path
  52. %default-package-module-path
  53. fold-packages
  54. fold-available-packages
  55. find-packages-by-name
  56. find-package-locations
  57. find-best-packages-by-name
  58. specification->package
  59. specification->package+output
  60. specification->location
  61. specifications->manifest
  62. generate-package-cache))
  63. ;;; Commentary:
  64. ;;;
  65. ;;; General utilities for the software distribution---i.e., the modules under
  66. ;;; (gnu packages ...).
  67. ;;;
  68. ;;; Code:
  69. ;; By default, we store patches, auxiliary files and bootstrap binaries
  70. ;; alongside Guile modules. This is so that these extra files can be
  71. ;; found without requiring a special setup, such as a specific
  72. ;; installation directory and an extra environment variable. One
  73. ;; advantage of this setup is that everything just works in an
  74. ;; auto-compilation setting.
  75. (define %bootstrap-binaries-path
  76. (make-parameter
  77. (map (cut string-append <> "/gnu/packages/bootstrap")
  78. %load-path)))
  79. (define %auxiliary-files-path
  80. (make-parameter
  81. (map (cut string-append <> "/gnu/packages/aux-files")
  82. %load-path)))
  83. (define (search-auxiliary-file file-name)
  84. "Search the auxiliary FILE-NAME. Return #f if not found."
  85. (search-path (%auxiliary-files-path) file-name))
  86. (define (search-patch file-name)
  87. "Search the patch FILE-NAME. Raise an error if not found."
  88. (or (search-path (%patch-path) file-name)
  89. (raise (condition
  90. (&message (message (format #f (G_ "~a: patch not found")
  91. file-name)))))))
  92. (define-syntax-rule (search-patches file-name ...)
  93. "Return the list of absolute file names corresponding to each
  94. FILE-NAME found in %PATCH-PATH."
  95. (list (search-patch file-name) ...))
  96. (define (search-bootstrap-binary file-name system)
  97. "Search the bootstrap binary FILE-NAME for SYSTEM. Raise an error if not
  98. found."
  99. (or (search-path (%bootstrap-binaries-path)
  100. (string-append system "/" file-name))
  101. (raise (condition
  102. (&message
  103. (message
  104. (format #f (G_ "could not find bootstrap binary '~a' \
  105. for system '~a'")
  106. file-name system)))))))
  107. (define %distro-root-directory
  108. ;; Absolute file name of the module hierarchy. Since (gnu packages …) might
  109. ;; live in a directory different from (guix), try to get the best match.
  110. (letrec-syntax ((dirname* (syntax-rules ()
  111. ((_ file)
  112. (dirname file))
  113. ((_ file head tail ...)
  114. (dirname (dirname* file tail ...)))))
  115. (try (syntax-rules ()
  116. ((_ (file things ...) rest ...)
  117. (match (search-path %load-path file)
  118. (#f
  119. (try rest ...))
  120. (absolute
  121. (dirname* absolute things ...))))
  122. ((_)
  123. #f))))
  124. (try ("gnu/packages/base.scm" gnu/ packages/)
  125. ("gnu/packages.scm" gnu/)
  126. ("guix.scm"))))
  127. (define %default-package-module-path
  128. ;; Default search path for package modules.
  129. `((,%distro-root-directory . "gnu/packages")))
  130. (define (cache-is-authoritative?)
  131. "Return true if the pre-computed package cache is authoritative. It is not
  132. authoritative when entries have been added via GUIX_PACKAGE_PATH or '-L'
  133. flags."
  134. (equal? (%package-module-path)
  135. (append %default-package-module-path
  136. (package-path-entries))))
  137. (define %package-module-path
  138. ;; Search path for package modules. Each item must be either a directory
  139. ;; name or a pair whose car is a directory and whose cdr is a sub-directory
  140. ;; to narrow the search.
  141. (let* ((not-colon (char-set-complement (char-set #\:)))
  142. (environment (string-tokenize (or (getenv "GUIX_PACKAGE_PATH") "")
  143. not-colon))
  144. (channels (package-path-entries)))
  145. ;; Automatically add channels and items from $GUIX_PACKAGE_PATH to Guile's
  146. ;; search path. For historical reasons, $GUIX_PACKAGE_PATH goes to the
  147. ;; front; channels go to the back so that they don't override Guix' own
  148. ;; modules.
  149. (set! %load-path
  150. (append environment %load-path channels))
  151. (set! %load-compiled-path
  152. (append environment %load-compiled-path channels))
  153. (make-parameter
  154. (append environment
  155. %default-package-module-path
  156. channels))))
  157. (define %patch-path
  158. ;; Define it after '%package-module-path' so that '%load-path' contains user
  159. ;; directories, allowing patches in $GUIX_PACKAGE_PATH to be found.
  160. (make-parameter
  161. (map (lambda (directory)
  162. (if (string=? directory %distro-root-directory)
  163. (string-append directory "/gnu/packages/patches")
  164. directory))
  165. %load-path)))
  166. (define (fold-available-packages proc init)
  167. "Fold PROC over the list of available packages. For each available package,
  168. PROC is called along these lines:
  169. (PROC NAME VERSION RESULT
  170. #:outputs OUTPUTS
  171. #:location LOCATION
  172. …)
  173. PROC can use #:allow-other-keys to ignore the bits it's not interested in.
  174. When a package cache is available, this procedure does not actually load any
  175. package module."
  176. (define cache
  177. (load-package-cache (current-profile)))
  178. (if (and cache (cache-is-authoritative?))
  179. (vhash-fold (lambda (name vector result)
  180. (match vector
  181. (#(name version module symbol outputs
  182. supported? deprecated?
  183. file line column)
  184. (proc name version result
  185. #:outputs outputs
  186. #:location (and file
  187. (location file line column))
  188. #:supported? supported?
  189. #:deprecated? deprecated?))))
  190. init
  191. cache)
  192. (fold-packages (lambda (package result)
  193. (proc (package-name package)
  194. (package-version package)
  195. result
  196. #:outputs (package-outputs package)
  197. #:location (package-location package)
  198. #:supported?
  199. (->bool
  200. (member (%current-system)
  201. (package-supported-systems package)))
  202. #:deprecated?
  203. (->bool
  204. (package-superseded package))))
  205. init)))
  206. (define* (fold-packages proc init
  207. #:optional
  208. (modules (all-modules (%package-module-path)
  209. #:warn
  210. warn-about-load-error))
  211. #:key (select? (negate hidden-package?)))
  212. "Call (PROC PACKAGE RESULT) for each available package defined in one of
  213. MODULES that matches SELECT?, using INIT as the initial value of RESULT. It
  214. is guaranteed to never traverse the same package twice."
  215. (fold-module-public-variables (lambda (object result)
  216. (if (and (package? object) (select? object))
  217. (proc object result)
  218. result))
  219. init
  220. modules))
  221. (define %package-cache-file
  222. ;; Location of the package cache.
  223. "/lib/guix/package.cache")
  224. (define load-package-cache
  225. (mlambda (profile)
  226. "Attempt to load the package cache. On success return a vhash keyed by
  227. package names. Return #f on failure."
  228. (match profile
  229. (#f #f)
  230. (profile
  231. (catch 'system-error
  232. (lambda ()
  233. (define lst
  234. (load-compiled (string-append profile %package-cache-file)))
  235. (fold (lambda (item vhash)
  236. (match item
  237. (#(name version module symbol outputs
  238. supported? deprecated?
  239. file line column)
  240. (vhash-cons name item vhash))))
  241. vlist-null
  242. lst))
  243. (lambda args
  244. (if (= ENOENT (system-error-errno args))
  245. #f
  246. (apply throw args))))))))
  247. (define find-packages-by-name/direct ;bypass the cache
  248. (let ((packages (delay
  249. (fold-packages (lambda (p r)
  250. (vhash-cons (package-name p) p r))
  251. vlist-null)))
  252. (version>? (lambda (p1 p2)
  253. (version>? (package-version p1) (package-version p2)))))
  254. (lambda* (name #:optional version)
  255. "Return the list of packages with the given NAME. If VERSION is not #f,
  256. then only return packages whose version is prefixed by VERSION, sorted in
  257. decreasing version order."
  258. (let ((matching (sort (vhash-fold* cons '() name (force packages))
  259. version>?)))
  260. (if version
  261. (filter (lambda (package)
  262. (version-prefix? version (package-version package)))
  263. matching)
  264. matching)))))
  265. (define (cache-lookup cache name)
  266. "Lookup package NAME in CACHE. Return a list sorted in increasing version
  267. order."
  268. (define (package-version<? v1 v2)
  269. (version>? (vector-ref v2 1) (vector-ref v1 1)))
  270. (sort (vhash-fold* cons '() name cache)
  271. package-version<?))
  272. (define* (find-packages-by-name name #:optional version)
  273. "Return the list of packages with the given NAME. If VERSION is not #f,
  274. then only return packages whose version is prefixed by VERSION, sorted in
  275. decreasing version order."
  276. (define cache
  277. (load-package-cache (current-profile)))
  278. (if (and (cache-is-authoritative?) cache)
  279. (match (cache-lookup cache name)
  280. (#f #f)
  281. ((#(_ versions modules symbols _ _ _ _ _ _) ...)
  282. (fold (lambda (version* module symbol result)
  283. (if (or (not version)
  284. (version-prefix? version version*))
  285. (cons (module-ref (resolve-interface module)
  286. symbol)
  287. result)
  288. result))
  289. '()
  290. versions modules symbols)))
  291. (find-packages-by-name/direct name version)))
  292. (define* (find-package-locations name #:optional version)
  293. "Return a list of version/location pairs corresponding to each package
  294. matching NAME and VERSION."
  295. (define cache
  296. (load-package-cache (current-profile)))
  297. (if (and cache (cache-is-authoritative?))
  298. (match (cache-lookup cache name)
  299. (#f '())
  300. ((#(name versions modules symbols outputs
  301. supported? deprecated?
  302. files lines columns) ...)
  303. (fold (lambda (version* file line column result)
  304. (if (and file
  305. (or (not version)
  306. (version-prefix? version version*)))
  307. (alist-cons version* (location file line column)
  308. result)
  309. result))
  310. '()
  311. versions files lines columns)))
  312. (map (lambda (package)
  313. (cons (package-version package) (package-location package)))
  314. (find-packages-by-name/direct name version))))
  315. (define (find-best-packages-by-name name version)
  316. "If version is #f, return the list of packages named NAME with the highest
  317. version numbers; otherwise, return the list of packages named NAME and at
  318. VERSION."
  319. (if version
  320. (find-packages-by-name name version)
  321. (match (find-packages-by-name name)
  322. (()
  323. '())
  324. ((matches ...)
  325. ;; Return the subset of MATCHES with the higher version number.
  326. (let ((highest (package-version (first matches))))
  327. (take-while (lambda (p)
  328. (string=? (package-version p) highest))
  329. matches))))))
  330. (define (generate-package-cache directory)
  331. "Generate under DIRECTORY a cache of all the available packages.
  332. The primary purpose of the cache is to speed up package lookup by name such
  333. that we don't have to traverse and load all the package modules, thereby also
  334. reducing the memory footprint."
  335. (define cache-file
  336. (string-append directory %package-cache-file))
  337. (define (expand-cache module symbol variable result+seen)
  338. (match (false-if-exception (variable-ref variable))
  339. ((? package? package)
  340. (match result+seen
  341. ((result . seen)
  342. (if (or (vhash-assq package seen)
  343. (hidden-package? package))
  344. (cons result seen)
  345. (cons (cons `#(,(package-name package)
  346. ,(package-version package)
  347. ,(module-name module)
  348. ,symbol
  349. ,(package-outputs package)
  350. ,(->bool
  351. (member (%current-system)
  352. (package-supported-systems package)))
  353. ,(->bool (package-superseded package))
  354. ,@(let ((loc (package-location package)))
  355. (if loc
  356. `(,(location-file loc)
  357. ,(location-line loc)
  358. ,(location-column loc))
  359. '(#f #f #f))))
  360. result)
  361. (vhash-consq package #t seen))))))
  362. (_
  363. result+seen)))
  364. (define exp
  365. (first
  366. (fold-module-public-variables* expand-cache
  367. (cons '() vlist-null)
  368. (all-modules (%package-module-path)
  369. #:warn
  370. warn-about-load-error))))
  371. (mkdir-p (dirname cache-file))
  372. (call-with-output-file cache-file
  373. (lambda (port)
  374. ;; Store the cache as a '.go' file. This makes loading fast and reduces
  375. ;; heap usage since some of the static data is directly mmapped.
  376. (put-bytevector port
  377. (compile `'(,@exp)
  378. #:to 'bytecode
  379. #:opts '(#:to-file? #t)))))
  380. cache-file)
  381. (define %sigint-prompt
  382. ;; The prompt to jump to upon SIGINT.
  383. (make-prompt-tag "interruptible"))
  384. (define (call-with-sigint-handler thunk handler)
  385. "Call THUNK and return its value. Upon SIGINT, call HANDLER with the signal
  386. number in the context of the continuation of the call to this function, and
  387. return its return value."
  388. (call-with-prompt %sigint-prompt
  389. (lambda ()
  390. (sigaction SIGINT
  391. (lambda (signum)
  392. (sigaction SIGINT SIG_DFL)
  393. (abort-to-prompt %sigint-prompt signum)))
  394. (dynamic-wind
  395. (const #t)
  396. thunk
  397. (cut sigaction SIGINT SIG_DFL)))
  398. (lambda (k signum)
  399. (handler signum))))
  400. ;;;
  401. ;;; Package specification.
  402. ;;;
  403. (define* (%find-package spec name version)
  404. (match (find-best-packages-by-name name version)
  405. ((pkg . pkg*)
  406. (unless (null? pkg*)
  407. (warning (G_ "ambiguous package specification `~a'~%") spec)
  408. (warning (G_ "choosing ~a@~a from ~a~%")
  409. (package-name pkg) (package-version pkg)
  410. (location->string (package-location pkg))))
  411. (match (package-superseded pkg)
  412. ((? package? new)
  413. (info (G_ "package '~a' has been superseded by '~a'~%")
  414. (package-name pkg) (package-name new))
  415. new)
  416. (#f
  417. pkg)))
  418. (x
  419. (if version
  420. (leave (G_ "~A: package not found for version ~a~%") name version)
  421. (leave (G_ "~A: unknown package~%") name)))))
  422. (define (specification->package spec)
  423. "Return a package matching SPEC. SPEC may be a package name, or a package
  424. name followed by an at-sign and a version number. If the version number is not
  425. present, return the preferred newest version."
  426. (let-values (((name version) (package-name->name+version spec)))
  427. (%find-package spec name version)))
  428. (define (specification->location spec)
  429. "Return the location of the highest-numbered package matching SPEC, a
  430. specification such as \"guile@2\" or \"emacs\"."
  431. (let-values (((name version) (package-name->name+version spec)))
  432. (match (find-package-locations name version)
  433. (()
  434. (if version
  435. (leave (G_ "~A: package not found for version ~a~%") name version)
  436. (leave (G_ "~A: unknown package~%") name)))
  437. (lst
  438. (let* ((highest (match lst (((version . _) _ ...) version)))
  439. (locations (take-while (match-lambda
  440. ((version . location)
  441. (string=? version highest)))
  442. lst)))
  443. (match locations
  444. (((version . location) . rest)
  445. (unless (null? rest)
  446. (warning (G_ "ambiguous package specification `~a'~%") spec)
  447. (warning (G_ "choosing ~a@~a from ~a~%")
  448. name version
  449. (location->string location)))
  450. location)))))))
  451. (define* (specification->package+output spec #:optional (output "out"))
  452. "Return the package and output specified by SPEC, or #f and #f; SPEC may
  453. optionally contain a version number and an output name, as in these examples:
  454. guile
  455. guile@2.0.9
  456. guile:debug
  457. guile@2.0.9:debug
  458. If SPEC does not specify a version number, return the preferred newest
  459. version; if SPEC does not specify an output, return OUTPUT."
  460. (let-values (((name version sub-drv)
  461. (package-specification->name+version+output spec output)))
  462. (match (%find-package spec name version)
  463. (#f
  464. (values #f #f))
  465. (package
  466. (if (member sub-drv (package-outputs package))
  467. (values package sub-drv)
  468. (leave (G_ "package `~a' lacks output `~a'~%")
  469. (package-full-name package)
  470. sub-drv))))))
  471. (define (specifications->manifest specs)
  472. "Given SPECS, a list of specifications such as \"emacs@25.2\" or
  473. \"guile:debug\", return a profile manifest."
  474. ;; This procedure exists mostly so users of 'guix package -m' don't have to
  475. ;; fiddle with multiple-value returns.
  476. (packages->manifest
  477. (map (compose list specification->package+output) specs)))