install.libs.R 3.98 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

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

Laurae's avatar
Laurae committed
10
11
12
13
# Check for precompilation
if (!use_precompile) {

  # Check repository content
14
  source_dir <- file.path(R_PACKAGE_SOURCE, "src", fsep = "/")
Laurae's avatar
Laurae committed
15
16
17
  setwd(source_dir)
  
  if (!file.exists("_IS_FULL_PACKAGE")) {
Guolin Ke's avatar
Guolin Ke committed
18
19
20
    unlink("./include", recursive = TRUE)
    unlink("./src", recursive = TRUE)
    unlink("./compute", recursive = TRUE)
Guolin Ke's avatar
Guolin Ke committed
21
    unlink("./build", recursive = TRUE)
Laurae's avatar
Laurae committed
22
23
24
    if (!file.copy("./../../include", "./", overwrite = TRUE, recursive = TRUE)) {
      stop("Cannot find folder LightGBM/include")
    }
25
26
27
    if (!file.copy("./../../src", "./", overwrite = TRUE, recursive = TRUE)) {
      stop("Cannot find folder LightGBM/src")
    }
Guolin Ke's avatar
Guolin Ke committed
28
29
30
    if (!file.copy("./../../compute", "./", overwrite = TRUE, recursive = TRUE)) {
      print("Cannot find folder LightGBM/compute, disabling GPU build.")
      use_gpu <- FALSE
Laurae's avatar
Laurae committed
31
32
    }
    if (!file.copy("./../../CMakeLists.txt", "./", overwrite = TRUE, recursive = TRUE)) {
Guolin Ke's avatar
Guolin Ke committed
33
      stop("Cannot find file LightGBM/CMakeLists.txt")
Laurae's avatar
Laurae committed
34
    }
35
  }
Laurae's avatar
Laurae committed
36
37
  
  # Prepare building package
38
  build_dir <- file.path(source_dir, "build", fsep = "/")
Laurae's avatar
Laurae committed
39
40
41
  dir.create(build_dir, recursive = TRUE, showWarnings = FALSE)
  setwd(build_dir)
  
42
  # Prepare installation steps
Guolin Ke's avatar
Guolin Ke committed
43
  cmake_cmd <- "cmake "
44
  build_cmd <- "make _lightgbm"
45
  lib_folder <- file.path(R_PACKAGE_SOURCE, "src", fsep = "/")
Laurae's avatar
Laurae committed
46
  
Guolin Ke's avatar
Guolin Ke committed
47
48
49
50
  if (use_gpu) {
    cmake_cmd <- paste0(cmake_cmd, " -DUSE_GPU=ON ")
  }

51
  # Check if Windows installation (for gcc vs Visual Studio)
Laurae's avatar
Laurae committed
52
53
  if (WINDOWS) {
    if (use_mingw) {
Guolin Ke's avatar
Guolin Ke committed
54
      cmake_cmd <- paste0(cmake_cmd, " -G \"MinGW Makefiles\" ")
55
      build_cmd <- "mingw32-make.exe _lightgbm"
56
      system(paste0(cmake_cmd, " ..")) # Must build twice for Windows due sh.exe in Rtools
Laurae's avatar
Laurae committed
57
    } else {
Guolin Ke's avatar
Guolin Ke committed
58
59
      try_vs <- 0
      local_vs_def <- ""
60
      vs_versions <- c("Visual Studio 15 2017 Win64", "Visual Studio 14 2015 Win64")
Guolin Ke's avatar
Guolin Ke committed
61
62
63
64
65
66
67
      for(vs in vs_versions){
        vs_def <- paste0(" -G \"", vs, "\"")
        tmp_cmake_cmd <- paste0(cmake_cmd, vs_def)
        try_vs <- system(paste0(tmp_cmake_cmd, " .."))
        if (try_vs == 0) {
          local_vs_def = vs_def
          break
68
69
        } else {
          unlink("./*", recursive = TRUE) # Clean up build directory
Guolin Ke's avatar
Guolin Ke committed
70
71
72
73
        }
      }
      if (try_vs == 1) {
        cmake_cmd <- paste0(cmake_cmd, " -G \"MinGW Makefiles\" ") # Switch to MinGW on failure, try build once
74
        system(paste0(cmake_cmd, " ..")) # Must build twice for Windows due sh.exe in Rtools
75
        build_cmd <- "mingw32-make.exe _lightgbm"
76
      } else {
Guolin Ke's avatar
Guolin Ke committed
77
        cmake_cmd <- paste0(cmake_cmd, local_vs_def)
78
79
80
        build_cmd <- "cmake --build . --target _lightgbm  --config Release"
        lib_folder <- file.path(R_PACKAGE_SOURCE, "src/Release", fsep = "/")
      }
81
82
    }
  }
Laurae's avatar
Laurae committed
83
84
85
86
  
  # Install
  system(paste0(cmake_cmd, " .."))
  system(build_cmd)
87
  src <- file.path(lib_folder, paste0("lib_lightgbm", SHLIB_EXT), fsep = "/")
Laurae's avatar
Laurae committed
88
89
  
} else {
90

Laurae's avatar
Laurae committed
91
  # Has precompiled package
92
93
94
95
96
  lib_folder <- file.path(R_PACKAGE_SOURCE, "../", fsep = "/")
  if (file.exists(file.path(lib_folder, paste0("lib_lightgbm", SHLIB_EXT), fsep = "/"))) {
    src <- file.path(lib_folder, paste0("lib_lightgbm", SHLIB_EXT), fsep = "/")
  } else if (file.exists(file.path(lib_folder, paste0("Release/lib_lightgbm", SHLIB_EXT), fsep = "/"))) {
    src <- file.path(lib_folder, paste0("Release/lib_lightgbm", SHLIB_EXT), fsep = "/") 
Laurae's avatar
Laurae committed
97
  } else {
98
    src <- file.path(lib_folder, paste0("/windows/x64/DLL/lib_lightgbm", SHLIB_EXT), fsep = "/") # Expected result: installation will fail if it is not here or any other
99
  }
Laurae's avatar
Laurae committed
100
  
101
102
}

Laurae's avatar
Laurae committed
103
# Check installation correctness
104
dest <- file.path(R_PACKAGE_DIR, paste0("libs", R_ARCH), fsep = "/")
105
dir.create(dest, recursive = TRUE, showWarnings = FALSE)
Laurae's avatar
Laurae committed
106
107
if (file.exists(src)) {
  cat("Found library file: ", src, " to move to ", dest, sep = "")
108
109
  file.copy(src, dest, overwrite = TRUE)
} else {
Laurae's avatar
Laurae committed
110
  stop(paste0("Cannot find lib_lightgbm", SHLIB_EXT))
111
}