blob: b0de85d805e806dcd90b10332659bb72c003d258 [file] [log] [blame]
Brian Silvermana6f7ce02018-07-07 15:04:00 -07001cmake_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
54target_include_directories(GSL INTERFACE
55 $<BUILD_INTERFACE:
56 ${CMAKE_CURRENT_SOURCE_DIR}/include
57 >
58)
59
60if ((CMAKE_VERSION GREATER 3.7.9) OR (CMAKE_VERSION EQUAL 3.7.9))
61 if (MSVC_IDE)
62 option(VS_ADD_NATIVE_VISUALIZERS "Configure project to use Visual Studio native visualizers" TRUE)
63 else()
64 set(VS_ADD_NATIVE_VISUALIZERS FALSE CACHE INTERNAL "Native visualizers are Visual Studio extension" FORCE)
65 endif()
66
67 # add natvis file to the library so it will automatically be loaded into Visual Studio
68 if(VS_ADD_NATIVE_VISUALIZERS)
69 target_sources(GSL INTERFACE
70 ${CMAKE_CURRENT_SOURCE_DIR}/GSL.natvis
71 )
72 endif()
73endif()
74
75install(
76 DIRECTORY include/gsl
77 DESTINATION include
78)
79
80option(GSL_TEST "Generate tests." ${GSL_STANDALONE_PROJECT})
81if (GSL_TEST)
82 enable_testing()
83 add_subdirectory(tests)
84endif ()