-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFindR.cmake
172 lines (141 loc) · 4.58 KB
/
FindR.cmake
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# Copyright (c) 2017-2024 King Abdullah University of Science and Technology,
# All rights reserved.
# ExaGeoStat is a software package, provided by King Abdullah University of Science and Technology (KAUST).
# @file FindR.cmake
# @brief Find the R and Rcpp library, Set some helpful variables.
# @version 1.1.0
# @author Mahmoud ElKarargy
# @author David Helmy
# @date 2024-01-14
# Usage:
# find_package(R [REQUIRED] [QUIET] )
#
# It sets the following variables:
# R _FOUND ... true if R and Rcpp is found on the system
# R_LIBRARIES ... full path to R and Rcpp library
# R_INCLUDE_DIRS ... R and Rcpp include directory
#
# The following variables will be checked by the function
# R_ROOT_PATH ... if set, the R libraries are exclusively searched
# under this path
# R_LIB_PATH ... if set, the Rcpp libraries are exclusively searched
# under this path
if (DEFINED ENV{R_HOME})
set(R_ROOT_PATH "$ENV{R_HOME}")
else ()
execute_process(COMMAND R RHOME OUTPUT_VARIABLE R_HOME)
string(REGEX REPLACE "\n" "" R_HOME "${R_HOME}")
set(R_ROOT_PATH "${R_HOME}")
endif ()
if (NOT R_INCLUDE_PATH)
execute_process(COMMAND ${R_ROOT_PATH}/bin/Rscript -e "cat(Sys.getenv('R_INCLUDE_DIR'))" OUTPUT_VARIABLE R_INCLUDE_DIR)
string(REGEX REPLACE "\n" "" R_INCLUDE_DIR "${R_INCLUDE_DIR}")
set(R_INCLUDE_PATH "${R_INCLUDE_DIR}")
message(STATUS "R Include Path : " ${R_INCLUDE_PATH})
endif ()
if (NOT RCPP_LIB_PATH)
execute_process(
COMMAND ${R_ROOT_PATH}/bin/Rscript -e "cat(find.package('Rcpp'))"
OUTPUT_VARIABLE RCPP_LIB_PATH
RESULT_VARIABLE RSCRIPT_RESULT
)
# Check if the command was successful
if (RSCRIPT_RESULT EQUAL 0)
# Trim whitespace from both ends of the output
string(STRIP ${RCPP_LIB_PATH} RCPP_LIB_PATH)
message(STATUS "RCPP_LIB_PATH: ${RCPP_LIB_PATH}")
else()
message(FATAL_ERROR "Error running Rscript. Exit code: ${RSCRIPT_RESULT}")
endif()
endif ()
message(STATUS "R Home Path : " ${R_ROOT_PATH})
if (R_ROOT_PATH)
if (APPLE)
find_library(
R_DYN_LIB
NAMES "libR.dylib"
PATHS ${R_ROOT_PATH}
PATH_SUFFIXES "lib" "lib64" "bin"
NO_DEFAULT_PATH
)
else ()
#find libs
find_library(
R_DYN_LIB
NAMES "libR.so"
PATHS ${R_ROOT_PATH}
PATH_SUFFIXES "lib" "lib64" "bin"
NO_DEFAULT_PATH
)
endif ()
if (R_DYN_LIB MATCHES R_DYN_LIB-NOTFOUND)
set(R_DYN_LIB "")
message("R is built with no dynamic library support")
endif ()
set(R_LIB
${R_DYN_LIB}
)
else ()
error("R is not installed ")
endif (R_ROOT_PATH)
if (R_INCLUDE_PATH)
# find includes
find_path(
R_INCLUDE_DIRS
REQUIRED
NAMES "R.h"
PATHS ${R_INCLUDE_PATH}
PATH_SUFFIXES "include"
NO_DEFAULT_PATH
)
endif ()
if (RCPP_LIB_PATH)
#find libs
find_library(
RCPP_LIB
REQUIRED
NAMES "Rcpp.so"
PATHS ${RCPP_LIB_PATH}
PATH_SUFFIXES "/libs" "/lib64" "/bin"
NO_DEFAULT_PATH
)
# find includes
find_path(
RCPP_INCLUDE_DIRS
REQUIRED
NAMES "Rcpp.h"
PATHS ${RCPP_LIB_PATH}
PATH_SUFFIXES "/include"
NO_DEFAULT_PATH
)
else ()
message("Rcpp is not installed ...")
endif (RCPP_LIB_PATH)
set(R_INCLUDE
${R_INCLUDE}
${R_INCLUDE_DIRS}
${RCPP_INCLUDE_DIRS}
)
add_library(R INTERFACE IMPORTED)
if (R_LIB)
set_target_properties(R
PROPERTIES INTERFACE_INCLUDE_DIRECTORIES "${R_INCLUDE}"
INTERFACE_LINK_LIBRARIES "${R_LIB}"
IMPORTED_LOCATION ${RCPP_LIB}
)
include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(R DEFAULT_MSG
R_INCLUDE R_LIB)
include_directories(${R_INCLUDE})
mark_as_advanced(R_INCLUDE R_INCLUDE_DIRS RCPP_INCLUDE_DIRS R_LIB RCPP_LIB)
else ()
set_target_properties(R
PROPERTIES INTERFACE_INCLUDE_DIRECTORIES "${R_INCLUDE}"
IMPORTED_LOCATION ${RCPP_LIB}
)
include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(R DEFAULT_MSG
R_INCLUDE)
include_directories(${R_INCLUDE})
mark_as_advanced(R_INCLUDE R_INCLUDE_DIRS RCPP_INCLUDE_DIRS RCPP_LIB)
endif ()