print.scm 8.1 KB

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