Skip to content

Commit a5394d9

Browse files
committed
better LdFlags
1 parent ba22b3f commit a5394d9

File tree

4 files changed

+58
-45
lines changed

4 files changed

+58
-45
lines changed

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Package: RcppThread
22
Title: R-Friendly Threading in C++
3-
Version: 2.1.1
3+
Version: 2.1.2
44
Authors@R: c(
55
person("Thomas", "Nagler",, "[email protected]", role = c("aut", "cre"),
66
comment = c(ORCID = "0000-0003-1855-0046"))

NEWS.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# RcppThread 2.1.1
1+
# RcppThread 2.1.2
22

33
NEW FEATURE
44

R/LdFlags.R

Lines changed: 55 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,74 +1,87 @@
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+
}
417

5-
fl <- tempfile("test", fileext = ".cpp")
18+
writeLibAtomicTest <- function(file) {
619
cat(
720
"#include <atomic>
821
std::atomic<int> x;
922
int main() {
1023
std::atomic_is_lock_free(&x);
1124
return 0;
1225
}",
13-
file = fl
26+
file = file
1427
)
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"])
1537

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")
2140

22-
return(!failed)
41+
# clean up temporary files
42+
unlink(tmp)
43+
44+
!failed
2345
}
2446

2547
hasAtomicSupport <- function() {
2648
if (checkForLibAtomic())
2749
return(TRUE)
2850

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"])
4457

45-
return(!failed)
58+
# clean up temporary files
59+
unlink(tmp)
60+
61+
!failed
4662
}
4763

4864
checkForLibPthread <- function() {
4965
if (.Platform$OS.type == "windows")
5066
return(FALSE)
5167

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
6679
}
6780

6881

6982
#' Get portable linker flags for libraries building on RcppThread
7083
#'
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
7285
#' linker flags for `pthread` and `libatomic`, if available.
7386
#'
7487
#' Use as

man/LdFlags.Rd

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)