########################################################################
#
# The Makefile for the Simple DirectMedia Layer library
#
# This makefile requires the GNU version of 'make'
# Available at:  ftp://prep.ai.mit.edu/gnu/make/
#
########################################################################

# Determine the library version
version = $(shell cat .version)

# Determine the target platform
ifeq ($(target), )
target := $(shell cat .target 2>/dev/null)
endif
ifeq ($(target), )
target := $(shell uname | tr '[A-Z]' '[a-z]')
ifeq ($(findstring irix,$(target)), irix)
target := irix
endif
ifeq ($(findstring cygwin,$(target)), cygwin)
target := win32
endif
endif

# Set the default flags
STATIC_LIB = lib/libSDL.a
OBJ=obj

# Include the system dependent configuration
include .config
include make/$(target)/config

config:
	@sh make/Configure.sh $(target)

.PHONY: linux win32 beos
linux win32 beos:
	@$(MAKE) distclean >/dev/null; $(MAKE) target=$@

# Compiler flags
CFLAGS = $(OPTIMIZE) -D_BUILDING_SDL $(OPTIONS) $(ARCH_FLAGS) $(INCLUDES)
ifeq ($(static), true)
CFLAGS += -D_SDL_STATIC_LIB
endif
CXXFLAGS = $(CFLAGS)

# Set the directory search paths (GNU make magic)
override VPATH += src:src/hermes:src/stub:src/stub/$(target)
ifeq ($(ARCH_VPATH), )
ARCH_VPATH = src/$(target)
endif
override VPATH += $(ARCH_VPATH)
CFLAGS += -Iinclude $(patsubst %,-I%,$(subst :, ,$(VPATH)))

# Portable object files
PORTABLE_OBJS = $(patsubst %.c,obj/%.o,$(notdir $(wildcard src/*.c)))

# Objects for the static library
ifeq ($(static), true)
STATIC_OBJS = $(PORTABLE_OBJS) $(STATIC_ARCH_OBJS) $(ASM_OBJS)
else
STATIC_OBJS = $(patsubst %.c,obj/stub/%.o,$(notdir $(wildcard src/stub/*.c))) \
    $(patsubst %.c,obj/stub/%.o,$(notdir $(wildcard src/stub/$(target)/*.c)))
endif

# Dump object files into the 'obj' subdirectory
obj/%.o: %c
	$(CC) -c $(CFLAGS) $< -o $@

# Rule for building the static library
$(STATIC_LIB): $(STATIC_OBJS)
	$(AR) $(ARFLAGS) $@ $^
	$(RANLIB) $@

# Include the rules for building the rest of the libraries
include make/$(target)/rules

# Rule for building a static version of SDL
static:
	$(MAKE) clean
	rm -f $(STATIC_LIB)
	$(MAKE) static=true

# Rule for building test programs
tests:
	cd test && $(MAKE)

# Rule for installing SDL on the system
install:
	sh bin_dist/install-sdl.sh

# Rule for building a binary distribution
bindist:
	@if [ "$(target)" = "" ]; then \
		echo "No target specified -- run 'make $@ target=XXX'"; \
		exit 1; \
	fi
	@if [ ! -f .version ]; then \
		echo "What version of SDL is this?  No .version file!"; \
		exit 1; \
	fi
	dist="SDL-`cat .version`" && \
		mkdir $$dist; \
		cp -r bin_dist/* $$dist; \
		tar cf - \
		   README COPYING BUGS CREDITS docs.html docs include lib | \
		(cd $$dist; tar xpf -); \
		rm -rf `find $$dist -name CVS -print`; \
		tar cvf ../$$dist-$(target).tar $$dist; \
		gzip -v ../$$dist-$(target).tar; \
		rm -rf $$dist

# Rule for generating export lists from the header files
exports:
	make -C src/stub
	make -C src/win32/exports
	make -C src/beos/exports
	make -C src/macos/exports

# Rule for cleaning up the distribution
clean:
	rm -rf obj
	$(MAKE) -C test $@

distclean: clean
	rm -rf lib
	cp .config-dist .config
	rm -f .target
	$(MAKE) -C test $@

# We aren't allowed to redistribute the DirectX headers ..
spotless: distclean
	$(MAKE) -C directx $@
