aboutsummaryrefslogtreecommitdiffstats
path: root/test/Makefile
blob: 57b9df506dd2863577a71a9849eb07fc2db82fc4 (plain) (blame)
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
TARGET:=usdx-tests
include ../Makefile.inc

headers+=$(wildcard *.hpp) $(wildcard */*.hpp) $(wildcard */*/*.hpp)
sources+=$(wildcard *.cpp) $(wildcard */*.cpp) $(wildcard */*/*.cpp)

LDFLAGS+=-lcppunit
CXXFLAGS-COVERAGE:=-fprofile-arcs -ftest-coverage
LDFLAGS-COVERAGE:=-coverage
TARGET-COVERAGE:=$(TARGET)-coverage
COVERAGE-DIR:=$(TOP)coverage

objects-coverage:=$(patsubst %,$(COVERAGE-DIR)/obj/test/%,$(sources:.cpp=.o))
coverage:=$(COVERAGE-DIR)/coverage.info.tmp $(COVERAGE-DIR)/coverage.info \
		$(objects-coverage:.o=.gcda) $(objects-coverage:.o=.gcno) \
		$(COVERAGE-DIR)/obj/

# include the apropriate dir from main src tree
SRC_INC=$(addprefix -I$(TOP)src/,$(filter-out ..%,$(dir $<)))

##############################################################################

.PHONY:	all clean run

all:	$(TARGET)

clean:
	-$(RM) -r $(TARGET) $(TARGET-COVERAGE) $(objects) $(deps) $(coverage)

run:	$(TARGET)
	./$(TARGET)

ifneq ($(MAKECMDGOALS),clean)
-include $(deps)
endif

##############################################################################
# normal build

$(TARGET):	$(objects) $(deps)
	$(CXX) $(LDFLAGS) -o $(TARGET) $(objects)

%.o:		%.cpp
	$(CXX) $(CXXFLAGS) $(SRC_INC) -MMD -c $< -o $@

%.d:		%.cpp
	$(CXX) $(CXXFLAGS) $(SRC_INC) -MM -c $< -MF $@

##############################################################################
# build and execute tests with code coverage and build nice html pages

test-with-coverage:			$(COVERAGE-DIR)/html/index.html
	@test ! -e .error

$(COVERAGE-DIR)/html/index.html:	$(COVERAGE-DIR)/coverage.info
	genhtml $(COVERAGE-DIR)/coverage.info -o $(COVERAGE-DIR)/html/ -p \
		"$(realpath ./$(TOP)/src)"

$(COVERAGE-DIR)/coverage.info:		$(COVERAGE-DIR)/coverage.info.tmp
	lcov --extract $(COVERAGE-DIR)/coverage.info.tmp \
		"$(realpath ./$(TOP))/src/*" \
		-o $(COVERAGE-DIR)/coverage.info

$(COVERAGE-DIR)/coverage.info.tmp:	$(TARGET-COVERAGE)
	-$(RM) .error
	lcov --directory $(TOP) --zerocounters
	./$(TARGET-COVERAGE) || touch .error
	lcov --directory $(TOP) --capture \
		--output-file $(COVERAGE-DIR)/coverage.info.tmp -b .

$(TARGET-COVERAGE):			$(objects-coverage) $(deps)
	$(CXX) $(LDFLAGS) $(LDFLAGS-COVERAGE) -o $(TARGET-COVERAGE) \
		$(objects-coverage)

$(COVERAGE-DIR)/obj/test/%.o:		%.cpp
	@[ -d $(dir $@) ] || mkdir -p $(dir $@)
	$(CXX) $(CXXFLAGS) $(CXXFLAGS-COVERAGE) $(SRC_INC) -MMD -c $< -o $@