#
# GNU make 3.80, or better, is required here
#

all: bin/cachecc1.so bin/distccwrap

bin/cachecc1.so: obj/cachecc1.o obj/hash.o obj/followcc1.o obj/utils.o
bin/cachecc1.so: obj/distcc1.o
bin/distccwrap: obj/distccwrap.o

CCFLAGS = -O -Wall -Werror $(CFLAGS)
bin/%.so: LDFLAGS += -nostdlib -shared -ldl
bin/%.so: CCFLAGS += -fPIC

# gcc 3.x is highly recommended.
CC = gcc

bin/% bin/%.so:
	@mkdir -p $(@D)/.
	$(CC) -o $@ $^ $(LDFLAGS)

obj/%.o: %.c cachecc1.h
	@mkdir -p $(@D)/.
	$(CC) -o $@ $< -c $(CCFLAGS)

clean:
	rm -f *~
	rm -rf bin obj tmp

test: all
	rm -rf tmp
	mkdir tmp
	$(ENV) $(CC) -S -o tmp/tmp.s cachecc1.c
	$(ENV) $(CC) -S -o tmp/tmp.s cachecc1.c
	# two files : the cached entry and tmp.s
	[ `find tmp -type f | wc -l` = 2 ]
	# one link : the gcc signature
	[ `find tmp -type l | wc -l` = 1 ]
	# five directories : one that tells if gcc is 3.x
	# and four to contain the two files, the link and the cache
	[ `find tmp -type d | wc -l` = 5 ]

# Set LD_PRELOAD for the test
test: ENV := LD_PRELOAD=$(shell pwd)/bin/cachecc1.so
# Set the cache location
test: ENV += CACHECC1_DIR=$(shell pwd)/tmp/cachecc1dir
# cachecc1.so must be loaded in the command that calls $(CC).
# 'env' will be this command
test: ENV += env

# arch-tag: 1a774470-2cc4-4ed4-b65a-6558c61420fe
