gnu: picard: Return #t from phases.
[guix.git] / guix / import / print.scm
blob0bec32c8dc0d9baeb15686bd9575d026e9daa99d
1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2017 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/>.
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
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                        #\/)))
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)))
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 var
61           `(license
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)))
80       `(origin
81          (method ,(procedure-name method))
82          (uri (string-append ,@(factorize-uri uri version)))
83          (sha256
84           (base32
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
88          ;; "search-patches".
89          ,@(if (null? patches) '()
90                `((patches (search-patches ,@(map basename patches))))))))
92   (define (package-lists->code lsts)
93     (list 'quasiquote
94           (map (match-lambda
95                  ((label pkg . out)
96                   (let ((mod (package-module-name pkg)))
97                     (list label
98                           ;; FIXME: using '@ certainly isn't pretty, but it
99                           ;; avoids having to import the individual package
100                           ;; modules.
101                           (list 'unquote
102                                 (list '@ mod (variable-name pkg mod)))))))
103                lsts)))
105   (let ((name                (package-name package))
106         (version             (package-version package))
107         (source              (package-source package))
108         (build-system        (package-build-system package))
109         (arguments           (package-arguments package))
110         (inputs              (package-inputs package))
111         (propagated-inputs   (package-propagated-inputs package))
112         (native-inputs       (package-native-inputs package))
113         (outputs             (package-outputs package))
114         (native-search-paths (package-native-search-paths package))
115         (search-paths        (package-search-paths package))
116         (replacement         (package-replacement package))
117         (synopsis            (package-synopsis package))
118         (description         (package-description package))
119         (license             (package-license package))
120         (home-page           (package-home-page package))
121         (supported-systems   (package-supported-systems package))
122         (properties          (package-properties package)))
123     `(package
124        (name ,name)
125        (version ,version)
126        (source ,(source->code source version))
127        ,@(match properties
128            (() '())
129            (_  `((properties ,properties))))
130        ,@(if replacement
131              `((replacement ,replacement))
132              '())
133        (build-system ,(symbol-append (build-system-name build-system)
134                                      '-build-system))
135        ,@(match arguments
136            (() '())
137            (args `((arguments ,(list 'quasiquote args)))))
138        ,@(match outputs
139            (("out") '())
140            (outs `((outputs (list ,@outs)))))
141        ,@(match native-inputs
142            (() '())
143            (pkgs `((native-inputs ,(package-lists->code pkgs)))))
144        ,@(match inputs
145            (() '())
146            (pkgs `((inputs ,(package-lists->code pkgs)))))
147        ,@(match propagated-inputs
148            (() '())
149            (pkgs `((propagated-inputs ,(package-lists->code pkgs)))))
150        ,@(if (lset= string=? supported-systems %supported-systems)
151              '()
152              `((supported-systems (list ,@supported-systems))))
153        ,@(match (map search-path-specification->code native-search-paths)
154            (() '())
155            (paths `((native-search-paths (list ,@paths)))))
156        ,@(match (map search-path-specification->code search-paths)
157            (() '())
158            (paths `((search-paths (list ,@paths)))))
159        (home-page ,home-page)
160        (synopsis ,synopsis)
161        (description ,description)
162        (license ,(if (list? license)
163                      `(list ,@(map license->code license))
164                      (license->code license))))))