Skip to content

Commit 8163a92

Browse files
authored
Merge 4927783 into c9369c4
2 parents c9369c4 + 4927783 commit 8163a92

File tree

6 files changed

+148
-112
lines changed

6 files changed

+148
-112
lines changed

.github/workflows/R-CMD-check.yaml

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,7 @@ jobs:
6969
- name: Install cmdstan
7070
run: |
7171
cmdstanr::check_cmdstan_toolchain(fix = TRUE)
72-
if (Sys.getenv("CMDSTANR_USE_RTOOLS") == "TRUE") {
73-
cmdstanr::install_cmdstan(cores = 2, version = "2.35.0-rc2")
74-
} else {
75-
cmdstanr::install_cmdstan(cores = 2)
76-
}
72+
cmdstanr::install_cmdstan(cores = 2)
7773
shell: Rscript {0}
7874

7975
- name: Session info

R/install.R

Lines changed: 65 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -453,63 +453,72 @@ build_cmdstan <- function(dir,
453453
} else {
454454
run_cmd <- make_cmd()
455455
}
456-
withr::with_path(
457-
c(
458-
toolchain_PATH_env_var(),
459-
tbb_path(dir = dir)
460-
),
461-
wsl_compatible_run(
462-
command = run_cmd,
463-
args = c(translation_args, paste0("-j", cores), "build"),
464-
wd = dir,
465-
echo_cmd = is_verbose_mode(),
466-
echo = !quiet || is_verbose_mode(),
467-
spinner = quiet,
468-
error_on_status = FALSE,
469-
stderr_callback = function(x, p) { if (quiet) message(x) },
470-
timeout = timeout
456+
withr::with_envvar(
457+
c("HOME" = short_path(Sys.getenv("HOME"))),
458+
withr::with_path(
459+
c(
460+
toolchain_PATH_env_var(),
461+
tbb_path(dir = dir)
462+
),
463+
wsl_compatible_run(
464+
command = run_cmd,
465+
args = c(translation_args, paste0("-j", cores), "build"),
466+
wd = dir,
467+
echo_cmd = is_verbose_mode(),
468+
echo = !quiet || is_verbose_mode(),
469+
spinner = quiet,
470+
error_on_status = FALSE,
471+
stderr_callback = function(x, p) { if (quiet) message(x) },
472+
timeout = timeout
473+
)
471474
)
472475
)
473476
}
474477

475478
clean_cmdstan <- function(dir = cmdstan_path(),
476479
cores = getOption("mc.cores", 2),
477480
quiet = FALSE) {
478-
withr::with_path(
479-
c(
480-
toolchain_PATH_env_var(),
481-
tbb_path(dir = dir)
482-
),
483-
wsl_compatible_run(
484-
command = make_cmd(),
485-
args = "clean-all",
486-
wd = dir,
487-
echo_cmd = is_verbose_mode(),
488-
echo = !quiet || is_verbose_mode(),
489-
spinner = quiet,
490-
error_on_status = FALSE,
491-
stderr_callback = function(x, p) { if (quiet) message(x) }
481+
withr::with_envvar(
482+
c("HOME" = short_path(Sys.getenv("HOME"))),
483+
withr::with_path(
484+
c(
485+
toolchain_PATH_env_var(),
486+
tbb_path(dir = dir)
487+
),
488+
wsl_compatible_run(
489+
command = make_cmd(),
490+
args = "clean-all",
491+
wd = dir,
492+
echo_cmd = is_verbose_mode(),
493+
echo = !quiet || is_verbose_mode(),
494+
spinner = quiet,
495+
error_on_status = FALSE,
496+
stderr_callback = function(x, p) { if (quiet) message(x) }
497+
)
492498
)
493499
)
494500
}
495501

496502
build_example <- function(dir, cores, quiet, timeout) {
497-
withr::with_path(
498-
c(
499-
toolchain_PATH_env_var(),
500-
tbb_path(dir = dir)
501-
),
502-
wsl_compatible_run(
503-
command = make_cmd(),
504-
args = c(paste0("-j", cores),
505-
cmdstan_ext(file.path("examples", "bernoulli", "bernoulli"))),
506-
wd = dir,
507-
echo_cmd = is_verbose_mode(),
508-
echo = !quiet || is_verbose_mode(),
509-
spinner = quiet,
510-
error_on_status = FALSE,
511-
stderr_callback = function(x, p) { if (quiet) message(x) },
512-
timeout = timeout
503+
withr::with_envvar(
504+
c("HOME" = short_path(Sys.getenv("HOME"))),
505+
withr::with_path(
506+
c(
507+
toolchain_PATH_env_var(),
508+
tbb_path(dir = dir)
509+
),
510+
wsl_compatible_run(
511+
command = make_cmd(),
512+
args = c(paste0("-j", cores),
513+
cmdstan_ext(file.path("examples", "bernoulli", "bernoulli"))),
514+
wd = dir,
515+
echo_cmd = is_verbose_mode(),
516+
echo = !quiet || is_verbose_mode(),
517+
spinner = quiet,
518+
error_on_status = FALSE,
519+
stderr_callback = function(x, p) { if (quiet) message(x) },
520+
timeout = timeout
521+
)
513522
)
514523
)
515524
}
@@ -849,7 +858,11 @@ toolchain_PATH_env_var <- function() {
849858
rtools4x_toolchain_path <- function() {
850859
toolchain <- ifelse(is_ucrt_toolchain(), "ucrt64", "mingw64")
851860
if (Sys.getenv("CMDSTANR_USE_RTOOLS") != "") {
852-
toolchain <- "x86_64-w64-mingw32.static.posix"
861+
if (arch_is_aarch64()) {
862+
toolchain <- "aarch64-w64-mingw32.static.posix"
863+
} else {
864+
toolchain <- "x86_64-w64-mingw32.static.posix"
865+
}
853866
}
854867
repair_path(file.path(rtools4x_home_path(), toolchain, "bin"))
855868
}
@@ -871,10 +884,16 @@ rtools4x_version <- function() {
871884

872885
rtools4x_home_path <- function() {
873886
rtools_ver <- rtools4x_version()
887+
if (arch_is_aarch64()) {
888+
rtools_ver <- paste0(rtools_ver, "_AARCH64")
889+
}
874890
path <- Sys.getenv(paste0("RTOOLS", rtools_ver, "_HOME"))
875891

876892
if (!nzchar(path)) {
877893
default_path <- repair_path(file.path(paste0("C:/rtools", rtools_ver)))
894+
if (arch_is_aarch64()) {
895+
default_path <- paste0(default_path, "-aarch64")
896+
}
878897
if (dir.exists(default_path)) {
879898
path <- default_path
880899
}

R/model.R

Lines changed: 46 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -665,53 +665,56 @@ compile <- function(quiet = TRUE,
665665
expose_stan_functions(self$functions, !quiet)
666666
}
667667

668-
withr::with_path(
669-
c(
670-
toolchain_PATH_env_var(),
671-
tbb_path()
672-
),
673-
run_log <- wsl_compatible_run(
674-
command = make_cmd(),
675-
args = c(wsl_safe_path(repair_path(tmp_exe)),
676-
cpp_options_to_compile_flags(cpp_options),
677-
stancflags_val),
678-
wd = cmdstan_path(),
679-
echo = !quiet || is_verbose_mode(),
680-
echo_cmd = is_verbose_mode(),
681-
spinner = quiet && rlang::is_interactive() && !identical(Sys.getenv("IN_PKGDOWN"), "true"),
682-
stderr_callback = function(x, p) {
683-
if (!startsWith(x, paste0(make_cmd(), ": *** No rule to make target"))) {
684-
message(x)
685-
}
686-
if (grepl("PCH file", x) || grepl("precompiled header", x) || grepl(".hpp.gch", x) ) {
687-
warning(
688-
"CmdStan's precompiled header (PCH) files may need to be rebuilt.\n",
689-
"If your model failed to compile please run rebuild_cmdstan().\n",
690-
"If the issue persists please open a bug report.",
691-
call. = FALSE
692-
)
693-
}
694-
if (grepl("No space left on device", x) || grepl("error in backend: IO failure on output stream", x)) {
695-
warning(
696-
"The C++ compiler ran out of disk space and was unable to build the executables for your model!\n",
697-
"See the above error for more details.",
698-
call. = FALSE
699-
)
700-
}
701-
if (os_is_macos()) {
702-
if (R.version$arch == "aarch64"
703-
&& grepl("but the current translation unit is being compiled for target", x)) {
668+
withr::with_envvar(
669+
c("HOME" = short_path(Sys.getenv("HOME"))),
670+
withr::with_path(
671+
c(
672+
toolchain_PATH_env_var(),
673+
tbb_path()
674+
),
675+
run_log <- wsl_compatible_run(
676+
command = make_cmd(),
677+
args = c(wsl_safe_path(repair_path(tmp_exe)),
678+
cpp_options_to_compile_flags(cpp_options),
679+
stancflags_val),
680+
wd = cmdstan_path(),
681+
echo = !quiet || is_verbose_mode(),
682+
echo_cmd = is_verbose_mode(),
683+
spinner = quiet && rlang::is_interactive() && !identical(Sys.getenv("IN_PKGDOWN"), "true"),
684+
stderr_callback = function(x, p) {
685+
if (!startsWith(x, paste0(make_cmd(), ": *** No rule to make target"))) {
686+
message(x)
687+
}
688+
if (grepl("PCH file", x) || grepl("precompiled header", x) || grepl(".hpp.gch", x) ) {
704689
warning(
705-
"The C++ compiler has errored due to incompatibility between the x86 and ",
706-
"Apple Silicon architectures.\n",
707-
"If you are running R inside an IDE (RStudio, VSCode, ...), ",
708-
"make sure the IDE is a native Apple Silicon app.\n",
690+
"CmdStan's precompiled header (PCH) files may need to be rebuilt.\n",
691+
"If your model failed to compile please run rebuild_cmdstan().\n",
692+
"If the issue persists please open a bug report.",
709693
call. = FALSE
710694
)
711695
}
712-
}
713-
},
714-
error_on_status = FALSE
696+
if (grepl("No space left on device", x) || grepl("error in backend: IO failure on output stream", x)) {
697+
warning(
698+
"The C++ compiler ran out of disk space and was unable to build the executables for your model!\n",
699+
"See the above error for more details.",
700+
call. = FALSE
701+
)
702+
}
703+
if (os_is_macos()) {
704+
if (R.version$arch == "aarch64"
705+
&& grepl("but the current translation unit is being compiled for target", x)) {
706+
warning(
707+
"The C++ compiler has errored due to incompatibility between the x86 and ",
708+
"Apple Silicon architectures.\n",
709+
"If you are running R inside an IDE (RStudio, VSCode, ...), ",
710+
"make sure the IDE is a native Apple Silicon app.\n",
711+
call. = FALSE
712+
)
713+
}
714+
}
715+
},
716+
error_on_status = FALSE
717+
)
715718
)
716719
)
717720
if (is.na(run_log$status) || run_log$status != 0) {

R/run.R

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -410,18 +410,21 @@ CmdStanRun <- R6::R6Class(
410410
check_target_exe <- function(exe) {
411411
exe_path <- file.path(cmdstan_path(), exe)
412412
if (!file.exists(exe_path)) {
413-
withr::with_path(
414-
c(
415-
toolchain_PATH_env_var(),
416-
tbb_path()
417-
),
418-
run_log <- wsl_compatible_run(
419-
command = make_cmd(),
420-
args = exe,
421-
wd = cmdstan_path(),
422-
echo_cmd = TRUE,
423-
echo = TRUE,
424-
error_on_status = TRUE
413+
withr::with_envvar(
414+
c("HOME" = short_path(Sys.getenv("HOME"))),
415+
withr::with_path(
416+
c(
417+
toolchain_PATH_env_var(),
418+
tbb_path()
419+
),
420+
run_log <- wsl_compatible_run(
421+
command = make_cmd(),
422+
args = exe,
423+
wd = cmdstan_path(),
424+
echo_cmd = TRUE,
425+
echo = TRUE,
426+
error_on_status = TRUE
427+
)
425428
)
426429
)
427430
}

R/utils.R

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,10 @@ is_rosetta2 <- function() {
8383
rosetta2
8484
}
8585

86+
arch_is_aarch64 <- function() {
87+
isTRUE(R.version$arch == "aarch64")
88+
}
89+
8690
# Returns the type of make command to use to compile depending on the OS
8791
make_cmd <- function() {
8892
if (os_is_windows() && !os_is_wsl() && (Sys.getenv("CMDSTANR_USE_RTOOLS") == "")) {
@@ -103,6 +107,14 @@ stanc_cmd <- function() {
103107

104108
# paths and extensions ----------------------------------------------------
105109

110+
short_path <- function(path) {
111+
if (os_is_windows()) {
112+
utils::shortPathName(path)
113+
} else {
114+
path
115+
}
116+
}
117+
106118
# Replace `\\` with `/` in a vector of paths
107119
# Needed for windows if CmdStan version is < 2.21:
108120
# https://github.com/stan-dev/cmdstanr/issues/1#issuecomment-539118598
@@ -688,11 +700,14 @@ assert_file_exists <- checkmate::makeAssertionFunction(check_file_exists)
688700
# Model methods & expose_functions helpers ------------------------------------------------------
689701
get_cmdstan_flags <- function(flag_name) {
690702
cmdstan_path <- cmdstanr::cmdstan_path()
691-
flags <- wsl_compatible_run(
692-
command = "make",
693-
args = c("-s", paste0("print-", flag_name)),
694-
wd = cmdstan_path
695-
)$stdout
703+
withr::with_envvar(
704+
c("HOME" = short_path(Sys.getenv("HOME"))),
705+
flags <- wsl_compatible_run(
706+
command = "make",
707+
args = c("-s", paste0("print-", flag_name)),
708+
wd = cmdstan_path
709+
)$stdout
710+
)
696711

697712
flags <- gsub("\n", "", flags, fixed = TRUE)
698713

tests/testthat/test-model-init.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ test_that("all fitting methods work with provided init files", {
2020
mod$optimize(data = data_list, init = init_json_1, seed = 123)
2121
)
2222
expect_vb_output(
23-
mod$variational(data = data_list, init = init_json_1, seed = 123)
23+
mod$variational(data = data_list, init = init_json_1, seed = 1234)
2424
)
2525
expect_laplace_output(
2626
mod$laplace(data = data_list, init = init_json_1, seed = 123)

0 commit comments

Comments
 (0)