From 68f0a2ac37d58f5e28dfb8dc95b27d4ba3865967 Mon Sep 17 00:00:00 2001 From: Yuansheng Wang Date: Sat, 16 May 2026 18:23:31 +0800 Subject: [PATCH] build: add lua-cjson submodule and fix make bench for macOS - Add vendor/lua-cjson as a git submodule (openresty/lua-cjson) - Switch LUA_ENV to use DYLD_LIBRARY_PATH on Darwin (LD_LIBRARY_PATH is ignored by macOS SIP and dyld) - Prepend vendor/lua-cjson/?.so to LUA_CPATH so require("cjson") resolves - Add LUAJIT_PREFIX / LUAJIT_INC derivation from the luajit binary path - Add vendor/lua-cjson/cjson.so target; builds with -bundle -undefined dynamic_lookup on macOS (required by dyld for extension modules) - Make bench depend on vendor/lua-cjson/cjson.so so it auto-builds cjson --- .gitmodules | 3 +++ Makefile | 18 ++++++++++++++++-- vendor/lua-cjson | 1 + 3 files changed, 20 insertions(+), 2 deletions(-) create mode 100644 .gitmodules create mode 160000 vendor/lua-cjson diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..2d1c2aa --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "vendor/lua-cjson"] + path = vendor/lua-cjson + url = https://github.com/openresty/lua-cjson.git diff --git a/Makefile b/Makefile index fe5609d..dcf93ee 100644 --- a/Makefile +++ b/Makefile @@ -1,9 +1,16 @@ # Overridable: `make bench LUAJIT=/path/to/luajit LUA_CPATH='...'` LUAJIT ?= $(shell command -v luajit 2>/dev/null || echo /usr/local/openresty/luajit/bin/luajit) -LUA_CPATH ?= ./?.so;/usr/local/openresty/lualib/?.so;/usr/local/lib/lua/5.1/?.so;/usr/local/openresty/luajit/lib/lua/5.1/?.so +LUA_CPATH ?= ./vendor/lua-cjson/?.so;./?.so;/usr/local/openresty/lualib/?.so;/usr/local/lib/lua/5.1/?.so;/usr/local/openresty/luajit/lib/lua/5.1/?.so + +LUAJIT_PREFIX ?= $(shell dirname $$(dirname $$(command -v $(LUAJIT) 2>/dev/null || echo /usr/local/openresty/luajit/bin/luajit))) +LUAJIT_INC ?= $(LUAJIT_PREFIX)/include/luajit-2.1 LIB_DIR := $(CURDIR)/target/release +ifeq ($(shell uname),Darwin) +LUA_ENV := DYLD_LIBRARY_PATH=$(LIB_DIR) LUA_CPATH='$(LUA_CPATH)' +else LUA_ENV := LD_LIBRARY_PATH=$(LIB_DIR) LUA_CPATH='$(LUA_CPATH)' +endif .PHONY: help build test lint bench clean @@ -22,8 +29,15 @@ test: build ## Run cargo tests + busted Lua tests lint: ## Run clippy with -D warnings cargo clippy --release --all-targets -- -D warnings -bench: build ## Run the LuaJIT vs cjson benchmark +bench: build vendor/lua-cjson/cjson.so ## Run the LuaJIT vs cjson benchmark $(LUA_ENV) $(LUAJIT) benches/lua_bench.lua +vendor/lua-cjson/cjson.so: | vendor/lua-cjson/Makefile +ifeq ($(shell uname),Darwin) + $(MAKE) -C vendor/lua-cjson PREFIX=$(LUAJIT_PREFIX) LUA_INCLUDE_DIR=$(LUAJIT_INC) LUA=$(LUAJIT) CJSON_LDFLAGS="-bundle -undefined dynamic_lookup" +else + $(MAKE) -C vendor/lua-cjson PREFIX=$(LUAJIT_PREFIX) LUA_INCLUDE_DIR=$(LUAJIT_INC) LUA=$(LUAJIT) +endif + clean: ## Remove build artifacts cargo clean diff --git a/vendor/lua-cjson b/vendor/lua-cjson new file mode 160000 index 0000000..617164a --- /dev/null +++ b/vendor/lua-cjson @@ -0,0 +1 @@ +Subproject commit 617164a0fe3bb647128178f9caefb304b8573bb6