From 3bfd7e0920d4bfe7d893d5999aace29510700b06 Mon Sep 17 00:00:00 2001 From: pedro-bento Date: Wed, 21 Oct 2026 18:37:55 +0100 Subject: [PATCH] Embedded luajit in hate-no-d --- .gitignore | 2 + .gitmodules | 3 + Makefile | 200 ++++++++++++++++++++++++++++++++++++++++++++++ README.md | 9 +++ compile_flags.txt | 1 + hate-no-d.c | 77 ++++++++++++++++++ 6 files changed, 292 insertions(+) create mode 100644 .gitignore create mode 100644 .gitmodules create mode 100644 Makefile create mode 100644 compile_flags.txt create mode 100644 hate-no-d.c diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ccaa541 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +hate-no-d +quick-start.lua diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..e7491eb --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "submodules/luajit"] + path = submodules/luajit + url = https://luajit.org/git/luajit.git diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..a4ffe4b --- /dev/null +++ b/Makefile @@ -0,0 +1,200 @@ +# hate-no-d +# +# Native platforms are auto-detected from uname. Cross targets are selected explicitly: +# make +# make PLATFORM=Android ANDROID_NDK=/opt/android-ndk +# make PLATFORM=Android ANDROID_NDK=... ANDROID_ABI=armeabi-v7a ANDROID_API=21 +# make PLATFORM=iOS IOS_ARCH=arm64 IOS_MIN=12.0 + +# luajit requires GNU Make (use `gmake` on FreeBSD/OpenBSD/NetBSD). +ifndef .FEATURES + $(error This build requires GNU Make -- on BSD use 'gmake') +endif + +.DEFAULT_GOAL := all + +LUAJIT_DIR := submodules/luajit +LUAJIT_SRC := $(LUAJIT_DIR)/src +LUAJIT_AR := $(LUAJIT_SRC)/libluajit.a +BIN := hate-no-d + +LUAJIT_XCFLAGS := -DLUAJIT_ENABLE_LUA52COMPAT +LUAJIT_EXTRA := + +# Platform detection. PLATFORM= always wins over uname. +PLATFORM ?= $(shell uname -s) + +ifeq ($(PLATFORM),Linux) + OS_KIND := linux +else ifeq ($(PLATFORM),Android) + OS_KIND := android +else ifeq ($(PLATFORM),Darwin) + OS_KIND := macos +else ifeq ($(PLATFORM),iOS) + OS_KIND := ios +else ifneq (,$(filter $(PLATFORM),FreeBSD OpenBSD NetBSD DragonFly GNU/kFreeBSD)) + OS_KIND := bsd +else + OS_KIND := posix +endif + +# Linux (ELF) +ifeq ($(OS_KIND),linux) + EXPORT_LDFLAGS := -Wl,-E + PLAT_LIBS := -lm -ldl + LINK_TARGET := $(BIN) +endif + +# BSD (ELF or Mach-O, dlopen lives in libc) +ifeq ($(OS_KIND),bsd) + EXPORT_LDFLAGS := -Wl,-E + PLAT_LIBS := -lm + LINK_TARGET := $(BIN) +endif + +# Generic POSIX fallback (Solaris/illumos, AIX, Haiku, ...). +# Note: Solaris ld's -E is NOT --export-dynamic, so it is deliberately off. +ifeq ($(OS_KIND),posix) + EXPORT_LDFLAGS := + PLAT_LIBS := -lm + LINK_TARGET := $(BIN) +endif + +# macOS (Mach-O) +# +# ld64 exports main-executable globals by default; -export_dynamic only stops +# -dead_strip / LTO from discarding the Lua API. GC64 is mandatory on arm64. +# JIT under the hardened runtime needs MAP_JIT, which is compiled in only when +# the deployment target is >= 11.0. +ifeq ($(OS_KIND),macos) + ifeq ($(origin CC),default) + CC := clang + endif + MACOSX_DEPLOYMENT_TARGET ?= 11.0 + export MACOSX_DEPLOYMENT_TARGET + + EXPORT_LDFLAGS := -Wl,-export_dynamic + PLAT_LIBS := -lm + LINK_TARGET := $(BIN) + + # MAP_JIT path: needed for the hardened runtime (and therefore notarization). + # Requires MACOSX_DEPLOYMENT_TARGET >= 11.0 or LuaJIT compiles it back out. + MACOSX_HRT ?= 1 + ifeq ($(MACOSX_HRT),1) + LUAJIT_XCFLAGS += -DLUAJIT_ENABLE_OSX_HRT + endif +endif + +# Android (ELF, cross-compiled with the NDK) +# +# 64-bit ABIs build on any 64-bit host. armeabi-v7a additionally needs a +# 32-bit host compiler: make ... HOST_CC="gcc -m32" +ifeq ($(OS_KIND),android) + ANDROID_NDK ?= $(ANDROID_NDK_HOME) + ANDROID_API ?= 21 + ANDROID_ABI ?= arm64-v8a + + ifeq ($(strip $(ANDROID_NDK)),) + $(error PLATFORM=Android requires ANDROID_NDK (or ANDROID_NDK_HOME) to be set) + endif + + ifeq ($(ANDROID_ABI),arm64-v8a) + ANDROID_TRIPLET := aarch64-linux-android + else ifeq ($(ANDROID_ABI),armeabi-v7a) + ANDROID_TRIPLET := armv7a-linux-androideabi + else ifeq ($(ANDROID_ABI),x86_64) + ANDROID_TRIPLET := x86_64-linux-android + else ifeq ($(ANDROID_ABI),x86) + ANDROID_TRIPLET := i686-linux-android + else + $(error ANDROID_ABI must be one of: arm64-v8a, armeabi-v7a, x86_64, x86) + endif + + ifeq ($(shell uname -s),Darwin) + ANDROID_HOST_TAG ?= darwin-x86_64 + else + ANDROID_HOST_TAG ?= linux-x86_64 + endif + + ANDROID_BIN := $(ANDROID_NDK)/toolchains/llvm/prebuilt/$(ANDROID_HOST_TAG)/bin + ANDROID_CC := $(ANDROID_BIN)/$(ANDROID_TRIPLET)$(ANDROID_API)-clang + + CC := $(ANDROID_CC) + EXPORT_LDFLAGS := -Wl,-E + PLAT_LIBS := -lm + LINK_TARGET := $(BIN) + + LUAJIT_EXTRA := CROSS=$(ANDROID_BIN)/$(ANDROID_TRIPLET)- STATIC_CC=$(ANDROID_CC) TARGET_LD=$(ANDROID_CC) "TARGET_AR=$(ANDROID_BIN)/llvm-ar rcus" TARGET_STRIP=$(ANDROID_BIN)/llvm-strip +endif + +# iOS (Mach-O, ARM64 only) +# +# There is no runnable binary: link libluajit.a into your Xcode app target. +# The JIT is compiled out by LuaJIT itself (no runtime code generation is +# allowed on iOS), so you get the fast interpreter only. +ifeq ($(OS_KIND),ios) + IOS_SDK ?= $(shell xcrun --sdk iphoneos --show-sdk-path 2>/dev/null) + IOS_CC ?= $(shell xcrun --sdk iphoneos --find clang 2>/dev/null) + IOS_ARCH ?= arm64 + IOS_MIN ?= 12.0 + + ifeq ($(strip $(IOS_SDK)),) + $(error PLATFORM=iOS requires Xcode/xcrun on the build host) + endif + + IOS_FLAGS := -arch $(IOS_ARCH) -isysroot $(IOS_SDK) -mios-version-min=$(IOS_MIN) + + CC := $(IOS_CC) + EXPORT_LDFLAGS := + PLAT_LIBS := + LINK_TARGET := $(LUAJIT_AR) + + # DEFAULT_CC keeps the host toolchain (minilua/buildvm) native; CROSS + # switches only the target objects to the iOS SDK. + LUAJIT_EXTRA := DEFAULT_CC=clang CROSS=$(dir $(IOS_CC)) TARGET_SYS=iOS "TARGET_FLAGS=$(IOS_FLAGS)" +endif + +ifndef LINK_TARGET + $(error Unsupported platform '$(PLATFORM)') +endif + +# Compiler / linker flags +CFLAGS ?= -O2 -Wall +CPPFLAGS += -I$(LUAJIT_SRC) + +# LuaJIT: static archive only, built through the upstream GNU Makefile. +LUAJIT_MAKE_ARGS := BUILDMODE=static XCFLAGS="$(LUAJIT_XCFLAGS)" $(LUAJIT_EXTRA) + +$(LUAJIT_AR): + $(MAKE) -C $(LUAJIT_SRC) $(LUAJIT_MAKE_ARGS) "LJCORE_O=ljamalg.o" libluajit.a + +# Targets +all: $(LINK_TARGET) + +$(BIN): hate-no-d.c $(LUAJIT_AR) + $(CC) $(CFLAGS) $(CPPFLAGS) -o $@ hate-no-d.c \ + $(LUAJIT_AR) $(EXPORT_LDFLAGS) $(LDFLAGS) $(PLAT_LIBS) + +luajit: $(LUAJIT_AR) + +# Show the resolved configuration for the current (or selected) platform. +# Useful for checking a cross build before committing to it. +print-config: + @echo "PLATFORM = $(PLATFORM)" + @echo "OS_KIND = $(OS_KIND)" + @echo "CC = $(CC)" + @echo "CFLAGS = $(CFLAGS)" + @echo "CPPFLAGS = $(CPPFLAGS)" + @echo "EXPORT_LDFLAGS = $(EXPORT_LDFLAGS)" + @echo "PLAT_LIBS = $(PLAT_LIBS)" + @echo "LINK_TARGET = $(LINK_TARGET)" + @echo "LUAJIT_MAKE_ARGS = $(LUAJIT_MAKE_ARGS)" + @echo "MACOSX_DEPLOYMENT_TARGET = $(MACOSX_DEPLOYMENT_TARGET) (HRT=$(MACOSX_HRT))" + @echo "ANDROID_ABI / API = $(ANDROID_ABI) / $(ANDROID_API)" + @echo "IOS_ARCH / MIN = $(IOS_ARCH) / $(IOS_MIN)" + +clean: + $(MAKE) -C $(LUAJIT_DIR) clean + rm -f $(BIN) + +.PHONY: all clean luajit print-config diff --git a/README.md b/README.md index 5ee7636..804d65d 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,11 @@ # hate-no-d +A [Love2D](https://www.love2d.org/) antagonist. + +## Quick start + +```sh +make +echo -e 'print("Hey! I am hate-no-d!")' > quick-start.lua +./hate-no-d quick-start.lua +``` diff --git a/compile_flags.txt b/compile_flags.txt new file mode 100644 index 0000000..cb6453e --- /dev/null +++ b/compile_flags.txt @@ -0,0 +1 @@ +-I./submodules/luajit/src diff --git a/hate-no-d.c b/hate-no-d.c new file mode 100644 index 0000000..4b1f18b --- /dev/null +++ b/hate-no-d.c @@ -0,0 +1,77 @@ +#include +#include + +#include +#include +#include + +static int traceback(lua_State *L) { + if (!lua_isstring(L, 1)) { + if (lua_isnoneornil(L, 1) || !luaL_callmeta(L, 1, "__tostring") || !lua_isstring(L, -1)) { + return 1; + } + + lua_remove(L, 1); + } + + luaL_traceback(L, L, lua_tostring(L, 1), 1); + + return 1; +} + +static void report(lua_State *L) { + const char *msg = lua_tostring(L, -1); + if (!msg) { + msg = "(error object is not a string)"; + } + + fprintf(stderr, "[ERROR] hate-no-d: %s\n", msg); + fflush(stderr); + lua_pop(L, 1); +} + +int main(int argc, char *argv[]) { + if (argc < 2) { + fprintf(stderr, "Usage: %s [args...]\n", argv[0]); + return EXIT_FAILURE; + } + + const char *file_path = argv[1]; + + lua_State *L = luaL_newstate(); + if (!L) { + fprintf(stderr, "[ERROR] hate-no-d: cannot create Lua state\n"); + return EXIT_FAILURE; + } + + luaL_openlibs(L); + + lua_createtable(L, argc - 1, 1); + for (int i = 0; i < argc; i++) { + lua_pushstring(L, argv[i]); + lua_rawseti(L, -2, i - 1); + } + lua_setglobal(L, "arg"); + + int status = luaL_loadfile(L, file_path); + if (status != LUA_OK) { + report(L); + lua_close(L); + return EXIT_FAILURE; + } + + lua_pushcfunction(L, traceback); + lua_insert(L, -2); + + status = lua_pcall(L, 0, 0, -2); + if (status != LUA_OK) { + report(L); + lua_close(L); + return EXIT_FAILURE; + } + + lua_pop(L, 1); + lua_close(L); + + return EXIT_SUCCESS; +}