Skip to content

Commit 686f90e

Browse files
committed
finishing up
1 parent f360fa4 commit 686f90e

File tree

5 files changed

+116
-5
lines changed

5 files changed

+116
-5
lines changed

.gitignore

-1
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,4 @@ src-x64
1313
tbb.log
1414

1515
R/tbb-autodetected.R
16-
src/install.libs.R
1716

R/tbb-autodetected.R.in

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11

22
TBB_LIB <- "@TBB_LIB@"
33
TBB_INC <- "@TBB_INC@"
4-
TBB_NAME <- "@TBB_NAME@"
4+
TBB_NAME <- "@TBB_NAME@"

src/Makevars.in

+2-3
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@ PKG_CPPFLAGS = @PKG_CPPFLAGS@
99
PKG_CXXFLAGS = @CXX11STD@ -DRCPP_PARALLEL_USE_TBB=1
1010
PKG_CXXFLAGS += -DTBB_SUPPRESS_DEPRECATED_MESSAGES=1
1111

12-
PKG_LIBS = -Wl,-L"$(TBB_LIB)" @TBB_RPATH@ -l$(TBB_NAME) -ltbbmalloc
13-
PKG_LIBS += @PKG_LIBS_EXTRA@
12+
PKG_LIBS = @PKG_LIBS@ @PKG_LIBS_EXTRA@
1413

1514
.PHONY: all tbb tbb-clean
1615

@@ -23,7 +22,7 @@ $(OBJECTS): tbb
2322
# NOTE: TBB libraries are installed via install.libs.R.
2423
# However, we need to copy headers here so that they are visible during compilation.
2524
tbb: tbb-clean
26-
TBB_LIB="$(TBB_LIB)" TBB_INC="$(TBB_INC)" TBB_NAME="$(TBB_NAME)" @R@ -s -f install.libs.R --args build
25+
@TBB_LIB="$(TBB_LIB)" TBB_INC="$(TBB_INC)" TBB_NAME="$(TBB_NAME)" @R@ -s -f install.libs.R --args build
2726

2827
# NOTE: we do not want to clean ../inst/lib or ../inst/libs here,
2928
# as we may be writing to those locations in multiarch builds

src/install.libs.R

