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 =
ifneq ($(wildcard $(UPX_LZMADIR)/C/7zip/.),)
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 default
lzma_d_c%.S : tc_bfdname = elf32-m68k

c := tc.method-lzma.gcc
$c  = $(tc.m68k-atari.tos.gcc)
$c += -fpic -mpcrel
$c += -Os -fomit-frame-pointer
$c += -fno-unit-at-a-time
$c += -ffunction-sections
$c += -fwrapv
$c += -I$(UPX_LZMADIR)
$c += -I$(top_srcdir)/src

lzma_d_c%.S : lzma_d_c.c
	$(call tc,gcc) $(PP_FLAGS) -S $< -o tmp/$T.s
	cat tmp/$T.s | $(BLDEL) | sed -e '1,/LzmaDecode:/d' -e '/\.size/d' -e '/\.ident/d' -e '/\.section/d' -e 's/\.L/$(LABEL_PREFIX)/g' -e '/^[ $(tab)]rts.*/d' -e 's/bsr\.l/bsr.w/' | $(BLDEL) > $@
	# the following stuff is for debugging only
	$(call tc,gcc) -c -MF /dev/null $@ -o tmp/$T.o
	$(call tc,f-objstrip,tmp/$T.o)
	$(call tc,objcopy) -O binary tmp/$T.o tmp/$T.bin
	head -c-0 tmp/$T.bin > tmp/$T.out
	$(call tc,objdump) -b binary -m m68k -D tmp/$T.out | $(RTRIM) > tmp/$T.out.disasm

lzma_d_cf.% : PP_FLAGS = -DFAST
lzma_d_cs.% : PP_FLAGS = -DSMALL
lzma_d_cf.% : LABEL_PREFIX = .Lf
lzma_d_cs.% : LABEL_PREFIX = .Ls
