From 1c3cd188e4d56cbd60a7d70e903682fc03219358 Mon Sep 17 00:00:00 2001
From: Lars Bilke <lars.bilke@ufz.de>
Date: Tue, 23 Mar 2021 09:38:33 +0100
Subject: [PATCH] [CMake] Collect third-party (CPM) licenses.

- New target write-licenses to write license file
third_party_licenses.txt
- License file is installed into ./share/info/
---
 scripts/ci/extends/template-build-linux.yml |  2 +-
 scripts/cmake/Dependencies.cmake            | 10 ++++++++++
 scripts/cmake/packaging/Pack.cmake          | 12 +++++-------
 3 files changed, 16 insertions(+), 8 deletions(-)

diff --git a/scripts/ci/extends/template-build-linux.yml b/scripts/ci/extends/template-build-linux.yml
index e39b5e74b1a..e2ed2485e1f 100644
--- a/scripts/ci/extends/template-build-linux.yml
+++ b/scripts/ci/extends/template-build-linux.yml
@@ -31,5 +31,5 @@
       fi
     - |
       if [[ "$CHECK_WARNINGS" = true ]] ; then
-          if [[ $(cat make.output | grep warning -i) ]]; then printf 'There were compiler warnings:\n\n'; cat make.output | grep warning -i; exit 1; fi
+          if [[ $(cat make.output | grep warning -i | grep -v 'WARNING: no license files found') ]]; then printf 'There were compiler warnings:\n\n'; cat make.output | grep warning -i | grep -v 'WARNING: no license files found'; exit 1; fi
       fi
diff --git a/scripts/cmake/Dependencies.cmake b/scripts/cmake/Dependencies.cmake
index 75ad4beb456..b2ed384942a 100644
--- a/scripts/cmake/Dependencies.cmake
+++ b/scripts/cmake/Dependencies.cmake
@@ -411,3 +411,13 @@ if(CLANG_FORMAT_PROGRAM OR CMAKE_FORMAT_PROGRAM)
             "CMAKE_FORMAT_EXCLUDE scripts/cmake/CPM.cmake|.*/Tests.cmake|scripts/cmake/jedbrown/.*|scripts/cmake/conan/conan.cmake|scripts/cmake/vector-of-bool/.*"
     )
 endif()
+
+# Third-party licenses
+CPMAddPackage(
+    NAME CPMLicenses.cmake GITHUB_REPOSITORY cpm-cmake/CPMLicenses.cmake
+    VERSION 0.0.5
+)
+cpm_licenses_create_disclaimer_target(
+    write-licenses "${PROJECT_BINARY_DIR}/third_party_licenses.txt"
+    "${CPM_PACKAGES}"
+)
diff --git a/scripts/cmake/packaging/Pack.cmake b/scripts/cmake/packaging/Pack.cmake
index ddc52dddd66..9440814d3d6 100644
--- a/scripts/cmake/packaging/Pack.cmake
+++ b/scripts/cmake/packaging/Pack.cmake
@@ -82,14 +82,12 @@ endif()
 configure_file(Documentation/README.txt.in ${PROJECT_BINARY_DIR}/README.txt)
 install(FILES ${PROJECT_BINARY_DIR}/README.txt DESTINATION .)
 
-install(FILES ${PROJECT_BINARY_DIR}/CMakeCache.txt
-        DESTINATION ${CMAKE_INSTALL_INFODIR}
+install(FILES ${PROJECT_BINARY_DIR}/CMakeCache.txt TYPE INFO)
+install(FILES ${PROJECT_BINARY_DIR}/cmake_args TYPE INFO OPTIONAL)
+install(
+    CODE "execute_process(COMMAND ${CMAKE_COMMAND} --build ${PROJECT_BINARY_DIR} -t write-licenses)"
 )
-if(EXISTS ${PROJECT_BINARY_DIR}/cmake-args)
-    install(FILES ${PROJECT_BINARY_DIR}/cmake-args
-            DESTINATION ${CMAKE_INSTALL_INFODIR}
-    )
-endif()
+install(FILES ${PROJECT_BINARY_DIR}/third_party_licenses.txt TYPE INFO)
 
 # Install dependencies via GET_RUNTIME_DEPENDENCIES. Available since CMake 3.16.
 if(${CMAKE_VERSION} VERSION_LESS 3.16 OR NOT OGS_INSTALL_DEPENDENCIES)
-- 
GitLab