build_r.R 2.45 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
args <- commandArgs(trailingOnly = TRUE)
INSTALL_AFTER_BUILD <- !("--skip-install" %in% args)

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

19
20
# system() will not raise an R exception if the process called
# fails. Wrapping it here to get that behavior
21
.run_shell_command <- function(cmd, ...) {
22
    exit_code <- system(cmd, ...)
23
    if (exit_code != 0L) {
24
25
26
27
        stop(paste0("Command failed with exit code: ", exit_code))
    }
}

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

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

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

49
50
51
52
53
54
result <- file.copy(
  from = "src/"
  , to = file.path("lightgbm_r", "src/")
  , recursive = TRUE
  , overwrite = TRUE
)
Gao Tao's avatar
Gao Tao committed
55
56
.handle_result(result)

57
58
59
60
61
62
result <- file.copy(
  from = "compute/"
  , to = file.path("lightgbm_r", "src/")
  , recursive = TRUE
  , overwrite = TRUE
)
James Lamb's avatar
James Lamb committed
63
64
.handle_result(result)

65
66
67
68
69
result <- file.copy(
  from = "CMakeLists.txt"
  , to = file.path("lightgbm_r", "inst", "bin/")
  , overwrite = TRUE
)
James Lamb's avatar
James Lamb committed
70
71
.handle_result(result)

72
# Build the package (do not touch this line!)
James Lamb's avatar
James Lamb committed
73
74
75
# 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
76
cmd <- "R CMD build lightgbm_r --keep-empty-dirs"
77
.run_shell_command(cmd)
James Lamb's avatar
James Lamb committed
78
79
80

# Install the package
version <- gsub(
81
82
83
  "Version: ",
  "",
  grep(
84
85
86
    "Version: "
    , readLines(con = file.path("lightgbm_r", "DESCRIPTION"))
    , value = TRUE
87
  )
James Lamb's avatar
James Lamb committed
88
89
90
)
tarball <- file.path(getwd(), sprintf("lightgbm_%s.tar.gz", version))

91
cmd <- sprintf("R CMD INSTALL %s --no-multiarch --with-keep.source", tarball)
92
93
94
95
96
if (INSTALL_AFTER_BUILD) {
  .run_shell_command(cmd)
} else {
  print(sprintf("Skipping installation. Install the package with command '%s'", cmd))
}