#
# Wii Makefile
#

.PHONY: package

ifeq ($(strip $(DEVKITPRO)),)
$(error "DEVKITPRO must be set in your environment.")
endif

ifeq ($(strip $(DEVKITPPC)),)
$(error "DEVKITPPC must be set in your environment.")
endif

AS      := powerpc-eabi-as
CC      := powerpc-eabi-gcc
CXX     := powerpc-eabi-g++
AR      := powerpc-eabi-ar
OBJCOPY := powerpc-eabi-objcopy
ELF2DOL := elf2dol

PORTLIBS ?= $(DEVKITPRO)/portlibs/wii

#
# Wii target rules
#
STRIP  = /bin/true

#
# Override library paths
#

#
# If building with SDLWii, these need to be set manually--no sdl-config.
# libSDL has to be linked at a very specific place in the library list,
# but SDL_LDFLAGS gets appended to the end, so set it to something useless.
#
SDL_CFLAGS     = -isystem $(DEVKITPRO)/libogc/include/SDL
SDL_LDFLAGS    = $(SDL_CFLAGS)

EXTRA_INCLUDES = -isystem $(DEVKITPRO)/libogc/include \
                 -isystem $(DEVKITPRO)/libfat-ogc/include \
                 -isystem $(PORTLIBS)/include
EXTRA_LIBS     = -L$(DEVKITPRO)/libogc/lib/wii \
                 -L$(DEVKITPRO)/libfat-ogc/lib/wii \
                 -L$(PORTLIBS)/lib \
                 $(if $(BUILD_SDL), -lSDL,) \
                 -lfat -lasnd -lwiikeyboard -lwiiuse -lbte -logc -lstdc++ -lm


MACHDEP = -DGEKKO -mrvl -mcpu=750 -meabi -mhard-float

ARCH_CFLAGS   += ${EXTRA_INCLUDES} $(MACHDEP)
ARCH_CXXFLAGS += ${ARCH_CFLAGS}
ARCH_LDFLAGS  += ${EXTRA_LIBS} $(MACHDEP)

package: mzx
	${ELF2DOL} ${mzxrun} boot.dol
	${ELF2DOL} ${mzx} megazeux.dol

clean:
	@rm -f boot.dol megazeux.dol arch/wii/*.d arch/wii/*.o

#
# Vile hack, remove me ASAP
#
arch/wii/%.o: arch/wii/%.c
	$(if ${V},,@echo "  CC      " $<)
	${CC} -MD ${core_cflags} ${core_flags} -c $< -o $@

#
# Need to nest Wii binaries in a subdir
#
build := build/${SUBPLATFORM}/apps/megazeux
build: package ${build}
ifeq ($(BUILD_SDL),)
	${CP} arch/wii/pad.config ${build}
else
	${CP} arch/wii/pad.config.sdl ${build}/pad.config
endif
	${CP} arch/wii/icon.png ${build}
	${CP} boot.dol megazeux.dol ${build}
	@sed "s/%VERSION%/${VERSION}/g;s/%DATE%/`date -u +%Y%m%d%H%M`/g" \
	    arch/wii/meta.xml > ${build}/meta.xml
	${RM} ${build}/${mzxrun} ${build}/${mzxrun}.debug
	${RM} ${build}/${mzx} ${build}/${mzx}.debug

include arch/zip.inc
