Some cmake script cleanup and refactoring.

This commit is contained in:
Davis King 2017-10-16 20:55:28 -04:00
parent c0b60faaf0
commit 3b2d73db68
9 changed files with 180 additions and 150 deletions

View File

@ -1,2 +1,10 @@
cmake_minimum_required(VERSION 2.8.12)
# Set this so that the dlib subfolder will build both a static and shared
# library, since the only reason to build this CMakeLists.txt is if you want
# to install dlib. It should be noted however that installing dlib is not
# necessary. A simpler approach is to use it the way shown in
# examples/CMakeLists.txt
set(DLIB_IN_PROJECT_BUILD false)
add_subdirectory(dlib)

View File

@ -7,6 +7,10 @@
cmake_minimum_required(VERSION 2.8.12)
project(dlib)
include(cmake_utils/set_compiler_specific_options.cmake)
# Adhere to GNU filesystem layout conventions
include(GNUInstallDirs)
@ -23,6 +27,9 @@ set(VERSION ${CPACK_PACKAGE_VERSION_MAJOR}.${CPACK_PACKAGE_VERSION_MINOR}.${CPAC
get_directory_property(has_parent PARENT_DIRECTORY)
if(has_parent)
set(DLIB_VERSION ${VERSION} PARENT_SCOPE)
if (NOT DEFINED DLIB_IN_PROJECT_BUILD)
set(DLIB_IN_PROJECT_BUILD true)
endif()
endif()
# Set only because there are old target_link_libraries() statements in the
@ -517,7 +524,7 @@ if (NOT TARGET dlib)
set(CUDA_FOUND 0)
endif()
if (CUDA_FOUND AND COMPILER_CAN_DO_CPP_11)
if (CUDA_FOUND AND (NOT USING_OLD_VISUAL_STUDIO_COMPILER))
# 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
@ -527,7 +534,7 @@ if (NOT TARGET dlib)
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})
string(REGEX MATCHALL "-D[^ ]*" FLAGS_FOR_NVCC "${CMAKE_CXX_FLAGS}")
endif()
@ -603,7 +610,7 @@ if (NOT TARGET dlib)
endif()
endif()
if (CUDA_FOUND AND cudnn AND COMPILER_CAN_DO_CPP_11 AND cuda_test_compile_worked AND cudnn_test_compile_worked AND cudnn_include)
if (CUDA_FOUND AND cudnn AND (NOT USING_OLD_VISUAL_STUDIO_COMPILER) AND cuda_test_compile_worked AND cudnn_test_compile_worked AND cudnn_include)
set(source_files ${source_files}
dnn/cuda_dlib.cu
dnn/cudnn_dlibapi.cpp
@ -624,7 +631,7 @@ if (NOT TARGET dlib)
else()
set(DLIB_USE_CUDA OFF CACHE STRING ${DLIB_USE_BLAS_STR} FORCE )
toggle_preprocessor_switch(DLIB_USE_CUDA)
if (COMPILER_CAN_DO_CPP_11)
if (NOT USING_OLD_VISUAL_STUDIO_COMPILER)
if (cuda_test_compile_worked)
if (NOT cudnn OR NOT cudnn_include OR NOT cudnn_test_compile_worked)
message(STATUS "*** cuDNN V5.0 OR GREATER NOT FOUND. DLIB WILL NOT USE CUDA. ***")
@ -709,21 +716,30 @@ if (NOT TARGET dlib)
INTERFACE $<INSTALL_INTERFACE:include>
PUBLIC ${dlib_needed_includes}
)
target_link_libraries(dlib_shared PRIVATE ${dlib_needed_libraries})
target_link_libraries(dlib_shared PUBLIC ${dlib_needed_libraries})
target_compile_options(dlib_shared PRIVATE ${active_preprocessor_switches})
endif()
# Allow the unit tests to ask us to compile the all/source.cpp file just to make sure it compiles.
if (DLIB_TEST_COMPILE_ALL_SOURCE_CPP)
ADD_LIBRARY(dlib_all_source_cpp STATIC all/source.cpp)
add_library(dlib_all_source_cpp STATIC all/source.cpp)
target_link_libraries(dlib_all_source_cpp dlib)
target_compile_options(dlib_all_source_cpp PUBLIC ${active_preprocessor_switches})
enable_cpp11_for_target(dlib_all_source_cpp)
endif()
if (TARGET dlib)
enable_cpp11_for_target(dlib)
target_compile_options(dlib PUBLIC ${active_compile_opts})
endif()
if (TARGET dlib_shared)
enable_cpp11_for_target(dlib_shared)
target_compile_options(dlib_shared PUBLIC ${active_compile_opts})
endif()
# Install the library
if (NOT DLIB_IN_PROJECT_BUILD)
cmake_minimum_required(VERSION 2.8.8)
if(UNIX)
set_target_properties(dlib_shared PROPERTIES
OUTPUT_NAME dlib

View File

@ -1,105 +1,5 @@
# This is a CMake file meant to be included via include()
# It will trigger a compilation of dlib *in the project*
# including it
cmake_minimum_required(VERSION 2.8.12)
set(DLIB_IN_PROJECT_BUILD true)
if (POLICY CMP0054)
cmake_policy(SET CMP0054 NEW)
endif()
# Determine the path to dlib.
set(dlib_path ${CMAKE_CURRENT_LIST_DIR})
include(${dlib_path}/cmake_utils/add_global_compiler_switch.cmake)
include(${dlib_path}/cmake_utils/use_cpp_11.cmake)
if (CMAKE_COMPILER_IS_GNUCXX)
# By default, g++ won't warn or error if you forget to return a value in a
# function which requires you to do so. This option makes it give a warning
# for doing this.
add_global_compiler_switch(-Wreturn-type)
endif()
if ("Clang" MATCHES ${CMAKE_CXX_COMPILER_ID})
# Increase clang's default tempalte recurision depth so the dnn examples don't error out.
add_definitions(-ftemplate-depth=500)
endif()
set(gcc_like_compilers GNU Clang Intel)
set(intel_archs x86_64 i386 i686 AMD64 x86)
# Setup some options to allow a user to enable SSE and AVX instruction use.
if ((";${gcc_like_compilers};" MATCHES ";${CMAKE_CXX_COMPILER_ID};") AND
(";${intel_archs};" MATCHES ";${CMAKE_SYSTEM_PROCESSOR};") AND NOT USE_AUTO_VECTOR)
option(USE_SSE2_INSTRUCTIONS "Compile your program with SSE2 instructions" OFF)
option(USE_SSE4_INSTRUCTIONS "Compile your program with SSE4 instructions" OFF)
option(USE_AVX_INSTRUCTIONS "Compile your program with AVX instructions" OFF)
if(USE_AVX_INSTRUCTIONS)
add_definitions(-mavx)
message(STATUS "Enabling AVX instructions")
elseif (USE_SSE4_INSTRUCTIONS)
add_definitions(-msse4)
message(STATUS "Enabling SSE4 instructions")
elseif(USE_SSE2_INSTRUCTIONS)
add_definitions(-msse2)
message(STATUS "Enabling SSE2 instructions")
endif()
elseif (MSVC OR "${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC") # else if using Visual Studio
# Use SSE2 by default when using Visual Studio.
option(USE_SSE2_INSTRUCTIONS "Compile your program with SSE2 instructions" ON)
# Visual Studio 2005 didn't support SSE4
if (NOT MSVC80)
option(USE_SSE4_INSTRUCTIONS "Compile your program with SSE4 instructions" OFF)
endif()
# Visual Studio 2005 and 2008 didn't support AVX
if (NOT MSVC80 AND NOT MSVC90)
option(USE_AVX_INSTRUCTIONS "Compile your program with AVX instructions" OFF)
endif()
include(CheckTypeSize)
check_type_size( "void*" SIZE_OF_VOID_PTR)
if(USE_AVX_INSTRUCTIONS)
add_definitions(/arch:AVX)
message(STATUS "Enabling AVX instructions")
elseif (USE_SSE4_INSTRUCTIONS)
# Visual studio doesn't have an /arch:SSE2 flag when building in 64 bit modes.
# So only give it when we are doing a 32 bit build.
if (SIZE_OF_VOID_PTR EQUAL 4)
add_definitions(/arch:SSE2)
endif()
message(STATUS "Enabling SSE4 instructions")
add_definitions(-DDLIB_HAVE_SSE2)
add_definitions(-DDLIB_HAVE_SSE3)
add_definitions(-DDLIB_HAVE_SSE41)
elseif(USE_SSE2_INSTRUCTIONS)
# Visual studio doesn't have an /arch:SSE2 flag when building in 64 bit modes.
# So only give it when we are doing a 32 bit build.
if (SIZE_OF_VOID_PTR EQUAL 4)
add_definitions(/arch:SSE2)
endif()
message(STATUS "Enabling SSE2 instructions")
add_definitions(-DDLIB_HAVE_SSE2)
endif()
# By default Visual Studio does not support .obj files with more than 65k sections
# Code generated by file_to_code_ex and code using DNN module can have them
# this flag enables > 65k sections, but produces .obj files that will not be readable by
# VS 2005
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /bigobj")
endif()
# This is really optional, but nice. It will make sure the build mode
# created by cmake is always release by default.
include(${dlib_path}/cmake_utils/release_build_by_default)
# Don't add dlib if it's already been added to the cmake project
if (NOT TARGET dlib)
add_subdirectory(${dlib_path} dlib_build)
endif()
add_subdirectory(${CMAKE_CURRENT_LIST_DIR} dlib_build)

View File

@ -2,6 +2,8 @@
cmake_minimum_required(VERSION 2.8.12)
message(WARNING "add_global_compiler_switch() is deprecated. Use target_compile_options() instead")
# Make macros that can add compiler switches to the entire project. Not just
# to the current cmake folder being built.
macro ( add_global_compiler_switch switch_name )

View File

@ -33,6 +33,9 @@ set(dlib_LIBRARIES ${dlib_LIBRARIES} "@dlib_needed_libraries@")
set(dlib_LIBS ${dlib_LIBRARIES} "@dlib_needed_libraries@")
set(dlib_INCLUDE_DIRS "@CMAKE_INSTALL_FULL_INCLUDEDIR@" "@dlib_needed_includes@")
mark_as_advanced(dlib_LIBRARIES)
mark_as_advanced(dlib_LIBS)
mark_as_advanced(dlib_INCLUDE_DIRS)
# Mark these variables above as deprecated.
function(__deprecated_var var access)
@ -46,4 +49,3 @@ variable_watch(dlib_INCLUDE_DIRS __deprecated_var)
include(@CMAKE_INSTALL_FULL_INCLUDEDIR@/dlib/cmake_utils/use_cpp_11.cmake)

View File

@ -0,0 +1,106 @@
cmake_minimum_required(VERSION 2.8.12)
set(USING_OLD_VISUAL_STUDIO_COMPILER 0)
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. ")
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 "So we aren't enabling things that require full C++11 support (e.g. the deep learning tools).")
message(STATUS "Also, be aware that Visual Studio's version naming is confusing, in particular, there are multiple versions of 'update 3'")
message(STATUS "So if you are getting this message you need to update to the newer version of Visual Studio to use full C++11.")
set(USING_OLD_VISUAL_STUDIO_COMPILER 1)
endif()
# push USING_OLD_VISUAL_STUDIO_COMPILER to the parent so we can use it in the
# examples CMakeLists.txt file.
get_directory_property(has_parent PARENT_DIRECTORY)
if(has_parent)
set(USING_OLD_VISUAL_STUDIO_COMPILER ${USING_OLD_VISUAL_STUDIO_COMPILER} PARENT_SCOPE)
endif()
set(gcc_like_compilers GNU Clang Intel)
set(intel_archs x86_64 i386 i686 AMD64 x86)
# Setup some options to allow a user to enable SSE and AVX instruction use.
if ((";${gcc_like_compilers};" MATCHES ";${CMAKE_CXX_COMPILER_ID};") AND
(";${intel_archs};" MATCHES ";${CMAKE_SYSTEM_PROCESSOR};") AND NOT USE_AUTO_VECTOR)
option(USE_SSE2_INSTRUCTIONS "Compile your program with SSE2 instructions" OFF)
option(USE_SSE4_INSTRUCTIONS "Compile your program with SSE4 instructions" OFF)
option(USE_AVX_INSTRUCTIONS "Compile your program with AVX instructions" OFF)
if(USE_AVX_INSTRUCTIONS)
list(APPEND active_compile_opts -mavx)
message(STATUS "Enabling AVX instructions")
elseif (USE_SSE4_INSTRUCTIONS)
list(APPEND active_compile_opts -msse4)
message(STATUS "Enabling SSE4 instructions")
elseif(USE_SSE2_INSTRUCTIONS)
list(APPEND active_compile_opts -msse2)
message(STATUS "Enabling SSE2 instructions")
endif()
elseif (MSVC OR "${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC") # else if using Visual Studio
# Use SSE2 by default when using Visual Studio.
option(USE_SSE2_INSTRUCTIONS "Compile your program with SSE2 instructions" ON)
option(USE_SSE4_INSTRUCTIONS "Compile your program with SSE4 instructions" OFF)
option(USE_AVX_INSTRUCTIONS "Compile your program with AVX instructions" OFF)
include(CheckTypeSize)
check_type_size( "void*" SIZE_OF_VOID_PTR)
if(USE_AVX_INSTRUCTIONS)
list(APPEND active_compile_opts /arch:AVX)
message(STATUS "Enabling AVX instructions")
elseif (USE_SSE4_INSTRUCTIONS)
# Visual studio doesn't have an /arch:SSE2 flag when building in 64 bit modes.
# So only give it when we are doing a 32 bit build.
if (SIZE_OF_VOID_PTR EQUAL 4)
list(APPEND active_compile_opts /arch:SSE2)
endif()
message(STATUS "Enabling SSE4 instructions")
list(APPEND active_preprocessor_switches "-DDLIB_HAVE_SSE2")
list(APPEND active_preprocessor_switches "-DDLIB_HAVE_SSE3")
list(APPEND active_preprocessor_switches "-DDLIB_HAVE_SSE41")
elseif(USE_SSE2_INSTRUCTIONS)
# Visual studio doesn't have an /arch:SSE2 flag when building in 64 bit modes.
# So only give it when we are doing a 32 bit build.
if (SIZE_OF_VOID_PTR EQUAL 4)
list(APPEND active_compile_opts /arch:SSE2)
endif()
message(STATUS "Enabling SSE2 instructions")
list(APPEND active_preprocessor_switches "-DDLIB_HAVE_SSE2")
endif()
endif()
if (CMAKE_COMPILER_IS_GNUCXX)
# By default, g++ won't warn or error if you forget to return a value in a
# function which requires you to do so. This option makes it give a warning
# for doing this.
list(APPEND active_compile_opts "-Wreturn-type")
endif()
if ("Clang" MATCHES ${CMAKE_CXX_COMPILER_ID})
# Increase clang's default tempalte recurision depth so the dnn examples don't error out.
list(APPEND active_compile_opts "-ftemplate-depth=500")
endif()
if (MSVC)
# By default Visual Studio does not support .obj files with more than 65k sections.
# However, code generated by file_to_code_ex and code using DNN module can have
# them. So this flag enables > 65k sections, but produces .obj files
# that will not be readable by VS 2005.
list(APPEND active_compile_opts "/bigobj")
if(CMAKE_CXX_COMPILER_ID MATCHES "Clang" AND CMAKE_CXX_COMPILER_VERSION VERSION_GREATER 3.3)
# Clang can compile all Dlib's code at Windows platform. Tested with Clang 5
list(APPEND active_compile_opts "-Xclang -fcxx-exceptions")
endif()
endif()

