Skip to content

Commit 12ca8ff

Browse files
committed
Simplify flang preference helper signature
1 parent 869c683 commit 12ca8ff

3 files changed

Lines changed: 69 additions & 92 deletions

File tree

R/compiler.R

Lines changed: 10 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,17 @@
1-
quickr_flang_path <- function(which = Sys.which) {
2-
flang_new <- which("flang-new")
1+
quickr_flang_path <- function() {
2+
flang_new <- Sys.which("flang-new")
33
if (nzchar(flang_new)) {
44
return(flang_new)
55
}
6-
flang <- which("flang")
6+
flang <- Sys.which("flang")
77
if (nzchar(flang)) {
88
return(flang)
99
}
1010
""
1111
}
1212

13-
quickr_flang_available <- function(
14-
which = Sys.which,
15-
system2 = base::system2
16-
) {
17-
flang <- quickr_flang_path(which = which)
13+
quickr_flang_available <- function() {
14+
flang <- quickr_flang_path()
1815
if (!nzchar(flang)) {
1916
return(list(path = "", available = FALSE))
2017
}
@@ -146,11 +143,7 @@ quickr_fortran_compiler_option <- function(
146143
)
147144
}
148145

149-
quickr_prefer_flang <- function(
150-
sysname = Sys.info()[["sysname"]],
151-
which = Sys.which,
152-
system2 = base::system2
153-
) {
146+
quickr_prefer_flang <- function() {
154147
compiler_opt <- quickr_fortran_compiler_option()
155148
if (identical(compiler_opt, "flang")) {
156149
return(TRUE)
@@ -163,8 +156,8 @@ quickr_prefer_flang <- function(
163156
}
164157

165158
# Best-effort: on macOS, prefer flang if it is available.
166-
if (sysname == "Darwin") {
167-
info <- quickr_flang_available(which = which, system2 = system2)
159+
if (Sys.info()[["sysname"]] == "Darwin") {
160+
info <- quickr_flang_available()
168161
return(isTRUE(info$available))
169162
}
170163

@@ -194,8 +187,6 @@ quickr_default_fortran_makevars_lines <- function(
194187

195188
quickr_fcompiler_env <- function(
196189
build_dir,
197-
which = Sys.which,
198-
system2 = base::system2,
199190
write_lines = writeLines,
200191
sysname = Sys.info()[["sysname"]],
201192
use_openmp = FALSE,
@@ -211,13 +202,9 @@ quickr_fcompiler_env <- function(
211202

212203
flang <- ""
213204
flang_runtime <- character()
214-
use_flang <- isTRUE(quickr_prefer_flang(
215-
sysname = sysname,
216-
which = which,
217-
system2 = system2
218-
))
205+
use_flang <- quickr_prefer_flang()
219206
if (use_flang) {
220-
flang_info <- quickr_flang_available(which = which, system2 = system2)
207+
flang_info <- quickr_flang_available()
221208
flang <- flang_info$path
222209
if (!isTRUE(flang_info$available)) {
223210
if (isTRUE(explicit_request)) {

tests/testthat/test-compiler.R

Lines changed: 42 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ test_that("quickr_r_cmd_config_value returns empty on command failure", {
4949
)
5050
})
5151

52-
test_that("quickr_flang_path and quickr_prefer_flang are deterministic with stubs", {
53-
which <- function(x) {
52+
test_that("quickr_flang_path prefers flang-new", {
53+
which_stub <- function(x) {
5454
if (x == "flang-new") {
5555
"/tmp/flang-new"
5656
} else if (x == "flang") {
@@ -59,28 +59,28 @@ test_that("quickr_flang_path and quickr_prefer_flang are deterministic with stub
5959
""
6060
}
6161
}
62-
system2_stub <- function(command, args, stdout = TRUE, stderr = TRUE, ...) {
63-
"flang version"
64-
}
6562

66-
expect_identical(quickr:::quickr_flang_path(which = which), "/tmp/flang-new")
63+
local_mocked_bindings(Sys.which = which_stub, .package = "base")
6764

68-
withr::local_options(quickr.fortran_compiler = "auto")
65+
expect_identical(quickr_flang_path(), "/tmp/flang-new")
66+
})
6967

70-
expect_true(quickr:::quickr_prefer_flang(
71-
sysname = "Darwin",
72-
which = which,
73-
system2 = system2_stub
74-
))
75-
expect_false(quickr:::quickr_prefer_flang(sysname = "Linux", which = which))
68+
test_that("flang probe helpers do not expose test-only process hooks", {
69+
expect_false("which" %in% names(formals(quickr_flang_path)))
70+
expect_false("which" %in% names(formals(quickr_flang_available)))
71+
expect_false("system2" %in% names(formals(quickr_flang_available)))
72+
expect_false("which" %in% names(formals(quickr_prefer_flang)))
73+
expect_false("system2" %in% names(formals(quickr_prefer_flang)))
74+
expect_false("which" %in% names(formals(quickr:::quickr_fcompiler_env)))
75+
expect_false("system2" %in% names(formals(quickr:::quickr_fcompiler_env)))
7676
})
7777

7878
test_that("quickr_prefer_flang respects quickr.fortran_compiler", {
7979
withr::local_options(quickr.fortran_compiler = "flang")
80-
expect_true(quickr:::quickr_prefer_flang(sysname = "Linux"))
80+
expect_true(quickr_prefer_flang())
8181

8282
withr::local_options(quickr.fortran_compiler = "gfortran")
83-
expect_false(quickr:::quickr_prefer_flang(sysname = "Darwin"))
83+
expect_false(quickr_prefer_flang())
8484
})
8585

8686
test_that("quickr_default_fortran_makevars_lines relaxes gfortran cost model", {
@@ -140,8 +140,6 @@ test_that("quickr_fcompiler_env writes Makevars when flang is usable", {
140140
file.create(flang)
141141
file.create(file.path(prefix, "lib", "libflang_rt.runtime.dylib"))
142142

143-
which <- function(x) if (x == "flang-new") flang else ""
144-
145143
cache_env <- environment(quickr:::quickr_flang_runtime_flags)
146144
old_cache <- cache_env$cache
147145
cache_env$cache <- NULL
@@ -150,11 +148,15 @@ test_that("quickr_fcompiler_env writes Makevars when flang is usable", {
150148
build_dir <- file.path(temp, "build")
151149
dir.create(build_dir)
152150

151+
local_mocked_bindings(
152+
Sys.which = function(x) if (x == "flang-new") flang else "",
153+
system2 = function(...) "",
154+
.package = "base"
155+
)
156+
153157
withr::local_options(quickr.fortran_compiler = "flang")
154158
env <- quickr:::quickr_fcompiler_env(
155159
build_dir = build_dir,
156-
which = which,
157-
system2 = function(...) "",
158160
sysname = "Darwin"
159161
)
160162
expect_true(startsWith(env, "R_MAKEVARS_USER="))
@@ -167,8 +169,6 @@ test_that("quickr_fcompiler_env writes Makevars for default gfortran flags", {
167169
withr::local_options(quickr.fortran_compiler = "gfortran")
168170
env <- quickr:::quickr_fcompiler_env(
169171
build_dir = build_dir,
170-
which = function(cmd) "",
171-
system2 = function(...) "",
172172
sysname = "Linux",
173173
config_value = function(name) {
174174
if (identical(name, "FC")) "gfortran -m64" else ""
@@ -186,12 +186,12 @@ test_that("quickr_fcompiler_env writes Makevars for default gfortran flags", {
186186
test_that("quickr_fcompiler_env errors when flang is explicitly requested but unavailable", {
187187
build_dir <- withr::local_tempdir()
188188

189+
local_mocked_bindings(Sys.which = function(cmd) "", .package = "base")
190+
189191
withr::local_options(quickr.fortran_compiler = "flang")
190192
expect_error(
191193
quickr:::quickr_fcompiler_env(
192-
build_dir = build_dir,
193-
which = function(cmd) "",
194-
system2 = function(...) structure("", status = 1L)
194+
build_dir = build_dir
195195
),
196196
"configured to use flang",
197197
fixed = TRUE
@@ -202,10 +202,12 @@ test_that("quickr_flang_available returns unavailable when system2 fails", {
202202
which_stub <- function(x) if (x == "flang-new") "/tmp/flang-new" else ""
203203
system2_fail <- function(...) structure("error", status = 1L)
204204

205-
result <- quickr:::quickr_flang_available(
206-
which = which_stub,
207-
system2 = system2_fail
205+
local_mocked_bindings(
206+
Sys.which = which_stub,
207+
system2 = system2_fail,
208+
.package = "base"
208209
)
210+
result <- quickr:::quickr_flang_available()
209211
expect_identical(result$path, "/tmp/flang-new")
210212
expect_false(result$available)
211213
})
@@ -317,7 +319,7 @@ test_that("quickr_prefer_flang returns FALSE when flang_auto_disabled", {
317319
.package = "quickr"
318320
)
319321

320-
expect_false(quickr:::quickr_prefer_flang(sysname = "Darwin"))
322+
expect_false(quickr_prefer_flang())
321323
})
322324

323325
test_that("quickr_fcompiler_env handles flang unavailable for non-explicit request", {
@@ -337,8 +339,6 @@ test_that("quickr_fcompiler_env handles flang unavailable for non-explicit reque
337339
# Should return character() since not explicit and flang unavailable
338340
result <- quickr:::quickr_fcompiler_env(
339341
build_dir = build_dir,
340-
which = function(x) "",
341-
system2 = function(...) "",
342342
sysname = "Darwin",
343343
config_value = function(name) if (identical(name, "FC")) "clang" else ""
344344
)
@@ -361,12 +361,15 @@ test_that("quickr_fcompiler_env errors when flang runtime not found on Darwin ex
361361
dir.create(build_dir)
362362

363363
withr::local_options(quickr.fortran_compiler = "flang")
364+
local_mocked_bindings(
365+
Sys.which = function(x) if (x == "flang-new") flang else "",
366+
system2 = function(...) "",
367+
.package = "base"
368+
)
364369

365370
expect_error(
366371
quickr:::quickr_fcompiler_env(
367372
build_dir = build_dir,
368-
which = function(x) if (x == "flang-new") flang else "",
369-
system2 = function(...) "",
370373
sysname = "Darwin"
371374
),
372375
"could not locate the flang runtime library"
@@ -390,11 +393,16 @@ test_that("quickr_fcompiler_env falls back when flang runtime not found non-expl
390393
dir.create(build_dir)
391394

392395
withr::local_options(quickr.fortran_compiler = "auto")
396+
local_mocked_bindings(
397+
quickr_prefer_flang = function(...) TRUE,
398+
quickr_flang_available = function(...) {
399+
list(path = flang, available = TRUE)
400+
},
401+
.package = "quickr"
402+
)
393403

394404
result <- quickr:::quickr_fcompiler_env(
395405
build_dir = build_dir,
396-
which = function(x) if (x == "flang-new") flang else "",
397-
system2 = function(...) "",
398406
sysname = "Darwin",
399407
config_value = function(name) if (identical(name, "FC")) "clang" else ""
400408
)
Lines changed: 17 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,24 @@
11
skip_on_cran()
22

33
test_that("quickr_fcompiler_env prefers flang-new when requested", {
4-
which <- function(cmd) {
4+
which_stub <- function(cmd) {
55
if (identical(cmd, "flang-new")) {
66
return("/opt/bin/flang-new")
77
}
88
""
99
}
10+
local_mocked_bindings(
11+
Sys.which = which_stub,
12+
system2 = function(...) "",
13+
.package = "base"
14+
)
1015

1116
withr::local_options(quickr.fortran_compiler = "flang")
1217

1318
build_dir <- tempfile("quickr-build-")
1419
dir.create(build_dir)
1520
env <- quickr:::quickr_fcompiler_env(
1621
build_dir,
17-
system2 = function(...) "",
18-
which = which,
1922
sysname = "Linux"
2023
)
2124

@@ -29,21 +32,24 @@ test_that("quickr_fcompiler_env prefers flang-new when requested", {
2932
})
3033

3134
test_that("quickr_fcompiler_env falls back to flang when flang-new missing", {
32-
which <- function(cmd) {
35+
which_stub <- function(cmd) {
3336
if (identical(cmd, "flang")) {
3437
return("/opt/bin/flang")
3538
}
3639
""
3740
}
41+
local_mocked_bindings(
42+
Sys.which = which_stub,
43+
system2 = function(...) "",
44+
.package = "base"
45+
)
3846

3947
withr::local_options(quickr.fortran_compiler = "flang")
4048

4149
build_dir <- tempfile("quickr-build-")
4250
dir.create(build_dir)
4351
env <- quickr:::quickr_fcompiler_env(
4452
build_dir,
45-
system2 = function(...) "",
46-
which = which,
4753
sysname = "Linux"
4854
)
4955

@@ -57,15 +63,17 @@ test_that("quickr_fcompiler_env falls back to flang when flang-new missing", {
5763
})
5864

5965
test_that("quickr_fcompiler_env returns empty when disabled or unavailable", {
60-
which <- function(cmd) ""
6166
build_dir <- tempfile("quickr-build-")
6267
dir.create(build_dir)
68+
local_mocked_bindings(
69+
quickr_prefer_flang = function(...) FALSE,
70+
.package = "quickr"
71+
)
6372

6473
withr::local_options(quickr.fortran_compiler = "gfortran")
6574
expect_equal(
6675
quickr:::quickr_fcompiler_env(
6776
build_dir,
68-
which = which,
6977
config_value = function(name) if (identical(name, "FC")) "clang" else ""
7078
),
7179
character()
@@ -75,40 +83,14 @@ test_that("quickr_fcompiler_env returns empty when disabled or unavailable", {
7583
expect_equal(
7684
quickr:::quickr_fcompiler_env(
7785
build_dir,
78-
which = which,
7986
config_value = function(name) if (identical(name, "FC")) "clang" else ""
8087
),
8188
character()
8289
)
8390
})
8491

85-
test_that("quickr_prefer_flang defaults to TRUE on macOS when flang exists", {
86-
which <- function(cmd) {
87-
if (identical(cmd, "flang-new")) {
88-
return("/opt/bin/flang-new")
89-
}
90-
""
91-
}
92-
93-
withr::local_options(quickr.fortran_compiler = "auto")
94-
95-
expect_true(quickr:::quickr_prefer_flang(
96-
sysname = "Darwin",
97-
which = which,
98-
system2 = function(...) ""
99-
))
100-
expect_false(quickr:::quickr_prefer_flang(sysname = "Linux", which = which))
101-
})
102-
10392
test_that("quickr.fortran_compiler = \"gfortran\" disables auto preference", {
104-
which <- function(cmd) {
105-
if (identical(cmd, "flang-new")) {
106-
return("/opt/bin/flang-new")
107-
}
108-
""
109-
}
110-
11193
withr::local_options(quickr.fortran_compiler = "gfortran")
11294

113-
expect_false(quickr:::quickr_prefer_flang(sysname = "Darwin", which = which))
95+
expect_false(quickr_prefer_flang())
11496
})

0 commit comments

Comments
 (0)