Skip to content

Commit 9a6c9c1

Browse files
committed
Fix aligned allocation for old macOS targets
1 parent 711e25d commit 9a6c9c1

6 files changed

Lines changed: 78 additions & 4 deletions

File tree

NEWS.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# RcppParallel (development version)
22

3+
* Fixed bundled oneTBB builds and downstream compilation with Clang and libc++
4+
when targeting macOS 10.12 or earlier, where C++17 aligned allocation is not
5+
available. (#219)
6+
37

48
# RcppParallel 6.2.0
59

R/tbb-autodetected.R.in

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
TBB_ENABLED <- @TBB_ENABLED@
33
TBB_LIB <- "@TBB_LIB@"
44
TBB_INC <- "@TBB_INC@"
5+
TBB_CXXFLAGS <- "@TBB_CXXFLAGS@"
56

67
TBB_NAME <- "@TBB_NAME@"
7-
TBB_MALLOC_NAME <- "@TBB_MALLOC_NAME@"
8+
TBB_MALLOC_NAME <- "@TBB_MALLOC_NAME@"

R/tbb.R

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@ tbbCxxFlags <- function() {
5757

5858
flags <- c("-DRCPP_PARALLEL_USE_TBB=1")
5959

60+
if (nzchar(TBB_CXXFLAGS))
61+
flags <- c(flags, TBB_CXXFLAGS)
62+
6063
# if TBB_INC is set, apply those library paths
6164
tbbInc <- Sys.getenv("TBB_INC", unset = TBB_INC)
6265
if (!file.exists(tbbInc)) {

src/Makevars.in

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ TBB_LIB = @TBB_LIB@
1010
TBB_INC = @TBB_INC@
1111
TBB_NAME = @TBB_NAME@
1212
TBB_MALLOC_NAME = @TBB_MALLOC_NAME@
13+
TBB_CXXFLAGS = @TBB_CXXFLAGS@
1314

1415
PKG_CPPFLAGS = @PKG_CPPFLAGS@
1516
PKG_CXXFLAGS = @PKG_CXXFLAGS@
@@ -28,7 +29,8 @@ tbb: tbb-clean
2829
@TBB_LIB="$(TBB_LIB)" TBB_INC="$(TBB_INC)" \
2930
TBB_NAME="$(TBB_NAME)" TBB_MALLOC_NAME="$(TBB_MALLOC_NAME)" \
3031
CC="$(CC)" CFLAGS="$(CFLAGS)" CPPFLAGS="$(CPPFLAGS)" \
31-
CXX="$(CXX)" CXXFLAGS="$(CXXFLAGS)" LDFLAGS="$(LDFLAGS)" \
32+
CXX="$(CXX)" CXXFLAGS="$(CXXFLAGS) $(TBB_CXXFLAGS)" \
33+
LDFLAGS="$(LDFLAGS)" \
3234
CMAKE="$(CMAKE)" "@R@" -s -f install.libs.R --args build
3335

3436
# NOTE: we do not want to clean ../inst/lib or ../inst/libs here,

tests/test-cxx-flags.R

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Unit tests for the compiler flags handed to downstream packages through
2+
# RcppParallel::CxxFlags().
3+
4+
RcppParallel:::test_init()
5+
6+
flags <- tbbCxxFlags()
7+
8+
assert(is.character(flags))
9+
assert(length(flags) == 1L)
10+
assert(!is.na(flags))
11+
assert(grepl("-DRCPP_PARALLEL_USE_TBB=1", flags, fixed = TRUE))
12+
13+
hasNoAlignedAllocation <- grepl(
14+
"-fno-aligned-allocation",
15+
flags,
16+
fixed = TRUE
17+
)
18+
assert(identical(hasNoAlignedAllocation, nzchar(TBB_CXXFLAGS)))

tools/config/configure.R

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,46 @@
11

22
# make sure we call correct version of R
33
rExe <- if (.Platform$OS.type == "windows") "R.exe" else "R"
4-
define(R = file.path(R.home("bin"), rExe))
4+
rPath <- file.path(R.home("bin"), rExe)
5+
define(R = rPath)
6+
7+
# Clang enables C++17 aligned allocation based on the deployment target, but
8+
# the corresponding libc++ entry points are unavailable before macOS 10.13.
9+
# Probe through R CMD SHLIB so we see the compiler, standard library, flags,
10+
# launchers, and user Makevars settings that will actually build the package.
11+
tbbNeedsNoAlignedAllocation <- function() {
12+
13+
if (!identical(Sys.info()[["sysname"]], "Darwin"))
14+
return(FALSE)
15+
16+
probeDir <- tempfile("RcppParallel-configure-")
17+
dir.create(probeDir)
18+
on.exit(unlink(probeDir, recursive = TRUE), add = TRUE)
19+
20+
source <- file.path(probeDir, "probe.cpp")
21+
writeLines(c(
22+
"#include <cstddef>",
23+
"#if defined(__clang__) && defined(_LIBCPP_VERSION) && \\",
24+
" defined(__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__) && \\",
25+
" __ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ < 101300",
26+
"#pragma message(\"RCPP_PARALLEL_NO_ALIGNED_ALLOCATION\")",
27+
"#endif",
28+
"int rcppParallelConfigureProbe;"
29+
), source)
30+
31+
output <- tryCatch(
32+
suppressWarnings(system2(
33+
rPath,
34+
c("CMD", "SHLIB", shQuote(source)),
35+
stdout = TRUE,
36+
stderr = TRUE
37+
)),
38+
error = function(cnd) character()
39+
)
40+
41+
any(grepl("RCPP_PARALLEL_NO_ALIGNED_ALLOCATION", output, fixed = TRUE))
42+
43+
}
544

645
# check whether user has Makevars file that might cause trouble
746
makevars <- Sys.getenv("R_MAKEVARS_USER", unset = "~/.R/Makevars")
@@ -237,7 +276,14 @@ if (!is.na(tbbLib)) {
237276
# TBB is always enabled: either one was supplied via TBB_LIB / TBB_ROOT, or we
238277
# built the bundled copy, and failing to do either is fatal above
239278
define(TBB_ENABLED = TRUE)
240-
define(PKG_CXXFLAGS = "-DRCPP_PARALLEL_USE_TBB=1")
279+
tbbCxxFlags <- if (tbbNeedsNoAlignedAllocation()) {
280+
"-fno-aligned-allocation"
281+
} else {
282+
""
283+
}
284+
define(TBB_CXXFLAGS = tbbCxxFlags)
285+
pkgCxxFlags <- c("-DRCPP_PARALLEL_USE_TBB=1", tbbCxxFlags)
286+
define(PKG_CXXFLAGS = paste(pkgCxxFlags[nzchar(pkgCxxFlags)], collapse = " "))
241287

242288
# macOS needs some extra flags set
243289
if (Sys.info()[["sysname"]] == "Darwin") {

0 commit comments

Comments
 (0)