-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
build_r.R
96 lines (84 loc) · 2.45 KB
/
build_r.R
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
# For macOS users who have decided to use gcc
# (replace 8 with version of gcc installed on your machine)
# NOTE: your gcc / g++ from Homebrew is probably in /usr/local/bin
#export CXX=/usr/local/bin/g++-8 CC=/usr/local/bin/gcc-8
# Sys.setenv("CXX" = "/usr/local/bin/g++-8")
# Sys.setenv("CC" = "/usr/local/bin/gcc-8")
args <- commandArgs(trailingOnly = TRUE)
INSTALL_AFTER_BUILD <- !("--skip-install" %in% args)
# R returns FALSE (not a non-zero exit code) if a file copy operation
# breaks. Let's fix that
.handle_result <- function(res) {
if (!res) {
stop("Copying files failed!")
}
}
# system() will not raise an R exception if the process called
# fails. Wrapping it here to get that behavior
.run_shell_command <- function(cmd, ...) {
exit_code <- system(cmd, ...)
if (exit_code != 0L) {
stop(paste0("Command failed with exit code: ", exit_code))
}
}
# Make a new temporary folder to work in
unlink(x = "lightgbm_r", recursive = TRUE)
dir.create("lightgbm_r")
# copy in the relevant files
result <- file.copy(
from = "R-package/./"
, to = "lightgbm_r/"
, recursive = TRUE
, overwrite = TRUE
)
.handle_result(result)
result <- file.copy(
from = "include/"
, to = file.path("lightgbm_r", "src/")
, recursive = TRUE
, overwrite = TRUE
)
.handle_result(result)
result <- file.copy(
from = "src/"
, to = file.path("lightgbm_r", "src/")
, recursive = TRUE
, overwrite = TRUE
)
.handle_result(result)
result <- file.copy(
from = "compute/"
, to = file.path("lightgbm_r", "src/")
, recursive = TRUE
, overwrite = TRUE
)
.handle_result(result)
result <- file.copy(
from = "CMakeLists.txt"
, to = file.path("lightgbm_r", "inst", "bin/")
, overwrite = TRUE
)
.handle_result(result)
# Build the package (do not touch this line!)
# NOTE: --keep-empty-dirs is necessary to keep the deep paths expected
# by CMake while also meeting the CRAN req to create object files
# on demand
cmd <- "R CMD build lightgbm_r --keep-empty-dirs"
.run_shell_command(cmd)
# Install the package
version <- gsub(
"Version: ",
"",
grep(
"Version: "
, readLines(con = file.path("lightgbm_r", "DESCRIPTION"))
, value = TRUE
)
)
tarball <- file.path(getwd(), sprintf("lightgbm_%s.tar.gz", version))
cmd <- sprintf("R CMD INSTALL %s --no-multiarch --with-keep.source", tarball)
if (INSTALL_AFTER_BUILD) {
.run_shell_command(cmd)
} else {
print(sprintf("Skipping installation. Install the package with command '%s'", cmd))
}