gnu: r-igraph: Move to (gnu packages cran).
[jackhill/guix/guix.git] / guix / build / python-build-system.scm
CommitLineData
40506d5d 1;;; GNU Guix --- Functional package management for GNU
9c2563a8 2;;; Copyright © 2013, 2015, 2016, 2018, 2019 Ludovic Courtès <ludo@gnu.org>
7b96bf82 3;;; Copyright © 2013 Andreas Enge <andreas@enge.fr>
40506d5d 4;;; Copyright © 2013 Nikita Karetnikov <nikita@karetnikov.org>
e35b09ca 5;;; Copyright © 2015, 2018 Mark H Weaver <mhw@netris.org>
7db40bce 6;;; Copyright © 2016 Hartmut Goebel <h.goebel@crazy-compilers.com>
d57888a8 7;;; Copyright © 2018 Ricardo Wurmus <rekado@elephly.net>
89e7f90d 8;;; Copyright © 2018 Arun Isaac <arunisaac@systemreboot.net>
40506d5d
NK
9;;;
10;;; This file is part of GNU Guix.
11;;;
12;;; GNU Guix is free software; you can redistribute it and/or modify it
13;;; under the terms of the GNU General Public License as published by
14;;; the Free Software Foundation; either version 3 of the License, or (at
15;;; your option) any later version.
16;;;
17;;; GNU Guix is distributed in the hope that it will be useful, but
18;;; WITHOUT ANY WARRANTY; without even the implied warranty of
19;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20;;; GNU General Public License for more details.
21;;;
22;;; You should have received a copy of the GNU General Public License
23;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
24
25(define-module (guix build python-build-system)
b5b73a82 26 #:use-module ((guix build gnu-build-system) #:prefix gnu:)
40506d5d
NK
27 #:use-module (guix build utils)
28 #:use-module (ice-9 match)
29 #:use-module (ice-9 ftw)
30 #:use-module (srfi srfi-1)
31 #:use-module (srfi srfi-26)
32 #:export (%standard-phases
a2ff4f02
HG
33 add-installed-pythonpath
34 site-packages
9c2563a8 35 python-version
40506d5d
NK
36 python-build))
37
38;; Commentary:
39;;
40;; Builder-side code of the standard Python package build procedure.
41;;
c1019287
HG
42;;
43;; Backgound about the Python installation methods
44;;
45;; In Python there are different ways to install packages: distutils,
46;; setuptools, easy_install and pip. All of these are sharing the file
47;; setup.py, introduced with distutils in Python 2.0. The setup.py file can be
48;; considered as a kind of Makefile accepting targets (or commands) like
49;; "build" and "install". As of autumn 2016 the recommended way to install
50;; Python packages is using pip.
51;;
52;; For both distutils and setuptools, running "python setup.py install" is the
53;; way to install Python packages. With distutils the "install" command
54;; basically copies all packages into <prefix>/lib/pythonX.Y/site-packages.
55;;
56;; Some time later "setuptools" was established to enhance distutils. To use
57;; setuptools, the developer imports setuptools in setup.py. When importing
58;; setuptools, the original "install" command gets overwritten by setuptools'
59;; "install" command.
60;;
61;; The command-line tools easy_install and pip are both capable of finding and
62;; downloading the package source from PyPI (the Python Package Index). Both
63;; of them import setuptools and execute the "setup.py" file under their
64;; control. Thus the "setup.py" behaves as if the developer had imported
65;; setuptools within setup.py - even is still using only distutils.
66;;
67;; Setuptools' "install" command (to be more precise: the "easy_install"
68;; command which is called by "install") will put the path of the currently
69;; installed version of each package and it's dependencies (as declared in
70;; setup.py) into an "easy-install.pth" file. In Guix each packages gets its
71;; own "site-packages" directory and thus an "easy-install.pth" of its own.
72;; To avoid conflicts, the python build system renames the file to
73;; <packagename>.pth in the phase rename-pth-file. To ensure that Python will
74;; process the .pth file, easy_install also creates a basic "site.py" in each
75;; "site-packages" directory. The file is the same for all packages, thus
76;; there is no need to rename it. For more information about .pth files and
77;; the site module, please refere to
78;; https://docs.python.org/3/library/site.html.
79;;
80;; The .pth files contain the file-system paths (pointing to the store) of all
81;; dependencies. So the dependency is hidden in the .pth file but is not
82;; visible in the file-system. Now if packages A and B both required packages
83;; P, but in different versions, Guix will not detect this when installing
84;; both A and B to a profile. (For details and example see
85;; https://lists.gnu.org/archive/html/guix-devel/2016-10/msg01233.html.)
86;;
87;; Pip behaves a bit different then easy_install: it always executes
88;; "setup.py" with the option "--single-version-externally-managed" set. This
89;; makes setuptools' "install" command run the original "install" command
90;; instead of the "easy_install" command, so no .pth file (and no site.py)
91;; will be created. The "site-packages" directory only contains the package
92;; and the related .egg-info directory.
93;;
94;; This is exactly what we need for Guix and this is what we mimic in the
95;; install phase below.
96;;
97;; As a draw back, the magic of the .pth file of linking to the other required
98;; packages is gone and these packages have now to be declared as
99;; "propagated-inputs".
100;;
101;; Note: Importing setuptools also adds two sub-commands: "install_egg_info"
102;; and "install_scripts". These sub-commands are executed even if
103;; "--single-version-externally-managed" is set, thus the .egg-info directory
104;; and the scripts defined in entry-points will always be created.
105
40506d5d 106
46bcdcc2
MB
107(define setuptools-shim
108 ;; Run setup.py with "setuptools" being imported, which will patch
109 ;; "distutils". This is needed for packages using "distutils" instead of
110 ;; "setuptools" since the former does not understand the
111 ;; "--single-version-externally-managed" flag.
112 ;; Python code taken from pip 9.0.1 pip/utils/setuptools_build.py
113 (string-append
114 "import setuptools, tokenize;__file__='setup.py';"
115 "f=getattr(tokenize, 'open', open)(__file__);"
116 "code=f.read().replace('\\r\\n', '\\n');"
117 "f.close();"
118 "exec(compile(code, __file__, 'exec'))"))
40506d5d 119
5f7565d1 120(define (call-setuppy command params use-setuptools?)
b191f88e
AE
121 (if (file-exists? "setup.py")
122 (begin
123 (format #t "running \"python setup.py\" with command ~s and parameters ~s~%"
124 command params)
5f7565d1 125 (if use-setuptools?
e35b09ca
MW
126 (apply invoke "python" "-c" setuptools-shim
127 command params)
128 (apply invoke "python" "./setup.py" command params)))
b191f88e
AE
129 (error "no setup.py found")))
130
5f7565d1 131(define* (build #:key use-setuptools? #:allow-other-keys)
b191f88e 132 "Build a given Python package."
1e626454
MW
133 (call-setuppy "build" '() use-setuptools?)
134 #t)
b191f88e 135
5f7565d1 136(define* (check #:key tests? test-target use-setuptools? #:allow-other-keys)
842ded33 137 "Run the test suite of a given Python package."
7b96bf82 138 (if tests?
b002f964
HG
139 ;; Running `setup.py test` creates an additional .egg-info directory in
140 ;; build/lib in some cases, e.g. if the source is in a sub-directory
141 ;; (given with `package_dir`). This will by copied to the output, too,
142 ;; so we need to remove.
143 (let ((before (find-files "build" "\\.egg-info$" #:directories? #t)))
1e626454
MW
144 (call-setuppy test-target '() use-setuptools?)
145 (let* ((after (find-files "build" "\\.egg-info$" #:directories? #t))
171a117c 146 (inter (lset-difference string=? after before)))
1e626454
MW
147 (for-each delete-file-recursively inter)))
148 (format #t "test suite not run~%"))
149 #t)
842ded33 150
9c2563a8 151(define (python-version python)
9f6509c6
MW
152 (let* ((version (last (string-split python #\-)))
153 (components (string-split version #\.))
154 (major+minor (take components 2)))
155 (string-join major+minor ".")))
824af8ca 156
a2ff4f02
HG
157(define (site-packages inputs outputs)
158 "Return the path of the current output's Python site-package."
159 (let* ((out (assoc-ref outputs "out"))
160 (python (assoc-ref inputs "python")))
161 (string-append out "/lib/python"
9c2563a8 162 (python-version python)
a2ff4f02
HG
163 "/site-packages/")))
164
165(define (add-installed-pythonpath inputs outputs)
166 "Prepend the Python site-package of OUTPUT to PYTHONPATH. This is useful
167when running checks after installing the package."
168 (let ((old-path (getenv "PYTHONPATH"))
169 (add-path (site-packages inputs outputs)))
170 (setenv "PYTHONPATH"
171 (string-append add-path
172 (if old-path (string-append ":" old-path) "")))
173 #t))
174
5f7565d1 175(define* (install #:key outputs (configure-flags '()) use-setuptools?
b191f88e
AE
176 #:allow-other-keys)
177 "Install a given Python package."
178 (let* ((out (assoc-ref outputs "out"))
5f7565d1
HG
179 (params (append (list (string-append "--prefix=" out))
180 (if use-setuptools?
181 ;; distutils does not accept these flags
182 (list "--single-version-externally-managed"
183 "--root=/")
184 '())
7db40bce 185 configure-flags)))
1e626454
MW
186 (call-setuppy "install" params use-setuptools?)
187 #t))
b191f88e 188
3df47231 189(define* (wrap #:key inputs outputs #:allow-other-keys)
40506d5d 190 (define (list-of-files dir)
89e7f90d
AI
191 (find-files dir (lambda (file stat)
192 (and (eq? 'regular (stat:type stat))
193 (not (wrapper? file))))))
40506d5d
NK
194
195 (define bindirs
196 (append-map (match-lambda
197 ((_ . dir)
198 (list (string-append dir "/bin")
199 (string-append dir "/sbin"))))
200 outputs))
201
202 (let* ((out (assoc-ref outputs "out"))
3df47231 203 (python (assoc-ref inputs "python"))
40506d5d
NK
204 (var `("PYTHONPATH" prefix
205 ,(cons (string-append out "/lib/python"
9c2563a8 206 (python-version python)
824af8ca 207 "/site-packages")
40506d5d
NK
208 (search-path-as-string->list
209 (or (getenv "PYTHONPATH") ""))))))
210 (for-each (lambda (dir)
211 (let ((files (list-of-files dir)))
212 (for-each (cut wrap-program <> var)
213 files)))
1e626454
MW
214 bindirs)
215 #t))
40506d5d 216
6f8fe4b2
FB
217(define* (rename-pth-file #:key name inputs outputs #:allow-other-keys)
218 "Rename easy-install.pth to NAME.pth to avoid conflicts between packages
219installed with setuptools."
c1019287
HG
220 ;; Even if the "easy-install.pth" is not longer created, we kept this phase.
221 ;; There still may be packages creating an "easy-install.pth" manually for
222 ;; some good reason.
6f8fe4b2
FB
223 (let* ((out (assoc-ref outputs "out"))
224 (python (assoc-ref inputs "python"))
225 (site-packages (string-append out "/lib/python"
9c2563a8 226 (python-version python)
6f8fe4b2
FB
227 "/site-packages"))
228 (easy-install-pth (string-append site-packages "/easy-install.pth"))
229 (new-pth (string-append site-packages "/" name ".pth")))
230 (when (file-exists? easy-install-pth)
231 (rename-file easy-install-pth new-pth))
232 #t))
233
3bacb7a6
MW
234(define* (ensure-no-mtimes-pre-1980 #:rest _)
235 "Ensure that there are no mtimes before 1980-01-02 in the source tree."
236 ;; Rationale: patch-and-repack creates tarballs with timestamps at the POSIX
237 ;; epoch, 1970-01-01 UTC. This causes problems with Python packages,
238 ;; because Python eggs are ZIP files, and the ZIP format does not support
239 ;; timestamps before 1980.
240 (let ((early-1980 315619200)) ; 1980-01-02 UTC
241 (ftw "." (lambda (file stat flag)
242 (unless (<= early-1980 (stat:mtime stat))
243 (utime file early-1980 early-1980))
244 #t))
245 #t))
246
d57888a8
RW
247(define* (enable-bytecode-determinism #:rest _)
248 "Improve determinism of pyc files."
d57888a8
RW
249 ;; Use deterministic hashes for strings, bytes, and datetime objects.
250 (setenv "PYTHONHASHSEED" "0")
251 #t)
252
40506d5d 253(define %standard-phases
6bbb37a5
MC
254 ;; The build phase only builds C extensions and copies the Python sources,
255 ;; while the install phase byte-compiles and copies them to the prefix
256 ;; directory. The tests are run after the install phase because otherwise
257 ;; the cached .pyc generated during the tests execution seem to interfere
258 ;; with the byte compilation of the install phase.
f84218ac 259 (modify-phases gnu:%standard-phases
3bacb7a6 260 (add-after 'unpack 'ensure-no-mtimes-pre-1980 ensure-no-mtimes-pre-1980)
d57888a8
RW
261 (add-after 'ensure-no-mtimes-pre-1980 'enable-bytecode-determinism
262 enable-bytecode-determinism)
189be331 263 (delete 'bootstrap)
6bbb37a5 264 (delete 'configure) ;not needed
f8503e2b 265 (replace 'build build)
6bbb37a5
MC
266 (delete 'check) ;moved after the install phase
267 (replace 'install install)
268 (add-after 'install 'check check)
f8503e2b
LC
269 (add-after 'install 'wrap wrap)
270 (add-before 'strip 'rename-pth-file rename-pth-file)))
40506d5d
NK
271
272(define* (python-build #:key inputs (phases %standard-phases)
273 #:allow-other-keys #:rest args)
274 "Build the given Python package, applying all of PHASES in order."
275 (apply gnu:gnu-build #:inputs inputs #:phases phases args))
276
277;;; python-build-system.scm ends here