Skip to content

Commit 153d995

Browse files
authored
Fix building XGBoost with libomp 15 (dmlc#8384) (dmlc#8387)
1 parent 463313d commit 153d995

File tree

3 files changed

+36
-6
lines changed

3 files changed

+36
-6
lines changed

CMakeLists.txt

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,8 +171,24 @@ if (USE_OPENMP)
171171
# Require CMake 3.16+ on Mac OSX, as previous versions of CMake had trouble locating
172172
# OpenMP on Mac. See https://github.com/dmlc/xgboost/pull/5146#issuecomment-568312706
173173
cmake_minimum_required(VERSION 3.16)
174-
endif (APPLE)
175-
find_package(OpenMP REQUIRED)
174+
find_package(OpenMP)
175+
if (NOT OpenMP_FOUND)
176+
# Try again with extra path info; required for libomp 15+ from Homebrew
177+
execute_process(COMMAND brew --prefix libomp
178+
OUTPUT_VARIABLE HOMEBREW_LIBOMP_PREFIX
179+
OUTPUT_STRIP_TRAILING_WHITESPACE)
180+
set(OpenMP_C_FLAGS
181+
"-Xpreprocessor -fopenmp -I${HOMEBREW_LIBOMP_PREFIX}/include")
182+
set(OpenMP_CXX_FLAGS
183+
"-Xpreprocessor -fopenmp -I${HOMEBREW_LIBOMP_PREFIX}/include")
184+
set(OpenMP_C_LIB_NAMES omp)
185+
set(OpenMP_CXX_LIB_NAMES omp)
186+
set(OpenMP_omp_LIBRARY ${HOMEBREW_LIBOMP_PREFIX}/lib/libomp.dylib)
187+
find_package(OpenMP REQUIRED)
188+
endif ()
189+
else ()
190+
find_package(OpenMP REQUIRED)
191+
endif ()
176192
endif (USE_OPENMP)
177193
#Add for IBM i
178194
if (${CMAKE_SYSTEM_NAME} MATCHES "OS400")

R-package/configure

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2709,8 +2709,15 @@ fi
27092709

27102710
if test `uname -s` = "Darwin"
27112711
then
2712-
OPENMP_CXXFLAGS='-Xclang -fopenmp'
2713-
OPENMP_LIB='-lomp'
2712+
if command -v brew &> /dev/null
2713+
then
2714+
HOMEBREW_LIBOMP_PREFIX=`brew --prefix libomp`
2715+
else
2716+
# Homebrew not found
2717+
HOMEBREW_LIBOMP_PREFIX=''
2718+
fi
2719+
OPENMP_CXXFLAGS="-Xpreprocessor -fopenmp -I${HOMEBREW_LIBOMP_PREFIX}/include"
2720+
OPENMP_LIB="-lomp -L${HOMEBREW_LIBOMP_PREFIX}/lib"
27142721
ac_pkg_openmp=no
27152722
{ $as_echo "$as_me:${as_lineno-$LINENO}: checking whether OpenMP will work in a package" >&5
27162723
$as_echo_n "checking whether OpenMP will work in a package... " >&6; }

R-package/configure.ac

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,15 @@ fi
2828

2929
if test `uname -s` = "Darwin"
3030
then
31-
OPENMP_CXXFLAGS='-Xclang -fopenmp'
32-
OPENMP_LIB='-lomp'
31+
if command -v brew &> /dev/null
32+
then
33+
HOMEBREW_LIBOMP_PREFIX=`brew --prefix libomp`
34+
else
35+
# Homebrew not found
36+
HOMEBREW_LIBOMP_PREFIX=''
37+
fi
38+
OPENMP_CXXFLAGS="-Xpreprocessor -fopenmp -I${HOMEBREW_LIBOMP_PREFIX}/include"
39+
OPENMP_LIB="-lomp -L${HOMEBREW_LIBOMP_PREFIX}/lib"
3340
ac_pkg_openmp=no
3441
AC_MSG_CHECKING([whether OpenMP will work in a package])
3542
AC_LANG_CONFTEST([AC_LANG_PROGRAM([[#include <omp.h>]], [[ return (omp_get_max_threads() <= 1); ]])])

0 commit comments

Comments
 (0)