Warning, /education/cantor/cmake/FindR.cmake is written in an unsupported language. File is not indexed.

0001 # - Try to find R
0002 # Once done this will define
0003 #
0004 #  R_FOUND - system has R
0005 #  R_EXECUTABLE - executable of R
0006 #  R_HOME - home directory of R
0007 #  R_INCLUDE_DIR - the R include directory
0008 #  R_LIBRARIES - Link these to use R
0009 
0010 # find the R binary
0011 FIND_PROGRAM(R_EXECUTABLE NAMES R R.bat)
0012 
0013 SET(ABORT_CONFIG FALSE)
0014 IF(R_EXECUTABLE)
0015 
0016   # find R_HOME
0017   IF(NOT R_HOME)
0018     EXECUTE_PROCESS(
0019       COMMAND ${R_EXECUTABLE} "--slave" "--no-save" "-e" "cat(R.home())"
0020       OUTPUT_VARIABLE R_HOME)
0021   ENDIF(NOT R_HOME)
0022   IF(NOT R_HOME)
0023     MESSAGE(STATUS "Could NOT determine R_HOME (probably you misspecified the location of R)")
0024   ENDIF(NOT R_HOME)
0025 
0026   IF(WIN32)
0027     # remove R.bat header from R_HOME
0028     STRING(REGEX REPLACE ".*\n" "" R_HOME "${R_HOME}")
0029     # search for correct exe in R_HOME (R.bat is not working)
0030     unset(R_EXECUTABLE CACHE)
0031     FIND_PROGRAM(R_EXECUTABLE R HINTS ${R_HOME}/bin ${R_HOME}/bin/x64)
0032   ENDIF()
0033 
0034   # find R include dir
0035   IF(NOT R_INCLUDE_DIR)
0036     IF(WIN32)    # This version of the test will not work with R < 2.9.0, but the other version (in the else part) will not work on windows (and on windows the paths are generally standard, anyway).
0037       EXECUTE_PROCESS(
0038         COMMAND ${R_EXECUTABLE} "--slave" "--no-save" "-e" "cat(R.home('include'))"
0039         OUTPUT_VARIABLE R_INCLUDE_DIR)
0040     ELSE(WIN32)
0041       EXECUTE_PROCESS(
0042         COMMAND ${R_EXECUTABLE} CMD sh -c "printf $R_INCLUDE_DIR"
0043         OUTPUT_VARIABLE R_INCLUDE_DIR)
0044     ENDIF(WIN32)
0045   ENDIF(NOT R_INCLUDE_DIR)
0046 
0047   IF(NOT R_INCLUDE_DIR)
0048     SET(R_INCLUDE_DIR ${R_HOME}/include)
0049     MESSAGE(STATUS "R_Home not findable via R. Guessing")
0050   ENDIF(NOT R_INCLUDE_DIR)
0051 
0052   FIND_PATH(R_INCLUDE_DIR R.h)
0053 
0054   # check for existence of libR.so/R.dll
0055   IF(WIN32)
0056     set(CMAKE_FIND_LIBRARY_SUFFIXES ".lib" ".dll")
0057   ENDIF()
0058 
0059   FIND_LIBRARY(R_R_LIBRARY R
0060     HINTS ${R_HOME}/lib ${R_SHARED_LIB_DIR} ${R_HOME}/bin ${R_HOME}/bin/x64)
0061   IF(NOT R_R_LIBRARY)
0062     MESSAGE(STATUS "libR/R.dll not found. Make sure the location of R was detected correctly, above, and R was compiled with the --enable-shlib option")
0063   ELSE(NOT R_R_LIBRARY)
0064     GET_FILENAME_COMPONENT(R_SHARED_LIB_DIR ${R_R_LIBRARY}
0065       PATH)
0066     SET(R_LIBRARIES ${R_R_LIBRARY})
0067   ENDIF(NOT R_R_LIBRARY)
0068 
0069   # for at least some versions of R, we seem to have to link against -lRlapack. Else loading some
0070   # R packages will fail due to unresolved symbols, or we can't link against -lR.
0071   # However, we can't do this unconditionally,
0072   # as this is not available in some configurations of R
0073 
0074   FIND_LIBRARY(R_LAPACK_LIBRARY
0075     Rlapack
0076     HINTS ${R_SHARED_LIB_DIR} )
0077   IF(NOT R_LAPACK_LIBRARY)
0078     #MESSAGE(STATUS "No, it does not exist in ${R_SHARED_LIB_DIR}")
0079   ELSE(NOT R_LAPACK_LIBRARY)
0080     #MESSAGE(STATUS "Yes, ${R_LAPACK_LIBRARY} exists")
0081     SET(R_LIBRARIES ${R_LIBRARIES} ${R_LAPACK_LIBRARY})
0082     IF(NOT WIN32)
0083       # Query gfortran to get the libgfortran.so path
0084       FIND_PROGRAM(_GFORTRAN_EXECUTABLE NAMES gfortran)
0085       IF(_GFORTRAN_EXECUTABLE)
0086         EXECUTE_PROCESS(COMMAND ${_GFORTRAN_EXECUTABLE} -print-file-name=libgfortran.so
0087                         OUTPUT_VARIABLE _libgfortran_path
0088                         OUTPUT_STRIP_TRAILING_WHITESPACE
0089                        )
0090       ENDIF()
0091       IF(EXISTS ${_libgfortran_path})
0092         SET(GFORTRAN_LIBRARY ${_libgfortran_path})
0093       ELSE()
0094         # if libgfortran wasn't found at this point, the installation is probably broken
0095         # Let's try to find the library nonetheless.
0096         FIND_LIBRARY(GFORTRAN_LIBRARY gfortran)
0097       ENDIF()
0098       IF (GFORTRAN_LIBRARY)
0099         # needed when linking to Rlapack on linux for some unknown reason.
0100         # apparently not needed on windows (let's see, when it comes back to bite us, though)
0101         # and compiling on windows is hard enough even without requiring libgfortran, too.
0102         SET(R_LIBRARIES ${R_LIBRARIES} ${GFORTRAN_LIBRARY})
0103       ELSE (GFORTRAN_LIBRARY)
0104         MESSAGE(STATUS "gfortran is needed for Rlapack but it could not be found")
0105         SET(ABORT_CONFIG TRUE)
0106       ENDIF (GFORTRAN_LIBRARY)
0107     ENDIF(NOT WIN32)
0108   ENDIF(NOT R_LAPACK_LIBRARY)
0109 
0110   # for at least some versions of R, we seem to have to link against -lRlapack. Else loading some
0111   # R packages will fail due to unresolved symbols, or we can't link against -lR.
0112   # However, we can't do this unconditionally,
0113   # as this is not available in some configurations of R
0114 
0115   FIND_LIBRARY(R_BLAS_LIBRARY
0116     Rblas
0117     HINTS ${R_SHARED_LIB_DIR} )
0118   IF(NOT R_BLAS_LIBRARY)
0119     #MESSAGE(STATUS "No, it does not exist in ${R_SHARED_LIB_DIR}")
0120   ELSE(NOT R_BLAS_LIBRARY)
0121     #MESSAGE(STATUS "Yes, ${R_BLAS_LIBRARY} exists")
0122     SET(R_LIBRARIES ${R_LIBRARIES} ${R_BLAS_LIBRARY})
0123   ENDIF(NOT R_BLAS_LIBRARY)
0124 
0125 ENDIF( R_EXECUTABLE )
0126 
0127 IF(ABORT_CONFIG)
0128   SET(R_FOUND FALSE)
0129 ELSE(ABORT_CONFIG)
0130   INCLUDE(FindPackageHandleStandardArgs)
0131   FIND_PACKAGE_HANDLE_STANDARD_ARGS(R  DEFAULT_MSG
0132                                   R_EXECUTABLE R_INCLUDE_DIR R_R_LIBRARY)
0133 
0134   MARK_AS_ADVANCED(R_INCLUDE_DIR R_R_LIBRARY R_LAPACK_LIBRARY R_BLAS_LIBRARY)
0135 ENDIF(ABORT_CONFIG)