Skip to content

Commit 23f8af9

Browse files
committed
need to link to RcppParallel in plugin on Windows
1 parent 19cd92b commit 23f8af9

File tree

2 files changed

+55
-35
lines changed

2 files changed

+55
-35
lines changed

R/flags.R

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,32 @@
11

22
#' Compilation flags for RcppParallel
3-
#'
3+
#'
44
#' Output the compiler or linker flags required to build against RcppParallel.
5-
#'
5+
#'
66
#' These functions are typically called from `Makevars` as follows:
7-
#'
7+
#'
88
#' ```
99
#' PKG_LIBS += $(shell "${R_HOME}/bin/Rscript" -e "RcppParallel::LdFlags()")
1010
#' ```
11-
#'
11+
#'
1212
#' On Windows, the flags ensure that the package links with the built-in TBB
1313
#' library. On Linux and macOS, the output is empty, because TBB is loaded
1414
#' dynamically on load by `RcppParallel`.
15-
#'
15+
#'
1616
#' \R packages using RcppParallel should also add the following to their
1717
#' `NAMESPACE` file:
18-
#'
18+
#'
1919
#' ```
2020
#' importFrom(RcppParallel, RcppParallelLibs)
2121
#' ```
22-
#'
22+
#'
2323
#' This is necessary to ensure that \pkg{RcppParallel} (and so, TBB) is loaded
2424
#' and available.
25-
#'
25+
#'
2626
#' @name flags
2727
#' @rdname flags
2828
#' @aliases RcppParallelLibs LdFlags CxxFlags
29-
#'
29+
#'
3030
#' @return Returns \code{NULL}, invisibly. These functions are called for
3131
#' their side effects (writing the associated flags to stdout).
3232
#'

R/tbb.R

Lines changed: 46 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,53 @@
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
1414
tbbLibraryPath <- 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

4747
tbbCxxFlags <- 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
8181
tbbLdFlags <- 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

104124
tbbRoot <- 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

Comments
 (0)