MAMBA_VER = 4_55
ZLIBUTIL = ./zlibutil.exe

CC		:= ppu-gcc
CXX		:= ppu-g++
LD		:= ppu-ld 
OBJCOPY		:= ppu-objcopy
OBJDUMP		:= ppu-objdump
AR 		:= ppu-ar
STRIP		:= ppu-strip
INCLUDE		= $(PS3DEV)/ppu/ppu/include lv2/include lv1/include
LIBS           	=
BUILD_TYPE     	= 
GCC_INCLUDE_DIR = lv2/include lv1/include

CFLAGS = -ffunction-sections -fdata-sections -O2 -m64 -fno-builtin -fno-exceptions \
			   -Wall -Wno-strict-aliasing -Wno-multichar $(foreach dir,$(INCLUDE),-I$(dir)) \
			   -DLV2 -DUSE_LV1_PEEK_POKE -DFIRMWARE_$(MAMBA_VER) -ffreestanding
CFLAGS += --std=gnu99

LDFLAGS= -T stage2.ld -nostartfiles -nostdlib -nodefaultlibs -Wl,-static -L. $(foreach dir,$(LIBSDIR),-L$(dir)) $(LIBS) \
	 -Wl,--gc-sections -Wl,-Map=stage2.map

OBJS = stage2/start.o stage2/main.o stage2/storage_ext.o stage2/crypto.o stage2/modulespatch.o stage2/config.o stage2/region.o \
	lv2/src/patch.o lv2/src/interrupt.o lv2/src/interrupt_c.o lv2/src/io.o lv2/src/libc.o \
	lv2/src/libc_c.o lv2/src/memory.o lv2/src/memory_c.o lv2/src/thread.o lv2/src/thread_c.o lv2/src/process.o \
	lv2/src/synchronization.o lv2/src/modules.o lv2/src/modules_c.o lv2/src/storage.o lv2/src/object.o \
	lv2/src/security.o lv2/src/time.o  lv2/src/syscall.o \
	lv1/src/hvcall.o lv1/src/stor.o lv1/src/device.o 

all: mamba_$(MAMBA_VER).bin

objdump: stage$(MAMBA_VER).bin
	$(OBJDUMP) -D -EB -b binary -m powerpc:common64 mamba_$(MAMBA_VER).bin

%.bin: %.elf
	$(OBJCOPY) -O binary $< $@	
	$(ZLIBUTIL) mamba_$(MAMBA_VER).bin mamba_$(MAMBA_VER).lz.bin

mamba_$(MAMBA_VER).elf: $(OBJS) stage2.ld
	$(CC) $(LDFLAGS) -o $@ $(OBJS)

%.o: %.S
	$(CC) $(CFLAGS) -c -o $@ $<

%.o: %.c
	$(CC) $(CFLAGS) -c -o $@ $<

clean:
	rm -f *.o *.elf *.map stage2/*.o lv2/src/*.o lv1/src/*.o 
