summaryrefslogtreecommitdiff
path: root/Makefile
blob: b72df8eee0a8381e8f50d0cfa624bd2d2d882937 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
all: test

LMODULES := supple supple.request supple.objects supple.sandbox \
	    supple.host supple.comms supple.track
CMODULES := supple.capi
MODULES := $(LMODULES) $(CMODULES)
LUA_VER := 5.1

TEST_MODULES := $(MODULES)

PREFIX ?= /usr/local

INST_BASE := $(PREFIX)
WRAPPER_PATH := $(INST_BASE)/lib/supple-sandbox$(LUA_VER)

LINST_ROOT := $(DESTDIR)$(INST_BASE)/share/lua/$(LUA_VER)
CINST_ROOT := $(DESTDIR)$(INST_BASE)/lib/lua/$(LUA_VER)
BINST_ROOT := $(DESTDIR)$(INST_BASE)/lib
LMOD_FILES := $(patsubst %,%.lua,$(subst .,/,$(LMODULES)))
CMOD_FILES := $(patsubst %,%.so,$(subst .,/,$(CMODULES)))

CMOD_TARGETS := $(patsubst %,lib/%.so,$(subst .,/,$(CMODULES)))
CMOD_OBJECTS := $(patsubst %,lib/%.o,$(subst .,/,$(CMODULES)))

.PRECIOUS: $(CMOD_OBJECTS)

LUA_INTERP_NAME := lua$(LUA_VER)
LUA_INTERP_PATH := $(shell which lua$(LUA_VER))

INCS := -I/usr/include/lua$(LUA_VER)
OPT := -O0 -g
WARN := -Wall -Werror
DEFS := -D'LUA_INTERP_NAME="$(LUA_INTERP_NAME)"' \
        -D'LUA_INTERP_PATH="$(LUA_INTERP_PATH)"'
CFLAGS := $(INCS) $(OPT) $(WARN) $(DEFS) $(CFLAGS)
LFLAGS := -O1 -g $(LFLAGS)

%.so: %.o
	$(CC) $(LFLAGS) -shared -o $@ $^ -llua$(LUA_VER)

%.o: %.c
	$(CC) $(CFLAGS) -fPIC -o $@ -c $<

build: $(CMOD_TARGETS) wrapper

wrapper: src/wrapper.c
	$(CC) $(LFLAGS) $(CFLAGS) -o $@ $< -llua$(LUA_VER)

testwrapper: src/wrapper.c
	$(CC) $(LFLAGS) $(CFLAGS) -DTESTING_SUPPLE -o $@ $< -llua$(LUA_VER)
	-chown root:root $@ && chmod u+s $@
	ls -l $@

COPY_LMOD := sed -e's!@@WRAPPER_BIN@@!$(WRAPPER_PATH)!g;/START_TEST_SUPPLE/,/END_TEST_SUPPLE/d'

install: build
	mkdir -p $(LINST_ROOT)/supple
	mkdir -p $(CINST_ROOT)/supple
	mkdir -p $(BINST_ROOT)
	cp wrapper $(DESTDIR)$(WRAPPER_PATH)
	chown root:root $(DESTDIR)$(WRAPPER_PATH)
	chmod u+s $(DESTDIR)$(WRAPPER_PATH)
	for MOD in $(sort $(LMOD_FILES)); do \
		$(COPY_LMOD) lib/$${MOD} > $(LINST_ROOT)/$${MOD}; \
	done
	for MOD in $(sort $(CMOD_FILES)); do \
		cp lib/$${MOD} $(CINST_ROOT)/$${MOD}; \
	done

ifeq ($(DEBUG),gdb)
GDB := gdb --args
endif

LUA := LUA_PATH="$(shell pwd)/lib/?.lua;$(shell pwd)/extras/luacov/src/?.lua;;" LUA_CPATH="$(shell pwd)/lib/?.so;;" $(GDB) $(LUA_INTERP_PATH)

clean:
	$(RM) luacov.report.out luacov.stats.out
	$(RM) $(CMOD_TARGETS) $(CMOD_OBJECTS)
	$(RM) wrapper testwrapper

distclean: clean
	find . -name "*~" -delete

.PHONY: example
example:
	$(LUA) example/supple-example.lua

.PHONY: test
test: build testwrapper
	@$(RM) luacov.stats.out
	@ERR=0; \
	for MOD in $(sort $(TEST_MODULES)); do \
		echo -n "$${MOD}: "; \
		$(LUA) test/test-$${MOD}.lua; \
		test "x$$?" = "x0" || ERR=1; \
	done; \
	$(LUA) extras/luacov/src/bin/luacov -X luacov. -X test. $(TEST_MODULES); \
	exit $$ERR

.PHONY: interactive
interactive: build
	$(LUA) -e'lace=require"supple"' -i