meson-build-system.scm 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2017 Peter Mikkelsen <petermikkelsen10@gmail.com>
  3. ;;; Copyright © 2018 Ricardo Wurmus <rekado@elephly.net>
  4. ;;; Copyright © 2018 Marius Bakke <mbakke@fastmail.com>
  5. ;;; Copyright © 2021, 2023 Maxim Cournoyer <maxim.cournoyer@gmail.com>
  6. ;;;
  7. ;;; This file is part of GNU Guix.
  8. ;;;
  9. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  10. ;;; under the terms of the GNU General Public License as published by
  11. ;;; the Free Software Foundation; either version 3 of the License, or (at
  12. ;;; your option) any later version.
  13. ;;;
  14. ;;; GNU Guix is distributed in the hope that it will be useful, but
  15. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  16. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. ;;; GNU General Public License for more details.
  18. ;;;
  19. ;;; You should have received a copy of the GNU General Public License
  20. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  21. (define-module (guix build meson-build-system)
  22. #:use-module ((guix build gnu-build-system) #:prefix gnu:)
  23. #:use-module ((guix build glib-or-gtk-build-system) #:prefix glib-or-gtk:)
  24. #:use-module (guix build utils)
  25. #:use-module (guix build gremlin)
  26. #:use-module (guix elf)
  27. #:use-module (ice-9 match)
  28. #:use-module (rnrs io ports)
  29. #:use-module (srfi srfi-1)
  30. #:export (%standard-phases
  31. meson-build))
  32. ;; Commentary:
  33. ;;
  34. ;; Builder-side code of the standard meson build procedure.
  35. ;;
  36. ;; Code:
  37. (define* (configure #:key outputs configure-flags build-type
  38. #:allow-other-keys)
  39. "Configure the given package."
  40. (let* ((out (assoc-ref outputs "out"))
  41. (bindir (assoc-ref outputs "bin"))
  42. (libdir (assoc-ref outputs "lib"))
  43. (includedir (assoc-ref outputs "include"))
  44. (source-dir (getcwd))
  45. (build-dir "../build")
  46. (prefix (assoc-ref outputs "out"))
  47. (args `(,(string-append "--prefix=" prefix)
  48. ,@(if bindir
  49. (list (string-append "--bindir=" bindir "/bin"))
  50. '())
  51. ,@(if libdir
  52. (cons (string-append "--libdir=" libdir "/lib")
  53. (if includedir
  54. '()
  55. (list
  56. (string-append "--includedir="
  57. libdir "/include"))))
  58. '())
  59. ,@(if includedir
  60. (list (string-append "--includedir="
  61. includedir "/include"))
  62. '())
  63. ,(string-append "--buildtype=" build-type)
  64. ,(string-append "-Dc_link_args=-Wl,-rpath="
  65. (assoc-ref outputs "out") "/lib")
  66. ,(string-append "-Dcpp_link_args=-Wl,-rpath="
  67. (assoc-ref outputs "out") "/lib")
  68. ,@configure-flags
  69. ,source-dir)))
  70. (mkdir build-dir)
  71. (chdir build-dir)
  72. (apply invoke "meson" "setup" args)))
  73. (define* (build #:key parallel-build?
  74. #:allow-other-keys)
  75. "Build a given meson package."
  76. (invoke "ninja" "-j" (if parallel-build?
  77. (number->string (parallel-job-count))
  78. "1")))
  79. (define* (check #:key tests? test-options parallel-tests?
  80. #:allow-other-keys)
  81. (if tests?
  82. (begin
  83. (setenv "MESON_TESTTHREADS"
  84. (if parallel-tests?
  85. (number->string (parallel-job-count))
  86. "1"))
  87. ;; Always provide "-t 0" to disable the 30 s default timeout.
  88. (apply invoke "meson" "test" "--print-errorlogs" "-t" "0" test-options))
  89. (format #t "test suite not run~%")))
  90. (define* (install #:rest args)
  91. (invoke "ninja" "install"))
  92. (define* (shrink-runpath #:key (elf-directories '("lib" "lib64" "libexec"
  93. "bin" "sbin"))
  94. outputs #:allow-other-keys)
  95. "Go through all ELF files from ELF-DIRECTORIES and shrink the RUNPATH
  96. since a lot of directories are left over from the build phase of meson,
  97. for example libraries only needed for the tests."
  98. (define handle-output
  99. (match-lambda
  100. ((output . directory)
  101. (let* ((elf-dirnames (map (lambda (subdir)
  102. (string-append directory "/" subdir))
  103. elf-directories))
  104. (existing-elf-dirs (filter (lambda (dir)
  105. (and (file-exists? dir)
  106. (file-is-directory? dir)))
  107. elf-dirnames))
  108. (elf-pred (lambda (name stat)
  109. (elf-file? name)))
  110. (elf-list (concatenate (map (lambda (dir)
  111. (find-files dir elf-pred))
  112. existing-elf-dirs))))
  113. (for-each strip-runpath elf-list)))))
  114. (for-each handle-output (alist-delete "debug" outputs))
  115. #t)
  116. (define %standard-phases
  117. ;; The standard-phases of glib-or-gtk contains a superset of the phases
  118. ;; from the gnu-build-system. If the glib-or-gtk? key is #f (the default)
  119. ;; then the extra phases will be removed again in (guix build-system meson).
  120. (modify-phases glib-or-gtk:%standard-phases
  121. (delete 'bootstrap)
  122. (replace 'configure configure)
  123. (replace 'build build)
  124. (replace 'check check)
  125. (replace 'install install)
  126. (add-after 'strip 'shrink-runpath shrink-runpath)))
  127. (define* (meson-build #:key inputs phases
  128. #:allow-other-keys #:rest args)
  129. "Build the given package, applying all of PHASES in order."
  130. (apply gnu:gnu-build #:inputs inputs #:phases phases args))
  131. ;;; meson-build-system.scm ends here