install.libs.R 4.35 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.")
}

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

13
if (!(R_int_UUID == "0310d4b8-ccb1-4bb8-ba94-d36a55f60262"
14
    || R_int_UUID == "2fdf6c18-697a-4ba7-b8ef-11c0d92f1327")){
15
  print("Warning: unmatched R_INTERNALS_UUID, may cannot run normally.")
16
}
James Lamb's avatar
James Lamb committed
17
18
19
20
21
22

# Move in CMakeLists.txt
if (!file.copy("../inst/bin/CMakeLists.txt", "CMakeLists.txt", overwrite = TRUE)){
  stop("Copying CMakeLists failed")
}

Laurae's avatar
Laurae committed
23
24
25
26
# Check for precompilation
if (!use_precompile) {

  # Check repository content
27
  source_dir <- file.path(R_PACKAGE_SOURCE, "src", fsep = "/")
Laurae's avatar
Laurae committed
28
  setwd(source_dir)
29

Laurae's avatar
Laurae committed
30
  # Prepare building package
31
  build_dir <- file.path(source_dir, "build", fsep = "/")
Laurae's avatar
Laurae committed
32
33
  dir.create(build_dir, recursive = TRUE, showWarnings = FALSE)
  setwd(build_dir)
34

35
  # Prepare installation steps
Guolin Ke's avatar
Guolin Ke committed
36
  cmake_cmd <- "cmake "
37
  build_cmd <- "make _lightgbm"
38
  lib_folder <- file.path(R_PACKAGE_SOURCE, "src", fsep = "/")
39

Guolin Ke's avatar
Guolin Ke committed
40
41
42
  if (use_gpu) {
    cmake_cmd <- paste0(cmake_cmd, " -DUSE_GPU=ON ")
  }
43
44
45
  if (R_ver >= 3.5) {
    cmake_cmd <- paste0(cmake_cmd, " -DUSE_R35=ON ")
  }
Guolin Ke's avatar
Guolin Ke committed
46

47
48
49
50
51
52
53
54
55
  # Could NOT find OpenMP_C on Mojave workaround
  if (Sys.info()['sysname'] == 'Darwin' && !(grepl('^gcc', Sys.getenv('CC', '')) & grepl('^g\\+\\+', Sys.getenv('CXX', '')))) {
    cmake_cmd <- paste(cmake_cmd, ' -DOpenMP_C_FLAGS="-Xpreprocessor -fopenmp -I$(brew --prefix libomp)/include" ')
    cmake_cmd <- paste(cmake_cmd, ' -DOpenMP_C_LIB_NAMES="omp" ')
    cmake_cmd <- paste(cmake_cmd, ' -DOpenMP_CXX_FLAGS="-Xpreprocessor -fopenmp -I$(brew --prefix libomp)/include" ')
    cmake_cmd <- paste(cmake_cmd, ' -DOpenMP_CXX_LIB_NAMES="omp" ')
    cmake_cmd <- paste(cmake_cmd, ' -DOpenMP_omp_LIBRARY="$(brew --prefix libomp)/lib/libomp.dylib" ')
  }

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

Laurae's avatar
Laurae committed
89
90
91
  # Install
  system(paste0(cmake_cmd, " .."))
  system(build_cmd)
92
  src <- file.path(lib_folder, paste0("lib_lightgbm", SHLIB_EXT), fsep = "/")
93

Laurae's avatar
Laurae committed
94
} else {
95

Laurae's avatar
Laurae committed
96
  # Has precompiled package
97
98
99
100
  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 = "/"))) {
101
    src <- file.path(lib_folder, paste0("Release/lib_lightgbm", SHLIB_EXT), fsep = "/")
Laurae's avatar
Laurae committed
102
  } else {
103
    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
104
  }
105

106
107
}

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