|
1 | | -checkForLibAtomic <- function() { |
2 | | - if (.Platform$OS.type == "windows") |
3 | | - return(FALSE) |
| 1 | +getCompiler <- function() { |
| 2 | + tools::Rcmd(c("config", "CXX"), stdout = TRUE) |
| 3 | +} |
| 4 | + |
| 5 | +runCmd <- function(...) { |
| 6 | + args <- list(command = paste(...)) |
| 7 | + args$ignore.stdout = TRUE |
| 8 | + args$ignore.stderr = TRUE |
| 9 | + do.call(system, args) |
| 10 | +} |
| 11 | + |
| 12 | +createTestFiles <- function() { |
| 13 | + src <- tempfile("test", fileext = ".cpp") |
| 14 | + lib <- tempfile("test", fileext = ".out") |
| 15 | + c(src = src, lib = lib) |
| 16 | +} |
4 | 17 |
|
5 | | - fl <- tempfile("test", fileext = ".cpp") |
| 18 | +writeLibAtomicTest <- function(file) { |
6 | 19 | cat( |
7 | 20 | "#include <atomic> |
8 | 21 | std::atomic<int> x; |
9 | 22 | int main() { |
10 | 23 | std::atomic_is_lock_free(&x); |
11 | 24 | return 0; |
12 | 25 | }", |
13 | | - file = fl |
| 26 | + file = file |
14 | 27 | ) |
| 28 | +} |
| 29 | + |
| 30 | +checkForLibAtomic <- function() { |
| 31 | + if (.Platform$OS.type == "windows") |
| 32 | + return(TRUE) |
| 33 | + |
| 34 | + # create temporary source and out files |
| 35 | + tmp <- createTestFiles() |
| 36 | + writeLibAtomicTest(tmp["src"]) |
15 | 37 |
|
16 | | - compiler <- tools::Rcmd(c("config", "CXX"), stdout = TRUE) |
17 | | - failed <- system(paste(compiler, fl, "-latomic"), |
18 | | - ignore.stdout = TRUE, |
19 | | - ignore.stderr = TRUE) |
20 | | - unlink(fl) |
| 38 | + # check whether test program can be compiled |
| 39 | + failed <- runCmd(getCompiler(), tmp["src"], "-o", tmp["lib"], "-latomic") |
21 | 40 |
|
22 | | - return(!failed) |
| 41 | + # clean up temporary files |
| 42 | + unlink(tmp) |
| 43 | + |
| 44 | + !failed |
23 | 45 | } |
24 | 46 |
|
25 | 47 | hasAtomicSupport <- function() { |
26 | 48 | if (checkForLibAtomic()) |
27 | 49 | return(TRUE) |
28 | 50 |
|
29 | | - fl <- tempfile("test", fileext = ".cpp") |
30 | | - cat( |
31 | | - "#include <atomic> |
32 | | - std::atomic<int> x; |
33 | | - int main() { |
34 | | - std::atomic_is_lock_free(&x); |
35 | | - return 0; |
36 | | - }", |
37 | | - file = fl |
38 | | - ) |
39 | | - compiler <- tools::Rcmd(c("config", "CXX"), stdout = TRUE) |
40 | | - failed <- system(paste(compiler, fl), |
41 | | - ignore.stdout = TRUE, |
42 | | - ignore.stderr = TRUE) |
43 | | - unlink(fl) |
| 51 | + # create temporary source and out files |
| 52 | + tmp <- createTestFiles() |
| 53 | + writeLibAtomicTest(tmp["src"]) |
| 54 | + |
| 55 | + # check whether test program can be compiled |
| 56 | + failed <- runCmd(getCompiler(), tmp["src"], "-o", tmp["lib"]) |
44 | 57 |
|
45 | | - return(!failed) |
| 58 | + # clean up temporary files |
| 59 | + unlink(tmp) |
| 60 | + |
| 61 | + !failed |
46 | 62 | } |
47 | 63 |
|
48 | 64 | checkForLibPthread <- function() { |
49 | 65 | if (.Platform$OS.type == "windows") |
50 | 66 | return(FALSE) |
51 | 67 |
|
52 | | - fl <- tempfile("test", fileext = ".cpp") |
53 | | - cat( |
54 | | - "#include <pthread.h> |
55 | | - int main() { |
56 | | - return 0; |
57 | | - }", |
58 | | - file = fl) |
59 | | - |
60 | | - compiler <- system("R CMD config CXX", intern = TRUE) |
61 | | - failed <- system(paste(compiler, fl, "-lpthread"), |
62 | | - ignore.stdout = TRUE, |
63 | | - ignore.stderr = TRUE) |
64 | | - unlink(fl) |
65 | | - return(!failed) |
| 68 | + # create temporary source and out files |
| 69 | + tmp <- createTestFiles() |
| 70 | + cat("#include <pthread.h> \n int main() { return 0; }", file = tmp["src"]) |
| 71 | + |
| 72 | + # check whether test program can be compiled |
| 73 | + failed <- runCmd(getCompiler(), tmp["src"], "-o", tmp["lib"], "-lpthread") |
| 74 | + |
| 75 | + # clean up temporary files |
| 76 | + unlink(tmp) |
| 77 | + |
| 78 | + !failed |
66 | 79 | } |
67 | 80 |
|
68 | 81 |
|
69 | 82 | #' Get portable linker flags for libraries building on RcppThread |
70 | 83 | #' |
71 | | -#' To be used in `Makevars` on Linux and OSX. Returns a character vector with |
| 84 | +#' To be used in `Makevars` on Linux and OSX. Returns a string with |
72 | 85 | #' linker flags for `pthread` and `libatomic`, if available. |
73 | 86 | #' |
74 | 87 | #' Use as |
|
0 commit comments