packages.scm 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2012, 2013, 2014, 2015, 2016, 2017 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. #:autoload (guix profiles) (packages->manifest)
  32. #:use-module (ice-9 vlist)
  33. #:use-module (ice-9 match)
  34. #:use-module (srfi srfi-1)
  35. #:use-module (srfi srfi-11)
  36. #:use-module (srfi srfi-26)
  37. #:use-module (srfi srfi-34)
  38. #:use-module (srfi srfi-35)
  39. #:use-module (srfi srfi-39)
  40. #:export (search-patch
  41. search-patches
  42. search-auxiliary-file
  43. search-bootstrap-binary
  44. %patch-path
  45. %auxiliary-files-path
  46. %bootstrap-binaries-path
  47. %package-module-path
  48. fold-packages
  49. find-packages-by-name
  50. find-best-packages-by-name
  51. find-newest-available-packages
  52. specification->package
  53. specification->package+output
  54. specifications->manifest))
  55. ;;; Commentary:
  56. ;;;
  57. ;;; General utilities for the software distribution---i.e., the modules under
  58. ;;; (gnu packages ...).
  59. ;;;
  60. ;;; Code:
  61. ;; By default, we store patches, auxiliary files and bootstrap binaries
  62. ;; alongside Guile modules. This is so that these extra files can be
  63. ;; found without requiring a special setup, such as a specific
  64. ;; installation directory and an extra environment variable. One
  65. ;; advantage of this setup is that everything just works in an
  66. ;; auto-compilation setting.
  67. (define %bootstrap-binaries-path
  68. (make-parameter
  69. (map (cut string-append <> "/gnu/packages/bootstrap")
  70. %load-path)))
  71. (define %auxiliary-files-path
  72. (make-parameter
  73. (map (cut string-append <> "/gnu/packages/aux-files")
  74. %load-path)))
  75. (define (search-auxiliary-file file-name)
  76. "Search the auxiliary FILE-NAME. Return #f if not found."
  77. (search-path (%auxiliary-files-path) file-name))
  78. (define (search-patch file-name)
  79. "Search the patch FILE-NAME. Raise an error if not found."
  80. (or (search-path (%patch-path) file-name)
  81. (raise (condition
  82. (&message (message (format #f (G_ "~a: patch not found")
  83. file-name)))))))
  84. (define-syntax-rule (search-patches file-name ...)
  85. "Return the list of absolute file names corresponding to each
  86. FILE-NAME found in %PATCH-PATH."
  87. (list (search-patch file-name) ...))
  88. (define (search-bootstrap-binary file-name system)
  89. "Search the bootstrap binary FILE-NAME for SYSTEM. Raise an error if not
  90. found."
  91. (or (search-path (%bootstrap-binaries-path)
  92. (string-append system "/" file-name))
  93. (raise (condition
  94. (&message
  95. (message
  96. (format #f (G_ "could not find bootstrap binary '~a' \
  97. for system '~a'")
  98. file-name system)))))))
  99. (define %distro-root-directory
  100. ;; Absolute file name of the module hierarchy.
  101. (dirname (search-path %load-path "guix.scm")))
  102. (define %package-module-path
  103. ;; Search path for package modules. Each item must be either a directory
  104. ;; name or a pair whose car is a directory and whose cdr is a sub-directory
  105. ;; to narrow the search.
  106. (let* ((not-colon (char-set-complement (char-set #\:)))
  107. (environment (string-tokenize (or (getenv "GUIX_PACKAGE_PATH") "")
  108. not-colon)))
  109. ;; Automatically add items from $GUIX_PACKAGE_PATH to Guile's search path.
  110. (for-each (lambda (directory)
  111. (set! %load-path (cons directory %load-path))
  112. (set! %load-compiled-path
  113. (cons directory %load-compiled-path)))
  114. environment)
  115. (make-parameter
  116. (append environment `((,%distro-root-directory . "gnu/packages"))))))
  117. (define %patch-path
  118. ;; Define it after '%package-module-path' so that '%load-path' contains user
  119. ;; directories, allowing patches in $GUIX_PACKAGE_PATH to be found.
  120. (make-parameter
  121. (map (lambda (directory)
  122. (if (string=? directory %distro-root-directory)
  123. (string-append directory "/gnu/packages/patches")
  124. directory))
  125. %load-path)))
  126. (define* (fold-packages proc init
  127. #:optional
  128. (modules (all-modules (%package-module-path)))
  129. #:key (select? (negate hidden-package?)))
  130. "Call (PROC PACKAGE RESULT) for each available package defined in one of
  131. MODULES that matches SELECT?, using INIT as the initial value of RESULT. It
  132. is guaranteed to never traverse the same package twice."
  133. (fold-module-public-variables (lambda (object result)
  134. (if (and (package? object) (select? object))
  135. (proc object result)
  136. result))
  137. init
  138. modules))
  139. (define find-packages-by-name
  140. (let ((packages (delay
  141. (fold-packages (lambda (p r)
  142. (vhash-cons (package-name p) p r))
  143. vlist-null)))
  144. (version>? (lambda (p1 p2)
  145. (version>? (package-version p1) (package-version p2)))))
  146. (lambda* (name #:optional version)
  147. "Return the list of packages with the given NAME. If VERSION is not #f,
  148. then only return packages whose version is prefixed by VERSION, sorted in
  149. decreasing version order."
  150. (let ((matching (sort (vhash-fold* cons '() name (force packages))
  151. version>?)))
  152. (if version
  153. (filter (lambda (package)
  154. (string-prefix? version (package-version package)))
  155. matching)
  156. matching)))))
  157. (define find-newest-available-packages
  158. (mlambda ()
  159. "Return a vhash keyed by package names, and with
  160. associated values of the form
  161. (newest-version newest-package ...)
  162. where the preferred package is listed first."
  163. ;; FIXME: Currently, the preferred package is whichever one
  164. ;; was found last by 'fold-packages'. Find a better solution.
  165. (fold-packages (lambda (p r)
  166. (let ((name (package-name p))
  167. (version (package-version p)))
  168. (match (vhash-assoc name r)
  169. ((_ newest-so-far . pkgs)
  170. (case (version-compare version newest-so-far)
  171. ((>) (vhash-cons name `(,version ,p) r))
  172. ((=) (vhash-cons name `(,version ,p ,@pkgs) r))
  173. ((<) r)))
  174. (#f (vhash-cons name `(,version ,p) r)))))
  175. vlist-null)))
  176. (define (find-best-packages-by-name name version)
  177. "If version is #f, return the list of packages named NAME with the highest
  178. version numbers; otherwise, return the list of packages named NAME and at
  179. VERSION."
  180. (if version
  181. (find-packages-by-name name version)
  182. (match (vhash-assoc name (find-newest-available-packages))
  183. ((_ version pkgs ...) pkgs)
  184. (#f '()))))
  185. (define %sigint-prompt
  186. ;; The prompt to jump to upon SIGINT.
  187. (make-prompt-tag "interruptible"))
  188. (define (call-with-sigint-handler thunk handler)
  189. "Call THUNK and return its value. Upon SIGINT, call HANDLER with the signal
  190. number in the context of the continuation of the call to this function, and
  191. return its return value."
  192. (call-with-prompt %sigint-prompt
  193. (lambda ()
  194. (sigaction SIGINT
  195. (lambda (signum)
  196. (sigaction SIGINT SIG_DFL)
  197. (abort-to-prompt %sigint-prompt signum)))
  198. (dynamic-wind
  199. (const #t)
  200. thunk
  201. (cut sigaction SIGINT SIG_DFL)))
  202. (lambda (k signum)
  203. (handler signum))))
  204. ;;;
  205. ;;; Package specification.
  206. ;;;
  207. (define* (%find-package spec name version)
  208. (match (find-best-packages-by-name name version)
  209. ((pkg . pkg*)
  210. (unless (null? pkg*)
  211. (warning (G_ "ambiguous package specification `~a'~%") spec)
  212. (warning (G_ "choosing ~a@~a from ~a~%")
  213. (package-name pkg) (package-version pkg)
  214. (location->string (package-location pkg))))
  215. (match (package-superseded pkg)
  216. ((? package? new)
  217. (info (G_ "package '~a' has been superseded by '~a'~%")
  218. (package-name pkg) (package-name new))
  219. new)
  220. (#f
  221. pkg)))
  222. (x
  223. (if version
  224. (leave (G_ "~A: package not found for version ~a~%") name version)
  225. (leave (G_ "~A: unknown package~%") name)))))
  226. (define (specification->package spec)
  227. "Return a package matching SPEC. SPEC may be a package name, or a package
  228. name followed by an at-sign and a version number. If the version number is not
  229. present, return the preferred newest version."
  230. (let-values (((name version) (package-name->name+version spec)))
  231. (%find-package spec name version)))
  232. (define* (specification->package+output spec #:optional (output "out"))
  233. "Return the package and output specified by SPEC, or #f and #f; SPEC may
  234. optionally contain a version number and an output name, as in these examples:
  235. guile
  236. guile@2.0.9
  237. guile:debug
  238. guile@2.0.9:debug
  239. If SPEC does not specify a version number, return the preferred newest
  240. version; if SPEC does not specify an output, return OUTPUT."
  241. (let-values (((name version sub-drv)
  242. (package-specification->name+version+output spec output)))
  243. (match (%find-package spec name version)
  244. (#f
  245. (values #f #f))
  246. (package
  247. (if (member sub-drv (package-outputs package))
  248. (values package sub-drv)
  249. (leave (G_ "package `~a' lacks output `~a'~%")
  250. (package-full-name package)
  251. sub-drv))))))
  252. (define (specifications->manifest specs)
  253. "Given SPECS, a list of specifications such as \"emacs@25.2\" or
  254. \"guile:debug\", return a profile manifest."
  255. ;; This procedure exists mostly so users of 'guix package -m' don't have to
  256. ;; fiddle with multiple-value returns.
  257. (packages->manifest
  258. (map (compose list specification->package+output) specs)))