Skip to content

Commit

Permalink
Big build and test cleanup.
Browse files Browse the repository at this point in the history
- Inline tests/Compare.* into tests/test.sh, and fix various issues,
  like assuming '.' is in the $PATH
- Add targets instrumented with LLVM sanitizers to the Makefile, and
  support running tests with them

TODO: Describe these changes in more detail in README, and add
instructions.
  • Loading branch information
Andy C committed Jul 20, 2016
1 parent 63683cb commit 759a20d
Show file tree
Hide file tree
Showing 12 changed files with 426 additions and 190 deletions.
190 changes: 145 additions & 45 deletions Makefile
Expand Up @@ -22,68 +22,168 @@
# THIS SOFTWARE.
# ****************************************************************/

CFLAGS = -g
CFLAGS = -O2
CFLAGS =

CC = gcc -Wall -g -Wwrite-strings
CC = gcc -fprofile-arcs -ftest-coverage # then gcov f1.c; cat f1.c.gcov
CC = gcc -g -Wall -pedantic
CC = gcc -O4 -Wall -pedantic -fno-strict-aliasing

YACC = bison -d -y
#YACC = yacc -d -S
#YFLAGS = -d -S
# -S uses sprintf in yacc parser instead of sprint

OFILES = b.o main.o parse.o proctab.o tran.o lib.o run.o lex.o

SOURCE = awk.h ytab.c ytab.h proto.h awkgram.y lex.c b.c main.c \
maketab.c parse.c lib.c run.c tran.c proctab.c

LISTING = awk.h proto.h awkgram.y lex.c b.c main.c maketab.c parse.c \
lib.c run.c tran.c
# CLANG_DIR should be set to build and run tests under sanitizers.
ifdef CLANG_DIR
san_cc := $(CLANG_DIR)/bin/clang
else
san_cc := clang
endif

# -d: produce a header file
# -y: emulate POSIX yacc
YFLAGS := -d -y

obj_files := \
b.o \
main.o \
parse.o \
proctab.o \
tran.o \
lib.o \
run.o \
lex.o \
ytab.o

default: bwk-dbg

all: bwk bwk-dbg bwk-cov bwk-asan bwk-msan bwk-ubsan bwk-sancov-func test_bin

test_bin: tests/echo tests/time

HEADER_DEPS := awk.h ytab.h proto.h

compile_c = $(CC) $(CFLAGS) -c -o $@ $<

# TODO: Remove all this duplication. Options:
# - eval ?
# - shell script to gen fragments, and then include? Like .d fragments.

# Compile objects
_obj/bwk/%.o: %.c $(HEADER_DEPS)
mkdir -p _obj/bwk
$(compile_c)

_obj/bwk-dbg/%.o: %.c $(HEADER_DEPS)
mkdir -p _obj/bwk-dbg
$(compile_c)

_obj/bwk-cov/%.o: %.c $(HEADER_DEPS)
mkdir -p _obj/bwk-cov
$(compile_c)

_obj/bwk-asan/%.o: %.c $(HEADER_DEPS)
mkdir -p _obj/bwk-asan
$(compile_c)

_obj/bwk-msan/%.o: %.c $(HEADER_DEPS)
mkdir -p _obj/bwk-msan
$(compile_c)

_obj/bwk-ubsan/%.o: %.c $(HEADER_DEPS)
mkdir -p _obj/bwk-ubsan
$(compile_c)

_obj/bwk-sancov-func/%.o: %.c $(HEADER_DEPS)
mkdir -p _obj/bwk-sancov-func
$(compile_c)

# NOTE: What is ALLOC used for? Specify a different allocator, I guess.
link_bwk = $(CC) -o $@ $(CFLAGS) $^ $(ALLOC) -lm

# "release" target, stripped
bwk: CFLAGS += -O4 -Wall -pedantic -fno-strict-aliasing
bwk: $(addprefix _obj/bwk/,$(obj_files))
$(link_bwk)
strip bwk

# "debug" target compiles faster
bwk-dbg: CFLAGS += -Wall -pedantic
bwk-dbg: $(addprefix _obj/bwk-dbg/,$(obj_files))
$(link_bwk)

# Build instrumented for coverage.
# gcov f1.c; cat f1.c.gcov
bwk-cov: CFLAGS += -fprofile-arcs -ftest-coverage
bwk-cov: $(addprefix _obj/bwk-cov/,$(obj_files))
$(link_bwk)

# Binaries built with Clang sanitizers. All of these should be unstripped
# because they show stack traces at runtime.
bwk-asan: CC := $(san_cc)
bwk-asan: CFLAGS += -fsanitize=address -g
bwk-asan: $(addprefix _obj/bwk-asan/,$(obj_files))
$(link_bwk)

bwk-msan: CC := $(san_cc)
bwk-msan: CFLAGS += -fsanitize=memory -g
bwk-msan: $(addprefix _obj/bwk-msan/,$(obj_files))
$(link_bwk)

bwk-ubsan: CC := $(san_cc)
bwk-ubsan: CFLAGS += -fsanitize=undefined -fno-omit-frame-pointer -g
bwk-ubsan: $(addprefix _obj/bwk-ubsan/,$(obj_files))
$(link_bwk)

# NOTES:
# - Coverage works on top of top of sanitizers. Just pick one I guess.
# - There are different types of coverage, e.g. -fsanitize-coverage = func, bb
# or edge. Picking one for now.
bwk-sancov-func: CC := $(san_cc)
bwk-sancov-func: CFLAGS += -fsanitize=memory -g -fsanitize-coverage=func
bwk-sancov-func: $(addprefix _obj/bwk-sancov-func/,$(obj_files))
$(link_bwk)

#
# Code Generation
#

