build_r.R 2.48 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
8
9
# 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")

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

16
17
18
19
20
21
22
23
24
# 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 != 0){
        stop(paste0("Command failed with exit code: ", exit_code))
    }
}

James Lamb's avatar
James Lamb committed
25
26
27
28
29
# Make a new temporary folder to work in
unlink(x = "lightgbm_r", recursive = TRUE)
dir.create("lightgbm_r")

# copy in the relevant files
30
31
32
33
result <- file.copy(from = "R-package/./",
                    to = "lightgbm_r/",
                    recursive = TRUE,
                    overwrite = TRUE)
James Lamb's avatar
James Lamb committed
34
35
.handle_result(result)

36
37
38
39
result <- file.copy(from = "include/",
                    to = file.path("lightgbm_r", "src/"),
                    recursive = TRUE,
                    overwrite = TRUE)
James Lamb's avatar
James Lamb committed
40
41
.handle_result(result)

42
43
result <- file.copy(from = "src/",
                    to = file.path("lightgbm_r", "src/"),
Gao Tao's avatar
Gao Tao committed
44
45
46
47
48
49
                    recursive = TRUE,
                    overwrite = TRUE)
.handle_result(result)

result <- file.copy(from = "compute/",
                    to = file.path("lightgbm_r", "src/"),
50
51
                    recursive = TRUE,
                    overwrite = TRUE)
James Lamb's avatar
James Lamb committed
52
53
.handle_result(result)

54
55
56
result <- file.copy(from = "CMakeLists.txt",
                    to = file.path("lightgbm_r", "inst", "bin/"),
                    overwrite = TRUE)
James Lamb's avatar
James Lamb committed
57
58
59
60
61
62
.handle_result(result)

# 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
63
64
cmd <- "R CMD build lightgbm_r --keep_empty-dirs"
.run_shell_command(cmd)
James Lamb's avatar
James Lamb committed
65
66
67

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

78
79
cmd <- sprintf("R CMD INSTALL %s --no-multiarch", tarball)
.run_shell_command(cmd)
James Lamb's avatar
James Lamb committed
80
81

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