From 514507d84e6d426c183e2a4809ba0a3ad252e93f Mon Sep 17 00:00:00 2001
Message-ID: <514507d84e6d426c183e2a4809ba0a3ad252e93f.1730111243.git.arnaud.lechevallier@free.fr>
From: Arnaud Lechevallier <arnaud.lechevallier@free.fr>
Date: Mon, 28 Oct 2024 11:08:02 +0100
Subject: [PATCH v1] gnu: Add guile-raylib

* gnu/packages/game-development.scm: (raylib-with-extras) new variable.

* gnu/packages/guile-xyz.scm: (guile-raylib) new variable.

Change-Id: I7d4b961186249da17a114ce7f1ac2fd93ac7fa08
---
 gnu/packages/game-development.scm |  51 +++++++++++++++
 gnu/packages/guile-xyz.scm        | 102 ++++++++++++++++++++++++++++++
 2 files changed, 153 insertions(+)

diff --git a/gnu/packages/game-development.scm b/gnu/packages/game-development.scm
index 0bae7cfe11..2c9c6a1548 100644
--- a/gnu/packages/game-development.scm
+++ b/gnu/packages/game-development.scm
@@ -30,6 +30,7 @@
 ;;; Copyright © 2022 dan <i@dan.games>
 ;;; Copyright © 2023, 2024 John Kehayias <john.kehayias@protonmail.com>
 ;;; Copyright © 2024 Nicolas Graves <ngraves@ngraves.fr>
+;;; Copyright © 2024 Arnaud Lechevallier <arnaud.lechevallier@free.fr>
 ;;;
 ;;; This file is part of GNU Guix.
 ;;;
@@ -3311,6 +3312,56 @@ (define-public raylib
     (home-page "https://www.raylib.com/")
     (license license:zlib)))
 
