qt-utils.scm 6.6 KB

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