Debian Codegraph @

R/extsoft.R

raw
1
2 renv_extsoft_curl_version <- function() {
3 Sys.getenv("RENV_EXTSOFT_CURL_VERSION", unset = "7.77.0")
4 }
5
6 renv_extsoft_install <- function(quiet = FALSE) {
7
8 extsoft <- renv_paths_extsoft()
9
10 ensure_directory(extsoft)
11 ensure_directory(file.path(extsoft, "lib/i386"))
12 ensure_directory(file.path(extsoft, "lib/x64"))
13
14 root <- "https://s3.amazonaws.com/rstudio-buildtools/extsoft"
15
16 files <- c(
17 sprintf("curl-%s-win32-mingw.zip", renv_extsoft_curl_version()),
18 "glpk32.zip",
19 "glpk64.zip",
20 "local323.zip",
21 "nlopt-2.4.2.zip",
22 "spatial324.zip"
23 )
24
25 # check for missing installs
26 files <- Filter(renv_extsoft_install_required, files)
27 if (empty(files)) {
28 if (!quiet) writef("- External software is up to date.")
29 return(TRUE)
30 }
31
32 if (interactive()) {
33
34 bulletin(
35 "The following external software tools will be installed:",
36 files,
37 sprintf("Tools will be installed into %s.", renv_path_pretty(extsoft))
38 )
39
40 cancel_if(!proceed())
41
42 }
43
44 for (file in files) {
45
46 # download the file
47 url <- file.path(root, file)
48 destfile <- renv_scope_tempfile("renv-archive-", fileext = ".zip")
49 download(url, destfile = destfile, quiet = quiet)
50
51 # write manifest
52 manifest <- renv_extsoft_manifest_path(file)
53 ensure_parent_directory(manifest)
54
55 before <- list.files(extsoft, recursive = TRUE)
56
57 # unpack archive
58 if (file == "glpk32.zip") {
59
60 unzip(destfile, files = "include/glpk.h", exdir = extsoft)
61 unzip(destfile, exdir = file.path(extsoft, "lib/i386"), junkpaths = TRUE)
62
63 } else if (file == "glpk64.zip") {
64
65 unzip(destfile, files = "include/glpk.h", exdir = extsoft)
66 unzip(destfile, exdir = file.path(extsoft, "lib/x64"), junkpaths = TRUE)
67
68 } else if (file == "nlopt-2.4.2.zip") {
69
70 unzip(destfile, exdir = extsoft)
71
72 file.copy(file.path(extsoft, "nlopt-2.4.2/include"), extsoft, recursive = TRUE)
73 file.copy(file.path(extsoft, "nlopt-2.4.2/lib"), extsoft, recursive = TRUE)
74 unlink(file.path(extsoft, "nlopt-2.4.2"), recursive = TRUE)
75
76
77 } else {
78
79 unzip(destfile, exdir = extsoft)
80
81 }
82
83 after <- list.files(extsoft, recursive = TRUE)
84 writeLines(setdiff(after, before), con = manifest)
85
86 }
87
88 writef("- External software successfully updated.")
89 TRUE
90
91 }
92
93 renv_extsoft_install_required <- function(file) {
94
95 manifest <- renv_extsoft_manifest_path(file)
96 if (!file.exists(manifest))
97 return(TRUE)
98
99 files <- catch(readLines(manifest, warn = FALSE))
100 if (inherits(files, "error"))
101 return(FALSE)
102
103 paths <- renv_paths_extsoft(files)
104 !all(file.exists(paths))
105
106 }
107
108 renv_extsoft_use <- function(quiet = FALSE) {
109
110 extsoft <- renv_paths_extsoft()
111 path <- "~/.R/Makevars"
112
113 ensure_parent_directory(path)
114 original <- if (file.exists(path))
115 readLines(path, warn = FALSE)
116 else
117 character()
118
119 contents <- original
120
121 localsoft <- paste("LOCAL_SOFT", extsoft, sep = " = ")
122 contents <- insert(contents, "^#?LOCAL_SOFT", localsoft)
123
124 localcpp <- "LOCAL_CPPFLAGS = -I\"$(LOCAL_SOFT)/include\""
125 contents <- insert(contents, "^#?LOCAL_CPPFLAGS", localcpp)
126
127 locallibs <- "LOCAL_LIBS = -L\"$(LOCAL_SOFT)/lib$(R_ARCH)\" -L\"$(LOCAL_SOFT)/lib\""
128 contents <- insert(contents, "^#?LOCAL_LIBS", locallibs)
129
130 libxml <- paste("LIB_XML", extsoft, sep = " = ")
131 contents <- insert(contents, "^#?LIB_XML", libxml)
132
133 if (identical(original, contents))
134 return(TRUE)
135
136 if (interactive()) {
137
138 bulletin(
139 "The following entries will be added to ~/.R/Makevars:",
140 c(localsoft, libxml, localcpp, locallibs),
141 "These tools will be used when compiling R packages from source."
142 )
143
144 cancel_if(!proceed())
145
146 }
147
148 if (!quiet) writef("- '%s' has been updated.", path)
149 writeLines(contents, con = path)
150 TRUE
151
152 }
153
154 renv_extsoft_manifest_path <- function(file) {
155 name <- paste(file, "manifest", sep = ".")
156 renv_paths_extsoft("manifests", name)
157 }