Skip to content

Commit

Permalink
Setup R-directory-structure using cmake.
Browse files Browse the repository at this point in the history
  • Loading branch information
Yashwants19 committed Jul 15, 2020
1 parent 8e90655 commit 862736f
Show file tree
Hide file tree
Showing 48 changed files with 4,207 additions and 48 deletions.
104 changes: 104 additions & 0 deletions CMake/FindR.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
# -*- cmake -*-
#
# FindR.cmake: Try to find R
#
# (C) Copyright 2005-2012 EDF-EADS-Phimeca
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License.
#
# This library is distributed in the hope that it will be useful
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
# @author dutka
# @date 2010-02-04 16:44:49 +0100 (Thu, 04 Feb 2010)
# Id Makefile.am 1473 2010-02-04 15:44:49Z dutka
#
#
# - Try to find R
# Once done this will define
#
# R_FOUND - System has R
# R_LIBRARIES - The libraries needed to use R
# R_DEFINITIONS - Compiler switches required for using R
# R_EXECUTABLE - The R interpreter


if ( R_EXECUTABLE AND R_LIBRARIES )
# in cache already
set( R_FIND_QUIETLY TRUE )
endif ( R_EXECUTABLE AND R_LIBRARIES )

#IF (NOT WIN32)
# # use pkg-config to get the directories and then use these values
# # in the FIND_PATH() and FIND_LIBRARY() calls
# FIND_PACKAGE(PkgConfig)
# PKG_CHECK_MODULES(PC_R R)
# SET(R_DEFINITIONS ${PC_R_CFLAGS_OTHER})
#ENDIF (NOT WIN32)

find_program ( R_EXECUTABLE
NAMES R R.exe
DOC "Path to the R command interpreter"
)

get_filename_component ( _R_EXE_PATH ${R_EXECUTABLE} PATH )

if ( R_EXECUTABLE )
execute_process ( COMMAND ${R_EXECUTABLE} RHOME
OUTPUT_VARIABLE _R_HOME
OUTPUT_STRIP_TRAILING_WHITESPACE
)

execute_process ( COMMAND ${R_EXECUTABLE} CMD config --cppflags
OUTPUT_VARIABLE R_CXX_FLAGS
OUTPUT_STRIP_TRAILING_WHITESPACE
)

endif ( R_EXECUTABLE )

find_library ( R_LIBRARIES
NAMES R
HINTS
${_R_HOME}/lib
${_R_HOME}/lib/x86_64
)


set ( R_PACKAGES )
if ( R_EXECUTABLE )
foreach ( _component ${R_FIND_COMPONENTS} )
if ( NOT R_${_component}_FOUND )
execute_process ( COMMAND echo "library(${_component})"
COMMAND ${R_EXECUTABLE} --no-save --silent --no-readline --slave
RESULT_VARIABLE _res
OUTPUT_VARIABLE _trashout
ERROR_VARIABLE _trasherr
)
if ( NOT _res )
message ( STATUS "Looking for R package ${_component} - found" )
set ( R_${_component}_FOUND 1 CACHE INTERNAL "True if R package ${_component} is here" )
else ( NOT _res )
message ( STATUS "Looking for R package ${_component} - not found" )
set ( R_${_component}_FOUND 0 CACHE INTERNAL "True if R package ${_component} is here" )
endif ( NOT _res )
list ( APPEND R_PACKAGES R_${_component}_FOUND )
endif ( NOT R_${_component}_FOUND )
endforeach ( _component )
endif ( R_EXECUTABLE )

include ( FindPackageHandleStandardArgs )

# handle the QUIETLY and REQUIRED arguments and set R_FOUND to TRUE if
# all listed variables are TRUE
find_package_handle_standard_args ( R DEFAULT_MSG R_EXECUTABLE R_LIBRARIES R_CXX_FLAGS ${R_PACKAGES} )

mark_as_advanced ( R_EXECUTABLE R_LIBRARIES R_CXX_FLAGS ${R_PACKAGES} )
10 changes: 9 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,19 @@ else()
endif()
option(BUILD_GO_BINDINGS "Build Go bindings." ON)

# If building Go bindings then build go shared libraries.
# If building Go bindings then build go shared libraries.
if (BUILD_GO_BINDINGS)
set(BUILD_GO_SHLIB ON)
endif()

# Detect whether the user passed BUILD_R_BINDINGS in order to determine if
# we should fail if R isn't found.
if (BUILD_R_BINDINGS)
set(FORCE_BUILD_R_BINDINGS ON)
else()
set(FORCE_BUILD_R_BINDINGS OFF)
endif()
option(BUILD_R_BINDINGS "Build R bindings." ON)
# Build Markdown bindings for documentation. This is used as part of website
# generation.
option(BUILD_MARKDOWN_BINDINGS "Build Markdown bindings for website documentation." OFF)
Expand Down
61 changes: 58 additions & 3 deletions src/mlpack/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,6 @@ if (BUILD_JULIA_BINDINGS)
"\nend\ninclude(\"functions.jl\")\ninclude(\"serialization.jl\")\nend\n")
endif ()


