1 cmake_minimum_required(VERSION 3.1.3) 2 3 project(GSL CXX) 4 5 include(ExternalProject) 6 find_package(Git) 7 8 # creates a library GSL which is an interface (header files only) 9 add_library(GSL INTERFACE) 10 11 # determine whether this is a standalone project or included by other projects 12 set(GSL_STANDALONE_PROJECT OFF) 13 if (CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR) 14 set(GSL_STANDALONE_PROJECT ON) 15 endif () 16 17 set(GSL_CXX_STANDARD "14" CACHE STRING "Use c++ standard") 18 set(GSL_CXX_STD "cxx_std_${GSL_CXX_STANDARD}") 19 20 if (MSVC) 21 set(GSL_CXX_STD_OPT "-std:c++${GSL_CXX_STANDARD}") 22 else() 23 set(GSL_CXX_STD_OPT "-std=c++${GSL_CXX_STANDARD}") 24 endif() 25 26 # when minimum version required is 3.8.0 remove if below 27 # both branches do exactly the same thing 28 if (CMAKE_VERSION VERSION_LESS 3.7.9) 29 include(CheckCXXCompilerFlag) 30 CHECK_CXX_COMPILER_FLAG("${GSL_CXX_STD_OPT}" COMPILER_SUPPORTS_CXX_STANDARD) 31 32 if(COMPILER_SUPPORTS_CXX_STANDARD) 33 target_compile_options(GSL INTERFACE "${GSL_CXX_STD_OPT}") 34 else() 35 message(FATAL_ERROR "The compiler ${CMAKE_CXX_COMPILER} has no c++${GSL_CXX_STANDARD} support. Please use a different C++ compiler.") 36 endif() 37 else () 38 target_compile_features(GSL INTERFACE "${GSL_CXX_STD}") 39 # on *nix systems force the use of -std=c++XX instead of -std=gnu++XX (default) 40 set(CMAKE_CXX_EXTENSIONS OFF) 41 endif() 42 43 # add definitions to the library and targets that consume it 44 target_compile_definitions(GSL INTERFACE 45 $<$<CXX_COMPILER_ID:MSVC>: 46 # remove unnecessary warnings about unchecked iterators 47 _SCL_SECURE_NO_WARNINGS 48 # remove deprecation warnings about std::uncaught_exception() (from catch) 49 _SILENCE_CXX17_UNCAUGHT_EXCEPTION_DEPRECATION_WARNING 50 > 51 ) 52 53 # add include folders to the library and targets that consume it 54 # the SYSTEM keyword suppresses warnings for users of the library 55 if(GSL_STANDALONE_PROJECT) 56 target_include_directories(GSL INTERFACE 57 $<BUILD_INTERFACE: 58 ${CMAKE_CURRENT_SOURCE_DIR}/include 59 > 60 ) 61 else() 62 target_include_directories(GSL SYSTEM INTERFACE 63 $<BUILD_INTERFACE: 64 ${CMAKE_CURRENT_SOURCE_DIR}/include 65 > 66 ) 67 endif() 68 69 70 if (CMAKE_VERSION VERSION_GREATER 3.7.8) 71 if (MSVC_IDE) 72 option(VS_ADD_NATIVE_VISUALIZERS "Configure project to use Visual Studio native visualizers" TRUE) 73 else() 74 set(VS_ADD_NATIVE_VISUALIZERS FALSE CACHE INTERNAL "Native visualizers are Visual Studio extension" FORCE) 75 endif() 76 77 # add natvis file to the library so it will automatically be loaded into Visual Studio 78 if(VS_ADD_NATIVE_VISUALIZERS) 79 target_sources(GSL INTERFACE 80 ${CMAKE_CURRENT_SOURCE_DIR}/GSL.natvis 81 ) 82 endif() 83 endif() 84 85 install( 86 DIRECTORY include/gsl 87 DESTINATION include 88 ) 89 90 option(GSL_TEST "Generate tests." ${GSL_STANDALONE_PROJECT}) 91 if (GSL_TEST) 92 enable_testing() 93 add_subdirectory(tests) 94 endif () 95