build_r.R 12.2 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
args <- commandArgs(trailingOnly = TRUE)
INSTALL_AFTER_BUILD <- !("--skip-install" %in% args)
10
11
TEMP_R_DIR <- file.path(getwd(), "lightgbm_r")
TEMP_SOURCE_DIR <- file.path(TEMP_R_DIR, "src")
12

13
14
15
16
17
18
19
20
21
22
23
# [description]
#     Parse the content of commandArgs() into a structured
#     list. This returns a list with two sections.
#       * "flags" = a character of vector of flags like "--use-gpu"
#       * "keyword_args" = a named character vector, where names
#           refer to options and values are the option values. For
#           example, c("--boost-librarydir" = "/usr/lib/x86_64-linux-gnu")
.parse_args <- function(args) {
  out_list <- list(
    "flags" = character(0L)
    , "keyword_args" = character(0L)
24
    , "make_args" = character(0L)
25
26
  )
  for (arg in args) {
27
    if (any(grepl("^\\-j[0-9]+", arg))) {  # nolint: non_portable_path
28
        out_list[["make_args"]] <- arg
29
30
    } else if (any(grepl("=", arg, fixed = TRUE))) {
      split_arg <- strsplit(arg, "=", fixed = TRUE)[[1L]]
31
32
33
34
35
36
37
38
39
40
41
      arg_name <- split_arg[[1L]]
      arg_value <- split_arg[[2L]]
      out_list[["keyword_args"]][[arg_name]] <- arg_value
    } else {
      out_list[["flags"]] <- c(out_list[["flags"]], arg)
    }
  }
  return(out_list)
}
parsed_args <- .parse_args(args)

42
SKIP_VIGNETTES <- "--no-build-vignettes" %in% parsed_args[["flags"]]
43
44
45
46
47
48
49
50
51
52
53
54
55
USING_GPU <- "--use-gpu" %in% parsed_args[["flags"]]
USING_MINGW <- "--use-mingw" %in% parsed_args[["flags"]]
USING_MSYS2 <- "--use-msys2" %in% parsed_args[["flags"]]

# this maps command-line arguments to defines passed into CMake,
ARGS_TO_DEFINES <- c(
  "--boost-root" = "-DBOOST_ROOT"
  , "--boost-dir" = "-DBoost_DIR"
  , "--boost-include-dir" = "-DBoost_INCLUDE_DIR"
  , "--boost-librarydir" = "-DBOOST_LIBRARYDIR"
  , "--opencl-include-dir" = "-DOpenCL_INCLUDE_DIR"
  , "--opencl-library" = "-DOpenCL_LIBRARY"
)
56
57

recognized_args <- c(
58
59
  "--no-build-vignettes"
  , "--skip-install"
60
61
62
  , "--use-gpu"
  , "--use-mingw"
  , "--use-msys2"
63
  , names(ARGS_TO_DEFINES)
64
)
65
66
67
68
69
given_args <- c(
  parsed_args[["flags"]]
  , names(parsed_args[["keyword_args"]])
)
unrecognized_args <- setdiff(given_args, recognized_args)
70
71
72
if (length(unrecognized_args) > 0L) {
  msg <- paste0(
    "Unrecognized arguments: "
73
    , toString(unrecognized_args)
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
  )
  stop(msg)
}

# [description] Replace statements in install.libs.R code based on
#               command-line flags
.replace_flag <- function(variable_name, value, content) {
  out <- gsub(
    pattern = paste0(variable_name, " <-.*")
    , replacement = paste0(variable_name, " <- ", as.character(value))
    , x = content
  )
  return(out)
}

89
90
91
install_libs_content <- readLines(
  file.path("R-package", "src", "install.libs.R")
)
92
93
94
install_libs_content <- .replace_flag("use_gpu", USING_GPU, install_libs_content)
install_libs_content <- .replace_flag("use_mingw", USING_MINGW, install_libs_content)
install_libs_content <- .replace_flag("use_msys2", USING_MSYS2, install_libs_content)
95

96
97
98
99
100
101
102
# set up extra flags based on keyword arguments
keyword_args <- parsed_args[["keyword_args"]]
if (length(keyword_args) > 0L) {
  cmake_args_to_add <- NULL
  for (i in seq_len(length(keyword_args))) {
    arg_name <- names(keyword_args)[[i]]
    define_name <- ARGS_TO_DEFINES[[arg_name]]
103
    arg_value <- shQuote(normalizePath(keyword_args[[arg_name]], winslash = "/"))
104
105
106
107
108
    cmake_args_to_add <- c(cmake_args_to_add, paste0(define_name, "=", arg_value))
  }
  install_libs_content <- gsub(
    pattern = paste0("command_line_args <- NULL")
    , replacement = paste0(
109
110
111
      "command_line_args <- c(\'"
      , paste(cmake_args_to_add, collapse = "', '")
      , "')"
112
113
    )
    , x = install_libs_content
114
    , fixed = TRUE
115
116
117
  )
}

