From 733e052f763e8d592486557fcbd15638a00ac10e Mon Sep 17 00:00:00 2001 From: Andrew Johnson Date: Tue, 23 Jan 2024 23:31:52 +0200 Subject: [PATCH] Alternate windows approach --- R/utils.R | 30 ++++++++++++++++++++---------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/R/utils.R b/R/utils.R index 881d9549..6ef64e2f 100644 --- a/R/utils.R +++ b/R/utils.R @@ -740,25 +740,30 @@ with_cmdstan_flags <- function(expr) { libs <- c("LDLIBS", "LIBSUNDIALS", "TBB_TARGETS", "LDFLAGS_TBB") libs <- paste(sapply(libs, get_cmdstan_flags), collapse = " ") - if (.Platform$OS.type == "windows") { + if (os_is_windows()) { libs <- paste(libs, "-fopenmp -lstdc++") } lib_paths <- c("/stan/lib/stan_math/lib/tbb/", "/stan/lib/stan_math/lib/sundials_6.1.1/lib/") + new_makevars <- c( + PKG_CPPFLAGS = ifelse(cmdstan_version() <= "2.30.1", "-DCMDSTAN_JSON", ""), + PKG_CXXFLAGS = paste(cxxflags, cmdstanr_includes, r_includes, collapse = " "), + PKG_LIBS = libs + ) + if (os_is_windows()) { + new_makevars <- c( + new_makevars, + SHLIB_LD = paste0(rtools4x_toolchain_path(),"/gcc"), + LOCAL_CPPFLAGS = paste0("-L'",rtools4x_toolchain_path(),"/../include'"), + LOCAL_LIBS = paste0("-L'",rtools4x_toolchain_path(),"/../lib'") + ) + } withr::with_path( c( paste0(cmdstan_path(), lib_paths), toolchain_PATH_env_var() ), - withr::with_makevars( - c( - USE_CXX14 = 1, - PKG_CPPFLAGS = ifelse(cmdstan_version() <= "2.30.1", "-DCMDSTAN_JSON", ""), - PKG_CXXFLAGS = paste(cxxflags, cmdstanr_includes, r_includes, collapse = " "), - PKG_LIBS = libs - ), - expr - ) + withr::with_makevars(new_makevars, expr) ) } @@ -818,11 +823,16 @@ expose_model_methods <- function(env, force_recompile = FALSE, verbose = FALSE) } methods_dll <- tempfile(fileext = .Platform$dynlib.ext) + envvar <- c("current") + if (os_is_windows()) { + envvar <- c(envvar, "BINPREF" = paste0(rtools4x_toolchain_path(), "/")) + } with_cmdstan_flags( processx::run( command = file.path(R.home(component = "bin"), "R"), args = c("CMD", "SHLIB", repair_path(model_obj_file), repair_path(precomp_methods_file), "-o", repair_path(methods_dll)), + env = envvar, echo = verbose || is_verbose_mode(), echo_cmd = is_verbose_mode(), error_on_status = FALSE