RPy2 fails to build and install (via pip) on OS X (10.8.5) against the current Homebrew R installation (and using brew's python); it cannot find libR:
ld: library not found for -lR clang: error: linker command failed with exit code 1 (use -v to see invocation)
As best as I can determine, this is due to problems with both Homebrew and RPy2.
Homebrew builds R as a framework; by default the framework build process does not produce a shared library and Homebrew does not set the required flag. Also, Homebrew does not expose an option to build the library.
According to this MacPorts post:
the R-Project binary build of R for OS X does build libR, so this should probably the default in Homebrew, or at least an exposed option. I've patched my brew r.rb formula to include the "--enable-R-shlib" flag, which builds libR.dylib within the R framework. But RPy2's setup.py still would not discover libR. By further patching the formula to link libR under brew's prefix (/usr/local/lib by default), RPy2 builds successfully. But this happened despite not linking the headers, which seemed fishy to me.
Pip's output suggests RPy2 is not consistently discovering the library location:
Configuration for R as a library: include_dirs: ('/usr/local/Cellar/r/3.1.0/R.framework/Resources/include',) libraries: ('lzma', 'iconv', 'icucore', 'm') library_dirs: () extra_link_args: ('-F/usr/local/Cellar/r/3.1.0/R.framework/..', '-framework R') # OSX-specific (included in extra_link_args) framework_dirs: ('-F/usr/local/Cellar/r/3.1.0/R.framework/..',) frameworks: ('-framework R',)
It finds the headers within the framework, but library_dirs is empty; I think it should point to within the framework.
I think RPy2 gets the headers from this R query:
"/usr/local/Cellar/r/3.1.0/R.framework/Resources/bin/R" CMD config --cppflags
Perhaps it is expecting to get the library path from the linker query:
"/usr/local/Cellar/r/3.1.0/R.framework/Resources/bin/R" CMD config --ldflags
But this returns:
-F/usr/local/Cellar/r/3.1.0/R.framework/.. -framework R -llzma -licucore -lm -liconv
which does not point to libR, presumably because the R runtime does not use the shared lib.
In setup.py, getRinterface_ext starts with:
def getRinterface_ext(): #r_libs = [os.path.join(RHOME, 'lib'), os.path.join(RHOME, 'modules')] r_libs = 
If RHOME were set to the value of "R RHOME" and the commented line were kept, I suspect the build would work. As it stands, I think getRinterface_ext will always have an empty r_libs. I presume the commented line is commented for a reason, so I haven't tried to fix this myself.
I will submit an issue to Homebrew about patching the R formula once I'm clear what the proper fix is on both the RPy2 and Homebrew sides.