Use CMake targets provided by FindOpenMP instead of manually setting flags if possible

This commit is contained in:
Sebastian Messmer 2018-09-27 23:31:26 -07:00
parent 06b2b1d5f1
commit 4911fcd4f4

View File

@ -9,37 +9,46 @@ target_compile_definitions(cryptopp PUBLIC $<$<CONFIG:Debug>:CRYPTOPP_DEBUG>) #
add_compile_options($<$<CONFIG:Debug>:-DCRYPTOPP_DEBUG>) # add to stuff built in subdirectories (like the actual library)
if(NOT DISABLE_OPENMP)
if((APPLE AND ((CMAKE_CXX_COMPILER_ID STREQUAL "AppleClang") OR (CMAKE_CXX_COMPILER_ID STREQUAL "Clang")))
AND ((CMAKE_CXX_COMPILER_VERSION VERSION_GREATER_EQUAL "7.0") AND (CMAKE_VERSION VERSION_LESS "3.12.0")))
# Workaround because older cmake on apple doesn't support FindOpenMP
message(STATUS "Applying workaround for OSX OpenMP")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Xclang -fopenmp")
target_compile_options(cryptopp PUBLIC -Xclang -fopenmp) # also add these flags to all libraries depending on this
target_link_libraries(cryptopp PRIVATE -lomp)
elseif(MSVC)
if (MSVC)
message(WARNING "MSVC does not support the OpenMP 4.0 standard used by Crypto++. Disabling OpenMP. This can cause degraded performance.")
else()
find_package(OpenMP)
if (NOT OpenMP_CXX_LIBRARIES)
# Older CMake didn't have OpenMP_CXX_LIBRARIES defined yet, but passed the library in OpenMP_CXX_FLAGS
message(STATUS "We are on an older CMake that does not set OpenMP_CXX_LIBRARIES. Setting it from OpenMP_CXX_FLAGS.")
set(OpenMP_CXX_LIBRARIES ${OpenMP_CXX_FLAGS})
if(NOT OPENMP_FOUND)
if((APPLE AND ((CMAKE_CXX_COMPILER_ID STREQUAL "AppleClang") OR (CMAKE_CXX_COMPILER_ID STREQUAL "Clang")))
AND ((CMAKE_CXX_COMPILER_VERSION VERSION_GREATER_EQUAL "7.0") AND (CMAKE_VERSION VERSION_LESS "3.12.0")))
# Workaround because older cmake on apple doesn't support FindOpenMP
message(STATUS "Applying workaround for OSX OpenMP with old cmake that doesn't have FindOpenMP")
set(OpenMP_CXX_FLAGS "-Xclang -fopenmp")
set(Additional_OpenMP_Libraries_Workaround "-lomp")
else()
message(FATAL_ERROR "Did not find OpenMP. Build with -DDISABLE_OPENMP=ON if you want to allow this and are willing to take the performance hit.")
endif()
else()
set(Additional_OpenMP_Libraries_Workaround "")
endif()
if(OPENMP_FOUND)
message(STATUS "Using OpenMP flags: ${OpenMP_CXX_FLAGS}")
message(STATUS "Using OpenMP library: ${OpenMP_CXX_LIBRARIES}")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OpenMP_CXX_FLAGS}")
target_link_libraries(cryptopp PRIVATE ${OpenMP_CXX_LIBRARIES})
# also add these flags to all libraries depending on this
string(REPLACE " " ";" REPLACED_FLAGS ${OpenMP_CXX_FLAGS})
target_compile_options(cryptopp PUBLIC ${REPLACED_FLAGS})
else(OPENMP_FOUND)
message(FATAL_ERROR "Did not find OpenMP. Build with -DDISABLE_OPENMP=ON if you want to allow this and are willing to take the performance hit.")
endif(OPENMP_FOUND)
if(NOT TARGET OpenMP::OpenMP_CXX)
# We're on cmake < 3.9, handle behavior of the old FindOpenMP implementation
message(STATUS "Applying workaround for old CMake that doesn't define FindOpenMP using targets")
add_library(OpenMP_TARGET INTERFACE)
add_library(OpenMP::OpenMP_CXX ALIAS OpenMP_TARGET)
target_compile_options(OpenMP_TARGET INTERFACE ${OpenMP_CXX_FLAGS}) # add to all targets depending on this
find_package(Threads REQUIRED)
target_link_libraries(OpenMP_TARGET INTERFACE Threads::Threads)
target_link_libraries(OpenMP_TARGET INTERFACE ${OpenMP_CXX_FLAGS} ${Additional_OpenMP_Libraries_Workaround})
endif()
target_link_libraries(cryptopp PUBLIC OpenMP::OpenMP_CXX)
# also add these flags to the third party Crypto++ build setup that is built in a subdirectory
message(STATUS "OpenMP flags: ${OpenMP_CXX_FLAGS}")
string(REPLACE " " ";" REPLACED_FLAGS ${OpenMP_CXX_FLAGS})
add_compile_options(${REPLACED_FLAGS})
endif()
else(NOT DISABLE_OPENMP)
else()
message(WARNING "OpenMP is disabled. This can cause degraded performance.")
endif(NOT DISABLE_OPENMP)
endif()
set(BUILD_TESTING OFF)