qt-utils.scm 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2016 David Craven <david@craven.ch>
  3. ;;; Copyright © 2019, 2020, 2021 Hartmut Goebel <h.goebel@crazy-compilers.com>
  4. ;;; Copyright © 2020 Jakub Kądziołka <kuba@kadziolka.net>
  5. ;;; Copyright © 2021 Ludovic Courtès <ludo@gnu.org>
  6. ;;; Copyright © 2021, 2022 Maxim Cournoyer <maxim.cournoyer@gmail.com>
  7. ;;; Copyright © 2021 Brendan Tildesley <mail@brendan.scot>
  8. ;;;
  9. ;;; This file is part of GNU Guix.
  10. ;;;
  11. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  12. ;;; under the terms of the GNU General Public License as published by
  13. ;;; the Free Software Foundation; either version 3 of the License, or (at
  14. ;;; your option) any later version.
  15. ;;;
  16. ;;; GNU Guix is distributed in the hope that it will be useful, but
  17. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  18. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. ;;; GNU General Public License for more details.
  20. ;;;
  21. ;;; You should have received a copy of the GNU General Public License
  22. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  23. (define-module (guix build qt-utils)
  24. #:use-module (guix build utils)
  25. #:use-module (ice-9 match)
  26. #:use-module (srfi srfi-1)
  27. #:use-module (srfi srfi-26)
  28. #:use-module (srfi srfi-71)
  29. #:export (wrap-qt-program
  30. wrap-all-qt-programs
  31. %qt-wrap-excluded-inputs))
  32. (define %default-qt-major-version "5")
  33. (define %qt-wrap-excluded-inputs
  34. '(list "cmake" "extra-cmake-modules" "qttools"))
  35. ;; NOTE: Apart from standard subdirectories of /share, Qt also provides
  36. ;; facilities for per-application data directories, such as
  37. ;; /share/quassel. Thus, we include the output directory even if it doesn't
  38. ;; contain any of the standard subdirectories.
  39. (define* (variables-for-wrapping base-directories output-directory
  40. #:key
  41. (qt-major-version %default-qt-major-version))
  42. (define (collect-sub-dirs base-directories file-type subdirectory selectors)
  43. ;; Append SUBDIRECTORY and each of BASE-DIRECTORIES, and return the subset
  44. ;; that exists and has at least one of the SELECTORS sub-directories,
  45. ;; unless SELECTORS is the empty list. FILE-TYPE should by 'directory or
  46. ;; 'regular file. For the later, it allows searching for plain files
  47. ;; rather than directories.
  48. (define exists? (match file-type
  49. ('directory directory-exists?)
  50. ('regular file-exists?)))
  51. (filter-map (lambda (dir)
  52. (let ((directory (string-append dir subdirectory)))
  53. (and (exists? directory)
  54. (or (null? selectors)
  55. (any (lambda (selector)
  56. (exists?
  57. (string-append directory selector)))
  58. selectors))
  59. directory)))
  60. base-directories))
  61. (filter-map
  62. (match-lambda
  63. ((variable type file-type directory selectors ...)
  64. (match (collect-sub-dirs base-directories file-type directory selectors)
  65. (()
  66. #f)
  67. (directories
  68. `(,variable ,type ,directories)))))
  69. ;; These shall match the search-path-specification for Qt and KDE
  70. ;; libraries.
  71. (list
  72. ;; The XDG environment variables are defined with the 'suffix type, which
  73. ;; allows the users to override or extend their value, so that custom icon
  74. ;; themes can be honored, for example.
  75. '("XDG_DATA_DIRS" suffix directory "/share"
  76. ;; These are "selectors": consider /share if and only if at least
  77. ;; one of these sub-directories exist. This avoids adding
  78. ;; irrelevant packages to XDG_DATA_DIRS just because they have a
  79. ;; /share sub-directory.
  80. "/applications" "/cursors" "/fonts" "/icons" "/glib-2.0/schemas"
  81. "/mime" "/sounds" "/themes" "/wallpapers")
  82. '("XDG_CONFIG_DIRS" suffix directory "/etc/xdg")
  83. ;; We wrap exactly to avoid potentially mixing Qt5/Qt6 components, which
  84. ;; would cause warnings, perhaps problems.
  85. `("QT_PLUGIN_PATH" = directory
  86. ,(format #f "/lib/qt~a/plugins" qt-major-version))
  87. `("QML2_IMPORT_PATH" = directory
  88. ,(format #f "/lib/qt~a/qml" qt-major-version))
  89. ;; QTWEBENGINEPROCESS_PATH accepts a single value, which makes 'exact the
  90. ;; most suitable environment variable type for it.
  91. `("QTWEBENGINEPROCESS_PATH" = regular
  92. ,(format #f "/lib/qt~a/libexec/QtWebEngineProcess" qt-major-version)))))
  93. (define* (wrap-qt-program* program #:key inputs output-dir
  94. qt-wrap-excluded-inputs
  95. (qt-major-version %default-qt-major-version))
  96. (define input-directories
  97. (filter-map
  98. (match-lambda
  99. ((label . directory)
  100. (and (not (member label qt-wrap-excluded-inputs))
  101. directory)))
  102. inputs))
  103. (let ((vars-to-wrap (variables-for-wrapping
  104. (cons output-dir input-directories)
  105. output-dir
  106. #:qt-major-version qt-major-version)))
  107. (when (not (null? vars-to-wrap))
  108. (apply wrap-program program vars-to-wrap))))
  109. (define* (wrap-qt-program program-name #:key inputs output
  110. (qt-wrap-excluded-inputs %qt-wrap-excluded-inputs)
  111. (qt-major-version %default-qt-major-version))
  112. "Wrap the specified program (which must reside in the OUTPUT's \"/bin\"
  113. directory) with suitably set environment variables.
  114. This is like qt-build-systems's phase \"qt-wrap\", but only the named program
  115. is wrapped."
  116. (wrap-qt-program* (string-append output "/bin/" program-name)
  117. #:output-dir output #:inputs inputs
  118. #:qt-wrap-excluded-inputs qt-wrap-excluded-inputs
  119. #:qt-major-version qt-major-version))
  120. (define* (wrap-all-qt-programs #:key inputs outputs
  121. qtbase
  122. (qt-wrap-excluded-outputs '())
  123. (qt-wrap-excluded-inputs %qt-wrap-excluded-inputs)
  124. #:allow-other-keys)
  125. "Implement qt-build-systems's phase \"qt-wrap\": look for executables in
  126. \"bin\", \"sbin\" and \"libexec\" of all outputs and create wrappers with
  127. suitably set environment variables if found.
  128. Wrapping is not applied to outputs whose name is listed in
  129. QT-WRAP-EXCLUDED-OUTPUTS. This is useful when an output is known not
  130. to contain any Qt binaries, and where wrapping would gratuitously
  131. add a dependency of that output on Qt."
  132. (define qt-major-version
  133. (if qtbase
  134. (let ((_ version (package-name->name+version
  135. (strip-store-file-name qtbase))))
  136. (first (string-split version #\.)))
  137. ;; Provide a fall-back for build systems not having a #:qtbase
  138. ;; argument.
  139. %default-qt-major-version))
  140. (define (find-files-to-wrap output-dir)
  141. (append-map
  142. (lambda (dir)
  143. (if (directory-exists? dir)
  144. (find-files dir (lambda (file stat)
  145. (not (wrapped-program? file))))
  146. (list)))
  147. (list (string-append output-dir "/bin")
  148. (string-append output-dir "/sbin")
  149. (string-append output-dir "/libexec")
  150. (string-append output-dir "/lib/libexec"))))
  151. (define handle-output
  152. (match-lambda
  153. ((output . output-dir)
  154. (unless (member output qt-wrap-excluded-outputs)
  155. (for-each (cut wrap-qt-program* <>
  156. #:output-dir output-dir #:inputs inputs
  157. #:qt-wrap-excluded-inputs qt-wrap-excluded-inputs
  158. #:qt-major-version qt-major-version)
  159. (find-files-to-wrap output-dir))))))
  160. (for-each handle-output outputs))