Subject: [PATCH v2 10/65] build-system/cmake: Optionally build tests.

From: Greg Hogan <[email-masked]>
Date: Tue, 22 Oct 2024 18:09:00 +0000

* guix/build/cmake-build-system.scm (configure): Create and use CMake
variable cache file. Set the CMake variable BUILD_TESTING to the value
of TESTS? so that a package can optionally build tests. Set
CMAKE_COLOR_DIAGNOSTICS to ON.

Change-Id: Ia69de938a56733f717d4b4c6207b499bcee524ff
---
 guix/build/cmake-build-system.scm | 75 ++++++++++++++++++-------------
 1 file changed, 44 insertions(+), 31 deletions(-)

diff --git a/guix/build/cmake-build-system.scm b/guix/build/cmake-build-system.scm
index b90a0c14a9..0933801ecb 100644
--- a/guix/build/cmake-build-system.scm
+++ b/guix/build/cmake-build-system.scm
@@ -35,7 +35,7 @@ (define-module (guix build cmake-build-system)
 ;; Code:
 
 (define* (configure #:key outputs (configure-flags '()) (out-of-source? #t)
-                    build-type target generator
+                    build-type target generator (tests? #t)
                     #:allow-other-keys)
   "Configure the given package."
   (let* ((out        (assoc-ref outputs "out"))
@@ -50,37 +50,50 @@ (define* (configure #:key outputs (configure-flags '()) (out-of-source? #t)
       (chdir "../build"))
     (format #t "build directory: ~s~%" (getcwd))
 
-    (let ((args `(,srcdir
-                  ,@(if generator
-                        (list (string-append "-G" generator))
-                        '())
-                  ,@(if build-type
-                        (list (string-append "-DCMAKE_BUILD_TYPE="
-                                             build-type))
-                        '())
-                  ,(string-append "-DCMAKE_INSTALL_PREFIX=" out)
-                  ;; ensure that the libraries are installed into /lib
-                  "-DCMAKE_INSTALL_LIBDIR=lib"
-                  ;; add input libraries to rpath
-                  "-DCMAKE_INSTALL_RPATH_USE_LINK_PATH=TRUE"
-                  ;; add (other) libraries of the project itself to rpath
-                  ,(string-append "-DCMAKE_INSTALL_RPATH=" out "/lib")
-                  ;; enable verbose output from builds
-                  "-DCMAKE_VERBOSE_MAKEFILE=ON"
+    (call-with-temporary-output-file
+      (lambda (temp port)
+        (let ((args `(,srcdir
+                      ;; Load variables into the the cache to prevent
+                      ;; warnings about unused manually-specified variables.
+                      ,(string-append "-C " temp)
+                      ,@(if generator
+                            (list (string-append "-G" generator))
+                            '())
+                      ,@configure-flags)))
 
-                  ;;  Cross-build
-                  ,@(if target
-                        (list (string-append "-DCMAKE_C_COMPILER="
-                                             target "-gcc")
-                              (string-append "-DCMAKE_CXX_COMPILER="
-                                             target "-g++")
-                              (if (string-contains target "mingw")
-                                  "-DCMAKE_SYSTEM_NAME=Windows"
-                                  "-DCMAKE_SYSTEM_NAME=Linux"))
-                        '())
-                  ,@configure-flags)))
-      (format #t "running 'cmake' with arguments ~s~%" args)
-      (apply invoke "cmake" args))))
+          (define save-to-cache
+            (lambda* (name value)
+              ;; <type> and <docstring> arguments are used only by CMake GUIs.
+              (format port "set(~a \"~a\" CACHE STRING \"\")~%" name value)))
+
+          (if build-type
+              (save-to-cache "CMAKE_BUILD_TYPE" build-type))
+          (save-to-cache "CMAKE_INSTALL_PREFIX" out)
+          ;; Ensure that the libraries are installed into /lib.
+          (save-to-cache "CMAKE_INSTALL_LIBDIR" "lib")
+          ;; Add input libraries to rpath.
+          (save-to-cache "CMAKE_INSTALL_RPATH_USE_LINK_PATH" "TRUE")
+          ;; Add (other) libraries of the project itself to rpath.
+          (save-to-cache "CMAKE_INSTALL_RPATH" (string-append out "/lib"))
+          ;; Enable verbose output from builds.
+          (save-to-cache "CMAKE_VERBOSE_MAKEFILE" "ON")
+          ;; Enable colored compiler diagnostics.
+          (save-to-cache "CMAKE_COLOR_DIAGNOSTICS" "ON")
+          ;; BUILD_TESTING in an option of CMake's CTest module.
+          (save-to-cache "BUILD_TESTING" (if tests? "ON" "OFF"))
+
+          ;; Cross-build
+          (if target
+              (begin
+                (save-to-cache "CMAKE_C_COMPILER" (string-append target "-gcc"))
+                (save-to-cache "CMAKE_CXX_COMPILER" (string-append target "-g++"))
+                (if (string-contains target "mingw")
+                    (save-to-cache "CMAKE_SYSTEM_NAME" "Windows")
+                    (save-to-cache "CMAKE_SYSTEM_NAME" "Linux"))))
+
+          (close-port port)
+          (format #t "running 'cmake' with arguments ~s~%" args)
+          (apply invoke "cmake" args))))))
 
 (define* (build #:key (parallel-build? #t) #:allow-other-keys)
   (apply invoke "cmake"
-- 
2.46.1