gnu: emacs-org: Update to 9.4.
[jackhill/guix/guix.git] / guix / import / print.scm
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
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 (guix import utils)
29 #:use-module (ice-9 control)
30 #:use-module (ice-9 match)
31 #:export (package->code))
32
33 ;; FIXME: the quasiquoted arguments field may contain embedded package
34 ;; objects, e.g. in #:disallowed-references; they will just be printed with
35 ;; their usual #<package ...> representation, not as variable names.
36 (define (package->code package)
37 "Return an S-expression representing the source code that produces PACKAGE
38 when evaluated."
39 ;; The module in which the package PKG is defined
40 (define (package-module-name pkg)
41 (map string->symbol
42 (string-split (string-drop-right
43 (location-file (package-location pkg)) 4)
44 #\/)))
45
46 ;; Return the first candidate variable name that is bound to VAL.
47 (define (variable-name val mod)
48 (match (let/ec return
49 (module-for-each (lambda (sym var)
50 (if (eq? val (variable-ref var))
51 (return sym)
52 #f))
53 (resolve-interface mod)))
54 ((? symbol? sym) sym)
55 (_ #f)))
56
57 ;; Print either license variable name or the code for a license object
58 (define (license->code lic)
59 (let ((var (variable-name lic '(guix licenses))))
60 (or (symbol-append 'license: var)
61 `(license
62 (name ,(license-name lic))
63 (uri ,(license-uri lic))
64 (comment ,(license-comment lic))))))
65
66 (define (search-path-specification->code spec)
67 `(search-path-specification
68 (variable ,(search-path-specification-variable spec))
69 (files (list ,@(search-path-specification-files spec)))
70 (separator ,(search-path-specification-separator spec))
71 (file-type (quote ,(search-path-specification-file-type spec)))
72 (file-pattern ,(search-path-specification-file-pattern spec))))
73
74 (define (source->code source version)
75 (let ((uri (origin-uri source))
76 (method (origin-method source))
77 (sha256 (origin-sha256 source))
78 (file-name (origin-file-name source))
79 (patches (origin-patches source)))
80 `(origin
81 (method ,(procedure-name method))
82 (uri (string-append ,@(match (factorize-uri uri version)
83 ((? string? uri) (list uri))
84 (factorized factorized))))
85 (sha256
86 (base32
87 ,(format #f "~a" (bytevector->nix-base32-string sha256))))
88 ;; FIXME: in order to be able to throw away the directory prefix,
89 ;; we just assume that the patch files can be found with
90 ;; "search-patches".
91 ,@(if (null? patches) '()
92 `((patches (search-patches ,@(map basename patches))))))))
93
94 (define (package-lists->code lsts)
95 (list 'quasiquote
96 (map (match-lambda
97 ((? symbol? s)
98 (list (symbol->string s) (list 'unquote s)))
99 ((label pkg . out)
100 (let ((mod (package-module-name pkg)))
101 (cons* label
102 ;; FIXME: using '@ certainly isn't pretty, but it
103 ;; avoids having to import the individual package
104 ;; modules.
105 (list 'unquote
106 (list '@ mod (variable-name pkg mod)))
107 out))))
108 lsts)))
109
110 (let ((name (package-name package))
111 (version (package-version package))
112 (source (package-source package))
113 (build-system (package-build-system package))
114 (arguments (package-arguments package))
115 (inputs (package-inputs package))
116 (propagated-inputs (package-propagated-inputs package))
117 (native-inputs (package-native-inputs package))
118 (outputs (package-outputs package))
119 (native-search-paths (package-native-search-paths package))
120 (search-paths (package-search-paths package))
121 (replacement (package-replacement package))
122 (synopsis (package-synopsis package))
123 (description (package-description package))
124 (license (package-license package))
125 (home-page (package-home-page package))
126 (supported-systems (package-supported-systems package))
127 (properties (package-properties package)))
128 `(define-public ,(string->symbol name)
129 (package
130 (name ,name)
131 (version ,version)
132 (source ,(source->code source version))
133 ,@(match properties
134 (() '())
135 (_ `((properties ,properties))))
136 ,@(if replacement
137 `((replacement ,replacement))
138 '())
139 (build-system (@ (guix build-system ,(build-system-name build-system))
140 ,(symbol-append (build-system-name build-system)
141 '-build-system)))
142 ,@(match arguments
143 (() '())
144 (args `((arguments ,(list 'quasiquote args)))))
145 ,@(match outputs
146 (("out") '())
147 (outs `((outputs (list ,@outs)))))
148 ,@(match native-inputs
149 (() '())
150 (pkgs `((native-inputs ,(package-lists->code pkgs)))))
151 ,@(match inputs
152 (() '())
153 (pkgs `((inputs ,(package-lists->code pkgs)))))
154 ,@(match propagated-inputs
155 (() '())
156 (pkgs `((propagated-inputs ,(package-lists->code pkgs)))))
157 ,@(if (lset= string=? supported-systems %supported-systems)
158 '()
159 `((supported-systems (list ,@supported-systems))))
160 ,@(match (map search-path-specification->code native-search-paths)
161 (() '())
162 (paths `((native-search-paths (list ,@paths)))))
163 ,@(match (map search-path-specification->code search-paths)
164 (() '())
165 (paths `((search-paths (list ,@paths)))))
166 (home-page ,home-page)
167 (synopsis ,synopsis)
168 (description ,description)
169 (license ,(if (list? license)
170 `(list ,@(map license->code license))
171 (license->code license)))))))