118
119
120
121
122
123
124
125
126
127
128
129
130
131
# if provided, set '-j' in 'make' commands in install.libs.R
if (length(parsed_args[["make_args"]]) > 0L) {
  install_libs_content <- gsub(
    pattern = "make_args_from_build_script <- character(0L)"
    , replacement = paste0(
      "make_args_from_build_script <- c(\""
      , paste0(parsed_args[["make_args"]], collapse = "\", \"")
      , "\")"
    )
    , x = install_libs_content
    , fixed = TRUE
  )
}

James Lamb's avatar
James Lamb committed
132
133
# R returns FALSE (not a non-zero exit code) if a file copy operation
# breaks. Let's fix that
134
.handle_result <- function(res) {
135
  if (!all(res)) {
136
137
    stop("Copying files failed!")
  }
138
  return(invisible(NULL))
James Lamb's avatar
James Lamb committed
139
140
}

141
# system() will not raise an R exception if the process called
142
143
144
145
146
147
148
149
# 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({
150
        require("processx")  # nolint: undesirable_function
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
      })
    })
    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)) {
175
176
        stop(paste0("Command failed with exit code: ", exit_code))
    }
177
    return(invisible(exit_code))
178
179
}

James Lamb's avatar
James Lamb committed
180
# Make a new temporary folder to work in
181
182
unlink(x = TEMP_R_DIR, recursive = TRUE)
dir.create(TEMP_R_DIR)
James Lamb's avatar
James Lamb committed
183
184

# copy in the relevant files
185
186
result <- file.copy(
  from = "R-package/./"
187
  , to = sprintf("%s/", TEMP_R_DIR)
188
189
190
  , recursive = TRUE
  , overwrite = TRUE
)
James Lamb's avatar
James Lamb committed
191
192
.handle_result(result)

193
194
195
196
197
198
# overwrite src/install.libs.R with new content based on command-line flags
writeLines(
  text = install_libs_content
  , con = file.path(TEMP_SOURCE_DIR, "install.libs.R")
)

199
200
201
202
203
204
205
206
207
208
209
210
211
212
# Add blank Makevars files
result <- file.copy(
  from = file.path(TEMP_R_DIR, "inst", "Makevars")
  , to = file.path(TEMP_SOURCE_DIR, "Makevars")
  , overwrite = TRUE
)
.handle_result(result)
result <- file.copy(
  from = file.path(TEMP_R_DIR, "inst", "Makevars.win")
  , to = file.path(TEMP_SOURCE_DIR, "Makevars.win")
  , overwrite = TRUE
)
.handle_result(result)

213
214
result <- file.copy(
  from = "include/"
215
  , to =  sprintf("%s/", TEMP_SOURCE_DIR)
216
217
218
  , recursive = TRUE
  , overwrite = TRUE
)
James Lamb's avatar
James Lamb committed
219
220
.handle_result(result)

221
222
result <- file.copy(
  from = "src/"
223
  , to = sprintf("%s/", TEMP_SOURCE_DIR)
224
225
226
  , recursive = TRUE
  , overwrite = TRUE
)
Gao Tao's avatar
Gao Tao committed
227
228
.handle_result(result)

229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
EIGEN_R_DIR <- file.path(TEMP_SOURCE_DIR, "include", "Eigen")
dir.create(EIGEN_R_DIR)

eigen_modules <- c(
  "Cholesky"
  , "Core"
  , "Dense"
  , "Eigenvalues"
  , "Geometry"
  , "Householder"
  , "Jacobi"
  , "LU"
  , "QR"
  , "SVD"
)
for (eigen_module in eigen_modules) {
  result <- file.copy(
246
    from = file.path("external_libs", "eigen", "Eigen", eigen_module)
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
    , to = EIGEN_R_DIR
    , recursive = FALSE
    , overwrite = TRUE
  )
  .handle_result(result)
}

dir.create(file.path(EIGEN_R_DIR, "src"))

for (eigen_module in c(eigen_modules, "misc", "plugins")) {
  if (eigen_module == "Dense") {
    next
  }
  module_dir <- file.path(EIGEN_R_DIR, "src", eigen_module)
  dir.create(module_dir, recursive = TRUE)
  result <- file.copy(
263
    from = sprintf("%s/", file.path("external_libs", "eigen", "Eigen", "src", eigen_module))
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
    , to = sprintf("%s/", file.path(EIGEN_R_DIR, "src"))
    , recursive = TRUE
    , overwrite = TRUE
  )
  .handle_result(result)
}

.replace_pragmas <- function(filepath) {
  pragma_patterns <- c(
    "^.*#pragma clang diagnostic.*$"
    , "^.*#pragma diag_suppress.*$"
    , "^.*#pragma GCC diagnostic.*$"
    , "^.*#pragma region.*$"
    , "^.*#pragma endregion.*$"
    , "^.*#pragma warning.*$"
  )
  content <- readLines(filepath)
  for (pragma_pattern in pragma_patterns) {
    content <- content[!grepl(pragma_pattern, content)]
  }
  writeLines(content, filepath)
}

# remove pragmas that suppress warnings, to appease R CMD check
.replace_pragmas(
  file.path(EIGEN_R_DIR, "src", "Core", "arch", "SSE", "Complex.h")
)
.replace_pragmas(
  file.path(EIGEN_R_DIR, "src", "Core", "util", "DisableStupidWarnings.h")
)

