@@ -727,18 +727,38 @@ get_cmdstan_flags <- function(flag_name) {
727727 paste(flags , collapse = " " )
728728}
729729
730+ check_sundials_fpic <- function (verbose ) {
731+ if (! os_is_linux()){
732+ return (invisible (NULL ))
733+ }
734+ sundials_flags <- get_cmdstan_flags(" CPPFLAGS_SUNDIALS" )
735+ local_flags <- cmdstan_make_local()
736+ if (any(grepl(" -fPIC" , c(sundials_flags , local_flags ), fixed = TRUE ))) {
737+ return (invisible (NULL ))
738+ }
739+ if (interactive()) {
740+ message(" SUNDIALS needs to be compiled with -fPIC when exposing functions or " ,
741+ " model methods on Linux.\n " ,
742+ " Updating your make/local file to include -fPIC and rebuilding CmdStan now..." )
743+ }
744+ cmdstan_make_local(cpp_options = list (" CPPFLAGS_SUNDIALS += -fPIC" ), append = TRUE )
745+ rebuild_cmdstan(quiet = ! verbose )
746+ if (interactive()) {
747+ message(" CmdStan has been rebuilt, continuing with model compilation..." )
748+ }
749+ }
750+
730751rcpp_source_stan <- function (code , env , verbose = FALSE , ... ) {
752+ check_sundials_fpic(verbose )
731753 cxxflags <- get_cmdstan_flags(" CXXFLAGS" )
732754 cmdstanr_includes <- system.file(" include" , package = " cmdstanr" , mustWork = TRUE )
733755 cmdstanr_includes <- paste0(" -I\" " , cmdstanr_includes ," \" " )
734- libs <- c(" LDLIBS" , " LIBSUNDIALS" , " TBB_TARGETS" , " LDFLAGS_TBB" )
756+ libs <- c(" LDLIBS" , " LIBSUNDIALS" , " TBB_TARGETS" , " LDFLAGS_TBB" , " SUNDIALS_TARGETS " )
735757 libs <- paste(sapply(libs , get_cmdstan_flags ), collapse = " " )
736758 if (.Platform $ OS.type == " windows" ) {
737759 libs <- paste(libs , " -fopenmp" )
738760 }
739- lib_paths <- c(" /stan/lib/stan_math/lib/tbb/" ,
740- " /stan/lib/stan_math/lib/sundials_6.1.1/lib/" )
741- withr :: with_path(paste0(cmdstan_path(), lib_paths ),
761+ withr :: with_path(repair_path(file.path(cmdstan_path()," stan/lib/stan_math/lib/tbb" )),
742762 withr :: with_makevars(
743763 c(
744764 USE_CXX14 = 1 ,
0 commit comments