@@ -37,39 +37,54 @@ tbbLibraryPath <- function(name = NULL) {
3737 # find the request library (if any)
3838 libNames <- tbbLibNames [[sysname ]]
3939 for (libName in libNames ) {
40+
4041 tbbName <- file.path(tbbRoot , libName )
4142 if (file.exists(tbbName ))
4243 return (tbbName )
44+
45+ arch <- if (nzchar(.Platform $ r_arch )) .Platform $ r_arch
46+ suffix <- paste(c(" lib" , arch , libName ), collapse = " /" )
47+ tbbName <- system.file(suffix , package = " RcppParallel" )
48+ if (file.exists(tbbName ))
49+ return (tbbName )
50+
4351 }
4452
4553}
4654
4755tbbCxxFlags <- function () {
4856
49- flags <- character ()
50-
51- # opt-in to TBB on Windows
52- if (is_windows()) {
53- flags <- c(flags , " -DRCPP_PARALLEL_USE_TBB=1" )
57+ if (! TBB_ENABLED )
58+ return (" -DRCPP_PARALLEL_USE_TBB=0" )
59+
60+ flags <- c(" -DRCPP_PARALLEL_USE_TBB=1" )
61+
62+ # TBB does not have assembly code for Windows ARM64
63+ # so we need to use compiler builtins
64+ if (TBB_ENABLED && is_windows()) {
5465 if (R.version $ arch == " aarch64" ) {
55- # TBB does not have assembly code for Windows ARM64
56- # so we need to use compiler builtins
5766 flags <- c(flags , " -DTBB_USE_GCC_BUILTINS" )
5867 }
5968 }
6069
6170 # if TBB_INC is set, apply those library paths
6271 tbbInc <- Sys.getenv(" TBB_INC" , unset = TBB_INC )
63- if (nzchar(tbbInc )) {
64-
65- # add include path
66- flags <- c(flags , paste0(" -I" , asBuildPath(tbbInc )))
67-
68- # prefer new interface if version.h exists
72+ if (! file.exists(tbbInc )) {
73+ tbbInc <- system.file(" include" , package = " RcppParallel" )
74+ }
75+
76+ # add include path
77+ if (nzchar(tbbInc ) && file.exists(tbbInc )) {
78+
79+ # prefer new interface if version.h exists -- we keep this
80+ # for compatibility with packages like StanHeaders, rstan
6981 versionPath <- file.path(tbbInc , " tbb/version.h" )
7082 if (file.exists(versionPath ))
7183 flags <- c(flags , " -DTBB_INTERFACE_NEW" )
72-
84+
85+ # now add the include path
86+ flags <- c(flags , paste0(" -I" , asBuildPath(tbbInc )))
87+
7388 }
7489
7590 # return flags as string
@@ -79,41 +94,31 @@ tbbCxxFlags <- function() {
7994
8095# Return the linker flags required for TBB on this platform
8196tbbLdFlags <- function () {
82-
83- tbbFlags <- tbbLdFlagsImpl()
84-
97+
98+ # on Windows, we statically link to oneTBB
8599 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 )
100+
101+ libPath <- system.file(" libs" , package = " RcppParallel" )
102+ if (nzchar(.Platform $ r_arch ))
103+ libPath <- file.path(libPath , .Platform $ r_arch )
104+
105+ ldFlags <- sprintf(" -L%s -lRcppParallel" , asBuildPath(libPath ))
106+ return (ldFlags )
107+
90108 }
91-
92- tbbFlags
93-
94- }
95-
96- tbbLdFlagsImpl <- function () {
97-
109+
98110 # shortcut if TBB_LIB defined
99111 tbbLib <- Sys.getenv(" TBB_LINK_LIB" , Sys.getenv(" TBB_LIB" , unset = TBB_LIB ))
100112 if (nzchar(tbbLib )) {
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-
108- return (sprintf(fmt , asBuildPath(tbbLib )))
113+ fmt <- " -L%1$s -Wl,-rpath,%1$s -l%2$s -l%3$s"
114+ return (sprintf(fmt , asBuildPath(tbbLib ), TBB_NAME , TBB_MALLOC_NAME ))
109115 }
110-
111- # on Mac, Windows and Solaris, we need to explicitly link (#206)
112- needsExplicitFlags <- is_mac() || is_windows() || (is_solaris() && ! is_sparc())
113- if (needsExplicitFlags ) {
114- libPath <- asBuildPath(tbbLibraryPath())
115- libFlag <- paste0(" -L" , libPath )
116- return (paste(libFlag , " -ltbb" , " -ltbbmalloc" ))
116+
117+ # explicitly link on macOS
118+ # https://github.com/RcppCore/RcppParallel/issues/206
119+ if (is_mac()) {
120+ fmt <- " -L%s -l%s -l%s"
121+ return (sprintf(fmt , asBuildPath(tbbLibraryPath()), TBB_NAME , TBB_MALLOC_NAME ))
117122 }
118123
119124 # nothing required on other platforms
0 commit comments