# -*- makefile -*-
#
#  Copyright (c) 2001-2009 Dan Gudmundsson, Bjorn Gustavsson
#
#  See the file "license.terms" for information on usage and redistribution
#  of this file, and for a DISCLAIMER OF ALL WARRANTIES.
#

#   Uncomment the two lines beginning with UNIVERSAL below if you want
#   to build universal binaries that will work on both Tiger and Leopard.
#
#   You will also need to do
#
#      export MACOSX_DEPLOYMENT_TARGET=10.4
#
#   in a bash shell before running this Makefile.
#

#UNIVERSAL_SDK = -isysroot /Developer/SDKs/MacOSX10.4u.sdk
#UNIVERSAL_FLAGS = $(UNIVERSAL_SDK) -arch i386 -arch ppc

CC = cc

#
# The settings of CFLAGS and LDFLAGS that follows assume 
# the SDL framework has been installed in /Library/Frameworks.
#

CFLAGS	= -g -O2 -Wall -D_THREAD_SAFE -D_OSX_COCOA -I/Library/Frameworks/SDL.framework/Headers -F/Library/Frameworks $(ERLINC) $(UNIVERSAL_FLAGS)

LDFLAGS = -F/Library/Frameworks -framework OpenGL -framework SDL -framework OpenGL -framework AGL -framework IOKit -framework Cocoa -L. $(UNIVERSAL_FLAGS)

TARGETDIR = ../priv

## The driver part needs erlang includes
ERL_DIR := $(shell echo 'io:format("~s~n",[code:root_dir()]),halt().' | erl | sed 's,^[0-9]*> *,,g' | tail +2)
ERLINC = -I$(ERL_DIR)/usr/include


# Files

COMMON	=	esdl_wrapper esdl_gen esdl_spec esdl_util \
		esdl_video esdl_events esdl_audio \
		esdl_gl esdl_glu esdl_glext

COMMON_OBJ	= $(COMMON:%=%.o)

# Targets

#target: libsdlmain.a $(TARGETDIR)/sdlwrapper $(TARGETDIR)/sdl_driver.so
target:  $(TARGETDIR)/sdl_driver.so


clean:  
	rm -f $(COMMON_OBJ) 
	rm -f ../priv/*.so ../priv/sdlwrapper *.o libsdlmain.a
	rm -f *~ erl_crash.dump

##
##

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

%.o:	%.m
	$(CC) -c -I/Library/Frameworks/SDL.framework/Headers $(CFLAGS) $<


#esdl_main.o :	esdl_main.c
#	$(CC) -c -ObjC $(CFLAGS) $(ERLINC) $<


esdl_driver.o :	esdl_driver.c
	$(CC) -c -ObjC $(CFLAGS) $(ERLINC) $<

libsdlmain.a	: SDLMain.o
	ar cru libsdlmain.a SDLMain.o
	ranlib libsdlmain.a


##
## sdlwrapper

#$(TARGETDIR)/sdlwrapper: esdl_main.o esdl_stream.o $(COMMON_OBJ) 
#	$(CC) $< esdl_stream.o $(COMMON_OBJ) $(LDFLAGS) -o $@

##
## sdl_driver.so

$(TARGETDIR)/sdl_driver.so: esdl_driver.o $(COMMON_OBJ)
	$(CC) $< $(COMMON_OBJ) $(LDFLAGS) -bundle -flat_namespace -undefined suppress -o $@
