11
22#' Get the Path to a TBB Library
3- #'
3+ #'
44#' Retrieve the path to a TBB library. This can be useful for \R packages
55#' using RcppParallel that wish to use, or re-use, the version of TBB that
66#' RcppParallel has been configured to use.
7- #'
7+ #'
88#' @param name
99#' The name of the TBB library to be resolved. Normally, this is one of
1010#' `tbb`, `tbbmalloc`, or `tbbmalloc_proxy`. When `NULL`, the library
1111#' path containing the TBB libraries is returned instead.
12- #'
12+ #'
1313#' @export
1414tbbLibraryPath <- function(name = NULL) {
15-
15+
1616 # library paths for different OSes
1717 sysname <- Sys.info()[["sysname"]]
18-
18+
1919 # find root for TBB install
2020 tbbRoot <- Sys.getenv("TBB_LIB", unset = tbbRoot())
2121 if (is.null(name))
2222 return(tbbRoot)
23-
23+
2424 # form library names
2525 tbbLibNames <- list(
2626 "Darwin" = paste0("lib", name, ".dylib"),
2727 "Windows" = paste0( name, ".dll"),
2828 "SunOS" = paste0("lib", name, ".so"),
2929 "Linux" = paste0("lib", name, c(".so.2", ".so"))
3030 )
31-
31+
3232 # skip systems that we know not to be compatible
3333 isCompatible <- !is_sparc() && !is.null(tbbLibNames[[sysname]])
3434 if (!isCompatible)
3535 return(NULL)
36-
36+
3737 # find the request library (if any)
3838 libNames <- tbbLibNames[[sysname]]
3939 for (libName in libNames) {
4040 tbbName <- file.path(tbbRoot, libName)
4141 if (file.exists(tbbName))
4242 return(tbbName)
4343 }
44-
44+
4545}
4646
4747tbbCxxFlags <- function() {
48-
48+
4949 flags <- character()
50-
50+
5151 # opt-in to TBB on Windows
5252 if (is_windows()) {
5353 flags <- c(flags, "-DRCPP_PARALLEL_USE_TBB=1")
@@ -57,58 +57,78 @@ tbbCxxFlags <- function() {
5757 flags <- c(flags, "-DTBB_USE_GCC_BUILTINS")
5858 }
5959 }
60-
60+
6161 # if TBB_INC is set, apply those library paths
6262 tbbInc <- Sys.getenv("TBB_INC", unset = TBB_INC)
6363 if (nzchar(tbbInc)) {
64-
64+
6565 # add include path
6666 flags <- c(flags, paste0("-I", asBuildPath(tbbInc)))
67-
67+
6868 # prefer new interface if version.h exists
6969 versionPath <- file.path(tbbInc, "tbb/version.h")
7070 if (file.exists(versionPath))
7171 flags <- c(flags, "-DTBB_INTERFACE_NEW")
72-
72+
7373 }
74-
74+
7575 # return flags as string
7676 paste(flags, collapse = " ")
77-
77+
7878}
7979
8080# Return the linker flags required for TBB on this platform
8181tbbLdFlags <- function() {
82-
82+
83+ tbbFlags <- tbbLdFlagsImpl()
84+
85+ if (is_windows()) {
86+ libDir <- system.file("libs", .Platform$r_arch, package = "RcppParallel")
87+ libName <- paste0("RcppParallel", .Platform$dynlib.ext)
88+ newFlags <- sprintf("-L%s -lRcppParallel", shQuote(libDir))
89+ tbbFlags <- paste(newFlags, tbbFlags)
90+ }
91+
92+ tbbFlags
93+
94+ }
95+
96+ tbbLdFlagsImpl <- function() {
97+
8398 # shortcut if TBB_LIB defined
8499 tbbLib <- Sys.getenv("TBB_LINK_LIB", Sys.getenv("TBB_LIB", unset = TBB_LIB))
85100 if (nzchar(tbbLib)) {
86- fmt <- if (is_windows()) "-L%1$s -ltbb -ltbbmalloc"
87- else "-L%1$s -Wl,-rpath,%1$s -ltbb -ltbbmalloc"
101+
102+ fmt <- if (is_windows()) {
103+ "-L%1$s -ltbb -ltbbmalloc"
104+ } else {
105+ "-L%1$s -Wl,-rpath,%1$s -ltbb -ltbbmalloc"
106+ }
107+
88108 return(sprintf(fmt, asBuildPath(tbbLib)))
89109 }
90-
110+
91111 # on Mac, Windows and Solaris, we need to explicitly link (#206)
92112 needsExplicitFlags <- is_mac() || is_windows() || (is_solaris() && !is_sparc())
93113 if (needsExplicitFlags) {
94114 libPath <- asBuildPath(tbbLibraryPath())
95115 libFlag <- paste0("-L", libPath)
96116 return(paste(libFlag, "-ltbb", "-ltbbmalloc"))
97117 }
98-
118+
99119 # nothing required on other platforms
100120 ""
101-
121+
102122}
103123
104124tbbRoot <- function() {
105-
125+
106126 if (nzchar(TBB_LIB))
107127 return(TBB_LIB)
108-
128+
109129 rArch <- .Platform$r_arch
110130 parts <- c("lib", if (nzchar(rArch)) rArch)
111131 libDir <- paste(parts, collapse = "/")
112132 system.file(libDir, package = "RcppParallel")
113-
133+
114134}
0 commit comments