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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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. ;;;
  6. ;;; This file is part of GNU Guix.
  7. ;;;
  8. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  9. ;;; under the terms of the GNU General Public License as published by
  10. ;;; the Free Software Foundation; either version 3 of the License, or (at
  11. ;;; your option) any later version.
  12. ;;;
  13. ;;; GNU Guix is distributed in the hope that it will be useful, but
  14. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  15. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. ;;; GNU General Public License for more details.
  17. ;;;
  18. ;;; You should have received a copy of the GNU General Public License
  19. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  20. (define-module (guix build glib-or-gtk-build-system)
  21. #:use-module ((guix build gnu-build-system) #:prefix gnu:)
  22. #:use-module (guix build utils)
  23. #:use-module (ice-9 match)
  24. #:use-module (ice-9 regex)
  25. #:use-module (ice-9 ftw)
  26. #:use-module (srfi srfi-1)
  27. #:use-module (srfi srfi-26)
  28. #:export (%standard-phases
  29. glib-or-gtk-build))
  30. ;; Commentary:
  31. ;;
  32. ;; Builder-side code of the standard glib-or-gtk build procedure.
  33. ;;
  34. ;; Code:
  35. (define (subdirectory-exists? parent sub-directory)
  36. (directory-exists? (string-append parent sub-directory)))
  37. (define (directory-included? directory directories-list)
  38. "Is DIRECTORY included in DIRECTORIES-LIST?"
  39. (fold (lambda (s p) (or (string-ci=? s directory) p))
  40. #f directories-list))
  41. ;; We do not include $HOME/.guix-profile/gtk-v.0 (v=2 or 3) because we do not
  42. ;; want to mix gtk+-2 and gtk+-3 modules. See
  43. ;; https://developer.gnome.org/gtk3/stable/gtk-running.html
  44. (define (gtk-module-directories inputs)
  45. "Check for the existence of \"libdir/gtk-v.0\" in INPUTS. Return a list
  46. with all found directories."
  47. (let* ((version
  48. (if (string-match "gtk\\+-3"
  49. (or (assoc-ref inputs "gtk+")
  50. (assoc-ref inputs "source")
  51. "gtk+-3")) ; we default to version 3
  52. "3.0"
  53. "2.0"))
  54. (gtk-module
  55. (lambda (input prev)
  56. (let* ((in (match input
  57. ((_ . dir) dir)
  58. (_ "")))
  59. (libdir
  60. (string-append in "/lib/gtk-" version)))
  61. (if (and (directory-exists? libdir)
  62. (not (directory-included? libdir prev)))
  63. (cons libdir prev)
  64. prev)))))
  65. (fold gtk-module '() inputs)))
  66. ;; See
  67. ;; http://www.freedesktop.org/wiki/DesktopThemeSpec
  68. ;; http://freedesktop.org/wiki/Specifications/sound-theme-spec
  69. ;; http://freedesktop.org/wiki/Specifications/icon-theme-spec
  70. ;;
  71. ;; Currently desktop themes are not well supported and do not honor
  72. ;; XDG_DATA_DIRS. One example is evince which only looks for desktop themes
  73. ;; in $HOME/.themes (for backward compatibility) and in XDG_DATA_HOME (which
  74. ;; defaults to $HOME/.local/share). One way to handle these applications
  75. ;; appears to be by making $HOME/.themes a symlink to
  76. ;; $HOME/.guix-profile/share/themes.
  77. (define (data-directories inputs)
  78. "Check for the existence of \"$datadir/glib-2.0/schemas\" or XDG themes data
  79. in INPUTS. Return a list with all found directories."
  80. (define (data-directory input previous)
  81. (let* ((in (match input
  82. ((_ . dir) dir)
  83. (_ "")))
  84. (datadir (string-append in "/share")))
  85. (if (and (or (subdirectory-exists? datadir "/glib-2.0/schemas")
  86. (subdirectory-exists? datadir "/sounds")
  87. (subdirectory-exists? datadir "/themes")
  88. (subdirectory-exists? datadir "/cursors")
  89. (subdirectory-exists? datadir "/wallpapers")
  90. (subdirectory-exists? datadir "/icons")
  91. (subdirectory-exists? datadir "/mime")) ;shared-mime-info
  92. (not (directory-included? datadir previous)))
  93. (cons datadir previous)
  94. previous)))
  95. (fold data-directory '() inputs))
  96. ;; All GIO modules are expected to be installed in GLib's $libdir/gio/modules
  97. ;; directory. That directory has to include a file called giomodule.cache
  98. ;; listing all available modules. GIO can be made aware of modules in other
  99. ;; directories with the help of the environment variable GIO_EXTRA_MODULES.
  100. ;; The official GIO documentation states that this environment variable should
  101. ;; only be used for testing and not in a production environment. However, it
  102. ;; appears that there is no other way of specifying multiple modules
  103. ;; directories (NIXOS also does use this variable). See
  104. ;; https://developer.gnome.org/gio/stable/running-gio-apps.html
  105. (define (gio-module-directories inputs)
  106. "Check for the existence of \"$libdir/gio/modules\" in the INPUTS and
  107. returns a list with all found directories."
  108. (define (gio-module-directory input previous)
  109. (let* ((in (match input
  110. ((_ . dir) dir)
  111. (_ "")))
  112. (gio-mod-dir (string-append in "/lib/gio/modules")))
  113. (if (and (directory-exists? gio-mod-dir)
  114. (not (directory-included? gio-mod-dir previous)))
  115. (cons gio-mod-dir previous)
  116. previous)))
  117. (fold gio-module-directory '() inputs))
  118. (define* (wrap-all-programs #:key inputs outputs
  119. (glib-or-gtk-wrap-excluded-outputs '())
  120. #:allow-other-keys)
  121. "Implement phase \"glib-or-gtk-wrap\": look for GSettings schemas and
  122. gtk+-v.0 libraries and create wrappers with suitably set environment variables
  123. if found.
  124. Wrapping is not applied to outputs whose name is listed in
  125. GLIB-OR-GTK-WRAP-EXCLUDED-OUTPUTS. This is useful when an output is known not
  126. to contain any GLib or GTK+ binaries, and where wrapping would gratuitously
  127. add a dependency of that output on GLib and GTK+."
  128. (define handle-output
  129. (match-lambda
  130. ((output . directory)
  131. (unless (member output glib-or-gtk-wrap-excluded-outputs)
  132. (let* ((bindir (string-append directory "/bin"))
  133. (libexecdir (string-append directory "/libexec"))
  134. (bin-list (append (find-files bindir ".*")
  135. (find-files libexecdir ".*")))
  136. (datadirs (data-directories
  137. (alist-cons output directory inputs)))
  138. (gtk-mod-dirs (gtk-module-directories
  139. (alist-cons output directory inputs)))
  140. (gio-mod-dirs (gio-module-directories
  141. (alist-cons output directory inputs)))
  142. (data-env-var
  143. (if (not (null? datadirs))
  144. `("XDG_DATA_DIRS" ":" prefix ,datadirs)
  145. #f))
  146. (gtk-mod-env-var
  147. (if (not (null? gtk-mod-dirs))
  148. `("GTK_PATH" ":" prefix ,gtk-mod-dirs)
  149. #f))
  150. (gio-mod-env-var
  151. (if (not (null? gio-mod-dirs))
  152. `("GIO_EXTRA_MODULES" ":" prefix ,gio-mod-dirs)
  153. #f)))
  154. (cond
  155. ((and data-env-var gtk-mod-env-var gio-mod-env-var)
  156. (for-each (cut wrap-program <> inputs
  157. data-env-var
  158. gtk-mod-env-var
  159. gio-mod-env-var)
  160. bin-list))
  161. ((and data-env-var gtk-mod-env-var (not gio-mod-env-var))
  162. (for-each (cut wrap-program <> inputs
  163. data-env-var
  164. gtk-mod-env-var)
  165. bin-list))
  166. ((and data-env-var (not gtk-mod-env-var) gio-mod-env-var)
  167. (for-each (cut wrap-program <> inputs
  168. data-env-var
  169. gio-mod-env-var)
  170. bin-list))
  171. ((and (not data-env-var) gtk-mod-env-var gio-mod-env-var)
  172. (for-each (cut wrap-program <> inputs
  173. gio-mod-env-var
  174. gtk-mod-env-var)
  175. bin-list))
  176. ((and data-env-var (not gtk-mod-env-var) (not gio-mod-env-var))
  177. (for-each (cut wrap-program <> inputs
  178. data-env-var)
  179. bin-list))
  180. ((and (not data-env-var) gtk-mod-env-var (not gio-mod-env-var))
  181. (for-each (cut wrap-program <> inputs
  182. gtk-mod-env-var)
  183. bin-list))
  184. ((and (not data-env-var) (not gtk-mod-env-var) gio-mod-env-var)
  185. (for-each (cut wrap-program <> inputs
  186. gio-mod-env-var)
  187. bin-list))))))))
  188. (for-each handle-output outputs)
  189. #t)
  190. (define* (compile-glib-schemas #:key outputs #:allow-other-keys)
  191. "Implement phase \"glib-or-gtk-compile-schemas\": compile \"glib\" schemas
  192. if needed."
  193. (for-each (match-lambda
  194. ((output . directory)
  195. (let ((schemasdir (string-append directory
  196. "/share/glib-2.0/schemas")))
  197. (when (and (directory-exists? schemasdir)
  198. (not (file-exists?
  199. (string-append schemasdir "/gschemas.compiled"))))
  200. (invoke "glib-compile-schemas" schemasdir)))))
  201. outputs)
  202. #t)
  203. (define %standard-phases
  204. (modify-phases gnu:%standard-phases
  205. (add-after 'install 'glib-or-gtk-compile-schemas compile-glib-schemas)
  206. (add-after 'install 'glib-or-gtk-wrap wrap-all-programs)))
  207. (define* (glib-or-gtk-build #:key inputs (phases %standard-phases)
  208. #:allow-other-keys #:rest args)
  209. "Build the given package, applying all of PHASES in order."
  210. (apply gnu:gnu-build #:inputs inputs #:phases phases args))
  211. ;;; glib-or-gtk-build-system.scm ends here