Files
esp32-lua-api/Makefile
T
evan 772618ef89 feat(json): JSON for apps through vendored lua-cjson
lua-cjson decodes straight onto the Lua stack, so a response costs its
text plus the table it becomes rather than a document in between, and it
brings the encode half that a C tokenizer would have left to write here.

It is a module rather than a global: a global namespace is a contract a
firmware implements, and nothing about this needs a provider. Registering
into package.preload also puts it ahead of the SD-card searcher, so an
implementation cannot be shadowed, and an app that never requires it
never pays for the module.

Depth is capped at 32 through the module's own knobs rather than by
patching the vendored source. Decoding recurses on the C stack and
upstream defaults to 1000, which assumes a server rather than a FreeRTOS
task.
2026-08-05 16:41:51 -04:00

71 lines
2.9 KiB
Makefile

LUA ?= lua
PYTHON ?= python3
CC ?= cc
CXX ?= c++
AR ?= ar
CONTRACTS := $(sort $(wildcard lua/api/core/*.lua lua/api/features/*.lua lua/api/features/*/*.lua \
lua/api/lib/*.lua))
LUA_C := $(sort $(wildcard native/src/vendor/lua/*.c))
LUA_OBJECTS := $(patsubst native/src/vendor/lua/%.c,_build/lua/%.o,$(LUA_C))
LUA_LIBRARY := _build/liblua.a
CJSON_C := $(sort $(wildcard native/src/vendor/cjson/*.c))
CJSON_OBJECTS := $(patsubst native/src/vendor/cjson/%.c,_build/cjson/%.o,$(CJSON_C))
LUA_SMOKE := _build/lua-smoke
RUNTIME_TEST := _build/runtime-test
LUAC32 := _build/luac32
RUNTIME_CPP := $(sort $(wildcard native/src/runtime/*.cpp native/src/bindings/core/*.cpp \
native/src/bindings/lib/*.cpp \
native/src/bindings/features/*.cpp native/src/bindings/features/*/*.cpp) native/src/embedded_modules.cpp)
.PHONY: api test embed compiledb format
# Vendored Lua and the generated module blob are skipped via .clang-format-ignore.
format:
@clang-format -i $(shell find native -name '*.cpp' -o -name '*.h')
embed: $(LUAC32)
@$(PYTHON) tools/embed.py $(LUAC32) lua/lib native/src/embedded_modules.cpp
api:
@$(PYTHON) tools/gen_api.py
compiledb:
@bear -- $(MAKE) -B $(RUNTIME_TEST) $(LUA_SMOKE) $(LUAC32)
test: $(LUA_SMOKE) $(RUNTIME_TEST) $(LUAC32)
@printf '%-32s ' tools/test_gen_api.py; $(PYTHON) tools/test_gen_api.py
@$(PYTHON) tools/gen_api.py --check
@printf '%-32s ' generated-api; echo ok
@$(PYTHON) tools/embed.py $(LUAC32) lua/lib native/src/embedded_modules.cpp --check
@printf '%-32s ' embedded-modules; echo ok
@$(LUA) -e 'assert(_VERSION == "Lua 5.4", "tests require Lua 5.4")'
@for file in $(CONTRACTS); do $(LUA) -e "assert(loadfile('$$file'))" || exit 1; done
@printf '%-32s ' contracts; echo ok
@printf '%-32s ' lua/test/ui.lua; $(LUA) lua/test/ui.lua
@printf '%-32s ' lua/test/hints.lua; $(LUA) lua/test/hints.lua
@printf '%-32s ' native/test/lua_smoke.c; $(LUA_SMOKE) && echo ok
@printf '%-32s ' native/test/runtime_test.cpp; $(RUNTIME_TEST) && echo ok
$(LUA_SMOKE): native/test/lua_smoke.c $(LUA_LIBRARY)
@$(CC) -std=c99 -DLUA_32BITS -I native/src/vendor/lua $< $(LUA_LIBRARY) -lm -ldl -o $@
$(RUNTIME_TEST): $(RUNTIME_CPP) native/test/runtime_test.cpp $(LUA_LIBRARY) $(CJSON_OBJECTS)
@$(CXX) -std=c++11 -Wall -Wextra -DLUA_32BITS -I native/include -I native/src/vendor/lua \
$(RUNTIME_CPP) native/test/runtime_test.cpp $(CJSON_OBJECTS) $(LUA_LIBRARY) -lm -ldl -o $@
$(LUA_LIBRARY): $(LUA_OBJECTS)
@$(AR) rcs $@ $^
$(LUAC32): tools/dump.c $(LUA_LIBRARY)
@$(CC) -std=c99 -DLUA_32BITS -I native/src/vendor/lua $< $(LUA_LIBRARY) -lm -o $@
_build/lua/%.o: native/src/vendor/lua/%.c
@mkdir -p $(@D)
@$(CC) -std=c99 -DLUA_32BITS -I native/src/vendor/lua -c $< -o $@
# gnu99 rather than c99: lua-cjson calls strncasecmp, which is POSIX rather than ISO C.
_build/cjson/%.o: native/src/vendor/cjson/%.c
@mkdir -p $(@D)
@$(CC) -std=gnu99 -DLUA_32BITS -I native/src/vendor/lua -I native/src/vendor/cjson -c $< -o $@