Debian Codegraph @

R/abi.R

raw
1
2 renv_abi_check <- function(packages = NULL,
3 ...,
4 libpaths = NULL,
5 project = NULL)
6 {
7 if (renv_platform_windows()) {
8 writef("- ABI conflict checks are not available on Windows.")
9 return()
10 }
11
12 # disable via option if necessary
13 enabled <- getOption("renv.abi.check", default = TRUE)
14 if (identical(enabled, FALSE))
15 return()
16
17 # resolve arguments
18 project <- renv_project_resolve(project)
19 libpaths <- libpaths %||% renv_libpaths_all()
20
21 # read installed packages
22 packages <- packages %||% renv_abi_packages(project, libpaths)
23
24 # analyze each package
25 problems <- stack()
26 map(packages, function(package) {
27 tryCatch(
28 renv_abi_check_impl(package, problems),
29 error = warnify
30 )
31 })
32
33 # report problems
34 data <- problems$data()
35 if (empty(data)) {
36 fmt <- "- No ABI problems were detected in the set of installed packages."
37 writef(fmt)
38 return(invisible(data))
39 }
40
41 # combine everything together
42 tbl <- bind(data)
43
44 # make reports for each different type
45 reasons <- unique(tbl$reason)
46 if ("Rcpp_precious_list" %in% reasons) {
47 packages <- sort(unique(tbl$package[tbl$reason == "Rcpp_precious_list"]))
48 bulletin(
49 "The following packages were built against a newer version of Rcpp than is currently available:",
50 packages,
51 c(
52 paste(
53 "These packages depend on Rcpp (>= 1.0.7);",
54 "however, Rcpp", renv_package_version("Rcpp"), "is currently installed."
55 ),
56 "Consider installing a new version of Rcpp with 'install.packages(\"Rcpp\")'."
57 )
58 )
59 }
60
61 if ("missing" %in% reasons) {
62
63 missing <- tbl[tbl$reason == "missing", ]
64 bulletin(
65 "The following required system libraries are unavailable:",
66 unique(missing$dependency),
67 c(
68 "These system libraries may need to be re-installed.",
69 "Alternatively, you may need to re-install the packages which depend on these libraries."
70 )
71 )
72
73 # now, for each dependency, list the packages which require it
74 for (dep in unique(missing$dependency)) {
75 caution(header(sprintf("%s (required by)", dep)))
76 caution(paste("-", sort(tbl$package[missing$dependency == dep])))
77 caution()
78 }
79
80 }
81
82 invisible(tbl)
83
84 }
85
86 renv_abi_check_impl <- function(package, problems) {
87
88 # find path to package
89 pkgpath <- renv_package_find(package)
90
91 # check for dependency issues
92 if (renv_platform_macos())
93 renv_abi_deps_macos(package, problems)
94 else if (renv_platform_linux())
95 renv_abi_deps_linux(package, problems)
96
97 # look for an associated shared object
98 shlib <- renv_package_shlib(pkgpath)
99 if (!file.exists(shlib))
100 return()
101
102 # read symbols from LinkingTo dependency packages
103 pkgdesc <- renv_description_read(path = pkgpath)
104 if (is.null(pkgdesc$LinkingTo))
105 return()
106
107 # read symbols from the library
108 symbols <- renv_abi_symbols(shlib)
109
110 # handle Rcpp
111 linkdeps <- renv_description_parse_field(pkgdesc$LinkingTo)
112 if ("Rcpp" %in% linkdeps$Package && renv_package_installed("Rcpp"))
113 renv_abi_check_impl_rcpp(package, symbols, problems)
114
115 # TODO: other checks? more direct symbol checks for other packages?
116
117 }
118
119 renv_abi_check_impl_rcpp <- function(package, symbols, problems) {
120
121 # read Rcpp symbols
122 rcpplib <- renv_package_shlib("Rcpp")
123 rcppsyms <- renv_abi_symbols(rcpplib)
124
125 # perform checks for different versions of Rcpp
126 renv_abi_check_impl_rcpp_preciouslist(package, symbols, rcppsyms, problems)
127
128 }
129
130 renv_abi_check_impl_rcpp_preciouslist <- function(package, symbols, rcppsyms, problems) {
131
132 # check for dependency on Rcpp_precious APIs
133 required <- grep("Rcpp_precious", symbols$symbol, value = TRUE)
134 if (empty(required))
135 return()
136
137 # check for Rcpp_precious APIs being available
138 available <- grep("Rcpp_precious", rcppsyms$symbol, value = TRUE)
139 if (length(available))
140 return()
141
142 # skip if Rcpp appears to be new enough anyhow
143 version <- renv_package_version("Rcpp")
144 if (renv_version_ge(version, "1.0.7"))
145 return()
146
147 problem <- renv_abi_problem(
148 package = paste(package, renv_package_version(package)),
149 dependency = paste("Rcpp", renv_package_version("Rcpp")),
150 reason = "Rcpp_precious_list"
151 )
152
153 problems$push(problem)
154
155 }
156
157 renv_abi_symbols <- function(path, args = NULL) {
158
159 # invoke nm to read symbols
160 output <- renv_system_exec(
161 command = "nm",
162 args = c("-D", args, renv_shell_path(path)),
163 action = "reading symbols"
164 )
165
166 # parse output
167 parts <- strsplit(output, "\\s+")
168 data <- .mapply(c, parts, NULL)
169 names(data) <- c("offset", "type", "symbol")
170
171 # join into data.frame
172 as_data_frame(data)
173
174 }
175
176 renv_abi_problem <- function(package, dependency, reason) {
177
178 list(
179 package = package,
180 dependency = dependency,
181 reason = reason
182 )
183
184 }
185
186 renv_abi_packages <- function(project, libpaths) {
187
188 # create a lockfile
189 lockfile <- snapshot(
190 library = libpaths,
191 lockfile = NULL,
192 type = "all",
193 project = project
194 )
195
196 # return package names
197 names(lockfile$Packages)
198
199 }
200
201 renv_abi_deps_macos <- function(package, problems) {
202 # TODO
203 }
204
205 renv_abi_deps_linux <- function(package, problems) {
206
207 # get shlib path, if any
208 shlib <- renv_package_shlib(package)
209 if (!file.exists(shlib))
210 return()
211
212 # attempt to read dependencies
213 output <- renv_system_exec("ldd", renv_shell_path(shlib))
214
215 # look for 'not found' entries
216 idx <- regexpr(" => not found", output, fixed = TRUE)
217 matches <- substring(output, 2L, idx - 1L)
218
219 # drop duplicates, empty strings
220 names <- unique(matches[nzchar(matches)])
221 if (empty(names))
222 return()
223
224 # add problems
225 for (name in names) {
226
227 fmt <- "%s %s (%s)"
228 package <- sprintf(fmt, package, renv_package_version(package), shlib)
229
230 problem <- renv_abi_problem(
231 package = package,
232 dependency = name,
233 reason = "missing"
234 )
235
236 problems$push(problem)
237
238 }
239
240 }