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