wlandau / instantiate

Pre-compiled CmdStan models in R packages
https://wlandau.github.io/instantiate/
Other
22 stars 2 forks source link

Use src/install.libs.R instead of configuration scripts to install executables in packages #12

Closed wlandau closed 10 months ago

wlandau commented 11 months ago

This includes both cmdstanr and compiled Stan models. From https://cran.r-project.org/doc/manuals/R-exts.html#Package-subdirectories:

In very special cases packages may create binary files other than the shared objects/DLLs in the src directory. Such files will not be installed in a multi-architecture setting since R CMD INSTALL --libs-only is used to merge multiple sub-architectures and it only copies shared objects/DLLs. If a package wants to install other binaries (for example executable programs), it should provide an R script src/install.libs.R which will be run as part of the installation in the src build directory instead of copying the shared objects/DLLs. The script is run in a separate R environment containing the following variables: R_PACKAGE_NAME (the name of the package), R_PACKAGE_SOURCE (the path to the source directory of the package), R_PACKAGE_DIR (the path of the target installation directory of the package), R_ARCH (the arch-dependent part of the path, often empty), SHLIB_EXT (the extension of shared objects) and WINDOWS (TRUE on Windows, FALSE elsewhere). Something close to the default behavior could be replicated with the following src/install.libs.R file:

files <- Sys.glob(paste0("*", SHLIB_EXT)) dest <- file.path(R_PACKAGE_DIR, paste0('libs', R_ARCH)) dir.create(dest, recursive = TRUE, showWarnings = FALSE) file.copy(files, dest, overwrite = TRUE) if(file.exists("symbols.rds")) file.copy("symbols.rds", dest, overwrite = TRUE)

On the other hand, executable programs could be installed along the lines of

execs <- c("one", "two", "three") if(WINDOWS) execs <- paste0(execs, ".exe") if ( any(file.exists(execs)) ) { dest <- file.path(R_PACKAGE_DIR, paste0('bin', R_ARCH)) dir.create(dest, recursive = TRUE, showWarnings = FALSE) file.copy(execs, dest, overwrite = TRUE) }

Note the use of architecture-specific subdirectories of bin where needed. (Executables should installed under a bin directory and not under libs. It is good practice to check that they can be executed as part of the installation script, so a broken package is not installed.)

wlandau commented 10 months ago

Using install.libs.R instead of configure looks like it automatically fixes #9. I might even be able to close #1.