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 # (To distribute this file outside of CMake, substitute the full
# License text for the above reference.) # License text for the above reference.)
include(CheckCSourceCompiles) get_property(_ENABLED_LANGUAGES GLOBAL PROPERTY ENABLED_LANGUAGES)
include(CheckCXXSourceCompiles) list(FIND _ENABLED_LANGUAGES "C" _HAVE_LANGUAGE_C)
include(${CMAKE_CURRENT_LIST_DIR}/FindPackageHandleStandardArgs.cmake) list(FIND _ENABLED_LANGUAGES "CXX" _HAVE_LANGUAGE_CXX)
unset(_ENABLED_LANGUAGES)
set(_OPENMP_REQUIRED_VARS)
set(OpenMP_C_FLAG_CANDIDATES set(OpenMP_C_FLAG_CANDIDATES
#Gnu #Gnu
@ -62,8 +65,6 @@ int main() {
#endif #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, # if these are set then do not try to find them again,
# by avoiding any try_compiles for the flags # by avoiding any try_compiles for the flags
if(DEFINED OpenMP_C_FLAGS AND DEFINED OpenMP_CXX_FLAGS) if(DEFINED OpenMP_C_FLAGS AND DEFINED OpenMP_CXX_FLAGS)
@ -72,43 +73,68 @@ if(DEFINED OpenMP_C_FLAGS AND DEFINED OpenMP_CXX_FLAGS)
endif(DEFINED OpenMP_C_FLAGS AND DEFINED OpenMP_CXX_FLAGS) endif(DEFINED OpenMP_C_FLAGS AND DEFINED OpenMP_CXX_FLAGS)
# check c compiler # check c compiler
foreach(FLAG ${OpenMP_C_FLAG_CANDIDATES}) if(NOT _HAVE_LANGUAGE_C EQUAL -1)
set(SAFE_CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS}") include(CheckCSourceCompiles)
set(CMAKE_REQUIRED_FLAGS "${FLAG}")
unset(OpenMP_FLAG_DETECTED CACHE) foreach(FLAG ${OpenMP_C_FLAG_CANDIDATES})
message(STATUS "Try OpenMP C flag = [${FLAG}]") set(SAFE_CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS}")
check_c_source_compiles("${OpenMP_CXX_TEST_SOURCE}" OpenMP_FLAG_DETECTED) set(CMAKE_REQUIRED_FLAGS "${FLAG}")
set(CMAKE_REQUIRED_FLAGS "${SAFE_CMAKE_REQUIRED_FLAGS}") unset(OpenMP_FLAG_DETECTED CACHE)
if(OpenMP_FLAG_DETECTED) message(STATUS "Try OpenMP C flag = [${FLAG}]")
set(OpenMP_C_FLAGS_INTERNAL "${FLAG}") check_c_source_compiles("${OpenMP_C_TEST_SOURCE}" OpenMP_FLAG_DETECTED)
break() set(CMAKE_REQUIRED_FLAGS "${SAFE_CMAKE_REQUIRED_FLAGS}")
endif(OpenMP_FLAG_DETECTED) if(OpenMP_FLAG_DETECTED)
endforeach(FLAG ${OpenMP_C_FLAG_CANDIDATES}) set(OpenMP_C_FLAGS_INTERNAL "${FLAG}")
break()
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 # check cxx compiler
foreach(FLAG ${OpenMP_CXX_FLAG_CANDIDATES}) if(NOT _HAVE_LANGUAGE_CXX EQUAL -1)
set(SAFE_CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS}") include(CheckCXXSourceCompiles)
set(CMAKE_REQUIRED_FLAGS "${FLAG}") # use the same source for CXX as C for now
unset(OpenMP_FLAG_DETECTED CACHE) set(OpenMP_CXX_TEST_SOURCE ${OpenMP_C_TEST_SOURCE})
message(STATUS "Try OpenMP CXX flag = [${FLAG}]")
check_cxx_source_compiles("${OpenMP_C_TEST_SOURCE}" OpenMP_FLAG_DETECTED)
set(CMAKE_REQUIRED_FLAGS "${SAFE_CMAKE_REQUIRED_FLAGS}")
if(OpenMP_FLAG_DETECTED)
set(OpenMP_CXX_FLAGS_INTERNAL "${FLAG}")
break()
endif(OpenMP_FLAG_DETECTED)
endforeach(FLAG ${OpenMP_CXX_FLAG_CANDIDATES})
set(OpenMP_C_FLAGS "${OpenMP_C_FLAGS_INTERNAL}" foreach(FLAG ${OpenMP_CXX_FLAG_CANDIDATES})
CACHE STRING "C compiler flags for OpenMP parallization") 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_CXX_TEST_SOURCE}" OpenMP_FLAG_DETECTED)
set(CMAKE_REQUIRED_FLAGS "${SAFE_CMAKE_REQUIRED_FLAGS}")
if(OpenMP_FLAG_DETECTED)
set(OpenMP_CXX_FLAGS_INTERNAL "${FLAG}")
break()
endif(OpenMP_FLAG_DETECTED)
endforeach(FLAG ${OpenMP_CXX_FLAG_CANDIDATES})
set(OpenMP_CXX_FLAGS "${OpenMP_CXX_FLAGS_INTERNAL}" set(OpenMP_CXX_FLAGS "${OpenMP_CXX_FLAGS_INTERNAL}"
CACHE STRING "C++ compiler flags for OpenMP parallization") 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( list(APPEND _OPENMP_REQUIRED_VARS OpenMP_CXX_FLAGS)
OpenMP_C_FLAGS unset(OpenMP_CXX_FLAG_CANDIDATES)
OpenMP_CXX_FLAGS 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()