@@ -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