ytab.c ytab.h: awk.h proto.h awkgram.y
$(YACC) -o ytab.c $(YFLAGS) awkgram.y

proctab.c: maketab
./maketab >proctab.c

SHIP = README FIXES $(SOURCE) ytab[ch].bak makefile \
awk.1
maketab: ytab.h maketab.c
$(CC) $(CFLAGS) maketab.c -o maketab

a.out: ytab.o $(OFILES)
$(CC) $(CFLAGS) ytab.o $(OFILES) $(ALLOC) -lm
#
# Test utils
#

$(OFILES): awk.h ytab.h proto.h
tests/echo: tests/echo.c
tests/time: tests/time.c

ytab.o: awk.h proto.h awkgram.y
$(YACC) $(YFLAGS) awkgram.y
mv y.tab.c ytab.c
mv y.tab.h ytab.h
$(CC) $(CFLAGS) -c ytab.c
#
# Release
#

proctab.c: maketab
./maketab >proctab.c
source := awk.h ytab.c ytab.h proto.h awkgram.y lex.c b.c main.c \
maketab.c parse.c lib.c run.c tran.c proctab.c

maketab: ytab.h maketab.c
$(CC) $(CFLAGS) maketab.c -o maketab
ship := README FIXES $(source) ytab[ch].bak makefile awk.1

bundle:
@cp ytab.h ytabh.bak
@cp ytab.c ytabc.bak
@bundle $(SHIP)
@bundle $(ship)

tar:
@cp ytab.h ytabh.bak
@cp ytab.c ytabc.bak
@bundle $(SHIP) >awk.shar
@tar cf awk.tar $(SHIP)
@bundle $(ship) >awk.shar
@tar cf awk.tar $(ship)
gzip awk.tar
ls -l awk.tar.gz
@zip awk.zip $(SHIP)
@zip awk.zip $(ship)
ls -l awk.zip

names:
@echo $(LISTING)

clean:
rm -r -f _obj
rm -f \
a.out *.o *.obj maketab \
*.bb *.bbg *.da *.gcov *.gcno *.gcda \
proctab.c ytab.c ytab.h
bwk bwk-* maketab \
*.bb *.bbg *.da \
proctab.c ytab.c ytab.h \
tests/echo tests/time

.PHONY: clean
.PHONY: bundle tar names
20 changes: 18 additions & 2 deletions run.sh
Expand Up @@ -9,8 +9,24 @@ set -o errexit

download() {
mkdir -p _tmp
wget --directory _tmp 'https://www.cs.princeton.edu/~bwk/btl.mirror/awk.tar.gz'
wget --directory _tmp 'https://www.cs.princeton.edu/~bwk/btl.mirror/awktest.a'
wget --directory _tmp \
'https://www.cs.princeton.edu/~bwk/btl.mirror/awk.tar.gz'
wget --directory _tmp \
'https://www.cs.princeton.edu/~bwk/btl.mirror/awktest.a'
}

count() {
ls *.[chy] | grep -v -E 'ytab|proctab' | xargs wc -l | sort -n
}

coverage() {
mkdir -p _gcov
rm --verbose _gcov/*

# After running tests with bwk-cov, .gcno and .gcda files are in
# obj/bwk-cov, next to the objects.
gcov --object-directory obj/bwk-cov/ *.c
mv *.gcov _gcov
}

"$@"
50 changes: 50 additions & 0 deletions test-results/2016-06-12.txt
@@ -0,0 +1,50 @@
On Ubuntu 14.04, comparing against mawk.

CPU: Intel(R) Core(TM) i7-4790 CPU @ 3.60GHz

new old new/old

0.08 0.02 4.000 tt.02:
0.08 0.03 2.667 tt.02a:
0.05 0.01 5.000 tt.03:
0.05 0.02 2.500 tt.03a:
0.25 0.11 2.273 tt.04:
0.27 0.10 2.700 tt.05:
0.06 0.02 3.000 tt.06:
0.05 0.02 2.500 tt.07:
0.09 0.04 2.250 tt.12:
0.87 0.12 7.250 tt.13:
1.10 0.22 5.000 tt.13a:
0.01 0.01 1.000 tt.14:
0.52 0.21 2.476 tt.15:
0.28 0.12 2.333 tt.16:
2.72 0.67 4.060 tt.big:

6.69 1.72

avg new/old = 2.334
total new/old = 3.890
21 tests

new old new/old

0.08 0.02 4.000 tt.02:
0.08 0.03 2.667 tt.02a:
0.05 0.02 2.500 tt.03:
0.05 0.02 2.500 tt.03a:
0.25 0.11 2.273 tt.04:
0.27 0.10 2.700 tt.05:
0.05 0.01 5.000 tt.06:
0.05 0.02 2.500 tt.07:
0.09 0.04 2.250 tt.12:
0.88 0.12 7.333 tt.13:
1.08 0.22 4.909 tt.13a:
0.52 0.20 2.600 tt.15:
0.29 0.13 2.231 tt.16:
2.70 0.67 4.030 tt.big:

6.64 1.71

avg new/old = 2.262
total new/old = 3.883
21 tests
10 changes: 0 additions & 10 deletions tests/Compare.T

This file was deleted.

2 changes: 1 addition & 1 deletion tests/Compare.drek
Expand Up @@ -32,4 +32,4 @@ do
cat foo1t foo2t >>footot
done

ctimes footot
./ctimes footot
17 changes: 0 additions & 17 deletions tests/Compare.p

This file was deleted.

17 changes: 0 additions & 17 deletions tests/Compare.t

This file was deleted.

49 changes: 0 additions & 49 deletions tests/Compare.tt

This file was deleted.

0 comments on commit 759a20d

Please sign in to comment.