1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2017 Ricardo Wurmus <rekado@elephly.net>
4 ;;; This file is part of GNU Guix.
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.
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.
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/>.
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))
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
39 ;; The module in which the package PKG is defined
40 (define (package-module-name pkg)
42 (string-split (string-drop-right
43 (location-file (package-location pkg)) 4)
46 ;; Return the first candidate variable name that is bound to VAL.
47 (define (variable-name val mod)
49 (module-for-each (lambda (sym var)
50 (if (eq? val (variable-ref var))
53 (resolve-interface mod)))
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))))
62 (name ,(license-name lic))
63 (uri ,(license-uri lic))
64 (comment ,(license-comment lic))))))
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))))
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)))
81 (method ,(procedure-name method))
82 (uri (string-append ,@(factorize-uri uri version)))
85 ,(format #f "~a" (bytevector->nix-base32-string sha256))))
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
89 ,@(if (null? patches) '()
90 `((patches (search-patches ,@(map basename patches))))))))
92 (define (package-lists->code lsts)
96 (let ((mod (package-module-name pkg)))
98 ;; FIXME: using '@ certainly isn't pretty, but it
99 ;; avoids having to import the individual package
102 (list '@ mod (variable-name pkg mod)))
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)))
127 (source ,(source->code source version))
130 (_ `((properties ,properties))))
132 `((replacement ,replacement))
134 (build-system ,(symbol-append (build-system-name build-system)
138 (args `((arguments ,(list 'quasiquote args)))))
141 (outs `((outputs (list ,@outs)))))
142 ,@(match native-inputs
144 (pkgs `((native-inputs ,(package-lists->code pkgs)))))
147 (pkgs `((inputs ,(package-lists->code pkgs)))))
148 ,@(match propagated-inputs
150 (pkgs `((propagated-inputs ,(package-lists->code pkgs)))))
151 ,@(if (lset= string=? supported-systems %supported-systems)
153 `((supported-systems (list ,@supported-systems))))
154 ,@(match (map search-path-specification->code native-search-paths)
156 (paths `((native-search-paths (list ,@paths)))))
157 ,@(match (map search-path-specification->code search-paths)
159 (paths `((search-paths (list ,@paths)))))
160 (home-page ,home-page)
162 (description ,description)
163 (license ,(if (list? license)
164 `(list ,@(map license->code license))
165 (license->code license))))))