295
296
result <- file.copy(
  from = "CMakeLists.txt"
297
  , to = file.path(TEMP_R_DIR, "inst", "bin/")
298
299
  , overwrite = TRUE
)
James Lamb's avatar
James Lamb committed
300
301
.handle_result(result)

302
303
# remove CRAN-specific files
result <- file.remove(
304
305
  file.path(TEMP_R_DIR, "cleanup")
  , file.path(TEMP_R_DIR, "configure")
306
307
308
309
310
311
312
  , file.path(TEMP_R_DIR, "configure.ac")
  , file.path(TEMP_R_DIR, "configure.win")
  , file.path(TEMP_SOURCE_DIR, "Makevars.in")
  , file.path(TEMP_SOURCE_DIR, "Makevars.win.in")
)
.handle_result(result)

313
314
315
#------------#
# submodules #
#------------#
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
EXTERNAL_LIBS_R_DIR <- file.path(TEMP_SOURCE_DIR, "external_libs")
dir.create(EXTERNAL_LIBS_R_DIR)
for (submodule in list.dirs(
  path = "external_libs"
  , full.names = FALSE
  , recursive = FALSE
)) {
  # compute/ is a submodule with boost, only needed if
  # building the R package with GPU support;
  # eigen/ has a special treatment due to licensing aspects
  if ((submodule == "compute" && !USING_GPU) || submodule == "eigen") {
    next
  }
  result <- file.copy(
    from = sprintf("%s/", file.path("external_libs", submodule))
    , to = sprintf("%s/", EXTERNAL_LIBS_R_DIR)
    , recursive = TRUE
    , overwrite = TRUE
  )
  .handle_result(result)
}
337

338
# copy files into the place CMake expects
339
340
341
342
343
344
345
346
CMAKE_MODULES_R_DIR <- file.path(TEMP_SOURCE_DIR, "cmake", "modules")
dir.create(CMAKE_MODULES_R_DIR, recursive = TRUE)
result <- file.copy(
  from = file.path("cmake", "modules", "FindLibR.cmake")
  , to = sprintf("%s/", CMAKE_MODULES_R_DIR)
  , overwrite = TRUE
)
.handle_result(result)
347
for (src_file in c("lightgbm_R.cpp", "lightgbm_R.h")) {
348
349
350
351
352
353
354
355
356
357
358
359
  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)
}

360
361
362
363
364
365
366
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)

367
368
369
370
371
372
373
# R packages cannot have versions like 3.0.0rc1, but
# 3.0.0-1 is acceptable
LGB_VERSION <- readLines("VERSION.txt")[1L]
LGB_VERSION <- gsub(
  pattern = "rc"
  , replacement = "-"
  , x = LGB_VERSION
374
  , fixed = TRUE
375
376
377
378
379
380
381
382
383
384
)

# DESCRIPTION has placeholders for version
# and date so it doesn't have to be updated manually
DESCRIPTION_FILE <- file.path(TEMP_R_DIR, "DESCRIPTION")
description_contents <- readLines(DESCRIPTION_FILE)
description_contents <- gsub(
  pattern = "~~VERSION~~"
  , replacement = LGB_VERSION
  , x = description_contents
385
  , fixed = TRUE
386
387
388
389
390
)
description_contents <- gsub(
  pattern = "~~DATE~~"
  , replacement = as.character(Sys.Date())
  , x = description_contents
391
  , fixed = TRUE
392
)
393
394
395
396
397
398
description_contents <- gsub(
  pattern = "~~CXXSTD~~"
  , replacement = "C++11"
  , x = description_contents
  , fixed = TRUE
)
399
400
writeLines(description_contents, DESCRIPTION_FILE)

James Lamb's avatar
James Lamb committed
401
402
403
# 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
404
405
406
407
408
r_build_args <- c("CMD", "build", TEMP_R_DIR, "--keep-empty-dirs")
if (isTRUE(SKIP_VIGNETTES)) {
  r_build_args <- c(r_build_args, "--no-build-vignettes")
}
.run_shell_command("R", r_build_args)
James Lamb's avatar
James Lamb committed
409
410
411

# Install the package
version <- gsub(
412
413
414
415
416
  pattern = "Version: ",
  replacement = "",
  x = grep(
    pattern = "Version: "
    , x = readLines(con = file.path(TEMP_R_DIR, "DESCRIPTION"))
417
    , value = TRUE
418
    , fixed = TRUE
419
  )
420
  , fixed = TRUE
James Lamb's avatar
James Lamb committed
421
422
423
)
tarball <- file.path(getwd(), sprintf("lightgbm_%s.tar.gz", version))

424
425
install_cmd <- "R"
install_args <- c("CMD", "INSTALL", "--no-multiarch", "--with-keep.source", tarball)
426
if (INSTALL_AFTER_BUILD) {
427
  .run_shell_command(install_cmd, install_args)
428
} else {
429
  cmd <- paste0(install_cmd, " ", paste0(install_args, collapse = " "))
430
431
  print(sprintf("Skipping installation. Install the package with command '%s'", cmd))
}