|
| 1 | + |
| 2 | +# !diagnostics suppress=R_PACKAGE_DIR,SHLIB_EXT,R_ARCH |
| 3 | +.install.libs <- function(tbbLib) { |
| 4 | + |
| 5 | + # copy default library |
| 6 | + files <- Sys.glob(paste0("*", SHLIB_EXT)) |
| 7 | + dest <- file.path(R_PACKAGE_DIR, paste0("libs", R_ARCH)) |
| 8 | + dir.create(dest, recursive = TRUE, showWarnings = FALSE) |
| 9 | + file.copy(files, dest, overwrite = TRUE) |
| 10 | + |
| 11 | + # copy symbols if available |
| 12 | + if (file.exists("symbols.rds")) |
| 13 | + file.copy("symbols.rds", dest, overwrite = TRUE) |
| 14 | + |
| 15 | + # also copy to package 'libs' folder, for devtools |
| 16 | + libsDest <- paste0("../libs", R_ARCH) |
| 17 | + dir.create(libsDest, recursive = TRUE, showWarnings = FALSE) |
| 18 | + file.copy(files, libsDest, overwrite = TRUE) |
| 19 | + |
| 20 | + # copy tbb (NOTE: do not use inst/ folder as R will resolve symlinks, |
| 21 | + # behavior which we do _not_ want here!) |
| 22 | + tbbDest <- file.path(R_PACKAGE_DIR, paste0("lib", R_ARCH)) |
| 23 | + dir.create(tbbDest, recursive = TRUE, showWarnings = FALSE) |
| 24 | + |
| 25 | + # note: on Linux, TBB gets compiled with extensions like |
| 26 | + # '.so.2', so be ready to handle those |
| 27 | + shlibPattern <- switch( |
| 28 | + Sys.info()[["sysname"]], |
| 29 | + Windows = "^tbb.*\\.dll$", |
| 30 | + Darwin = "^libtbb.*\\.dylib$", |
| 31 | + "^libtbb.*\\.so.*$" |
| 32 | + ) |
| 33 | + |
| 34 | + if (!nzchar(tbbLib)) { |
| 35 | + |
| 36 | + # using bundled TBB |
| 37 | + tbbLibs <- list.files( |
| 38 | + path = "tbb/build/lib_release", |
| 39 | + pattern = shlibPattern, |
| 40 | + full.names = TRUE |
| 41 | + ) |
| 42 | + |
| 43 | + for (tbbLib in tbbLibs) { |
| 44 | + system2("cp", c("-P", shQuote(tbbLib), shQuote(tbbDest))) |
| 45 | + } |
| 46 | + |
| 47 | + } else { |
| 48 | + |
| 49 | + # using system tbb |
| 50 | + tbbLibs <- list.files( |
| 51 | + path = tbbLib, |
| 52 | + pattern = shlibPattern, |
| 53 | + full.names = TRUE |
| 54 | + ) |
| 55 | + |
| 56 | + # don't copy symlinks |
| 57 | + tbbLibs <- tbbLibs[!nzchar(Sys.readlink(tbbLibs))] |
| 58 | + |
| 59 | + # copy / link the libraries |
| 60 | + useSymlinks <- Sys.getenv("TBB_USE_SYMLINKS", unset = "TRUE") |
| 61 | + if (useSymlinks) { |
| 62 | + file.symlink(tbbLibs, tbbDest) |
| 63 | + } else { |
| 64 | + for (tbbLib in tbbLibs) { |
| 65 | + system2("cp", c("-P", shQuote(tbbLib), shQuote(tbbDest))) |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + } |
| 70 | + |
| 71 | +} |
| 72 | + |
| 73 | +useTbbPreamble <- function(tbbInc) { |
| 74 | + dir.create("../inst/include", recursive = TRUE, showWarnings = FALSE) |
| 75 | + for (suffix in c("oneapi", "serial", "tbb")) { |
| 76 | + tbbPath <- file.path(tbbInc, suffix) |
| 77 | + if (file.exists(tbbPath)) { |
| 78 | + file.copy(tbbPath, "../inst/include", recursive = TRUE) |
| 79 | + } |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +useSystemTbb <- function(tbbLib, tbbInc) { |
| 84 | + useTbbPreamble(tbbInc) |
| 85 | +} |
| 86 | + |
| 87 | +useBundledTbb <- function() { |
| 88 | + useTbbPreamble("tbb/include") |
| 89 | + dir.create("tbb/build", showWarnings = FALSE) |
| 90 | + system("cd tbb/build; cmake -DTBB_TEST=0 -DTBB_EXAMPLES=0 .. && cmake --build .") |
| 91 | + system("cd tbb/build; mv *_relwithdebinfo lib_release") |
| 92 | +} |
| 93 | + |
| 94 | + |
| 95 | +# Main ---- |
| 96 | + |
| 97 | +tbbLib <- Sys.getenv("TBB_LIB") |
| 98 | +tbbInc <- Sys.getenv("TBB_INC") |
| 99 | + |
| 100 | +args <- commandArgs(trailingOnly = TRUE) |
| 101 | +if (identical(args, "build")) { |
| 102 | + if (nzchar(tbbLib) && nzchar(tbbInc)) { |
| 103 | + useSystemTbb(tbbLib, tbbInc) |
| 104 | + } else { |
| 105 | + useBundledTbb() |
| 106 | + } |
| 107 | +} else { |
| 108 | + source("../R/tbb-autodetected.R") |
| 109 | + .install.libs(tbbLib) |
| 110 | +} |
0 commit comments