CC = gcc
NASM = nasm

# asm optimizations (shouldn't be needed)
USE_ASM=0

CFLAGS = -Wall -O2 -fPIC
CFLAGS+= -fomit-frame-pointer
CFLAGS+= -ffast-math
CPPFLAGS = -DSIN
CPPFLAGS+= -DNDEBUG
CPPFLAGS+= -I../client -I../qcommon -I../game
CPPFLAGS+= -I../macosx
CPPFLAGS+= -I../ref_gl
LDFLAGS = -dynamiclib
LDFLAGS+= -Wl,-single_module
# export only the relevant functions:
LDFLAGS+= -Wl,-exported_symbols_list,ref.export
LDLIBS =

ifeq ($(USE_ASM),1)
CPPFLAGS+= -Did386=1
# enable text relocs in 'slidable image' :
LDFLAGS+= -Wl,-read_only_relocs,suppress
else
CPPFLAGS+= -Did386=0
endif

CPPFLAGS+= -DGLIMP_SDL
SDL_FRAMEWORK_PATH=../macosx
SDL_LIBS  =-F$(SDL_FRAMEWORK_PATH)
SDL_CFLAGS=-F$(SDL_FRAMEWORK_PATH)
SDL_LIBS +=-Wl,-framework,SDL -Wl,-framework,Cocoa
CFLAGS+= $(SDL_CFLAGS)
LDLIBS+= $(SDL_LIBS)

REFSOFT = ../game/q_shared.o \
	../qcommon/crc.o \
	../qcommon/glob.o \
	../linux/q_shlinux.o \
	../linux/qgl_linux.o \
	../linux/gl_sdl.o \
	gl_console3d.o \
	gl_arrays.o \
	gl_draw.o \
	gl_image.o \
	gl_light.o \
	gl_mesh.o \
	gl_model.o \
	gl_rmain.o \
	gl_rmisc.o \
	gl_rsurf.o \
	gl_sinmdl.o \
	gl_splatter.o \
	gl_sprite.o \
	gl_tesselate.o \
	gl_warp.o

ifeq ($(USE_ASM),1)
REFASM = ../nasm/math_ref.o \
	../nasm/math.o \
	../nasm/sys_ia32.o
endif

.PHONY: all test clean
#

OBJECTS = $(REFSOFT) $(REFASM)

all: ref_gl.dylib

ref_gl.dylib: $(OBJECTS)
	$(CC) -o $@ $(LDFLAGS) $(OBJECTS) $(LDLIBS)

clean:
	rm -f *.o
	rm -f ../linux/*.o
	rm -f ../game/*.o
	rm -f ../qcommon/*.o
	rm -f ../gas/*.o

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

%.o: %.asm
	$(NASM) -I../nasm/ -f macho -o $@ $<
