Skip to content

Commit 9ad19e6

Browse files
committed
alternative using R CMD SHLIB
1 parent da6266a commit 9ad19e6

File tree

1 file changed

+18
-11
lines changed

1 file changed

+18
-11
lines changed

R/Attributes.R

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -592,15 +592,30 @@ compileAttributes <- function(pkgdir = ".", verbose = getOption("verbose")) {
592592

593593
.openmpPluginDarwin <- function() {
594594

595+
# generate a test script for compilation
596+
script <- tempfile("openmp-detect-", fileext = ".cpp")
597+
writeLines("", con = script)
598+
on.exit(unlink(script, force = TRUE), add = TRUE)
599+
595600
# get the C++ compiler from R
596601
r <- file.path(R.home("bin"), "R")
597-
cxx <- tryCatch(
598-
system2(r, c("CMD", "config", "CXX"), stdout = TRUE),
602+
output <- tryCatch(
603+
system2(r, c("CMD", "SHLIB", "--dry-run", shQuote(script)), stdout = TRUE),
599604
condition = identity
600605
)
601-
if (inherits(cxx, "condition"))
606+
if (inherits(output, "condition"))
602607
return(.openmpPluginDefault())
603608

609+
# extract the compiler invocation from the shlib output
610+
# use some heuristics here...
611+
index <- grep("make would use", output)
612+
compile <- output[[index + 1L]]
613+
614+
# use everything up to the first include flag, which is normally
615+
# the R headers from CPPFLAGS
616+
idx <- regexpr(" -I", compile, fixed = TRUE)
617+
cxx <- substring(compile, 1L, idx - 1L)
618+
604619
# check the compiler version
605620
command <- paste(cxx, "--version")
606621
version <- tryCatch(
@@ -613,29 +628,21 @@ compileAttributes <- function(pkgdir = ".", verbose = getOption("verbose")) {
613628
# if we're using Apple clang, use alternate flags
614629
# assume libomp was installed following https://mac.r-project.org/openmp/
615630
if (any(grepl("Apple clang", version))) {
616-
617631
cxxflags <- "-Xclang -fopenmp"
618632
libs <- "-lomp"
619-
620633
}
621634

622635
# if we're using Homebrew clang, add in libomp include paths
623636
else if (any(grepl("Homebrew clang", version))) {
624-
625-
# ensure Homebrew paths are included during compilation
626637
machine <- Sys.info()[["machine"]]
627638
prefix <- if (machine == "arm64") "/opt/homebrew" else "/usr/local"
628-
629-
# build flags
630639
cxxflags <- sprintf("-I%s/opt/libomp/include -fopenmp", prefix)
631640
libs <- sprintf("-L%s/opt/libomp/lib -fopenmp", prefix)
632641

633642
# otherwise, use default -fopenmp flags for other compilers (LLVM clang; gcc)
634643
} else {
635-
636644
cxxflags <- "-fopenmp"
637645
libs <- "-fopenmp"
638-
639646
}
640647

641648
list(env = list(PKG_CXXFLAGS = cxxflags, PKG_LIBS = libs))

0 commit comments

Comments
 (0)