-
Notifications
You must be signed in to change notification settings - Fork 59
Expand file tree
/
Copy pathinstall.libs.R
More file actions
273 lines (219 loc) · 7.42 KB
/
install.libs.R
File metadata and controls
273 lines (219 loc) · 7.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
# !diagnostics suppress=R_PACKAGE_DIR,SHLIB_EXT,R_ARCH
.install.libs <- function(tbbLib) {
# copy default library
files <- Sys.glob(paste0("*", SHLIB_EXT))
dest <- file.path(R_PACKAGE_DIR, paste0("libs", R_ARCH))
dir.create(dest, recursive = TRUE, showWarnings = FALSE)
file.copy(files, dest, overwrite = TRUE)
# copy symbols if available
if (file.exists("symbols.rds"))
file.copy("symbols.rds", dest, overwrite = TRUE)
# also copy to package 'libs' folder, for devtools
libsDest <- paste0("../libs", R_ARCH)
dir.create(libsDest, recursive = TRUE, showWarnings = FALSE)
file.copy(files, libsDest, overwrite = TRUE)
# copy tbb (NOTE: do not use inst/ folder as R will resolve symlinks,
# behavior which we do _not_ want here!)
tbbDest <- file.path(R_PACKAGE_DIR, paste0("lib", R_ARCH))
dir.create(tbbDest, recursive = TRUE, showWarnings = FALSE)
# note: on Linux, TBB gets compiled with extensions like
# '.so.2', so be ready to handle those
shlibPattern <- switch(
Sys.info()[["sysname"]],
Windows = "^tbb.*\\.dll$",
Darwin = "^libtbb.*\\.dylib$",
"^libtbb.*\\.so.*$"
)
# WASM only supports static libraries
if (R.version$os == "emscripten") {
shlibPattern <- "^libtbb.*\\.a$"
}
if (!nzchar(tbbLib)) {
# using bundled TBB
tbbLibs <- list.files(
path = "tbb/build/lib_release",
pattern = shlibPattern,
full.names = TRUE
)
for (tbbLib in tbbLibs) {
system2("cp", c("-P", shQuote(tbbLib), shQuote(tbbDest)))
}
} else {
# using system tbb
tbbLibs <- list.files(
path = tbbLib,
pattern = shlibPattern,
full.names = TRUE
)
# don't copy symlinks
tbbLibs <- tbbLibs[!nzchar(Sys.readlink(tbbLibs))]
# copy / link the libraries
useSymlinks <- Sys.getenv("TBB_USE_SYMLINKS", unset = .Platform$OS.type != "windows")
if (useSymlinks) {
file.symlink(tbbLibs, tbbDest)
} else {
for (tbbLib in tbbLibs) {
system2("cp", c("-P", shQuote(tbbLib), shQuote(tbbDest)))
}
}
}
# on Windows, we create a stub library that links to us so that
# older binaries (like rstan) can still load
if (.Platform$OS.type == "windows") {
tbbDll <- file.path(tbbDest, "tbb.dll")
if (!file.exists(tbbDll)) {
writeLines("** creating tbb stub library")
status <- system("R CMD SHLIB -o tbb-compat/tbb.dll tbb-compat/tbb-compat.cpp")
if (status != 0)
stop("error building tbb stub library")
file.copy("tbb-compat/tbb.dll", file.path(tbbDest, "tbb.dll"))
}
}
}
useTbbPreamble <- function(tbbInc) {
dir.create("../inst/include", recursive = TRUE, showWarnings = FALSE)
for (suffix in c("oneapi", "serial", "tbb")) {
tbbPath <- file.path(tbbInc, suffix)
if (file.exists(tbbPath)) {
file.copy(tbbPath, "../inst/include", recursive = TRUE)
}
}
}
useSystemTbb <- function(tbbLib, tbbInc) {
useTbbPreamble(tbbInc)
}
useBundledTbb <- function() {
useTbbPreamble("tbb/include")
dir.create("tbb/build-tbb", showWarnings = FALSE)
cmake <- Sys.getenv("CMAKE", unset = "cmake")
buildType <- Sys.getenv("CMAKE_BUILD_TYPE", unset = "Release")
verbose <- Sys.getenv("VERBOSE", unset = "0")
splitCompilerVar("CC", "CFLAGS")
splitCompilerVar("CXX", "CXXFLAGS")
prependFlags("CPPFLAGS", "CFLAGS")
prependFlags("CPPFLAGS", "CXXFLAGS")
cmakeFlags <- c(
forwardEnvVar("CC", "CMAKE_C_COMPILER"),
forwardEnvVar("CXX", "CMAKE_CXX_COMPILER"),
forwardEnvVar("CFLAGS", "CMAKE_C_FLAGS"),
forwardEnvVar("CXXFLAGS", "CMAKE_CXX_FLAGS"),
forwardEnvVar("CMAKE_BUILD_TYPE", "CMAKE_BUILD_TYPE"),
"-DTBB_TEST=0",
"-DTBB_EXAMPLES=0",
"-DTBB_STRICT=0",
".."
)
if (R.version$os == "emscripten") {
cmakeFlags <- c(
"-DEMSCRIPTEN=1",
"-DTBBMALLOC_BUILD=0",
"-DBUILD_SHARED_LIBS=0",
cmakeFlags
)
}
writeLines("*** configuring tbb")
owd <- setwd("tbb/build-tbb")
output <- system2(cmake, shQuote(cmakeFlags), stdout = TRUE, stderr = TRUE)
status <- attr(output, "status")
if (is.numeric(status) && status != 0L) {
writeLines(output)
stop("error configuring tbb (status code ", status, ")")
} else if (!identical(verbose, "0")) {
writeLines(output)
}
setwd(owd)
if (!identical(verbose, "0")) {
writeLines("*** dumping CMakeCache.txt")
writeLines(readLines("tbb/build-tbb/CMakeCache.txt"))
}
writeLines("*** building tbb")
owd <- setwd("tbb/build-tbb")
output <- system2(cmake, c("--build", ".", "--config", "release"), stdout = TRUE, stderr = TRUE)
status <- attr(output, "status")
if (is.numeric(status) && status != 0L) {
writeLines(output)
stop("error building tbb (status code ", status, ")")
} else if (!identical(verbose, "0")) {
writeLines(output)
}
setwd(owd)
shlibPattern <- switch(
Sys.info()[["sysname"]],
Windows = "^tbb.*\\.dll$",
Darwin = "^libtbb.*\\.dylib$",
"^libtbb.*\\.so.*$"
)
# WASM only supports static libraries
if (R.version$os == "emscripten") {
shlibPattern <- "^libtbb.*\\.a$"
}
tbbFiles <- list.files(
file.path(getwd(), "tbb/build-tbb"),
pattern = shlibPattern,
recursive = TRUE,
full.names = TRUE
)
dir.create("tbb/build/lib_release", recursive = TRUE, showWarnings = FALSE)
file.copy(tbbFiles, "tbb/build/lib_release", overwrite = TRUE)
unlink("tbb/build-tbb", recursive = TRUE)
writeLines("*** finished building tbb")
}
getenv <- function(key, unset = "") {
Sys.getenv(key, unset = unset)
}
setenv <- function(key, value) {
args <- list(paste(value, collapse = " "))
names(args) <- key
do.call(Sys.setenv, args)
}
# CMake doesn't support flags specified directly as part of the compiler
# definition, so manually split it here.
splitCompilerVar <- function(compilerVar, flagsVar) {
compiler <- Sys.getenv(compilerVar, unset = NA)
if (is.na(compiler))
return(FALSE)
tokens <- scan(text = compiler, what = character(), quiet = TRUE)
if (length(tokens) < 2L)
return(FALSE)
setenv(compilerVar, tokens[[1L]])
oldFlags <- Sys.getenv(flagsVar)
newFlags <- c(tokens[-1L], oldFlags)
setenv(flagsVar, newFlags[nzchar(newFlags)])
TRUE
}
# Given an environment variable like 'CC', forward that to CMake
# via the corresponding CMAKE_C_COMPILER flag.
forwardEnvVar <- function(envVar, cmakeVar) {
envVal <- Sys.getenv(envVar, unset = NA)
if (!is.na(envVal)) {
sprintf("-D%s=%s", cmakeVar, envVal)
}
}
prependFlags <- function(prependFlags, toFlags) {
prependVal <- Sys.getenv(prependFlags, unset = NA)
if (is.na(prependVal))
return(FALSE)
oldVal <- Sys.getenv(toFlags, unset = NA)
if (is.na(oldVal)) {
setenv(toFlags, prependVal)
} else {
setenv(toFlags, paste(prependVal, oldVal))
}
TRUE
}
# Main ----
tbbLib <- Sys.getenv("TBB_LIB")
tbbInc <- Sys.getenv("TBB_INC")
args <- commandArgs(trailingOnly = TRUE)
if (identical(args, "build")) {
if (nzchar(tbbLib) && nzchar(tbbInc)) {
useSystemTbb(tbbLib, tbbInc)
} else if (.Platform$OS.type == "windows") {
writeLines("** building RcppParallel without tbb backend")
} else {
useBundledTbb()
}
} else {
source("../R/tbb-autodetected.R")
.install.libs(tbbLib)
}