You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

349 lines
13 KiB

  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2015 Federico Beffa <beffa@fbengineering.ch>
  3. ;;; Copyright © 2016 Eric Bavier <bavier@member.fsf.org>
  4. ;;; Copyright © 2016 Nils Gillmann <ng0@n0.is>
  5. ;;; Copyright © 2018 Ricardo Wurmus <rekado@elephly.net>
  6. ;;;
  7. ;;; This file is part of GNU Guix.
  8. ;;;
  9. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  10. ;;; under the terms of the GNU General Public License as published by
  11. ;;; the Free Software Foundation; either version 3 of the License, or (at
  12. ;;; your option) any later version.
  13. ;;;
  14. ;;; GNU Guix is distributed in the hope that it will be useful, but
  15. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  16. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. ;;; GNU General Public License for more details.
  18. ;;;
  19. ;;; You should have received a copy of the GNU General Public License
  20. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  21. (define-module (guix import hackage)
  22. #:use-module (ice-9 match)
  23. #:use-module (ice-9 regex)
  24. #:use-module (srfi srfi-34)
  25. #:use-module (srfi srfi-26)
  26. #:use-module (srfi srfi-11)
  27. #:use-module (srfi srfi-1)
  28. #:use-module ((guix download) #:select (download-to-store url-fetch))
  29. #:use-module ((guix utils) #:select (package-name->name+version
  30. canonical-newline-port))
  31. #:use-module (guix http-client)
  32. #:use-module ((guix import utils) #:select (factorize-uri recursive-import))
  33. #:use-module (guix import cabal)
  34. #:use-module (guix store)
  35. #:use-module (gcrypt hash)
  36. #:use-module (guix base32)
  37. #:use-module (guix memoization)
  38. #:use-module (guix upstream)
  39. #:use-module (guix packages)
  40. #:use-module ((guix utils) #:select (call-with-temporary-output-file))
  41. #:export (hackage->guix-package
  42. hackage-recursive-import
  43. %hackage-updater
  44. guix-package->hackage-name
  45. hackage-name->package-name
  46. hackage-fetch
  47. hackage-source-url
  48. hackage-cabal-url
  49. hackage-package?))
  50. (define ghc-standard-libraries
  51. ;; List of libraries distributed with ghc (7.10.2). We include GHC itself as
  52. ;; some packages list it.
  53. '("array"
  54. "base"
  55. "bin-package-db"
  56. "binary"
  57. "bytestring"
  58. "cabal" ;; in the output of `ghc-pkg list` Cabal is uppercased, but
  59. ;; hackage-name->package-name takes this into account.
  60. "containers"
  61. "deepseq"
  62. "directory"
  63. "filepath"
  64. "ghc"
  65. "ghc-prim"
  66. "haskeline"
  67. "hoopl"
  68. "hpc"
  69. "integer-gmp"
  70. "pretty"
  71. "process"
  72. "rts"
  73. "template-haskell"
  74. "terminfo"
  75. "time"
  76. "transformers"
  77. "unix"
  78. "win32"
  79. "xhtml"))
  80. (define package-name-prefix "ghc-")
  81. (define (hackage-source-url name version)
  82. "Given a Hackage package NAME and VERSION, return a url to the source
  83. tarball."
  84. (string-append "https://hackage.haskell.org/package/" name
  85. "/" name "-" version ".tar.gz"))
  86. (define* (hackage-cabal-url name #:optional version)
  87. "Given a Hackage package NAME and VERSION, return a url to the corresponding
  88. .cabal file on Hackage. If VERSION is #f or missing, the url for the latest
  89. version is returned."
  90. (if version
  91. (string-append "https://hackage.haskell.org/package/"
  92. name "-" version "/" name ".cabal")
  93. (string-append "https://hackage.haskell.org/package/"
  94. name "/" name ".cabal")))
  95. (define (hackage-name->package-name name)
  96. "Given the NAME of a Cabal package, return the corresponding Guix name."
  97. (if (string-prefix? package-name-prefix name)
  98. (string-downcase name)
  99. (string-append package-name-prefix (string-downcase name))))
  100. (define guix-package->hackage-name
  101. (let ((uri-rx (make-regexp "https?://hackage.haskell.org/package/([^/]+)/.*"))
  102. (name-rx (make-regexp "(.*)-[0-9\\.]+")))
  103. (lambda (package)
  104. "Given a Guix package name, return the corresponding Hackage name."
  105. (let* ((source-url (and=> (package-source package) origin-uri))
  106. (name (match:substring (regexp-exec uri-rx source-url) 1)))
  107. (match (regexp-exec name-rx name)
  108. (#f name)
  109. (m (match:substring m 1)))))))
  110. (define (hackage-fetch name-version)
  111. "Return the Cabal file for the package NAME-VERSION, or #f on failure. If
  112. the version part is omitted from the package name, then return the latest
  113. version."
  114. (guard (c ((and (http-get-error? c)
  115. (= 404 (http-get-error-code c)))
  116. #f)) ;"expected" if package is unknown
  117. (let-values (((name version) (package-name->name+version name-version)))
  118. (let* ((url (hackage-cabal-url name version))
  119. (port (http-fetch url))
  120. (result (read-cabal (canonical-newline-port port))))
  121. (close-port port)
  122. result))))
  123. (define string->license
  124. ;; List of valid values from
  125. ;; https://www.haskell.org
  126. ;; /cabal/release/cabal-latest/doc/API/Cabal/Distribution-License.html.
  127. (match-lambda
  128. ("GPL-2" 'gpl2)
  129. ("GPL-3" 'gpl3)
  130. ("GPL" "'gpl??")
  131. ("AGPL-3" 'agpl3)
  132. ("AGPL" "'agpl??")
  133. ("LGPL-2.1" 'lgpl2.1)
  134. ("LGPL-3" 'lgpl3)
  135. ("LGPL" "'lgpl??")
  136. ("BSD2" 'bsd-2)
  137. ("BSD3" 'bsd-3)
  138. ("MIT" 'expat)
  139. ("ISC" 'isc)
  140. ("MPL" 'mpl2.0)
  141. ("Apache-2.0" 'asl2.0)
  142. ((x) (string->license x))
  143. ((lst ...) `(list ,@(map string->license lst)))
  144. (_ #f)))
  145. (define (cabal-dependencies->names cabal)
  146. "Return the list of dependencies names from the CABAL package object,
  147. not including test suite dependencies or custom-setup dependencies."
  148. (let* ((lib (cabal-package-library cabal))
  149. (lib-deps (if (pair? lib)
  150. (map cabal-dependency-name
  151. (append-map cabal-library-dependencies lib))
  152. '()))
  153. (exe (cabal-package-executables cabal))
  154. (exe-deps (if (pair? exe)
  155. (map cabal-dependency-name
  156. (append-map cabal-executable-dependencies exe))
  157. '())))
  158. (delete-duplicates (append lib-deps exe-deps))))
  159. (define (cabal-test-dependencies->names cabal)
  160. "Return the list of test suite dependencies from the CABAL package
  161. object."
  162. (let* ((ts (cabal-package-test-suites cabal))
  163. (ts-deps (if (pair? ts)
  164. (map cabal-dependency-name
  165. (append-map cabal-test-suite-dependencies ts))
  166. '())))
  167. ts-deps))
  168. (define (cabal-custom-setup-dependencies->names cabal)
  169. "Return the list of custom-setup dependencies from the CABAL package
  170. object."
  171. (let* ((custom-setup-dependencies (or (and=> (cabal-package-custom-setup cabal)
  172. cabal-custom-setup-dependencies)
  173. '())))
  174. (map cabal-dependency-name custom-setup-dependencies)))
  175. (define (filter-dependencies dependencies own-name)
  176. "Filter the dependencies included with the GHC compiler from DEPENDENCIES, a
  177. list with the names of dependencies. OWN-NAME is the name of the Cabal
  178. package being processed and is used to filter references to itself."
  179. (filter (lambda (d) (not (member (string-downcase d)
  180. (cons own-name ghc-standard-libraries))))
  181. dependencies))
  182. (define* (hackage-module->sexp cabal #:key (include-test-dependencies? #t))
  183. "Return the `package' S-expression for a Cabal package. CABAL is the
  184. representation of a Cabal file as produced by 'read-cabal'."
  185. (define name
  186. (cabal-package-name cabal))
  187. (define version
  188. (cabal-package-version cabal))
  189. (define source-url
  190. (hackage-source-url name version))
  191. (define hackage-dependencies
  192. ((compose (cut filter-dependencies <>
  193. (cabal-package-name cabal))
  194. (cut cabal-dependencies->names <>))
  195. cabal))
  196. (define hackage-native-dependencies
  197. ((compose (cut filter-dependencies <>
  198. (cabal-package-name cabal))
  199. ;; FIXME: Check include-test-dependencies?
  200. (lambda (cabal)
  201. (append (if include-test-dependencies?
  202. (cabal-test-dependencies->names cabal)
  203. '())
  204. (cabal-custom-setup-dependencies->names cabal))))
  205. cabal))
  206. (define dependencies
  207. (map (lambda (name)
  208. (list name (list 'unquote (string->symbol name))))
  209. (map hackage-name->package-name
  210. hackage-dependencies)))
  211. (define native-dependencies
  212. (map (lambda (name)
  213. (list name (list 'unquote (string->symbol name))))
  214. (map hackage-name->package-name
  215. hackage-native-dependencies)))
  216. (define (maybe-inputs input-type inputs)
  217. (match inputs
  218. (()
  219. '())
  220. ((inputs ...)
  221. (list (list input-type
  222. (list 'quasiquote inputs))))))
  223. (define (maybe-arguments)
  224. (if (not include-test-dependencies?)
  225. '((arguments `(#:tests? #f)))
  226. '()))
  227. (let ((tarball (with-store store
  228. (download-to-store store source-url))))
  229. (values
  230. `(package
  231. (name ,(hackage-name->package-name name))
  232. (version ,version)
  233. (source (origin
  234. (method url-fetch)
  235. (uri (string-append ,@(factorize-uri source-url version)))
  236. (sha256
  237. (base32
  238. ,(if tarball
  239. (bytevector->nix-base32-string (file-sha256 tarball))
  240. "failed to download tar archive")))))
  241. (build-system haskell-build-system)
  242. ,@(maybe-inputs 'inputs dependencies)
  243. ,@(maybe-inputs 'native-inputs native-dependencies)
  244. ,@(maybe-arguments)
  245. (home-page ,(cabal-package-home-page cabal))
  246. (synopsis ,(cabal-package-synopsis cabal))
  247. (description ,(cabal-package-description cabal))
  248. (license ,(string->license (cabal-package-license cabal))))
  249. (append hackage-dependencies hackage-native-dependencies))))
  250. (define hackage->guix-package
  251. (memoize
  252. (lambda* (package-name #:key
  253. (include-test-dependencies? #t)
  254. (port #f)
  255. (cabal-environment '()))
  256. "Fetch the Cabal file for PACKAGE-NAME from hackage.haskell.org, or, if the
  257. called with keyword parameter PORT, from PORT. Return the `package'
  258. S-expression corresponding to that package, or #f on failure.
  259. CABAL-ENVIRONMENT is an alist defining the environment in which the Cabal
  260. conditionals are evaluated. The accepted keys are: \"os\", \"arch\", \"impl\"
  261. and the name of a flag. The value associated with a flag has to be either the
  262. symbol 'true' or 'false'. The value associated with other keys has to conform
  263. to the Cabal file format definition. The default value associated with the
  264. keys \"os\", \"arch\" and \"impl\" is \"linux\", \"x86_64\" and \"ghc\"
  265. respectively."
  266. (let ((cabal-meta (if port
  267. (read-cabal (canonical-newline-port port))
  268. (hackage-fetch package-name))))
  269. (and=> cabal-meta (compose (cut hackage-module->sexp <>
  270. #:include-test-dependencies?
  271. include-test-dependencies?)
  272. (cut eval-cabal <> cabal-environment)))))))
  273. (define* (hackage-recursive-import package-name . args)
  274. (recursive-import package-name #f
  275. #:repo->guix-package (lambda (name repo)
  276. (apply hackage->guix-package (cons name args)))
  277. #:guix-name hackage-name->package-name))
  278. (define (hackage-package? package)
  279. "Return #t if PACKAGE is a Haskell package from Hackage."
  280. (define haskell-url?
  281. (let ((hackage-rx (make-regexp "https?://hackage.haskell.org")))
  282. (lambda (url)
  283. (regexp-exec hackage-rx url))))
  284. (let ((source-url (and=> (package-source package) origin-uri))
  285. (fetch-method (and=> (package-source package) origin-method)))
  286. (and (eq? fetch-method url-fetch)
  287. (match source-url
  288. ((? string?)
  289. (haskell-url? source-url))
  290. ((source-url ...)
  291. (any haskell-url? source-url))))))
  292. (define (latest-release package)
  293. "Return an <upstream-source> for the latest release of PACKAGE."
  294. (let* ((hackage-name (guix-package->hackage-name package))
  295. (cabal-meta (hackage-fetch hackage-name)))
  296. (match cabal-meta
  297. (#f
  298. (format (current-error-port)
  299. "warning: failed to parse ~a~%"
  300. (hackage-cabal-url hackage-name))
  301. #f)
  302. ((_ *** ("version" (version)))
  303. (let ((url (hackage-source-url hackage-name version)))
  304. (upstream-source
  305. (package (package-name package))
  306. (version version)
  307. (urls (list url))))))))
  308. (define %hackage-updater
  309. (upstream-updater
  310. (name 'hackage)
  311. (description "Updater for Hackage packages")
  312. (pred hackage-package?)
  313. (latest latest-release)))
  314. ;;; cabal.scm ends here