Debian Codegraph @

R/overwrite_shpkg.r

raw symbols SCIP
1 #' Overwrite rpath of linked shared library in osx
2 #'
3 #' Overwrite rpath of linked shared library
4 #' (e.g. \code{JuniperKernel/libs/JuniperKernel.so}
5 #' in osx only.
6 #' Typically, it is called by \code{.onLoad()} to update rpath if
7 #' \code{pbdZMQ} or \code{pbdZMQ/libs/libzmq.*.dylib} was moved to
8 #' a personal directory
9 #' (e.g. the binary package was installed to a none default path).
10 #' The commands \code{otool} and \code{install_name_tool} are required.
11 #' Permission may be needed (e.g. \code{sudo}) to overwrite the shared
12 #' library.
13 #'
14 #' @param mylib
15 #' the path where \code{mypkg} was installed (default \code{NULL} that will
16 #' search from R's path)
17 #' @param mypkg
18 #' the package for where \code{mypkg.so} will be checked or updated
19 #' @param linkingto
20 #' the package for where \code{libshpkg*.dylib} is located
21 #' @param shlib
22 #' name of shlib to be searched for
23 #'
24 #' @author Wei-Chen Chen \email{wccsnow@@gmail.com}.
25 #'
26 #' Programming with Big Data in R Website: \url{https://pbdr.org/}
27 #'
28 #' @examples
29 #' \dontrun{
30 #' ### Called by .onLoad() within "JuniperKernel/R/zzz.R"
31 #' overwrite.shpkg.rpath(mypkg = "JuniperKernel",
32 #' linkingto = "pbdZMQ",
33 #' shlib = "zmq")
34 #' }
35 #'
36 #' @keywords compile
37 #' @rdname zz_overwrite_shpkg
38 #' @name Overwrite shpkg
39 NULL
40
41
42 #' @rdname zz_overwrite_shpkg
43 #' @export
44 overwrite.shpkg.rpath <- function(mylib = NULL,
45 mypkg = "JuniperKernel", linkingto = "pbdZMQ", shlib = "zmq"){
46 if(Sys.info()[['sysname']] == "Darwin"){
47 cmd.ot <- system("which otool", intern = TRUE)
48
49 ### Find mylib if NULL.
50 if(is.null(mylib)){
51 mylib <- tools::file_path_as_absolute(system.file(package = mypkg))
52 }
53
54 ### Get rpath from mypkg's shared library.
55 fn.so <- paste(mylib, "/", mypkg, "/libs/", mypkg, ".so", sep = "")
56 rpath <- system(paste(cmd.ot, " -L ", fn.so, sep = ""), intern = TRUE)
57
58 ### Get the installed dylib from mypkg's rpath.
59 ptn1 <- paste("^\\t(.*/", linkingto, "/libs/lib", shlib, ".*\\.dylib) .*$",
60 sep = "")
61 i.rpath <- grep(ptn1, rpath)
62
63 ### 0 means external shlib was linked.
64 ### 1 means internal shlib was built.
65 if(length(i.rpath) == 1){
66 fn.dylib <- gsub(ptn1, "\\1", rpath[i.rpath])
67
68 ### Check if the file were moved to somewhere.
69 if(!file.exists(fn.dylib)){
70 ### Search the new location from R's library path.
71 dn <- tools::file_path_as_absolute(
72 system.file("./libs", package = linkingto))
73 ptn2 <- paste("lib", shlib, ".*\\.dylib", sep = "")
74 fn <- list.files(path = dn, pattern = ptn2)
75
76 ### Install the new location to the mypkg's shared library.
77 if(length(fn) == 1){
78 cmd.int <- system("which install_name_tool", intern = TRUE)
79
80 new.fn.dylib <- paste(dn, "/", fn, sep = "")
81 cmd <- paste(cmd.int, " -change ", fn.dylib, " ", new.fn.dylib,
82 " ", fn.so,
83 sep = "")
84 system(cmd)
85 } else{
86 stop("The internal shlib can not be identified in linkingto.")
87 }
88 }
89 }
90 }
91
92 return(invisible())
93 } # End of overwrite.shpkg().