qt-utils.scm 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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, 2022, 2023 Maxim Cournoyer <maxim.cournoyer@gmail.com>
  8. ;;; Copyright © 2021 Brendan Tildesley <mail@brendan.scot>
  9. ;;;
  10. ;;; This file is part of GNU Guix.
  11. ;;;
  12. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  13. ;;; under the terms of the GNU General Public License as published by
  14. ;;; the Free Software Foundation; either version 3 of the License, or (at
  15. ;;; your option) any later version.
  16. ;;;
  17. ;;; GNU Guix is distributed in the hope that it will be useful, but
  18. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  19. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. ;;; GNU General Public License for more details.
  21. ;;;
  22. ;;; You should have received a copy of the GNU General Public License
  23. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  24. (define-module (guix build qt-utils)
  25. #:use-module (guix build utils)
  26. #:use-module (ice-9 match)
  27. #:use-module (srfi srfi-1)
  28. #:use-module (srfi srfi-26)
  29. #:use-module (srfi srfi-71)
  30. #:export (wrap-qt-program
  31. wrap-all-qt-programs
  32. %qt-wrap-excluded-inputs))
  33. (define %default-qt-major-version "5")
  34. (define %qt-wrap-excluded-inputs
  35. '(list "cmake" "extra-cmake-modules" "qttools"))
  36. ;; NOTE: Apart from standard subdirectories of /share, Qt also provides
  37. ;; facilities for per-application data directories, such as
  38. ;; /share/quassel. Thus, we include the output directory even if it doesn't
  39. ;; contain any of the standard subdirectories.
  40. (define* (variables-for-wrapping base-directories output-directory
  41. #:key
  42. (qt-major-version %default-qt-major-version))
  43. (define (collect-sub-dirs base-directories file-type subdirectory selectors)
  44. ;; Append SUBDIRECTORY and each of BASE-DIRECTORIES, and return the subset
  45. ;; that exists and has at least one of the SELECTORS sub-directories,
  46. ;; unless SELECTORS is the empty list. FILE-TYPE should by 'directory or
  47. ;; 'regular file. For the later, it allows searching for plain files
  48. ;; rather than directories.
  49. (define exists? (match file-type
  50. ('directory directory-exists?)
  51. ('regular file-exists?)))
  52. (filter-map (lambda (dir)
  53. (let ((directory (string-append dir subdirectory)))
  54. (and (exists? directory)
  55. (or (null? selectors)
  56. (any (lambda (selector)
  57. (exists?
  58. (string-append directory selector)))
  59. selectors))
  60. directory)))
  61. base-directories))
  62. (filter-map
  63. (match-lambda
  64. ((variable type file-type directory selectors ...)
  65. (match (collect-sub-dirs base-directories file-type directory selectors)
  66. (()
  67. #f)
  68. (directories
  69. `(,variable ,type ,directories)))))
  70. ;; These shall match the search-path-specification for Qt and KDE
  71. ;; libraries.
  72. (list
  73. ;; The XDG environment variables are defined with the 'suffix type, which
  74. ;; allows the users to override or extend their value, so that custom icon
  75. ;; themes can be honored, for example.
  76. '("XDG_DATA_DIRS" suffix directory "/share"
  77. ;; These are "selectors": consider /share if and only if at least
  78. ;; one of these sub-directories exist. This avoids adding
  79. ;; irrelevant packages to XDG_DATA_DIRS just because they have a
  80. ;; /share sub-directory.
  81. "/applications" "/cursors" "/fonts" "/icons" "/glib-2.0/schemas"
  82. "/mime" "/sounds" "/themes" "/wallpapers")
  83. '("XDG_CONFIG_DIRS" suffix directory "/etc/xdg")
  84. `("QT_PLUGIN_PATH" prefix directory
  85. ,(format #f "/lib/qt~a/plugins" qt-major-version))
  86. `("QML2_IMPORT_PATH" = directory
  87. ,(format #f "/lib/qt~a/qml" qt-major-version))
  88. ;; QTWEBENGINEPROCESS_PATH accepts a single value, which makes 'exact the
  89. ;; most suitable environment variable type for it.
  90. `("QTWEBENGINEPROCESS_PATH" = regular
  91. ,(format #f "/lib/qt~a/libexec/QtWebEngineProcess" qt-major-version)))))
  92. (define* (wrap-qt-program* program #:key sh inputs output-dir
  93. qt-wrap-excluded-inputs
  94. (qt-major-version %default-qt-major-version))
  95. (define input-directories
  96. (filter-map
  97. (match-lambda
  98. ((label . directory)
  99. (and (not (member label qt-wrap-excluded-inputs))
  100. directory)))
  101. inputs))
  102. (let ((vars-to-wrap (variables-for-wrapping
  103. (cons output-dir input-directories)
  104. output-dir
  105. #:qt-major-version qt-major-version)))
  106. (when (not (null? vars-to-wrap))
  107. (apply wrap-program program #:sh sh vars-to-wrap))))
  108. (define* (wrap-qt-program program-name #:key (sh (which "bash")) inputs output
  109. (qt-wrap-excluded-inputs %qt-wrap-excluded-inputs)
  110. (qt-major-version %default-qt-major-version))
  111. "Wrap the specified program (which must reside in the OUTPUT's \"/bin\"
  112. directory) with suitably set environment variables.
  113. This is like qt-build-systems's phase \"qt-wrap\", but only the named program
  114. is wrapped."
  115. (wrap-qt-program* (string-append output "/bin/" program-name)
  116. #:sh sh
  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 (sh (which "bash")) 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. #:sh sh
  157. #:output-dir output-dir #:inputs inputs
  158. #:qt-wrap-excluded-inputs qt-wrap-excluded-inputs
  159. #:qt-major-version qt-major-version)
  160. (find-files-to-wrap output-dir))))))
  161. (for-each handle-output outputs))