Debian Codegraph @

R/r.R

raw
1
2 R <- function() {
3 bin <- normalizePath(R.home("bin"), winslash = "/")
4 exe <- if (renv_platform_windows()) "R.exe" else "R"
5 file.path(bin, exe)
6 }
7
8 r <- function(args, ...) {
9
10 # ensure R_LIBS is set; unset R_LIBS_USER and R_LIBS_SITE
11 # so that R_LIBS will always take precedence
12 rlibs <- paste(renv_libpaths_all(), collapse = .Platform$path.sep)
13 renv_scope_envvars(R_LIBS = rlibs, R_LIBS_USER = "NULL", R_LIBS_SITE = "NULL")
14
15 # ensure Rtools is on the PATH for Windows
16 renv_scope_rtools()
17
18 # use the same tar for installation as currently configured
19 tar <- Sys.getenv("R_INSTALL_TAR", unset = renv_tar_exe(default = "internal"))
20 renv_scope_envvars(R_INSTALL_TAR = tar)
21
22 # invoke r
23 suppressWarnings(system2(R(), args, ...))
24
25 }
26
27 r_exec_error <- function(package, output, label, extra) {
28
29 # installation failed; write output for user
30 fmt <- "Error %sing package '%s':"
31 header <- sprintf(fmt, label, package)
32
33 lines <- paste(rep("=", nchar(header)), collapse = "")
34
35 # try to add diagnostic information if possible
36 diagnostics <- r_exec_error_diagnostics(package, output)
37 if (!empty(diagnostics)) {
38 size <- min(getOption("width"), 78L)
39 dividers <- paste(rep.int("-", size), collapse = "")
40 output <- c(output, paste(dividers, diagnostics, collapse = "\n\n"))
41 }
42
43 # normalize 'extra'
44 extra <- if (is.integer(extra))
45 paste("error code", extra)
46 else
47 paste(renv_path_pretty(extra), "does not exist")
48
49 # stop with an error
50 footer <- sprintf("%s of package '%s' failed [%s]", label, package, extra)
51 all <- c(header, lines, "", output, footer)
52 abort(all)
53
54 }
55
56 r_exec_error_diagnostics_fortran_library <- function() {
57
58 checker <- function(output) {
59 pattern <- "library not found for -l(quadmath|gfortran|fortran)"
60 idx <- grep(pattern, output, ignore.case = TRUE)
61 if (length(idx))
62 return(unique(output[idx]))
63 }
64
65 suggestion <- "
66 R was unable to find one or more FORTRAN libraries during compilation.
67 This often implies that the FORTRAN compiler has not been properly configured.
68 Please see https://stackoverflow.com/q/35999874 for more information.
69 "
70
71 list(
72 checker = checker,
73 suggestion = suggestion
74 )
75
76 }
77
78 r_exec_error_diagnostics_fortran_binary <- function() {
79
80 checker <- function(output) {
81 pattern <- "gfortran: no such file or directory"
82 idx <- grep(pattern, output, ignore.case = TRUE)
83 if (length(idx))
84 return(unique(output[idx]))
85 }
86
87 suggestion <- "
88 R was unable to find the gfortran binary.
89 gfortran is required for the compilation of Fortran source files.
90 Please check that gfortran is installed and available on the PATH.
91 Please see https://stackoverflow.com/q/35999874 for more information.
92 "
93
94 list(
95 checker = checker,
96 suggestion = suggestion
97 )
98
99 }
100
101 r_exec_error_diagnostics_openmp <- function() {
102
103 checker <- function(output) {
104 pattern <- "unsupported option '-fopenmp'"
105 idx <- grep(pattern, output, fixed = TRUE)
106 if (length(idx))
107 return(unique(output[idx]))
108 }
109
110 suggestion <- "
111 R is currently configured to use a compiler that does not have OpenMP support.
112 You may need to disable OpenMP, or update your compiler toolchain.
113 Please see https://support.bioconductor.org/p/119536/ for a related discussion.
114 "
115
116 list(
117 checker = checker,
118 suggestion = suggestion
119 )
120
121 }
122
123 r_exec_error_diagnostics_libintl <- function() {
124
125 checker <- function(output) {
126 if (!renv_platform_macos())
127 return()
128 pattern <- "fatal error: 'libintl.h' file not found"
129 idx <- grep(pattern, output, fixed = TRUE)
130 if (length(idx))
131 return(unique(output[idx]))
132 }
133
134 suggestion <- "
135 The compilation error above occurred because 'libintl.h' was not found.
136 This header is provided by the 'gettext' library, which is not bundled with macOS.
137
138 You may be able to install it by running:
139
140 source(\"https://mac.R-project.org/bin/install.R\")
141 install.libs(\"gettext\")
142
143 See https://mac.r-project.org/bin/ for more details.
144 "
145
146 list(
147 checker = checker,
148 suggestion = suggestion
149 )
150
151 }
152
153 r_exec_error_diagnostics <- function(package, output) {
154
155 diagnostics <- list(
156 r_exec_error_diagnostics_fortran_library(),
157 r_exec_error_diagnostics_fortran_binary(),
158 r_exec_error_diagnostics_openmp(),
159 r_exec_error_diagnostics_libintl()
160 )
161
162 suggestions <- uapply(diagnostics, function(diagnostic) {
163
164 check <- catch(diagnostic$checker(output))
165 if (!is.character(check))
166 return()
167
168 reasons <- paste("-", dQuote(check), collapse = "\n")
169 paste(diagnostic$suggestion, "Reason(s):", reasons, sep = "\n")
170
171 })
172
173 as.character(suggestions)
174
175 }
176
177 # install package called 'package' located at path 'path'
178 r_cmd_install <- function(package, path, ...) {
179
180 # normalize path to package
181 path <- renv_path_normalize(path, mustWork = TRUE)
182
183 # unpack .zip source archives before install
184 # https://github.com/rstudio/renv/issues/1359
185 ftype <- renv_file_type(path)
186 atype <- renv_archive_type(path)
187 ptype <- renv_package_type(path)
188
189 unpack <-
190 ftype == "file" &&
191 atype == "zip" &&
192 ptype == "source"
193
194 if (unpack) {
195 newpath <- renv_package_unpack(package, path, force = TRUE)
196 if (!identical(newpath, path)) {
197 path <- newpath
198 defer(unlink(path, recursive = TRUE))
199 }
200 }
201
202 # rename binary .zip files if necessary
203 rename <-
204 ftype == "file" &&
205 atype == "zip" &&
206 ptype == "binary"
207
208 if (rename) {
209 regexps <- .standard_regexps()
210 fmt <- "^%s(?:_%s)?\\.zip$"
211 pattern <- sprintf(fmt, regexps$valid_package_name, regexps$valid_package_version)
212 if (!grepl(pattern, basename(path), perl = TRUE)) {
213 dir <- renv_scope_tempfile(package)
214 ensure_directory(dir)
215 newpath <- file.path(dir, paste(package, "zip", sep = "."))
216 renv_file_copy(path, newpath)
217 path <- newpath
218 }
219 }
220
221 # resolve default library path
222 library <- renv_libpaths_active()
223
224 # validate that we have command line tools installed and
225 # available for e.g. macOS
226 if (renv_platform_macos() && renv_package_type(path) == "source")
227 renv_xcode_check()
228
229 # perform platform-specific pre-install checks
230 renv_scope_install()
231
232 # perform the install
233 # note that we need to supply '-l' below as otherwise the library paths
234 # could be changed by, for example, site-specific profiles
235 args <- c(
236 "--vanilla",
237 "CMD", "INSTALL", "--preclean", "--no-multiarch",
238 if (config$install.keep.source()) "--with-keep.source" else "--without-keep.source",
239 r_cmd_install_option(package, "configure.args", TRUE),
240 r_cmd_install_option(package, "configure.vars", TRUE),
241 r_cmd_install_option(package, c("install.opts", "INSTALL_opts"), FALSE),
242 "-l", renv_shell_path(library),
243 ...,
244 renv_shell_path(path)
245 )
246
247 if (config$install.verbose()) {
248
249 status <- r(args, stdout = "", stderr = "")
250 if (!identical(status, 0L))
251 stopf("install of package '%s' failed", package)
252
253 installpath <- file.path(library, package)
254 if (!file.exists(installpath)) {
255 fmt <- "install of package '%s' failed: %s does not exist"
256 stopf(fmt, package, renv_path_pretty(installpath))
257 }
258
259 installpath
260
261 } else {
262
263 output <- r(args, stdout = TRUE, stderr = TRUE)
264 status <- attr(output, "status") %||% 0L
265 if (!identical(status, 0L))
266 r_exec_error(package, output, "install", status)
267
268 installpath <- file.path(library, package)
269 if (!file.exists(installpath))
270 r_exec_error(package, output, "install", installpath)
271
272 installpath
273
274 }
275
276
277 }
278
279 r_cmd_build <- function(package, path, ...) {
280
281 path <- renv_path_normalize(path, mustWork = TRUE)
282 args <- c("--vanilla", "CMD", "build", "--md5", ..., renv_shell_path(path))
283
284 output <- r(args, stdout = TRUE, stderr = TRUE)
285 status <- attr(output, "status") %||% 0L
286 if (!identical(status, 0L))
287 r_exec_error(package, output, "build", status)
288
289 pasted <- paste(output, collapse = "\n")
290 pattern <- "[*] building .([a-zA-Z0-9_.-]+)."
291 matches <- regexec(pattern, pasted)
292 text <- regmatches(pasted, matches)
293
294 tarball <- text[[1L]][[2L]]
295 if (!file.exists(tarball))
296 r_exec_error(package, output, "build", tarball)
297
298 file.path(getwd(), tarball)
299
300 }
301
302 r_cmd_install_option <- function(package, options, configure) {
303
304 # read option -- first, check for package-specific option, then
305 # fall back to 'global' option
306 for (option in options) {
307 value <- r_cmd_install_option_impl(package, option, configure)
308 if (!is.null(value))
309 return(value)
310 }
311
312 }
313
314 r_cmd_install_option_impl <- function(package, option, configure) {
315
316 value <-
317 getOption(paste(option, package, sep = ".")) %||%
318 getOption(option)
319
320 if (is.null(value))
321 return(NULL)
322
323 # if the value is named, treat it as a list,
324 # mapping package names to their configure arguments
325 if (!is.null(names(value)))
326 value <- as.list(value)
327
328 # check for named values
329 if (!is.null(names(value))) {
330 value <- value[[package]]
331 if (is.null(value))
332 return(NULL)
333 }
334
335 # if this is a configure option, format specially
336 if (configure) {
337 confkey <- sub(".", "-", option, fixed = TRUE)
338 confval <- if (!is.null(names(value)))
339 shQuote(paste(names(value), value, sep = "=", collapse = " "))
340 else
341 shQuote(paste(value, collapse = " "))
342 return(sprintf("--%s=%s", confkey, confval))
343 }
344
345 # otherwise, just paste it
346 paste(value, collapse = " ")
347
348 }