Rian Quinn | d933e51 | 2018-04-27 13:56:25 -0600 | [diff] [blame] | 1 | cmake_minimum_required(VERSION 3.1.3) |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 2 | |
Tamas Kenez | 9b454b7 | 2015-09-23 17:43:36 +0200 | [diff] [blame] | 3 | project(GSL CXX) |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 4 | |
Neil MacIntosh | b2ee484 | 2017-07-13 13:53:56 -0700 | [diff] [blame] | 5 | include(ExternalProject) |
Nicholas Guriev | e249f86 | 2017-08-17 06:00:30 +0400 | [diff] [blame] | 6 | find_package(Git) |
Neil MacIntosh | b2ee484 | 2017-07-13 13:53:56 -0700 | [diff] [blame] | 7 | |
Tiago | 8b320e3 | 2017-04-25 17:08:36 -0700 | [diff] [blame] | 8 | # creates a library GSL which is an interface (header files only) |
| 9 | add_library(GSL INTERFACE) |
| 10 | |
bfierz | b014508 | 2017-09-07 02:50:30 +0200 | [diff] [blame] | 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 | |
Anna Gringauze | 21cb6bb | 2018-06-15 10:13:11 -0700 | [diff] [blame] | 17 | set(GSL_CXX_STANDARD "14" CACHE STRING "Use c++ standard") |
| 18 | set(GSL_CXX_STD "cxx_std_${GSL_CXX_STANDARD}") |
Anna Gringauze | cb2d1af | 2018-06-07 13:36:56 -0700 | [diff] [blame] | 19 | |
Anna Gringauze | 21cb6bb | 2018-06-15 10:13:11 -0700 | [diff] [blame] | 20 | if (MSVC) |
| 21 | set(GSL_CXX_STD_OPT "-std:c++${GSL_CXX_STANDARD}") |
Anna Gringauze | cb2d1af | 2018-06-07 13:36:56 -0700 | [diff] [blame] | 22 | else() |
Anna Gringauze | 21cb6bb | 2018-06-15 10:13:11 -0700 | [diff] [blame] | 23 | set(GSL_CXX_STD_OPT "-std=c++${GSL_CXX_STANDARD}") |
Anna Gringauze | cb2d1af | 2018-06-07 13:36:56 -0700 | [diff] [blame] | 24 | endif() |
| 25 | |
Tiago | 8b320e3 | 2017-04-25 17:08:36 -0700 | [diff] [blame] | 26 | # when minimum version required is 3.8.0 remove if below |
| 27 | # both branches do exactly the same thing |
Jérémie Delaitre | 30b1641 | 2018-02-14 14:26:44 +1300 | [diff] [blame] | 28 | if (CMAKE_VERSION VERSION_LESS 3.7.9) |
Anna Gringauze | 21cb6bb | 2018-06-15 10:13:11 -0700 | [diff] [blame] | 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}") |
Anna Gringauze | cb2d1af | 2018-06-07 13:36:56 -0700 | [diff] [blame] | 34 | else() |
Anna Gringauze | 21cb6bb | 2018-06-15 10:13:11 -0700 | [diff] [blame] | 35 | message(FATAL_ERROR "The compiler ${CMAKE_CXX_COMPILER} has no c++${GSL_CXX_STANDARD} support. Please use a different C++ compiler.") |
Tiago | 8b320e3 | 2017-04-25 17:08:36 -0700 | [diff] [blame] | 36 | endif() |
| 37 | else () |
Anna Gringauze | 21cb6bb | 2018-06-15 10:13:11 -0700 | [diff] [blame] | 38 | target_compile_features(GSL INTERFACE "${GSL_CXX_STD}") |
Tiago | 8b320e3 | 2017-04-25 17:08:36 -0700 | [diff] [blame] | 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 |
Anna Gringauze | 21cb6bb | 2018-06-15 10:13:11 -0700 | [diff] [blame] | 48 | # remove deprecation warnings about std::uncaught_exception() (from catch) |
| 49 | _SILENCE_CXX17_UNCAUGHT_EXCEPTION_DEPRECATION_WARNING |
Tiago | 8b320e3 | 2017-04-25 17:08:36 -0700 | [diff] [blame] | 50 | > |
| 51 | ) |
| 52 | |
| 53 | # add include folders to the library and targets that consume it |
hannesweisbach | 0f68d13 | 2018-11-29 00:37:59 +0100 | [diff] [blame] | 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 | |
Tiago | 8b320e3 | 2017-04-25 17:08:36 -0700 | [diff] [blame] | 69 | |
k-brac | be3c3c2 | 2019-01-15 01:43:17 +0100 | [diff] [blame] | 70 | if (CMAKE_VERSION VERSION_GREATER 3.7.8) |
Rian Quinn | d933e51 | 2018-04-27 13:56:25 -0600 | [diff] [blame] | 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() |
paweldac | 210cc08 | 2017-11-28 16:31:06 +0100 | [diff] [blame] | 76 | |
Rian Quinn | d933e51 | 2018-04-27 13:56:25 -0600 | [diff] [blame] | 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() |
paweldac | 210cc08 | 2017-11-28 16:31:06 +0100 | [diff] [blame] | 83 | endif() |
Tiago | 8b320e3 | 2017-04-25 17:08:36 -0700 | [diff] [blame] | 84 | |
Casey Carter | 4e8f95b | 2017-02-07 15:59:37 -0800 | [diff] [blame] | 85 | install( |
| 86 | DIRECTORY include/gsl |
| 87 | DESTINATION include |
Jason Horsburgh | 7505111 | 2015-11-21 19:13:21 +0000 | [diff] [blame] | 88 | ) |
| 89 | |
bfierz | b014508 | 2017-09-07 02:50:30 +0200 | [diff] [blame] | 90 | option(GSL_TEST "Generate tests." ${GSL_STANDALONE_PROJECT}) |
| 91 | if (GSL_TEST) |
| 92 | enable_testing() |
| 93 | add_subdirectory(tests) |
| 94 | endif () |