aboutsummaryrefslogtreecommitdiff
path: root/Makefile
blob: fc0cdac9cfbfc7138ad34724a43043712eb6c951 (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
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
#     Compilation Options
# Debug mode [yes/no] (allowing to debug the library via gdb):
DEBUG   ?= no
# Optional modules (remove unwanted ones):
MODULES ?= cgi fastcgi loadfiles urlenc
# Specify your favourite C compiler here:
CC      ?= gcc


# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
#     Preparations
# Compile under the most strict conditions:
CFLAGS   = -xc -ansi -pedantic -Wall -Wextra -MMD
# Debug and optimisation are not compatible:
ifeq '$(DEBUG)' 'yes'
CFLAGS  += -g -O0
else
CFLAGS  += -O3
endif

# Directories definitions:
INCLUDE  = include
BUILD    = build
SRCDIR   = src
EXADIR   = examples
# Library itself:
LIB      = libmagi.a
# Modules:
EXTERNAL = cookie error file param request response session $(MODULES)
INTERNAL = cookies multipart tools urlencoded

# 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:
EXASRC   = $(wildcard $(EXADIR)/*.c)
EXAMPLES = $(foreach x,$(EXASRC:.c=),$(BUILD)/$(x))
# Dependency files:
DEPS     = $(OBJ:.o=.d) $(EXAMPLES:=.d)

# Flags collections:
SRCFLAGS = $(CFLAGS) -I$(INCLUDE)/magi
EXAFLAGS = $(CFLAGS) -I$(INCLUDE)
LFLAGS   = -static -L$(BUILD) -lmagi

# Build directories:
BUILDIRS = $(BUILD) $(BUILD)/$(SRCDIR) $(BUILD)/$(EXADIR)


# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
#     Targets
.PHONY: default examples clean clean-nontarget clean-deps clean-all

default: $(TARGET)

examples: $(EXAMPLES)

clean: clean-nontarget
	rm -f $(TARGET)
clean-nontarget: clean-deps
	rm -f $(OBJ) $(EXAMPLES)
clean-deps:
	rm -f $(DEPS)
clean-all:
	rm -rf $(BUILD)


# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
#     Compilation
# No product should be in case of cleaning:
ifneq (clean,$(MAKECMDGOAL))

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

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

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

$(BUILDIRS):
	mkdir -p $@


# Including dependency files:
-include $(DEPS)
endif