From 44d31540ecbc32c5c34040eb6de2f3b173c54e8d Mon Sep 17 00:00:00 2001 From: Kino Date: Sat, 24 Aug 2024 17:01:11 +0800 Subject: [PATCH 1/3] Fix OpenMP on macOS --- CMakeLists.txt | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index f2d31d9e010..a300feae031 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -287,6 +287,13 @@ endif() # OpenMP (optional) option(WITH_OPENMP "Build with parallelization using OpenMP" TRUE) if(WITH_OPENMP) + if(APPLE) + set(OpenMP_C_FLAGS "-Xpreprocessor -fopenmp -I/usr/local/opt/libomp/include") + set(OpenMP_C_LIB_NAMES "omp") + set(OpenMP_CXX_FLAGS "${OpenMP_C_FLAGS}") + set(OpenMP_CXX_LIB_NAMES "${OpenMP_C_LIB_NAMES}") + set(OpenMP_omp_LIBRARY "/usr/local/opt/libomp/lib/libomp.dylib") + endif() find_package(OpenMP COMPONENTS C CXX) endif() if(OpenMP_FOUND) From e91faf0e3d0fe9c952684cc319155ff231332710 Mon Sep 17 00:00:00 2001 From: Kino Date: Fri, 20 Sep 2024 23:42:32 +0800 Subject: [PATCH 2/3] Remove hardcode --- CMakeLists.txt | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index a300feae031..471ee04dd41 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -288,11 +288,17 @@ endif() option(WITH_OPENMP "Build with parallelization using OpenMP" TRUE) if(WITH_OPENMP) if(APPLE) - set(OpenMP_C_FLAGS "-Xpreprocessor -fopenmp -I/usr/local/opt/libomp/include") - set(OpenMP_C_LIB_NAMES "omp") - set(OpenMP_CXX_FLAGS "${OpenMP_C_FLAGS}") - set(OpenMP_CXX_LIB_NAMES "${OpenMP_C_LIB_NAMES}") - set(OpenMP_omp_LIBRARY "/usr/local/opt/libomp/lib/libomp.dylib") + # libomp 15.0+ from brew is keg-only, + # because it can override GCC headers and result in broken builds, + # so have to search in other locations. + execute_process(COMMAND brew --prefix libomp + OUTPUT_VARIABLE HOMEBREW_LIBOMP_PREFIX + OUTPUT_STRIP_TRAILING_WHITESPACE) + set(OpenMP_C_FLAGS "-Xpreprocessor -fopenmp -I${HOMEBREW_LIBOMP_PREFIX}/include") + set(OpenMP_CXX_FLAGS "-Xpreprocessor -fopenmp -I${HOMEBREW_LIBOMP_PREFIX}/include") + set(OpenMP_C_LIB_NAMES omp) + set(OpenMP_CXX_LIB_NAMES omp) + set(OpenMP_omp_LIBRARY ${HOMEBREW_LIBOMP_PREFIX}/lib/libomp.dylib) endif() find_package(OpenMP COMPONENTS C CXX) endif() From 3aa76094e4c6e911c87570adc9525a601b3613fc Mon Sep 17 00:00:00 2001 From: Kino Date: Sun, 20 Oct 2024 10:36:07 +0800 Subject: [PATCH 3/3] Adopt the way from LightGBM --- CMakeLists.txt | 31 +++++++++++++++++++------------ 1 file changed, 19 insertions(+), 12 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 471ee04dd41..5062b7274e7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -286,21 +286,28 @@ endif() # OpenMP (optional) option(WITH_OPENMP "Build with parallelization using OpenMP" TRUE) +option(USE_HOMEBREW_FALLBACK "(macOS-only) also look in 'brew --prefix' for libraries (e.g. OpenMP)" TRUE) if(WITH_OPENMP) if(APPLE) - # libomp 15.0+ from brew is keg-only, - # because it can override GCC headers and result in broken builds, - # so have to search in other locations. - execute_process(COMMAND brew --prefix libomp - OUTPUT_VARIABLE HOMEBREW_LIBOMP_PREFIX - OUTPUT_STRIP_TRAILING_WHITESPACE) - set(OpenMP_C_FLAGS "-Xpreprocessor -fopenmp -I${HOMEBREW_LIBOMP_PREFIX}/include") - set(OpenMP_CXX_FLAGS "-Xpreprocessor -fopenmp -I${HOMEBREW_LIBOMP_PREFIX}/include") - set(OpenMP_C_LIB_NAMES omp) - set(OpenMP_CXX_LIB_NAMES omp) - set(OpenMP_omp_LIBRARY ${HOMEBREW_LIBOMP_PREFIX}/lib/libomp.dylib) + find_package(OpenMP COMPONENTS C CXX) + if(NOT OpenMP_FOUND) + if(USE_HOMEBREW_FALLBACK) + # libomp 15.0+ from brew is keg-only, so have to search in other locations. + # See https://github.com/Homebrew/homebrew-core/issues/112107#issuecomment-1278042927. + execute_process(COMMAND brew --prefix libomp + OUTPUT_VARIABLE HOMEBREW_LIBOMP_PREFIX + OUTPUT_STRIP_TRAILING_WHITESPACE) + set(OpenMP_C_FLAGS "-Xpreprocessor -fopenmp -I${HOMEBREW_LIBOMP_PREFIX}/include") + set(OpenMP_CXX_FLAGS "-Xpreprocessor -fopenmp -I${HOMEBREW_LIBOMP_PREFIX}/include") + set(OpenMP_C_LIB_NAMES omp) + set(OpenMP_CXX_LIB_NAMES omp) + set(OpenMP_omp_LIBRARY ${HOMEBREW_LIBOMP_PREFIX}/lib/libomp.dylib) + endif() + find_package(OpenMP COMPONENTS C CXX) + endif() + else() + find_package(OpenMP COMPONENTS C CXX) endif() - find_package(OpenMP COMPONENTS C CXX) endif() if(OpenMP_FOUND) string(APPEND CMAKE_C_FLAGS " ${OpenMP_C_FLAGS}")