diff --git a/CMakeLists.txt b/CMakeLists.txt index 3a11a977140e..5a7baed97089 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -7,9 +7,14 @@ OPTION(USE_TIMETAG "Set to ON to output time costs" OFF) OPTION(USE_CUDA "Enable CUDA-accelerated training (EXPERIMENTAL)" OFF) OPTION(USE_DEBUG "Set to ON for Debug mode" OFF) OPTION(USE_SANITIZER "Use santizer flags" OFF) -SET(ENABLED_SANITIZERS "address" "leak" "undefined" CACHE STRING - "Semicolon separated list of sanitizer names. E.g 'address;leak'. Supported sanitizers are -address, leak, undefined and thread.") +SET( + ENABLED_SANITIZERS + "address" "leak" "undefined" + CACHE + STRING + "Semicolon separated list of sanitizer names, e.g., 'address;leak'. \ +Supported sanitizers are address, leak, undefined and thread." +) OPTION(BUILD_CPP_TEST "Build C++ tests with Google Test" OFF) OPTION(BUILD_STATIC_LIB "Build static library" OFF) OPTION(__BUILD_FOR_R "Set to ON if building lib_lightgbm for use with the R package" OFF) @@ -63,7 +68,10 @@ elseif(CMAKE_CXX_COMPILER_ID STREQUAL "AppleClang") cmake_minimum_required(VERSION 3.16) elseif(MSVC) if(MSVC_VERSION LESS 1900) - message(FATAL_ERROR "The compiler ${CMAKE_CXX_COMPILER} doesn't support required C++11 features. Please use a newer MSVC.") + message( + FATAL_ERROR + "The compiler ${CMAKE_CXX_COMPILER} doesn't support required C++11 features. Please use a newer MSVC." + ) endif() cmake_minimum_required(VERSION 3.8) endif() @@ -197,17 +205,20 @@ if(USE_CUDA) set(CMAKE_CUDA_STANDARD_REQUIRED ON) endif() - set(BASE_DEFINES - -DPOWER_FEATURE_WORKGROUPS=12 - -DUSE_CONSTANT_BUF=0 + set( + BASE_DEFINES + -DPOWER_FEATURE_WORKGROUPS=12 + -DUSE_CONSTANT_BUF=0 ) - set(ALLFEATS_DEFINES - ${BASE_DEFINES} - -DENABLE_ALL_FEATURES + set( + ALLFEATS_DEFINES + ${BASE_DEFINES} + -DENABLE_ALL_FEATURES ) - set(FULLDATA_DEFINES - ${ALLFEATS_DEFINES} - -DIGNORE_INDICES + set( + FULLDATA_DEFINES + ${ALLFEATS_DEFINES} + -DIGNORE_INDICES ) message(STATUS "ALLFEATS_DEFINES: ${ALLFEATS_DEFINES}") @@ -222,7 +233,8 @@ if(USE_CUDA) set(histograms ${histograms} PARENT_SCOPE) endif() target_compile_definitions( - histo${hsize}${hname} PRIVATE + histo${hsize}${hname} + PRIVATE -DCONST_HESSIAN=${hconst} ${hdir} ) @@ -278,21 +290,24 @@ if(${MM_MALLOC}) endif() if(UNIX OR MINGW OR CYGWIN) - SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -pthread -Wextra -Wall -Wno-ignored-attributes -Wno-unknown-pragmas -Wno-return-type") - if(USE_DEBUG) - SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g -O0") - else() - SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O3") - endif() - if(USE_SWIG) - SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-strict-aliasing") - endif() - if(NOT USE_OPENMP) - SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-unknown-pragmas -Wno-unused-private-field") - endif() - if(__BUILD_FOR_R AND CMAKE_CXX_COMPILER_ID STREQUAL "GNU") - SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-cast-function-type") - endif() + SET( + CMAKE_CXX_FLAGS + "${CMAKE_CXX_FLAGS} -std=c++11 -pthread -Wextra -Wall -Wno-ignored-attributes -Wno-unknown-pragmas -Wno-return-type" + ) + if(USE_DEBUG) + SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g -O0") + else() + SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O3") + endif() + if(USE_SWIG) + SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-strict-aliasing") + endif() + if(NOT USE_OPENMP) + SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-unknown-pragmas -Wno-unused-private-field") + endif() + if(__BUILD_FOR_R AND CMAKE_CXX_COMPILER_ID STREQUAL "GNU") + SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-cast-function-type") + endif() endif() if(WIN32 AND MINGW) @@ -300,7 +315,8 @@ if(WIN32 AND MINGW) endif() if(MSVC) - SET(variables + SET( + variables CMAKE_C_FLAGS_DEBUG CMAKE_C_FLAGS_MINSIZEREL CMAKE_C_FLAGS_RELEASE @@ -344,15 +360,17 @@ if(USE_MPI) include_directories(${MPI_CXX_INCLUDE_PATH}) endif() -file(GLOB SOURCES - src/boosting/*.cpp - src/io/*.cpp - src/metric/*.cpp - src/objective/*.cpp - src/network/*.cpp - src/treelearner/*.cpp +file( + GLOB + SOURCES + src/boosting/*.cpp + src/io/*.cpp + src/metric/*.cpp + src/objective/*.cpp + src/network/*.cpp + src/treelearner/*.cpp if(USE_CUDA) - src/treelearner/*.cu + src/treelearner/*.cu endif() ) @@ -375,7 +393,7 @@ if(BUILD_STATIC_LIB) else() add_library(_lightgbm SHARED) endif() -# LightGBM headers include openmp, cuda, R etc. headers, +# LightGBM headers include openmp, cuda, R etc. headers, # thus PUBLIC is required for building _lightgbm_swig target. target_link_libraries(_lightgbm PUBLIC lightgbm_capi_objs lightgbm_objs) @@ -395,28 +413,53 @@ if(USE_SWIG) set_target_properties(_lightgbm_swig PROPERTIES OUTPUT_NAME "lib_lightgbm_swig") if(WIN32) if(MINGW OR CYGWIN) - add_custom_command(TARGET _lightgbm_swig POST_BUILD + add_custom_command( + TARGET _lightgbm_swig + POST_BUILD COMMAND "${Java_JAVAC_EXECUTABLE}" -d . java/*.java - COMMAND "${CMAKE_COMMAND}" -E copy_if_different "${PROJECT_SOURCE_DIR}/lib_lightgbm.dll" com/microsoft/ml/lightgbm/windows/x86_64 - COMMAND "${CMAKE_COMMAND}" -E copy_if_different "${PROJECT_SOURCE_DIR}/lib_lightgbm_swig.dll" com/microsoft/ml/lightgbm/windows/x86_64 - COMMAND "${Java_JAR_EXECUTABLE}" -cf lightgbmlib.jar com) + COMMAND + "${CMAKE_COMMAND}" + -E + copy_if_different + "${PROJECT_SOURCE_DIR}/lib_lightgbm.dll" + com/microsoft/ml/lightgbm/windows/x86_64 + COMMAND + "${CMAKE_COMMAND}" + -E + copy_if_different + "${PROJECT_SOURCE_DIR}/lib_lightgbm_swig.dll" + com/microsoft/ml/lightgbm/windows/x86_64 + COMMAND "${Java_JAR_EXECUTABLE}" -cf lightgbmlib.jar com + ) else() - add_custom_command(TARGET _lightgbm_swig POST_BUILD + add_custom_command( + TARGET _lightgbm_swig + POST_BUILD COMMAND "${Java_JAVAC_EXECUTABLE}" -d . java/*.java COMMAND cp "${PROJECT_SOURCE_DIR}/Release/*.dll" com/microsoft/ml/lightgbm/windows/x86_64 - COMMAND "${Java_JAR_EXECUTABLE}" -cf lightgbmlib.jar com) + COMMAND "${Java_JAR_EXECUTABLE}" -cf lightgbmlib.jar com + ) endif() elseif(APPLE) - add_custom_command(TARGET _lightgbm_swig POST_BUILD - COMMAND "${Java_JAVAC_EXECUTABLE}" -d . java/*.java - COMMAND cp "${PROJECT_SOURCE_DIR}/*.dylib" com/microsoft/ml/lightgbm/osx/x86_64 - COMMAND cp "${PROJECT_SOURCE_DIR}/lib_lightgbm_swig.jnilib" com/microsoft/ml/lightgbm/osx/x86_64/lib_lightgbm_swig.dylib - COMMAND "${Java_JAR_EXECUTABLE}" -cf lightgbmlib.jar com) + add_custom_command( + TARGET _lightgbm_swig + POST_BUILD + COMMAND "${Java_JAVAC_EXECUTABLE}" -d . java/*.java + COMMAND cp "${PROJECT_SOURCE_DIR}/*.dylib" com/microsoft/ml/lightgbm/osx/x86_64 + COMMAND + cp + "${PROJECT_SOURCE_DIR}/lib_lightgbm_swig.jnilib" + com/microsoft/ml/lightgbm/osx/x86_64/lib_lightgbm_swig.dylib + COMMAND "${Java_JAR_EXECUTABLE}" -cf lightgbmlib.jar com + ) else() - add_custom_command(TARGET _lightgbm_swig POST_BUILD - COMMAND "${Java_JAVAC_EXECUTABLE}" -d . java/*.java - COMMAND cp "${PROJECT_SOURCE_DIR}/*.so" com/microsoft/ml/lightgbm/linux/x86_64 - COMMAND "${Java_JAR_EXECUTABLE}" -cf lightgbmlib.jar com) + add_custom_command( + TARGET _lightgbm_swig + POST_BUILD + COMMAND "${Java_JAVAC_EXECUTABLE}" -d . java/*.java + COMMAND cp "${PROJECT_SOURCE_DIR}/*.so" com/microsoft/ml/lightgbm/linux/x86_64 + COMMAND "${Java_JAR_EXECUTABLE}" -cf lightgbmlib.jar com + ) endif() endif() @@ -525,9 +568,11 @@ if(BUILD_CPP_TEST) target_link_libraries(testlightgbm PRIVATE lightgbm_objs GTest::GTest) endif() -install(TARGETS lightgbm _lightgbm - RUNTIME DESTINATION ${CMAKE_INSTALL_PREFIX}/bin - LIBRARY DESTINATION ${CMAKE_INSTALL_PREFIX}/lib - ARCHIVE DESTINATION ${CMAKE_INSTALL_PREFIX}/lib) +install( + TARGETS lightgbm _lightgbm + RUNTIME DESTINATION ${CMAKE_INSTALL_PREFIX}/bin + LIBRARY DESTINATION ${CMAKE_INSTALL_PREFIX}/lib + ARCHIVE DESTINATION ${CMAKE_INSTALL_PREFIX}/lib +) install(DIRECTORY ${LightGBM_HEADER_DIR}/LightGBM DESTINATION ${CMAKE_INSTALL_PREFIX}/include) diff --git a/cmake/IntegratedOpenCL.cmake b/cmake/IntegratedOpenCL.cmake index 8673f3bdead2..5473311196a7 100644 --- a/cmake/IntegratedOpenCL.cmake +++ b/cmake/IntegratedOpenCL.cmake @@ -31,7 +31,13 @@ if(NOT OpenCL-ICD-Loader_POPULATED) message(STATUS "Populated OpenCL ICD Loader") endif() list(APPEND INTEGRATED_OPENCL_INCLUDES ${OPENCL_ICD_LOADER_HEADERS_DIR}) -list(APPEND INTEGRATED_OPENCL_LIBRARIES ${opencl-icd-loader_BINARY_DIR}/Release/OpenCL.lib cfgmgr32.lib runtimeobject.lib) +list( + APPEND + INTEGRATED_OPENCL_LIBRARIES + ${opencl-icd-loader_BINARY_DIR}/Release/OpenCL.lib + cfgmgr32.lib + runtimeobject.lib +) list(APPEND INTEGRATED_OPENCL_DEFINITIONS CL_TARGET_OPENCL_VERSION=120) # Build Independent Boost libraries @@ -42,8 +48,62 @@ set(BOOST_BASE "${PROJECT_BINARY_DIR}/Boost") set(BOOST_BOOTSTRAP "${BOOST_BASE}/source/bootstrap.bat") set(BOOST_BUILD "${BOOST_BASE}/source/b2.exe") set(BOOST_FLAGS "") -list(APPEND BOOST_SUBMODULES "libs/algorithm" "libs/align" "libs/any" "libs/array" "libs/assert" "libs/bind" "libs/chrono" "libs/compute" "libs/concept_check" "libs/config" "libs/container" "libs/container_hash" "libs/core" "libs/detail" "libs/filesystem" "libs/foreach" "libs/format" "libs/function" "libs/function_types" "libs/fusion" "libs/headers" "libs/integer" "libs/io" "libs/iterator" "libs/lexical_cast" "libs/math" "libs/move" "libs/mpl" "libs/multi_index" "libs/numeric/conversion" "libs/optional" "libs/predef" "libs/preprocessor" "libs/property_tree" "libs/range" "libs/ratio" "libs/serialization" "libs/smart_ptr" "libs/static_assert" "libs/system" "libs/throw_exception" "libs/tuple" "libs/typeof" "libs/type_index" "libs/type_traits" "libs/utility" "libs/uuid" "libs/winapi" "tools/boost_install" "tools/build") -ExternalProject_Add(Boost +list( + APPEND + BOOST_SUBMODULES + "libs/algorithm" + "libs/align" + "libs/any" + "libs/array" + "libs/assert" + "libs/bind" + "libs/chrono" + "libs/compute" + "libs/concept_check" + "libs/config" + "libs/container" + "libs/container_hash" + "libs/core" + "libs/detail" + "libs/filesystem" + "libs/foreach" + "libs/format" + "libs/function" + "libs/function_types" + "libs/fusion" + "libs/headers" + "libs/integer" + "libs/io" + "libs/iterator" + "libs/lexical_cast" + "libs/math" + "libs/move" + "libs/mpl" + "libs/multi_index" + "libs/numeric/conversion" + "libs/optional" + "libs/predef" + "libs/preprocessor" + "libs/property_tree" + "libs/range" + "libs/ratio" + "libs/serialization" + "libs/smart_ptr" + "libs/static_assert" + "libs/system" + "libs/throw_exception" + "libs/tuple" + "libs/typeof" + "libs/type_index" + "libs/type_traits" + "libs/utility" + "libs/uuid" + "libs/winapi" + "tools/boost_install" + "tools/build" +) +ExternalProject_Add( + Boost TMP_DIR "${BOOST_BASE}/tmp" STAMP_DIR "${BOOST_BASE}/stamp" DOWNLOAD_DIR "${BOOST_BASE}/download" @@ -57,7 +117,21 @@ ExternalProject_Add(Boost UPDATE_COMMAND "" PATCH_COMMAND "" CONFIGURE_COMMAND ${BOOST_BOOTSTRAP} - BUILD_COMMAND ${BOOST_BUILD} -sBOOST_ROOT=${BOOST_BASE}/source -a -q -j ${J} --with-headers --with-chrono --with-filesystem --with-system link=static runtime-link=shared variant=release threading=multi cxxflags="${BOOST_FLAGS}" + BUILD_COMMAND + ${BOOST_BUILD} + -sBOOST_ROOT=${BOOST_BASE}/source + -a + -q + -j ${J} + --with-headers + --with-chrono + --with-filesystem + --with-system + link=static + runtime-link=shared + variant=release + threading=multi + cxxflags="${BOOST_FLAGS}" INSTALL_COMMAND "" ) set(BOOST_INCLUDE "${BOOST_BASE}/source" CACHE PATH "") @@ -75,9 +149,21 @@ if(MSVC) else() message(FATAL_ERROR "Unrecognized MSVC version number: ${MSVC_VERSION}") endif() - list(APPEND INTEGRATED_OPENCL_LIBRARIES ${BOOST_LIBRARY}/libboost_filesystem-vc${MSVC_TOOLCHAIN_ID}-mt-x64-${BOOST_VERSION_UNDERSCORE}.lib) - list(APPEND INTEGRATED_OPENCL_LIBRARIES ${BOOST_LIBRARY}/libboost_system-vc${MSVC_TOOLCHAIN_ID}-mt-x64-${BOOST_VERSION_UNDERSCORE}.lib) - list(APPEND INTEGRATED_OPENCL_LIBRARIES ${BOOST_LIBRARY}/libboost_chrono-vc${MSVC_TOOLCHAIN_ID}-mt-x64-${BOOST_VERSION_UNDERSCORE}.lib) + list( + APPEND + INTEGRATED_OPENCL_LIBRARIES + ${BOOST_LIBRARY}/libboost_filesystem-vc${MSVC_TOOLCHAIN_ID}-mt-x64-${BOOST_VERSION_UNDERSCORE}.lib + ) + list( + APPEND + INTEGRATED_OPENCL_LIBRARIES + ${BOOST_LIBRARY}/libboost_system-vc${MSVC_TOOLCHAIN_ID}-mt-x64-${BOOST_VERSION_UNDERSCORE}.lib + ) + list( + APPEND + INTEGRATED_OPENCL_LIBRARIES + ${BOOST_LIBRARY}/libboost_chrono-vc${MSVC_TOOLCHAIN_ID}-mt-x64-${BOOST_VERSION_UNDERSCORE}.lib + ) else() message(FATAL_ERROR "Integrated OpenCL build is not yet available for MinGW") endif() diff --git a/cmake/Sanitizer.cmake b/cmake/Sanitizer.cmake index 9ceaa1bc5125..a3768effac0d 100644 --- a/cmake/Sanitizer.cmake +++ b/cmake/Sanitizer.cmake @@ -28,14 +28,12 @@ macro(enable_sanitizers SANITIZERS) string(TOLOWER ${_san} _san) if(_san MATCHES "thread") if(${_use_other_sanitizers}) - message(FATAL_ERROR - "thread sanitizer is not compatible with ${_san} sanitizer.") + message(FATAL_ERROR "thread sanitizer is not compatible with ${_san} sanitizer.") endif() set(_use_thread_sanitizer 1) else() if(${_use_thread_sanitizer}) - message(FATAL_ERROR - "${_san} sanitizer is not compatible with thread sanitizer.") + message(FATAL_ERROR "${_san} sanitizer is not compatible with thread sanitizer.") endif() set(_use_other_sanitizers 1) endif() diff --git a/cmake/modules/FindLibR.cmake b/cmake/modules/FindLibR.cmake index 0331f47eb31d..470d0df081fd 100644 --- a/cmake/modules/FindLibR.cmake +++ b/cmake/modules/FindLibR.cmake @@ -44,8 +44,7 @@ function(create_rlib_for_msvc) find_program(DLLTOOL_EXE dlltool) if(NOT DLLTOOL_EXE) - message(FATAL_ERROR "dlltool.exe not found!\ - \nDo you have Rtools installed with its MinGW's bin/ in PATH?") + message(FATAL_ERROR "dlltool.exe not found!\nDo you have Rtools installed with its MinGW's bin/ in PATH?") endif() set(LIBR_MSVC_CORE_LIBRARY "${CMAKE_CURRENT_BINARY_DIR}/R.lib" CACHE PATH "R.lib filepath") @@ -62,7 +61,8 @@ function(create_rlib_for_msvc) "${CMAKE_CURRENT_BINARY_DIR}/make-r-def.R" "${LIBR_CORE_LIBRARY}" "${CMAKE_CURRENT_BINARY_DIR}/R.def" ) - execute_process(COMMAND ${DLLTOOL_EXE} + execute_process( + COMMAND ${DLLTOOL_EXE} "--input-def" "${CMAKE_CURRENT_BINARY_DIR}/R.def" "--output-lib" "${LIBR_MSVC_CORE_LIBRARY}" ) @@ -75,7 +75,10 @@ endfunction() if(CMAKE_R_VERSION) message(STATUS "R version passed into FindLibR.cmake: ${CMAKE_R_VERSION}") elseif(WIN32) - message(FATAL_ERROR "Expected CMAKE_R_VERSION to be passed in on Windows but none was provided. Check src/install.libs.R") + message( + FATAL_ERROR + "Expected CMAKE_R_VERSION to be passed in on Windows but none was provided. Check src/install.libs.R" + ) endif() @@ -149,8 +152,11 @@ if(NOT LIBR_EXECUTABLE) endif() if(NOT LIBR_HOME) - message(FATAL_ERROR "Unable to locate R executable.\ - \nEither add its location to PATH or provide it through the LIBR_EXECUTABLE CMake variable") + message( + FATAL_ERROR + "Unable to locate R executable.\ +\nEither add its location to PATH or provide it through the LIBR_EXECUTABLE CMake variable" + ) endif() # set exe location based on R_ARCH @@ -159,8 +165,11 @@ if(NOT LIBR_EXECUTABLE) endif() if(NOT LIBR_EXECUTABLE) - message(FATAL_ERROR "Unable to locate R executable.\ - \nEither add its location to PATH or provide it through the LIBR_EXECUTABLE CMake variable") + message( + FATAL_ERROR + "Unable to locate R executable.\ +\nEither add its location to PATH or provide it through the LIBR_EXECUTABLE CMake variable" + ) endif() endif() @@ -182,7 +191,14 @@ set(LIBR_EXECUTABLE ${LIBR_EXECUTABLE} CACHE PATH "R executable") set(LIBR_INCLUDE_DIRS ${LIBR_INCLUDE_DIRS} CACHE PATH "R include directory") # where is R.so / R.dll / libR.so likely to be found? -set(LIBR_PATH_HINTS "${CMAKE_CURRENT_BINARY_DIR}" "${LIBR_HOME}/lib" "${LIBR_HOME}/bin/${R_ARCH}" "${LIBR_HOME}/bin" "${LIBR_LIBRARIES}") +set( + LIBR_PATH_HINTS + "${CMAKE_CURRENT_BINARY_DIR}" + "${LIBR_HOME}/lib" + "${LIBR_HOME}/bin/${R_ARCH}" + "${LIBR_HOME}/bin" + "${LIBR_LIBRARIES}" +) # look for the core R library find_library( @@ -194,11 +210,11 @@ find_library( # starting from CMake 3.17, find_library() will not find .dll files by default # https://cmake.org/cmake/help/v3.17/release/3.17.html#other-changes if(WIN32 AND NOT LIBR_CORE_LIBRARY) - find_file( - LIBR_CORE_LIBRARY - NAME R.dll - HINTS ${LIBR_PATH_HINTS} - ) + find_file( + LIBR_CORE_LIBRARY + NAME R.dll + HINTS ${LIBR_PATH_HINTS} + ) endif() set(LIBR_CORE_LIBRARY ${LIBR_CORE_LIBRARY} CACHE PATH "R core shared library") @@ -216,7 +232,8 @@ endif() include(FindPackageHandleStandardArgs) if(WIN32 AND MSVC) - find_package_handle_standard_args(LibR DEFAULT_MSG + find_package_handle_standard_args( + LibR DEFAULT_MSG LIBR_HOME LIBR_EXECUTABLE LIBR_INCLUDE_DIRS @@ -224,7 +241,8 @@ if(WIN32 AND MSVC) LIBR_MSVC_CORE_LIBRARY ) else() - find_package_handle_standard_args(LibR DEFAULT_MSG + find_package_handle_standard_args( + LibR DEFAULT_MSG LIBR_HOME LIBR_EXECUTABLE LIBR_INCLUDE_DIRS