python.scm 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2013, 2014, 2015, 2016, 2017 Ludovic Courtès <ludo@gnu.org>
  3. ;;; Copyright © 2013 Andreas Enge <andreas@enge.fr>
  4. ;;; Copyright © 2013 Nikita Karetnikov <nikita@karetnikov.org>
  5. ;;;
  6. ;;; This file is part of GNU Guix.
  7. ;;;
  8. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  9. ;;; under the terms of the GNU General Public License as published by
  10. ;;; the Free Software Foundation; either version 3 of the License, or (at
  11. ;;; your option) any later version.
  12. ;;;
  13. ;;; GNU Guix is distributed in the hope that it will be useful, but
  14. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  15. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. ;;; GNU General Public License for more details.
  17. ;;;
  18. ;;; You should have received a copy of the GNU General Public License
  19. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  20. (define-module (guix build-system python)
  21. #:use-module (guix store)
  22. #:use-module (guix utils)
  23. #:use-module (guix memoization)
  24. #:use-module (guix packages)
  25. #:use-module (guix derivations)
  26. #:use-module (guix search-paths)
  27. #:use-module (guix build-system)
  28. #:use-module (guix build-system gnu)
  29. #:use-module (ice-9 match)
  30. #:use-module (srfi srfi-1)
  31. #:use-module (srfi srfi-26)
  32. #:export (%python-build-system-modules
  33. package-with-python2
  34. strip-python2-variant
  35. default-python
  36. default-python2
  37. python-build
  38. python-build-system
  39. pypi-uri))
  40. ;; Commentary:
  41. ;;
  42. ;; Standard build procedure for Python packages using 'setup.py'. This is
  43. ;; implemented as an extension of 'gnu-build-system'.
  44. ;;
  45. ;; Code:
  46. (define* (pypi-uri name version #:optional (extension ".tar.gz"))
  47. "Return a URI string for the Python package hosted on the Python Package
  48. Index (PyPI) corresponding to NAME and VERSION. EXTENSION is the file name
  49. extension, such as '.tar.gz'."
  50. (string-append "https://files.pythonhosted.org/packages/source/"
  51. (string-take name 1) "/" name "/"
  52. name "-" version extension))
  53. (define %python-build-system-modules
  54. ;; Build-side modules imported by default.
  55. `((guix build python-build-system)
  56. ,@%gnu-build-system-modules))
  57. (define (default-python)
  58. "Return the default Python package."
  59. ;; Lazily resolve the binding to avoid a circular dependency.
  60. (let ((python (resolve-interface '(gnu packages python))))
  61. (module-ref python 'python-wrapper)))
  62. (define (default-python2)
  63. "Return the default Python 2 package."
  64. (let ((python (resolve-interface '(gnu packages python))))
  65. (module-ref python 'python-2)))
  66. (define* (package-with-explicit-python python old-prefix new-prefix
  67. #:key variant-property)
  68. "Return a procedure of one argument, P. The procedure creates a package with
  69. the same fields as P, which is assumed to use PYTHON-BUILD-SYSTEM, such that
  70. it is compiled with PYTHON instead. The inputs are changed recursively
  71. accordingly. If the name of P starts with OLD-PREFIX, this is replaced by
  72. NEW-PREFIX; otherwise, NEW-PREFIX is prepended to the name.
  73. When VARIANT-PROPERTY is present, it is used as a key to search for
  74. pre-defined variants of this transformation recorded in the 'properties' field
  75. of packages. The property value must be the promise of a package. This is a
  76. convenient way for package writers to force the transformation to use
  77. pre-defined variants."
  78. (define package-variant
  79. (if variant-property
  80. (lambda (package)
  81. (assq-ref (package-properties package)
  82. variant-property))
  83. (const #f)))
  84. (define (transform p)
  85. (cond
  86. ;; If VARIANT-PROPERTY is present, use that.
  87. ((package-variant p)
  88. => force)
  89. ;; Otherwise build the new package object graph.
  90. ((eq? (package-build-system p) python-build-system)
  91. (package
  92. (inherit p)
  93. (location (package-location p))
  94. (name (let ((name (package-name p)))
  95. (string-append new-prefix
  96. (if (string-prefix? old-prefix name)
  97. (substring name
  98. (string-length old-prefix))
  99. name))))
  100. (arguments
  101. (let ((python (if (promise? python)
  102. (force python)
  103. python)))
  104. (ensure-keyword-arguments (package-arguments p)
  105. `(#:python ,python))))))
  106. (else p)))
  107. (define (cut? p)
  108. (or (not (eq? (package-build-system p) python-build-system))
  109. (package-variant p)))
  110. (package-mapping transform cut?))
  111. (define package-with-python2
  112. ;; Note: delay call to 'default-python2' until after the 'arguments' field
  113. ;; of packages is accessed to avoid a circular dependency when evaluating
  114. ;; the top-level of (gnu packages python).
  115. (package-with-explicit-python (delay (default-python2))
  116. "python-" "python2-"
  117. #:variant-property 'python2-variant))
  118. (define (strip-python2-variant p)
  119. "Remove the 'python2-variant' property from P."
  120. (package
  121. (inherit p)
  122. (properties (alist-delete 'python2-variant (package-properties p)))))
  123. (define* (lower name
  124. #:key source inputs native-inputs outputs system target
  125. (python (default-python))
  126. #:allow-other-keys
  127. #:rest arguments)
  128. "Return a bag for NAME."
  129. (define private-keywords
  130. '(#:source #:target #:python #:inputs #:native-inputs))
  131. (and (not target) ;XXX: no cross-compilation
  132. (bag
  133. (name name)
  134. (system system)
  135. (host-inputs `(,@(if source
  136. `(("source" ,source))
  137. '())
  138. ,@inputs
  139. ;; Keep the standard inputs of 'gnu-build-system'.
  140. ,@(standard-packages)))
  141. (build-inputs `(("python" ,python)
  142. ,@native-inputs))
  143. (outputs outputs)
  144. (build python-build)
  145. (arguments (strip-keyword-arguments private-keywords arguments)))))
  146. (define* (python-build store name inputs
  147. #:key
  148. (tests? #t)
  149. (test-target "test")
  150. (use-setuptools? #t)
  151. (configure-flags ''())
  152. (phases '(@ (guix build python-build-system)
  153. %standard-phases))
  154. (outputs '("out"))
  155. (search-paths '())
  156. (system (%current-system))
  157. (guile #f)
  158. (imported-modules %python-build-system-modules)
  159. (modules '((guix build python-build-system)
  160. (guix build utils))))
  161. "Build SOURCE using PYTHON, and with INPUTS. This assumes that SOURCE
  162. provides a 'setup.py' file as its build system."
  163. (define builder
  164. `(begin
  165. (use-modules ,@modules)
  166. (python-build #:name ,name
  167. #:source ,(match (assoc-ref inputs "source")
  168. (((? derivation? source))
  169. (derivation->output-path source))
  170. ((source)
  171. source)
  172. (source
  173. source))
  174. #:configure-flags ,configure-flags
  175. #:system ,system
  176. #:test-target ,test-target
  177. #:tests? ,tests?
  178. #:use-setuptools? ,use-setuptools?
  179. #:phases ,phases
  180. #:outputs %outputs
  181. #:search-paths ',(map search-path-specification->sexp
  182. search-paths)
  183. #:inputs %build-inputs)))
  184. (define guile-for-build
  185. (match guile
  186. ((? package?)
  187. (package-derivation store guile system #:graft? #f))
  188. (#f ; the default
  189. (let* ((distro (resolve-interface '(gnu packages commencement)))
  190. (guile (module-ref distro 'guile-final)))
  191. (package-derivation store guile system #:graft? #f)))))
  192. (build-expression->derivation store name builder
  193. #:inputs inputs
  194. #:system system
  195. #:modules imported-modules
  196. #:outputs outputs
  197. #:guile-for-build guile-for-build))
  198. (define python-build-system
  199. (build-system
  200. (name 'python)
  201. (description "The standard Python build system")
  202. (lower lower)))
  203. ;;; python.scm ends here