opam.scm 19 KB

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