summaryrefslogtreecommitdiff
path: root/Makefile
blob: c795ad45c7c0cb8964faeab8f840229d6ef166a3 (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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# Versioning
VERSION = 0.1.3
NAME = Klaatu

# Tools
CC = gcc
CPP = cpp
CFLAGS = -O2 -pipe
LDFLAGS =

# Paths
DESTDIR =
prefix = /usr
bindir = $(prefix)/bin
includedir = $(prefix)/include
libdir = $(prefix)/lib
moddir = $(libdir)/sheep-$(VERSION)

# Compilation parameters
SCFLAGS = -Wall -Wextra -Wno-unused-parameter -fPIC -Iinclude $(CFLAGS)
SLDFLAGS = -ldl $(LDFLAGS)

# Debug
ifeq ($(D),1)
SCFLAGS += -O0 -g
endif

# Build parameters
ifeq ($(V),1)
Q =
cmd = $(2)
else
Q = @
cmd = echo $(1); $(2)
endif

# User targets
all: libsheep sheep lib

install: install-libsheep install-sheep install-lib

libsheep: sheep/libsheep-$(VERSION).so

sheep: sheep/sheep

# Build targets
include sheep/Makefile
libsheep-obj := $(addprefix sheep/, $(libsheep-obj))
sheep-obj := $(addprefix sheep/, $(sheep-obj))

sheep/libsheep-$(VERSION).so: $(libsheep-obj)
	$(Q)$(call cmd, "   LD     $@",					\
		$(CC) $(SCFLAGS) -o $@ $^ $(SLDFLAGS) -shared)

install-libsheep: sheep/libsheep-$(VERSION).so
	mkdir -p $(DESTDIR)$(libdir)
	cp $^ $(DESTDIR)$(libdir)
	ln -s $^ $(DESTDIR)$(libdir)/libsheep.so
	mkdir -p $(DESTDIR)$(includedir)
	cp -r include/* $(DESTDIR)$(includedir)

sheep/sheep: sheep/libsheep-$(VERSION).so $(sheep-obj)
	$(Q)$(call cmd, "   LD     $@",					\
		$(CC) $(SCFLAGS) -Lsheep -o $@ $(sheep-obj) -lsheep-$(VERSION))

install-sheep: sheep/sheep
	mkdir -p $(DESTDIR)$(bindir)
	cp $^ $(DESTDIR)$(bindir)

$(libsheep-obj): include/sheep/config.h sheep/make.deps

include/sheep/config.h: Makefile
	$(Q)$(call cmd, "   CF     $@",					\
		rm -f $@;						\
		echo "#define SHEEP_VERSION \"$(VERSION)\"" >> $@;	\
		echo "#define SHEEP_NAME \"$(NAME)\"" >> $@;		\
		echo "#define SHEEP_MODDIR \"$(moddir)\"" >> $@)

sheep/make.deps:
	$(Q)$(call cmd, "   MK     $@",					\
		rm -f $@;						\
		$(foreach obj,$(libsheep-obj),				\
			$(CPP) -Iinclude -MM -MT $(obj)			\
				$(basename $(obj)).c >> $@; ))

include lib/Makefile
lib := $(addprefix lib/, $(lib))
lib-so := $(filter %.so, $(lib))

lib: $(lib)

install-lib: $(lib)
	mkdir -p $(DESTDIR)$(moddir)
	cp $^ $(DESTDIR)$(moddir)

$(lib-so): sheep/libsheep-$(VERSION).so
	$(Q)$(call cmd, "   LD     $@",					\
		$(CC) $(SCFLAGS) -Lsheep -o $@ $(basename $@).c		\
			-lsheep-$(VERSION) -shared			\
			$($(subst lib/,,$(basename $@))-LDFLAGS))

# Cleanup
ifneq ($(MAKECMDGOALS),clean)
-include sheep/make.deps
endif

clean := sheep/libsheep-$(VERSION).so $(libsheep-obj)
clean += sheep/sheep $(sheep-obj)
clean += include/sheep/config.h sheep/make.deps
clean += $(lib-so)

clean:
	$(Q)$(foreach subdir,$(sort $(dir $(clean))),			\
		$(call cmd, "   CL     $(subdir)",			\
			rm -f $(filter $(subdir)%,$(clean)); ))

# Build library
%.o: %.c
	$(Q)$(call cmd, "   CC     $@",					\
		$(CC) $(SCFLAGS) -o $@ -c $<)

# Misc
PHONY := all libsheep sheep lib clean
PHONY += install install-libsheep install-sheep install-lib
.PHONY: $(PHONY)