Skip to content

Commit

Permalink
fix R build for version 3.5 and above (#1346)
Browse files Browse the repository at this point in the history
  • Loading branch information
guolinke committed Apr 28, 2018
1 parent fb55d72 commit 4967709
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 4 deletions.
5 changes: 5 additions & 0 deletions CMakeLists.txt
Expand Up @@ -13,6 +13,7 @@ OPTION(USE_OPENMP "Enable OpenMP" ON)
OPTION(USE_GPU "Enable GPU-acclerated training (EXPERIMENTAL)" OFF)
OPTION(USE_SWIG "Enable SWIG to generate Java API" OFF)
OPTION(USE_HDFS "Enable HDFS support (EXPERIMENTAL)" OFF)
OPTION(USE_R35 "Set to ON if your R version is not smaller than 3.5" OFF)

if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
if(CMAKE_CXX_COMPILER_VERSION VERSION_LESS "4.8.2")
Expand Down Expand Up @@ -49,6 +50,10 @@ if(USE_SWIG)
include_directories($ENV{JAVA_HOME}/include/linux)
endif(USE_SWIG)

if(USE_R35)
ADD_DEFINITIONS(-DR_VER_ABOVE_35)
endif()

if(USE_MPI)
find_package(MPI REQUIRED)
ADD_DEFINITIONS(-DUSE_MPI)
Expand Down
2 changes: 1 addition & 1 deletion R-package/README.md
Expand Up @@ -61,7 +61,7 @@ options(devtools.install.args = "--no-multiarch") # if you have 64-bit R only, y
install_github("Microsoft/LightGBM", subdir = "R-package")
```

If you are using a precompiled dll/lib locally, you can move the dll/lib into LightGBM root folder, modify `LightGBM/R-package/src/install.libs.R`'s 2nd line (change `use_precompile <- FALSE` to `use_precompile <- TRUE`), and install R-package as usual.
If you are using a precompiled dll/lib locally, you can move the dll/lib into LightGBM root folder, modify `LightGBM/R-package/src/install.libs.R`'s 2nd line (change `use_precompile <- FALSE` to `use_precompile <- TRUE`), and install R-package as usual. ** NOTE: If your R version is not smaller than 3.5.0, you should set `DUSE_R35=ON` in cmake options when build precompiled dll/lib **.

When your package installation is done, you can check quickly if your LightGBM R package is working by running the following:

Expand Down
10 changes: 10 additions & 0 deletions R-package/src/install.libs.R
Expand Up @@ -7,6 +7,13 @@ if (.Machine$sizeof.pointer != 8){
stop("Only support 64-bit R, please check your the version of your R and Rtools.")
}

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

if (!(R_int_UUID == "0310d4b8-ccb1-4bb8-ba94-d36a55f60262"
|| R_int_UUID == "2fdf6c18-697a-4ba7-b8ef-11c0d92f1327")){
print("Warning: unmatched R_INTERNALS_UUID, may cannot run normally.")
}
# Check for precompilation
if (!use_precompile) {

Expand Down Expand Up @@ -47,6 +54,9 @@ if (!use_precompile) {
if (use_gpu) {
cmake_cmd <- paste0(cmake_cmd, " -DUSE_GPU=ON ")
}
if (R_ver >= 3.5) {
cmake_cmd <- paste0(cmake_cmd, " -DUSE_R35=ON ")
}

# Check if Windows installation (for gcc vs Visual Studio)
if (WINDOWS) {
Expand Down
38 changes: 35 additions & 3 deletions include/LightGBM/R_object_helper.h
Expand Up @@ -9,6 +9,36 @@
#include <cstdint>

#define TYPE_BITS 5
// use .Internal(internalsID()) to uuid
#define R_INTERNALS_UUID "2fdf6c18-697a-4ba7-b8ef-11c0d92f1327"


#ifdef R_VER_ABOVE_35
#define NAMED_BITS 16
struct lgbm_sxpinfo {
unsigned int type : 5;
unsigned int scalar : 1;
unsigned int obj : 1;
unsigned int alt : 1;
unsigned int gp : 16;
unsigned int mark : 1;
unsigned int debug : 1;
unsigned int trace : 1;
unsigned int spare : 1;
unsigned int gcgen : 1;
unsigned int gccls : 3;
unsigned int named : NAMED_BITS;
unsigned int extra : 32 - NAMED_BITS;
};

// 64bit pointer
#if INTPTR_MAX == INT64_MAX
typedef ptrdiff_t R_xlen_t;
#else
typedef int R_xlen_t;
#endif

#else
struct lgbm_sxpinfo {
unsigned int type : 5;
unsigned int obj : 1;
Expand All @@ -22,6 +52,9 @@ struct lgbm_sxpinfo {
unsigned int gccls : 3;
};

typedef int R_xlen_t;
#endif

struct lgbm_primsxp {
int offset;
};
Expand Down Expand Up @@ -71,8 +104,8 @@ typedef struct LGBM_SER {
} LGBM_SER, *LGBM_SE;

struct lgbm_vecsxp {
int length;
int truelength;
R_xlen_t length;
R_xlen_t truelength;
};

typedef struct VECTOR_SER {
Expand All @@ -96,7 +129,6 @@ typedef union { VECTOR_SER s; double align; } SEXPREC_ALIGN;

#define R_IS_NULL(x) ((*(LGBM_SE)(x)).sxpinfo.type == 0)


// 64bit pointer
#if INTPTR_MAX == INT64_MAX

Expand Down

0 comments on commit 4967709

Please sign in to comment.