+110
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
2+
# !diagnostics suppress=R_PACKAGE_DIR,SHLIB_EXT,R_ARCH
3+
.install.libs <- function(tbbLib) {
4+
5+
# copy default library
6+
files <- Sys.glob(paste0("*", SHLIB_EXT))
7+
dest <- file.path(R_PACKAGE_DIR, paste0("libs", R_ARCH))
8+
dir.create(dest, recursive = TRUE, showWarnings = FALSE)
9+
file.copy(files, dest, overwrite = TRUE)
10+
11+
# copy symbols if available
12+
if (file.exists("symbols.rds"))
13+
file.copy("symbols.rds", dest, overwrite = TRUE)
14+
15+
# also copy to package 'libs' folder, for devtools
16+
libsDest <- paste0("../libs", R_ARCH)
17+
dir.create(libsDest, recursive = TRUE, showWarnings = FALSE)
18+
file.copy(files, libsDest, overwrite = TRUE)
19+
20+
# copy tbb (NOTE: do not use inst/ folder as R will resolve symlinks,
21+
# behavior which we do _not_ want here!)
22+
tbbDest <- file.path(R_PACKAGE_DIR, paste0("lib", R_ARCH))
23+
dir.create(tbbDest, recursive = TRUE, showWarnings = FALSE)
24+
25+
# note: on Linux, TBB gets compiled with extensions like
26+
# '.so.2', so be ready to handle those
27+
shlibPattern <- switch(
28+
Sys.info()[["sysname"]],
29+
Windows = "^tbb.*\\.dll$",
30+
Darwin = "^libtbb.*\\.dylib$",
31+
"^libtbb.*\\.so.*$"
32+
)
33+
34+
if (!nzchar(tbbLib)) {
35+
36+
# using bundled TBB
37+
tbbLibs <- list.files(
38+
path = "tbb/build/lib_release",
39+
pattern = shlibPattern,
40+
full.names = TRUE
41+
)
42+
43+
for (tbbLib in tbbLibs) {
44+
system2("cp", c("-P", shQuote(tbbLib), shQuote(tbbDest)))
45+
}
46+
47+
} else {
48+
49+
# using system tbb
50+
tbbLibs <- list.files(
51+
path = tbbLib,
52+
pattern = shlibPattern,
53+
full.names = TRUE
54+
)
55+
56+
# don't copy symlinks
57+
tbbLibs <- tbbLibs[!nzchar(Sys.readlink(tbbLibs))]
58+
59+
# copy / link the libraries
60+
useSymlinks <- Sys.getenv("TBB_USE_SYMLINKS", unset = "TRUE")
61+
if (useSymlinks) {
62+
file.symlink(tbbLibs, tbbDest)
63+
} else {
64+
for (tbbLib in tbbLibs) {
65+
system2("cp", c("-P", shQuote(tbbLib), shQuote(tbbDest)))
66+
}
67+
}
68+
69+
}
70+
71+
}
72+
73+
useTbbPreamble <- function(tbbInc) {
74+
dir.create("../inst/include", recursive = TRUE, showWarnings = FALSE)
75+
for (suffix in c("oneapi", "serial", "tbb")) {
76+
tbbPath <- file.path(tbbInc, suffix)
77+
if (file.exists(tbbPath)) {
78+
file.copy(tbbPath, "../inst/include", recursive = TRUE)
79+
}
80+
}
81+
}
82+
83+
useSystemTbb <- function(tbbLib, tbbInc) {
84+
useTbbPreamble(tbbInc)
85+
}
86+
87+
useBundledTbb <- function() {
88+
useTbbPreamble("tbb/include")
89+
dir.create("tbb/build", showWarnings = FALSE)
90+
system("cd tbb/build; cmake -DTBB_TEST=0 -DTBB_EXAMPLES=0 .. && cmake --build .")
91+
system("cd tbb/build; mv *_relwithdebinfo lib_release")
92+
}
93+
94+
95+
# Main ----
96+
97+
tbbLib <- Sys.getenv("TBB_LIB")
98+
tbbInc <- Sys.getenv("TBB_INC")
99+
100+
args <- commandArgs(trailingOnly = TRUE)
101+
if (identical(args, "build")) {
102+
if (nzchar(tbbLib) && nzchar(tbbInc)) {
103+
useSystemTbb(tbbLib, tbbInc)
104+
} else {
105+
useBundledTbb()
106+
}
107+
} else {
108+
source("../R/tbb-autodetected.R")
109+
.install.libs(tbbLib)
110+
}

tools/config/configure.R

+3
Original file line numberDiff line numberDiff line change
@@ -213,8 +213,10 @@ define(
213213

214214
# set TBB_RPATH
215215
if (!is.na(tbbLib)) {
216+
define(PKG_LIBS = "-Wl,-L\"$(TBB_LIB)\" @TBB_RPATH@ -l$(TBB_NAME) -ltbbmalloc")
216217
define(TBB_RPATH = sprintf("-Wl,-rpath,%s", shQuote(tbbLib)))
217218
} else {
219+
define(PKG_LIBS = "@TBB_RPATH@ -l$(TBB_NAME) -ltbbmalloc")
218220
define(TBB_RPATH = "")
219221
}
220222

@@ -225,6 +227,7 @@ if (!is.na(tbbLib)) {
225227
define(PKG_CPPFLAGS = "-I../inst/include")
226228
}
227229

230+
228231
# macOS needs some extra flags set
229232
if (Sys.info()[["sysname"]] == "Darwin") {
230233
define(PKG_LIBS_EXTRA = "-Ltbb/build/lib_release -ltbb -Wl,-rpath,\"@loader_path/../lib\"")

0 commit comments

Comments
 (0)