hate-no-d/Makefile
pedro-bento 8a68a2c2ca
All checks were successful
release / linux/aarch64 (push) Successful in 50s
release / linux/armv7 (push) Successful in 1m24s
release / linux/i686 (push) Successful in 1m7s
release / linux/x86_64 (push) Successful in 39s
release / android (push) Successful in 1m38s
release / freebsd/x86_64 (push) Successful in 3m5s
release / windows/i686 (push) Successful in 59s
release / windows/x86_64 (push) Successful in 1m1s
Added release workflow
Co-authored-by: pedro-bento <mail@pbento.pt>
Co-committed-by: pedro-bento <mail@pbento.pt>
2026-10-21 22:09:00 +01:00

233 lines
7.3 KiB
Makefile

# 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
# make PLATFORM=Windows WIN_ARCH=x86_64 (cross build with mingw-w64)
# 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 ifeq ($(PLATFORM),Windows)
OS_KIND := windows
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
# Windows (PE, cross-compiled from a POSIX host with mingw-w64)
#
# Native builds use build.bat.
#
# 64-bit targets build on any 64-bit host. i686 also needs a 32-bit host
# compiler, because LuaJIT's buildvm runs natively and its pointer size must
# match the target:
# make PLATFORM=Windows WIN_ARCH=i686 HOST_CC="gcc -m32"
#
# No export table: ffi.C resolves through msvcrt/kernel32 handles on Windows,
# unlike ELF where the Lua API has to be exported with -Wl,-E.
ifeq ($(OS_KIND),windows)
WIN_ARCH ?= x86_64
ifeq ($(filter $(WIN_ARCH),x86_64 i686),)
$(error WIN_ARCH must be x86_64 or i686)
endif
CC := $(WIN_ARCH)-w64-mingw32-gcc
HOST_CC ?= gcc
PLAT_LIBS := -lm
BIN := $(BIN).exe
LINK_TARGET := $(BIN)
# LuaJIT derives HOST_CC from CC, so label both to the sub-make: otherwise
# the host tools (minilua, buildvm) are compiled with the cross compiler.
LUAJIT_EXTRA := "CC=$(CC)" "HOST_CC=$(HOST_CC)" TARGET_SYS=Windows
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)"
@echo "WIN_ARCH = $(WIN_ARCH) (HOST_CC=$(HOST_CC))"
clean:
$(MAKE) -C $(LUAJIT_DIR) clean
rm -f $(BIN)
.PHONY: all clean luajit print-config