From 7ff206d3ce6d7cf1e7285c020abe272871dbfc58 Mon Sep 17 00:00:00 2001 From: Andrew Johnson Date: Sat, 4 Jan 2025 19:44:16 +0800 Subject: [PATCH 1/9] Use stock Rtools toolchain by default for RTools44+ --- .github/workflows/R-CMD-check.yaml | 6 ------ R/install.R | 12 +++++++----- R/utils.R | 2 +- tests/testthat/test-install.R | 2 -- 4 files changed, 8 insertions(+), 14 deletions(-) diff --git a/.github/workflows/R-CMD-check.yaml b/.github/workflows/R-CMD-check.yaml index 9e23f3e3..efd02cd3 100644 --- a/.github/workflows/R-CMD-check.yaml +++ b/.github/workflows/R-CMD-check.yaml @@ -42,12 +42,6 @@ jobs: echo "CMDSTAN_PATH=${HOME}/.cmdstan" >> $GITHUB_ENV shell: bash - - name: Use stock RTools for Windows R-Devel - if: ${{ matrix.config.os == 'windows-latest' && matrix.config.r == 'devel' }} - run: | - echo "CMDSTANR_USE_RTOOLS=TRUE" >> $GITHUB_ENV - shell: bash - - uses: n1hility/cancel-previous-runs@v3 with: token: ${{ secrets.GITHUB_TOKEN }} diff --git a/R/install.R b/R/install.R index 9bb2550d..eb244d0a 100644 --- a/R/install.R +++ b/R/install.R @@ -643,7 +643,8 @@ check_rtools4x_windows_toolchain <- function(fix = FALSE, quiet = FALSE) { call. = FALSE ) } - if (Sys.getenv("CMDSTANR_USE_RTOOLS") != "") { + # No additional utilities/toolchains are needed with RTools44 + if (rtools4x_version() >= "44" && Sys.getenv("CMDSTANR_USE_MSYS_TOOLCHAIN") == "") { return(invisible(NULL)) } if (!is_toolchain_installed(app = "g++", path = toolchain_path) || @@ -855,10 +856,11 @@ toolchain_PATH_env_var <- function() { } rtools4x_toolchain_path <- function() { - toolchain <- ifelse(is_ucrt_toolchain(), "ucrt64", "mingw64") - if (Sys.getenv("CMDSTANR_USE_RTOOLS") != "") { - if (arch_is_aarch64()) { - toolchain <- "aarch64-w64-mingw32.static.posix" + if (arch_is_aarch64()) { + toolchain <- "aarch64-w64-mingw32.static.posix" + } else { + if (rtools4x_version() < "44" || Sys.getenv("CMDSTANR_USE_MSYS_TOOLCHAIN") != "") { + toolchain <- ifelse(is_ucrt_toolchain(), "ucrt64", "mingw64") } else { toolchain <- "x86_64-w64-mingw32.static.posix" } diff --git a/R/utils.R b/R/utils.R index 8c68641a..f1d7d5a6 100644 --- a/R/utils.R +++ b/R/utils.R @@ -96,7 +96,7 @@ arch_is_aarch64 <- function() { make_cmd <- function() { if (Sys.getenv("MAKE") != "") { Sys.getenv("MAKE") - } else if (os_is_windows() && !os_is_wsl() && (Sys.getenv("CMDSTANR_USE_RTOOLS") == "")) { + } else if (os_is_windows() && !os_is_wsl() && (rtools4x_version() < "44" || Sys.getenv("CMDSTANR_USE_MSYS_TOOLCHAIN") != "")) { "mingw32-make.exe" } else { "make" diff --git a/tests/testthat/test-install.R b/tests/testthat/test-install.R index 5d415543..211a19c3 100644 --- a/tests/testthat/test-install.R +++ b/tests/testthat/test-install.R @@ -1,6 +1,4 @@ context("install") -# Current tests need CmdStan 2.35 for stock rtools, but is not yet released -skip_if(Sys.getenv("CMDSTANR_USE_RTOOLS") != "") cmdstan_test_tarball_url <- Sys.getenv("CMDSTAN_TEST_TARBALL_URL") if (!nzchar(cmdstan_test_tarball_url)) { From e6aeb27686b11cee32db9a9b13f7dec8b31e8bc7 Mon Sep 17 00:00:00 2001 From: Andrew Johnson Date: Sat, 4 Jan 2025 21:41:04 +0800 Subject: [PATCH 2/9] Update tests, add warning for versions --- R/install.R | 7 +++++++ tests/testthat/test-install.R | 8 ++++---- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/R/install.R b/R/install.R index eb244d0a..1ca096db 100644 --- a/R/install.R +++ b/R/install.R @@ -103,6 +103,13 @@ install_cmdstan <- function(dir = NULL, } else { .cmdstanr$WSL <- FALSE } + if (os_is_windows() && !os_is_wsl() && version < "2.35.0") { + if (Sys.getenv("CMDSTANR_USE_MSYS_TOOLCHAIN") == "") { + stop("CmdStan versions prior to 2.35.0 require additional toolchain configuration on Windows.\n", + "Please set the environment variable CMDSTANR_USE_MSYS_TOOLCHAIN to 'true' and \n", + "call `check_cmdstan_toolchain(fix = TRUE)` before installing CmdStan.", call. = FALSE) + } + } if (check_toolchain) { check_cmdstan_toolchain(fix = FALSE, quiet = quiet) } diff --git a/tests/testthat/test-install.R b/tests/testthat/test-install.R index 211a19c3..b52df719 100644 --- a/tests/testthat/test-install.R +++ b/tests/testthat/test-install.R @@ -98,7 +98,7 @@ test_that("install_cmdstan() works with version and release_url", { expect_message( expect_output( install_cmdstan(dir = dir, overwrite = TRUE, cores = 4, - release_url = "https://github.com/stan-dev/cmdstan/releases/download/v2.33.0/cmdstan-2.33.0.tar.gz", + release_url = "https://github.com/stan-dev/cmdstan/releases/download/v2.33.0/cmdstan-2.35.0.tar.gz", wsl = os_is_wsl()), "Compiling, linking C++ code", fixed = TRUE @@ -110,7 +110,7 @@ test_that("install_cmdstan() works with version and release_url", { expect_message( expect_output( install_cmdstan(dir = dir, overwrite = TRUE, cores = 4, - version = "2.33.0", + version = "2.35.0", # the URL is intentionally invalid to test that the version has higher priority release_url = "https://github.com/stan-dev/cmdstan/releases/download/v2.27.3/cmdstan-2.27.3.tar.gz", wsl = os_is_wsl()), @@ -254,10 +254,10 @@ test_that("Install from release file works", { dir <- tempdir(check = TRUE) } - destfile = file.path(dir, "cmdstan-2.33.1.tar.gz") + destfile = file.path(dir, "cmdstan-2.35.0.tar.gz") download_with_retries( - "https://github.com/stan-dev/cmdstan/releases/download/v2.33.1/cmdstan-2.33.1.tar.gz", + "https://github.com/stan-dev/cmdstan/releases/download/v2.35.0/cmdstan-2.35.0.tar.gz", destfile) expect_message( From 11853aee4cbfbc229500de8565dd37768c5195f9 Mon Sep 17 00:00:00 2001 From: Andrew Johnson Date: Sat, 4 Jan 2025 21:44:14 +0800 Subject: [PATCH 3/9] Typo --- R/install.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/install.R b/R/install.R index 1ca096db..ef837b57 100644 --- a/R/install.R +++ b/R/install.R @@ -103,7 +103,7 @@ install_cmdstan <- function(dir = NULL, } else { .cmdstanr$WSL <- FALSE } - if (os_is_windows() && !os_is_wsl() && version < "2.35.0") { + if (os_is_windows() && !os_is_wsl() && isTRUE(version < "2.35.0")) { if (Sys.getenv("CMDSTANR_USE_MSYS_TOOLCHAIN") == "") { stop("CmdStan versions prior to 2.35.0 require additional toolchain configuration on Windows.\n", "Please set the environment variable CMDSTANR_USE_MSYS_TOOLCHAIN to 'true' and \n", From 62fd754519c2abe00a05dc1779e615c89db58dcc Mon Sep 17 00:00:00 2001 From: Andrew Johnson Date: Sat, 4 Jan 2025 22:28:34 +0800 Subject: [PATCH 4/9] Fix test url, add version checking to utils --- R/install.R | 2 +- R/utils.R | 2 +- tests/testthat/test-install.R | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/R/install.R b/R/install.R index ef837b57..48136251 100644 --- a/R/install.R +++ b/R/install.R @@ -866,7 +866,7 @@ rtools4x_toolchain_path <- function() { if (arch_is_aarch64()) { toolchain <- "aarch64-w64-mingw32.static.posix" } else { - if (rtools4x_version() < "44" || Sys.getenv("CMDSTANR_USE_MSYS_TOOLCHAIN") != "") { + if (rtools4x_version() < "44" || Sys.getenv("CMDSTANR_USE_MSYS_TOOLCHAIN") != "" || cmdstan_version() < "2.35.0") { toolchain <- ifelse(is_ucrt_toolchain(), "ucrt64", "mingw64") } else { toolchain <- "x86_64-w64-mingw32.static.posix" diff --git a/R/utils.R b/R/utils.R index f1d7d5a6..3904eeee 100644 --- a/R/utils.R +++ b/R/utils.R @@ -96,7 +96,7 @@ arch_is_aarch64 <- function() { make_cmd <- function() { if (Sys.getenv("MAKE") != "") { Sys.getenv("MAKE") - } else if (os_is_windows() && !os_is_wsl() && (rtools4x_version() < "44" || Sys.getenv("CMDSTANR_USE_MSYS_TOOLCHAIN") != "")) { + } else if (os_is_windows() && !os_is_wsl() && (rtools4x_version() < "44" || Sys.getenv("CMDSTANR_USE_MSYS_TOOLCHAIN") != "" || cmdstan_version() < "2.35.0")) { "mingw32-make.exe" } else { "make" diff --git a/tests/testthat/test-install.R b/tests/testthat/test-install.R index b52df719..e47f6915 100644 --- a/tests/testthat/test-install.R +++ b/tests/testthat/test-install.R @@ -98,7 +98,7 @@ test_that("install_cmdstan() works with version and release_url", { expect_message( expect_output( install_cmdstan(dir = dir, overwrite = TRUE, cores = 4, - release_url = "https://github.com/stan-dev/cmdstan/releases/download/v2.33.0/cmdstan-2.35.0.tar.gz", + release_url = "https://github.com/stan-dev/cmdstan/releases/download/v2.35.0/cmdstan-2.35.0.tar.gz", wsl = os_is_wsl()), "Compiling, linking C++ code", fixed = TRUE From cb9540731832ad2af9be8caed438860b1feec245 Mon Sep 17 00:00:00 2001 From: Andrew Johnson Date: Sat, 4 Jan 2025 22:37:21 +0800 Subject: [PATCH 5/9] Fixes for blank version --- R/install.R | 3 ++- R/utils.R | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/R/install.R b/R/install.R index 48136251..2b7a7b7b 100644 --- a/R/install.R +++ b/R/install.R @@ -866,7 +866,8 @@ rtools4x_toolchain_path <- function() { if (arch_is_aarch64()) { toolchain <- "aarch64-w64-mingw32.static.posix" } else { - if (rtools4x_version() < "44" || Sys.getenv("CMDSTANR_USE_MSYS_TOOLCHAIN") != "" || cmdstan_version() < "2.35.0") { + if (rtools4x_version() < "44" || Sys.getenv("CMDSTANR_USE_MSYS_TOOLCHAIN") != "" || + isTRUE(cmdstan_version(error_on_NA=FALSE) < "2.35.0")) { toolchain <- ifelse(is_ucrt_toolchain(), "ucrt64", "mingw64") } else { toolchain <- "x86_64-w64-mingw32.static.posix" diff --git a/R/utils.R b/R/utils.R index 3904eeee..4fde8149 100644 --- a/R/utils.R +++ b/R/utils.R @@ -96,7 +96,8 @@ arch_is_aarch64 <- function() { make_cmd <- function() { if (Sys.getenv("MAKE") != "") { Sys.getenv("MAKE") - } else if (os_is_windows() && !os_is_wsl() && (rtools4x_version() < "44" || Sys.getenv("CMDSTANR_USE_MSYS_TOOLCHAIN") != "" || cmdstan_version() < "2.35.0")) { + } else if (os_is_windows() && !os_is_wsl() && + (rtools4x_version() < "44" || Sys.getenv("CMDSTANR_USE_MSYS_TOOLCHAIN") != "" || isTRUE(cmdstan_version(error_on_NA=FALSE) < "2.35.0"))) { "mingw32-make.exe" } else { "make" From cdfbd69cdc7b16dacd1868d1a450336b0969a92e Mon Sep 17 00:00:00 2001 From: Andrew Johnson Date: Sat, 4 Jan 2025 23:49:49 +0800 Subject: [PATCH 6/9] Update test values --- tests/testthat/test-install.R | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/testthat/test-install.R b/tests/testthat/test-install.R index e47f6915..178bccc0 100644 --- a/tests/testthat/test-install.R +++ b/tests/testthat/test-install.R @@ -100,7 +100,7 @@ test_that("install_cmdstan() works with version and release_url", { install_cmdstan(dir = dir, overwrite = TRUE, cores = 4, release_url = "https://github.com/stan-dev/cmdstan/releases/download/v2.35.0/cmdstan-2.35.0.tar.gz", wsl = os_is_wsl()), - "Compiling, linking C++ code", + "Compiling C++ code", fixed = TRUE ), "Finished installing CmdStan", @@ -114,7 +114,7 @@ test_that("install_cmdstan() works with version and release_url", { # the URL is intentionally invalid to test that the version has higher priority release_url = "https://github.com/stan-dev/cmdstan/releases/download/v2.27.3/cmdstan-2.27.3.tar.gz", wsl = os_is_wsl()), - "Compiling, linking C++ code", + "Compiling C++ code", fixed = TRUE ), "Finished installing CmdStan", @@ -123,7 +123,7 @@ test_that("install_cmdstan() works with version and release_url", { "version and release_url shouldn't both be specified", fixed = TRUE ) - expect_true(dir.exists(file.path(dir, "cmdstan-2.33.0"))) + expect_true(dir.exists(file.path(dir, "cmdstan-2.35.0"))) set_cmdstan_path(cmdstan_default_path()) }) @@ -265,7 +265,7 @@ test_that("Install from release file works", { install_cmdstan(dir = dir, cores = 2, quiet = FALSE, overwrite = TRUE, release_file = destfile, wsl = os_is_wsl()), - "Compiling, linking C++ code", + "Compiling C++ code", fixed = TRUE ), "CmdStan path set", From fe07ccd8b4a6bf5a786eb5bf75ea03bc9cb61962 Mon Sep 17 00:00:00 2001 From: Andrew Johnson Date: Sun, 5 Jan 2025 11:30:24 +0800 Subject: [PATCH 7/9] Fix test failures --- tests/testthat/test-install.R | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/tests/testthat/test-install.R b/tests/testthat/test-install.R index 178bccc0..df6d73cc 100644 --- a/tests/testthat/test-install.R +++ b/tests/testthat/test-install.R @@ -26,13 +26,13 @@ test_that("install_cmdstan() successfully installs cmdstan", { test_that("install_cmdstan() errors if installation already exists", { install_dir <- cmdstan_default_install_path() - dir <- file.path(install_dir, "cmdstan-2.23.0") + dir <- file.path(install_dir, "cmdstan-2.35.0") if (!dir.exists(dir)) { dir.create(dir, recursive = TRUE) } expect_warning( install_cmdstan(dir = install_dir, overwrite = FALSE, - version = "2.23.0", wsl = FALSE), + version = "2.35.0", wsl = FALSE), "An installation already exists", fixed = TRUE ) @@ -74,13 +74,13 @@ test_that("install_cmdstan() errors if it times out", { test_that("install_cmdstan() errors if invalid version or URL", { expect_error( - install_cmdstan(version = "2.23.2", wsl = os_is_wsl()), - "Download of CmdStan failed with error: cannot open URL 'https://github.com/stan-dev/cmdstan/releases/download/v2.23.2/cmdstan-2.23.2.tar.gz'\nPlease check if the supplied version number is valid." + install_cmdstan(version = "2.35.5", wsl = os_is_wsl()), + "Download of CmdStan failed with error: cannot open URL 'https://github.com/stan-dev/cmdstan/releases/download/v2.35.5/cmdstan-2.35.5.tar.gz'\nPlease check if the supplied version number is valid." ) expect_error( - install_cmdstan(release_url = "https://github.com/stan-dev/cmdstan/releases/download/v2.23.2/cmdstan-2.23.2.tar.gz", + install_cmdstan(release_url = "https://github.com/stan-dev/cmdstan/releases/download/v2.35.5/cmdstan-2.35.5.tar.gz", wsl = os_is_wsl()), - "Download of CmdStan failed with error: cannot open URL 'https://github.com/stan-dev/cmdstan/releases/download/v2.23.2/cmdstan-2.23.2.tar.gz'\nPlease check if the supplied release URL is valid." + "Download of CmdStan failed with error: cannot open URL 'https://github.com/stan-dev/cmdstan/releases/download/v2.35.5/cmdstan-2.35.5.tar.gz'\nPlease check if the supplied release URL is valid." ) expect_error( install_cmdstan(release_url = "https://github.com/stan-dev/cmdstan/releases/tag/v2.24.0", wsl = os_is_wsl()), From e540183c1579063350f03f112a294eeaf0b341ba Mon Sep 17 00:00:00 2001 From: Andrew Johnson Date: Sun, 5 Jan 2025 22:15:06 +0800 Subject: [PATCH 8/9] Update install.R --- R/install.R | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/R/install.R b/R/install.R index 2b7a7b7b..6041585a 100644 --- a/R/install.R +++ b/R/install.R @@ -104,7 +104,9 @@ install_cmdstan <- function(dir = NULL, .cmdstanr$WSL <- FALSE } if (os_is_windows() && !os_is_wsl() && isTRUE(version < "2.35.0")) { - if (Sys.getenv("CMDSTANR_USE_MSYS_TOOLCHAIN") == "") { + # RTools44 can be used unmodified with CmdStan 2.35+ + # For new installs of older versions, users need to install mingw32-make and MSYS gcc + if (Sys.getenv("CMDSTANR_USE_MSYS_TOOLCHAIN") == "" && rtools4x_version() == "44") { stop("CmdStan versions prior to 2.35.0 require additional toolchain configuration on Windows.\n", "Please set the environment variable CMDSTANR_USE_MSYS_TOOLCHAIN to 'true' and \n", "call `check_cmdstan_toolchain(fix = TRUE)` before installing CmdStan.", call. = FALSE) From 3f093696f7ca07f1a86cb8195cc1ec2dd57629ca Mon Sep 17 00:00:00 2001 From: Andrew Johnson Date: Tue, 7 Jan 2025 07:29:47 +0800 Subject: [PATCH 9/9] Update documentation for CMDSTANR_USE_MSYS_TOOLCHAIN --- DESCRIPTION | 2 +- R/install.R | 13 ++++++++++++- man/fit-method-save_object.Rd | 2 +- man/install_cmdstan.Rd | 14 +++++++++++++- 4 files changed, 27 insertions(+), 4 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 4464e5c5..46f0ccf6 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -32,7 +32,7 @@ URL: https://mc-stan.org/cmdstanr/, https://discourse.mc-stan.org BugReports: https://github.com/stan-dev/cmdstanr/issues Encoding: UTF-8 LazyData: true -RoxygenNote: 7.3.1 +RoxygenNote: 7.3.2 Roxygen: list(markdown = TRUE, r6 = FALSE) SystemRequirements: CmdStan (https://mc-stan.org/users/interfaces/cmdstan) Depends: diff --git a/R/install.R b/R/install.R index 6041585a..93886885 100644 --- a/R/install.R +++ b/R/install.R @@ -20,8 +20,19 @@ #' #' The `check_cmdstan_toolchain()` function attempts to check for the required #' C++ toolchain. It is called internally by `install_cmdstan()` but can also -#' be called directly by the user. +#' be called directly by the user. On Windows only, calling the function with +#' the `fix = TRUE` argument will attempt to install the necessary toolchain +#' components if they are not found. For Windows users with RTools44 no additional +#' toolchain configuration is required. For users with older versions of RTools, +#' the function will install `mingw32-make` and `g++` from MSYS using the +#' RTools-provided `pacman` package manager. This can also be manually requested +#' by setting the environment variable `CMDSTANR_USE_MSYS_TOOLCHAIN` to 'true' #' +#' NOTE: When installing CmdStan on Windows with RTools44 and CmdStan versions +#' prior to 2.35.0, the above additional toolchain configuration +#' is still required. To enable this configuration, set the environment variable +#' `CMDSTANR_USE_MSYS_TOOLCHAIN` to 'true' and call +#' `check_cmdstan_toolchain(fix = TRUE)`. #' #' @export #' @param dir (string) The path to the directory in which to install CmdStan. diff --git a/man/fit-method-save_object.Rd b/man/fit-method-save_object.Rd index d2c2ff34..6b1999fb 100644 --- a/man/fit-method-save_object.Rd +++ b/man/fit-method-save_object.Rd @@ -19,7 +19,7 @@ model object. Because the contents of the CmdStan output CSV files are only read into R lazily (i.e., as needed), the \verb{$save_object()} method is the safest way to guarantee that everything has been read in before saving. -See the "Saving fitted model objects" sections of the +See the "Saving fitted model objects" section of the \href{https://mc-stan.org/cmdstanr/articles/cmdstanr.html}{\emph{Getting started with CmdStanR}} vignette for some suggestions on faster model saving for large models. } diff --git a/man/install_cmdstan.Rd b/man/install_cmdstan.Rd index 44450d8f..3b88d772 100644 --- a/man/install_cmdstan.Rd +++ b/man/install_cmdstan.Rd @@ -118,7 +118,19 @@ should typically be followed by calling \code{rebuild_cmdstan()}. The \code{check_cmdstan_toolchain()} function attempts to check for the required C++ toolchain. It is called internally by \code{install_cmdstan()} but can also -be called directly by the user. +be called directly by the user. On Windows only, calling the function with +the \code{fix = TRUE} argument will attempt to install the necessary toolchain +components if they are not found. For Windows users with RTools44 no additional +toolchain configuration is required. For users with older versions of RTools, +the function will install \code{mingw32-make} and \verb{g++} from MSYS using the +RTools-provided \code{pacman} package manager. This can also be manually requested +by setting the environment variable \code{CMDSTANR_USE_MSYS_TOOLCHAIN} to 'true' + +NOTE: When installing CmdStan on Windows with RTools44 and CmdStan versions +prior to 2.35.0, the above additional toolchain configuration +is still required. To enable this configuration, set the environment variable +\code{CMDSTANR_USE_MSYS_TOOLCHAIN} to 'true' and call +\code{check_cmdstan_toolchain(fix = TRUE)}. } \examples{ \dontrun{