From d452c3b061549008404b81e359c8a8e3f2f043de Mon Sep 17 00:00:00 2001 From: Stefan Reinhold Date: Tue, 17 Jul 2018 10:23:44 +0200 Subject: [PATCH 01/11] Make library importable by CMake using find_package --- CMakeLists.txt | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index b0de85d..8259516 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -5,6 +5,9 @@ project(GSL CXX) include(ExternalProject) find_package(Git) +# Use GNUInstallDirs to provide the right locations on all platforms +include(GNUInstallDirs) + # creates a library GSL which is an interface (header files only) add_library(GSL INTERFACE) @@ -55,6 +58,7 @@ target_include_directories(GSL INTERFACE $ + $ ) if ((CMAKE_VERSION GREATER 3.7.9) OR (CMAKE_VERSION EQUAL 3.7.9)) @@ -72,10 +76,18 @@ if ((CMAKE_VERSION GREATER 3.7.9) OR (CMAKE_VERSION EQUAL 3.7.9)) endif() endif() +install(TARGETS GSL EXPORT GSLConfig + ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} + LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} + RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} +) install( DIRECTORY include/gsl - DESTINATION include + DESTINATION ${CMAKE_INSTALL_INCLUDEDIR} ) +# Make library importable by other projects +install(EXPORT GSLConfig DESTINATION share/GSL/cmake) +export(TARGETS GSL FILE GSLConfig.cmake) option(GSL_TEST "Generate tests." ${GSL_STANDALONE_PROJECT}) if (GSL_TEST) From 030454e3e9b6cbb46e6be5bb31c730bca715b1b0 Mon Sep 17 00:00:00 2001 From: Stefan Reinhold Date: Tue, 17 Jul 2018 11:09:58 +0200 Subject: [PATCH 02/11] Fix for Windows: CMake complains about INTERFACE_SOURCES directory --- CMakeLists.txt | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 8259516..1693069 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -58,7 +58,9 @@ target_include_directories(GSL INTERFACE $ - $ + $ ) if ((CMAKE_VERSION GREATER 3.7.9) OR (CMAKE_VERSION EQUAL 3.7.9)) From 0a31a14d0939f6cb1f6b92bb73429b9165a1b0bc Mon Sep 17 00:00:00 2001 From: Stefan Reinhold Date: Tue, 17 Jul 2018 11:56:38 +0200 Subject: [PATCH 03/11] Fix INSTALL_INTERFACE for exported target CMake war reporting: 'Target "GSL" INTERFACE_SOURCES property contains path: "..." which is prefixed in the source directory'. CMake was confused because the $ had linebreaks in it. Putting everything in one line fixed the issue. --- CMakeLists.txt | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 1693069..f9b0454 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -55,12 +55,8 @@ target_compile_definitions(GSL INTERFACE # add include folders to the library and targets that consume it target_include_directories(GSL INTERFACE - $ - $ + $ + $ ) if ((CMAKE_VERSION GREATER 3.7.9) OR (CMAKE_VERSION EQUAL 3.7.9)) @@ -79,9 +75,7 @@ if ((CMAKE_VERSION GREATER 3.7.9) OR (CMAKE_VERSION EQUAL 3.7.9)) endif() install(TARGETS GSL EXPORT GSLConfig - ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} - LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} - RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} + PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR} ) install( DIRECTORY include/gsl From 9e1645b990e6476e40a531f7964d9f2b33f7e7e4 Mon Sep 17 00:00:00 2001 From: Stefan Reinhold Date: Tue, 17 Jul 2018 13:07:27 +0200 Subject: [PATCH 04/11] Do not install .natvis files. This fixes an issue where CMake complains about INTERFACE_SOURCES containing a path which is prefixed in source directory on windows. --- CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index f9b0454..077160c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -69,7 +69,7 @@ if ((CMAKE_VERSION GREATER 3.7.9) OR (CMAKE_VERSION EQUAL 3.7.9)) # add natvis file to the library so it will automatically be loaded into Visual Studio if(VS_ADD_NATIVE_VISUALIZERS) target_sources(GSL INTERFACE - ${CMAKE_CURRENT_SOURCE_DIR}/GSL.natvis + $ ) endif() endif() From e2b57d16f92b7d4fa8363dfefcced940dd658645 Mon Sep 17 00:00:00 2001 From: Stefan Reinhold Date: Tue, 17 Jul 2018 17:43:54 +0200 Subject: [PATCH 05/11] Export target GSL in namespace Microsoft.GSL This is to avoid conflicts with GNU Scientific Library which also has an exported target named GSL. --- CMakeLists.txt | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 077160c..7cf29a7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -74,7 +74,7 @@ if ((CMAKE_VERSION GREATER 3.7.9) OR (CMAKE_VERSION EQUAL 3.7.9)) endif() endif() -install(TARGETS GSL EXPORT GSLConfig +install(TARGETS GSL EXPORT Microsoft.GSLConfig PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR} ) install( @@ -82,8 +82,8 @@ install( DESTINATION ${CMAKE_INSTALL_INCLUDEDIR} ) # Make library importable by other projects -install(EXPORT GSLConfig DESTINATION share/GSL/cmake) -export(TARGETS GSL FILE GSLConfig.cmake) +install(EXPORT Microsoft.GSLConfig NAMESPACE Microsoft.GSL:: DESTINATION share/Microsoft.GSL/cmake) +export(TARGETS GSL NAMESPACE Microsoft.GSL:: FILE GSLConfig.cmake) option(GSL_TEST "Generate tests." ${GSL_STANDALONE_PROJECT}) if (GSL_TEST) From e3ffe1c5cbc11b01c535365acdde0a3e2fc6c2a3 Mon Sep 17 00:00:00 2001 From: Stefan Reinhold Date: Tue, 17 Jul 2018 17:56:11 +0200 Subject: [PATCH 06/11] Fix naming of exported CMake configuration file --- CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 7cf29a7..c10d025 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -83,7 +83,7 @@ install( ) # 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 GSLConfig.cmake) +export(TARGETS GSL NAMESPACE Microsoft.GSL:: FILE Microsoft.GSLConfig.cmake) option(GSL_TEST "Generate tests." ${GSL_STANDALONE_PROJECT}) if (GSL_TEST) From ffe8ffb452a52305863d9aab6499fd621b1eab25 Mon Sep 17 00:00:00 2001 From: Stefan Reinhold Date: Wed, 18 Jul 2018 09:35:03 +0200 Subject: [PATCH 07/11] Dummy commit to force CI re-run From ebe88443447012a40c8b95742650c78a696ef5ec Mon Sep 17 00:00:00 2001 From: Hannes Steffenhagen Date: Sat, 8 Feb 2020 12:49:43 +0000 Subject: [PATCH 08/11] Make GSL includes based on CMAKE_INSTALL_INCLUDEDIR This is to avoid inconsistencies with the `install` command which specifies this as the destination for includes. --- CMakeLists.txt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index ff226ef..96ab24b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -58,12 +58,12 @@ target_compile_definitions(GSL INTERFACE if(GSL_STANDALONE_PROJECT) target_include_directories(GSL INTERFACE $ - $ + $ ) else() target_include_directories(GSL SYSTEM INTERFACE $ - $ + $ ) endif() From 886fc95142ab4156eab45dada3daace1d42c8dd9 Mon Sep 17 00:00:00 2001 From: Hannes Steffenhagen Date: Sat, 8 Feb 2020 12:55:19 +0000 Subject: [PATCH 09/11] Remove PUBLIC_HEADER attribute from `install` command MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Since we already explicitly install `include/gsl`, this doesn’t do anything. --- CMakeLists.txt | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 96ab24b..b7c1370 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -83,9 +83,7 @@ if (CMAKE_VERSION VERSION_GREATER 3.7.8) endif() endif() -install(TARGETS GSL EXPORT Microsoft.GSLConfig - PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR} -) +install(TARGETS GSL EXPORT Microsoft.GSLConfig) install( DIRECTORY include/gsl DESTINATION ${CMAKE_INSTALL_INCLUDEDIR} From 9c0c6b246ceeba4d11a024b05ad6c2e8f7d2ca69 Mon Sep 17 00:00:00 2001 From: Hannes Steffenhagen Date: Sat, 8 Feb 2020 12:58:25 +0000 Subject: [PATCH 10/11] Add Microsoft.GSL::GSL alias for GSL This ensures that dependents can be agnostic about whether GSL was added via `add_subdirectory` or `find_package` --- CMakeLists.txt | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index b7c1370..e0d704e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -92,6 +92,10 @@ install( 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() From 849f14083a08e1c8bcba1fe4bba94683a333e77b Mon Sep 17 00:00:00 2001 From: Hannes Steffenhagen Date: Sat, 8 Feb 2020 13:11:11 +0000 Subject: [PATCH 11/11] Add a note on CMake usage of exported target to the README --- README.md | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/README.md b/README.md index 183553f..bc43251 100644 --- a/README.md +++ b/README.md @@ -95,5 +95,14 @@ Include the library using: #include +## Usage in CMake + +The library provides a Config file for CMake, once installed it can be found via + + find_package(Microsoft.GSL CONFIG) + +Which, when successful, will add library target called `Microsoft.GSL::GSL` which you can use via the usual +`target_link_libraries` mechanism. + ## Debugging visualization support For Visual Studio users, the file [GSL.natvis](./GSL.natvis) in the root directory of the repository can be added to your project if you would like more helpful visualization of GSL types in the Visual Studio debugger than would be offered by default.