~rcr/rirc

ref: 281a93e4c12c84dc6f263e49e79fd3e61c3ce874 rirc/Makefile -rw-r--r-- 3.1 KiB
281a93e4 — Richard Robbins remove submodules, refactor Makefile 10 months ago
                                                                                
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
VERSION := 0.1.4

# Release and debug executable names
BIN_R := rirc
BIN_D := rirc.debug

# Install paths
BIN_DIR := /usr/local/bin
MAN_DIR := /usr/local/share/man/man1

# Build, source, lib, and test directories
DIR_B := bld
DIR_S := src
DIR_L := lib
DIR_T := test

include lib/mbedtls.Makefile

CFLAGS ?= -O2 -flto
CFLAGS += -D NDEBUG

CFLAGS_DEBUG ?=
CFLAGS_DEBUG += -O0 -g3

CFLAGS_LOCAL := -std=c11 -Wall -Wextra -Werror -D VERSION=\"$(VERSION)\"
CFLAGS_LOCAL += -I . -I $(MBEDTLS_SRC)/include/
CFLAGS_LOCAL += -D MBEDTLS_CONFIG_FILE='<$(MBEDTLS_CFG)>'
CFLAGS_LOCAL += -D _POSIX_C_SOURCE=200809L
CFLAGS_LOCAL += -D _DARWIN_C_SOURCE

LDFLAGS ?=
LDFLAGS += -lpthread

SRC     := $(shell find $(DIR_S) -name '*.c')
SUBDIRS += $(shell find $(DIR_S) -name '*.c' -exec dirname {} \; | sort -u)

SRC_G   := $(shell find $(DIR_S) -name '*.gperf')
SUBDIRS += $(shell find $(DIR_S) -name '*.gperf' -exec dirname {} \; | sort -u)

# Release, debug, and testcase build objects
OBJS_D := $(patsubst $(DIR_S)/%.c, $(DIR_B)/%.db.o, $(SRC))
OBJS_R := $(patsubst $(DIR_S)/%.c, $(DIR_B)/%.o,    $(SRC))
OBJS_T := $(patsubst $(DIR_S)/%.c, $(DIR_B)/%.t,    $(SRC))
OBJS_T += $(DIR_B)/utils/tree.t # Header only file

# Gperf generated source files
OBJS_G := $(patsubst %.gperf, %.gperf.out, $(SRC_G))

# Release build executable
$(BIN_R): $(MBEDTLS_LIBS) $(OBJS_G) $(OBJS_R)
	@echo "  CC    $@"
	@$(CC) $(LDFLAGS) -o $@ $(OBJS_R) $(MBEDTLS_LIBS)

# Debug build executable
$(BIN_D): $(MBEDTLS_LIBS) $(OBJS_G) $(OBJS_D)
	@echo "  CC    $@"
	@$(CC) $(LDFLAGS) -o $@ $(OBJS_D) $(MBEDTLS_LIBS)

# Release build objects
$(DIR_B)/%.o: $(DIR_S)/%.c | $(DIR_B)
	@echo "  CC    $<"
	@$(CPP) $(CFLAGS) $(CFLAGS_LOCAL) -MM -MP -MT $@ -MF $(@:.o=.o.d) $<
	@$(CC)  $(CFLAGS) $(CFLAGS_LOCAL) -c -o $@ $<

# Debug build objects
$(DIR_B)/%.db.o: $(DIR_S)/%.c | $(DIR_B)
	@echo "  CC    $<"
	@$(CPP) $(CFLAGS_DEBUG) $(CFLAGS_LOCAL) -MM -MP -MT $@ -MF $(@:.o=.o.d) $<
	@$(CC)  $(CFLAGS_DEBUG) $(CFLAGS_LOCAL) -c -o $@ $<

# Testcases
$(DIR_B)/%.t: $(DIR_T)/%.c $(OBJS_G) | $(DIR_B)
	@$(CPP) $(CFLAGS_DEBUG) $(CFLAGS_LOCAL) -MM -MP -MT $@ -MF $(@:.t=.t.d) $<
	@$(CC)  $(CFLAGS_DEBUG) $(CFLAGS_LOCAL) -c -o $(@:.t=.t.o) $<
	@$(CC)  $(CFLAGS_DEBUG) $(CFLAGS_LOCAL) -o $@ $(@:.t=.t.o)
	@./$@ || mv $@ $(@:.t=.td)
	@[ -f $@ ]

# Build directories
$(DIR_B):
	@for dir in $(patsubst $(DIR_S)/%, $(DIR_B)/%, $(SUBDIRS)); do mkdir -p $$dir; done

config.h:
	cp config.def.h config.h

%.gperf.out: %.gperf
	gperf --output-file=$@ $<

all:
	@$(MAKE) --silent $(BIN_R)
	@$(MAKE) --silent $(BIN_D)

check:
	@$(MAKE) --silent $(OBJS_T)

clean:
	@rm -rf $(DIR_B)
	@rm -vf $(BIN_R) $(BIN_D) $(OBJS_G)

libs:
	@$(MAKE) --silent $(MBEDTLS_LIBS)

install: $(BIN_R)
	@echo installing executable to $(BIN_DIR)
	@echo installing manual page to $(MAN_DIR)
	@mkdir -p $(BIN_DIR)
	@mkdir -p $(MAN_DIR)
	@cp -f rirc $(BIN_DIR)
	@chmod 755 $(BIN_DIR)/rirc
	@sed "s/VERSION/$(VERSION)/g" < rirc.1 > $(MAN_DIR)/rirc.1

uninstall:
	rm -f $(BIN_DIR)/rirc
	rm -f $(MAN_DIR)/rirc.1

-include $(OBJS_R:.o=.o.d)
-include $(OBJS_D:.o=.o.d)
-include $(OBJS_T:.t=.t.d)

.DEFAULT_GOAL := $(BIN_R)

.PHONY: all check clean libs install uninstall

.SUFFIXES: