install.libs.R 7.41 KB
Newer Older
Laurae's avatar
Laurae committed
1
2
# User options
use_precompile <- FALSE
3
4
use_gpu <- FALSE
use_mingw <- FALSE
Laurae's avatar
Laurae committed
5

6
if (.Machine$sizeof.pointer != 8L) {
7
  stop("LightGBM only supports 64-bit R, please check the version of R and Rtools.")
Guolin Ke's avatar
Guolin Ke committed
8
9
}

10
R_int_UUID <- .Internal(internalsID())
11
R_ver <- as.double(R.Version()$major) + as.double(R.Version()$minor) / 10.0
12

13
if (!(R_int_UUID == "0310d4b8-ccb1-4bb8-ba94-d36a55f60262"
14
    || R_int_UUID == "2fdf6c18-697a-4ba7-b8ef-11c0d92f1327")) {
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
  warning("Warning: unmatched R_INTERNALS_UUID, may not run normally.")
}

# system() will not raise an R exception if the process called
# 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)) {
        stop(paste0("Command failed with exit code: ", exit_code))
    }
    return(invisible(exit_code))
}

# try to generate Visual Studio build files
.generate_vs_makefiles <- function(cmake_args) {
  vs_versions <- c(
    "Visual Studio 16 2019"
    , "Visual Studio 15 2017"
    , "Visual Studio 14 2015"
  )
  working_vs_version <- NULL
  for (vs_version in vs_versions) {
    message(sprintf("Trying '%s'", vs_version))
    # if the build directory is not empty, clean it
    if (file.exists("CMakeCache.txt")) {
      file.remove("CMakeCache.txt")
    }
    vs_cmake_args <- c(
      cmake_args
      , "-G"
      , shQuote(vs_version)
      , "-A"
      , "x64"
    )
    exit_code <- .run_shell_command("cmake", c(vs_cmake_args, ".."), strict = FALSE)
    if (exit_code == 0L) {
      message(sprintf("Successfully created build files for '%s'", vs_version))
      return(invisible(TRUE))
    }

  }
  return(invisible(FALSE))
86
}
James Lamb's avatar
James Lamb committed
87
88

# Move in CMakeLists.txt
89
90
91
92
93
94
95
write_succeeded <- file.copy(
  "../inst/bin/CMakeLists.txt"
  , "CMakeLists.txt"
  , overwrite = TRUE
)
if (!write_succeeded) {
  stop("Copying CMakeLists.txt failed")
James Lamb's avatar
James Lamb committed
96
97
}

98
99
100
101
# Get some paths
source_dir <- file.path(R_PACKAGE_SOURCE, "src", fsep = "/")
build_dir <- file.path(source_dir, "build", fsep = "/")

