GSL/CMakeLists.txt

104 lines
3.5 KiB
CMake
Raw Normal View History

cmake_minimum_required(VERSION 3.1.3)
2015-08-20 21:09:14 -04:00
project(GSL CXX)
2015-08-20 21:09:14 -04:00
include(ExternalProject)
find_package(Git)
# Use GNUInstallDirs to provide the right locations on all platforms
include(GNUInstallDirs)
Update CMake usage (#493) * Refactor cmake file to have GSL as an interface CMake supports header only libraries as INTERFACE libraries. Using interfaces libraries make is easier for users to use the library because one just need to "link" agaisnt the library and necessary include paths, definitions, flags... will be taken care of. This commit creates a new interface library called GSL. It then add the following things to the GSL library: - compiler flags ex: (-std=c++14) - definitions ex: _SCL_SECURE_NO_WARNINGS - include paths ex: include/gsl - natvis file Another project can now have the GSL project as a git submodule and one only need to add the two following lines to their project to use the GSL. add_subdirectory(GSL) target_link_libraries(<some target> GSL) After cmake 3.8.0 a lot of the logic can be simplified. Right now the cmake file has an if for version checking, but when the minimun required version is 3.8.0 one can just delete the branching and keep the simpler version. * Cut support for c++11 Compiling on GCC6.2 with only the c++11 flag will generate compilation errors. For example some of the errors are related to the use of enable_if_t which is a c++14 feature. To avoid compilation errors this comiit removes c++11 support on linux. * Refactor code that pulls unittest-cpp Two minor changes: - uses cmake to find a proper installation of git (in case user does not have it on the path) - checks for the CMakeLists file instead. This is needed for the build itself and seems like a better way to do the checking * Refactor tests so they show together on VS This commit will make a VS geenrated project to group all tests under GSL_tests * Refactor tests configuration This creates a test configuration interface and add all the previous compiler options to that interface. compiler options are now sorted so it is easier to find them, and also one per line, so that modifications are easier to track from git.
2017-04-25 20:08:36 -04:00
# creates a library GSL which is an interface (header files only)
add_library(GSL INTERFACE)
# determine whether this is a standalone project or included by other projects
set(GSL_STANDALONE_PROJECT OFF)
if (CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR)
set(GSL_STANDALONE_PROJECT ON)
endif ()
set(GSL_CXX_STANDARD "14" CACHE STRING "Use c++ standard")
set(GSL_CXX_STD "cxx_std_${GSL_CXX_STANDARD}")
if (MSVC)
set(GSL_CXX_STD_OPT "-std:c++${GSL_CXX_STANDARD}")
else()
set(GSL_CXX_STD_OPT "-std=c++${GSL_CXX_STANDARD}")
endif()
Update CMake usage (#493) * Refactor cmake file to have GSL as an interface CMake supports header only libraries as INTERFACE libraries. Using interfaces libraries make is easier for users to use the library because one just need to "link" agaisnt the library and necessary include paths, definitions, flags... will be taken care of. This commit creates a new interface library called GSL. It then add the following things to the GSL library: - compiler flags ex: (-std=c++14) - definitions ex: _SCL_SECURE_NO_WARNINGS - include paths ex: include/gsl - natvis file Another project can now have the GSL project as a git submodule and one only need to add the two following lines to their project to use the GSL. add_subdirectory(GSL) target_link_libraries(<some target> GSL) After cmake 3.8.0 a lot of the logic can be simplified. Right now the cmake file has an if for version checking, but when the minimun required version is 3.8.0 one can just delete the branching and keep the simpler version. * Cut support for c++11 Compiling on GCC6.2 with only the c++11 flag will generate compilation errors. For example some of the errors are related to the use of enable_if_t which is a c++14 feature. To avoid compilation errors this comiit removes c++11 support on linux. * Refactor code that pulls unittest-cpp Two minor changes: - uses cmake to find a proper installation of git (in case user does not have it on the path) - checks for the CMakeLists file instead. This is needed for the build itself and seems like a better way to do the checking * Refactor tests so they show together on VS This commit will make a VS geenrated project to group all tests under GSL_tests * Refactor tests configuration This creates a test configuration interface and add all the previous compiler options to that interface. compiler options are now sorted so it is easier to find them, and also one per line, so that modifications are easier to track from git.
2017-04-25 20:08:36 -04:00
# when minimum version required is 3.8.0 remove if below
# both branches do exactly the same thing
if (CMAKE_VERSION VERSION_LESS 3.7.9)
include(CheckCXXCompilerFlag)
CHECK_CXX_COMPILER_FLAG("${GSL_CXX_STD_OPT}" COMPILER_SUPPORTS_CXX_STANDARD)
if(COMPILER_SUPPORTS_CXX_STANDARD)
target_compile_options(GSL INTERFACE "${GSL_CXX_STD_OPT}")
else()
message(FATAL_ERROR "The compiler ${CMAKE_CXX_COMPILER} has no c++${GSL_CXX_STANDARD} support. Please use a different C++ compiler.")
Update CMake usage (#493) * Refactor cmake file to have GSL as an interface CMake supports header only libraries as INTERFACE libraries. Using interfaces libraries make is easier for users to use the library because one just need to "link" agaisnt the library and necessary include paths, definitions, flags... will be taken care of. This commit creates a new interface library called GSL. It then add the following things to the GSL library: - compiler flags ex: (-std=c++14) - definitions ex: _SCL_SECURE_NO_WARNINGS - include paths ex: include/gsl - natvis file Another project can now have the GSL project as a git submodule and one only need to add the two following lines to their project to use the GSL. add_subdirectory(GSL) target_link_libraries(<some target> GSL) After cmake 3.8.0 a lot of the logic can be simplified. Right now the cmake file has an if for version checking, but when the minimun required version is 3.8.0 one can just delete the branching and keep the simpler version. * Cut support for c++11 Compiling on GCC6.2 with only the c++11 flag will generate compilation errors. For example some of the errors are related to the use of enable_if_t which is a c++14 feature. To avoid compilation errors this comiit removes c++11 support on linux. * Refactor code that pulls unittest-cpp Two minor changes: - uses cmake to find a proper installation of git (in case user does not have it on the path) - checks for the CMakeLists file instead. This is needed for the build itself and seems like a better way to do the checking * Refactor tests so they show together on VS This commit will make a VS geenrated project to group all tests under GSL_tests * Refactor tests configuration This creates a test configuration interface and add all the previous compiler options to that interface. compiler options are now sorted so it is easier to find them, and also one per line, so that modifications are easier to track from git.
2017-04-25 20:08:36 -04:00
endif()
else ()
target_compile_features(GSL INTERFACE "${GSL_CXX_STD}")
Update CMake usage (#493) * Refactor cmake file to have GSL as an interface CMake supports header only libraries as INTERFACE libraries. Using interfaces libraries make is easier for users to use the library because one just need to "link" agaisnt the library and necessary include paths, definitions, flags... will be taken care of. This commit creates a new interface library called GSL. It then add the following things to the GSL library: - compiler flags ex: (-std=c++14) - definitions ex: _SCL_SECURE_NO_WARNINGS - include paths ex: include/gsl - natvis file Another project can now have the GSL project as a git submodule and one only need to add the two following lines to their project to use the GSL. add_subdirectory(GSL) target_link_libraries(<some target> GSL) After cmake 3.8.0 a lot of the logic can be simplified. Right now the cmake file has an if for version checking, but when the minimun required version is 3.8.0 one can just delete the branching and keep the simpler version. * Cut support for c++11 Compiling on GCC6.2 with only the c++11 flag will generate compilation errors. For example some of the errors are related to the use of enable_if_t which is a c++14 feature. To avoid compilation errors this comiit removes c++11 support on linux. * Refactor code that pulls unittest-cpp Two minor changes: - uses cmake to find a proper installation of git (in case user does not have it on the path) - checks for the CMakeLists file instead. This is needed for the build itself and seems like a better way to do the checking * Refactor tests so they show together on VS This commit will make a VS geenrated project to group all tests under GSL_tests * Refactor tests configuration This creates a test configuration interface and add all the previous compiler options to that interface. compiler options are now sorted so it is easier to find them, and also one per line, so that modifications are easier to track from git.
2017-04-25 20:08:36 -04:00
# on *nix systems force the use of -std=c++XX instead of -std=gnu++XX (default)
set(CMAKE_CXX_EXTENSIONS OFF)
endif()
# add definitions to the library and targets that consume it
target_compile_definitions(GSL INTERFACE
$<$<CXX_COMPILER_ID:MSVC>:
# remove unnecessary warnings about unchecked iterators
_SCL_SECURE_NO_WARNINGS
# remove deprecation warnings about std::uncaught_exception() (from catch)
_SILENCE_CXX17_UNCAUGHT_EXCEPTION_DEPRECATION_WARNING
Update CMake usage (#493) * Refactor cmake file to have GSL as an interface CMake supports header only libraries as INTERFACE libraries. Using interfaces libraries make is easier for users to use the library because one just need to "link" agaisnt the library and necessary include paths, definitions, flags... will be taken care of. This commit creates a new interface library called GSL. It then add the following things to the GSL library: - compiler flags ex: (-std=c++14) - definitions ex: _SCL_SECURE_NO_WARNINGS - include paths ex: include/gsl - natvis file Another project can now have the GSL project as a git submodule and one only need to add the two following lines to their project to use the GSL. add_subdirectory(GSL) target_link_libraries(<some target> GSL) After cmake 3.8.0 a lot of the logic can be simplified. Right now the cmake file has an if for version checking, but when the minimun required version is 3.8.0 one can just delete the branching and keep the simpler version. * Cut support for c++11 Compiling on GCC6.2 with only the c++11 flag will generate compilation errors. For example some of the errors are related to the use of enable_if_t which is a c++14 feature. To avoid compilation errors this comiit removes c++11 support on linux. * Refactor code that pulls unittest-cpp Two minor changes: - uses cmake to find a proper installation of git (in case user does not have it on the path) - checks for the CMakeLists file instead. This is needed for the build itself and seems like a better way to do the checking * Refactor tests so they show together on VS This commit will make a VS geenrated project to group all tests under GSL_tests * Refactor tests configuration This creates a test configuration interface and add all the previous compiler options to that interface. compiler options are now sorted so it is easier to find them, and also one per line, so that modifications are easier to track from git.
2017-04-25 20:08:36 -04:00
>
)
# add include folders to the library and targets that consume it
# the SYSTEM keyword suppresses warnings for users of the library
if(GSL_STANDALONE_PROJECT)
target_include_directories(GSL INTERFACE
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
)
else()
target_include_directories(GSL SYSTEM INTERFACE
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
)
endif()
Update CMake usage (#493) * Refactor cmake file to have GSL as an interface CMake supports header only libraries as INTERFACE libraries. Using interfaces libraries make is easier for users to use the library because one just need to "link" agaisnt the library and necessary include paths, definitions, flags... will be taken care of. This commit creates a new interface library called GSL. It then add the following things to the GSL library: - compiler flags ex: (-std=c++14) - definitions ex: _SCL_SECURE_NO_WARNINGS - include paths ex: include/gsl - natvis file Another project can now have the GSL project as a git submodule and one only need to add the two following lines to their project to use the GSL. add_subdirectory(GSL) target_link_libraries(<some target> GSL) After cmake 3.8.0 a lot of the logic can be simplified. Right now the cmake file has an if for version checking, but when the minimun required version is 3.8.0 one can just delete the branching and keep the simpler version. * Cut support for c++11 Compiling on GCC6.2 with only the c++11 flag will generate compilation errors. For example some of the errors are related to the use of enable_if_t which is a c++14 feature. To avoid compilation errors this comiit removes c++11 support on linux. * Refactor code that pulls unittest-cpp Two minor changes: - uses cmake to find a proper installation of git (in case user does not have it on the path) - checks for the CMakeLists file instead. This is needed for the build itself and seems like a better way to do the checking * Refactor tests so they show together on VS This commit will make a VS geenrated project to group all tests under GSL_tests * Refactor tests configuration This creates a test configuration interface and add all the previous compiler options to that interface. compiler options are now sorted so it is easier to find them, and also one per line, so that modifications are easier to track from git.
2017-04-25 20:08:36 -04:00
if (CMAKE_VERSION VERSION_GREATER 3.7.8)
if (MSVC_IDE)
option(VS_ADD_NATIVE_VISUALIZERS "Configure project to use Visual Studio native visualizers" TRUE)
else()
set(VS_ADD_NATIVE_VISUALIZERS FALSE CACHE INTERNAL "Native visualizers are Visual Studio extension" FORCE)
endif()
# add natvis file to the library so it will automatically be loaded into Visual Studio
if(VS_ADD_NATIVE_VISUALIZERS)
target_sources(GSL INTERFACE
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/GSL.natvis>
)
endif()
endif()
Update CMake usage (#493) * Refactor cmake file to have GSL as an interface CMake supports header only libraries as INTERFACE libraries. Using interfaces libraries make is easier for users to use the library because one just need to "link" agaisnt the library and necessary include paths, definitions, flags... will be taken care of. This commit creates a new interface library called GSL. It then add the following things to the GSL library: - compiler flags ex: (-std=c++14) - definitions ex: _SCL_SECURE_NO_WARNINGS - include paths ex: include/gsl - natvis file Another project can now have the GSL project as a git submodule and one only need to add the two following lines to their project to use the GSL. add_subdirectory(GSL) target_link_libraries(<some target> GSL) After cmake 3.8.0 a lot of the logic can be simplified. Right now the cmake file has an if for version checking, but when the minimun required version is 3.8.0 one can just delete the branching and keep the simpler version. * Cut support for c++11 Compiling on GCC6.2 with only the c++11 flag will generate compilation errors. For example some of the errors are related to the use of enable_if_t which is a c++14 feature. To avoid compilation errors this comiit removes c++11 support on linux. * Refactor code that pulls unittest-cpp Two minor changes: - uses cmake to find a proper installation of git (in case user does not have it on the path) - checks for the CMakeLists file instead. This is needed for the build itself and seems like a better way to do the checking * Refactor tests so they show together on VS This commit will make a VS geenrated project to group all tests under GSL_tests * Refactor tests configuration This creates a test configuration interface and add all the previous compiler options to that interface. compiler options are now sorted so it is easier to find them, and also one per line, so that modifications are easier to track from git.
2017-04-25 20:08:36 -04:00
install(TARGETS GSL EXPORT Microsoft.GSLConfig)
install(
DIRECTORY include/gsl
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
)
# Make library importable by other projects
install(EXPORT Microsoft.GSLConfig NAMESPACE Microsoft.GSL:: DESTINATION share/Microsoft.GSL/cmake)
export(TARGETS GSL NAMESPACE Microsoft.GSL:: FILE Microsoft.GSLConfig.cmake)
# Add Microsoft.GSL::GSL alias for GSL so that dependents can be agnostic about
# whether GSL was added via `add_subdirectory` or `find_package`
add_library(Microsoft.GSL::GSL ALIAS GSL)
option(GSL_TEST "Generate tests." ${GSL_STANDALONE_PROJECT})
if (GSL_TEST)
enable_testing()
add_subdirectory(tests)
endif ()