| 1 |
|
| 2 |
renv_ext_enabled <- function() { |
| 3 |
|
| 4 |
# disable on Windows; may be able to re-evaluate in future |
| 5 |
if (renv_platform_windows()) |
| 6 |
return(FALSE) |
| 7 |
|
| 8 |
# disable if we're embedded |
| 9 |
if (!identical(.packageName, "renv")) |
| 10 |
return(FALSE) |
| 11 |
|
| 12 |
# otherwise, check envvar |
| 13 |
truthy(Sys.getenv("RENV_EXT_ENABLED", unset = "TRUE")) |
| 14 |
|
| 15 |
} |
| 16 |
|
| 17 |
renv_ext_init <- function() { |
| 18 |
|
| 19 |
if (!renv_ext_enabled() || is.null(the$dll_info)) |
| 20 |
return() |
| 21 |
|
| 22 |
envir <- renv_envir_self() |
| 23 |
symbols <- ls(envir = envir, pattern = "^__ffi__") |
| 24 |
map(symbols, function(symbol) { |
| 25 |
renv_binding_replace( |
| 26 |
envir = envir, |
| 27 |
symbol = substring(symbol, 8L), |
| 28 |
replacement = envir[[symbol]] |
| 29 |
) |
| 30 |
}) |
| 31 |
|
| 32 |
} |
| 33 |
|
| 34 |
renv_ext_onload <- function(libname, pkgname) { |
| 35 |
|
| 36 |
if (!renv_ext_enabled()) |
| 37 |
return() |
| 38 |
|
| 39 |
# if we're being invoked via devtools::load_all(), compile extensions |
| 40 |
package <- file.path(libname, pkgname) |
| 41 |
libsdir <- renv_package_libsdir(package) |
| 42 |
|
| 43 |
# use alternate library path for load_all + tests |
| 44 |
compile <- |
| 45 |
renv_envvar_exists("DEVTOOLS_LOAD") && |
| 46 |
!renv_envvar_exists("CALLR_IS_RUNNING") |
| 47 |
|
| 48 |
if (compile) { |
| 49 |
renv_ext_compile(package, libsdir) |
| 50 |
} |
| 51 |
|
| 52 |
# now try to load it |
| 53 |
soname <- paste0("renv", .Platform$dynlib.ext) |
| 54 |
sofile <- file.path(libsdir, soname) |
| 55 |
if (file.exists(sofile)) { |
| 56 |
info <- library.dynam("renv", pkgname, libname) |
| 57 |
the$dll_info <- info |
| 58 |
} |
| 59 |
|
| 60 |
} |
| 61 |
|
| 62 |
renv_ext_compile <- function(package, libsdir = renv_package_libsdir(package)) { |
| 63 |
|
| 64 |
if (!renv_ext_enabled()) |
| 65 |
return() |
| 66 |
|
| 67 |
soname <- if (renv_platform_windows()) "renv.dll" else "renv.so" |
| 68 |
unlink(file.path(libsdir, soname)) |
| 69 |
|
| 70 |
extdirs <- file.path(package, c("inst/ext", "ext")) |
| 71 |
extdir <- filter(extdirs, file.exists)[[1L]] |
| 72 |
|
| 73 |
srcfiles <- list.files(extdir, "\\.c$", full.names = TRUE) |
| 74 |
ensure_directory(libsdir) |
| 75 |
file.copy(srcfiles, libsdir) |
| 76 |
|
| 77 |
renv_scope_wd(libsdir) |
| 78 |
r <- file.path(R.home("bin"), if (.Platform$OS.type == "unix") "R" else "R.exe") |
| 79 |
system2(r, c("CMD", "SHLIB", shQuote(basename(srcfiles)))) |
| 80 |
|
| 81 |
oldfiles <- list.files(pattern = "\\.[co]$", full.names = TRUE) |
| 82 |
unlink(oldfiles) |
| 83 |
|
| 84 |
} |
| 85 |
|