-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathcompiler.R
More file actions
161 lines (145 loc) · 3.98 KB
/
Copy pathcompiler.R
File metadata and controls
161 lines (145 loc) · 3.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
quickr_flang_path <- function(which = Sys.which) {
flang_new <- which("flang-new")
if (nzchar(flang_new)) {
return(flang_new)
}
flang <- which("flang")
if (nzchar(flang)) {
return(flang)
}
""
}
quickr_flang_runtime_flags <- local({
cache <- NULL
function(flang, sysname = Sys.info()[["sysname"]]) {
stopifnot(is_string(flang))
if (!nzchar(flang) || sysname != "Darwin") {
return(character())
}
if (!file.exists(flang)) {
return(character())
}
if (!is.null(cache)) {
return(cache)
}
resolved <- normalizePath(flang, winslash = "/", mustWork = FALSE)
prefix <- dirname(dirname(resolved))
libdirs <- unique(
c(
file.path(prefix, "lib"),
file.path(prefix, "lib64"),
Sys.glob(file.path(prefix, "lib", "clang", "*", "lib", "darwin")),
Sys.glob(file.path(prefix, "lib64", "clang", "*", "lib", "darwin"))
)
)
runtime <- "libflang_rt.runtime.dylib"
libdir <- keep(libdirs, function(d) file.exists(file.path(d, runtime)))
if (!length(libdir)) {
cache <<- character()
return(cache)
}
cache <<- c(sprintf("-L%s", libdir[[1L]]), "-lflang_rt.runtime")
cache
}
})
quickr_env_is_true <- function(name) {
val <- Sys.getenv(name, unset = "")
if (!nzchar(val)) {
return(FALSE)
}
tolower(val) %in% c("1", "true", "t", "yes", "y", "on")
}
quickr_prefer_flang <- function(
sysname = Sys.info()[["sysname"]],
which = Sys.which
) {
opt <- getOption("quickr.prefer_flang")
if (isFALSE(opt)) {
return(FALSE)
}
if (quickr_env_is_true("QUICKR_PREFER_FLANG")) {
return(TRUE)
}
if (isTRUE(getOption("quickr.prefer_flang_force"))) {
return(TRUE)
}
if (interactive() && isTRUE(opt)) {
return(TRUE)
}
# Best-effort: on macOS, prefer flang if it is available.
if (
isTRUE(getOption("quickr.prefer_flang_auto", TRUE)) && sysname == "Darwin"
) {
return(nzchar(quickr_flang_path(which = which)))
}
FALSE
}
quickr_fcompiler_env <- function(
build_dir,
which = Sys.which,
prefer_flang = quickr_prefer_flang(which = which),
prefer_flang_force = isTRUE(getOption("quickr.prefer_flang_force")) ||
quickr_env_is_true("QUICKR_PREFER_FLANG"),
write_lines = writeLines,
sysname = Sys.info()[["sysname"]],
use_openmp = FALSE
) {
stopifnot(is.character(build_dir), length(build_dir) == 1L, nzchar(build_dir))
use_openmp <- isTRUE(use_openmp)
flang <- ""
flang_runtime <- character()
use_flang <- isTRUE(prefer_flang)
if (use_flang) {
flang <- quickr_flang_path(which = which)
if (!nzchar(flang)) {
use_flang <- FALSE
}
}
if (use_openmp && use_flang && !isTRUE(prefer_flang_force)) {
use_flang <- FALSE
flang <- ""
}
if (use_flang) {
flang_runtime <- if (sysname == "Darwin") {
quickr_flang_runtime_flags(flang = flang, sysname = sysname)
} else {
character()
}
if (sysname == "Darwin" && !length(flang_runtime)) {
if (isTRUE(prefer_flang_force)) {
stop(
"quickr was configured to use flang (",
flang,
") but could not locate the flang runtime library (libflang_rt.runtime.dylib) to link against.\n",
"Either reinstall flang so the runtime is available, or disable flang selection with:\n",
" options(quickr.prefer_flang = FALSE)\n",
"or:\n",
" Sys.setenv(QUICKR_PREFER_FLANG = 0)\n"
)
}
use_flang <- FALSE
flang_runtime <- character()
}
}
if (!use_flang && !use_openmp) {
return(character())
}
makevars_path <- file.path(build_dir, "Makevars.quickr")
makevars_lines <- c(
if (use_flang) {
c(
sprintf("FC=%s", flang),
sprintf("F77=%s", flang),
if (length(flang_runtime)) {
paste(c("FLIBS +=", flang_runtime), collapse = " ")
}
)
},
if (use_openmp) openmp_makevars_lines()
)
write_lines(
makevars_lines,
makevars_path
)
sprintf("R_MAKEVARS_USER=%s", makevars_path)
}