Improved build system

Author Mitja Felicijan <mitja.felicijan@gmail.com> 2026-05-16 23:23:26 +0200
Committer Mitja Felicijan <mitja.felicijan@gmail.com> 2026-05-16 23:23:26 +0200
Commit 7244cb2c40f76e18ecf9b775f15dc2176a92a6c6 (patch)
-rw-r--r-- Makefile 12
-rw-r--r-- makext.mk 46
2 files changed, 56 insertions, 2 deletions
diff --git a/Makefile b/Makefile
...
5
SRCS    = core.c json.c http.c timer.c util.c log.c assert.c request.c env.c crypto.c sqlite.c fs.c process.c template.c ini.c path.c stash.c sqlite/sqlite3.c cjson/cJSON.c
5
SRCS    = core.c json.c http.c timer.c util.c log.c assert.c request.c env.c crypto.c sqlite.c fs.c process.c template.c ini.c path.c stash.c sqlite/sqlite3.c cjson/cJSON.c
6
OBJS    = $(SRCS:.c=.o)
6
OBJS    = $(SRCS:.c=.o)
7
  
7
  
  
8
MEX_ASSURE = zig make cmake flex bison
  
9
MEX_DESCRIPTION = "Lightweight Lua runtime powered by LuaJIT and libev"
  
10
  
  
11
include makext.mk
  
12
help: .help
  
13
  
  
14
build: .assure luna # Statically compiles luna binary
  
15
  
8
luna: $(OBJS)
16
luna: $(OBJS)
9
	$(CC) $(CFLAGS) -static -o $@ $^ $(LIBS)
17
	$(CC) $(CFLAGS) -static -o $@ $^ $(LIBS)
10
  
18
  
11
%.o: %.c
19
%.o: %.c
12
	$(CC) $(CFLAGS) -c -o $@ $<
20
	$(CC) $(CFLAGS) -c -o $@ $<
13
  
21
  
14
requirements:
22
requirements: .assure # Compiles all required libraries
15
	cd luajit/src && \
23
	cd luajit/src && \
16
		make clean && \
24
		make clean && \
17
		make -j`nproc` CC="zig cc -target x86_64-linux-musl" HOST_CC="gcc" BUILDMODE=static TARGET_LDFLAGS="-lunwind"
25
		make -j`nproc` CC="zig cc -target x86_64-linux-musl" HOST_CC="gcc" BUILDMODE=static TARGET_LDFLAGS="-lunwind"
...
64
			-DBUILD_STATIC_LIBS=ON && \
72
			-DBUILD_STATIC_LIBS=ON && \
65
		make -j`nproc`
73
		make -j`nproc`
66
  
74
  
67
clean:
75
clean: # Cleans up build artefacts
68
	rm -f luna *.o
76
	rm -f luna *.o
69
  
77
  
70
.PHONY: clean requirements
78
.PHONY: clean requirements
diff --git a/makext.mk b/makext.mk
  
1
# Makext is a collection of useful extensions for Makefiles, aimed at
  
2
# simplifying and enhancing the functionality of Make-based projects. These
  
3
# extensions provide additional features and convenience functions to
  
4
# improve the build process, manage dependencies, and streamline common
  
5
# tasks.
  
6
#
  
7
# Visit the GitHub repository at https://github.com/mitjafelicijan/makext
  
8
# to learn more and contribute to the project.
  
9
#
  
10
# `makext` was written by Mitja Felicijan and is released under the BSD
  
11
# two-clause license, see the LICENSE file for more information.
  
12
  
  
13
# Load environmental files from `MEX_ENVIRONMENT`. By default GNU make
  
14
# loads what is already in `env`. This extends it to other files.
  
15
ifdef MEX_ENVIRONMENT
  
16
TEMP_ENV_FILES=$(shell echo $(MEX_ENVIRONMENT) | tr ',' ' ')
  
17
$(foreach file,$(TEMP_ENV_FILES),$(eval include $(file)))
  
18
endif
  
19
  
  
20
# Help extension that lists all the targets with descriptions
  
21
# and adds description and license information if data provided.
  
22
.PHONY: .help
  
23
.help:
  
24
ifdef MEX_DESCRIPTION
  
25
	@printf "%s\n\n" $(MEX_DESCRIPTION) | fmt
  
26
endif
  
27
	@echo "Targets:"
  
28
	@grep -vE '^[[:space:]]' $(MAKEFILE_LIST) | grep -E '^.*:.* #' | sed -E 's/(.*):(.*):.*#(.*)/  \2###\3/' | column -t -s '###'
  
29
ifdef MEX_LICENSE
  
30
	@printf "\n%s" $(MEX_LICENSE) | fmt
  
31
endif
  
32
  
  
33
# Checks `MEX_ASSURE` variable if all the programs declared actually
  
34
# exist on a machine. If not this exists make with error.
  
35
.PHONY: .assure
  
36
.assure:
  
37
ifndef MEX_ASSURE
  
38
	@printf "Variable MEX_ASSURE is not defined. Can not check for programs.\n"
  
39
else
  
40
	@for prog in $(shell echo $(MEX_ASSURE)); do \
  
41
		if ! which $$prog > /dev/null; then \
  
42
			echo "Error: '$$prog' not found on this machine."; \
  
43
			exit 1; \
  
44
		fi; \
  
45
	done
  
46
endif