print.scm 7.3 KB

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