print.scm 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. (method ,(procedure-name method))
  77. (uri (string-append ,@(match (factorize-uri uri version)
  78. ((? string? uri) (list uri))
  79. (factorized factorized))))
  80. ,(if (equal? (content-hash-algorithm hash) 'sha256)
  81. `(sha256 (base32 ,(bytevector->nix-base32-string
  82. (content-hash-value hash))))
  83. `(hash (content-hash ,(bytevector->nix-base32-string
  84. (content-hash-value hash))
  85. ,(content-hash-algorithm hash))))
  86. ;; FIXME: in order to be able to throw away the directory prefix,
  87. ;; we just assume that the patch files can be found with
  88. ;; "search-patches".
  89. ,@(if (null? patches) '()
  90. `((patches (search-patches ,@(map basename patches))))))))
  91. (define (package-lists->code lsts)
  92. (list 'quasiquote
  93. (map (match-lambda
  94. ((? symbol? s)
  95. (list (symbol->string s) (list 'unquote s)))
  96. ((label pkg . out)
  97. (let ((mod (package-module-name pkg)))
  98. (cons* label
  99. ;; FIXME: using '@ certainly isn't pretty, but it
  100. ;; avoids having to import the individual package
  101. ;; modules.
  102. (list 'unquote
  103. (list '@ mod (variable-name pkg mod)))
  104. out))))
  105. lsts)))
  106. (let ((name (package-name package))
  107. (version (package-version package))
  108. (source (package-source package))
  109. (build-system (package-build-system package))
  110. (arguments (package-arguments package))
  111. (inputs (package-inputs package))
  112. (propagated-inputs (package-propagated-inputs package))
  113. (native-inputs (package-native-inputs package))
  114. (outputs (package-outputs package))
  115. (native-search-paths (package-native-search-paths package))
  116. (search-paths (package-search-paths package))
  117. (replacement (package-replacement package))
  118. (synopsis (package-synopsis package))
  119. (description (package-description package))
  120. (license (package-license package))
  121. (home-page (package-home-page package))
  122. (supported-systems (package-supported-systems package))
  123. (properties (package-properties package)))
  124. `(define-public ,(string->symbol name)
  125. (package
  126. (name ,name)
  127. (version ,version)
  128. (source ,(source->code source version))
  129. ,@(match properties
  130. (() '())
  131. (_ `((properties ,properties))))
  132. ,@(if replacement
  133. `((replacement ,replacement))
  134. '())
  135. (build-system (@ (guix build-system ,(build-system-name build-system))
  136. ,(symbol-append (build-system-name build-system)
  137. '-build-system)))
  138. ,@(match arguments
  139. (() '())
  140. (args `((arguments ,(list 'quasiquote args)))))
  141. ,@(match outputs
  142. (("out") '())
  143. (outs `((outputs (list ,@outs)))))
  144. ,@(match native-inputs
  145. (() '())
  146. (pkgs `((native-inputs ,(package-lists->code pkgs)))))
  147. ,@(match inputs
  148. (() '())
  149. (pkgs `((inputs ,(package-lists->code pkgs)))))
  150. ,@(match propagated-inputs
  151. (() '())
  152. (pkgs `((propagated-inputs ,(package-lists->code pkgs)))))
  153. ,@(if (lset= string=? supported-systems %supported-systems)
  154. '()
  155. `((supported-systems (list ,@supported-systems))))
  156. ,@(match (map search-path-specification->code native-search-paths)
  157. (() '())
  158. (paths `((native-search-paths (list ,@paths)))))
  159. ,@(match (map search-path-specification->code search-paths)
  160. (() '())
  161. (paths `((search-paths (list ,@paths)))))
  162. (home-page ,home-page)
  163. (synopsis ,synopsis)
  164. (description ,description)
  165. (license ,(if (list? license)
  166. `(list ,@(map license->code license))
  167. (license->code license)))))))