Improved how cmake sets up clang and cuda. This is basically to work around

partial C++11 support and funny cmake behavior of clang on OS X.
This commit is contained in:
Davis King 2016-12-14 15:09:53 -05:00
parent 5ba92d9f25
commit b85cb68e79
2 changed files with 30 additions and 12 deletions

View File

@ -506,11 +506,20 @@ if (NOT TARGET dlib)
if (CUDA_FOUND AND COMPILER_CAN_DO_CPP_11) if (CUDA_FOUND AND COMPILER_CAN_DO_CPP_11)
# There is some bug in cmake that causes it to mess up the
# -std=c++11 option if you let it propagate it to nvcc in some
# cases. So instead we disable this and manually include
# things from CMAKE_CXX_FLAGS in the CUDA_NVCC_FLAGS list below.
set(CUDA_PROPAGATE_HOST_FLAGS OFF)
# Grab all the -D flags from CMAKE_CXX_FLAGS so we can pass them
# to nvcc.
string(REGEX MATCHALL "-D[^ ]*" FLAGS_FOR_NVCC ${CMAKE_CXX_FLAGS})
set(CUDA_HOST_COMPILATION_CPP ON) set(CUDA_HOST_COMPILATION_CPP ON)
# Note that we add __STRICT_ANSI__ to avoid freaking out nvcc with gcc specific # Note that we add __STRICT_ANSI__ to avoid freaking out nvcc with gcc specific
# magic in the standard C++ header files (since nvcc uses gcc headers on # magic in the standard C++ header files (since nvcc uses gcc headers on
# linux). # linux).
list(APPEND CUDA_NVCC_FLAGS "-arch=sm_30;-std=c++11;-D__STRICT_ANSI__;-D_MWAITXINTRIN_H_INCLUDED;-D_FORCE_INLINES") list(APPEND CUDA_NVCC_FLAGS "-arch=sm_30;-std=c++11;-D__STRICT_ANSI__;-D_MWAITXINTRIN_H_INCLUDED;-D_FORCE_INLINES;${FLAGS_FOR_NVCC}")
include(cmake_utils/test_for_cudnn/find_cudnn.txt) include(cmake_utils/test_for_cudnn/find_cudnn.txt)

View File

@ -22,6 +22,19 @@ if(MSVC AND MSVC_VERSION VERSION_LESS 1900)
message(FATAL_ERROR "C++11 is required to use dlib, but the version of Visual Studio you are using is too old and doesn't support C++11. You need Visual Studio 2015 or newer. ") message(FATAL_ERROR "C++11 is required to use dlib, but the version of Visual Studio you are using is too old and doesn't support C++11. You need Visual Studio 2015 or newer. ")
endif() endif()
macro(test_compiler_for_cpp11)
message(STATUS "Building a C++11 test project to see if your compiler supports C++11")
try_compile(test_for_cpp11_worked ${PROJECT_BINARY_DIR}/cpp11_test_build
${CMAKE_CURRENT_LIST_DIR}/test_for_cpp11 cpp11_test)
if (test_for_cpp11_worked)
message(STATUS "C++11 activated.")
set(COMPILER_CAN_DO_CPP_11 1)
else()
set(COMPILER_CAN_DO_CPP_11 0)
message(STATUS "********** Your compiler failed to build a C++11 project. C++11 is required to use all parts of dlib! **********")
endif()
endmacro()
# Now turn on the appropriate compiler switch to enable C++11 if you have a # Now turn on the appropriate compiler switch to enable C++11 if you have a
# C++11 compiler. In CMake 3.1 there is a simple flag you can set, but earlier # C++11 compiler. In CMake 3.1 there is a simple flag you can set, but earlier
# verions of CMake are not so convenient. # verions of CMake are not so convenient.
@ -43,15 +56,7 @@ if (CMAKE_VERSION VERSION_LESS "3.1.2")
endif() endif()
else() else()
# Since we don't know what compiler this is just try to build a c++11 project and see if it compiles. # Since we don't know what compiler this is just try to build a c++11 project and see if it compiles.
message(STATUS "Building a C++11 test project to see if your compiler supports C++11") test_compiler_for_cpp11()
try_compile(test_for_cpp11_worked ${PROJECT_BINARY_DIR}/cpp11_test_build
${CMAKE_CURRENT_LIST_DIR}/test_for_cpp11 cpp11_test)
if (test_for_cpp11_worked)
message(STATUS "C++11 activated.")
set(COMPILER_CAN_DO_CPP_11 1)
else()
message(FATAL_ERROR "*** Your compiler failed to build a C++11 project, so dlib won't use C++11 features.***")
endif()
endif() endif()
elseif(MSVC AND CMAKE_CXX_COMPILER_VERSION VERSION_LESS 19.0.24215.1 ) elseif(MSVC AND CMAKE_CXX_COMPILER_VERSION VERSION_LESS 19.0.24215.1 )
message(STATUS "NOTE: Visual Studio didn't have good enough C++11 support until Visual Studio 2015 update 3 (v19.0.24215.1)") message(STATUS "NOTE: Visual Studio didn't have good enough C++11 support until Visual Studio 2015 update 3 (v19.0.24215.1)")
@ -79,10 +84,14 @@ else()
if (NOT CMAKE_CXX_STANDARD OR CMAKE_CXX_STANDARD LESS 11) if (NOT CMAKE_CXX_STANDARD OR CMAKE_CXX_STANDARD LESS 11)
set(CMAKE_CXX_STANDARD 11) set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED YES) set(CMAKE_CXX_STANDARD_REQUIRED YES)
if (APPLE AND "${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang") if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
# Sometimes clang will lie and report that it supports C++11 when
# really it doesn't support thread_local. So check for that.
test_compiler_for_cpp11()
add_global_compiler_switch("-std=c++11") add_global_compiler_switch("-std=c++11")
else()
message(STATUS "C++11 activated.")
endif() endif()
message(STATUS "C++11 activated.")
endif() endif()
endif() endif()
endif() endif()