guix: qt-build-system, qt-utils: Unify wrapping of qt-programs.
[jackhill/guix/guix.git] / guix / build / qt-utils.scm
CommitLineData
2098d024
DC
1;;; GNU Guix --- Functional package management for GNU
2;;; Copyright © 2016 David Craven <david@craven.ch>
7e24e1e5 3;;; Copyright © 2019, 2020, 2021 Hartmut Goebel <h.goebel@crazy-compilers.com>
2098d024
DC
4;;;
5;;; This file is part of GNU Guix.
6;;;
7;;; GNU Guix is free software; you can redistribute it and/or modify it
8;;; under the terms of the GNU General Public License as published by
9;;; the Free Software Foundation; either version 3 of the License, or (at
10;;; your option) any later version.
11;;;
12;;; GNU Guix is distributed in the hope that it will be useful, but
13;;; WITHOUT ANY WARRANTY; without even the implied warranty of
14;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15;;; GNU General Public License for more details.
16;;;
17;;; You should have received a copy of the GNU General Public License
18;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
19
20(define-module (guix build qt-utils)
21 #:use-module (guix build utils)
7e24e1e5
HG
22 #:use-module (ice-9 match)
23 #:use-module (srfi srfi-1)
24 #:use-module (srfi srfi-26)
25 #:export (wrap-qt-program
26 wrap-all-qt-programs))
27
28
29(define (variables-for-wrapping base-directories)
30
31 (define (collect-sub-dirs base-directories subdirectory)
32 (filter-map
33 (lambda (dir)
34 (let ((directory (string-append dir subdirectory)))
35 (if (directory-exists? directory) directory #f)))
36 base-directories))
37
38 (filter
39 (lambda (var-to-wrap) (not (null? (last var-to-wrap))))
40 (map
41 (lambda (var-spec)
42 `(,(first var-spec) = ,(collect-sub-dirs base-directories (last var-spec))))
43 (list
44 ;; these shall match the search-path-specification for Qt and KDE
45 ;; libraries
46 '("XDG_DATA_DIRS" "/share")
47 '("XDG_CONFIG_DIRS" "/etc/xdg")
48 '("QT_PLUGIN_PATH" "/lib/qt5/plugins")
49 '("QML2_IMPORT_PATH" "/lib/qt5/qml")))))
50
51
52(define* (wrap-qt-program* program #:key inputs output-dir)
53
54 (define input-directories
55 ;; FIXME: Filter out unwanted inputs, e.g. cmake
56 (match inputs
57 (((_ . dir) ...)
58 dir)))
59
60 (let ((vars-to-wrap (variables-for-wrapping
61 (cons output-dir input-directories))))
62 (when (not (null? vars-to-wrap))
63 (apply wrap-program program vars-to-wrap))))
64
65
66(define* (wrap-qt-program program-name #:key inputs output)
67 "Wrap the specified programm (which must reside in the OUTPUT's \"/bin\"
68directory) with suitably set environment variables.
69
70This is like qt-build-systems's phase \"qt-wrap\", but only the named program
71is wrapped."
72 (wrap-qt-program* (string-append output "/bin/" program-name)
73 #:output-dir output #:inputs inputs))
74
75
76(define* (wrap-all-qt-programs #:key inputs outputs
77 (qt-wrap-excluded-outputs '())
78 #:allow-other-keys)
79 "Implement qt-build-systems's phase \"qt-wrap\": look for executables in
80\"bin\", \"sbin\" and \"libexec\" of all outputs and create wrappers with
81suitably set environment variables if found.
82
83Wrapping is not applied to outputs whose name is listed in
84QT-WRAP-EXCLUDED-OUTPUTS. This is useful when an output is known not
85to contain any Qt binaries, and where wrapping would gratuitously
86add a dependency of that output on Qt."
87 (define (find-files-to-wrap output-dir)
88 (append-map
89 (lambda (dir)
90 (if (directory-exists? dir) (find-files dir ".*") (list)))
91 (list (string-append output-dir "/bin")
92 (string-append output-dir "/sbin")
93 (string-append output-dir "/libexec")
94 (string-append output-dir "/lib/libexec"))))
95
96 (define handle-output
97 (match-lambda
98 ((output . output-dir)
99 (unless (member output qt-wrap-excluded-outputs)
100 (for-each (cut wrap-qt-program* <>
101 #:output-dir output-dir #:inputs inputs)
102 (find-files-to-wrap output-dir))))))
103
104 (for-each handle-output outputs)
105 #t)