miraisolutions / rTRNG

R package providing access and examples to TRNG C++ library
GNU General Public License v3.0
19 stars 5 forks source link

Problems building/loading package using rTRNG under macOS #13

Open richfitz opened 4 years ago

richfitz commented 4 years ago

I am trying to use rTRNG within a package and hitting an issue with the linking. Following #8 (which is probably the same underlying issue) have have created a basic skeleton following your instructions, which is here: https://github.com/richfitz/pkgUsingTRNG (unchanged from the result of running the demo script).

This appears to build, but does not load for me on macOS with the error:

$ R CMD INSTALL pkgUsingTRNG
* installing to library ‘/Library/Frameworks/R.framework/Versions/3.6/Resources/library’
* installing *source* package ‘pkgUsingTRNG’ ...
** using staged installation
** libs
clang++ -std=gnu++11 -I"/Library/Frameworks/R.framework/Resources/include" -DNDEBUG  -I"/Library/Frameworks/R.framework/Versions/3.6/Resources/library/Rcpp/include" -I"/Library/Frameworks/R.framework/Versions/3.6/Resources/library/rTRNG/include" -isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk -I/usr/local/include  -fPIC  -Wall -g -O2  -c RcppExports.cpp -o RcppExports.o
clang++ -std=gnu++11 -I"/Library/Frameworks/R.framework/Resources/include" -DNDEBUG  -I"/Library/Frameworks/R.framework/Versions/3.6/Resources/library/Rcpp/include" -I"/Library/Frameworks/R.framework/Versions/3.6/Resources/library/rTRNG/include" -isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk -I/usr/local/include  -fPIC  -Wall -g -O2  -c trng_hello_world.cpp -o trng_hello_world.o
clang++ -std=gnu++11 -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 pkgUsingTRNG.so RcppExports.o trng_hello_world.o /Library/Frameworks/R.framework/Versions/3.6/Resources/library/rTRNG/libs/rTRNG.so -F/Library/Frameworks/R.framework/.. -framework R -Wl,-framework -Wl,CoreFoundation
installing to /Library/Frameworks/R.framework/Versions/3.6/Resources/library/00LOCK-pkgUsingTRNG/00new/pkgUsingTRNG/libs
** R
** byte-compile and prepare package for lazy loading
** help
*** installing help indices
** building package indices
** testing if installed package can be loaded from temporary location
Error: package or namespace load failed for ‘pkgUsingTRNG’ in dyn.load(file, DLLpath = DLLpath, ...):
 unable to load shared object '/Library/Frameworks/R.framework/Versions/3.6/Resources/library/00LOCK-pkgUsingTRNG/00new/pkgUsingTRNG/libs/pkgUsingTRNG.so':
  dlopen(/Library/Frameworks/R.framework/Versions/3.6/Resources/library/00LOCK-pkgUsingTRNG/00new/pkgUsingTRNG/libs/pkgUsingTRNG.so, 6): Library not loaded: rTRNG.so
  Referenced from: /Library/Frameworks/R.framework/Versions/3.6/Resources/library/00LOCK-pkgUsingTRNG/00new/pkgUsingTRNG/libs/pkgUsingTRNG.so
  Reason: image not found
Error: loading failed
Execution halted
ERROR: loading failed
* removing ‘/Library/Frameworks/R.framework/Versions/3.6/Resources/library/pkgUsingTRNG’

(this is on R 3.6.1 on macOS 10.14.6 using the CRAN version of rTRNG)

In contrast this works fine on ubuntu (tested via docker using rocker/r-ver:4.0.0):

# R CMD INSTALL pkgUsingTRNG
* installing to library ‘/usr/local/lib/R/site-library’
* installing *source* package ‘pkgUsingTRNG’ ...
** using staged installation
** libs
g++ -std=gnu++11 -I"/usr/local/lib/R/include" -DNDEBUG  -I'/usr/local/lib/R/site-library/Rcpp/include' -I'/usr/local/lib/R/site-library/rTRNG/include' -I/usr/local/include   -fpic  -g -O2 -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -g  -c RcppExports.cpp -o RcppExports.o
g++ -std=gnu++11 -I"/usr/local/lib/R/include" -DNDEBUG  -I'/usr/local/lib/R/site-library/Rcpp/include' -I'/usr/local/lib/R/site-library/rTRNG/include' -I/usr/local/include   -fpic  -g -O2 -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -g  -c trng_hello_world.cpp -o trng_hello_world.o
g++ -std=gnu++11 -shared -L/usr/local/lib/R/lib -L/usr/local/lib -o pkgUsingTRNG.so RcppExports.o trng_hello_world.o /usr/local/lib/R/site-library/rTRNG/libs/rTRNG.so -L/usr/local/lib/R/lib -lR
installing to /usr/local/lib/R/site-library/00LOCK-pkgUsingTRNG/00new/pkgUsingTRNG/libs
** R
** byte-compile and prepare package for lazy loading
** help
*** installing help indices
** building package indices
** testing if installed package can be loaded from temporary location
** checking absolute paths in shared objects and dynamic libraries
** testing if installed package can be loaded from final location
** testing if installed package keeps a record of temporary installation path
* DONE (pkgUsingTRNG)

I have not yet tested this on windows.

Do you have any ideas as to why this might not work? Has this been found to work on macOS previously?

RolandASc commented 4 years ago

I think we are aware and don't really have a solution yet (we were also not sure if this case was really affecting anyone and whether it was worth doing something about it).

See also #10 and the related PR #11, as part of which the documentation has been updated to reflect this problem with MacOS (this should go into an upcoming new release of rTRNG, which is pending some valgrind investigations but hopefully not too far away).

@riccardoporreca might want to add something?

richfitz commented 4 years ago

Thanks for the response - I can confirm that on my mac rTRNG::check_rTRNG_linking() fails in the new version. If this is fixable we would try and use your package, otherwise we'll have to look at bundling trng too (or using something else), but your package looked very convenient!

riccardoporreca commented 4 years ago

@richfitz, I plan to go back to this in July to invetigate alternative ways of making a shared / dynamic library with TRNG available for linking in other R projects on macOS, where the library built as part of a standard R package installation cannot be linked against.

If you have a specific R project where you want to start using the TRNG library in C++ code, for the time being you can easily set it up as an R package with inst/include/trng, src/trng and src/Makevars same as in rTRNG. Any custom C++ code under src using TRNG will then work, similar to src/rdist.cpp. This is actually how we started using TRNG in an R project before deciding to make the rTRNG package.