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 licenses)
  22. #:use-module (guix packages)
  23. #:use-module ((guix diagnostics) #:select (location-file))
  24. #:use-module (guix search-paths)
  25. #:use-module (guix build-system)
  26. #:use-module (srfi srfi-1)
  27. #:use-module (srfi srfi-26)
  28. #:use-module (guix import utils)
  29. #:use-module (ice-9 control)
  30. #:use-module (ice-9 match)
  31. #:export (package->code))
  32. (define (redundant-input-labels? inputs)
  33. "Return #t if input labels in the INPUTS list are redundant."
  34. (every (match-lambda
  35. ((label (? package? package) . _)
  36. (string=? label (package-name package)))
  37. (_ #f))
  38. inputs))
  39. (define (package->code package)
  40. "Return an S-expression representing the source code that produces PACKAGE
  41. when evaluated."
  42. ;; The module in which the package PKG is defined
  43. (define (package-module-name pkg)
  44. (map string->symbol
  45. (string-split (string-drop-right
  46. (location-file (package-location pkg)) 4)
  47. #\/)))
  48. ;; Return the first candidate variable name that is bound to VAL.
  49. (define (variable-name val mod)
  50. (match (let/ec return
  51. (module-for-each (lambda (sym var)
  52. (if (eq? val (variable-ref var))
  53. (return sym)
  54. #f))
  55. (resolve-interface mod)))
  56. ((? symbol? sym) sym)
  57. (_ #f)))
  58. ;; Print either license variable name or the code for a license object
  59. (define (license->code lic)
  60. (let ((var (variable-name lic '(guix licenses))))
  61. (if var
  62. (symbol-append 'license: var)
  63. `(license
  64. ,(license-name lic)
  65. ,(license-uri lic)
  66. ,(license-comment lic)))))
  67. (define (search-path-specification->code spec)
  68. `(search-path-specification
  69. (variable ,(search-path-specification-variable spec))
  70. (files (list ,@(search-path-specification-files spec)))
  71. (separator ,(search-path-specification-separator spec))
  72. (file-type (quote ,(search-path-specification-file-type spec)))
  73. (file-pattern ,(search-path-specification-file-pattern spec))))
  74. (define (factorized-uri-code uri version)
  75. (match (factorize-uri uri version)
  76. ((? string? uri) uri)
  77. ((factorized ...) `(string-append ,@factorized))))
  78. (define (source->code source version)
  79. (let ((uri (origin-uri source))
  80. (method (origin-method source))
  81. (hash (origin-hash source))
  82. (file-name (origin-file-name source))
  83. (patches (origin-patches source)))
  84. `(origin
  85. ;; Since 'procedure-name' returns the procedure name within the
  86. ;; module where it's defined, not its public name. Thus, try hard to
  87. ;; find its public name and use 'procedure-name' as a last resort.
  88. (method ,(or (any (lambda (module)
  89. (variable-name method module))
  90. '((guix download)
  91. (guix git-download)
  92. (guix hg-download)
  93. (guix svn-download)))
  94. (procedure-name method)))
  95. (uri ,(if version
  96. (match uri
  97. ((? string? uri)
  98. (factorized-uri-code uri version))
  99. ((lst ...)
  100. `(list
  101. ,@(map (cut factorized-uri-code <> version) uri))))
  102. uri))
  103. ,(if (equal? (content-hash-algorithm hash) 'sha256)
  104. `(sha256 (base32 ,(bytevector->nix-base32-string
  105. (content-hash-value hash))))
  106. `(hash (content-hash ,(bytevector->nix-base32-string
  107. (content-hash-value hash))
  108. ,(content-hash-algorithm hash))))
  109. ;; FIXME: in order to be able to throw away the directory prefix,
  110. ;; we just assume that the patch files can be found with
  111. ;; "search-patches".
  112. ,@(cond ((null? patches)
  113. '())
  114. ((every string? patches)
  115. `((patches (search-patches ,@(map basename patches)))))
  116. (else
  117. `((patches (list ,@(map (match-lambda
  118. ((? string? file)
  119. `(search-patch ,file))
  120. ((? origin? origin)
  121. (source->code origin #f)))
  122. patches)))))))))
  123. (define (variable-reference module name)
  124. ;; FIXME: using '@ certainly isn't pretty, but it avoids having to import
  125. ;; the individual package modules.
  126. (list '@ module name))
  127. (define (object->code obj quoted?)
  128. (match obj
  129. ((? package? package)
  130. (let* ((module (package-module-name package))
  131. (name (variable-name package module)))
  132. (if quoted?
  133. (list 'unquote (variable-reference module name))
  134. (variable-reference module name))))
  135. ((? origin? origin)
  136. (let ((code (source->code origin #f)))
  137. (if quoted?
  138. (list 'unquote code)
  139. code)))
  140. ((lst ...)
  141. (let ((lst (map (cut object->code <> #t) lst)))
  142. (if quoted?
  143. lst
  144. (list 'quasiquote lst))))
  145. (obj
  146. obj)))
  147. (define (inputs->code inputs)
  148. (if (redundant-input-labels? inputs)
  149. `(list ,@(map (match-lambda ;no need for input labels ("new style")
  150. ((_ package)
  151. (let* ((module (package-module-name package))
  152. (name (variable-name package module)))
  153. (variable-reference module name)))
  154. ((_ package output)
  155. (let* ((module (package-module-name package))
  156. (name (variable-name package module)))
  157. (list 'quasiquote
  158. (list
  159. (list 'unquote
  160. (variable-reference module name))
  161. output)))))
  162. inputs))
  163. (list 'quasiquote ;preserve input labels (deprecated)
  164. (object->code inputs #t))))
  165. (let ((name (package-name package))
  166. (version (package-version package))
  167. (source (package-source package))
  168. (build-system (package-build-system package))
  169. (arguments (package-arguments package))
  170. (inputs (package-inputs package))
  171. (propagated-inputs (package-propagated-inputs package))
  172. (native-inputs (package-native-inputs package))
  173. (outputs (package-outputs package))
  174. (native-search-paths (package-native-search-paths package))
  175. (search-paths (package-search-paths package))
  176. (replacement (package-replacement package))
  177. (synopsis (package-synopsis package))
  178. (description (package-description package))
  179. (license (package-license package))
  180. (home-page (package-home-page package))
  181. (supported-systems (package-supported-systems package))
  182. (properties (package-properties package)))
  183. `(define-public ,(string->symbol name)
  184. (package
  185. (name ,name)
  186. (version ,version)
  187. (source ,(source->code source version))
  188. ,@(match properties
  189. (() '())
  190. (_ `((properties
  191. ,(list 'quasiquote (object->code properties #t))))))
  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)))))))