print.scm 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2017, 2020 Ricardo Wurmus <rekado@elephly.net>
  3. ;;; Copyright © 2021 Ludovic Courtès <ludo@gnu.org>
  4. ;;;
  5. ;;; This file is part of GNU Guix.
  6. ;;;
  7. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  8. ;;; under the terms of the GNU General Public License as published by
  9. ;;; the Free Software Foundation; either version 3 of the License, or (at
  10. ;;; your option) any later version.
  11. ;;;
  12. ;;; GNU Guix is distributed in the hope that it will be useful, but
  13. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. ;;; GNU General Public License for more details.
  16. ;;;
  17. ;;; You should have received a copy of the GNU General Public License
  18. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  19. (define-module (guix import print)
  20. #:use-module (guix base32)
  21. #:use-module (guix utils)
  22. #:use-module (guix licenses)
  23. #:use-module (guix packages)
  24. #:use-module (guix search-paths)
  25. #:use-module (guix build-system)
  26. #:use-module (gnu packages)
  27. #:use-module (srfi srfi-1)
  28. #:use-module (srfi srfi-26)
  29. #:use-module (guix import utils)
  30. #:use-module (ice-9 control)
  31. #:use-module (ice-9 match)
  32. #:export (package->code))
  33. (define (redundant-input-labels? inputs)
  34. "Return #t if input labels in the INPUTS list are redundant."
  35. (every (match-lambda
  36. ((label (? package? package) . _)
  37. (string=? label (package-name package)))
  38. (_ #f))
  39. inputs))
  40. (define (package->code package)
  41. "Return an S-expression representing the source code that produces PACKAGE
  42. when evaluated."
  43. ;; The module in which the package PKG is defined
  44. (define (package-module-name pkg)
  45. (map string->symbol
  46. (string-split (string-drop-right
  47. (location-file (package-location pkg)) 4)
  48. #\/)))
  49. ;; Return the first candidate variable name that is bound to VAL.
  50. (define (variable-name val mod)
  51. (match (let/ec return
  52. (module-for-each (lambda (sym var)
  53. (if (eq? val (variable-ref var))
  54. (return sym)
  55. #f))
  56. (resolve-interface mod)))
  57. ((? symbol? sym) sym)
  58. (_ #f)))
  59. ;; Print either license variable name or the code for a license object
  60. (define (license->code lic)
  61. (let ((var (variable-name lic '(guix licenses))))
  62. (if var
  63. (symbol-append 'license: var)
  64. `(license
  65. ,(license-name lic)
  66. ,(license-uri lic)
  67. ,(license-comment lic)))))
  68. (define (search-path-specification->code spec)
  69. `(search-path-specification
  70. (variable ,(search-path-specification-variable spec))
  71. (files (list ,@(search-path-specification-files spec)))
  72. (separator ,(search-path-specification-separator spec))
  73. (file-type (quote ,(search-path-specification-file-type spec)))
  74. (file-pattern ,(search-path-specification-file-pattern spec))))
  75. (define (factorized-uri-code uri version)
  76. (match (factorize-uri uri version)
  77. ((? string? uri) uri)
  78. ((factorized ...) `(string-append ,@factorized))))
  79. (define (source->code source version)
  80. (let ((uri (origin-uri source))
  81. (method (origin-method source))
  82. (hash (origin-hash source))
  83. (file-name (origin-file-name source))
  84. (patches (origin-patches source)))
  85. `(origin
  86. ;; Since 'procedure-name' returns the procedure name within the
  87. ;; module where it's defined, not its public name. Thus, try hard to
  88. ;; find its public name and use 'procedure-name' as a last resort.
  89. (method ,(or (any (lambda (module)
  90. (variable-name method module))
  91. '((guix download)
  92. (guix git-download)
  93. (guix hg-download)
  94. (guix svn-download)))
  95. (procedure-name method)))
  96. (uri ,(if version
  97. (match uri
  98. ((? string? uri)
  99. (factorized-uri-code uri version))
  100. ((lst ...)
  101. `(list
  102. ,@(map (cut factorized-uri-code <> version) uri))))
  103. uri))
  104. ,(if (equal? (content-hash-algorithm hash) 'sha256)
  105. `(sha256 (base32 ,(bytevector->nix-base32-string
  106. (content-hash-value hash))))
  107. `(hash (content-hash ,(bytevector->nix-base32-string
  108. (content-hash-value hash))
  109. ,(content-hash-algorithm hash))))
  110. ;; FIXME: in order to be able to throw away the directory prefix,
  111. ;; we just assume that the patch files can be found with
  112. ;; "search-patches".
  113. ,@(cond ((null? patches)
  114. '())
  115. ((every string? patches)
  116. `((patches (search-patches ,@(map basename patches)))))
  117. (else
  118. `((patches (list ,@(map (match-lambda
  119. ((? string? file)
  120. `(search-patch ,file))
  121. ((? origin? origin)
  122. (source->code origin #f)))
  123. patches)))))))))
  124. (define (variable-reference module name)
  125. ;; FIXME: using '@ certainly isn't pretty, but it avoids having to import
  126. ;; the individual package modules.
  127. (list '@ module name))
  128. (define (object->code obj quoted?)
  129. (match obj
  130. ((? package? package)
  131. (let* ((module (package-module-name package))
  132. (name (variable-name package module)))
  133. (if quoted?
  134. (list 'unquote (variable-reference module name))
  135. (variable-reference module name))))
  136. ((? origin? origin)
  137. (let ((code (source->code origin #f)))
  138. (if quoted?
  139. (list 'unquote code)
  140. code)))
  141. ((lst ...)
  142. (let ((lst (map (cut object->code <> #t) lst)))
  143. (if quoted?
  144. lst
  145. (list 'quasiquote lst))))
  146. (obj
  147. obj)))
  148. (define (inputs->code inputs)
  149. (if (redundant-input-labels? inputs)
  150. `(list ,@(map (match-lambda ;no need for input labels ("new style")
  151. ((_ package)
  152. (let* ((module (package-module-name package))
  153. (name (variable-name package module)))
  154. (variable-reference module name)))
  155. ((_ package output)
  156. (let* ((module (package-module-name package))
  157. (name (variable-name package module)))
  158. (list 'quasiquote
  159. (list
  160. (list 'unquote
  161. (variable-reference module name))
  162. output)))))
  163. inputs))
  164. (list 'quasiquote ;preserve input labels (deprecated)
  165. (object->code inputs #t))))
  166. (let ((name (package-name package))
  167. (version (package-version package))
  168. (source (package-source package))
  169. (build-system (package-build-system package))
  170. (arguments (package-arguments package))
  171. (inputs (package-inputs package))
  172. (propagated-inputs (package-propagated-inputs package))
  173. (native-inputs (package-native-inputs package))
  174. (outputs (package-outputs package))
  175. (native-search-paths (package-native-search-paths package))
  176. (search-paths (package-search-paths package))
  177. (replacement (package-replacement package))
  178. (synopsis (package-synopsis package))
  179. (description (package-description package))
  180. (license (package-license package))
  181. (home-page (package-home-page package))
  182. (supported-systems (package-supported-systems package))
  183. (properties (package-properties package)))
  184. `(define-public ,(string->symbol name)
  185. (package
  186. (name ,name)
  187. (version ,version)
  188. (source ,(source->code source version))
  189. ,@(match properties
  190. (() '())
  191. (_ `((properties ,properties))))
  192. ,@(if replacement
  193. `((replacement ,replacement))
  194. '())
  195. (build-system (@ (guix build-system ,(build-system-name build-system))
  196. ,(symbol-append (build-system-name build-system)
  197. '-build-system)))
  198. ,@(match arguments
  199. (() '())
  200. (_ `((arguments
  201. ,(list 'quasiquote (object->code arguments #t))))))
  202. ,@(match outputs
  203. (("out") '())
  204. (outs `((outputs (list ,@outs)))))
  205. ,@(match native-inputs
  206. (() '())
  207. (pkgs `((native-inputs ,(inputs->code pkgs)))))
  208. ,@(match inputs
  209. (() '())
  210. (pkgs `((inputs ,(inputs->code pkgs)))))
  211. ,@(match propagated-inputs
  212. (() '())
  213. (pkgs `((propagated-inputs ,(inputs->code pkgs)))))
  214. ,@(if (lset= string=? supported-systems %supported-systems)
  215. '()
  216. `((supported-systems (list ,@supported-systems))))
  217. ,@(match (map search-path-specification->code native-search-paths)
  218. (() '())
  219. (paths `((native-search-paths (list ,@paths)))))
  220. ,@(match (map search-path-specification->code search-paths)
  221. (() '())
  222. (paths `((search-paths (list ,@paths)))))
  223. (home-page ,home-page)
  224. (synopsis ,synopsis)
  225. (description ,description)
  226. (license ,(if (list? license)
  227. `(list ,@(map license->code license))
  228. (license->code license)))))))