Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
RcppParallel 6.0.0, such packages would fail to load with "LoadLibrary
failure: The specified module could not be found".

* The bundled oneTBB headers now guard against GCC's `<cpuid.h>` being
* The TBB headers installed with RcppParallel (whether from Rtools or from
the bundled copy of oneTBB) now guard against GCC's `<cpuid.h>` being
included before `<intrin.h>` on Windows (mingw). Previously, translation
units including `<cpuid.h>` before any TBB header would fail to compile,
as the `__cpuid` macro from `<cpuid.h>` conflicts with the `__cpuid()`
Expand Down
40 changes: 40 additions & 0 deletions src/install.libs.R
Original file line number Diff line number Diff line change
Expand Up @@ -88,13 +88,53 @@
}

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)
}
}

patchTbbMachineHeader("../inst/include/oneapi/tbb/detail/_machine.h")

}

# Guard the '#include <intrin.h>' in TBB's _machine.h against GCC's
# <cpuid.h> macro on mingw. The headers we ship may come from Rtools
# rather than from the bundled copy of oneTBB, so the guard must be
# applied to the copied headers, not just the bundled sources.
patchTbbMachineHeader <- function(path) {

if (!file.exists(path))
return()

contents <- readLines(path)
if (any(grepl("push_macro", contents, fixed = TRUE)))
return()

index <- which(contents == "#include <intrin.h>")
if (length(index) != 1L)
return()

replacement <- c(
"// GCC's <cpuid.h> defines a function-like '__cpuid' macro that mangles the",
"// __cpuid() declarations in mingw's <intrin.h>. Hide the macro while",
"// including <intrin.h> so the two headers can coexist in any order.",
"#if defined(__MINGW32__) && defined(__cpuid)",
"#pragma push_macro(\"__cpuid\")",
"#undef __cpuid",
"#include <intrin.h>",
"#pragma pop_macro(\"__cpuid\")",
"#else",
"#include <intrin.h>",
"#endif"
)

contents <- append(contents[-index], replacement, after = index - 1L)
writeLines(contents, path)

}

useSystemTbb <- function(tbbLib, tbbInc) {
Expand Down
Loading