#	$NetBSD: Makefile.fat,v 1.9 2018/07/25 23:45:32 kamil Exp $

S=		${.CURDIR}/../../../../..

PROG?=		fatboot
NOMAN=		# defined
NOPIE=		# defined
NOLIBCSANITIZER=# defined
NOSANITIZER=	# defined

LIBCRT0=	# nothing
LIBCRTI=	# nothing
LIBCRTBEGIN=	# nothing
LIBCRTEND=	# nothing
LIBC=		# nothing

AFLAGS.fatboot.S= ${${ACTIVE_CC} == "clang":?-no-integrated-as:}

.include <bsd.init.mk>

STRIPFLAG=	# override

SRCS=		fatboot.S

BINDIR=		/usr/mdec
BINMODE=	444

.PATH:		${.CURDIR}/..

LDFLAGS+=	-nostdlib -Wl,-e,start
CPPFLAGS+=	-I. -I${.CURDIR}/../../lib -I${S}
CPPFLAGS+=	-DFAT_ENTRY_SIZE=${FAT_ENTRY_SIZE}

.if ${MACHINE_ARCH} == "x86_64"
LDFLAGS+=	-Wl,-m,elf_i386
AFLAGS+=	-m32
.endif

BUILDSYMLINKS+=	$S/arch/i386/include	machine \
		$S/arch/x86/include	x86

DPSRCS+=	machine x86

CLEANFILES+=	${PROG}.tmp

${PROG}: ${OBJS}
	${_MKTARGET_LINK}
	${CC} -o ${PROG}.tmp ${LDFLAGS} -Wl,-Ttext,0x7c00 ${OBJS}
	@ set -- $$( ${NM} -t d ${PROG}.tmp | grep '\<pbr_space\>' \
		    | ${TOOL_SED} 's/^0*//'  ); \
		echo "#### There are $$1 free bytes in ${PROG}"
	${OBJCOPY} -O binary ${PROG}.tmp ${PROG}
	rm -f ${PROG}.tmp

.include <bsd.prog.mk>