-
Notifications
You must be signed in to change notification settings - Fork 0
/
Makefile
194 lines (163 loc) · 4.72 KB
/
Makefile
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# SPDX-License-Identifier: MPL-2.0
#
# This file is part of boislib,
# a Collection of portable libraries to extended the C ecosystem.
#
# Copyright (C) 2024 Matheus Souza <[email protected]>
# project name
PROJECT = boislib
# paths
TESTDIR ?= ./tests
SRCDIR ?= ./src
INCDIR ?= ./src
BUILDDIR := ./build
DBGDIR := $(BUILDDIR)/debug
RELDIR := $(BUILDDIR)/release
# install path
DESTDIR ?=
USRDIR := /usr/local
LIBDIR := $(DESTDIR)$(USRDIR)/lib
HEADERDIR := $(DESTDIR)$(USRDIR)/include
# compiler
CROSS-COMPILE ?=
CC ?= $(CROSS-COMPILE)gcc
CXX ?= $(CROSS-COMPILE)g++
AR ?= $(CROSS-COMPILE)ar
# compiler and linker flags
CFLAGS := -Wall -I $(INCDIR) -MMD -MP
CXXFLAGS := -Wall -I $(INCDIR) -MMD -MP
LDFLAGS ?=
CXXTESTFLAGS := -Wall -I $(INCDIR) -MMD -MP
LDTESTFLAGS ?= -lgtest -lgtest_main -lpthread
# configure for release or debug
ifeq ($(RELEASE),1)
EXEDIR := $(RELDIR)
OBJDIR := $(RELDIR)/obj
CFLAGS += -O2 -DNDEBUG
CXXFLAGS += -O2 -DNDEBUG
else
EXEDIR := $(DBGDIR)
OBJDIR := $(DBGDIR)/obj
CFLAGS += -ggdb3 -O0 -DDEBUG
CXXFLAGS += -ggdb3 -O0 -DDEBUG
endif
# sources to compile
ALLCSRCS ?= $(shell find $(SRCDIR) -type f -name "*.c")
ALLCXXSRCS ?= $(shell find $(SRCDIR) -type f -name "*.cpp")
TESTSRCS ?= $(shell find $(TESTDIR) -type f -name "*.cpp")
# set the linker to g++ if there is any c++ source code
ifeq ($(ALLCXXSRCS),)
LD := $(CROSS-COMPILE)gcc
else
LD := $(CROSS-COMPILE)g++
endif
# objects settings
COBJS := $(addprefix $(OBJDIR)/, $(notdir $(ALLCSRCS:.c=.o)))
CXXOBJS := $(addprefix $(OBJDIR)/, $(notdir $(ALLCXXSRCS:.cpp=.o)))
TESTOBJS := $(addprefix $(OBJDIR)/, $(notdir $(TESTSRCS:.cpp=.o)))
OBJS := $(COBJS) $(CXXOBJS)
DEPS := $(OBJS:.o=.d) $(TESTOBJS:.o=.d)
# paths where to search for sources
SRCPATHS := $(sort $(dir $(ALLCSRCS)) $(dir $(ALLCXXSRCS)) $(dir $(TESTSRCS)))
VPATH = $(SRCPATHS)
# output
OUTPATHS := $(sort $(dir $(ALLCSRCS:$(SRCDIR)/%=%)) $(dir $(ALLCXXSRCS:$(SRCDIR)/%=%)))
SHARED := $(ALLCSRCS:$(SRCDIR)/%.c=%.so) $(ALLCXXSRCS:$(SRCDIR)/%.cpp=%.so)
SHARED := $(EXEDIR)/lib/$(SHARED)
STATIC := $(ALLCSRCS:$(SRCDIR)/%.c=%.a) $(ALLCXXSRCS:$(SRCDIR)/%.cpp=%.a)
STATIC := $(EXEDIR)/lib/$(STATIC)
HEADERSSRC ?= $(shell find $(SRCDIR) -type f -name "*.h")
HEADERSDEST := $(HEADERSSRC:$(SRCDIR)/%=$(HEADERDIR)/%)
# targets
.PHONY: all tests run_tests coverage report format clean install
all: $(OBJDIR) $(OUTPATHS) $(OBJS) $(SHARED) $(STATIC)
# targets for the directories
$(OBJDIR):
ifeq ($(VERBOSE),1)
mkdir -p $(OBJDIR)
else
@mkdir -p $(OBJDIR)
endif
$(OUTPATHS):
ifeq ($(VERBOSE),1)
mkdir -p $(EXEDIR)/lib/$(OUTPATHS)
else
@mkdir -p $(EXEDIR)/lib/$(OUTPATHS)
endif
# target for c objects
$(COBJS) : $(OBJDIR)/%.o : %.c
ifeq ($(VERBOSE),1)
$(CC) -c $(CFLAGS) $< -o $@
else
@echo -n -e '[CC]\t$<\n'
@$(CC) -c $(CFLAGS) $< -o $@
endif
# target for cpp objects
$(CXXOBJS) : $(OBJDIR)/%.o : %.cpp
ifeq ($(VERBOSE),1)
$(CXX) -c $(CXXFLAGS) $< -o $@
else
@echo -n -e '[CXX]\t$<\n'
@$(CXX) -c $(CXXFLAGS) $< -o $@
endif
# target for shared libraries
$(SHARED): $(OBJS)
ifeq ($(VERBOSE),1)
$(LD) $(LDFLAGS) -shared $(OBJS) -o $@
else
@echo -n -e '[LD]\t./$@\n'
@$(LD) $(LDFLAGS) -shared $(OBJS) -o $@
endif
# target for static libraries
$(STATIC): $(OBJS)
ifeq ($(VERBOSE),1)
$(AR) rcs $@ $(OBJS)
else
@echo -n -e '[AR]\t./$@\n'
@$(AR) rcs $@ $(OBJS)
endif
# target for install
install: all
@cp -r --update --verbose $(EXEDIR)/lib/* $(LIBDIR)
@mkdir -p $(HEADERDIR)/$(OUTPATHS)
@cp --update --verbose $(HEADERSSRC) $(HEADERSDEST)
# target for test cpp objects
$(TESTOBJS) : $(OBJDIR)/%.o : %.cpp
ifeq ($(VERBOSE),1)
$(CXX) -c $(CXXTESTFLAGS) $< -o $@
else
@echo -n -e '[CXX]\t$<\n'
@$(CXX) -c $(CXXTESTFLAGS) $< -o $@
endif
tests: $(OBJDIR) $(OUTPATHS) $(OBJS) $(TESTOBJS)
ifeq ($(VERBOSE),1)
$(CXX) $(LDTESTFLAGS) $(OBJS) $(TESTOBJS) -o $(EXEDIR)/$(PROJECT)_tests
else
@echo -n -e '[LD]\t$(EXEDIR)/$(PROJECT)_tests\n'
@$(CXX) $(LDTESTFLAGS) $(OBJS) $(TESTOBJS) -o $(EXEDIR)/$(PROJECT)_tests
endif
run_tests: tests
$(EXEDIR)/$(PROJECT)_tests
run_tests_no_output: tests
@$(EXEDIR)/$(PROJECT)_tests --gtest_brief=1 > /dev/null
coverage: CFLAGS += --coverage
coverage: CXXFLAGS += --coverage
coverage: LDTESTFLAGS += --coverage
coverage: clean run_tests_no_output
@echo
@lcov --capture --directory $(OBJDIR) \
--output-file $(BUILDDIR)/coverage.info
report: coverage
@genhtml $(BUILDDIR)/coverage.info \
--output-directory $(BUILDDIR)/report > /dev/null
@echo
@echo -n "Written coverage report to $(BUILDDIR)/coverage/index.html"
@echo
format:
find . \
-regex '.*\.\(cpp\|hpp\|cc\|cxx\|c\|h\)' \
-exec clang-format -style=file -i {} \;
clean:
rm -rf $(BUILDDIR)
# Include the dependency files, should be the last in the makefile
-include $(DEPS)