Laurae's avatar
Laurae committed
102
103
104
105
# Check for precompilation
if (!use_precompile) {

  # Prepare building package
106
107
108
109
110
  dir.create(
    build_dir
    , recursive = TRUE
    , showWarnings = FALSE
  )
Laurae's avatar
Laurae committed
111
  setwd(build_dir)
112

113
  # Prepare installation steps
114
115
116
  cmake_args <- NULL
  build_cmd <- "make"
  build_args <- "_lightgbm"
117
  lib_folder <- file.path(source_dir, fsep = "/")
118

Guolin Ke's avatar
Guolin Ke committed
119
  if (use_gpu) {
120
    cmake_args <- c(cmake_args, "-DUSE_GPU=ON")
Guolin Ke's avatar
Guolin Ke committed
121
  }
122
  if (R_ver >= 3.5) {
123
    cmake_args <- c(cmake_args, "-DUSE_R35=ON")
124
  }
125
  cmake_args <- c(cmake_args, "-DBUILD_FOR_R=ON")
Guolin Ke's avatar
Guolin Ke committed
126

127
128
129
130
131
132
  # Pass in R version, used to help find R executable for linking
  R_version_string <- paste(
    R.Version()[["major"]]
    , R.Version()[["minor"]]
    , sep = "."
  )
133
134
135
136
137
138
  r_version_arg <- sprintf("-DCMAKE_R_VERSION='%s'", R_version_string)
  cmake_args <- c(cmake_args, r_version_arg)

  # the checks below might already run `cmake -G`. If they do, set this flag
  # to TRUE to avoid re-running it later
  makefiles_already_generated <- FALSE
139

140
  # Check if Windows installation (for gcc vs Visual Studio)
Laurae's avatar
Laurae committed
141
142
  if (WINDOWS) {
    if (use_mingw) {
143
144
145
146
147
148
      message("Trying to build with MinGW")
      # Must build twice for Windows due sh.exe in Rtools
      cmake_args <- c(cmake_args, "-G", shQuote("MinGW Makefiles"))
      .run_shell_command("cmake", c(cmake_args, ".."), strict = FALSE)
      build_cmd <- "mingw32-make.exe"
      build_args <- "_lightgbm"
Laurae's avatar
Laurae committed
149
    } else {
150
151
152
153
154
155
156
157
      visual_studio_succeeded <- .generate_vs_makefiles(cmake_args)
      if (!isTRUE(visual_studio_succeeded)) {
        warning("Building with Visual Studio failed. Attempting with MinGW")
        # Must build twice for Windows due sh.exe in Rtools
        cmake_args <- c(cmake_args, "-G", shQuote("MinGW Makefiles"))
        .run_shell_command("cmake", c(cmake_args, ".."), strict = FALSE)
        build_cmd <- "mingw32-make.exe"
        build_args <- "_lightgbm"
158
      } else {
159
160
        build_cmd <- "cmake"
        build_args <- c("--build", ".", "--target", "_lightgbm", "--config", "Release")
161
        lib_folder <- file.path(source_dir, "Release", fsep = "/")
162
        makefiles_already_generated <- TRUE
163
      }
164
    }
165
166
167
  } else {
      .run_shell_command("cmake", c(cmake_args, ".."))
      makefiles_already_generated <- TRUE
168
  }
169

170
171
172
173
  # generate build files
  if (!makefiles_already_generated) {
    .run_shell_command("cmake", c(cmake_args, ".."))
  }
174
175
176
177
178

  # R CMD check complains about the .NOTPARALLEL directive created in the cmake
  # Makefile. We don't need it here anyway since targets are built serially, so trying
  # to remove it with this hack
  generated_makefile <- file.path(
179
    build_dir
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
    , "Makefile"
  )
  if (file.exists(generated_makefile)) {
    makefile_txt <- readLines(
      con = generated_makefile
    )
    makefile_txt <- gsub(
      pattern = ".*NOTPARALLEL.*"
      , replacement = ""
      , x = makefile_txt
    )
    writeLines(
      text = makefile_txt
      , con = generated_makefile
      , sep = "\n"
    )
  }

198
199
  # build the library
  .run_shell_command(build_cmd, build_args)
200
  src <- file.path(lib_folder, paste0("lib_lightgbm", SHLIB_EXT), fsep = "/")
201

Laurae's avatar
Laurae committed
202
} else {
203

Laurae's avatar
Laurae committed
204
  # Has precompiled package
205
  lib_folder <- file.path(R_PACKAGE_SOURCE, "../", fsep = "/")
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
  shared_object_file <- file.path(
    lib_folder
    , paste0("lib_lightgbm", SHLIB_EXT)
    , fsep = "/"
  )
  release_file <- file.path(
    lib_folder
    , paste0("Release/lib_lightgbm", SHLIB_EXT)
    , fsep = "/"
  )
  windows_shared_object_file <- file.path(
    lib_folder
    , paste0("/windows/x64/DLL/lib_lightgbm", SHLIB_EXT)
    , fsep = "/"
  )
  if (file.exists(shared_object_file)) {
    src <- shared_object_file
  } else if (file.exists(release_file)) {
    src <- release_file
Laurae's avatar
Laurae committed
225
  } else {
226
227
    # Expected result: installation will fail if it is not here or any other
    src <- windows_shared_object_file
228
229
230
  }
}

231
232
233
234
# Packages with install.libs.R need to copy some artifacts into the
# expected places in the package structure.
# see https://cran.r-project.org/doc/manuals/r-devel/R-exts.html#Package-subdirectories,
# especially the paragraph on install.libs.R
235
dest <- file.path(R_PACKAGE_DIR, paste0("libs", R_ARCH), fsep = "/")
236
dir.create(dest, recursive = TRUE, showWarnings = FALSE)
Laurae's avatar
Laurae committed
237
if (file.exists(src)) {
238
  message(paste0("Found library file: ", src, " to move to ", dest))
239
  file.copy(src, dest, overwrite = TRUE)
240
241
242
243
244
245

  symbols_file <- file.path(source_dir, "symbols.rds")
  if (file.exists(symbols_file)) {
    file.copy(symbols_file, dest, overwrite = TRUE)
  }

246
} else {
Laurae's avatar
Laurae committed
247
  stop(paste0("Cannot find lib_lightgbm", SHLIB_EXT))
248
}
249
250
251

# clean up the "build" directory
if (dir.exists(build_dir)) {
252
  message("Removing 'build/' directory")
253
  unlink(
254
    x = build_dir
255
256
257
258
    , recursive = TRUE
    , force = TRUE
  )
}