opam.scm 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2018 Julien Lepiller <julien@lepiller.eu>
  3. ;;; Copyright © 2020 Martin Becze <mjbecze@riseup.net>
  4. ;;; Copyright © 2021 Xinglu Chen <public@yoctocell.xyz>
  5. ;;; Copyright © 2021 Sarah Morgensen <iskarian@mgsn.dev>
  6. ;;; Copyright © 2021 Alice Brenon <alice.brenon@ens-lyon.fr>
  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 (guix import opam)
  23. #:use-module (ice-9 ftw)
  24. #:use-module (ice-9 match)
  25. #:use-module (ice-9 peg)
  26. #:use-module ((ice-9 popen) #:select (open-pipe*))
  27. #:use-module (ice-9 receive)
  28. #:use-module (ice-9 textual-ports)
  29. #:use-module (ice-9 vlist)
  30. #:use-module (srfi srfi-1)
  31. #:use-module (srfi srfi-2)
  32. #:use-module ((srfi srfi-26) #:select (cut))
  33. #:use-module ((web uri) #:select (string->uri uri->string))
  34. #:use-module ((guix build utils) #:select (dump-port find-files mkdir-p))
  35. #:use-module (guix build-system)
  36. #:use-module (guix build-system ocaml)
  37. #:use-module (guix http-client)
  38. #:use-module (guix ui)
  39. #:use-module (guix packages)
  40. #:use-module (guix upstream)
  41. #:use-module ((guix utils) #:select (cache-directory
  42. version>?
  43. call-with-temporary-output-file))
  44. #:use-module (guix import utils)
  45. #:use-module ((guix licenses) #:prefix license:)
  46. #:export (opam->guix-package
  47. opam-recursive-import
  48. %opam-updater
  49. ;; The following patterns are exported for testing purposes.
  50. string-pat
  51. multiline-string
  52. list-pat
  53. dict
  54. condition))
  55. ;; Define a PEG parser for the opam format
  56. (define-peg-pattern comment none (and "#" (* COMMCHR) "\n"))
  57. (define-peg-pattern SP none (or " " "\n" comment))
  58. (define-peg-pattern SP2 body (or " " "\n"))
  59. (define-peg-pattern QUOTE none "\"")
  60. (define-peg-pattern QUOTE2 body "\"")
  61. (define-peg-pattern COLON none ":")
  62. ;; A string character is any character that is not a quote, or a quote preceded by a backslash.
  63. (define-peg-pattern COMMCHR none
  64. (or " " "!" "\\" "\"" (range #\# #\頋)))
  65. (define-peg-pattern STRCHR body
  66. (or " " "!" "\n" (and (ignore "\\") "\"")
  67. (ignore "\\\n") (and (ignore "\\") "\\")
  68. (range #\# #\頋)))
  69. (define-peg-pattern operator all (or "=" "!" "<" ">"))
  70. (define-peg-pattern records body (and (* SP) (* (and (or record weird-record) (* SP)))))
  71. (define-peg-pattern record all (and key COLON (* SP) value))
  72. (define-peg-pattern weird-record all (and key (* SP) dict))
  73. (define-peg-pattern key body (+ (or (range #\a #\z) "-")))
  74. (define-peg-pattern value body (and (or conditional-value ground-value operator) (* SP)))
  75. (define-peg-pattern choice-pat all (and (ignore "(") (* SP) choice (* SP) (ignore ")")))
  76. (define-peg-pattern choice body
  77. (or (and (or conditional-value ground-value) (* SP) (ignore "|") (* SP) choice)
  78. group-pat
  79. conditional-value
  80. ground-value))
  81. (define-peg-pattern group-pat all
  82. (and (or conditional-value ground-value) (* SP) (ignore "&") (* SP)
  83. (or group-pat conditional-value ground-value)))
  84. (define-peg-pattern ground-value body (and (or multiline-string string-pat choice-pat list-pat var) (* SP)))
  85. (define-peg-pattern conditional-value all (and ground-value (* SP) condition))
  86. (define-peg-pattern string-pat all (and QUOTE (* STRCHR) QUOTE))
  87. (define-peg-pattern list-pat all (and (ignore "[") (* SP) (* (and value (* SP))) (ignore "]")))
  88. (define-peg-pattern var all (+ (or (range #\a #\z) "-")))
  89. (define-peg-pattern multiline-string all
  90. (and QUOTE QUOTE QUOTE (* SP)
  91. (* (or SP2 STRCHR (and QUOTE2 (not-followed-by QUOTE))
  92. (and QUOTE2 QUOTE2 (not-followed-by QUOTE))))
  93. QUOTE QUOTE QUOTE))
  94. (define-peg-pattern dict all (and (ignore "{") (* SP) records (* SP) (ignore "}")))
  95. (define-peg-pattern condition body (and (ignore "{") condition-form (ignore "}")))
  96. (define-peg-pattern condition-form body
  97. (and
  98. (* SP)
  99. (or condition-and condition-or condition-form2)
  100. (* SP)))
  101. (define-peg-pattern condition-form2 body
  102. (and (* SP) (or condition-greater-or-equal condition-greater
  103. condition-lower-or-equal condition-lower
  104. condition-neq condition-eq condition-not
  105. condition-content) (* SP)))
  106. ;(define-peg-pattern condition-operator all (and (ignore operator) (* SP) condition-string))
  107. (define-peg-pattern condition-greater-or-equal all (and (ignore (and ">" "=")) (* SP) condition-string))
  108. (define-peg-pattern condition-greater all (and (ignore ">") (* SP) condition-string))
  109. (define-peg-pattern condition-lower-or-equal all (and (ignore (and "<" "=")) (* SP) condition-string))
  110. (define-peg-pattern condition-lower all (and (ignore "<") (* SP) condition-string))
  111. (define-peg-pattern condition-and all (and condition-form2 (* SP) (? (ignore "&")) (* SP) condition-form))
  112. (define-peg-pattern condition-or all (and condition-form2 (* SP) (ignore "|") (* SP) condition-form))
  113. (define-peg-pattern condition-eq all (and (? condition-content) (* SP) (ignore "=") (* SP) condition-content))
  114. (define-peg-pattern condition-neq all (and (? condition-content) (* SP) (ignore (and "!" "=")) (* SP) condition-content))
  115. (define-peg-pattern condition-not all (and (ignore (and "!")) (* SP) condition-content))
  116. (define-peg-pattern condition-content body (or condition-paren condition-string condition-var))
  117. (define-peg-pattern condition-content2 body (and condition-content (* SP) (not-followed-by (or "&" "=" "!"))))
  118. (define-peg-pattern condition-paren body (and "(" condition-form ")"))
  119. (define-peg-pattern condition-string all (and QUOTE (* STRCHR) QUOTE))
  120. (define-peg-pattern condition-var all (+ (or (range #\a #\z) "-" ":")))
  121. (define (opam-cache-directory path)
  122. (string-append (cache-directory) "/opam/" path))
  123. (define known-repositories
  124. '((opam . "https://opam.ocaml.org")
  125. (coq . "https://coq.inria.fr/opam/released")
  126. (coq-released . "https://coq.inria.fr/opam/released")
  127. (coq-core-dev . "https://coq.inria.fr/opam/core-dev")
  128. (coq-extra-dev . "https://coq.inria.fr/opam/extra-dev")
  129. (grew . "http://opam.grew.fr")))
  130. (define (get-uri repo-root)
  131. (let ((archive-file (string-append repo-root "/index.tar.gz")))
  132. (or (string->uri archive-file)
  133. (begin
  134. (warning (G_ "'~a' is not a valid URI~%") archive-file)
  135. 'bad-repo))))
  136. (define (repo-type repo)
  137. (match (assoc-ref known-repositories (string->symbol repo))
  138. (#f (if (file-exists? repo)
  139. `(local ,repo)
  140. `(remote ,(get-uri repo))))
  141. (url `(remote ,(get-uri url)))))
  142. (define (update-repository input)
  143. "Make sure the cache for opam repository INPUT is up-to-date"
  144. (let* ((output (opam-cache-directory (basename (port-filename input))))
  145. (cached-date (if (file-exists? output)
  146. (stat:mtime (stat output))
  147. (begin (mkdir-p output) 0))))
  148. (when (> (stat:mtime (stat input)) cached-date)
  149. (call-with-port
  150. (open-pipe* OPEN_WRITE "tar" "xz" "-C" output "-f" "-")
  151. (cut dump-port input <>)))
  152. output))
  153. (define* (get-opam-repository #:optional (repo "opam"))
  154. "Update or fetch the latest version of the opam repository and return the
  155. path to the repository."
  156. (match (repo-type repo)
  157. (('local p) p)
  158. (('remote 'bad-repo) #f) ; to weed it out during filter-map in opam-fetch
  159. (('remote r) (call-with-port (http-fetch/cached r) update-repository))))
  160. ;; Prevent Guile 3 from inlining this procedure so we can mock it in tests.
  161. (set! get-opam-repository get-opam-repository)
  162. (define (get-version-and-file path)
  163. "Analyse a candidate path and return an list containing information for proper
  164. version comparison as well as the source path for metadata."
  165. (and-let* ((metadata-file (string-append path "/opam"))
  166. (filename (basename path))
  167. (version (string-join (cdr (string-split filename #\.)) ".")))
  168. (and (file-exists? metadata-file)
  169. (eq? 'regular (stat:type (stat metadata-file)))
  170. (if (string-prefix? "v" version)
  171. `(V ,(substring version 1) ,metadata-file)
  172. `(digits ,version ,metadata-file)))))
  173. (define (keep-max-version a b)
  174. "Version comparison on the lists returned by the previous function taking the
  175. janestreet re-versioning into account (v-prefixed come first)."
  176. (match (cons a b)
  177. ((('V va _) . ('V vb _)) (if (version>? va vb) a b))
  178. ((('V _ _) . _) a)
  179. ((_ . ('V _ _)) b)
  180. ((('digits va _) . ('digits vb _)) (if (version>? va vb) a b))))
  181. (define (find-latest-version package repository)
  182. "Get the latest version of a package as described in the given repository."
  183. (let ((packages (string-append repository "/packages"))
  184. (filter (make-regexp (string-append "^" package "\\."))))
  185. (reduce keep-max-version #f
  186. (filter-map
  187. get-version-and-file
  188. (find-files packages filter #:directories? #t)))))
  189. (define (get-metadata opam-file)
  190. (with-input-from-file opam-file
  191. (lambda _
  192. (peg:tree (match-pattern records (get-string-all (current-input-port)))))))
  193. (define (substitute-char str what with)
  194. (string-join (string-split str what) with))
  195. (define (ocaml-name->guix-name name)
  196. (substitute-char
  197. (cond
  198. ((equal? name "ocamlfind") "ocaml-findlib")
  199. ((equal? name "coq") name)
  200. ((string-prefix? "ocaml" name) name)
  201. ((string-prefix? "conf-" name) (substring name 5))
  202. (else (string-append "ocaml-" name)))
  203. #\_ "-"))
  204. (define (metadata-ref file lookup)
  205. (fold (lambda (record acc)
  206. (match record
  207. ((record key val)
  208. (if (equal? key lookup)
  209. (match val
  210. (('list-pat . stuff) stuff)
  211. (('string-pat stuff) stuff)
  212. (('multiline-string stuff) stuff)
  213. (('dict records ...) records)
  214. (_ #f))
  215. acc))))
  216. #f file))
  217. (define (native? condition)
  218. (match condition
  219. (('condition-var var)
  220. (match var
  221. ("with-test" #t)
  222. ("test" #t)
  223. ("build" #t)
  224. (_ #f)))
  225. ((or ('condition-or cond-left cond-right) ('condition-and cond-left cond-right))
  226. (or (native? cond-left)
  227. (native? cond-right)))
  228. (_ #f)))
  229. (define (dependency->input dependency)
  230. (match dependency
  231. (('string-pat str) str)
  232. ;; Arbitrary select the first dependency
  233. (('choice-pat choice ...) (dependency->input (car choice)))
  234. (('group-pat val ...) (map dependency->input val))
  235. (('conditional-value val condition)
  236. (if (native? condition) "" (dependency->input val)))))
  237. (define (dependency->native-input dependency)
  238. (match dependency
  239. (('string-pat str) "")
  240. ;; Arbitrary select the first dependency
  241. (('choice-pat choice ...) (dependency->native-input (car choice)))
  242. (('group-pat val ...) (map dependency->native-input val))
  243. (('conditional-value val condition)
  244. (if (native? condition) (dependency->input val) ""))))
  245. (define (dependency->name dependency)
  246. (match dependency
  247. (('string-pat str) str)
  248. ;; Arbitrary select the first dependency
  249. (('choice-pat choice ...) (dependency->name (car choice)))
  250. (('group-pat val ...) (map dependency->name val))
  251. (('conditional-value val condition)
  252. (dependency->name val))))
  253. (define (dependency-list->names lst)
  254. (filter
  255. (lambda (name)
  256. (not (or
  257. (string-prefix? "conf-" name)
  258. (equal? name "ocaml")
  259. (equal? name "findlib"))))
  260. (map dependency->name lst)))
  261. (define (ocaml-names->guix-names names)
  262. (map ocaml-name->guix-name
  263. (remove (lambda (name)
  264. (or (equal? "" name))
  265. (equal? "ocaml" name))
  266. names)))
  267. (define (filter-dependencies depends)
  268. "Remove implicit dependencies from the list of dependencies in @var{depends}."
  269. (filter (lambda (name)
  270. (and (not (member name '("" "ocaml" "ocamlfind" "dune" "jbuilder")))
  271. (not (string-prefix? "base-" name))))
  272. depends))
  273. (define (depends->inputs depends)
  274. (filter-dependencies (map dependency->input depends)))
  275. (define (depends->native-inputs depends)
  276. (filter (lambda (name) (not (equal? "" name)))
  277. (map dependency->native-input depends)))
  278. (define (dependency-list->inputs lst)
  279. (map string->symbol
  280. (ocaml-names->guix-names lst)))
  281. (define* (opam-fetch name #:optional (repositories-specs '("opam")))
  282. (or (fold (lambda (repository others)
  283. (match (find-latest-version name repository)
  284. ((_ version file) `(("metadata" ,@(get-metadata file))
  285. ("version" . ,version)))
  286. (_ others)))
  287. #f
  288. (filter-map get-opam-repository repositories-specs))
  289. (warning (G_ "opam: package '~a' not found~%") name)))
  290. (define* (opam->guix-package name #:key (repo 'opam) version)
  291. "Import OPAM package NAME from REPOSITORY (a directory name) or, if
  292. REPOSITORY is #f, from the official OPAM repository. Return a 'package' sexp
  293. or #f on failure."
  294. (and-let* ((with-opam (if (member "opam" repo) repo (cons "opam" repo)))
  295. (opam-file (opam-fetch name with-opam))
  296. (version (assoc-ref opam-file "version"))
  297. (opam-content (assoc-ref opam-file "metadata"))
  298. (url-dict (metadata-ref opam-content "url"))
  299. (source-url (or (metadata-ref url-dict "src")
  300. (metadata-ref url-dict "archive")))
  301. (requirements (metadata-ref opam-content "depends"))
  302. (names (dependency-list->names requirements))
  303. (dependencies (filter-dependencies names))
  304. (native-dependencies (depends->native-inputs requirements))
  305. (inputs (dependency-list->inputs (depends->inputs requirements)))
  306. (native-inputs (dependency-list->inputs
  307. ;; Do not add dune nor jbuilder since they are
  308. ;; implicit inputs of the dune-build-system.
  309. (filter
  310. (lambda (name)
  311. (not (member name '("dune" "jbuilder"))))
  312. native-dependencies))))
  313. (let ((use-dune? (member "dune" names)))
  314. (call-with-temporary-output-file
  315. (lambda (temp port)
  316. (and (url-fetch source-url temp)
  317. (values
  318. `(package
  319. (name ,(ocaml-name->guix-name name))
  320. (version ,version)
  321. (source
  322. (origin
  323. (method url-fetch)
  324. (uri ,source-url)
  325. (sha256 (base32 ,(guix-hash-url temp)))))
  326. (build-system ,(if use-dune?
  327. 'dune-build-system
  328. 'ocaml-build-system))
  329. ,@(if (null? inputs)
  330. '()
  331. `((propagated-inputs (list ,@inputs))))
  332. ,@(if (null? native-inputs)
  333. '()
  334. `((native-inputs (list ,@native-inputs))))
  335. ,@(if (equal? name (guix-name->opam-name (ocaml-name->guix-name name)))
  336. '()
  337. `((properties
  338. ,(list 'quasiquote `((upstream-name . ,name))))))
  339. (home-page ,(metadata-ref opam-content "homepage"))
  340. (synopsis ,(metadata-ref opam-content "synopsis"))
  341. (description ,(beautify-description
  342. (metadata-ref opam-content "description")))
  343. (license ,(spdx-string->license
  344. (metadata-ref opam-content "license"))))
  345. (filter
  346. (lambda (name)
  347. (not (member name '("dune" "jbuilder"))))
  348. dependencies))))))))
  349. (define* (opam-recursive-import package-name #:key repo)
  350. (recursive-import package-name
  351. #:repo->guix-package opam->guix-package
  352. #:guix-name ocaml-name->guix-name
  353. #:repo repo))
  354. (define (guix-name->opam-name name)
  355. (if (string-prefix? "ocaml-" name)
  356. (substring name 6)
  357. name))
  358. (define (guix-package->opam-name package)
  359. "Given an OCaml PACKAGE built from OPAM, return the name of the
  360. package in OPAM."
  361. (let ((upstream-name (assoc-ref
  362. (package-properties package)
  363. 'upstream-name))
  364. (name (package-name package)))
  365. (if upstream-name
  366. upstream-name
  367. (guix-name->opam-name name))))
  368. (define (opam-package? package)
  369. "Return true if PACKAGE is an OCaml package from OPAM"
  370. (and
  371. (member (build-system-name (package-build-system package)) '(dune ocaml))
  372. (not (string-prefix? "ocaml4" (package-name package)))))
  373. (define (latest-release package)
  374. "Return an <upstream-source> for the latest release of PACKAGE."
  375. (and-let* ((opam-name (guix-package->opam-name package))
  376. (opam-file (opam-fetch opam-name))
  377. (version (assoc-ref opam-file "version"))
  378. (opam-content (assoc-ref opam-file "metadata"))
  379. (url-dict (metadata-ref opam-content "url"))
  380. (source-url (metadata-ref url-dict "src")))
  381. (upstream-source
  382. (package (package-name package))
  383. (version version)
  384. (urls (list source-url)))))
  385. (define %opam-updater
  386. (upstream-updater
  387. (name 'opam)
  388. (description "Updater for OPAM packages")
  389. (pred opam-package?)
  390. (latest latest-release)))