Skip to content

Commit da6266a

Browse files
committed
alternate openmp plugin changes
1 parent 274f5ab commit da6266a

File tree

2 files changed

+64
-3
lines changed

2 files changed

+64
-3
lines changed

ChangeLog

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
2025-12-01 Kevin Ushey <kevinushey@gmail.com>
2+
3+
* R/Attributes.R: Update OpenMP plugin for macOS
4+
15
2025-11-24 Dirk Eddelbuettel <edd@debian.org>
26

37
* inst/include/Rcpp/r/check_r_headers.h: Add RCPP_NO_R_HEADERS_CHECK

R/Attributes.R

Lines changed: 60 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -586,10 +586,67 @@ compileAttributes <- function(pkgdir = ".", verbose = getOption("verbose")) {
586586
list(env = list(PKG_CXXFLAGS ="-std=c++2b"))
587587
}
588588

589+
.openmpPluginDefault <- function() {
590+
list(env = list(PKG_CXXFLAGS = "-fopenmp", PKG_LIBS = "-fopenmp"))
591+
}
592+
593+
.openmpPluginDarwin <- function() {
594+
595+
# get the C++ compiler from R
596+
r <- file.path(R.home("bin"), "R")
597+
cxx <- tryCatch(
598+
system2(r, c("CMD", "config", "CXX"), stdout = TRUE),
599+
condition = identity
600+
)
601+
if (inherits(cxx, "condition"))
602+
return(.openmpPluginDefault())
603+
604+
# check the compiler version
605+
command <- paste(cxx, "--version")
606+
version <- tryCatch(
607+
system(command, intern = TRUE),
608+
condition = identity
609+
)
610+
if (inherits(version, "condition"))
611+
return(.openmpPluginDefault())
612+
613+
# if we're using Apple clang, use alternate flags
614+
# assume libomp was installed following https://mac.r-project.org/openmp/
615+
if (any(grepl("Apple clang", version))) {
616+
617+
cxxflags <- "-Xclang -fopenmp"
618+
libs <- "-lomp"
619+
620+
}
621+
622+
# if we're using Homebrew clang, add in libomp include paths
623+
else if (any(grepl("Homebrew clang", version))) {
624+
625+
# ensure Homebrew paths are included during compilation
626+
machine <- Sys.info()[["machine"]]
627+
prefix <- if (machine == "arm64") "/opt/homebrew" else "/usr/local"
628+
629+
# build flags
630+
cxxflags <- sprintf("-I%s/opt/libomp/include -fopenmp", prefix)
631+
libs <- sprintf("-L%s/opt/libomp/lib -fopenmp", prefix)
632+
633+
# otherwise, use default -fopenmp flags for other compilers (LLVM clang; gcc)
634+
} else {
635+
636+
cxxflags <- "-fopenmp"
637+
libs <- "-fopenmp"
638+
639+
}
640+
641+
list(env = list(PKG_CXXFLAGS = cxxflags, PKG_LIBS = libs))
642+
643+
}
644+
589645
## built-in OpenMP plugin
590-
.plugins[["openmp"]] <- function() {
591-
list(env = list(PKG_CXXFLAGS="-fopenmp",
592-
PKG_LIBS="-fopenmp"))
646+
.plugins[["openmp"]] <- if (Sys.info()[["sysname"]] == "Darwin") {
647+
.openmpPluginDarwin
648+
} else {
649+
.openmpPluginDefault
593650
}
594651

595652
.plugins[["unwindProtect"]] <- function() { # nocov start

0 commit comments

Comments
 (0)