Skip to content

Commit 31e7d74

Browse files
committed
try harder when forwarding compiler env vars
1 parent 84e4366 commit 31e7d74

File tree

2 files changed

+106
-31
lines changed

2 files changed

+106
-31
lines changed

R/utils.R

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,23 @@
22
# generate paths consumable by the compilers and linkers
33
# in particular, on Windows and Solaris, this means the path _cannot_ be quoted !!
44
asBuildPath <- function(path) {
5-
5+
66
# normalize paths using forward slashes
77
path <- normalizePath(path, winslash = "/", mustWork = FALSE)
8-
8+
99
# prefer short path names if the path has spaces
1010
if (is_windows() && grepl(" ", path, fixed = TRUE))
1111
path <- utils::shortPathName(path)
12-
12+
1313
# if we still have spaces, and we're not Windows or Solaris, try quoting
1414
if (grepl(" ", path, fixed = TRUE) && !is_solaris())
1515
path <- shQuote(path)
16-
16+
1717
# ensure we use forward slashes, even on Windows
1818
path <- chartr("\\", "/", path)
1919

2020
# return path
2121
path
22-
22+
2323
}
2424

src/install.libs.R

Lines changed: 101 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,27 @@
11

22
# !diagnostics suppress=R_PACKAGE_DIR,SHLIB_EXT,R_ARCH
33
.install.libs <- function(tbbLib) {
4-
4+
55
# copy default library
66
files <- Sys.glob(paste0("*", SHLIB_EXT))
77
dest <- file.path(R_PACKAGE_DIR, paste0("libs", R_ARCH))
88
dir.create(dest, recursive = TRUE, showWarnings = FALSE)
99
file.copy(files, dest, overwrite = TRUE)
10-
10+
1111
# copy symbols if available
1212
if (file.exists("symbols.rds"))
1313
file.copy("symbols.rds", dest, overwrite = TRUE)
14-
14+
1515
# also copy to package 'libs' folder, for devtools
1616
libsDest <- paste0("../libs", R_ARCH)
1717
dir.create(libsDest, recursive = TRUE, showWarnings = FALSE)
1818
file.copy(files, libsDest, overwrite = TRUE)
19-
19+
2020
# copy tbb (NOTE: do not use inst/ folder as R will resolve symlinks,
2121
# behavior which we do _not_ want here!)
2222
tbbDest <- file.path(R_PACKAGE_DIR, paste0("lib", R_ARCH))
2323
dir.create(tbbDest, recursive = TRUE, showWarnings = FALSE)
24-
24+
2525
# note: on Linux, TBB gets compiled with extensions like
2626
# '.so.2', so be ready to handle those
2727
shlibPattern <- switch(
@@ -34,32 +34,32 @@
3434
if (R.version$os == "emscripten") {
3535
shlibPattern <- "^libtbb.*\\.a$"
3636
}
37-
37+
3838
if (!nzchar(tbbLib)) {
39-
39+
4040
# using bundled TBB
4141
tbbLibs <- list.files(
4242
path = "tbb/build/lib_release",
4343
pattern = shlibPattern,
4444
full.names = TRUE
4545
)
46-
46+
4747
for (tbbLib in tbbLibs) {
4848
system2("cp", c("-P", shQuote(tbbLib), shQuote(tbbDest)))
4949
}
50-
50+
5151
} else {
52-
52+
5353
# using system tbb
5454
tbbLibs <- list.files(
5555
path = tbbLib,
5656
pattern = shlibPattern,
5757
full.names = TRUE
5858
)
59-
59+
6060
# don't copy symlinks
6161
tbbLibs <- tbbLibs[!nzchar(Sys.readlink(tbbLibs))]
62-
62+
6363
# copy / link the libraries
6464
useSymlinks <- Sys.getenv("TBB_USE_SYMLINKS", unset = .Platform$OS.type != "windows")
6565
if (useSymlinks) {
@@ -69,9 +69,9 @@
6969
system2("cp", c("-P", shQuote(tbbLib), shQuote(tbbDest)))
7070
}
7171
}
72-
72+
7373
}
74-
74+
7575
# on Windows, we create a stub library that links to us so that
7676
# older binaries (like rstan) can still load
7777
if (.Platform$OS.type == "windows") {
@@ -84,7 +84,7 @@
8484
file.copy("tbb-compat/tbb.dll", file.path(tbbDest, "tbb.dll"))
8585
}
8686
}
87-
87+
8888
}
8989

