Skip to content

Commit 3368809

Browse files
committed
add stancflags from make/local
1 parent f8f6321 commit 3368809

File tree

3 files changed

+32
-6
lines changed

3 files changed

+32
-6
lines changed

R/model.R

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -575,12 +575,15 @@ compile <- function(quiet = TRUE,
575575
stanc_built_options <- c(stanc_built_options, paste0("--", option_name, "=", "'", stanc_options[[i]], "'"))
576576
}
577577
}
578-
stancflags_standalone <- c("--standalone-functions", stancflags_val, stanc_built_options)
578+
579+
stancflags_local <- get_cmdstan_flags("STANCFLAGS")
580+
581+
stancflags_standalone <- c("--standalone-functions", stancflags_val, stanc_built_options, stancflags_local)
579582
self$functions$hpp_code <- get_standalone_hpp(temp_stan_file, stancflags_standalone)
580583
if (compile_standalone) {
581584
expose_functions(self$functions, !quiet)
582585
}
583-
stancflags_val <- paste0("STANCFLAGS += ", stancflags_val, paste0(" ", stanc_built_options, collapse = " "))
586+
stancflags_val <- paste0("STANCFLAGS += ", stancflags_val, paste0(" ", c(stanc_built_options, stancflags_local), collapse = " "))
584587
withr::with_path(
585588
c(
586589
toolchain_PATH_env_var(),

R/utils.R

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -661,19 +661,29 @@ assert_file_exists <- checkmate::makeAssertionFunction(check_file_exists)
661661
# Model methods & expose_functions helpers ------------------------------------------------------
662662
get_cmdstan_flags <- function(flag_name) {
663663
cmdstan_path <- cmdstanr::cmdstan_path()
664-
flags <- processx::run(
665-
"make",
664+
flags <- wsl_compatible_run(
665+
command = "make",
666666
args = c(paste0("print-", flag_name)),
667667
wd = cmdstan_path
668668
)$stdout
669669

670670
flags <- gsub("\n", "", flags, fixed = TRUE)
671671

672672
flags <- gsub(
673-
pattern = paste0(flag_name, " ="),
674-
replacement = "", x = flags, fixed = TRUE
673+
pattern = paste0(flag_name, "\\s(=|\\+=)(\\s|$)"),
674+
replacement = "", x = flags
675675
)
676676

677+
if (flags == "") {
678+
return(flags)
679+
}
680+
681+
if (flag_name == "STANCFLAGS") {
682+
# StanC flags need to be returned as a character vector
683+
flags_vec <- strsplit(x = flags, split = " ", fixed = TRUE)[[1]]
684+
return(flags_vec)
685+
}
686+
677687
if (flag_name %in% c("LDLIBS", "LDFLAGS_TBB")) {
678688
# shQuote -L paths and rpaths
679689
# The LDLIBS flags change paths to /c/ instead of C:/, need to revert to

tests/testthat/test-model-compile.R

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -801,3 +801,16 @@ test_that("dirname of stan_file is used as include path if no other paths suppli
801801
expect_true(mod_tmp$format())
802802
expect_s3_class(mod_tmp$compile(), "CmdStanModel")
803803
})
804+
805+
test_that("STANCFLAGS included from make/local", {
806+
make_local_old <- cmdstan_make_local()
807+
cmdstan_make_local(cpp_options = "STANCFLAGS += --O1", append = FALSE)
808+
out <- utils::capture.output(mod$compile(quiet = FALSE, force_recompile = TRUE))
809+
if(os_is_windows() && !os_is_wsl()) {
810+
out_w_flags <- "bin/stanc.exe --name='bernoulli_model' --O1 --o"
811+
} else {
812+
out_w_flags <- "bin/stanc --name='bernoulli_model' --O1 --o"
813+
}
814+
expect_output(print(out), out_w_flags)
815+
cmdstan_make_local(cpp_options = make_local_old, append = FALSE)
816+
})

0 commit comments

Comments
 (0)