#
# OS X makefile generics
#

STRIP   ?= strip

CROSS_COMPILE_X86_CC  ?= i686-apple-darwin10-gcc-4.2.1
CROSS_COMPILE_X86_CXX ?= i686-apple-darwin10-g++-4.2.1
CROSS_COMPILE_PPC_CC  ?= powerpc-apple-darwin10-gcc-4.2.1
CROSS_COMPILE_PPC_CXX ?= powerpc-apple-darwin10-g++-4.2.1

ifeq (${ARCH},)
ARCH := i686
endif

ifeq (${ARCH},i686)
CC        := ${CROSS_COMPILE_X86_CC}  -mmacosx-version-min=10.6
CXX       := ${CROSS_COMPILE_X86_CXX} -mmacosx-version-min=10.6
REAL_ARCH := i686
else

ifeq (${ARCH},amd64)
CC        := ${CROSS_COMPILE_X86_CC}  -m64 -mmacosx-version-min=10.6
CXX       := ${CROSS_COMPILE_X86_CXX} -m64 -mmacosx-version-min=10.6
REAL_ARCH := amd64
else

ifeq (${ARCH},ppc)
CC        := ${CROSS_COMPILE_PPC_CC}  -mmacosx-version-min=10.4
CXX       := ${CROSS_COMPILE_PPC_CXX} -mmacosx-version-min=10.4
REAL_ARCH := ppc
else

ifeq (${ARCH},ppc64)
CC        := ${CROSS_COMPILE_PPC_CC}  -m64 -mmacosx-version-min=10.4
CXX       := ${CROSS_COMPILE_PPC_CXX} -m64 -mmacosx-version-min=10.4
REAL_ARCH := ppc64
else

REAL_ARCH := $(error Invalid architecture selection)
endif
endif
endif
endif

DSOLDFLAGS = -dynamiclib
DSOPRE     = lib
DSOPOST    = .dylib
DSORPATH   =
DSOSONAME  = -install_name @executable_path/

ifeq (${BUILD_LIBSDL2},)
SDL_CFLAGS  ?= $(shell ${PREFIX}/bin/sdl-config --cflags)
SDL_LDFLAGS ?= $(shell ${PREFIX}/bin/sdl-config --libs)
else
SDL_CFLAGS  ?= $(shell ${PREFIX}/bin/sdl2-config --cflags)
SDL_LDFLAGS ?= $(shell ${PREFIX}/bin/sdl2-config --libs)
endif

LIBPNG_CFLAGS  ?= $(shell ${PREFIX}/bin/libpng-config --cflags)
LIBPNG_LDFLAGS ?= $(shell ${PREFIX}/bin/libpng-config --ldflags)

arch/darwin/SDLMain.o: arch/darwin/SDLMain.m
	$(if ${V},,@echo "  CC      " $<)
	${CC} -MD ${CFLAGS} ${SDL_CFLAGS} -c $< -o $@

package: all
	${MV} ${core_target} ${core_target}.${REAL_ARCH}
	${MV} ${editor_target} ${editor_target}.${REAL_ARCH}
	${MV} ${mzx} ${mzx}.${REAL_ARCH}
	${MV} ${mzxrun} ${mzxrun}.${REAL_ARCH}

clean:
	${RM} arch/darwin/SDLMain.o arch/darwin/SDLMain.d

package_clean: clean
	${RM} ${core_target}.${REAL_ARCH}
	${RM} ${editor_target}.${REAL_ARCH}
	${RM} ${network_target}.${REAL_ARCH}
	${RM} ${mzx}.${REAL_ARCH}
	${RM} ${mzxrun}.${REAL_ARCH}

lipo:
	@arch/darwin/lipo.sh

dist:
	@${CP} platform.inc platform.inc.orig
	@sed 's,blah,${HOME}/workspace/build-i686,g' \
	     platform.inc.orig >platform.inc
	@${MAKE} ARCH=i686 package
	@${MAKE} ARCH=i686 clean
	@sed 's,blah,${HOME}/workspace/build-powerpc,g' \
	     platform.inc.orig >platform.inc
	@${MAKE} ARCH=ppc package
	@${MAKE} ARCH=ppc clean
	@${MAKE} lipo

build := build/${SUBPLATFORM}/MegaZeux.app/Contents/Resources
build:
	${MKDIR} ${build}/../MacOS
	${CP} arch/darwin/MegaZeux.plist ${build}/../Info.plist
	${CP} contrib/icons/quantump.icns ${build}/MegaZeux.icns
	${RM} ${build}/*.debug ${build}/utils/*.debug
	${MV} ${build}/docs ${build}/../../..
	${MV} ${build}/LICENSE ${build}/../../../docs/
	${MV} ${build}/${mzx} ${build}/../MacOS/MegaZeux
	${MV} ${build}/${mzxrun} ${build}/../MacOS/MZXRun
	${MV} ${build}/*.dylib ${build}/../MacOS
	${LN} -s MegaZeux.app/Contents/Resources/config.txt ${build}/../../..
	${MKDIR} -p ${build}/../../../MZXRun.app/Contents
	${CP} arch/darwin/MZXRun.plist \
		${build}/../../../MZXRun.app/Contents/Info.plist
	${LN} -s ../../MegaZeux.app/Contents/Resources \
		${build}/../../../MZXRun.app/Contents/Resources
	${LN} -s ../../MegaZeux.app/Contents/MacOS \
		${build}/../../../MZXRun.app/Contents/MacOS

archive: build
	@arch/darwin/dmg.sh ${TARGET}
