aboutsummaryrefslogtreecommitdiff
path: root/Makefile
blob: 8d9041d2f5e03e27c202e91b20aadbac41a4a015 (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
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
#     Compilation Options
# Debug mode [yes/no] (allowing to debug the library via gdb):
DEBUG    = no
# Specify your favourite C compiler here:
CC       = gcc
# Specify your include directory (headers location):
INCDIR   = /usr/include
# Specify your libraries directory:
LIBDIR   = /usr/lib
# Specify location of man pages on your machine:
MANDIR   = /usr/share/man


# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
#     Preparations
# Compile as ANSI C code:
CFLAGS   = -xc -ansi -Wall
# Specify linker to use the library:
LFLAGS   = -L$(BUILD) -lmagi
# Debug and optimisation (as well as -static for valgrind) are not compatible:
ifeq '$(DEBUG)' 'yes'
CFLAGS  += -g -O0
else
CFLAGS  += -O3
LFLAGS  += -static
endif

# Directories definitions:
INCLUDE  = include
BUILD    = build
SRCDIR   = src
EXADIR   = examples
MAN      = man
# Library itself:
LIB      = libmagi.a
# Modules:
EXTERNAL = $(foreach x,$(notdir $(wildcard $(INCLUDE)/magi/*.h)),$(x:.h=))
INTERNAL = $(foreach x,$(notdir $(wildcard $(SRCDIR)/*.h)),$(x:.h=))

# Default target is library:
TARGET   = $(BUILD)/$(LIB)
# Determing needed object files:
EXTER_H  = $(foreach x,$(EXTERNAL:=.h),$(INCLUDE)/magi/$(x))
EXTER_C  = $(foreach x,$(EXTERNAL:=.c),$(SRCDIR)/$(x))
INTER_H  = $(foreach i,$(INTERNAL:=.h),$(SRCDIR)/$(i))
INTER_C  = $(INTER_H:.h=.c)
SRC      = $(INTER_C) $(EXTER_C)
OBJ      = $(foreach o,$(SRC:.c=.o),$(BUILD)/$(o))
# Example executables:
XSRC     = $(wildcard $(EXADIR)/*.c)
EXAMPLES = $(foreach x,$(XSRC:.c=),$(BUILD)/$(x))
# Dependency file:
DEPS     = deps.mk

SRCINC   = -I$(INCLUDE)/magi
XINC     = -I$(INCLUDE)
SRCBUILD = $(BUILD)/$(SRCDIR)
XBLD     = $(BUILD)/$(EXADIR)


# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
#     Targets
.PHONY: all examples clean

all: $(SRCBUILD) $(TARGET)

examples: all $(XBLD) $(EXAMPLES)

clean:
	rm -f $(TARGET) $(OBJ) $(EXAMPLES) $(DEPS)

install: all
	cp -r $(INCLUDE)/* $(INCDIR)
	cp $(TARGET) $(LIBDIR)
	cp $(MAN)/* $(MANDIR)/man3

uninstall:
	rm -r $(INCDIR)/magi.h $(INCDIR)/magi.hpp $(INCDIR)/magi
	rm $(LIBDIR)/$(LIB)
	rm $(MANDIR)/man3/magi.3 $(MANDIR)/man3/libmagi.3


# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
#     Compilation
-include $(DEPS)

# Packing object files into library:
$(TARGET): $(OBJ)
	ar -rcs $@ $^

# Compile object files from corresponding source:
$(BUILD)/%.o: %.c
	$(CC) $(CFLAGS) $(SRCINC) -c $< -o $@

# Compile executables from corresponding sources and library:
$(BUILD)/%: %.c $(TARGET)
	$(CC) $(CFLAGS) $(XINC) $< $(LFLAGS) -o $@

# Create build directories, if no such:
$(SRCBUILD) $(XBLD):
	mkdir -p $@

# Generate dependency file, adding corresponding build prefixes:
$(DEPS): $(SRC) $(EXASRC) $(EXTER_H) $(INTER_H) $(INCLUDE)/magi.h
	$(CC) $(SRCINC) $(SRC) -MM | sed '/^ /!s#^#$(SRCBUILD)/#' > $@
	$(CC) $(XINC) $(XSRC) -MM | sed '/^ /!s#^#$(XBLD)/#;s/\.o//' >> $@