# build lzma_d_cf.S and lzma_d_cs.S

MAKEFLAGS += -rR
.SUFFIXES:
.SECONDEXPANSION:

ifndef top_srcdir
__dir_list   = . .. ../.. ../../.. ../../../.. ../../../../..
__dir_list  += ../../../../../.. ../../../../../../.. ../../../../../../../..
__dir_search = $(firstword $(foreach v,$1,$(if $(wildcard $v/$2),$v)) $3)
top_srcdir := $(call __dir_search,$(__dir_list),src/bele.h,NOT_FOUND)
endif
include $(wildcard $(top_srcdir)/Makevars.global ./Makevars.local)
vpath %.c $(top_srcdir)/src/stub/src/c

STUBS =
include $(top_srcdir)/src/stub/src/c/Makevars.lzma
ifneq ($(UPX_LZMA_VERSION),)
STUBS += lzma_d_cf.S lzma_d_cs.S
endif

default.targets = all
ifeq ($(strip $(STUBS)),)
STUBS = NO_STUBS
all.targets =
endif
include $(top_srcdir)/src/stub/Makefile


# /***********************************************************************
# // method-lzma
# ************************************************************************/

lzma_d_c%.S : tc_list = method-lzma powerpc64le-linux.elf default
lzma_d_c%.S : tc_bfdname = elf64-powerpcle

c := tc.method-lzma.gcc
$c  = $(tc.powerpc64le-linux.elf.gcc)
$c += -fPIC
$c += -fomit-frame-pointer
$c += -fno-unit-at-a-time
$c += -ffunction-sections
$c += -fwrapv
$c += -DWITH_LZMA=$(UPX_LZMA_VERSION)
$c += -I$(UPX_LZMADIR)
$c += -I$(top_srcdir)/src
$c += -Wno-error

lzma_d_c%.S : lzma_d_c.c
	$(call tc,gcc) $(PP_FLAGS) -S $< -o tmp/$T.s
	sed "/b *_restgpr0_/a\
#include \"stub/src/arch/powerpc/64le/ppc_regs.h\"\n\
#include \"stub/src/powerpc64-savegpr0.S\"" <tmp/$T.s > tmp/$T.S
	$(call tc,gcc) $(PP_FLAGS) -c tmp/$T.S -o tmp/$T.o
	$(call tc,f-objstrip,tmp/$T.o)
	$(call tc,objcopy) -O binary --only-section .text.LzmaDecode tmp/$T.o tmp/$T.bin
	head -c-4 tmp/$T.bin > tmp/$T.out
	$(call tc,objdump) -b binary -m powerpc -D tmp/$T.out | $(RTRIM) > tmp/$T.out.disasm
	$(call tc,bin2h) --mode=gas tmp/$T.out $@

lzma_d_cf.% : PP_FLAGS = -DFAST  -O2
lzma_d_cs.% : PP_FLAGS = -DSMALL -Os
