# -*- 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

# or 
UNIVERSAL_FLAGS = -arch i386
CC = cc

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

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

LDFLAGS = -F/Library/Frameworks -framework SDL -framework IOKit -framework Carbon -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

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

# Targets

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_driver.o :	esdl_driver.c
	$(CC) -c -ObjC $(CFLAGS) $(ERLINC) $<

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

##
## sdl_driver.so

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