+(define-public raylib-with-extras
+  (let ((commit "282d6478baa51a509bf0a4b1d761a0bd7fd8bbf7")
+        (revision "0"))
+    (package
+      (inherit raylib)
+      (name "raylib")
+      (version (git-version "5.0" revision commit))
+      (source
+       (origin
+         (method git-fetch)
+         (uri (git-reference
+               (url "https://github.com/raysan5/raylib/")
+               (commit commit)))
+         (file-name (git-file-name name version))
+         (sha256
+          (base32 "03ml6vhn6lsrcl9d5xg310hkkayxdccz660qs2zwfn9dwcsw2rl8"))))
+      (arguments
+       (list
+        #:tests? #f ;no test
+        #:configure-flags #~(list "-DBUILD_SHARED_LIBS=ON"
+                                  "-DUSE_EXTERNAL_GLFW=ON"
+                                  "-DCMAKE_C_FLAGS=-lpulse")
+        #:phases #~(modify-phases %standard-phases
+                     (add-before 'configure 'configure-miniaudio
+                       ;; Use PulseAudio as raudio backend.
+                       (lambda _
+                         (substitute* "src/raudio.c"
+                           (("^#include \"external/miniaudio\\.h\"")
+                            "
+   #define MA_NO_RUNTIME_LINKING
+   #define MA_ENABLE_ONLY_SPECIFIC_BACKENDS
+   #define MA_ENABLE_PULSEAUDIO
+   #include \"external/miniaudio.h\"
+   "))))
+                     (add-before 'install 'install-parser
+                       (lambda* (#:key inputs outputs #:allow-other-keys)
+                         (let ((out (string-append (assoc-ref outputs "out")
+                                                   "/parser/output"))
+                               (src (string-append (assoc-ref inputs "source")
+                                                   "/parser/output")))
+                           (mkdir-p out)
+                           (copy-recursively src out)))))))
+      (inputs (list glfw-3.4 pulseaudio))
+      (propagated-inputs (list glfw-3.4))
+      (description
+       "raylib is a high-level library for video game programming.  It aims to
+  abstract away platform and graphics details, allowing you to focus on
+  writing your game.  This package also provides the API in a variety of file
+formats to create bindings for many programming languages."))))
+
 (define-public bbcsdl
   (package
     (name "bbcsdl")
diff --git a/gnu/packages/guile-xyz.scm b/gnu/packages/guile-xyz.scm
index 27a30e6622..1743c64e4e 100644
--- a/gnu/packages/guile-xyz.scm
+++ b/gnu/packages/guile-xyz.scm
@@ -51,6 +51,7 @@
 ;;; Copyright © 2024 Artyom Bologov <mail@aartaka.me>
 ;;; Copyright © 2024 Felix Lechner <felix.lechner@lease-up.com>
 ;;; Copyright © 2024 Alec Barreto <mrh57@posteo.net>
+;;; Copyright © 2024 Arnaud Lechevallier <arnaud.lechevallier@free.fr>
 ;;;
 ;;; This file is part of GNU Guix.
 ;;;
@@ -84,7 +85,9 @@ (define-module (gnu packages guile-xyz)
   #:use-module (gnu packages disk)
   #:use-module (gnu packages emacs)
   #:use-module (gnu packages emacs-xyz)
+  #:use-module (gnu packages freedesktop)
   #:use-module (gnu packages gawk)
+  #:use-module (gnu packages game-development)
   #:use-module (gnu packages gettext)
   #:use-module (gnu packages gl)
   #:use-module (gnu packages glib)
@@ -3549,6 +3552,105 @@ (define-public guile-hoot
     (home-page "https://spritely.institute/hoot")
     (license (list license:asl2.0 license:lgpl3+))))
 
+(define-public guile-raylib
+  (let ((commit "e6b2ac8a21ac83c426ece993daa90ac04a90d0c2")
+        (revision "0"))
+    (package
+      (name "guile-raylib")
+      (version (git-version "0.0" revision commit))
+      (source
+       (origin
+         (method git-fetch)
+         (uri (git-reference
+               (url "https://github.com/petelliott/raylib-guile")
+               (commit commit)))
+         (file-name (git-file-name name version))
+         (sha256
+          (base32 "114v2rcwqyczqw80hzm6ij8iqfr93x43kj8qkq8gk7w49wcq8c5c"))))
+      (build-system gnu-build-system)
+      (native-inputs (list pkg-config glfw-3.4 wayland))
+      (inputs (list guile-3.0 guile-lib))
+      (propagated-inputs (list raylib-with-extras))
+      (outputs '("out" "examples"))
+      (arguments
+       `(#:make-flags '("GUILE_AUTO_COMPILE=0")
+         #:tests? #f
+         #:modules ((guix build gnu-build-system)
+                    (guix build utils)
+                    (srfi srfi-1))
+         #:imported-modules ((guix build guile-build-system)
+                             ,@%default-gnu-imported-modules)
+         #:phases (modify-phases %standard-phases
+                    (delete 'configure)
+                    (delete 'strip) ;remove useless warning with go files.
+                    (delete 'install)
+                    (add-before 'build 'retrieve-raylib-api
+                      (lambda* (#:key inputs #:allow-other-keys)
+                        (let ((raylib-version (last (string-split (assoc-ref
+                                                                   inputs
+                                                                   "raylib")
+                                                                  #\-)))
+                              (raylib-xml (string-append (assoc-ref inputs
+                                                                    "raylib")
+                                           "/parser/output/raylib_api.xml")))
+                          (copy-file raylib-xml "raylib_api.xml") #t)))
+
+                    (add-after 'unpack 'fix-makefile
+                      (lambda _
+                        (use-modules (guix build guile-build-system))
+                        (substitute* "Makefile"
+                          (("--cflags guile-3.0")
+                           (format #f "--cflags guile-~a raylib"
+                                   (target-guile-effective-version))))
+                        (substitute* "Makefile"
+                          (("-lraylib")
+                           "`pkg-config --libs raylib`"))
+                        (substitute* "Makefile"
+                          (("install: all")
+                           "do-not-install:"))))
+
+                    (add-after 'fix-makefile 'fix-guile-extensions
+                      (lambda* (#:key inputs outputs #:allow-other-keys)
+                        (use-modules (guix build guile-build-system))
+                        (substitute* (find-files "." ".*\\.scm")
+                          (("libraylib-guile")
+                           (format #f
+                            "~a/lib/guile/~a/extensions/libraylib-guile"
+                            (assoc-ref outputs "out")
+                            (target-guile-effective-version)))) #t))
+
+                    (add-after 'build 'compile-and-install-guile-raylib
+                      (lambda* (#:key inputs outputs #:allow-other-keys)
+                        (use-modules (guix build guile-build-system))
+                        (let* ((out (assoc-ref outputs "out"))
+                               (examples (assoc-ref outputs "examples"))
+                               (scm "raylib.scm")
+                               (go "raylib.go")
+                               (lib "libraylib-guile.so")
+                               (effective-version (target-guile-effective-version))
+                               (lib-out (string-append out "/lib/guile/"
+                                                       effective-version))
+                               (share-out (string-append out
+                                                         "/share/guile/site/"
+                                                         effective-version)))
+                          ;; get rid off auto-compilation warning
+                          (setenv "GUILE_AUTO_COMPILE" "0")
+                          (invoke "guild" "compile" scm "-o" go)
+                          (install-file scm
+                                        (string-append share-out))
+                          (install-file go
+                                        (string-append lib-out "/site-ccache"))
+                          (install-file lib
+                                        (string-append lib-out "/extensions"))
+                          (copy-recursively "examples" examples)
+                          #t))))))
+
+      (home-page "https://github.com/petelliott/raylib-guile")
+      (synopsis "Guile bindings for raylib library")
+      (description
+       "This package provides GNU Guile bindings to the Raylib game development library.")
+      (license license:zlib))))
+
 (define-public guile-file-names
   (package
     (name "guile-file-names")

base-commit: e243b7546d4935e12271eb14e2471a28d7608a3d
-- 
2.46.0