View File

@ -13,4 +13,5 @@ include(find_cudnn.txt)
if (cudnn_include AND cudnn)
include_directories(${cudnn_include})
cuda_add_library(cudnn_test STATIC ../../dnn/cudnn_dlibapi.cpp ${cudnn} )
enable_cpp11_for_target(cudnn_test)
endif()

View File

@ -1,31 +1,28 @@
# This script checks if your compiler has C++11 support and enables it if it does.
# Also, it sets the COMPILER_CAN_DO_CPP_11 variable to 1 if it was successful.
# This script creates a function, enable_cpp11_for_target(), which checks if your
# compiler has C++11 support and enables it if it does.
cmake_minimum_required(VERSION 2.8.12)
# Don't rerun this script if its already been executed.
if (DEFINED COMPILER_CAN_DO_CPP_11)
return()
endif()
if (POLICY CMP0054)
cmake_policy(SET CMP0054 NEW)
endif()
set(_where_is_cmake_utils_dir ${CMAKE_CURRENT_LIST_DIR})
function(enable_cpp11_for_target target_name)
# Set to false unless we find out otherwise in the code below.
set(COMPILER_CAN_DO_CPP_11 0)
include(${CMAKE_CURRENT_LIST_DIR}/add_global_compiler_switch.cmake)
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. ")
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)
${_where_is_cmake_utils_dir}/test_for_cpp11 cpp11_test)
if (test_for_cpp11_worked)
message(STATUS "C++11 activated.")
set(COMPILER_CAN_DO_CPP_11 1)
@ -43,7 +40,7 @@ if (CMAKE_VERSION VERSION_LESS "3.1.2")
execute_process(COMMAND ${CMAKE_CXX_COMPILER} -dumpversion OUTPUT_VARIABLE GCC_VERSION)
if (GCC_VERSION VERSION_GREATER 4.8 OR GCC_VERSION VERSION_EQUAL 4.8)
message(STATUS "C++11 activated.")
add_global_compiler_switch("-std=gnu++11")
target_compile_options(${target_name} PUBLIC "-std=gnu++11")
set(COMPILER_CAN_DO_CPP_11 1)
endif()
elseif ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
@ -51,24 +48,13 @@ if (CMAKE_VERSION VERSION_LESS "3.1.2")
string (REGEX REPLACE ".*clang version ([0-9]+\\.[0-9]+).*" "\\1" CLANG_VERSION ${clang_full_version_string})
if (CLANG_VERSION VERSION_GREATER 3.3)
message(STATUS "C++11 activated.")
add_global_compiler_switch("-std=c++11")
target_compile_options(${target_name} PUBLIC "-std=c++11")
set(COMPILER_CAN_DO_CPP_11 1)
endif()
else()
# Since we don't know what compiler this is just try to build a c++11 project and see if it compiles.
test_compiler_for_cpp11()
endif()
elseif( MSVC AND CMAKE_CXX_COMPILER_ID MATCHES "Clang" AND CMAKE_CXX_COMPILER_VERSION VERSION_GREATER 3.3)
# Clang can compile all Dlib's code at Windows platform. Tested with Clang 5
message(STATUS "C++11 activated.")
add_global_compiler_switch("-Xclang -fcxx-exceptions")
set(COMPILER_CAN_DO_CPP_11 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 "So we aren't enabling things that require full C++11 support (e.g. the deep learning tools).")
message(STATUS "Also, be aware that Visual Studio's version naming is confusing, in particular, there are multiple versions of 'update 3'")
message(STATUS "So if you are getting this message you need to update to the newer version of Visual Studio to use full C++11.")
set(USING_OLD_VISUAL_STUDIO_COMPILER 1)
else()
# Set a flag if the compiler you are using is capable of providing C++11 features.
@ -84,19 +70,26 @@ else()
";${cxx_features};" MATCHES ";cxx_auto_type;")
set(COMPILER_CAN_DO_CPP_11 1)
# Set which standard to use unless someone has already set it to something
# newer.
if (NOT CMAKE_CXX_STANDARD OR CMAKE_CXX_STANDARD LESS 11)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED YES)
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")
else()
message(STATUS "C++11 activated.")
endif()
# Tell cmake that we need C++11 for dlib
target_compile_features(${target_name}
PUBLIC
cxx_rvalue_references
cxx_variadic_templates
cxx_lambdas
cxx_defaulted_move_initializers
cxx_delegating_constructors
cxx_thread_local
cxx_constexpr
cxx_decltype_incomplete_return_types
cxx_auto_type
)
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()
else()
message(STATUS "C++11 activated.")
endif()
endif()
endif()
@ -109,10 +102,12 @@ if (NOT COMPILER_CAN_DO_CPP_11)
CHECK_CXX_COMPILER_FLAG("-std=c++0x" COMPILER_SUPPORTS_CXX0X)
if(COMPILER_SUPPORTS_CXX11)
message(STATUS "C++11 activated (compiler doesn't have full C++11 support).")
add_global_compiler_switch("-std=c++11")
target_compile_options(${target_name} PUBLIC "-std=c++11")
elseif(COMPILER_SUPPORTS_CXX0X)
message(STATUS "C++0x activated (compiler doesn't have full C++11 support).")
add_global_compiler_switch("-std=c++0x")
target_compile_options(${target_name} PUBLIC "-std=c++0x")
endif()
endif()
endfunction()

View File

@ -40,7 +40,7 @@ project(examples)
# Tell cmake we will need dlib. This command will pull in dlib and compile it
# into your project. Note that you don't need to compile or install dlib. All
# it needs is the dlib source code folder and it will take care of everything.
include(../dlib/cmake)
add_subdirectory(../dlib dlib_build)
# The next thing we need to do is tell CMake about the code you want to