2018-09-23 18:48:49 +03:00
|
|
|
# For macOS users who have decided to use gcc
|
2018-09-02 04:30:30 +03:00
|
|
|
# (replace 8 with version of gcc installed on your machine)
|
2018-08-29 07:31:42 +03:00
|
|
|
# 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")
|
|
|
|
|
2020-03-31 07:01:29 +03:00
|
|
|
args <- commandArgs(trailingOnly = TRUE)
|
|
|
|
INSTALL_AFTER_BUILD <- !("--skip-install" %in% args)
|
2020-05-25 05:23:38 +03:00
|
|
|
TEMP_R_DIR <- file.path(getwd(), "lightgbm_r")
|
|
|
|
TEMP_SOURCE_DIR <- file.path(TEMP_R_DIR, "src")
|
2020-03-31 07:01:29 +03:00
|
|
|
|
2018-08-29 07:31:42 +03:00
|
|
|
# R returns FALSE (not a non-zero exit code) if a file copy operation
|
|
|
|
# breaks. Let's fix that
|
2018-09-23 18:48:49 +03:00
|
|
|
.handle_result <- function(res) {
|
|
|
|
if (!res) {
|
|
|
|
stop("Copying files failed!")
|
|
|
|
}
|
2018-08-29 07:31:42 +03:00
|
|
|
}
|
|
|
|
|
2019-05-08 11:57:01 +03:00
|
|
|
# system() will not raise an R exception if the process called
|
2020-05-14 19:31:13 +03:00
|
|
|
# fails. Wrapping it here to get that behavior.
|
|
|
|
#
|
|
|
|
# system() introduces a lot of overhead, at least on Windows,
|
|
|
|
# so trying processx if it is available
|
|
|
|
.run_shell_command <- function(cmd, args, strict = TRUE) {
|
|
|
|
on_windows <- .Platform$OS.type == "windows"
|
|
|
|
has_processx <- suppressMessages({
|
|
|
|
suppressWarnings({
|
|
|
|
require("processx") # nolint
|
|
|
|
})
|
|
|
|
})
|
|
|
|
if (has_processx && on_windows) {
|
|
|
|
result <- processx::run(
|
|
|
|
command = cmd
|
|
|
|
, args = args
|
|
|
|
, windows_verbatim_args = TRUE
|
|
|
|
, error_on_status = FALSE
|
|
|
|
, echo = TRUE
|
|
|
|
)
|
|
|
|
exit_code <- result$status
|
|
|
|
} else {
|
|
|
|
if (on_windows) {
|
|
|
|
message(paste0(
|
|
|
|
"Using system() to run shell commands. Installing "
|
|
|
|
, "'processx' with install.packages('processx') might "
|
|
|
|
, "make this faster."
|
|
|
|
))
|
|
|
|
}
|
|
|
|
cmd <- paste0(cmd, " ", paste0(args, collapse = " "))
|
|
|
|
exit_code <- system(cmd)
|
|
|
|
}
|
|
|
|
|
|
|
|
if (exit_code != 0L && isTRUE(strict)) {
|
2019-05-08 11:57:01 +03:00
|
|
|
stop(paste0("Command failed with exit code: ", exit_code))
|
|
|
|
}
|
2020-05-14 19:31:13 +03:00
|
|
|
return(invisible(exit_code))
|
2019-05-08 11:57:01 +03:00
|
|
|
}
|
|
|
|
|
2018-08-29 07:31:42 +03:00
|
|
|
# Make a new temporary folder to work in
|
2020-05-25 05:23:38 +03:00
|
|
|
unlink(x = TEMP_R_DIR, recursive = TRUE)
|
|
|
|
dir.create(TEMP_R_DIR)
|
2018-08-29 07:31:42 +03:00
|
|
|
|
|
|
|
# copy in the relevant files
|
2019-11-17 00:12:38 +03:00
|
|
|
result <- file.copy(
|
|
|
|
from = "R-package/./"
|
2020-05-25 05:23:38 +03:00
|
|
|
, to = sprintf("%s/", TEMP_R_DIR)
|
2019-11-17 00:12:38 +03:00
|
|
|
, recursive = TRUE
|
|
|
|
, overwrite = TRUE
|
|
|
|
)
|
2018-08-29 07:31:42 +03:00
|
|
|
.handle_result(result)
|
|
|
|
|
2019-11-17 00:12:38 +03:00
|
|
|
result <- file.copy(
|
|
|
|
from = "include/"
|
2020-05-25 05:23:38 +03:00
|
|
|
, to = sprintf("%s/", TEMP_SOURCE_DIR)
|
2019-11-17 00:12:38 +03:00
|
|
|
, recursive = TRUE
|
|
|
|
, overwrite = TRUE
|
|
|
|
)
|
2018-08-29 07:31:42 +03:00
|
|
|
.handle_result(result)
|
|
|
|
|
2019-11-17 00:12:38 +03:00
|
|
|
result <- file.copy(
|
|
|
|
from = "src/"
|
2020-05-25 05:23:38 +03:00
|
|
|
, to = sprintf("%s/", TEMP_SOURCE_DIR)
|
2019-11-17 00:12:38 +03:00
|
|
|
, recursive = TRUE
|
|
|
|
, overwrite = TRUE
|
|
|
|
)
|
2018-12-24 08:05:29 +03:00
|
|
|
.handle_result(result)
|
|
|
|
|
2019-11-17 00:12:38 +03:00
|
|
|
result <- file.copy(
|
|
|
|
from = "compute/"
|
2020-05-25 05:23:38 +03:00
|
|
|
, to = sprintf("%s/", TEMP_SOURCE_DIR)
|
2019-11-17 00:12:38 +03:00
|
|
|
, recursive = TRUE
|
|
|
|
, overwrite = TRUE
|
|
|
|
)
|
2018-08-29 07:31:42 +03:00
|
|
|
.handle_result(result)
|
|
|
|
|
2019-11-17 00:12:38 +03:00
|
|
|
result <- file.copy(
|
|
|
|
from = "CMakeLists.txt"
|
2020-05-25 05:23:38 +03:00
|
|
|
, to = file.path(TEMP_R_DIR, "inst", "bin/")
|
2019-11-17 00:12:38 +03:00
|
|
|
, overwrite = TRUE
|
|
|
|
)
|
2018-08-29 07:31:42 +03:00
|
|
|
.handle_result(result)
|
|
|
|
|
2020-05-25 05:23:38 +03:00
|
|
|
# copy files into the place CMake expects
|
|
|
|
for (src_file in c("lightgbm_R.cpp", "lightgbm_R.h", "R_object_helper.h")) {
|
|
|
|
result <- file.copy(
|
|
|
|
from = file.path(TEMP_SOURCE_DIR, src_file)
|
|
|
|
, to = file.path(TEMP_SOURCE_DIR, "src", src_file)
|
|
|
|
, overwrite = TRUE
|
|
|
|
)
|
|
|
|
.handle_result(result)
|
|
|
|
result <- file.remove(
|
|
|
|
file.path(TEMP_SOURCE_DIR, src_file)
|
|
|
|
)
|
|
|
|
.handle_result(result)
|
|
|
|
}
|
|
|
|
|
2020-06-14 03:56:34 +03:00
|
|
|
result <- file.copy(
|
|
|
|
from = file.path("R-package", "inst", "make-r-def.R")
|
|
|
|
, to = file.path(TEMP_R_DIR, "inst", "bin/")
|
|
|
|
, overwrite = TRUE
|
|
|
|
)
|
|
|
|
.handle_result(result)
|
|
|
|
|
2018-08-29 07:31:42 +03:00
|
|
|
# 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
|
2020-05-25 05:23:38 +03:00
|
|
|
.run_shell_command("R", c("CMD", "build", TEMP_R_DIR, "--keep-empty-dirs"))
|
2018-08-29 07:31:42 +03:00
|
|
|
|
|
|
|
# Install the package
|
|
|
|
version <- gsub(
|
2018-09-23 18:48:49 +03:00
|
|
|
"Version: ",
|
|
|
|
"",
|
|
|
|
grep(
|
2019-11-17 00:12:38 +03:00
|
|
|
"Version: "
|
2020-05-25 05:23:38 +03:00
|
|
|
, readLines(con = file.path(TEMP_R_DIR, "DESCRIPTION"))
|
2019-11-17 00:12:38 +03:00
|
|
|
, value = TRUE
|
2018-09-23 18:48:49 +03:00
|
|
|
)
|
2018-08-29 07:31:42 +03:00
|
|
|
)
|
|
|
|
tarball <- file.path(getwd(), sprintf("lightgbm_%s.tar.gz", version))
|
|
|
|
|
2020-05-14 19:31:13 +03:00
|
|
|
install_cmd <- "R"
|
|
|
|
install_args <- c("CMD", "INSTALL", "--no-multiarch", "--with-keep.source", tarball)
|
2020-03-31 07:01:29 +03:00
|
|
|
if (INSTALL_AFTER_BUILD) {
|
2020-05-14 19:31:13 +03:00
|
|
|
.run_shell_command(install_cmd, install_args)
|
2020-03-31 07:01:29 +03:00
|
|
|
} else {
|
2020-05-14 19:31:13 +03:00
|
|
|
cmd <- paste0(install_cmd, " ", paste0(install_args, collapse = " "))
|
2020-03-31 07:01:29 +03:00
|
|
|
print(sprintf("Skipping installation. Install the package with command '%s'", cmd))
|
|
|
|
}
|