From eec0fc5b982ac9d721c2b99a7a15bc390da4379e Mon Sep 17 00:00:00 2001 From: Dongdong Tian Date: Wed, 22 May 2024 19:42:17 +0800 Subject: [PATCH] cmake: Simplify the handling of OpenMP library in CMake and set OpenMP_ROOT for macOS CI (#8494) --- ci/config-gmt-unix.sh | 5 +++++ ci/simple-gmt-tests.bat | 7 +++++-- ci/simple-gmt-tests.sh | 7 +++++-- cmake/ConfigUserAdvancedTemplate.cmake | 4 ++++ src/CMakeLists.txt | 7 +++---- 5 files changed, 22 insertions(+), 8 deletions(-) diff --git a/ci/config-gmt-unix.sh b/ci/config-gmt-unix.sh index bd928692ac2..b7bdf83f261 100755 --- a/ci/config-gmt-unix.sh +++ b/ci/config-gmt-unix.sh @@ -21,6 +21,11 @@ set (CMAKE_C_FLAGS "-Wall -Wdeclaration-after-statement ${CMAKE_C_FLAGS}") set (CMAKE_C_FLAGS "-Wextra ${CMAKE_C_FLAGS}") EOF +# Set OpenMP_ROOT so that CMake can find the libomp header and library on macOS +if [[ "$RUNNER_OS" == "macOS" ]]; then + echo "set (OpenMP_ROOT $(brew --prefix)/opt/libomp/)" >> cmake/ConfigUser.cmake +fi + if [[ "$RUN_TESTS" == "true" ]]; then cat >> cmake/ConfigUser.cmake << 'EOF' set (CMAKE_BUILD_TYPE Debug) diff --git a/ci/simple-gmt-tests.bat b/ci/simple-gmt-tests.bat index c3b446cd506..84584360855 100644 --- a/ci/simple-gmt-tests.bat +++ b/ci/simple-gmt-tests.bat @@ -2,8 +2,8 @@ REM REM Run some simple GMT commands REM -REM Check GMT version -gmt --version +REM Check GMT splash screen +gmt REM Check GMT configuration bash %INSTALLDIR%/bin/gmt-config --all @@ -22,3 +22,6 @@ gmt grdimage @earth_relief_01d -JH10c -Baf -pdf map REM Check supplemental modules gmt earthtide -T2018-06-18T12:00:00 -Gsolid_tide_up.grd + +REM Check OpenMP support +gmt grdsample @earth_relief_01d -R0/20/0/20 -I30m -Gtopo_30m.nc -x2 diff --git a/ci/simple-gmt-tests.sh b/ci/simple-gmt-tests.sh index f99cf336fd7..0a6d49135f2 100755 --- a/ci/simple-gmt-tests.sh +++ b/ci/simple-gmt-tests.sh @@ -5,8 +5,8 @@ set -x -e -# Check GMT version -gmt --version +# Check GMT splash screen +gmt # Check GMT configuration gmt-config --all @@ -28,4 +28,7 @@ gmt grdimage @earth_relief_01d -JH10c -Baf -pdf map # Check supplemental modules gmt earthtide -T2018-06-18T12:00:00 -Gsolid_tide_up.grd +# Check OpenMP support +gmt grdsample @earth_relief_01d -R0/20/0/20 -I30m -Gtopo_30m.nc -x2 + set +x +e diff --git a/cmake/ConfigUserAdvancedTemplate.cmake b/cmake/ConfigUserAdvancedTemplate.cmake index 2ab0458dcfd..c909a7dca9c 100644 --- a/cmake/ConfigUserAdvancedTemplate.cmake +++ b/cmake/ConfigUserAdvancedTemplate.cmake @@ -127,6 +127,10 @@ # Set location of CURL (can be root directory or path to header file) [auto]: #set (CURL_ROOT "curl_install_prefix") +# Set location of OpenMP (can be root directory or path to header file) [auto]: +# CMake sometimes may fail to find the OpenMP library (libomp) on macOS. +#set (OpenMP_ROOT $(brew --prefix)/opt/libomp/) + # Set location of GLIB component gthread [auto]. This is an optional (and # experimental) option which you need to enable or disable: set (GMT_USE_THREADS TRUE) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 3844d31f83a..84ccde70f62 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -224,7 +224,7 @@ endif (LAPACK_FOUND) # Need to know if macOS and kernel is >= 22.4.0 for newLapack syntax if (CMAKE_HOST_SYSTEM_NAME STREQUAL "Darwin") - execute_process (COMMAND bash "-c" "echo ${CMAKE_HOST_SYSTEM_VERSION} | awk -F. '{if ($1 > 22 || ($1 == 22 && $2 > 3)) {print 1} else {print 0}}'" + execute_process (COMMAND bash "-c" "echo ${CMAKE_HOST_SYSTEM_VERSION} | awk -F. '{if ($1 > 22 || ($1 == 22 && $2 > 3)) {print 1} else {print 0}}'" OUTPUT_STRIP_TRAILING_WHITESPACE OUTPUT_VARIABLE ACCELERATE_NEW_LAPACK) else (CMAKE_HOST_SYSTEM_NAME STREQUAL "Darwin") set (ACCELERATE_NEW_LAPACK 0) @@ -272,15 +272,14 @@ if (APPLE) list (APPEND GMT_OPTIONAL_LIBRARIES ${ACCELERATE_FRAMEWORK}) endif (APPLE) -find_package (OpenMP) +find_package (OpenMP COMPONENTS C) if (GMT_OPENMP) set (GMT_ENABLE_OPENMP TRUE) message (WARNING "CMake variable GMT_OPENMP is deprecated and will be removed in the future releases. Use GMT_ENABLE_OPENMP instead.") endif (GMT_OPENMP) if (OPENMP_FOUND AND GMT_ENABLE_OPENMP) + list (APPEND GMT_OPTIONAL_LIBRARIES OpenMP::OpenMP_C) set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${OpenMP_C_FLAGS}") - set (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${OpenMP_EXE_LINKER_FLAGS}") - set (CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} ${OpenMP_SHARED_LINKER_FLAGS}") set (HAVE_OPENMP TRUE CACHE INTERNAL "OpenMP enabled." FORCE) set (GMT_CONFIG_OPENMP_MESSAGE "enabled" CACHE INTERNAL "OPENMP config message") elseif (OPENMP_FOUND AND NOT GMT_ENABLE_OPENMP)