install.libs.R 3.67 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
21
    unlink("./include", recursive = TRUE)
    unlink("./src", recursive = TRUE)
    unlink("./compute", recursive = TRUE)
	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
28
    if (!file.copy("./../../src", "./", overwrite = TRUE, recursive = TRUE)) {
      stop("Cannot find folder LightGBM/src")
    }
    if (use_gpu) {
Laurae's avatar
Laurae committed
29
30
31
32
33
34
      if (!file.copy("./../../compute", "./", overwrite = TRUE, recursive = TRUE)) {
        print("Cannot find folder LightGBM/compute, disabling GPU build.")
        use_gpu <- FALSE
      }
    }
    if (!file.copy("./../../CMakeLists.txt", "./", overwrite = TRUE, recursive = TRUE)) {
Guolin Ke's avatar
Guolin Ke committed
35
      stop("Cannot find file LightGBM/CMakeLists.txt")
Laurae's avatar
Laurae committed
36
    }
37
  }
Laurae's avatar
Laurae committed
38
39
  
  # Prepare building package
40
  build_dir <- file.path(source_dir, "build", fsep = "/")
Laurae's avatar
Laurae committed
41
42
43
  dir.create(build_dir, recursive = TRUE, showWarnings = FALSE)
  setwd(build_dir)
  
44
  # Prepare installation steps
45
46
  cmake_base <- "cmake "
  build_cmd <- "make _lightgbm -j"
47
  lib_folder <- file.path(R_PACKAGE_SOURCE, "src", fsep = "/")
Laurae's avatar
Laurae committed
48
  
49
  # Check if Windows installation (for gcc vs Visual Studio)
Laurae's avatar
Laurae committed
50
51
  if (WINDOWS) {
    if (use_mingw) {
52
53
54
      cmake_cmd <- paste0(cmake_base, " -G \"MinGW Makefiles\" ")
      build_cmd <- "mingw32-make.exe _lightgbm -j"
      system(paste0(cmake_cmd, " ..")) # Must build twice for Windows due sh.exe in Rtools
Laurae's avatar
Laurae committed
55
    } else {
56
57
58
59
60
61
62
63
64
65
66
      cmake_cmd <- paste0(cmake_base, " -DCMAKE_GENERATOR_PLATFORM=x64 ")
      tryVS <- system(paste0(cmake_cmd, " .."))
      if (tryVS == 1) {
        unlink("./*", recursive = TRUE) # Clean up build directory
        cmake_cmd <- paste0(cmake_base, " -G \"MinGW Makefiles\" ") # Switch to MinGW on failure, try build once
        system(paste0(cmake_cmd, " ..")) # Must build twice for Windows due sh.exe in Rtools
        build_cmd <- "mingw32-make.exe _lightgbm -j"
      } else {
        build_cmd <- "cmake --build . --target _lightgbm  --config Release"
        lib_folder <- file.path(R_PACKAGE_SOURCE, "src/Release", fsep = "/")
      }
67
68
    }
  }
Laurae's avatar
Laurae committed
69
70
  
  if (use_gpu) {
Guolin Ke's avatar
Guolin Ke committed
71
    cmake_cmd <- paste0(cmake_cmd, " -DUSE_GPU=ON ")
72
  }
Laurae's avatar
Laurae committed
73
74
75
76
  
  # Install
  system(paste0(cmake_cmd, " .."))
  system(build_cmd)
77
  src <- file.path(lib_folder, paste0("lib_lightgbm", SHLIB_EXT), fsep = "/")
Laurae's avatar
Laurae committed
78
79
  
} else {
80

Laurae's avatar
Laurae committed
81
  # Has precompiled package
82
83
84
85
86
  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
87
  } else {
88
    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
89
  }
Laurae's avatar
Laurae committed
90
  
91
92
}

Laurae's avatar
Laurae committed
93
# Check installation correctness
94
dest <- file.path(R_PACKAGE_DIR, paste0("libs", R_ARCH), fsep = "/")
95
dir.create(dest, recursive = TRUE, showWarnings = FALSE)
Laurae's avatar
Laurae committed
96
97
if (file.exists(src)) {
  cat("Found library file: ", src, " to move to ", dest, sep = "")
98
99
  file.copy(src, dest, overwrite = TRUE)
} else {
Laurae's avatar
Laurae committed
100
  stop(paste0("Cannot find lib_lightgbm", SHLIB_EXT))
101
}