From 4818f59fe027527e12615f2daeccc23dd1ca5563 Mon Sep 17 00:00:00 2001
From: bitstr0m <bitstr0m@proton.me>
Date: Thu, 10 Oct 2024 13:29:05 +0100
Subject: [PATCH] Fix luajit

Compiles and luajit executable passes tests found at
https://github.com/LuaJIT/LuaJIT-test-cleanup (excluding some that
appear to be related to the static build).

Compiled without W^X memory permission support because there appears to
be some issue with the mprotect() call.
---
 recipes/wip/dev/lang/luajit/recipe.toml | 24 +++++++++++++++++--
 recipes/wip/dev/lang/luajit/redox.patch | 31 +++++++++++++++++++++++++
 2 files changed, 53 insertions(+), 2 deletions(-)
 create mode 100644 recipes/wip/dev/lang/luajit/redox.patch

diff --git a/recipes/wip/dev/lang/luajit/recipe.toml b/recipes/wip/dev/lang/luajit/recipe.toml
index 237fe87bb..c2359afa7 100644
--- a/recipes/wip/dev/lang/luajit/recipe.toml
+++ b/recipes/wip/dev/lang/luajit/recipe.toml
@@ -1,5 +1,25 @@
-#TODO missing script for "make", see https://luajit.org/install.html
+#TODO remove -DLUAJIT_SECURITY_MCODE=0
 [source]
-tar = "https://luajit.org/download/LuaJIT-2.1.0-beta3.tar.gz"
+# LuaJIT is only available as a rolling release
+git = "https://luajit.org/git/luajit.git"
+rev = "2090842410e0ba6f81fad310a77bf5432488249a"
+
 [build]
 template = "custom"
+script = """
+rsync -av --delete "${COOKBOOK_SOURCE}/" ./
+
+# Cookbook doesn't apply patches to git repositiories (so we do it manually)
+# The patch is applied so that LUAJIT_OS is set to LUAJIT_OS_POSIX without
+# redefinition warnings
+git apply "${COOKBOOK_RECIPE}/redox.patch"
+
+${COOKBOOK_MAKE} -j ${COOKBOOK_MAKE_JOBS} install \
+  PREFIX="${COOKBOOK_STAGE}" \
+  BUILDMODE='static' \
+  TARGET_SYS='Redox' \
+  XCFLAGS='-DLUAJIT_SECURITY_MCODE=0' \
+  CROSS="${TARGET}-"
+
+cd "${COOKBOOK_STAGE}"/bin && ln -s luajit-2.1.* luajit
+"""
diff --git a/recipes/wip/dev/lang/luajit/redox.patch b/recipes/wip/dev/lang/luajit/redox.patch
new file mode 100644
index 000000000..aef67b4c1
--- /dev/null
+++ b/recipes/wip/dev/lang/luajit/redox.patch
@@ -0,0 +1,31 @@
+diff --git a/src/Makefile b/src/Makefile
+index 3a6a4329..450e8fe6 100644
+--- a/src/Makefile
++++ b/src/Makefile
+@@ -351,6 +351,9 @@ else
+   ifeq (GNU/kFreeBSD,$(TARGET_SYS))
+     TARGET_XLIBS+= -ldl
+   endif
++  ifeq (Redox,$(TARGET_SYS))
++    TARGET_XCFLAGS+= -DLUAJIT_OS=LUAJIT_OS_POSIX
++  endif
+ endif
+ endif
+ endif
+@@ -367,12 +370,16 @@ ifneq ($(HOST_SYS),$(TARGET_SYS))
+   else
+   ifeq (iOS,$(TARGET_SYS))
+     HOST_XCFLAGS+= -DLUAJIT_OS=LUAJIT_OS_OSX -DTARGET_OS_IPHONE=1
++  else
++  ifeq (Redox,$(TARGET_SYS))
++    HOST_XCFLAGS+= -DLUAJIT_OS=LUAJIT_OS_POSIX
+   else
+     HOST_XCFLAGS+= -DLUAJIT_OS=LUAJIT_OS_OTHER
+   endif
+   endif
+   endif
+   endif
++  endif
+ endif
+ 
+ ifneq (,$(CCDEBUG))
-- 
GitLab