#
# OS X makefile generics
#

DSOLDFLAGS = -dynamiclib
DSOPRE     = lib
DSOPOST    = .dylib
DSORPATH   =
DSOSONAME  = -install_name ${LIBDIR}/

STRIP   ?= strip

#
# darwin specific recipes.
# Enable 'make install' and 'make uninstall' for darwin builds.
#

ifeq (${SUBPLATFORM},darwin)

include arch/install.inc

# No platform-specific files to install
install-arch:
uninstall-arch:

endif

#
# darwin-dist specific recipes.
# These recipes are intended to produce portable Mac OS X .apps.
# This setup is extremely old and for Intel Macs has been superceded
# by the Xcode project folder (see: arch/xcode). However, this is still
# required to build PowerPC Mac .apps.
#

ifeq (${SUBPLATFORM},darwin-dist)

.PHONY: dist lipo package_clean

# Make dylib path the same as the executable path instead
DSOSONAME  = -install_name @executable_path/

# Override these to determine which builds are included in the .app.
PREFIX_I686   ?=
PREFIX_AMD64  ?=
PREFIX_PPC    ?=
PREFIX_PPC64  ?=

HAS_PREFIX=1
ifeq (${PREFIX_I686},)
ifeq (${PREFIX_AMD64},)
ifeq (${PREFIX_PPC},)
ifeq (${PREFIX_PPC64},)
HAS_PREFIX=0
endif
endif
endif
endif

# Override these to match the compiler(s) being used.
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

package: all
	${MV} ${mzx} ${mzx}.${REAL_ARCH}
ifneq (${BUILD_MZXRUN},)
	${MV} ${mzxrun} ${mzxrun}.${REAL_ARCH}
endif
ifneq (${BUILD_MODULAR},)
	${MV} ${core_target} ${core_target}.${REAL_ARCH}
	${MV} ${editor_target} ${editor_target}.${REAL_ARCH}
endif

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

lipo:
	@arch/darwin/lipo.sh

dist:
ifeq (${HAS_PREFIX},0)
	$(error No dist target prefixes defined)
endif
ifneq (${PREFIX_I686},)
	@${MAKE} PREFIX=${PREFIX_I686} ARCH=i686 package
	@${MAKE} PREFIX=${PREFIX_I686} ARCH=i686 clean
endif
ifneq (${PREFIX_AMD64},)
	@${MAKE} PREFIX=${PREFIX_AMD64} ARCH=amd64 package
	@${MAKE} PREFIX=${PREFIX_AMD64} ARCH=amd64 clean
endif
ifneq (${PREFIX_PPC},)
	@${MAKE} PREFIX=${PREFIX_PPC} ARCH=ppc package
	@${MAKE} PREFIX=${PREFIX_PPC} ARCH=ppc clean
endif
ifneq (${PREFIX_PPC64},)
	@${MAKE} PREFIX=${PREFIX_PPC64} ARCH=ppc64 package
	@${MAKE} PREFIX=${PREFIX_PPC64} ARCH=ppc64 clean
endif
	@${MAKE} lipo

mzx_app := build/${SUBPLATFORM}/MegaZeux.app/
mzxrun_app := build/${SUBPLATFORM}/MZXRun.app/

build := ${mzx_app}/Contents/Resources
build:
	${RM} ${build}/*.debug ${build}/utils/*.debug
	${MKDIR}                          ${mzx_app}/Contents/MacOS
	${CP} contrib/icons/quantump.icns ${mzx_app}/Contents/Resources/MegaZeux.icns
	${MV} ${build}/LICENSE            ${build}/docs/
	${MV} ${build}/docs               ${mzx_app}/../
	${MV} ${build}/${mzx}             ${mzx_app}/Contents/MacOS/MegaZeux
ifneq (${BUILD_MZXRUN},)
	${MV} ${build}/${mzxrun}          ${mzx_app}/Contents/MacOS/MZXRun
endif
ifneq (${BUILD_MODULAR},)
	${MV} ${build}/${core_target}     ${mzx_app}/Contents/MacOS/
	${MV} ${build}/${editor_target}   ${mzx_app}/Contents/MacOS/
endif
ifneq (${BUILD_MZXRUN},)
	${CP} -r ${mzx_app} ${mzxrun_app}
	${RM} ${mzx_app}/Contents/MacOS/MZXRun
	${RM} ${mzxrun_app}/Contents/MacOS/MegaZeux
ifneq (${BUILD_MODULAR},)
	${RM} ${mzxrun_app}/Contents/MacOS/${editor_target}
endif
	${CP} arch/darwin/MZXRun.plist    ${mzxrun_app}/Contents/Info.plist
endif
	${CP} arch/darwin/MegaZeux.plist  ${mzx_app}/Contents/Info.plist

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

endif # darwin-dist
