Skip to content

Commit

Permalink
Merge pull request #19 from jranke/cross-platform_paths
Browse files Browse the repository at this point in the history
Cross platform paths
  • Loading branch information
eddelbuettel committed Nov 28, 2020
2 parents 17dce5b + f63a26f commit 538b7b9
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 18 deletions.
18 changes: 2 additions & 16 deletions R/cfunction.R
Original file line number Diff line number Diff line change
Expand Up @@ -275,29 +275,16 @@ compileCode <- function(f, code, language, verbose) {
wd = getwd()
on.exit(setwd(wd))
## Prepare temp file names
if ( .Platform$OS.type == "windows" ) {
## windows files
dir <- gsub("\\\\", "/", tempdir())
libCFile <- paste(dir, "/", f, ".EXT", sep="")
libLFile <- paste(dir, "/", f, ".dll", sep="")
libLFile2 <- paste(dir, "/", f, ".dll", sep="")
}
else {
## UNIX-alike build
libCFile <- paste(tempdir(), "/", f, ".EXT", sep="")
libLFile <- paste(tempdir(), "/", f, .Platform$dynlib.ext, sep="")
libLFile2 <- paste(tempdir(), "/", f, ".sl", sep="")
}
extension <- switch(language, "C++"=".cpp", C=".c", Fortran=".f", F95=".f95",
ObjectiveC=".m", "ObjectiveC++"=".mm")
libCFile <- sub(".EXT$", extension, libCFile)
libCFile <- file.path(tempdir(), paste0(f, extension))
libLFile <- file.path(tempdir(), paste0(f, .Platform$dynlib.ext))

## Write the code to the temp file for compilation
write(code, libCFile)

## Compile the code using the running version of R if several available
if ( file.exists(libLFile) ) file.remove( libLFile )
if ( file.exists(libLFile2) ) file.remove( libLFile2 )

setwd(dirname(libCFile))
errfile <- paste( basename(libCFile), ".err.txt", sep = "" )
Expand All @@ -308,7 +295,6 @@ compileCode <- function(f, code, language, verbose) {
errmsg <- readLines( errfile )
unlink( errfile )

if ( !file.exists(libLFile) && file.exists(libLFile2) ) libLFile <- libLFile2
if ( !file.exists(libLFile) ) {
cat("\nERROR(s) during compilation: source code errors or compiler configuration errors!\n")
if ( !verbose ) system2(cmd, args = paste(" CMD SHLIB --dry-run --preclean", basename(libCFile)))
Expand Down
5 changes: 3 additions & 2 deletions R/utilities.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@ setMethod("moveDLL",
# Create new path
if (!dir.exists(directory)) stop("There is no directory ", directory)
extension <- tools::file_ext(old_path)
new_path <- file.path(directory, paste(name, extension, sep = "."))
new_path <- normalizePath(file.path(directory, paste(name, extension, sep = ".")))

active_paths <- sapply(getLoadedDLLs(), function(di) di[["path"]])
active_paths <- sapply(getLoadedDLLs()[-1],
function(di) normalizePath(di[["path"]]))
if (new_path %in% active_paths) {
if (unload) {
if (inherits(try(dyn.unload(new_path)), "try-error"))
Expand Down

0 comments on commit 538b7b9

Please sign in to comment.