r-build-system.scm 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2015, 2017, 2018 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/>.
  18. (define-module (guix build r-build-system)
  19. #:use-module ((guix build gnu-build-system) #:prefix gnu:)
  20. #:use-module (guix build utils)
  21. #:use-module (ice-9 match)
  22. #:use-module (ice-9 ftw)
  23. #:use-module (ice-9 popen)
  24. #:use-module (srfi srfi-1)
  25. #:use-module (srfi srfi-26)
  26. #:use-module (srfi srfi-35)
  27. #:export (%standard-phases
  28. r-build))
  29. ;; Commentary:
  30. ;;
  31. ;; Builder-side code of the standard build procedure for R packages.
  32. ;;
  33. ;; Code:
  34. (define (invoke-r command params)
  35. (apply invoke "R" "CMD" command params))
  36. (define (pipe-to-r command params)
  37. (let ((port (apply open-pipe* OPEN_WRITE "R" params)))
  38. (display command port)
  39. (let ((code (status:exit-val (close-pipe port))))
  40. (unless (zero? code)
  41. (raise (condition ((@@ (guix build utils) &invoke-error)
  42. (program "R")
  43. (arguments (cons command params))
  44. (exit-status (status:exit-val code))
  45. (term-signal (status:term-sig code))
  46. (stop-signal (status:stop-sig code)))))))))
  47. (define (generate-site-path inputs)
  48. (string-join (map (match-lambda
  49. ((_ . path)
  50. (string-append path "/site-library")))
  51. ;; Restrict to inputs beginning with "r-".
  52. (filter (match-lambda
  53. ((name . _)
  54. (string-prefix? "r-" name)))
  55. inputs))
  56. ":"))
  57. (define* (check #:key test-target inputs outputs tests? #:allow-other-keys)
  58. "Run the test suite of a given R package."
  59. (let* ((libdir (string-append (assoc-ref outputs "out") "/site-library/"))
  60. ;; R package names are case-sensitive and cannot be derived from the
  61. ;; Guix package name. The exact package name is required as an
  62. ;; argument to ‘tools::testInstalledPackage’, which runs the tests
  63. ;; for a package given its name and the path to the “library” (a
  64. ;; location for a collection of R packages) containing it.
  65. ;; Since there can only be one R package in any collection (=
  66. ;; “library”), the name of the only directory in the collection path
  67. ;; is the original name of the R package.
  68. (pkg-name (car (scandir libdir (negate (cut member <> '("." ".."))))))
  69. (testdir (string-append libdir pkg-name "/" test-target))
  70. (site-path (string-append libdir ":" (generate-site-path inputs))))
  71. (when (and tests? (file-exists? testdir))
  72. (setenv "R_LIBS_SITE" site-path)
  73. (pipe-to-r (string-append "tools::testInstalledPackage(\"" pkg-name "\", "
  74. "lib.loc = \"" libdir "\")")
  75. '("--no-save" "--slave")))
  76. #t))
  77. (define* (install #:key outputs inputs (configure-flags '())
  78. #:allow-other-keys)
  79. "Install a given R package."
  80. (let* ((out (assoc-ref outputs "out"))
  81. (site-library (string-append out "/site-library/"))
  82. (params (append configure-flags
  83. (list "--install-tests"
  84. (string-append "--library=" site-library)
  85. "--built-timestamp=1970-01-01"
  86. ".")))
  87. (site-path (string-append site-library ":"
  88. (generate-site-path inputs))))
  89. ;; If dependencies cannot be found at install time, R will refuse to
  90. ;; install the package.
  91. (setenv "R_LIBS_SITE" site-path)
  92. ;; Some R packages contain a configure script for which the CONFIG_SHELL
  93. ;; variable should be set.
  94. (setenv "CONFIG_SHELL" (which "bash"))
  95. (mkdir-p site-library)
  96. (invoke-r "INSTALL" params)))
  97. (define %standard-phases
  98. (modify-phases gnu:%standard-phases
  99. (delete 'bootstrap)
  100. (delete 'configure)
  101. (delete 'build)
  102. (delete 'check) ; tests must be run after installation
  103. (replace 'install install)
  104. (add-after 'install 'check check)))
  105. (define* (r-build #:key inputs (phases %standard-phases)
  106. #:allow-other-keys #:rest args)
  107. "Build the given R package, applying all of PHASES in order."
  108. (apply gnu:gnu-build #:inputs inputs #:phases phases args))
  109. ;;; r-build-system.scm ends here