Skip to content

Commit

Permalink
Merge pull request #747 from stan-dev/expose-functions-external
Browse files Browse the repository at this point in the history
Add version check when exposing external functions
  • Loading branch information
andrjohns authored Mar 24, 2023
2 parents 4c4dd09 + 3642755 commit 8bb211a
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 0 deletions.
1 change: 1 addition & 0 deletions R/model.R
Original file line number Diff line number Diff line change
Expand Up @@ -582,6 +582,7 @@ compile <- function(quiet = TRUE,
}
stancflags_standalone <- c("--standalone-functions", stancflags_val, stancflags_combined)
self$functions$hpp_code <- get_standalone_hpp(temp_stan_file, stancflags_standalone)
self$functions$external <- !is.null(user_header)
if (compile_standalone) {
expose_functions(self$functions, !quiet)
}
Expand Down
4 changes: 4 additions & 0 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -878,6 +878,10 @@ expose_functions <- function(function_env, global = FALSE, verbose = FALSE) {
"WSL CmdStan and will not be compiled",
call. = FALSE)
}
if (function_env$external && cmdstan_version() < "2.32") {
stop("Exporting standalone functions with external C++ is not available before CmdStan 2.32",
call. = FALSE)
}
require_suggested_package("Rcpp")
require_suggested_package("RcppEigen")
require_suggested_package("decor")
Expand Down
33 changes: 33 additions & 0 deletions tests/testthat/test-model-expose-functions.R
Original file line number Diff line number Diff line change
Expand Up @@ -147,3 +147,36 @@ test_that("Overloaded functions give meaningful errors", {
expect_error(funmod$expose_functions(),
"Overloaded functions are currently not able to be exposed to R! The following overloaded functions were found: fun1, fun3")
})

test_that("Exposing external functions errors before v2.32", {
skip_if(os_is_wsl())

if (getRversion() < '3.5.0') {
dir <- tempdir()
} else {
dir <- tempdir(check = TRUE)
}
install_cmdstan(dir = dir, cores = 2, quiet = FALSE,
overwrite = TRUE, version = "2.31.0",
wsl = os_is_wsl())

tmpfile <- tempfile(fileext = ".hpp")
hpp <-
"
#include <ostream>
namespace standalone_external_model_namespace {
int rtn_int(int x, std::ostream *pstream__) { return x; }
}"
cat(hpp, file = tmpfile, sep = "\n")
stanfile <- file.path(tempdir(), "standalone_external.stan")
cat("functions { int rtn_int(int x); }\n", file = stanfile)
expect_error({
cmdstan_model(
stan_file = stanfile,
user_header = tmpfile,
compile_standalone = TRUE
)
},
"Exporting standalone functions with external C++ is not available before CmdStan 2.32",
fixed = TRUE)
})

0 comments on commit 8bb211a

Please sign in to comment.