glib-or-gtk-build-system.scm 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2014 Federico Beffa <beffa@fbengineering.ch>
  3. ;;; Copyright © 2014, 2015 Ludovic Courtès <ludo@gnu.org>
  4. ;;; Copyright © 2018 Mark H Weaver <mhw@netris.org>
  5. ;;; Copyright © 2021 Maxime Devos <maximedevos@telenet.be>
  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 glib-or-gtk-build-system)
  23. #:use-module ((guix build gnu-build-system) #:prefix gnu:)
  24. #:use-module (guix build utils)
  25. #:use-module (ice-9 match)
  26. #:use-module (ice-9 regex)
  27. #:use-module (ice-9 ftw)
  28. #:use-module (srfi srfi-1)
  29. #:use-module (srfi srfi-26)
  30. #:export (%standard-phases
  31. %gdk-pixbuf-loaders-cache-file
  32. generate-gdk-pixbuf-loaders-cache
  33. glib-or-gtk-build))
  34. ;; Commentary:
  35. ;;
  36. ;; Builder-side code of the standard glib-or-gtk build procedure.
  37. ;;
  38. ;; Code:
  39. (define (subdirectory-exists? parent sub-directory)
  40. (directory-exists? (string-append parent sub-directory)))
  41. (define (directory-included? directory directories-list)
  42. "Is DIRECTORY included in DIRECTORIES-LIST?"
  43. (fold (lambda (s p) (or (string-ci=? s directory) p))
  44. #f directories-list))
  45. ;; We do not include $HOME/.guix-profile/gtk-v.0 (v=2 or 3) because we do not
  46. ;; want to mix gtk+-2 and gtk+-3 modules. See
  47. ;; https://developer.gnome.org/gtk3/stable/gtk-running.html
  48. (define (gtk-module-directories inputs)
  49. "Check for the existence of \"libdir/gtk-v.0\" in INPUTS. Return a list
  50. with all found directories."
  51. (let* ((version
  52. (cond
  53. ((string-match "gtk-4"
  54. (or (assoc-ref inputs "gtk")
  55. (assoc-ref inputs "source")
  56. ""))
  57. "4.0")
  58. ((string-match "gtk\\+-3"
  59. (or (assoc-ref inputs "gtk+")
  60. (assoc-ref inputs "source")
  61. ""))
  62. "3.0")
  63. ((string-match "gtk\\+-2"
  64. (or (assoc-ref inputs "gtk+")
  65. (assoc-ref inputs "source")
  66. ""))
  67. "2.0")
  68. (else
  69. "4.0"))) ; We default to version 4.0.
  70. (gtk-module
  71. (lambda (input prev)
  72. (let* ((in (match input
  73. ((_ . dir) dir)
  74. (_ "")))
  75. (libdir
  76. (string-append in "/lib/gtk-" version)))
  77. (if (and (directory-exists? libdir)
  78. (not (directory-included? libdir prev)))
  79. (cons libdir prev)
  80. prev)))))
  81. (fold gtk-module '() inputs)))
  82. ;; See
  83. ;; http://www.freedesktop.org/wiki/DesktopThemeSpec
  84. ;; http://freedesktop.org/wiki/Specifications/sound-theme-spec
  85. ;; http://freedesktop.org/wiki/Specifications/icon-theme-spec
  86. ;;
  87. ;; Currently desktop themes are not well supported and do not honor
  88. ;; XDG_DATA_DIRS. One example is evince which only looks for desktop themes
  89. ;; in $HOME/.themes (for backward compatibility) and in XDG_DATA_HOME (which
  90. ;; defaults to $HOME/.local/share). One way to handle these applications
  91. ;; appears to be by making $HOME/.themes a symlink to
  92. ;; $HOME/.guix-profile/share/themes.
  93. (define (data-directories inputs)
  94. "Check for the existence of \"$datadir/glib-2.0/schemas\" or XDG themes data
  95. in INPUTS. Return a list with all found directories."
  96. (define (data-directory input previous)
  97. (let* ((in (match input
  98. ((_ . dir) dir)
  99. (_ "")))
  100. (datadir (string-append in "/share")))
  101. (if (and (or (subdirectory-exists? datadir "/glib-2.0/schemas")
  102. (subdirectory-exists? datadir "/sounds")
  103. (subdirectory-exists? datadir "/themes")
  104. (subdirectory-exists? datadir "/cursors")
  105. (subdirectory-exists? datadir "/wallpapers")
  106. (subdirectory-exists? datadir "/icons")
  107. (subdirectory-exists? datadir "/mime")) ;shared-mime-info
  108. (not (directory-included? datadir previous)))
  109. (cons datadir previous)
  110. previous)))
  111. (fold data-directory '() inputs))
  112. ;; All GIO modules are expected to be installed in GLib's $libdir/gio/modules
  113. ;; directory. That directory has to include a file called giomodule.cache
  114. ;; listing all available modules. GIO can be made aware of modules in other
  115. ;; directories with the help of the environment variable GIO_EXTRA_MODULES.
  116. ;; The official GIO documentation states that this environment variable should
  117. ;; only be used for testing and not in a production environment. However, it
  118. ;; appears that there is no other way of specifying multiple modules
  119. ;; directories (NIXOS also does use this variable). See
  120. ;; https://developer.gnome.org/gio/stable/running-gio-apps.html
  121. (define (gio-module-directories inputs)
  122. "Check for the existence of \"$libdir/gio/modules\" in the INPUTS and
  123. returns a list with all found directories."
  124. (define (gio-module-directory input previous)
  125. (let* ((in (match input
  126. ((_ . dir) dir)
  127. (_ "")))
  128. (gio-mod-dir (string-append in "/lib/gio/modules")))
  129. (if (and (directory-exists? gio-mod-dir)
  130. (not (directory-included? gio-mod-dir previous)))
  131. (cons gio-mod-dir previous)
  132. previous)))
  133. (fold gio-module-directory '() inputs))
  134. (define* (wrap-all-programs #:key inputs outputs
  135. (glib-or-gtk-wrap-excluded-outputs '())
  136. #:allow-other-keys)
  137. "Implement phase \"glib-or-gtk-wrap\": look for GSettings schemas and
  138. gtk+-v.0 libraries and create wrappers with suitably set environment variables
  139. if found.
  140. Wrapping is not applied to outputs whose name is listed in
  141. GLIB-OR-GTK-WRAP-EXCLUDED-OUTPUTS. This is useful when an output is known not
  142. to contain any GLib or GTK+ binaries, and where wrapping would gratuitously
  143. add a dependency of that output on GLib and GTK+."
  144. ;; Do not require bash to be present in the package inputs
  145. ;; even when there is nothing to wrap.
  146. ;; Also, calculate (sh) only once to prevent some I/O.
  147. (define %sh (delay (search-input-file inputs "bin/bash")))
  148. (define (sh) (force %sh))
  149. (define handle-output
  150. (match-lambda
  151. ((output . directory)
  152. (unless (member output glib-or-gtk-wrap-excluded-outputs)
  153. (let* ((bindir (string-append directory "/bin"))
  154. (libexecdir (string-append directory "/libexec"))
  155. (bin-list (filter (negate wrapped-program?)
  156. (append (find-files bindir ".*")
  157. (find-files libexecdir ".*"))))
  158. (datadirs (data-directories
  159. (alist-cons output directory inputs)))
  160. (gtk-mod-dirs (gtk-module-directories
  161. (alist-cons output directory inputs)))
  162. (gio-mod-dirs (gio-module-directories
  163. (alist-cons output directory inputs)))
  164. (env-vars `(,@(if (not (null? datadirs))
  165. (list `("XDG_DATA_DIRS" ":" prefix ,datadirs))
  166. '())
  167. ,@(if (not (null? gtk-mod-dirs))
  168. (list `("GTK_PATH" ":" prefix ,gtk-mod-dirs))
  169. '())
  170. ,@(if (not (null? gio-mod-dirs))
  171. (list `("GIO_EXTRA_MODULES" ":"
  172. prefix ,gio-mod-dirs))
  173. '()))))
  174. (for-each (lambda (program)
  175. (apply wrap-program program #:sh (sh) env-vars))
  176. bin-list))))))
  177. (for-each handle-output outputs))
  178. (define* (compile-glib-schemas #:key outputs #:allow-other-keys)
  179. "Implement phase \"glib-or-gtk-compile-schemas\": compile \"glib\" schemas
  180. if needed."
  181. (for-each (match-lambda
  182. ((output . directory)
  183. (let ((schemasdir (string-append directory
  184. "/share/glib-2.0/schemas")))
  185. (when (and (directory-exists? schemasdir)
  186. (not (file-exists?
  187. (string-append schemasdir "/gschemas.compiled"))))
  188. (invoke "glib-compile-schemas" schemasdir)))))
  189. outputs))
  190. ;; This file is to be generated by the
  191. ;; `generate-gdk-pixbuf-loaders-cache' build phase defined below.
  192. (define %gdk-pixbuf-loaders-cache-file
  193. "lib/gdk-pixbuf-2.0/2.10.0/loaders.cache")
  194. (define (generate-gdk-pixbuf-loaders-cache directories outputs)
  195. "Generate the loaders.cache file used by gdk-pixbuf to locate the available
  196. loaders among DIRECTORIES, and set the GDK_PIXBUF_MODULE_FILE environment
  197. variable. The cache file is installed under OUTPUTS. Return the first cache
  198. file name if one was created else #f."
  199. (let* ((loaders (append-map
  200. (cut find-files <> "^libpixbufloader-.*\\.so$")
  201. (delete-duplicates directories)))
  202. (outputs* (map (cut string-append <> "/"
  203. %gdk-pixbuf-loaders-cache-file)
  204. outputs))
  205. (loaders.cache (first outputs*))
  206. (loaders.cache-copies (cdr outputs*)))
  207. (if (not (null? loaders))
  208. (begin
  209. (mkdir-p (dirname loaders.cache))
  210. (setenv "GDK_PIXBUF_MODULE_FILE" loaders.cache)
  211. (apply invoke "gdk-pixbuf-query-loaders" "--update-cache" loaders)
  212. (for-each (lambda (f)
  213. (mkdir-p (dirname f))
  214. (copy-file loaders.cache f))
  215. loaders.cache-copies)
  216. loaders.cache)
  217. #f)))
  218. (define* (generate-gdk-pixbuf-loaders-cache-file #:key inputs outputs
  219. #:allow-other-keys)
  220. "Build phase that Wraps the GENERATE-GDK-PIXBUF-LOADERS-CACHE procedure."
  221. ;; Conditionally compute the cache file if the gdk-pixbuf command is
  222. ;; available on PATH (it comes with gdk-pixbuf).
  223. (when (which "gdk-pixbuf-query-loaders")
  224. (let ((loaders.cache (generate-gdk-pixbuf-loaders-cache
  225. (map cdr inputs)
  226. (filter-map identity
  227. (list
  228. (assoc-ref outputs "out")
  229. (assoc-ref outputs "bin")
  230. (assoc-ref outputs "lib"))))))
  231. (when loaders.cache
  232. (format #t "GDK_PIXBUF_MODULE_FILE set to `~a'~%" loaders.cache)))))
  233. (define %standard-phases
  234. (modify-phases gnu:%standard-phases
  235. (add-after 'unpack 'generate-gdk-pixbuf-loaders-cache-file
  236. generate-gdk-pixbuf-loaders-cache-file)
  237. (add-after 'install 'glib-or-gtk-compile-schemas compile-glib-schemas)
  238. (add-after 'install 'glib-or-gtk-wrap wrap-all-programs)))
  239. (define* (glib-or-gtk-build #:key inputs (phases %standard-phases)
  240. #:allow-other-keys #:rest args)
  241. "Build the given package, applying all of PHASES in order."
  242. (apply gnu:gnu-build #:inputs inputs #:phases phases args))
  243. ;;; glib-or-gtk-build-system.scm ends here