9090
useTbbPreamble <- function(tbbInc) {
@@ -102,22 +102,32 @@ useSystemTbb <- function(tbbLib, tbbInc) {
102102
}
103103

104104
useBundledTbb <- function() {
105-
105+
106106
useTbbPreamble("tbb/include")
107107
dir.create("tbb/build-tbb", showWarnings = FALSE)
108-
108+
109109
cmake <- Sys.getenv("CMAKE", unset = "cmake")
110110
buildType <- Sys.getenv("CMAKE_BUILD_TYPE", unset = "Release")
111111
verbose <- Sys.getenv("VERBOSE", unset = "0")
112112

113+
splitCompilerVar("CC", "CFLAGS")
114+
splitCompilerVar("CXX", "CXXFLAGS")
115+
116+
prependFlags("CPPFLAGS", "CFLAGS")
117+
prependFlags("CPPFLAGS", "CXXFLAGS")
118+
113119
cmakeFlags <- c(
114-
sprintf("-DCMAKE_BUILD_TYPE=%s", buildType),
120+
forwardEnvVar("CC", "CMAKE_C_COMPILER"),
121+
forwardEnvVar("CXX", "CMAKE_CXX_COMPILER"),
122+
forwardEnvVar("CFLAGS", "CMAKE_C_FLAGS"),
123+
forwardEnvVar("CXXFLAGS", "CMAKE_CXX_FLAGS"),
124+
forwardEnvVar("CMAKE_BUILD_TYPE", "CMAKE_BUILD_TYPE"),
115125
"-DTBB_TEST=0",
116126
"-DTBB_EXAMPLES=0",
117127
"-DTBB_STRICT=0",
118128
".."
119129
)
120-
130+
121131
if (R.version$os == "emscripten") {
122132
cmakeFlags <- c(
123133
"-DEMSCRIPTEN=1",
@@ -126,7 +136,7 @@ useBundledTbb <- function() {
126136
cmakeFlags
127137
)
128138
}
129-
139+
130140
writeLines("*** configuring tbb")
131141
owd <- setwd("tbb/build-tbb")
132142
output <- system2(cmake, shQuote(cmakeFlags), stdout = TRUE, stderr = TRUE)
@@ -138,7 +148,12 @@ useBundledTbb <- function() {
138148
writeLines(output)
139149
}
140150
setwd(owd)
141-
151+
152+
if (!identical(verbose, "0")) {
153+
writeLines("*** dumping CMakeCache.txt")
154+
writeLines(readLines("tbb/build-tbb/CMakeCache.txt"))
155+
}
156+
142157
writeLines("*** building tbb")
143158
owd <- setwd("tbb/build-tbb")
144159
output <- system2(cmake, c("--build", ".", "--config", "release"), stdout = TRUE, stderr = TRUE)
@@ -150,34 +165,94 @@ useBundledTbb <- function() {
150165
writeLines(output)
151166
}
152167
setwd(owd)
153-
168+
154169
shlibPattern <- switch(
155170
Sys.info()[["sysname"]],
156171
Windows = "^tbb.*\\.dll$",
157172
Darwin = "^libtbb.*\\.dylib$",
158173
"^libtbb.*\\.so.*$"
159174
)
160-
175+
161176
# WASM only supports static libraries
162177
if (R.version$os == "emscripten") {
163178
shlibPattern <- "^libtbb.*\\.a$"
164179
}
165-
180+
166181
tbbFiles <- list.files(
167182
file.path(getwd(), "tbb/build-tbb"),
168183
pattern = shlibPattern,
169184
recursive = TRUE,
170185
full.names = TRUE
171186
)
172-
187+
173188
dir.create("tbb/build/lib_release", recursive = TRUE, showWarnings = FALSE)
174189
file.copy(tbbFiles, "tbb/build/lib_release", overwrite = TRUE)
175190
unlink("tbb/build-tbb", recursive = TRUE)
176191
writeLines("*** finished building tbb")
177-
192+
193+
}
194+
195+
getenv <- function(key, unset = "") {
196+
Sys.getenv(key, unset = unset)
197+
}
198+
199+
200+
setenv <- function(key, value) {
201+
args <- list(paste(value, collapse = " "))
202+
names(args) <- key
203+
do.call(Sys.setenv, args)
178204
}
179205

180206

207+
# CMake doesn't support flags specified directly as part of the compiler
208+
# definition, so manually split it here.
209+
splitCompilerVar <- function(compilerVar, flagsVar) {
210+
211+
compiler <- Sys.getenv(compilerVar, unset = NA)
212+
if (is.na(compiler))
213+
return(FALSE)
214+
215+
tokens <- scan(text = compiler, what = character(), quiet = TRUE)
216+
if (length(tokens) < 2L)
217+
return(FALSE)
218+
219+
setenv(compilerVar, tokens[[1L]])
220+
221+
oldFlags <- Sys.getenv(flagsVar)
222+
newFlags <- c(tokens[-1L], oldFlags)
223+
setenv(flagsVar, newFlags[nzchar(newFlags)])
224+
225+
TRUE
226+
227+
}
228+
229+
230+
# Given an environment variable like 'CC', forward that to CMake
231+
# via the corresponding CMAKE_C_COMPILER flag.
232+
forwardEnvVar <- function(envVar, cmakeVar) {
233+
envVal <- Sys.getenv(envVar, unset = NA)
234+
if (!is.na(envVal)) {
235+
sprintf("-D%s=%s", cmakeVar, shQuote(envVal))
236+
}
237+
}
238+
239+
prependFlags <- function(prependFlags, toFlags) {
240+
241+
prependVal <- Sys.getenv(prependFlags, unset = NA)
242+
if (is.na(prependVal))
243+
return(FALSE)
244+
245+
oldVal <- Sys.getenv(toFlags, unset = NA)
246+
if (is.na(oldVal)) {
247+
setenv(toFlags, prependVal)
248+
} else {
249+
setenv(toFlags, paste(prependVal, oldVal))
250+
}
251+
252+
TRUE
253+
254+
}
255+
181256
# Main ----
182257

183258
tbbLib <- Sys.getenv("TBB_LIB")

0 commit comments

Comments
 (0)