From e865d69d529133e0ca5ed3ce7965781a9bb681b1 Mon Sep 17 00:00:00 2001 From: mikejiang Date: Thu, 28 Feb 2019 16:33:16 -0800 Subject: [PATCH] build GatingSet.pb as shared lib. fix #7 --- R/build.R | 28 ++++++++++++++++++++++++---- R/hooks.R | 19 ++++++++++++++----- src/Makevars.in | 10 +++++++--- 3 files changed, 45 insertions(+), 12 deletions(-) diff --git a/R/build.R b/R/build.R index 2da5ba4..900bc04 100644 --- a/R/build.R +++ b/R/build.R @@ -37,16 +37,17 @@ pbCxxFlags <- function() { # Return the linker flags requried for pb on this platform pbLdFlags <- function(all = TRUE) { - gslibs <- system.file(file.path("lib", "GatingSet.pb.o"), package = "RProtoBufLib") +# gslibs <- system.file(file.path("lib", "GatingSet.pb.o"), package = "RProtoBufLib") # on Windows and Solaris we need to explicitly link against pb.dll if ((Sys.info()['sysname'] %in% c("Windows", "SunOS")) && !isSparc()) { pb <- pbLibPath() - res <- paste("-L", asBuildPath(dirname(pb)), " -lprotobuf", sep = "") + gs <- gsLibPath() + res <- paste("-L", asBuildPath(dirname(pb)), asBuildPath(dirname(gs)), " -lprotobuf -lGatingSet.pb", sep = "") } else { res <- "" } - if(all) - res <- paste(res, gslibs) + # if(all) + # res <- paste(res, gslibs) res } @@ -70,6 +71,25 @@ pbLibPath <- function(suffix = "") { NULL } } +gsLibPath <- function(suffix = "") { + sysname <- Sys.info()['sysname'] + pbSupported <- list( + "Darwin" = paste("libGatingSet.pb", suffix, ".dylib", sep = ""), + "Linux" = paste("libGatingSet.pb", suffix, ".so", sep = ""), + "Windows" = paste("libGatingSet.pb", suffix, ".dll", sep = ""), + "SunOS" = paste("libGatingSet.pb", suffix, ".so", sep = "") + ) + # browser() + if ((sysname %in% names(pbSupported)) && !isSparc()) { + libDir <- "lib/" + if (sysname == "Windows") + libDir <- paste(libDir, .Platform$r_arch, "/", sep="") + system.file(paste(libDir, pbSupported[[sysname]], sep = ""), + package = "RProtoBufLib") + } else { + NULL + } +} isSparc <- function() { Sys.info()['sysname'] == "SunOS" && Sys.info()[["machine"]] != "i86pc" diff --git a/R/hooks.R b/R/hooks.R index 8ef76ec..443e18c 100644 --- a/R/hooks.R +++ b/R/hooks.R @@ -7,12 +7,20 @@ dllInfo <- NULL pb <- pbLibPath() if (!is.null(pb)) { if (!file.exists(pb)) { - warning(paste("pb library", pb, "not found.")) + warning(paste("libprotobuf library", pb, "not found.")) } else { dllInfo <<- dyn.load(pb, local = FALSE, now = TRUE) } } - + gs <- gsLibPath() + if (!is.null(gs)) { + if (!file.exists(gs)) { + warning(paste("libGatingSet.pb library", gs, "not found.")) + } else { + dllInfo <<- c(dllInfo, dyn.load(gs, local = FALSE, now = TRUE)) + } + } + # load the package library # library.dynam("RProtoBufLib", pkgname, libname) @@ -23,7 +31,8 @@ dllInfo <- NULL # unload the package library # library.dynam.unload("RProtoBufLib", libpath) - # unload pb if we loaded it - if (!is.null(dllInfo)) - dyn.unload(dllInfo[["path"]]) + # unload dll if we loaded it + for(dll in dllInfo) + if (!is.null(dll)) + dyn.unload(dll[["path"]]) } diff --git a/src/Makevars.in b/src/Makevars.in index 63803fb..dce35c9 100644 --- a/src/Makevars.in +++ b/src/Makevars.in @@ -4,15 +4,19 @@ USER_INCLUDE = ${R_PACKAGE_DIR}/include USER_LIB_DIR = ${R_PACKAGE_DIR}/lib${R_ARCH}/ #expose compiled object of GatingSet pb API for cytolib to link to -GS_LIB = GatingSet_pb_lib/GatingSet.pb.o +GS_LIB=libGatingSet.pb${SHLIB_EXT} +GS_LIB_DIR = GatingSet_pb_lib all: copying - + +$(GS_LIB): + ${SHLIB_CXXLD} ${SHLIB_CXXLDFLAGS} ${CXXPICFLAGS} ${PKG_CPPFLAGS} -o ${GS_LIB_DIR}/${GS_LIB} ${GS_LIB_DIR}/GatingSet.pb.cc + #copy hdf5 library headers to package include copying: $(GS_LIB) mkdir -p "${USER_INCLUDE}" cp -r @PBBUILD@/include/google ${USER_INCLUDE} mkdir -p "${USER_LIB_DIR}" cp @PBBUILD@/lib/* ${USER_LIB_DIR} - cp $(GS_LIB) ${USER_LIB_DIR}/GatingSet.pb.o + cp ${GS_LIB_DIR}/$(GS_LIB) ${USER_LIB_DIR}/${GS_LIB}