Skip to content

Commit

Permalink
Makefile: better error handling.
Browse files Browse the repository at this point in the history
  • Loading branch information
rsm-lisper committed Jul 29, 2024
1 parent 090ab97 commit e7421a8
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 18 deletions.
48 changes: 30 additions & 18 deletions Exercise.mk
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,22 @@ CFLAGS=-Wall -Wextra -Werror -ggdb -std=c89

ANSI_COLORS=$(shell tput colors 2>/dev/null)
ifeq ($(ANSI_COLORS),$(filter $(ANSI_COLORS),8 16 88 256))
ANSIGREEN="\033[32m"
ANSIYELLOW="\033[33m"
ANSIBOLD="\033[1m"
ANSIRST="\033[0m"
ANSIGOK=" ["$(ANSIGREEN)"OK"$(ANSIRST)"] "
ANSIYBIN=$(ANSIYELLOW)$(BINARY)$(ANSIRST)
ANSIRED="\033[31m"
ANSIGREEN="\033[32m"
ANSIYELLOW="\033[33m"
ANSIBOLD="\033[1m"
ANSIRST="\033[0m"
DIFFCOLOR="always"
else
ANSIGREEN=""
ANSIYELLOW=""
ANSIBOLD=""
ANSIRST=""
ANSIGOK=" [OK] "
ANSIYBIN=$(BINARY)
ANSIRED=""
ANSIGREEN=""
ANSIYELLOW=""
ANSIBOLD=""
ANSIRST=""
DIFFCOLOR="never"
endif
ANSIGOK=" ["$(ANSIGREEN)"OK"$(ANSIRST)"] "
ANSIYBIN=$(ANSIYELLOW)$(BINARY)$(ANSIRST)

MAKEFLAGS+=--silent

Expand All @@ -36,15 +38,25 @@ $(BINARY): $(CFILES)
check: $(TEST_RESULTS)
echo "** "$(ANSIBOLD)$(ANSIYBIN)" test results:"$(ANSIBOLD)$(ANSIGOK)

%.res: TESTNAME=$(strip $(patsubst $(TESTDIR)%.tin,%,$?))
%.res: TESTARGS=$$(cat $(strip $(patsubst %.tin,%.targs,$?)) 2>/dev/null)
%.res: TESTOUT=$(strip $(patsubst %.tin,%.tout,$?))
%.res: TESTERR=$(strip $(patsubst %.tin,%.terr,$?))
%.res: %.tin
echo -n " - testing "$(ANSIBOLD)$(BINARY)$(ANSIRST)":" \
$(ANSIYELLOW)$(patsubst $(TESTDIR)%.tin,%,$?)$(ANSIRST)
TEST_ARGS=$$(cat $(patsubst %.tin,%.targs,$?) 2>/dev/null) ; \
./$(BINARY) $$TEST_ARGS <$? >$@ult 2>$@ult_err || ERR=1
diff --color=always --text $@ult $(patsubst %.tin,%.tout,$?)
if [ -f $(patsubst %.tin,%.terr,$?) ]; then \
diff --color=always --text $@ult_err $(patsubst %.tin,%.terr,$?); fi
$(ANSIYELLOW)$(TESTNAME)$(ANSIRST)
ifeq (,$(wildcard $(BINARY)))
echo "\n"$(ANSIRED)"Error! "$(ANSIBOLD)"./"$(BINARY)$(ANSIRST)$(ANSIRED)" not found." \
"Please 'make'."$(ANSIRST)
false
else
./$(BINARY) $(TESTARGS) <$? >$@ult 2>$@ult_err || true
diff --color=$(DIFFCOLOR) --text $@ult $(TESTOUT)
if [ -f $(strip $(TESTERR)) ]; then \
diff --color=$(DIFFCOLOR) --text $@ult_err $(TESTERR); \
fi
echo $(ANSIGOK)
endif

clean:
echo -n " - Cleaning "$(ANSIYBIN)":"
Expand Down
12 changes: 12 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,22 @@ SUBDIRS=$(wildcard chapter_*/)

TOPTARGETS=all check clean

ANSI_COLORS=$(shell tput colors 2>/dev/null)
ifeq ($(ANSI_COLORS),$(filter $(ANSI_COLORS),8 16 88 256))
ANSIRED="\033[31m"
ANSIGREEN="\033[32m"
ANSIYELLOW="\033[33m"
ANSIBOLD="\033[1m"
ANSIRST="\033[0m"
DIFFCOLOR="always"
else
ANSIRED=""
ANSIGREEN=""
ANSIYELLOW=""
ANSIBOLD=""
ANSIRST=""
DIFFCOLOR="never"
endif
ANSIGOK=" ["$(ANSIGREEN)"OK"$(ANSIRST)"] "

MAKEFLAGS+=--silent
Expand Down

0 comments on commit e7421a8

Please sign in to comment.