libxgboost4j is now part of the CMake build (#2373)

* [jvm-packages] Added libxgboost4j to CMake build

* [jvm-packages] Wired CMake build into create_jni.sh

* User newer CMake version on Travis

* Lowered CMake version constraints

* Fixed various quirks in the new CMake build
This commit is contained in:
Sergei Lebedev 2017-06-04 02:14:51 +02:00 committed by Tianqi Chen
parent ed6384ecbf
commit 2d9052bc7d
5 changed files with 48 additions and 25 deletions

View File

@ -45,7 +45,10 @@ addons:
apt: apt:
sources: sources:
- ubuntu-toolchain-r-test - ubuntu-toolchain-r-test
- george-edison55-precise-backports
packages: packages:
- cmake
- cmake-data
- doxygen - doxygen
- wget - wget
- libcurl4-openssl-dev - libcurl4-openssl-dev

View File

@ -1,4 +1,4 @@
cmake_minimum_required (VERSION 3.5) cmake_minimum_required (VERSION 3.2)
project (xgboost) project (xgboost)
find_package(OpenMP) find_package(OpenMP)
@ -91,6 +91,8 @@ else()
set(CMAKE_SHARED_LIBRARY_PREFIX "") set(CMAKE_SHARED_LIBRARY_PREFIX "")
endif() endif()
set(LINK_LIBRARIES dmlccore rabit)
option(PLUGIN_UPDATER_GPU "Build GPU accelerated tree construction plugin") option(PLUGIN_UPDATER_GPU "Build GPU accelerated tree construction plugin")
if(PLUGIN_UPDATER_GPU) if(PLUGIN_UPDATER_GPU)
@ -109,16 +111,29 @@ if(PLUGIN_UPDATER_GPU)
cuda_add_library(updater_gpu STATIC cuda_add_library(updater_gpu STATIC
${CUDA_SOURCES} ${CUDA_SOURCES}
) )
set(LINK_LIBRARIES ${LINK_LIBRARIES} updater_gpu)
endif() endif()
add_executable(xgboost ${SOURCES}) add_library(objxgboost OBJECT ${SOURCES})
add_library(libxgboost SHARED ${SOURCES}) set_target_properties(${objxgboost} PROPERTIES POSITION_INDEPENDENT_CODE 1)
target_link_libraries(xgboost dmlccore rabit)
target_link_libraries(libxgboost dmlccore rabit)
add_library(libxgboost SHARED $<TARGET_OBJECTS:objxgboost>)
add_executable(xgboost $<TARGET_OBJECTS:objxgboost>)
target_link_libraries(xgboost ${LINK_LIBRARIES})
target_link_libraries(libxgboost ${LINK_LIBRARIES})
if(PLUGIN_UPDATER_GPU) option(JVM_BINDINGS "Build JVM bindings" OFF)
target_link_libraries(xgboost updater_gpu)
target_link_libraries(libxgboost updater_gpu) if(JVM_BINDINGS)
find_package(JNI QUIET REQUIRED)
include_directories(${JNI_INCLUDE_DIRS} jvm-packages/xgboost4j/src/native)
add_library(libxgboost4j SHARED
$<TARGET_OBJECTS:objxgboost>
jvm-packages/xgboost4j/src/native/xgboost4j.cpp)
target_link_libraries(libxgboost4j
${LINK_LIBRARIES}
${JNI_LIBRARIES})
endif() endif()

View File

@ -9,18 +9,21 @@ pushd `dirname $0` > /dev/null
#settings according to os #settings according to os
dl="so" dl="so"
dis_omp=0 USE_OMP=ON
if [ $(uname) == "Darwin" ]; then if [ $(uname) == "Darwin" ]; then
export JAVA_HOME=$(/usr/libexec/java_home) export JAVA_HOME=$(/usr/libexec/java_home)
dl="dylib" dl="dylib"
#change this to 0 if your compiler support openmp #change this to 0 if your compiler support openmp
dis_omp=1 USE_OMP=OFF
fi fi
cd .. cd ..
make jvm no_omp=${dis_omp} mkdir -p build
cd jvm-packages cd build
cmake .. -DJVM_BINDINGS:BOOL=ON -DUSE_OPENMP:BOOL=${USE_OMP}
make
cd ../jvm-packages
echo "move native lib" echo "move native lib"
libPath="xgboost4j/src/main/resources/lib" libPath="xgboost4j/src/main/resources/lib"
@ -29,7 +32,7 @@ if [ ! -d "$libPath" ]; then
fi fi
rm -f xgboost4j/src/main/resources/lib/libxgboost4j.${dl} rm -f xgboost4j/src/main/resources/lib/libxgboost4j.${dl}
mv lib/libxgboost4j.so xgboost4j/src/main/resources/lib/libxgboost4j.${dl} cp ../lib/libxgboost4j.${dl} xgboost4j/src/main/resources/lib/libxgboost4j.${dl}
# copy python to native resources # copy python to native resources
cp ../dmlc-core/tracker/dmlc_tracker/tracker.py xgboost4j/src/main/resources/tracker.py cp ../dmlc-core/tracker/dmlc_tracker/tracker.py xgboost4j/src/main/resources/tracker.py
# copy test data files # copy test data files

View File

@ -31,7 +31,7 @@ class NativeLibLoader {
private static final Log logger = LogFactory.getLog(NativeLibLoader.class); private static final Log logger = LogFactory.getLog(NativeLibLoader.class);
private static boolean initialized = false; private static boolean initialized = false;
private static final String nativePath = "../lib/"; private static final String nativePath = "../../lib/";
private static final String nativeResourcePath = "/lib/"; private static final String nativeResourcePath = "/lib/";
private static final String[] libNames = new String[]{"xgboost4j"}; private static final String[] libNames = new String[]{"xgboost4j"};

View File

@ -25,7 +25,7 @@
// set handle // set handle
void setHandle(JNIEnv *jenv, jlongArray jhandle, void* handle) { void setHandle(JNIEnv *jenv, jlongArray jhandle, void* handle) {
#ifdef __APPLE__ #ifdef __APPLE__
long out = (long) handle; jlong out = (long) handle;
#else #else
int64_t out = (int64_t) handle; int64_t out = (int64_t) handle;
#endif #endif
@ -76,7 +76,8 @@ XGB_EXTERN_C int XGBoost4jCallbackDataIterNext(
batch, jenv->GetFieldID(batchClass, "featureValue", "[F")); batch, jenv->GetFieldID(batchClass, "featureValue", "[F"));
XGBoostBatchCSR cbatch; XGBoostBatchCSR cbatch;
cbatch.size = jenv->GetArrayLength(joffset) - 1; cbatch.size = jenv->GetArrayLength(joffset) - 1;
cbatch.offset = jenv->GetLongArrayElements(joffset, 0); cbatch.offset = reinterpret_cast<long *>(
jenv->GetLongArrayElements(joffset, 0));
if (jlabel != nullptr) { if (jlabel != nullptr) {
cbatch.label = jenv->GetFloatArrayElements(jlabel, 0); cbatch.label = jenv->GetFloatArrayElements(jlabel, 0);
CHECK_EQ(jenv->GetArrayLength(jlabel), static_cast<long>(cbatch.size)) CHECK_EQ(jenv->GetArrayLength(jlabel), static_cast<long>(cbatch.size))
@ -103,7 +104,8 @@ XGB_EXTERN_C int XGBoost4jCallbackDataIterNext(
CHECK_EQ((*set_function)(set_function_handle, cbatch), 0) CHECK_EQ((*set_function)(set_function_handle, cbatch), 0)
<< XGBGetLastError(); << XGBGetLastError();
// release the elements. // release the elements.
jenv->ReleaseLongArrayElements(joffset, cbatch.offset, 0); jenv->ReleaseLongArrayElements(
joffset, reinterpret_cast<jlong *>(cbatch.offset), 0);
jenv->DeleteLocalRef(joffset); jenv->DeleteLocalRef(joffset);
if (jlabel != nullptr) { if (jlabel != nullptr) {
jenv->ReleaseFloatArrayElements(jlabel, cbatch.label, 0); jenv->ReleaseFloatArrayElements(jlabel, cbatch.label, 0);