CC=gcc
CFLAGS=-g3 -I .

LIBS=-L /usr/lib/w32api
DLL1_LDFLAGS=-Wl,--export-all-symbols -Wl,--out-implib=libcrtest.dll.a

no_DLL2_LDFLAGS=-Wl,--export-all-symbols -Wl,--out-implib=libcrtestx_no.dll.a \
	-Wl,--enable-auto-import -Wl,--disable-runtime-pseudo-reloc

v1_DLL2_LDFLAGS=-Wl,--export-all-symbols -Wl,--out-implib=libcrtestx_v1.dll.a \
	-Wl,--enable-auto-import -Wl,--enable-runtime-pseudo-reloc-v1

v2_DLL2_LDFLAGS=-Wl,--export-all-symbols -Wl,--out-implib=libcrtestx_v2.dll.a \
	-Wl,--enable-auto-import -Wl,--enable-runtime-pseudo-reloc-v2

# use same image base to ensure that one or the other gets relocated
COMMON_IMAGE_BASE=-Wl,--image-base=0x20000000

no_EXE_LDFLAGS=-Wl,--enable-auto-import -Wl,--disable-runtime-pseudo-reloc
v1_EXE_LDFLAGS=-Wl,--enable-auto-import -Wl,--enable-runtime-pseudo-reloc-v1
v2_EXE_LDFLAGS=-Wl,--enable-auto-import -Wl,--enable-runtime-pseudo-reloc-v2
# DEBUG_LDFLAGS=-Wl,--enable-extra-pe-debug -Wl,--verbose

all: crtestx_v1.dll crtestx_v2.dll crtest.dll crtest1_v1.exe crtest1_v2.exe \
	crtest1_no_v1.exe crtest1_no_v2.exe # crtestx_no.dll crtest1_no.exe

crtest_dll.o: crtest_dll.c
	$(CC) $(CFLAGS) -c $<

crtest.o: crtest.c
	$(CC) $(CFLAGS) -c $<

crtest.dll: crtest_dll.o
	$(CC) -g3 -shared $(COMMON_IMAGE_BASE) -o crtest.dll $(DLL_LDFLAGS) $< $(LIBS)

crtestx_no.dll: crtest.o crtest.dll
	$(CC) -g3 -shared $(COMMON_IMAGE_BASE) $(no_DLL2_LDFLAGS) $(DEBUG_LDFLAGS) -o $@ crtest.o -L. -lcrtest $(LIBS)


crtestx_v1.dll: crtest.o crtest.dll
	$(CC) -g3 -shared $(COMMON_IMAGE_BASE) $(v1_DLL2_LDFLAGS) $(DEBUG_LDFLAGS) -o $@ crtest.o -L. -lcrtest $(LIBS)

crtestx_v2.dll: crtest.o crtest.dll
	$(CC) -g3 -shared $(COMMON_IMAGE_BASE) $(v2_DLL2_LDFLAGS) $(DEBUG_LDFLAGS) -o $@ crtest.o -L. -lcrtest $(LIBS)

crtest1_no.exe: crtest1.o crtestx_no.dll
	$(CC) -g3 $(v1_EXE_LDFLAGS) $(DEBUG_LDFLAGS) -o $@ crtest1.o -L. -lcrtestx_no -lcrtest $(LIBS)

crtest1_no_v1.exe: crtest1.o crtestx_v1.dll
	$(CC) -g3 $(no_EXE_LDFLAGS) $(DEBUG_LDFLAGS) -o $@ crtest1.o -L. -lcrtestx_v1 -lcrtest $(LIBS)

crtest1_no_v2.exe: crtest1.o crtestx_v2.dll
	$(CC) -g3 $(no_EXE_LDFLAGS) $(DEBUG_LDFLAGS) -o $@ crtest1.o -L. -lcrtestx_v2 -lcrtest $(LIBS)

crtest1_v1.exe: crtest1.o crtestx_v1.dll
	$(CC) -g3 $(v1_EXE_LDFLAGS) $(DEBUG_LDFLAGS) -o $@ crtest1.o -L. -lcrtestx_v1 -lcrtest $(LIBS)

crtest1_v2.exe: crtest1.o crtestx_v2.dll
	$(CC) -g3 $(v2_EXE_LDFLAGS) $(DEBUG_LDFLAGS) -o $@ crtest1.o -L. -lcrtestx_v2 -lcrtest $(LIBS)

.PHONY: clean

clean:
	rm -f *.o *.a crtest1_v1.exe crtest1_v2.exe crtest.dll
	rm -f crtestx_v1.dll crtestx_v2.dll crtest1_no_v1.exe crtest1_no_v2.exe
