CC=gcc
CFLAGS=-Wall -g
LIBFAKETIME=libfaketime.so

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

all: timetest $(LIBFAKETIME)

timetest: timetest.o
        $(CC) $(CFLAGS) -o $@ $<

ld-so.h: dummy.c
        $(CC) -o tmp-dummy dummy.c
        objcopy --only-section .interp --output-target binary tmp-dummy tmp-dummy.interp
        awk 'BEGIN{printf("#ifdef LD_SO\n#undef LD_SO\n#endif\n\n");} NR==1{gsub (/\000/, "", $$1); printf("#define LD_SO \"%s\"\n", $$1);}' tmp-dummy.interp > ld-so.h
        rm -f tmp-dummy*

$(LIBFAKETIME): faketime.so
        $(CC) -shared -o $@ -Wl,-soname,$(LIBFAKETIME) $< -ldl -e __faketime_main

faketime.so: faketime.c ld-so.h

test: all
        @echo ==== Without preloaded library...
        ./timetest
        @echo
        @echo ==== Set the date to yesterday...
        FAKE_TIME_OFFSET=-86400 LD_PRELOAD=./libfaketime.so ./timetest
        @echo
        @echo ==== Set the date to 2000/01/01 12:13:14...
        FAKE_TIME_START=946725194 LD_PRELOAD=./libfaketime.so ./timetest

clean:
        -rm -f *.o *.so core timetest $(LIBFAKETIME) ld-so.h tmp-dummy*