
#  Makefile used to compile this source code. This Makefile can generate
#  three binaries: [[x]], the automaton ready to run with
#  debug options (i.e. a debugger like gdb can be attached to it);
#  [[x-opt]], the automaton optimized for speed and without
#  debug information neither assertions; and [[x-test]], a
#  program automatically running self-tests to check code correctness.


.SUFFIXES:.c .o .h
.SECONDARY: .o #.c .h

INCLUDE=-I.

CFLAGS=-Wall -D_REENTRANT -DI686 -g -pipe $(INCLUDE)
COPTFLAGS=-Wall -O3 -DNDEBUG -DI686 -pipe $(INCLUDE) 

# For COPTFLAGS optimizations, no assertions, no _REENTRANT because
# single threaded

.PHONY: default
default: Makefile test x

BINARIES=x \
	 x-test x-opt

X_SOURCES=x.c \
	perf.c \
	reas.c \
	seg_desc.c \
	info.c \
	decoding.c \
	autotest.c \
	misc.c

X_OBJECTS=$(X_SOURCES:.c=.o)
X_OPT_OBJECTS=$(X_SOURCES:.c=-opt.o)
X_TEST_OBJECTS=$(X_SOURCES:.c=-test.o)

OBJECTS=$(X_OBJECTS) $(X_OPT_OBJECTS) $(X_TEST_OBJECTS)

.PHONY: all
all: Makefile bin test 

# compile c_sources

.PHONY: bin
bin: $(BINARIES) ;

LIBS=

%.o: %.c
	$(CC) $(CFLAGS) -c $< -o $@

x: $(X_OBJECTS)
	$(CC) $(CFLAGS) $^ -o $@ $(LIBS)

x.c: x.h \
	perf.h \
	reas.h \
	seg_desc.h \
	info.h \
	decoding.h \
	misc.h \
	autotest.h ;

perf.c: perf.h x.h ;

reas.c: reas.h x.h seg_desc.h ;

seg_desc.c: seg_desc.h x.h ;

info.c: info.h x.h misc.h ;

decoding.c: x.h misc.h reas.h decoding.h \
	info.h perf.h ;

autotest.c: x.h autotest.h reas.h \
	info.h seg_desc.h ;

#compile test version

%-test.o: %.c
	$(CC) $(CFLAGS) -DTEST -c $< -o $@

x-test: $(X_TEST_OBJECTS)
	$(CC) $(CFLAGS) $^ -o $@ $(LIBS) -lpthread

#compile optimize version

.PHONY: opt
opt: x-opt

%-opt.o: %.c
	$(CC) $(COPTFLAGS) -c $< -o $@

x-opt: $(X_OPT_OBJECTS)
	$(CC) $(COPTFLAGS) $^ -o $@ $(LIBS)

# run tests

.PHONY: test
test: x-test
	@echo "  ** do tests **"
	./x-test

# useful aliases

.PHONY: distrib
distrib: bin-clean  # for distribution

# cleanup

.PHONY: reallyclean
reallyclean: clean

.PHONY: clean
clean: bin-clean
	-rm error keep.erl # files generated by cpb

.PHONY: bin-clean
bin-clean:
	-rm $(BINARIES) $(OBJECTS)

