Skip to content

Commit c931420

Browse files
committed
Update OpenMP plugin scaffolding for macOS based on feedback
1 parent d0a5f76 commit c931420

File tree

1 file changed

+25
-12
lines changed

1 file changed

+25
-12
lines changed

R/Attributes.R

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -594,28 +594,41 @@ compileAttributes <- function(pkgdir = ".", verbose = getOption("verbose")) {
594594
libs <- "-fopenmp"
595595

596596
# Check if we're on macOS
597-
if (Sys.info()[['sysname']] == "Darwin") {
597+
if (Sys.info()[["sysname"]] == "Darwin") {
598598
# Get the C++ compiler from R CMD config CXX
599-
r_cmd <- file.path(R.home('bin'), 'R')
599+
r_cmd <- file.path(R.home("bin"), "R")
600600
cxx <- tryCatch({
601601
system2(r_cmd, c("CMD", "config", "CXX"), stdout = TRUE, stderr = FALSE)
602602
}, error = function(e) "")
603603

604604
if (length(cxx) > 0 && nzchar(cxx[1])) {
605-
# Extract just the compiler command (remove any flags)
606-
cxx_compiler <- strsplit(cxx[1], "\\s+")[[1]][1]
607-
608-
# Check if it's Apple clang by querying the compiler version
605+
# Check compiler version using full command (this includes any flags)
609606
compiler_version <- tryCatch({
610-
system2(cxx_compiler, "--version", stdout = TRUE, stderr = FALSE)
607+
system(paste(cxx[1], "--version"), intern = TRUE, ignore.stderr = TRUE)
611608
}, error = function(e) character(0))
612609

613-
# If we detect Apple clang, use -Xclang -fopenmp
614-
if (length(compiler_version) > 0 && any(grepl("Apple", compiler_version, ignore.case = TRUE))) {
615-
cxxflags <- "-Xclang -fopenmp"
616-
libs <- "-lomp"
610+
if (length(compiler_version) > 0) {
611+
# Check for Apple clang
612+
is_apple_clang <- any(grepl("Apple", compiler_version, ignore.case = TRUE))
613+
# Check for Homebrew clang
614+
is_homebrew_clang <- any(grepl("Homebrew", compiler_version, ignore.case = TRUE))
615+
616+
if (is_apple_clang) {
617+
# Apple clang requires -Xclang -fopenmp
618+
cxxflags <- "-Xclang -fopenmp"
619+
libs <- "-lomp"
620+
} else if (is_homebrew_clang) {
621+
# Homebrew clang needs include/lib paths for the added libomp formula
622+
# Determine prefix based on architecture (arm64 vs x86_64)
623+
arch <- Sys.info()[["machine"]]
624+
brew_prefix <- if (arch == "arm64") "/opt/homebrew" else "/usr/local"
625+
626+
cxxflags <- paste("-Xclang -fopenmp",
627+
paste0("-I", brew_prefix, "/opt/libomp/include"))
628+
libs <- paste(paste0("-L", brew_prefix, "/opt/libomp/lib"), "-lomp")
629+
}
630+
# Otherwise it's gcc and regular -fopenmp works (defaults are fine)
617631
}
618-
# Otherwise it's gcc and regular -fopenmp works
619632
}
620633
}
621634

0 commit comments

Comments
 (0)