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 lzma_d_cn.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 amd64-linux.elf default
lzma_d_c%.S : tc_bfdname = elf64-x86-64

c := tc.method-lzma.gcc
$c  = $(tc.amd64-linux.elf.gcc)
$c += -Os -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

lzma_d_c%.S : lzma_d_c.c
	$(call tc,gcc) $(PP_FLAGS) -c $< -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-1 tmp/$T.bin > tmp/$T.out
	$(call tc,objdump) -b binary -m i386:x86-64 -D tmp/$T.out | $(RTRIM) > tmp/$T.out.disasm
	$(call tc,bin2h) --mode=gas tmp/$T.out $@

lzma_d_cf.% : PP_FLAGS = -DFAST
lzma_d_cs.% : PP_FLAGS = -DSMALL
lzma_d_cn.% : PP_FLAGS = -DFAST -mno-red-zone
