FindOpenMP: do not fail if only C or CXX is enabled (#11910)

Inspired-By: Raymond Wan
This commit is contained in:
Rolf Eike Beer 2012-02-08 18:46:57 +01:00
parent 59ecc2c8ac
commit 81228e9d1d
1 changed files with 66 additions and 40 deletions

View File

@ -24,9 +24,12 @@
# (To distribute this file outside of CMake, substitute the full
# License text for the above reference.)
include(CheckCSourceCompiles)
include(CheckCXXSourceCompiles)
include(${CMAKE_CURRENT_LIST_DIR}/FindPackageHandleStandardArgs.cmake)
get_property(_ENABLED_LANGUAGES GLOBAL PROPERTY ENABLED_LANGUAGES)
list(FIND _ENABLED_LANGUAGES "C" _HAVE_LANGUAGE_C)
list(FIND _ENABLED_LANGUAGES "CXX" _HAVE_LANGUAGE_CXX)
unset(_ENABLED_LANGUAGES)
set(_OPENMP_REQUIRED_VARS)
set(OpenMP_C_FLAG_CANDIDATES
#Gnu
@ -62,8 +65,6 @@ int main() {
#endif
}
")
# use the same source for CXX as C for now
set(OpenMP_CXX_TEST_SOURCE ${OpenMP_C_TEST_SOURCE})
# if these are set then do not try to find them again,
# by avoiding any try_compiles for the flags
if(DEFINED OpenMP_C_FLAGS AND DEFINED OpenMP_CXX_FLAGS)
@ -72,12 +73,15 @@ if(DEFINED OpenMP_C_FLAGS AND DEFINED OpenMP_CXX_FLAGS)
endif(DEFINED OpenMP_C_FLAGS AND DEFINED OpenMP_CXX_FLAGS)
# check c compiler
if(NOT _HAVE_LANGUAGE_C EQUAL -1)
include(CheckCSourceCompiles)
foreach(FLAG ${OpenMP_C_FLAG_CANDIDATES})
set(SAFE_CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS}")
set(CMAKE_REQUIRED_FLAGS "${FLAG}")
unset(OpenMP_FLAG_DETECTED CACHE)
message(STATUS "Try OpenMP C flag = [${FLAG}]")
check_c_source_compiles("${OpenMP_CXX_TEST_SOURCE}" OpenMP_FLAG_DETECTED)
check_c_source_compiles("${OpenMP_C_TEST_SOURCE}" OpenMP_FLAG_DETECTED)
set(CMAKE_REQUIRED_FLAGS "${SAFE_CMAKE_REQUIRED_FLAGS}")
if(OpenMP_FLAG_DETECTED)
set(OpenMP_C_FLAGS_INTERNAL "${FLAG}")
@ -85,13 +89,25 @@ foreach(FLAG ${OpenMP_C_FLAG_CANDIDATES})
endif(OpenMP_FLAG_DETECTED)
endforeach(FLAG ${OpenMP_C_FLAG_CANDIDATES})
set(OpenMP_C_FLAGS "${OpenMP_C_FLAGS_INTERNAL}"
CACHE STRING "C compiler flags for OpenMP parallization")
list(APPEND _OPENMP_REQUIRED_VARS OpenMP_C_FLAGS)
unset(OpenMP_C_FLAG_CANDIDATES)
endif()
# check cxx compiler
if(NOT _HAVE_LANGUAGE_CXX EQUAL -1)
include(CheckCXXSourceCompiles)
# use the same source for CXX as C for now
set(OpenMP_CXX_TEST_SOURCE ${OpenMP_C_TEST_SOURCE})
foreach(FLAG ${OpenMP_CXX_FLAG_CANDIDATES})
set(SAFE_CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS}")
set(CMAKE_REQUIRED_FLAGS "${FLAG}")
unset(OpenMP_FLAG_DETECTED CACHE)
message(STATUS "Try OpenMP CXX flag = [${FLAG}]")
check_cxx_source_compiles("${OpenMP_C_TEST_SOURCE}" OpenMP_FLAG_DETECTED)
check_cxx_source_compiles("${OpenMP_CXX_TEST_SOURCE}" OpenMP_FLAG_DETECTED)
set(CMAKE_REQUIRED_FLAGS "${SAFE_CMAKE_REQUIRED_FLAGS}")
if(OpenMP_FLAG_DETECTED)
set(OpenMP_CXX_FLAGS_INTERNAL "${FLAG}")
@ -99,16 +115,26 @@ foreach(FLAG ${OpenMP_CXX_FLAG_CANDIDATES})
endif(OpenMP_FLAG_DETECTED)
endforeach(FLAG ${OpenMP_CXX_FLAG_CANDIDATES})
set(OpenMP_C_FLAGS "${OpenMP_C_FLAGS_INTERNAL}"
CACHE STRING "C compiler flags for OpenMP parallization")
set(OpenMP_CXX_FLAGS "${OpenMP_CXX_FLAGS_INTERNAL}"
CACHE STRING "C++ compiler flags for OpenMP parallization")
# handle the standard arguments for find_package
find_package_handle_standard_args(OpenMP DEFAULT_MSG
OpenMP_C_FLAGS OpenMP_CXX_FLAGS )
mark_as_advanced(
OpenMP_C_FLAGS
OpenMP_CXX_FLAGS
)
list(APPEND _OPENMP_REQUIRED_VARS OpenMP_CXX_FLAGS)
unset(OpenMP_CXX_FLAG_CANDIDATES)
unset(OpenMP_CXX_TEST_SOURCE)
endif()
unset(_HAVE_LANGUAGE_C)
unset(_HAVE_LANGUAGE_CXX)
if(_OPENMP_REQUIRED_VARS)
include(${CMAKE_CURRENT_LIST_DIR}/FindPackageHandleStandardArgs.cmake)
find_package_handle_standard_args(OpenMP
REQUIRED_VARS ${_OPENMP_REQUIRED_VARS})
mark_as_advanced(${_OPENMP_REQUIRED_VARS})
unset(_OPENMP_REQUIRED_VARS)
else()
message(SEND_ERROR "FindOpenMP requires C or CXX language to be enabled")
endif()