Skip to content

Commit

Permalink
Fix compilation failures for Stan models with #include statements (#…
Browse files Browse the repository at this point in the history
…1000)

* Fix quoting differences for standalone hpp

* Stray logging
  • Loading branch information
andrjohns authored Jun 20, 2024
1 parent a45d4f7 commit 9878dda
Showing 1 changed file with 14 additions and 6 deletions.
20 changes: 14 additions & 6 deletions R/model.R
Original file line number Diff line number Diff line change
Expand Up @@ -650,10 +650,11 @@ compile <- function(quiet = TRUE,
if (stancflags_local != "") {
stancflags_combined <- c(stancflags_combined, stancflags_local)
}
stancflags_standalone <- c("--standalone-functions", stancflags_val, stancflags_combined)
stanc_inc_paths <- include_paths_stanc3_args(include_paths, standalone_call = TRUE)
stancflags_standalone <- c("--standalone-functions", stanc_inc_paths, stancflags_combined)
self$functions$hpp_code <- get_standalone_hpp(temp_stan_file, stancflags_standalone)
private$model_methods_env_ <- new.env()
private$model_methods_env_$hpp_code_ <- get_standalone_hpp(temp_stan_file, c(stancflags_val, stancflags_combined))
private$model_methods_env_$hpp_code_ <- get_standalone_hpp(temp_stan_file, c(stanc_inc_paths, stancflags_combined))
self$functions$external <- !is.null(user_header)
self$functions$existing_exe <- FALSE

Expand Down Expand Up @@ -2337,20 +2338,27 @@ cpp_options_to_compile_flags <- function(cpp_options) {
cpp_built_options
}

include_paths_stanc3_args <- function(include_paths = NULL) {
include_paths_stanc3_args <- function(include_paths = NULL, standalone_call = FALSE) {
stancflags <- NULL
if (!is.null(include_paths)) {
assert_dir_exists(include_paths, access = "r")
include_paths <- sapply(absolute_path(include_paths), wsl_safe_path)
paths_w_space <- grep(" ", include_paths)
include_paths[paths_w_space] <- paste0("'", include_paths[paths_w_space], "'")
# Calling stanc3 directly through processx::run does not need quoting
if (!isTRUE(standalone_call)) {
paths_w_space <- grep(" ", include_paths)
include_paths[paths_w_space] <- paste0("'", include_paths[paths_w_space], "'")
}
include_paths <- paste0(include_paths, collapse = ",")
if (cmdstan_version() >= "2.24") {
include_paths_flag <- "--include-paths="
} else {
include_paths_flag <- "--include_paths="
}
stancflags <- paste0(stancflags, include_paths_flag, include_paths)
if (isTRUE(standalone_call)) {
stancflags <- c(stancflags, "--include-paths", include_paths)
} else {
stancflags <- paste0(stancflags, include_paths_flag, include_paths)
}
}
stancflags
}
Expand Down

0 comments on commit 9878dda

Please sign in to comment.