r-lib / pkgbuild

Find tools needed to build R packages
https://pkgbuild.r-lib.org
Other
66 stars 35 forks source link

has_compiler is not detecting compiler #63

Closed bhogan-mitre closed 5 years ago

bhogan-mitre commented 5 years ago

I am having a disconnect with the result of has_compiler. Are there some environment variables that need to be set in order for this to work properly?

has_compiler shows false:

> has_compiler()
[1] FALSE

But yet I can install packages from source, for example Rcpp:

> install.packages("Rcpp", type = "source")
trying URL 'https://cran.rstudio.com/src/contrib/Rcpp_1.0.0.tar.gz'
Content type 'application/x-gzip' length 3635277 bytes (3.5 MB)
==================================================
downloaded 3.5 MB

* installing *source* package ‘Rcpp’ ...
** package ‘Rcpp’ successfully unpacked and MD5 sums checked
** libs
clang++  -I"/Library/Frameworks/R.framework/Resources/include" -DNDEBUG -I../inst/include/  -I/usr/local/include   -fPIC  -Wall -g -O2  -c Date.cpp -o Date.o
clang++  -I"/Library/Frameworks/R.framework/Resources/include" -DNDEBUG -I../inst/include/  -I/usr/local/include   -fPIC  -Wall -g -O2  -c Module.cpp -o Module.o
clang++  -I"/Library/Frameworks/R.framework/Resources/include" -DNDEBUG -I../inst/include/  -I/usr/local/include   -fPIC  -Wall -g -O2  -c Rcpp_init.cpp -o Rcpp_init.o
clang++  -I"/Library/Frameworks/R.framework/Resources/include" -DNDEBUG -I../inst/include/  -I/usr/local/include   -fPIC  -Wall -g -O2  -c api.cpp -o api.o
clang++  -I"/Library/Frameworks/R.framework/Resources/include" -DNDEBUG -I../inst/include/  -I/usr/local/include   -fPIC  -Wall -g -O2  -c attributes.cpp -o attributes.o
clang++  -I"/Library/Frameworks/R.framework/Resources/include" -DNDEBUG -I../inst/include/  -I/usr/local/include   -fPIC  -Wall -g -O2  -c barrier.cpp -o barrier.o
clang++ -dynamiclib -Wl,-headerpad_max_install_names -undefined dynamic_lookup -single_module -multiply_defined suppress -L/Library/Frameworks/R.framework/Resources/lib -L/usr/local/lib -o Rcpp.so Date.o Module.o Rcpp_init.o api.o attributes.o barrier.o -F/Library/Frameworks/R.framework/.. -framework R -Wl,-framework -Wl,CoreFoundation
installing to /Library/Frameworks/R.framework/Versions/3.5/Resources/library/Rcpp/libs
** R
** inst
** byte-compile and prepare package for lazy loading
** help
*** installing help indices
** building package indices
** installing vignettes
** testing if installed package can be loaded
* DONE (Rcpp)

Session info:

> sessionInfo()
R version 3.5.2 (2018-12-20)
Platform: x86_64-apple-darwin15.6.0 (64-bit)
Running under: macOS Mojave 10.14.3

Matrix products: default
BLAS: /Library/Frameworks/R.framework/Versions/3.5/Resources/lib/libRblas.0.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/3.5/Resources/lib/libRlapack.dylib

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

loaded via a namespace (and not attached):
 [1] Rcpp_1.0.0          ps_1.3.0            prettyunits_1.0.2   rprojroot_1.3-2     digest_0.6.18       crayon_1.3.4        withr_2.1.2         assertthat_0.2.0   
 [9] R6_2.3.0            backports_1.1.3     magrittr_1.5        rlang_0.3.1         cli_1.0.1           fs_1.2.6            remotes_2.0.2       testthat_2.0.1     
[17] callr_3.1.1         desc_1.2.0          devtools_2.0.1.9000 tools_3.5.2         glue_1.3.0          pkgload_1.0.2       compiler_3.5.2      processx_3.2.1     
[25] pkgbuild_1.0.2      sessioninfo_1.1.1   memoise_1.1.0       usethis_1.4.0      

Thanks for any suggestions you could provide.

jimhester commented 5 years ago

Can you run it with debug = TRUE, e.g. pkgbuild::has_compiler(debug = TRUE)?

Also Rcpp is a C++ package and has_compiler() is checking C compilation, so it is possible that your C compiler is not working but the C++ one is.

bhogan-mitre commented 5 years ago

Thanks for the follow up.

> pkgbuild::has_compiler(debug = TRUE)
Trying to compile a simple C file
Running /Library/Frameworks/R.framework/Resources/bin/R CMD SHLIB foo.c
LDFLAGS=-L/usr/local/opt/gettext/lib -L/usr/local/opt/llvm/lib -Wl,-rpath,/usr/local/opt/llvm/lib  -I"/Library/Frameworks/R.framework/Resources/include" -DNDEBUG   -I/usr/local/include   -fPIC  -Wall -g -O2  -c foo.c -o foo.o
CFLAGS=-g: No such file or directory
make: *** [foo.o] Error 127
[1] FALSE

Let me know if there is a better test of the setup for C compilation.

jimhester commented 5 years ago

You seem to have no compiler set, did you change the CC environment variable?

bhogan-mitre commented 5 years ago

I hadn't changed it, or intentionally set it either.

> Sys.getenv("CC")
[1] ""

Explicitly setting to clang did not seem to make a difference.

> Sys.setenv(CC = "/usr/local/opt/llvm/bin/clang")
> Sys.getenv("CC")
[1] "/usr/local/opt/llvm/bin/clang"
> pkgbuild::has_compiler(debug = TRUE)
Trying to compile a simple C file
Running /Library/Frameworks/R.framework/Resources/bin/R CMD SHLIB foo.c
LDFLAGS=-L/usr/local/opt/gettext/lib -L/usr/local/opt/llvm/lib -Wl,-rpath,/usr/local/opt/llvm/lib  -I"/Library/Frameworks/R.framework/Resources/include" -DNDEBUG   -I/usr/local/include   -fPIC  -Wall -g -O2  -c foo.c -o foo.o
CFLAGS=-g: No such file or directory
make: *** [foo.o] Error 127
[1] FALSE

Here is one clue as to the disconnect: calling which clang from R returns a different result than from a terminal (even the RStudio terminal).

> system("which clang")
/usr/bin/clang

vs.

bhogan$ which clang
/usr/local/opt/llvm/bin/clang
bhogan-mitre commented 5 years ago

Okay, I am back in business here.

> pkgbuild::has_compiler(debug = TRUE)
Trying to compile a simple C file
Running /Library/Frameworks/R.framework/Resources/bin/R CMD SHLIB foo.c
clang -I"/Library/Frameworks/R.framework/Resources/include" -DNDEBUG   -I/usr/local/include   -fPIC  -Wall -g -O2  -c foo.c -o foo.o
clang -dynamiclib -Wl,-headerpad_max_install_names -undefined dynamic_lookup -single_module -multiply_defined suppress -L/Library/Frameworks/R.framework/Resources/lib -L/usr/local/lib -o foo.so foo.o -F/Library/Frameworks/R.framework/.. -framework R -Wl,-framework -Wl,CoreFoundation

[1] TRUE

Nothing needed to change with the compiler itself. I just had a lingering old ~/.R/Makevars around, presumably from an old Xcode install. This Q&A did the trick https://stackoverflow.com/a/49268991/4425601