From 7ca8623927280cdceebfce8e4413960efac90446 Mon Sep 17 00:00:00 2001 From: Aidan MacDonald Date: Sun, 28 Nov 2021 13:42:52 +0000 Subject: x1000-installer: add test & code coverage support to makefile Pass COVERAGE=1 to enable clang-based code coverage and pass SANITIZE=1 to enable sanitizers. 'make cov' will run the test app and show a coverage summary. Change-Id: I8a33e8b78665165d8da1818dc01f495f0c52cf06 --- lib/x1000-installer/.gitignore | 2 ++ lib/x1000-installer/Makefile | 56 ++++++++++++++++++++++++++++++++++++------ 2 files changed, 51 insertions(+), 7 deletions(-) (limited to 'lib') diff --git a/lib/x1000-installer/.gitignore b/lib/x1000-installer/.gitignore index dae360e116..e7ad25b29f 100644 --- a/lib/x1000-installer/.gitignore +++ b/lib/x1000-installer/.gitignore @@ -1,3 +1,5 @@ xf_test fakeNAND.bin fakeNAND_meta.bin +*.profraw +*.profdata diff --git a/lib/x1000-installer/Makefile b/lib/x1000-installer/Makefile index 35fa747aab..1a63e6d5b3 100644 --- a/lib/x1000-installer/Makefile +++ b/lib/x1000-installer/Makefile @@ -20,19 +20,61 @@ MTARINC = -I../microtar/src MTARLIB = ../microtar/libmicrotar.a CPPFLAGS = -Iinclude -Itest_lib $(MTARINC) -D_XOPEN_SOURCE=500 -D_POSIX_C_SOURCE=200809L -CFLAGS = -std=c99 -Wall -Wextra -O2 +CFLAGS = -std=c99 -Wall -Wextra +LDFLAGS = +PROFRAW_FILE=$(TBIN).profraw +PROFDATA_FILE=$(TBIN).profdata + +export LLVM_PROFILE_FILE=$(PROFRAW_FILE) + +ifeq ($(COVERAGE),1) + CC = clang + CFLAGS += -g -Og -fprofile-instr-generate -fcoverage-mapping + LDFLAGS += -fprofile-instr-generate -fcoverage-mapping +else + CFLAGS += -O2 +endif + +ifeq ($(SANITIZE),1) + CFLAGS += -fsanitize=address -fsanitize=undefined + LDFLAGS += -fsanitize=address -fsanitize=undefined +endif + +.PHONY: all all: $(LIB) $(TBIN) -%.o: %.c - $(CC) $(CFLAGS) $(CPPFLAGS) -c $< -o $@ +.PHONY: test +test: $(TBIN) + @./$(TBIN) -$(LIB): $(OBJ) - $(AR) rcs $@ $^ >/dev/null +.PHONY: cov +cov: $(PROFDATA_FILE) + @llvm-cov report $(TBIN) -instr-profile=$(PROFDATA_FILE) -$(TBIN): $(TOBJ) $(LIB) $(MTARLIB) - $(CC) -o $@ $^ +.PHONY: cov-show +cov-show: $(PROFDATA_FILE) + @llvm-cov show $(TBIN) --use-color -instr-profile=$(PROFDATA_FILE) $(f) | less -R +.PHONY: clean clean: rm -f $(LIB) $(OBJ) rm -f $(TBIN) $(TOBJ) + rm -f $(PROFRAW_FILE) $(PROFDATA_FILE) + rm -f fakeNAND.bin fakeNAND_meta.bin + +$(LIB): $(OBJ) + $(AR) rcs $@ $^ >/dev/null + +$(TBIN): $(TOBJ) $(LIB) $(MTARLIB) + $(CC) -o $@ $^ $(LDFLAGS) + +%.o: %.c + $(CC) $(CFLAGS) $(CPPFLAGS) -c $< -o $@ + +# use separate rule instead of depending on 'test' to avoid re-running +$(PROFRAW_FILE): $(TBIN) + @./$(TBIN) + +$(PROFDATA_FILE): $(PROFRAW_FILE) + @llvm-profdata merge -sparse $(PROFRAW_FILE) -o $(PROFDATA_FILE) -- cgit v1.2.3