forked from RcppCore/RcppParallel
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfigure.R
More file actions
332 lines (268 loc) · 8.77 KB
/
configure.R
File metadata and controls
332 lines (268 loc) · 8.77 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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
# make sure we call correct version of R
rExe <- if (.Platform$OS.type == "windows") "R.exe" else "R"
define(R = file.path(R.home("bin"), rExe))
# check whether user has Makevars file that might cause trouble
makevars <- Sys.getenv("R_MAKEVARS_USER", unset = "~/.R/Makevars")
if (file.exists(makevars)) {
contents <- readLines(makevars, warn = FALSE)
pattern <- "^(PKG_CPPFLAGS|PKG_CXXFLAGS)\\s*="
bad <- grep(pattern, contents, perl = TRUE, value = TRUE)
if (length(bad)) {
text <- c(
"",
sprintf("NOTE: '%s' contains variable declarations incompatible with RcppParallel:", makevars),
"",
paste0("\t", bad),
"",
"Makevars variables prefixed with 'PKG_' should be considered reserved for use by R packages.",
""
)
writeLines(text, con = stdout())
}
}
# Figure out the appropriate CXX prefix for the current
# version of R + configuration.
cxx <- "/usr/bin/c++"
candidates <- c("CXX11", "CXX1X", "CXX")
for (candidate in candidates) {
value <- r_cmd_config(candidate)
if (!is.null(value)) {
if (any(grepl("icpc", value))) {
define(COMPILER = "icc")
}
cxx <- candidate
break
}
}
# work around issue with '-Werror=format-security' being specified without
# a prior '-Wformat', which makes gcc angry
cxxflags <- read_r_config(sprintf("%sFLAGS", cxx), envir = NULL)[[1]]
broken <-
grepl(" -Werror=format-security ", cxxflags) &&
!grepl(" -Wformat ", cxxflags)
if (broken)
cxxflags <- gsub("-Werror=format-security", "-Wformat -Werror=format-security", cxxflags)
# add C++ standard if not set
if (!grepl("-std=", cxxflags, fixed = TRUE)) {
stdflag <- if (getRversion() < "4.0") {
"-std=c++0x"
} else {
"$(CXX11STD)"
}
cxxflags <- paste(stdflag, cxxflags)
}
# avoid including /usr/local/include, as this can cause
# RcppParallel to find and use a version of libtbb installed
# there as opposed to the bundled version
cppflags <- read_r_config("CPPFLAGS", envir = NULL)[[1]]
cppflags <- sub("(?: )?-I/usr/local/include", "", cppflags)
cppflags <- sub("(?: )?-I/opt/homebrew/include", "", cppflags)
cppflags <- sub("(?: )?-I/opt/local/libexec/onetbb/include", "", cppflags)
# define the set of flags appropriate to the current
# configuration of R
switch(
cxx,
CXX11 = define(
CC = "$(CC)",
CPPFLAGS = cppflags,
CXX11 = "$(CXX11)",
CXX11FLAGS = cxxflags,
CXX11STD = "$(CXX11STD)",
CXX11PICFLAGS = "$(CXX11PICFLAGS)"
),
CXX1X = define(
CC = "$(CC)",
CPPFLAGS = cppflags,
CXX11 = "$(CXX1X)",
CXX11FLAGS = cxxflags,
CXX11STD = "$(CXX1XSTD)",
CXX11PICFLAGS = "$(CXX1XPICFLAGS)"
),
CXX = define(
CC = "$(CC)",
CPPFLAGS = cppflags,
CXX11 = "$(CXX)",
CXX11FLAGS = cxxflags,
CXX11STD = "-std=c++0x",
CXX11PICFLAGS = "-fPIC"
),
stop("Failed to infer C / C++ compilation flags")
)
# on Windows, check for Rtools; if it exists, and we have tbb, use it
if (.Platform$OS.type == "windows") {
gccPath <- normalizePath(Sys.which("gcc"), winslash = "/")
tbbLib <- Sys.getenv("TBB_LIB", unset = NA)
if (is.na(tbbLib))
tbbLib <- normalizePath(file.path(gccPath, "../../lib"), winslash = "/")
tbbInc <- Sys.getenv("TBB_INC", unset = NA)
if (is.na(tbbInc))
tbbInc <- normalizePath(file.path(gccPath, "../../include"), winslash = "/")
tbbFiles <- list.files(tbbLib, pattern = "^libtbb")
if (length(tbbFiles)) {
tbbPattern <- "^lib(tbb\\d*(?:_static)?)\\.a$"
tbbName <- grep(tbbPattern, tbbFiles, perl = TRUE, value = TRUE)
tbbName <- gsub(tbbPattern, "\\1", tbbName, perl = TRUE)
tbbMallocPattern <- "^lib(tbbmalloc\\d*(?:_static)?)\\.a$"
tbbMallocName <- grep(tbbMallocPattern, tbbFiles, perl = TRUE, value = TRUE)
tbbMallocName <- gsub(tbbMallocPattern, "\\1", tbbMallocName, perl = TRUE)
Sys.setenv(
TBB_LIB = tbbLib,
TBB_INC = tbbInc,
TBB_NAME = tbbName,
TBB_MALLOC_NAME = tbbMallocName
)
}
}
# try and figure out path to TBB
tbbRoot <- Sys.getenv("TBB_ROOT", unset = NA)
tbbLib <- Sys.getenv("TBB_LIB", unset = NA)
tbbInc <- Sys.getenv("TBB_INC", unset = NA)
tbbName <- Sys.getenv("TBB_NAME", unset = "tbb")
tbbMallocName <- Sys.getenv("TBB_MALLOC_NAME", unset = "tbbmalloc")
# check TBB_ROOT first if defined
if (!is.na(tbbRoot)) {
if (is.na(tbbLib)) {
tbbLib <- file.path(tbbRoot, "lib")
}
if (is.na(tbbInc)) {
tbbInc <- file.path(tbbRoot, "include")
}
}
# if TBB_LIB is defined, guess TBB_INC
if (!is.na(tbbLib) && is.na(tbbInc)) {
tbbIncCandidate <- file.path(tbbLib, "../include")
if (file.exists(tbbIncCandidate)) {
tbbInc <- normalizePath(tbbIncCandidate)
}
}
# if TBB_LIB and TBB_INC are still not defined, try auto-detecting
tryAutoDetect <-
.Platform$OS.type == "unix" &&
Sys.getenv("TBB_AUTODETECT", unset = "FALSE") == "TRUE" &&
is.na(tbbLib) &&
is.na(tbbInc)
if (tryAutoDetect) {
sysInfo <- as.list(Sys.info())
homebrewPrefix <- if (sysInfo$sysname == "Darwin") {
"/opt/homebrew"
} else {
"/usr/local"
}
tbbLibSearch <- if (sysInfo$sysname == "Darwin") {
file.path(homebrewPrefix, "opt/tbb/lib/libtbb.dylib")
} else {
Sys.glob(c(
"/usr/*/libtbb.so",
"/usr/*/*/libtbb.so",
"/usr/*/*/*/libtbb.so"
))
}
tbbIncSearch <- if (sysInfo$sysname == "Darwin") {
file.path(homebrewPrefix, "opt/tbb/include/tbb")
} else {
Sys.glob(c(
"/usr/include/tbb.h",
"/usr/include/*/tbb.h"
))
}
if (length(tbbLibSearch) &&
length(tbbIncSearch) &&
file.exists(tbbLibSearch[[1L]]) &&
file.exists(tbbIncSearch[[1L]]))
{
tbbLib <- dirname(tbbLibSearch[[1L]])
tbbInc <- dirname(tbbIncSearch[[1L]])
}
}
# now, define TBB_LIB and TBB_INC as appropriate
define(
TBB_LIB = if (!is.na(tbbLib)) tbbLib else "",
TBB_INC = if (!is.na(tbbInc)) tbbInc else "",
TBB_NAME = tbbName,
TBB_MALLOC_NAME = tbbMallocName
)
# set PKG_LIBS
pkgLibs <- if (!is.na(tbbLib)) {
c(
"-Wl,-L\"$(TBB_LIB)\"",
sprintf("-Wl,-rpath,%s", shQuote(tbbLib)),
"-l$(TBB_NAME)",
"-l$(TBB_MALLOC_NAME)"
)
} else if (.Platform$OS.type == "windows") {
NULL
} else {
if (R.version$os == "emscripten") {
c(
"-Wl,-Ltbb/build/lib_release",
"-l$(TBB_NAME)"
)
} else {
c(
"-Wl,-Ltbb/build/lib_release",
"-l$(TBB_NAME)",
"-l$(TBB_MALLOC_NAME)"
)
}
}
# on Windows, we may need to link to ssp; otherwise,
# we see errors like
#
# C:\rtools43\x86_64-w64-mingw32.static.posix\bin/ld.exe: C:/rtools43/x86_64-w64-mingw32.static.posix/lib/libtbb12.a(allocator.cpp.obj):allocator.cpp:(.text+0x18b): undefined reference to `__stack_chk_fail'
#
if (.Platform$OS.type == "windows") {
pkgLibs <- c(pkgLibs, "-lssp")
}
define(PKG_LIBS = paste(pkgLibs, collapse = " "))
# if we're going to build tbb from sources, check for cmake
define(CMAKE = "")
if (is.na(tbbLib)) {
cmake <- local({
# check for envvar
cmake <- Sys.getenv("CMAKE", unset = NA)
if (!is.na(cmake))
return(cmake)
# check for path
cmake <- Sys.which("cmake")
if (nzchar(cmake))
return(cmake)
# check for macOS cmake
cmake <- "/Applications/CMake.app/Contents/bin/cmake"
if (file.exists(cmake))
return(cmake)
stop("cmake was not found")
})
# make sure we have an appropriate version of cmake installed
output <- system("cmake --version", intern = TRUE)[[1L]]
cmakeVersion <- numeric_version(sub("cmake version ", "", output))
if (cmakeVersion < "3.5") {
stop("error: RcppParallel requires cmake (>= 3.6); you have ", cmakeVersion)
}
define(CMAKE = cmake)
}
# set TBB_RPATH
if (!is.na(tbbLib)) {
define(TBB_RPATH = sprintf("-Wl,-rpath,%s", shQuote(tbbLib)))
} else {
define(TBB_RPATH = "")
}
# now, set up PKG_CPPFLAGS
if (!is.na(tbbLib)) {
define(PKG_CPPFLAGS = "-I../inst/include -I\"$(TBB_INC)\"")
} else {
define(PKG_CPPFLAGS = "-I../inst/include")
}
# PKG_CXXFLAGS
if (.Platform$OS.type == "windows" && is.na(tbbLib)) {
define(TBB_ENABLED = FALSE)
define(PKG_CXXFLAGS = "-DRCPP_PARALLEL_USE_TBB=0")
} else {
define(TBB_ENABLED = TRUE)
define(PKG_CXXFLAGS = "-DRCPP_PARALLEL_USE_TBB=1")
}
# macOS needs some extra flags set
if (Sys.info()[["sysname"]] == "Darwin") {
define(PKG_LIBS_EXTRA = "-Wl,-rpath,@loader_path/../lib")
} else {
define(PKG_LIBS_EXTRA = "")
}