CC = gcc

# asm optimizations:
USE_ASM=1

CFLAGS = -m32 -Wall -O2
CFLAGS+= -fomit-frame-pointer
CFLAGS+= -ffast-math
CPPFLAGS = -DSIN
CPPFLAGS+= -DNDEBUG
CPPFLAGS+= -I. -I../client -I../qcommon -I../game
LDFLAGS = -m32 -shared -Wl,--enable-auto-image-base -Wl,--no-undefined
LDLIBS = -lwinmm -lgdi32

ifeq ($(USE_ASM),1)
CPPFLAGS+= -Did386=1
else
CPPFLAGS+= -Did386=0
endif

REFSOFT = ../game/q_shared.o \
	../win32/q_shwin.o \
	../win32/rw_imp.o \
	../win32/rw_ddraw.o \
	../win32/rw_dib.o \
	r_alias.o \
	r_main.o \
	r_light.o \
	r_misc.o \
	r_model.o \
	r_spritemod.o \
	r_sinmdl.o \
	r_console3d.o \
	r_tesselate.o \
	r_part.o \
	r_poly.o \
	r_polyse.o \
	r_rast.o \
	r_scan.o \
	r_sprite.o \
	r_surf.o \
	r_aclip.o \
	r_bsp.o \
	r_draw.o \
	r_edge.o \
	r_image.o

ifeq ($(USE_ASM),1)
REFASM = ../gas/math_ref.o \
	../gas/math.o \
	../gas/d_polysa.o \
	../gas/d_polysa_16bit.o \
	../gas/r_aclipa.o \
	../gas/r_draw16.o \
	../gas/r_drawa.o \
	../gas/r_edgea.o \
	../gas/r_scana.o \
	../gas/r_spr8.o \
	../gas/r_surf8.o \
	../gas/r_varsa.o \
	../gas/sys_dosa.o
# D_DrawSpans16_16bit() is broken and not used.
#	../gas/r_draw16_16bit.o
endif

.PHONY: all test clean
#

OBJECTS = $(REFSOFT) $(REFASM)

all: ref_soft.dll

ref_soft.dll: $(OBJECTS) ../win32/ref_soft.def
	$(CC) -o ref_soft.dll $(LDFLAGS) $(OBJECTS) ../win32/ref_soft.def $(LDLIBS)

clean:
	rm -f *.o
	rm -f ../win32/*.o
	rm -f ../game/*.o
	rm -f ../gas/*.o

DO_AS=$(CC) -x assembler-with-cpp

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

%.o: %.s
	$(DO_AS) $(CFLAGS) $(CPPFLAGS) -c $< -o $@
