opam.scm 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  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. acc))))
  215. #f file))
  216. (define (native? condition)
  217. (match condition
  218. (('condition-var var)
  219. (match var
  220. ("with-test" #t)
  221. ("test" #t)
  222. ("build" #t)
  223. (_ #f)))
  224. ((or ('condition-or cond-left cond-right) ('condition-and cond-left cond-right))
  225. (or (native? cond-left)
  226. (native? cond-right)))
  227. (_ #f)))
  228. (define (dependency->input dependency)
  229. (match dependency
  230. (('string-pat str) str)
  231. ;; Arbitrary select the first dependency
  232. (('choice-pat choice ...) (dependency->input (car choice)))
  233. (('group-pat val ...) (map dependency->input val))
  234. (('conditional-value val condition)
  235. (if (native? condition) "" (dependency->input val)))))
  236. (define (dependency->native-input dependency)
  237. (match dependency
  238. (('string-pat str) "")
  239. ;; Arbitrary select the first dependency
  240. (('choice-pat choice ...) (dependency->native-input (car choice)))
  241. (('group-pat val ...) (map dependency->native-input val))
  242. (('conditional-value val condition)
  243. (if (native? condition) (dependency->input val) ""))))
  244. (define (dependency->name dependency)
  245. (match dependency
  246. (('string-pat str) str)
  247. ;; Arbitrary select the first dependency
  248. (('choice-pat choice ...) (dependency->name (car choice)))
  249. (('group-pat val ...) (map dependency->name val))
  250. (('conditional-value val condition)
  251. (dependency->name val))))
  252. (define (dependency-list->names lst)
  253. (filter
  254. (lambda (name)
  255. (not (or
  256. (string-prefix? "conf-" name)
  257. (equal? name "ocaml")
  258. (equal? name "findlib"))))
  259. (map dependency->name lst)))
  260. (define (ocaml-names->guix-names names)
  261. (map ocaml-name->guix-name
  262. (remove (lambda (name)
  263. (or (equal? "" name))
  264. (equal? "ocaml" name))
  265. names)))
  266. (define (filter-dependencies depends)
  267. "Remove implicit dependencies from the list of dependencies in @var{depends}."
  268. (filter (lambda (name)
  269. (and (not (member name '("" "ocaml" "ocamlfind" "dune" "jbuilder")))
  270. (not (string-prefix? "base-" name))))
  271. depends))
  272. (define (depends->inputs depends)
  273. (filter-dependencies (map dependency->input depends)))
  274. (define (depends->native-inputs depends)
  275. (filter (lambda (name) (not (equal? "" name)))
  276. (map dependency->native-input depends)))
  277. (define (dependency-list->inputs lst)
  278. (map string->symbol
  279. (ocaml-names->guix-names lst)))
  280. (define* (opam-fetch name #:optional (repositories-specs '("opam")))
  281. (or (fold (lambda (repository others)
  282. (match (find-latest-version name repository)
  283. ((_ version file) `(("metadata" ,@(get-metadata file))
  284. ("version" . ,version)))
  285. (_ others)))
  286. #f
  287. (filter-map get-opam-repository repositories-specs))
  288. (leave (G_ "package '~a' not found~%") name)))
  289. (define* (opam->guix-package name #:key (repo 'opam) version)
  290. "Import OPAM package NAME from REPOSITORY (a directory name) or, if
  291. REPOSITORY is #f, from the official OPAM repository. Return a 'package' sexp
  292. or #f on failure."
  293. (and-let* ((with-opam (if (member "opam" repo) repo (cons "opam" repo)))
  294. (opam-file (opam-fetch name with-opam))
  295. (version (assoc-ref opam-file "version"))
  296. (opam-content (assoc-ref opam-file "metadata"))
  297. (url-dict (metadata-ref opam-content "url"))
  298. (source-url (or (metadata-ref url-dict "src")
  299. (metadata-ref url-dict "archive")))
  300. (requirements (metadata-ref opam-content "depends"))
  301. (names (dependency-list->names requirements))
  302. (dependencies (filter-dependencies names))
  303. (native-dependencies (depends->native-inputs requirements))
  304. (inputs (dependency-list->inputs (depends->inputs requirements)))
  305. (native-inputs (dependency-list->inputs
  306. ;; Do not add dune nor jbuilder since they are
  307. ;; implicit inputs of the dune-build-system.
  308. (filter
  309. (lambda (name)
  310. (not (member name '("dune" "jbuilder"))))
  311. native-dependencies))))
  312. (let ((use-dune? (member "dune" names)))
  313. (call-with-temporary-output-file
  314. (lambda (temp port)
  315. (and (url-fetch source-url temp)
  316. (values
  317. `(package
  318. (name ,(ocaml-name->guix-name name))
  319. (version ,version)
  320. (source
  321. (origin
  322. (method url-fetch)
  323. (uri ,source-url)
  324. (sha256 (base32 ,(guix-hash-url temp)))))
  325. (build-system ,(if use-dune?
  326. 'dune-build-system
  327. 'ocaml-build-system))
  328. ,@(if (null? inputs)
  329. '()
  330. `((propagated-inputs (list ,@inputs))))
  331. ,@(if (null? native-inputs)
  332. '()
  333. `((native-inputs (list ,@native-inputs))))
  334. ,@(if (equal? name (guix-name->opam-name (ocaml-name->guix-name name)))
  335. '()
  336. `((properties
  337. ,(list 'quasiquote `((upstream-name . ,name))))))
  338. (home-page ,(metadata-ref opam-content "homepage"))
  339. (synopsis ,(metadata-ref opam-content "synopsis"))
  340. (description ,(metadata-ref opam-content "description"))
  341. (license ,(spdx-string->license
  342. (metadata-ref opam-content "license"))))
  343. (filter
  344. (lambda (name)
  345. (not (member name '("dune" "jbuilder"))))
  346. dependencies))))))))
  347. (define* (opam-recursive-import package-name #:key repo)
  348. (recursive-import package-name
  349. #:repo->guix-package opam->guix-package
  350. #:guix-name ocaml-name->guix-name
  351. #:repo repo))
  352. (define (guix-name->opam-name name)
  353. (if (string-prefix? "ocaml-" name)
  354. (substring name 6)
  355. name))
  356. (define (guix-package->opam-name package)
  357. "Given an OCaml PACKAGE built from OPAM, return the name of the
  358. package in OPAM."
  359. (let ((upstream-name (assoc-ref
  360. (package-properties package)
  361. 'upstream-name))
  362. (name (package-name package)))
  363. (if upstream-name
  364. upstream-name
  365. (guix-name->opam-name name))))
  366. (define (opam-package? package)
  367. "Return true if PACKAGE is an OCaml package from OPAM"
  368. (and
  369. (member (build-system-name (package-build-system package)) '(dune ocaml))
  370. (not (string-prefix? "ocaml4" (package-name package)))))
  371. (define (latest-release package)
  372. "Return an <upstream-source> for the latest release of PACKAGE."
  373. (and-let* ((opam-name (guix-package->opam-name package))
  374. (opam-file (opam-fetch opam-name))
  375. (version (assoc-ref opam-file "version"))
  376. (opam-content (assoc-ref opam-file "metadata"))
  377. (url-dict (metadata-ref opam-content "url"))
  378. (source-url (metadata-ref url-dict "src")))
  379. (upstream-source
  380. (package (package-name package))
  381. (version version)
  382. (urls (list source-url)))))
  383. (define %opam-updater
  384. (upstream-updater
  385. (name 'opam)
  386. (description "Updater for OPAM packages")
  387. (pred opam-package?)
  388. (latest latest-release)))