install.libs.R 4.74 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) {
Guolin Ke's avatar
Guolin Ke committed
7
8
9
  stop("Only support 64-bit R, please check your the version of your R and Rtools.")
}

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
  print("Warning: unmatched R_INTERNALS_UUID, may cannot run normally.")
16
}
James Lamb's avatar
James Lamb committed
17
18

# Move in CMakeLists.txt
19
if (!file.copy("../inst/bin/CMakeLists.txt", "CMakeLists.txt", overwrite = TRUE)) {
James Lamb's avatar
James Lamb committed
20
21
22
  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
  # Could NOT find OpenMP_C on Mojave workaround
48
49
50
  # Using this kind-of complicated pattern to avoid matching to
  # things like "pgcc"
  using_gcc <- grepl(
51
52
    pattern = "^gcc$|[/\\]+gcc$|^gcc\\-[0-9]+$|[/\\]+gcc\\-[0-9]+$"
    , x = Sys.getenv("CC", "")
53
54
  )
  using_gpp <- grepl(
55
56
    pattern = "^g\\+\\+$|[/\\]+g\\+\\+$|^g\\+\\+\\-[0-9]+$|[/\\]+g\\+\\+\\-[0-9]+$"
    , x = Sys.getenv("CXX", "")
57
  )
58
  on_mac <- Sys.info()["sysname"] == "Darwin"
59
  if (on_mac && !(using_gcc & using_gpp)) {
60
61
62
63
64
65
66
    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" ')
  }

67
  # Check if Windows installation (for gcc vs Visual Studio)
Laurae's avatar
Laurae committed
68
69
  if (WINDOWS) {
    if (use_mingw) {
Guolin Ke's avatar
Guolin Ke committed
70
      cmake_cmd <- paste0(cmake_cmd, " -G \"MinGW Makefiles\" ")
71
      build_cmd <- "mingw32-make.exe _lightgbm"
72
      system(paste0(cmake_cmd, " ..")) # Must build twice for Windows due sh.exe in Rtools
Laurae's avatar
Laurae committed
73
    } else {
74
      try_vs <- 0L
Guolin Ke's avatar
Guolin Ke committed
75
      local_vs_def <- ""
76
      vs_versions <- c("Visual Studio 16 2019", "Visual Studio 15 2017", "Visual Studio 14 2015")
77
      for (vs in vs_versions) {
78
        vs_def <- paste0(" -G \"", vs, "\" -A x64")
Guolin Ke's avatar
Guolin Ke committed
79
80
        tmp_cmake_cmd <- paste0(cmake_cmd, vs_def)
        try_vs <- system(paste0(tmp_cmake_cmd, " .."))
81
82
        if (try_vs == 0L) {
          local_vs_def <- vs_def
Guolin Ke's avatar
Guolin Ke committed
83
          break
84
85
        } else {
          unlink("./*", recursive = TRUE) # Clean up build directory
Guolin Ke's avatar
Guolin Ke committed
86
87
        }
      }
88
      if (try_vs == 1L) {
Guolin Ke's avatar
Guolin Ke committed
89
        cmake_cmd <- paste0(cmake_cmd, " -G \"MinGW Makefiles\" ") # Switch to MinGW on failure, try build once
90
        system(paste0(cmake_cmd, " ..")) # Must build twice for Windows due sh.exe in Rtools
91
        build_cmd <- "mingw32-make.exe _lightgbm"
92
      } else {
Guolin Ke's avatar
Guolin Ke committed
93
        cmake_cmd <- paste0(cmake_cmd, local_vs_def)
94
95
96
        build_cmd <- "cmake --build . --target _lightgbm  --config Release"
        lib_folder <- file.path(R_PACKAGE_SOURCE, "src/Release", fsep = "/")
      }
97
98
    }
  }
99

Laurae's avatar
Laurae committed
100
101
102
  # Install
  system(paste0(cmake_cmd, " .."))
  system(build_cmd)
103
  src <- file.path(lib_folder, paste0("lib_lightgbm", SHLIB_EXT), fsep = "/")
104

Laurae's avatar
Laurae committed
105
} else {
106

Laurae's avatar
Laurae committed
107
  # Has precompiled package
108
  lib_folder <- file.path(R_PACKAGE_SOURCE, "../", fsep = "/")
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
  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
128
  } else {
129
130
    # Expected result: installation will fail if it is not here or any other
    src <- windows_shared_object_file
131
132
133
  }
}

Laurae's avatar
Laurae committed
134
# Check installation correctness
135
dest <- file.path(R_PACKAGE_DIR, paste0("libs", R_ARCH), fsep = "/")
136
dir.create(dest, recursive = TRUE, showWarnings = FALSE)
Laurae's avatar
Laurae committed
137
138
if (file.exists(src)) {
  cat("Found library file: ", src, " to move to ", dest, sep = "")
139
140
  file.copy(src, dest, overwrite = TRUE)
} else {
Laurae's avatar
Laurae committed
141
  stop(paste0("Cannot find lib_lightgbm", SHLIB_EXT))
142
}