minetest-build-system.scm 9.3 KB

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