minetest-build-system.scm 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. ;;; Copyright © 2021 Maxime Devos <maximedevos@telenet.be>
  2. ;;;
  3. ;;; This file is part of GNU Guix.
  4. ;;;
  5. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  6. ;;; under the terms of the GNU General Public License as published by
  7. ;;; the Free Software Foundation; either version 3 of the License, or (at
  8. ;;; your option) any later version.
  9. ;;;
  10. ;;; GNU Guix is distributed in the hope that it will be useful, but
  11. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ;;; GNU General Public License for more details.
  14. ;;;
  15. ;;; You should have received a copy of the GNU General Public License
  16. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  17. (define-module (guix build minetest-build-system)
  18. #:use-module (guix build utils)
  19. #:use-module (srfi srfi-1)
  20. #:use-module (ice-9 format)
  21. #:use-module (ice-9 match)
  22. #:use-module (ice-9 rdelim)
  23. #:use-module (ice-9 receive)
  24. #:use-module (ice-9 regex)
  25. #:use-module (ice-9 exceptions)
  26. #:use-module ((guix build gnu-build-system) #:prefix gnu:)
  27. #:use-module ((guix build copy-build-system) #:prefix copy:)
  28. #:export (%standard-phases
  29. mod-install-plan minimise-png read-mod-name check))
  30. ;; (guix build copy-build-system) does not export 'install'.
  31. (define copy:install
  32. (assoc-ref copy:%standard-phases 'install))
  33. (define (mod-install-plan mod-name)
  34. `(("." ,(string-append "share/minetest/mods/" mod-name)
  35. ;; Only install files that will actually be used at run time.
  36. ;; This can save a little disk space.
  37. ;;
  38. ;; See <https://github.com/minetest/minetest/blob/master/doc/lua_api.txt>
  39. ;; for an incomple list of files that can be found in mods.
  40. #:include ("mod.conf" "modpack.conf" "settingtypes.txt" "depends.txt"
  41. "description.txt" "config.txt" "_config.txt")
  42. #:include-regexp (".lua$" ".png$" ".ogg$" ".obj$" ".b3d$" ".tr$"
  43. ".mts$"))))
  44. (define* (guess-mod-name #:key inputs #:allow-other-keys)
  45. "Try to determine the name of the mod or modpack that is being built.
  46. If it is unknown, make an educated guess."
  47. ;; Minetest doesn't care about the directory names in "share/minetest/mods"
  48. ;; so there is no technical problem if the directory names don't match
  49. ;; the mod names. The directory can appear in the GUI if the modpack
  50. ;; doesn't have the 'name' set though, so try to make a guess.
  51. (define (guess)
  52. (let* ((source (assoc-ref inputs "source"))
  53. ;; Don't retain a reference to the store.
  54. (file-name (strip-store-file-name source))
  55. ;; The "minetest-" prefix is not informative, so strip it.
  56. (file-name (if (string-prefix? "minetest-" file-name)
  57. (substring file-name (string-length "minetest-"))
  58. file-name))
  59. ;; Strip "-checkout" suffixes of git checkouts.
  60. (file-name (if (string-suffix? "-checkout" file-name)
  61. (substring file-name
  62. 0
  63. (- (string-length file-name)
  64. (string-length "-checkout")))
  65. file-name))
  66. (first-dot (string-index file-name #\.))
  67. ;; If the source code is in an archive (.tar.gz, .zip, ...),
  68. ;; strip the extension.
  69. (file-name (if first-dot
  70. (substring file-name 0 first-dot)
  71. file-name)))
  72. (format (current-error-port)
  73. "warning: the modpack ~a did not set 'name' in 'modpack.conf'~%"
  74. file-name)
  75. file-name))
  76. (cond ((file-exists? "mod.conf")
  77. ;; Mods must have 'name' set in "mod.conf", so don't guess.
  78. (read-mod-name "mod.conf"))
  79. ((file-exists? "modpack.conf")
  80. ;; While it is recommended to have 'name' set in 'modpack.conf',
  81. ;; it is optional, so guess a name if necessary.
  82. (read-mod-name "modpack.conf" guess))
  83. (#t (guess))))
  84. (define* (install #:key inputs #:allow-other-keys #:rest arguments)
  85. (apply copy:install
  86. #:install-plan (mod-install-plan (apply guess-mod-name arguments))
  87. arguments))
  88. (define %png-magic-bytes
  89. ;; Magic bytes of PNG images, see ‘5.2 PNG signatures’ in
  90. ;; ‘Portable Network Graphics (PNG) Specification (Second Edition)’
  91. ;; on <https://www.w3.org/TR/PNG/>.
  92. #vu8(137 80 78 71 13 10 26 10))
  93. (define png-file?
  94. ((@@ (guix build utils) file-header-match) %png-magic-bytes))
  95. (define* (minimise-png #:key inputs native-inputs #:allow-other-keys)
  96. "Minimise PNG images found in the working directory."
  97. (define optipng (which "optipng"))
  98. (define (optimise image)
  99. (format #t "Optimising ~a~%" image)
  100. (make-file-writable (dirname image))
  101. (make-file-writable image)
  102. (define old-size (stat:size (stat image)))
  103. ;; The mod "technic" has a file "technic_music_player_top.png" that
  104. ;; actually is a JPEG file, see
  105. ;; <https://github.com/minetest-mods/technic/issues/590>.
  106. (if (png-file? image)
  107. (invoke optipng "-o4" "-quiet" image)
  108. (format #t "warning: skipping ~a because it's not actually a PNG image~%"
  109. image))
  110. (define new-size (stat:size (stat image)))
  111. (values old-size new-size))
  112. (define files (find-files "." ".png$"))
  113. (let loop ((total-old-size 0)
  114. (total-new-size 0)
  115. (images (find-files "." ".png$")))
  116. (cond ((pair? images)
  117. (receive (old-size new-size)
  118. (optimise (car images))
  119. (loop (+ total-old-size old-size)
  120. (+ total-new-size new-size)
  121. (cdr images))))
  122. ((= total-old-size 0)
  123. (format #t "There were no PNG images to minimise."))
  124. (#t
  125. (format #t "Minimisation reduced size of images by ~,2f% (~,2f MiB to ~,2f MiB)~%"
  126. (* 100.0 (- 1 (/ total-new-size total-old-size)))
  127. (/ total-old-size (expt 1024 2))
  128. (/ total-new-size (expt 1024 2)))))))
  129. (define name-regexp (make-regexp "^name[ ]*=(.+)$"))
  130. (define* (read-mod-name mod.conf #:optional not-found)
  131. "Read the name of a mod from MOD.CONF. If MOD.CONF
  132. does not have a name field and NOT-FOUND is #false, raise an
  133. error. If NOT-FOUND is TRUE, call NOT-FOUND instead."
  134. (call-with-input-file mod.conf
  135. (lambda (port)
  136. (let loop ()
  137. (define line (read-line port))
  138. (if (eof-object? line)
  139. (if not-found
  140. (not-found)
  141. (error "~a does not have a 'name' field" mod.conf))
  142. (let ((match (regexp-exec name-regexp line)))
  143. (if (regexp-match? match)
  144. (string-trim-both (match:substring match 1) #\ )
  145. (loop))))))))
  146. (define* (check #:key outputs tests? #:allow-other-keys)
  147. "Test whether the mod loads. The mod must first be installed first."
  148. (define (all-mod-names directories)
  149. (append-map
  150. (lambda (directory)
  151. (map read-mod-name (find-files directory "mod.conf")))
  152. directories))
  153. (when tests?
  154. (mkdir "guix_testworld")
  155. ;; Add the mod to the mod search path, such that Minetest can find it.
  156. (setenv "MINETEST_MOD_PATH"
  157. (list->search-path-as-string
  158. (cons
  159. (string-append (assoc-ref outputs "out") "/share/minetest/mods")
  160. (search-path-as-string->list
  161. (or (getenv "MINETEST_MOD_PATH") "")))
  162. ":"))
  163. (with-directory-excursion "guix_testworld"
  164. (setenv "HOME" (getcwd))
  165. ;; Create a world in which all mods are loaded.
  166. (call-with-output-file "world.mt"
  167. (lambda (port)
  168. (display
  169. "gameid = minetest
  170. world_name = guix_testworld
  171. backend = sqlite3
  172. player_backend = sqlite3
  173. auth_backend = sqlite3
  174. " port)
  175. (for-each
  176. (lambda (mod)
  177. (format port "load_mod_~a = true~%" mod))
  178. (all-mod-names (search-path-as-string->list
  179. (getenv "MINETEST_MOD_PATH"))))))
  180. (receive (port pid)
  181. ((@@ (guix build utils) open-pipe-with-stderr)
  182. "xvfb-run" "--" "minetest" "--info" "--world" "." "--go")
  183. (format #t "Started Minetest with all mods loaded for testing~%")
  184. ;; Scan the output for error messages.
  185. ;; When the player has joined the server, stop minetest.
  186. (define (error? line)
  187. (and (string? line)
  188. (string-contains line ": ERROR[")))
  189. (define (stop? line)
  190. (and (string? line)
  191. (string-contains line "ACTION[Server]: singleplayer [127.0.0.1] joins game.")))
  192. (let loop ((has-errors? #f))
  193. (match `(,(read-line port) ,has-errors?)
  194. (((? error? line) _)
  195. (display line)
  196. (newline)
  197. (loop #t))
  198. (((? stop?) #f)
  199. (kill pid SIGINT)
  200. (close-port port)
  201. (waitpid pid))
  202. (((? eof-object?) #f)
  203. (error "minetest didn't start"))
  204. (((or (? stop?) (? eof-object?)) #t)
  205. (error "minetest raised an error"))
  206. (((? string? line) has-error?)
  207. (display line)
  208. (newline)
  209. (loop has-error?))))))))
  210. (define %standard-phases
  211. (modify-phases gnu:%standard-phases
  212. (delete 'bootstrap)
  213. (delete 'configure)
  214. (add-before 'build 'minimise-png minimise-png)
  215. (delete 'build)
  216. (delete 'check)
  217. (replace 'install install)
  218. ;; The 'check' phase requires the mod to be installed,
  219. ;; so move the 'check' phase after the 'install' phase.
  220. (add-after 'install 'check check)))
  221. ;;; minetest-build-system.scm ends here