build_r.R 2.3 KB
Newer Older
1
# For macOS users who have decided to use gcc
2
# (replace 8 with version of gcc installed on your machine)
James Lamb's avatar
James Lamb committed
3
4
5
6
7
# 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")

8
9
10
11
library(testthat)
library(roxygen2)
library(devtools)

James Lamb's avatar
James Lamb committed
12
13
# R returns FALSE (not a non-zero exit code) if a file copy operation
# breaks. Let's fix that
14
15
16
17
.handle_result <- function(res) {
  if (!res) {
    stop("Copying files failed!")
  }
James Lamb's avatar
James Lamb committed
18
19
20
21
22
23
24
}

# Make a new temporary folder to work in
unlink(x = "lightgbm_r", recursive = TRUE)
dir.create("lightgbm_r")

# copy in the relevant files
25
26
27
28
result <- file.copy(from = "R-package/./",
                    to = "lightgbm_r/",
                    recursive = TRUE,
                    overwrite = TRUE)
James Lamb's avatar
James Lamb committed
29
30
.handle_result(result)

31
32
33
34
result <- file.copy(from = "include/",
                    to = file.path("lightgbm_r", "src/"),
                    recursive = TRUE,
                    overwrite = TRUE)
James Lamb's avatar
James Lamb committed
35
36
.handle_result(result)

37
38
result <- file.copy(from = "src/",
                    to = file.path("lightgbm_r", "src/"),
Gao Tao's avatar
Gao Tao committed
39
40
41
42
43
44
                    recursive = TRUE,
                    overwrite = TRUE)
.handle_result(result)

result <- file.copy(from = "compute/",
                    to = file.path("lightgbm_r", "src/"),
45
46
                    recursive = TRUE,
                    overwrite = TRUE)
James Lamb's avatar
James Lamb committed
47
48
.handle_result(result)

49
50
51
result <- file.copy(from = "CMakeLists.txt",
                    to = file.path("lightgbm_r", "inst", "bin/"),
                    overwrite = TRUE)
James Lamb's avatar
James Lamb committed
52
53
54
.handle_result(result)

# rebuild documentation
55
# devtools::document(pkg = "lightgbm_r")
James Lamb's avatar
James Lamb committed
56
57
58
59
60

# Build the package
# 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
61
62
devtools::build(pkg = "lightgbm_r",
                args = c("--keep-empty-dirs"))
James Lamb's avatar
James Lamb committed
63
64
65

# Install the package
version <- gsub(
66
67
68
69
70
71
72
  "Version: ",
  "",
  grep(
    "Version: ",
    readLines(con = file.path("lightgbm_r", "DESCRIPTION")),
    value = TRUE
  )
James Lamb's avatar
James Lamb committed
73
74
75
)
tarball <- file.path(getwd(), sprintf("lightgbm_%s.tar.gz", version))

76
system(sprintf("R CMD INSTALL %s --no-multiarch", tarball))
James Lamb's avatar
James Lamb committed
77
78

# Run R CMD CHECK
79
# R CMD CHECK lightgbm_2.1.2.tar.gz --as-cran | tee check.log | cat