|
| 1 | +# |
| 2 | +# Makefile for unit tests using Catch2 and coverage with lcov |
| 3 | +# |
| 4 | + |
| 5 | +# Compiler settings |
| 6 | +CXX = g++ |
| 7 | +CXXFLAGS = -std=c++17 -g -Wall -Wextra -I.. -isystem ../../../../include |
| 8 | +LDFLAGS = |
| 9 | + |
| 10 | +# Coverage settings |
| 11 | +COVERAGE_CXXFLAGS = --coverage -fprofile-arcs -ftest-coverage |
| 12 | +COVERAGE_LDFLAGS = --coverage |
| 13 | + |
| 14 | +# Add library dependencies |
| 15 | +CXXFLAGS += $(shell pkg-config --cflags libavcodec libavutil catch2) |
| 16 | +LIBS = $(shell pkg-config --libs libavcodec libavutil catch2-with-main) |
| 17 | + |
| 18 | +# Source files |
| 19 | +TEST_SOURCES = catch_main.cpp test_pes.cpp |
| 20 | +SOURCE_UNDER_TEST = ../pes.cpp |
| 21 | + |
| 22 | +# Object files |
| 23 | +TEST_OBJS = $(TEST_SOURCES:.cpp=.o) |
| 24 | +SOURCE_OBJS = $(SOURCE_UNDER_TEST:.cpp=.o) |
| 25 | + |
| 26 | +# Test executable |
| 27 | +TEST_EXEC = test_runner |
| 28 | + |
| 29 | +# Main target |
| 30 | +all: $(TEST_EXEC) |
| 31 | + |
| 32 | +# Link test executable |
| 33 | +$(TEST_EXEC): $(TEST_OBJS) $(SOURCE_OBJS) |
| 34 | + $(CXX) $(CXXFLAGS) $(LDFLAGS) -o $@ $^ $(LIBS) |
| 35 | + |
| 36 | +# Compile test sources |
| 37 | +%.o: %.cpp |
| 38 | + $(CXX) $(CXXFLAGS) -c -o $@ $< |
| 39 | + |
| 40 | +# Compile sources under test |
| 41 | +../%.o: ../%.cpp |
| 42 | + $(CXX) $(CXXFLAGS) -c -o $@ $< |
| 43 | + |
| 44 | +# Run tests |
| 45 | +.PHONY: test |
| 46 | +test: $(TEST_EXEC) |
| 47 | + ./$(TEST_EXEC) -r console -r junit::out=test-results.xml |
| 48 | + |
| 49 | +# Clean |
| 50 | +.PHONY: clean |
| 51 | +clean: |
| 52 | + @echo "Cleaning test build artifacts..." |
| 53 | + rm -f $(TEST_OBJS) $(SOURCE_OBJS) $(TEST_EXEC) test-results.xml |
| 54 | + @echo "Cleaning coverage data..." |
| 55 | + rm -f *.gcda *.gcno ../*.gcda ../*.gcno coverage.info |
| 56 | + rm -rf coverage-html |
| 57 | + |
| 58 | +# Coverage targets |
| 59 | +.PHONY: coverage |
| 60 | +coverage: CXXFLAGS += $(COVERAGE_CXXFLAGS) |
| 61 | +coverage: LDFLAGS += $(COVERAGE_LDFLAGS) |
| 62 | +coverage: clean $(TEST_EXEC) |
| 63 | + @echo "Running tests with coverage..." |
| 64 | + ./$(TEST_EXEC) |
| 65 | + @echo "Generating coverage report..." |
| 66 | + lcov --capture --directory . --directory .. --output-file coverage.info |
| 67 | + lcov --remove coverage.info '/usr/*' '*/tests/*' --output-file coverage.info |
| 68 | + lcov --list coverage.info |
| 69 | + |
| 70 | +.PHONY: coverage-html |
| 71 | +coverage-html: coverage |
| 72 | + @echo "Generating HTML coverage report..." |
| 73 | + genhtml coverage.info --output-directory coverage-html |
| 74 | + @echo "Coverage report generated in coverage-html/index.html" |
| 75 | + |
| 76 | +.PHONY: help |
| 77 | +help: |
| 78 | + @echo "Available targets:" |
| 79 | + @echo " all - Build test executable (default)" |
| 80 | + @echo " test - Build and run tests with console output" |
| 81 | + @echo " coverage - Build with coverage, run tests, and generate coverage report" |
| 82 | + @echo " coverage-html - Generate HTML coverage report (includes coverage)" |
| 83 | + @echo " clean - Remove all build artifacts, test results, and coverage data" |
| 84 | + @echo " help - Show this help message" |
0 commit comments