python-build-system.scm 12 KB

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