1cmake_minimum_required(VERSION 3.1.3)
2
3project(GSL CXX)
4
5include(ExternalProject)
6find_package(Git)
7
8# creates a library GSL which is an interface (header files only)
9add_library(GSL INTERFACE)
10
11# determine whether this is a standalone project or included by other projects
12set(GSL_STANDALONE_PROJECT OFF)
13if (CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR)
14  set(GSL_STANDALONE_PROJECT ON)
15endif ()
16
17set(GSL_CXX_STANDARD "14" CACHE STRING "Use c++ standard")
18set(GSL_CXX_STD "cxx_std_${GSL_CXX_STANDARD}")
19
20if (MSVC)
21    set(GSL_CXX_STD_OPT "-std:c++${GSL_CXX_STANDARD}")
22else()
23    set(GSL_CXX_STD_OPT "-std=c++${GSL_CXX_STANDARD}")
24endif()
25
26# when minimum version required is 3.8.0 remove if below
27# both branches do exactly the same thing
28if (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()
37else ()
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)
41endif()
42
43# add definitions to the library and targets that consume it
44target_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
55if(GSL_STANDALONE_PROJECT)
56    target_include_directories(GSL INTERFACE
57        $<BUILD_INTERFACE:
58            ${CMAKE_CURRENT_SOURCE_DIR}/include
59        >
60    )
61else()
62    target_include_directories(GSL SYSTEM INTERFACE
63        $<BUILD_INTERFACE:
64            ${CMAKE_CURRENT_SOURCE_DIR}/include
65        >
66    )
67endif()
68
69
70if (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()
83endif()
84
85install(
86    DIRECTORY include/gsl
87    DESTINATION include
88)
89
90option(GSL_TEST "Generate tests." ${GSL_STANDALONE_PROJECT})
91if (GSL_TEST)
92	enable_testing()
93	add_subdirectory(tests)
94endif ()
95