aboutsummaryrefslogtreecommitdiff
path: root/Makefile
blob: d47cade43bbf536ee1aac2af7291f66335c722f9 (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
-include .make
ifeq ($(CC),)
	CC=cc
endif
ifeq ($(TARGET_PREFIX),)
	TARGET_PREFIX=/usr/local
endif

AR=ar rcs

LIB_DIR=$(TARGET_PREFIX)/lib
LIB_SUDO:=$(shell test -w $(LIB_DIR) || echo sudo)
INC_DIR=$(TARGET_PREFIX)/include
INC_SUDO:=$(shell test -w $(INC_DIR) || echo sudo)

CFLAGS=-std=c99 -Wall -Wextra -Wpedantic -fPIC -I./include -I./compat -O2 -c
SRCS:=$(shell find src compat | grep "\.c$$")
CLEANUP=$(MAKE) clean --no-print-directory

TESTS_CFLAGS=-std=c99 -Wall -Wextra -Wpedantic -I$(INC_DIR) -I./compat -Og -ggdb -c
TESTS_LDFLAGS=-L$(LIB_DIR) -Wl,-rpath,$(LIB_DIR) -lmoslib
TESTS_SRCS:=$(shell find tests compat | grep "\.c$$")

define probe_fun
	$(shell printf '#include "compat.h"\n#include <$(1)>\nint main(void){$(2);return 0;}' \
		| $(CC) $(CFLAGS) -x c - -o /dev/null 2>/dev/null \
		|| echo -DNO_$(shell echo $(2) | tr a-z A-Z))
endef

PROBED:= \
	$(call probe_fun,string.h,memmem) \
	$(call probe_fun,string.h,strdup)

CFLAGS+=$(PROBED)
TESTS_CFLAGS+=$(PROBED)

both: CLEANUP=true
both: clean shared static
	@$(MAKE) clean --no-print-directory

shared: clean compile headers
	$(LIB_SUDO) $(CC) -shared *.o -o $(LIB_DIR)/libmoslib.so
	$(LIB_SUDO) strip -x $(LIB_DIR)/libmoslib.so
	@$(CLEANUP)

static: clean compile headers
	$(LIB_SUDO) $(AR) $(LIB_DIR)/libmoslib.a *.o
	$(LIB_SUDO) strip -S $(LIB_DIR)/libmoslib.a
	@$(CLEANUP)

headers:
	$(INC_SUDO) cp -r include/* $(INC_DIR)

compile:
	$(CC) $(CFLAGS) $(SRCS)

test: clean
	$(CC) $(TESTS_CFLAGS) $(TESTS_SRCS)
	$(CC) $(TESTS_LDFLAGS) *.o -o run_tests
	./run_tests
	@$(CLEANUP)

clean:
	rm *.o run_tests 2>/dev/null || true
	rm -rf *.dSYM/ 2>/dev/null || true