Debian Codegraph @

R/preflight.R

raw
1
2 # TODO: can we finish this work and start using it?
3 preflight <- function(lockfile) {
4
5 problems <- stack()
6
7 # check that we can compile C programs
8 renv_preflight_compiler(problems)
9
10 # if rJava is being used, ensure that Java is properly configured
11 renv_preflight_java(lockfile, problems)
12
13 data <- problems$data()
14 if (length(data)) {
15
16 feedback <- lines(
17 "The following problems were detected in your environment:",
18 "",
19 paste(data, collapse = "\n\n"),
20 "",
21 "The environment may not be restored correctly."
22 )
23
24 caution(feedback)
25
26 }
27
28 length(data) == 0
29
30 }
31
32 renv_preflight_compiler <- function(problems) {
33
34 # try to compile a simple program
35 program <- "void test() {}"
36 file <- renv_scope_tempfile("renv-test-compile-", fileext = ".c")
37 writeLines(program, con = file)
38
39 args <- c("CMD", "SHLIB", renv_shell_path(file))
40 status <- system2(R(), args, stdout = FALSE, stderr = FALSE)
41
42 if (!identical(status, 0L)) {
43
44 feedback <- lines(
45 "- Cannot compile C / C++ files from source.",
46 " Please ensure you have a compiler toolchain installed."
47 )
48
49 problems$push(feedback)
50
51 }
52
53 }
54
55 renv_preflight_java <- function(lockfile, problems) {
56
57 # no need to check if we're not using rJava
58 records <- renv_lockfile_records(lockfile)
59 if (is.null(records[["rJava"]]))
60 return(TRUE)
61
62 # TODO: no need to do anything if we're only installing binaries?
63 switch(
64 Sys.info()[["sysname"]],
65 Windows = renv_preflight_java_windows(problems),
66 renv_preflight_java_unix(problems)
67 )
68
69 }
70
71 renv_preflight_java_windows <- function(problems) {
72
73 home <- Sys.getenv("JAVA_HOME", unset = NA)
74 feedback <- case(
75
76 is.na(home) ~ lines(
77 "- JAVA_HOME is not set.",
78 " Please ensure you have a Java Development Kit (JDK) installed."
79 ),
80
81 !file.exists(home) ~ lines(
82 "- JAVA_HOME is set to a non-existent directory.",
83 " Please ensure you have a Java Development Kit (JDK) installed."
84 )
85
86 )
87
88 if (!is.null(feedback))
89 problems$push(feedback)
90
91 }
92
93 renv_preflight_java_unix <- function(problems) {
94
95 args <- c("CMD", "javareconf", "--dry-run")
96 status <- system2(R(), args, stdout = FALSE, stderr = FALSE)
97 if (!identical(status, 0L)) {
98
99 feedback <- lines(
100 "- Cannot compile Java files from source.",
101 " Please ensure you have a Java Development Kit (JDK) installed."
102 )
103
104 problems$push(feedback)
105
106 }
107
108 }