Skip to content

Commit 83bd397

Browse files
committed
Fix old macOS Clang allocation flags
1 parent 711e25d commit 83bd397

7 files changed

Lines changed: 73 additions & 3 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 on macOS 10.12 and
4+
earlier with Clang and libc++, which require `-fno-aligned-allocation`.
5+
The flag is restricted to that compiler and platform combination. (#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-platform.R

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
tbb_cxx_compiler <- function() {
2+
3+
compiler <- Sys.getenv("CXX", unset = NA_character_)
4+
if (!is.na(compiler) && nzchar(compiler))
5+
return(compiler)
6+
7+
R <- file.path(R.home("bin"), "R")
8+
output <- tryCatch(
9+
system2(R, c("CMD", "config", "CXX"), stdout = TRUE, stderr = FALSE),
10+
error = function(cnd) character()
11+
)
12+
13+
if (length(output)) output[[1L]] else ""
14+
15+
}
16+
17+
tbb_needs_no_aligned_allocation <- function(
18+
sysname = Sys.info()[["sysname"]],
19+
release = Sys.info()[["release"]],
20+
compiler = tbb_cxx_compiler())
21+
{
22+
23+
if (!identical(sysname, "Darwin"))
24+
return(FALSE)
25+
26+
# Darwin 16 is macOS 10.12; Darwin 17 is macOS 10.13.
27+
darwinMajor <- suppressWarnings(as.integer(strsplit(release, ".", fixed = TRUE)[[1L]][[1L]]))
28+
if (is.na(darwinMajor) || darwinMajor >= 17L)
29+
return(FALSE)
30+
31+
compiler <- strsplit(compiler, "[[:space:]]+", perl = TRUE)[[1L]][[1L]]
32+
identical(basename(compiler), "clang++")
33+
34+
}

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: 2 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,7 @@ 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)" LDFLAGS="$(LDFLAGS)" \
3233
CMAKE="$(CMAKE)" "@R@" -s -f install.libs.R --args build
3334

3435
# 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 and platform-specific flags returned by
2+
# RcppParallel::CxxFlags().
3+
4+
RcppParallel:::test_init()
5+
6+
# Darwin 16 is macOS 10.12, the last release affected by this libc++ issue.
7+
assert(tbb_needs_no_aligned_allocation("Darwin", "16.7.0", "clang++"))
8+
assert(!tbb_needs_no_aligned_allocation("Darwin", "17.0.0", "clang++"))
9+
assert(!tbb_needs_no_aligned_allocation("Darwin", "16.7.0", "g++"))
10+
assert(!tbb_needs_no_aligned_allocation("Linux", "16.7.0", "clang++"))
11+
12+
flags <- tbbCxxFlags()
13+
assert(is.character(flags))
14+
assert(length(flags) == 1L)
15+
assert(!is.na(flags))
16+
17+
if (nzchar(TBB_CXXFLAGS))
18+
assert(grepl("-fno-aligned-allocation", flags, fixed = TRUE))

tools/config/configure.R

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
rExe <- if (.Platform$OS.type == "windows") "R.exe" else "R"
44
define(R = file.path(R.home("bin"), rExe))
55

6+
source("R/tbb-platform.R")
7+
68
# check whether user has Makevars file that might cause trouble
79
makevars <- Sys.getenv("R_MAKEVARS_USER", unset = "~/.R/Makevars")
810
if (file.exists(makevars)) {
@@ -237,7 +239,14 @@ if (!is.na(tbbLib)) {
237239
# TBB is always enabled: either one was supplied via TBB_LIB / TBB_ROOT, or we
238240
# built the bundled copy, and failing to do either is fatal above
239241
define(TBB_ENABLED = TRUE)
240-
define(PKG_CXXFLAGS = "-DRCPP_PARALLEL_USE_TBB=1")
242+
tbbCxxFlags <- if (tbb_needs_no_aligned_allocation()) {
243+
"-fno-aligned-allocation"
244+
} else {
245+
""
246+
}
247+
define(TBB_CXXFLAGS = tbbCxxFlags)
248+
pkgCxxFlags <- c("-DRCPP_PARALLEL_USE_TBB=1", tbbCxxFlags)
249+
define(PKG_CXXFLAGS = paste(pkgCxxFlags[nzchar(pkgCxxFlags)], collapse = " "))
241250

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

0 commit comments

Comments
 (0)