Skip to content

Commit

Permalink
[R-package] Fallback to MinGW when VS build fails (#664)
Browse files Browse the repository at this point in the history
* Change VS -> MinGW for CRAN

* Documentation to switch to MinGW by default

* Force cmake to run twice

* Try again dual build for Rtools

* Switch to cmake for building twice

* Try with Visual Studio as default, MinGW as fallback

* Try to remove VS appropriately when failing

* Attempt to get rid of build folder first

* Switch to unlink from rm rf

* Change lib_folder correctly when VS fails

* Add README updates from 1c8355c

* Update default compiler doc and add GPU online install doc

* Better GPU doc
  • Loading branch information
Laurae2 authored and guolinke committed Oct 9, 2017
1 parent 0fd487a commit 0b499da
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 17 deletions.
32 changes: 22 additions & 10 deletions R-package/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,22 @@ Note: 32-bit R/Rtools is not supported.

Installing [Rtools](https://cran.r-project.org/bin/windows/Rtools/) is mandatory, and only support the 64-bit version.

[cmake](https://cmake.org/) must be version 3.8 or higher.
[cmake](https://cmake.org/) must be version 3.8 or higher.

The default compiler is Visual Studio (or [MS Build](https://www.visualstudio.com/downloads/#build-tools-for-visual-studio-2017)) in Windows. You also can use [MinGW64](https://sourceforge.net/projects/mingw-w64/files/Toolchains%20targetting%20Win64/Personal%20Builds/mingw-builds/) (x86_64-posix-seh) to compile by setting `use_mingw` to `TRUE` in `R-package/src/install.libs.R`. For MinGW users who wants to install online, please check the end of this document for installation using a helper package ([Laurae2/lgbdl](https://github.com/Laurae2/lgbdl/)).
The default compiler is Visual Studio (or [MS Build](https://www.visualstudio.com/downloads/#build-tools-for-visual-studio-2017)) in Windows, with an automatic fallback to Rtools or any [MinGW64](https://sourceforge.net/projects/mingw-w64/files/Toolchains%20targetting%20Win64/Personal%20Builds/mingw-builds/) (x86_64-posix-seh) available (this means if you have only Rtools and cmake, it will compile fine).

It is recommended to use *Visual Studio* for its better multi-threading efficency in Windows for many core systems. For very simple systems (dual core computers or worse), MinGW64 is recommended for maximum performance. If you do not know what to choose, it is recommended to use [Visual Studio](https://www.visualstudio.com/downloads/#build-tools-for-visual-studio-2017).
To force the usage of Rtools / MinGW, you can set `use_mingw` to `TRUE` in `R-package/src/install.libs.R`.

#### Mac OS X Preparation
For users who wants to install online with GPU or want to choose a specific compiler, please check the end of this document for installation using a helper package ([Laurae2/lgbdl](https://github.com/Laurae2/lgbdl/)).

It is recommended to use *Visual Studio* for its better multi-threading efficency in Windows for many core systems. For very simple systems (dual core computers or worse), MinGW64 is recommended for maximum performance. If you do not know what to choose, it is recommended to use [Visual Studio](https://www.visualstudio.com/downloads/#build-tools-for-visual-studio-2017), the default compiler.

Gcc with OpenMP support must be installed first. Refer to [wiki](https://github.com/Microsoft/LightGBM/wiki/Installation-Guide#osx) for installing gcc with OpenMP support.
#### Mac OS X Preparation

gcc with OpenMP support must be installed first. Refer to [wiki](https://github.com/Microsoft/LightGBM/wiki/Installation-Guide#osx) for installing gcc with OpenMP support.

### Install

Install LightGBM R-package with the following command:

```sh
Expand Down Expand Up @@ -53,7 +57,7 @@ You can also install directly from R using the repository with `devtools`:

```r
library(devtools)
options(devtools.install.args = "--no-multiarch")
options(devtools.install.args = "--no-multiarch") # if you have 64-bit R only, you can skip this
install_github("Microsoft/LightGBM", subdir = "R-package")
```

Expand Down Expand Up @@ -93,13 +97,12 @@ In addition, if you are using a Visual Studio precompiled DLL, assuming you do n

Once you have all this setup, you can use `lgb.dl` from `lgbdl` package to install LightGBM from repository.

For instance, you can install the R package from LightGBM master commit of GitHub using the following from R:
For instance, you can install the R package from LightGBM master commit of GitHub with Visual Studio using the following from R:

```r
lgb.dl(commit = "master",
compiler = "gcc",
repo = "https://github.com/Microsoft/LightGBM",
cores = 4)
compiler = "vs",
repo = "https://github.com/Microsoft/LightGBM")
```

You may also install using a precompiled dll/lib using the following from R:
Expand All @@ -110,6 +113,15 @@ lgb.dl(commit = "master",
repo = "https://github.com/Microsoft/LightGBM")
```

You may also install online using a LightGBM with proper GPU support using Visual Studio (as an example here) using the following from R:

```r
lgb.dl(commit = "master",
compiler = "vs", # Remove this for MinGW + GPU installation
repo = "https://github.com/Microsoft/LightGBM",
use_gpu = TRUE)
```

For more details about options, please check [Laurae2/lgbdl](https://github.com/Laurae2/lgbdl/) R-package.

Examples
Expand Down
23 changes: 16 additions & 7 deletions R-package/src/install.libs.R
Original file line number Diff line number Diff line change
Expand Up @@ -42,19 +42,28 @@ if (!use_precompile) {
setwd(build_dir)

# Prepare installation steps
cmake_cmd <- "cmake "
build_cmd <- "make _lightgbm -j4"
cmake_base <- "cmake "
build_cmd <- "make _lightgbm -j"
lib_folder <- file.path(R_PACKAGE_SOURCE, "src", fsep = "/")

# Check if Windows installation (for gcc vs Visual Studio)
if (WINDOWS) {
if (use_mingw) {
cmake_cmd <- paste0(cmake_cmd, " -G \"MinGW Makefiles\" ")
build_cmd <- "mingw32-make.exe _lightgbm -j4"
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
} else {
cmake_cmd <- paste0(cmake_cmd, " -DCMAKE_GENERATOR_PLATFORM=x64 ")
build_cmd <- "cmake --build . --target _lightgbm --config Release"
lib_folder <- file.path(R_PACKAGE_SOURCE, "src/Release", fsep = "/")
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 = "/")
}
}
}

Expand Down

0 comments on commit 0b499da

Please sign in to comment.