wlandau / instantiate

Pre-compiled CmdStan models in R packages
https://wlandau.github.io/instantiate/
Other
22 stars 2 forks source link

PCH issues compiling a package after internal installation of CmdStan on Mac OS #9

Closed wlandau closed 10 months ago

wlandau commented 1 year ago

Running R 4.3.0 on an M2 Mac with OS 13.5, I am running into PCH issues trying to compile a package after internal installation of CmdStan. First, I install instantiate with the "internal" method where CmdStan is compiled inside the package file system.

Sys.setenv(CMDSTAN_INSTALL = "internal")
remotes::install_github('wlandau/instantiate")
library(instantiate)
stan_cmdstan_path() # CmdStan is inside the package file system
#> [1] "/Users/CENSORED/projects/instantiate/inst/cmdstan/cmdstan-2.32.2"

Then I create an example package with a Stan file inside.

path <- "example"
stan_package_create(path = path)
stan_package_configure(path = path)
fs::dir_tree(path)
#> example
#> ├── DESCRIPTION
#> ├── NAMESPACE
#> ├── NOTICE
#> ├── R
#> │   ├── model.R
#> │   └── package.R
#> ├── cleanup
#> ├── cleanup.win
#> ├── configure
#> ├── configure.win
#> ├── example.Rproj
#> ├── inst
#> │   └── stan
#> │       └── bernoulli.stan
#> └── man
#>     ├── example-package.Rd
#>     └── run_bernoulli_model.Rd

Compilation fails:

install.packages(
  pkgs = path,
  lib = temporary_library,
  type = "source",
  repos = NULL
)
#> * installing *source* package ‘example’ ...
#> ** using staged installation
#> Running make \
#>   /var/folders/4v/vh7xp8553lsbl49svl48g7p00000gp/T/RtmpUAgcPM/model-b6944df95fca \
#>   "STANCFLAGS +=  --name='bernoulli_model'"
#> 
#> --- Translating Stan model to C++ code ---
#> bin/stanc --name='bernoulli_model' --o=/var/folders/4v/vh7xp8553lsbl49svl48g7p00000gp/T/RtmpUAgcPM/model-b6944df95fca.hpp /var/folders/4v/vh7xp8553lsbl49svl48g7p00000gp/T/RtmpUAgcPM/model-b6944df95fca.stan
#> 
#> --- Compiling, linking C++ code ---
#> clang++ -arch arm64 -std=gnu++17 -falign-functions=64 -Wall -g -O2 -falign-functions=1 -Wno-deprecated-declarations -std=c++1y -Wno-unknown-warning-option -Wno-tautological-compare -Wno-sign-compare -D_REENTRANT -Wno-ignored-attributes      -I stan/lib/stan_math/lib/tbb_2020.3/include    -O3 -I src -I stan/src -I stan/lib/rapidjson_1.1.0/ -I lib/CLI11-1.9.1/ -I stan/lib/stan_math/ -I stan/lib/stan_math/lib/eigen_3.4.0 -I stan/lib/stan_math/lib/boost_1.78.0 -I stan/lib/stan_math/lib/sundials_6.1.1/include -I stan/lib/stan_math/lib/sundials_6.1.1/src/sundials -I/opt/R/arm64/include    -DBOOST_DISABLE_ASSERTS          -c -include-pch stan/src/stan/model/model_header.hpp.gch -x c++ -o /var/folders/4v/vh7xp8553lsbl49svl48g7p00000gp/T/RtmpUAgcPM/model-b6944df95fca.o /var/folders/4v/vh7xp8553lsbl49svl48g7p00000gp/T/RtmpUAgcPM/model-b6944df95fca.hpp
#> error: Default alignment for functions differs in PCH file vs. current file
#> error: Default alignment for functions differs in PCH file vs. current file
#> 
#> 1 error generated.
#> make: *** [/var/folders/4v/vh7xp8553lsbl49svl48g7p00000gp/T/RtmpUAgcPM/model-b6944df95fca] Error 1
#> 1 error generated.
#> make: *** [/var/folders/4v/vh7xp8553lsbl49svl48g7p00000gp/T/RtmpUAgcPM/model-b6944df95fca] Error 1
#> 
#> Error: An error occured during compilation! See the message above for more information.
#> In addition: Warning message:
#> CmdStan's precompiled header (PCH) files may need to be rebuilt.
#> If your model failed to compile please run rebuild_cmdstan().
#> If the issue persists please open a bug report. 
#> Execution halted
#> Could not compile Stan models.
#> ERROR: configuration failed for package ‘example’
#> * removing ‘/Users/C240390/projects/instantiate/library/example’
#> Warning in install.packages :
#>   installation of package ‘example’ had non-zero exit status

Manually rebuilding CmdStan makes it work, but having to do that defeats the purpose of instantiate. This is definitely related to #1.

wlandau commented 1 year ago

Indeed, commenting out the manually-inserted CXXFLAGS="$CXXFLAGS -falign-functions=1" line in configure made it work. But omitting it broke compilation before. All this makes #1 really important.