qt-utils.scm 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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 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. #:export (wrap-qt-program
  29. wrap-all-qt-programs
  30. %qt-wrap-excluded-inputs))
  31. (define %qt-wrap-excluded-inputs
  32. '(list "cmake" "extra-cmake-modules" "qttools"))
  33. ;; NOTE: Apart from standard subdirectories of /share, Qt also provides
  34. ;; facilities for per-application data directories, such as
  35. ;; /share/quassel. Thus, we include the output directory even if it doesn't
  36. ;; contain any of the standard subdirectories.
  37. (define (variables-for-wrapping base-directories output-directory)
  38. (define (collect-sub-dirs base-directories file-type subdirectory selectors)
  39. ;; Append SUBDIRECTORY and each of BASE-DIRECTORIES, and return the subset
  40. ;; that exists and has at least one of the SELECTORS sub-directories,
  41. ;; unless SELECTORS is the empty list. FILE-TYPE should by 'directory or
  42. ;; 'regular file. For the later, it allows searching for plain files
  43. ;; rather than directories.
  44. (define exists? (match file-type
  45. ('directory directory-exists?)
  46. ('regular file-exists?)))
  47. (filter-map (lambda (dir)
  48. (let ((directory (string-append dir subdirectory)))
  49. (and (exists? directory)
  50. (or (null? selectors)
  51. (any (lambda (selector)
  52. (exists?
  53. (string-append directory selector)))
  54. selectors))
  55. directory)))
  56. base-directories))
  57. (filter-map
  58. (match-lambda
  59. ((variable type file-type directory selectors ...)
  60. (match (collect-sub-dirs base-directories file-type directory selectors)
  61. (()
  62. #f)
  63. (directories
  64. `(,variable ,type ,directories)))))
  65. ;; These shall match the search-path-specification for Qt and KDE
  66. ;; libraries.
  67. (list
  68. ;; The XDG environment variables are defined with the 'suffix type, which
  69. ;; allows the users to override or extend their value, so that custom icon
  70. ;; themes can be honored, for example.
  71. '("XDG_DATA_DIRS" suffix directory "/share"
  72. ;; These are "selectors": consider /share if and only if at least
  73. ;; one of these sub-directories exist. This avoids adding
  74. ;; irrelevant packages to XDG_DATA_DIRS just because they have a
  75. ;; /share sub-directory.
  76. "/applications" "/cursors" "/fonts" "/icons" "/glib-2.0/schemas"
  77. "/mime" "/sounds" "/themes" "/wallpapers")
  78. '("XDG_CONFIG_DIRS" suffix directory "/etc/xdg")
  79. ;; The following variables can be extended by the user, but not
  80. ;; overridden, to ensure proper operation.
  81. '("QT_PLUGIN_PATH" prefix directory "/lib/qt5/plugins")
  82. '("QML2_IMPORT_PATH" prefix directory "/lib/qt5/qml")
  83. ;; QTWEBENGINEPROCESS_PATH accepts a single value, which makes 'exact the
  84. ;; most suitable environment variable type for it.
  85. '("QTWEBENGINEPROCESS_PATH" = regular
  86. "/lib/qt5/libexec/QtWebEngineProcess"))))
  87. (define* (wrap-qt-program* program #:key inputs output-dir
  88. qt-wrap-excluded-inputs)
  89. (define input-directories
  90. (filter-map
  91. (match-lambda
  92. ((label . directory)
  93. (and (not (member label qt-wrap-excluded-inputs))
  94. directory)))
  95. inputs))
  96. (let ((vars-to-wrap (variables-for-wrapping
  97. (cons output-dir input-directories)
  98. output-dir)))
  99. (when (not (null? vars-to-wrap))
  100. (apply wrap-program program vars-to-wrap))))
  101. (define* (wrap-qt-program program-name #:key inputs output
  102. (qt-wrap-excluded-inputs %qt-wrap-excluded-inputs))
  103. "Wrap the specified program (which must reside in the OUTPUT's \"/bin\"
  104. directory) with suitably set environment variables.
  105. This is like qt-build-systems's phase \"qt-wrap\", but only the named program
  106. is wrapped."
  107. (wrap-qt-program* (string-append output "/bin/" program-name)
  108. #:output-dir output #:inputs inputs
  109. #:qt-wrap-excluded-inputs qt-wrap-excluded-inputs))
  110. (define* (wrap-all-qt-programs #:key inputs outputs
  111. (qt-wrap-excluded-outputs '())
  112. (qt-wrap-excluded-inputs %qt-wrap-excluded-inputs)
  113. #:allow-other-keys)
  114. "Implement qt-build-systems's phase \"qt-wrap\": look for executables in
  115. \"bin\", \"sbin\" and \"libexec\" of all outputs and create wrappers with
  116. suitably set environment variables if found.
  117. Wrapping is not applied to outputs whose name is listed in
  118. QT-WRAP-EXCLUDED-OUTPUTS. This is useful when an output is known not
  119. to contain any Qt binaries, and where wrapping would gratuitously
  120. add a dependency of that output on Qt."
  121. (define (find-files-to-wrap output-dir)
  122. (append-map
  123. (lambda (dir)
  124. (if (directory-exists? dir)
  125. (find-files dir (lambda (file stat)
  126. (not (wrapped-program? file))))
  127. (list)))
  128. (list (string-append output-dir "/bin")
  129. (string-append output-dir "/sbin")
  130. (string-append output-dir "/libexec")
  131. (string-append output-dir "/lib/libexec"))))
  132. (define handle-output
  133. (match-lambda
  134. ((output . output-dir)
  135. (unless (member output qt-wrap-excluded-outputs)
  136. (for-each (cut wrap-qt-program* <>
  137. #:output-dir output-dir #:inputs inputs
  138. #:qt-wrap-excluded-inputs qt-wrap-excluded-inputs)
  139. (find-files-to-wrap output-dir))))))
  140. (for-each handle-output outputs))