# If we are building Go bindings, we have to end the 'module' declaration in
# models.go
if (BUILD_GO_BINDINGS)
Expand All @@ -179,6 +178,62 @@ if (BUILD_GO_BINDINGS)
endforeach()
endif()

# In Case of R_Bindings Move all of these header and source files to
# <package>/src/mlpack/.
if (BUILD_R_BINDINGS)
# Collect all header and source files in the library.
file(GLOB_RECURSE R_SRC_HPP_FILES RELATIVE "${CMAKE_CURRENT_SOURCE_DIR}" *.hpp)
file(GLOB_RECURSE R_SRC_CPP_FILES RELATIVE "${CMAKE_CURRENT_SOURCE_DIR}" *.cpp)
set(R_SRC_FILES ${R_SRC_HPP_FILES} ${R_SRC_CPP_FILES} PARENT SCOPE)

# First we have to create that directory though.
execute_process(
COMMAND ${CMAKE_COMMAND} -E
make_directory ${CMAKE_BINARY_DIR}/src/mlpack/bindings/R/mlpack/src/mlpack
)

# Then copy each of the header and source files over to that directory.
set(MLPACK_SOURCES
"${CMAKE_BINARY_DIR}/src/mlpack/mlpack_export.hpp"
"${CMAKE_CURRENT_SOURCE_DIR}/prereqs.hpp"
"${CMAKE_CURRENT_SOURCE_DIR}/core.hpp"
)

foreach(mlpack_sources ${MLPACK_SOURCES})
execute_process(
COMMAND ${CMAKE_COMMAND} -E copy_if_different
${mlpack_sources}
${CMAKE_BINARY_DIR}/src/mlpack/bindings/R/mlpack/src/mlpack
)
endforeach()

foreach(r_src_file ${R_SRC_FILES})
if ("${r_src_file}" MATCHES "methods/" OR
"${r_src_file}" MATCHES "core/")
execute_process(
COMMAND ${CMAKE_COMMAND} -E copy_if_different
${CMAKE_CURRENT_SOURCE_DIR}/${r_src_file}
${CMAKE_BINARY_DIR}/src/mlpack/bindings/R/mlpack/src/mlpack/${r_src_file}
)

# Collect path of all source files and append to ${R_SRC}.
if ("${r_src_file}" MATCHES ".cpp" AND
NOT "${r_src_file}" MATCHES "main.cpp" AND
# Note: this implementation of MVU does not work. See #189.
NOT "${r_src_file}" MATCHES "mvu")
string(APPEND R_SRC "#include <mlpack/${r_src_file}>\n")
endif()

endif()
endforeach()

# Then configure 'mlpack.h.in' using ${R_SRC}.
configure_file(
${CMAKE_SOURCE_DIR}/src/mlpack/bindings/R/mlpack/inst/include/mlpack.h.in
${CMAKE_BINARY_DIR}/src/mlpack/bindings/R/mlpack/inst/include/mlpack.h
@ONLY)
endif()

# If we are building Markdown documentation, we have to run some setup after we
# recurse into methods/. If not, this function is empty.
post_markdown_setup()
Expand Down Expand Up @@ -206,7 +261,7 @@ set_verbose(MLPACK_INCS_DIR ${CMAKE_INSTALL_INCLUDEDIR} CACHE STRING
"Installation directory for include files, relative to "
"${CMAKE_INSTALL_PREFIX}.")

# Getting the absolute path, for some reason CMAKE gets confused,
# Getting the absolute path, for some reason CMAKE gets confused,
# even with CMAKE_INSTALL_INCLUDEDIR
if (UNIX)
GNUInstallDirs_get_absolute_install_dir(MLPACK_LIBS_DIR CMAKE_INSTALL_LIBDIR)
Expand All @@ -230,7 +285,7 @@ export(TARGETS ${INSTALL_TARGETS} NAMESPACE mlpack::
FILE ${PROJECT_BINARY_DIR}/${TARGETS_EXPORT_NAME}.cmake)

# Install version, config and target files.
install(FILES ${PROJECT_CONFIG} ${VERSION_CONFIG}
install(FILES ${PROJECT_CONFIG} ${VERSION_CONFIG}
DESTINATION ${MLPACK_CMAKE_DIR})

install(EXPORT ${TARGETS_EXPORT_NAME}
Expand Down
1 change: 1 addition & 0 deletions src/mlpack/bindings/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ set(DIRS
markdown
python
go
R
tests
)

Expand Down
Loading

0 comments on commit 862736f

Please sign in to comment.