# Copyright 2000, International Business Machines Corporation and others.
# All Rights Reserved.
#
# This software has been released under the terms of the IBM Public
# License.  For details, see the LICENSE file in the top-level source
# directory or online at http://www.openafs.org/dl/license10.html
#
srcdir=@srcdir@
include @TOP_OBJDIR@/src/config/Makefile.config

# tell bsd.kmod.mk to build us vnode_if*
SRCS=	vnode_if.h

# We must set SRCS to include at least one .c file if we are also using
# vnode_if.h, due to a bug in the system makefiles where the empty string
# gets expanded to a ".o" entry in OBJS, which there is (correctly) no
# rule to make.  For simplicity, just set this as all of what would
# otherwise be AFS_OS_OBJS so that people have a familiar list to look for.
#
# OS specific "object" files:
SRCS+= \
	osi_crypto.c \
	osi_gcpags.c \
	osi_groups.c \
	osi_file.c \
	osi_inode.c \
	osi_misc.c \
	osi_sleep.c \
	osi_vcache.c \
	osi_vm.c \
	osi_vnodeops.c \
	osi_module.c

AFS_OS_OBJS = \
	rand-fortuna-kernel.o

#AFS_OS_NFSOBJS = \
#	osi_vfsops_nfs.o

AFS_OS_NONFSOBJS = \
	osi_vfsops.o

# System specific build commands and flags
KSRC = @BSD_KERNEL_PATH@
KBLD = @BSD_KERNEL_BUILD@

LIBAFS_REQ_DIRS = $(DESTDIR)$(KMODDIR)/

# keep symbols if --enable-debug-kernel
AC_DEBUG_FLAGS = @DEBUG_FLAGS@
.if !empty(AC_DEBUG_FLAGS)
DEBUG_FLAGS=	${AC_DEBUG_FLAGS}

# For debug builds, we also need /usr/lib/debug/* dirs to exist
LIBAFS_REQ_DIRS += ${DESTDIR}${KERN_DEBUGDIR}${KMODDIR}/
.endif

# We are not doing very well (WARNS=2 brings in printf format type checking)
WARNS= 1
# We have some sketchy code that redeclares prototypes
CWARNFLAGS+= -Wno-redundant-decls

GEN_KBLD_DIR=@abs_builddir@/kconf-GENERIC
AFS_OS_CLEAN=$(GEN_KBLD_DIR)

# Starting with FreeBSD 12, opt_global.h is effectively required to build, even
# for the GENERIC kernel. Normally this would be provided in the kernel build
# dir (--with-bsd-kernel-build). If that dir was not specified, we can still
# get the needed header files if we assume that the user is building for the
# GENERIC kernel. So if we don't have a kernel build dir, generate the header
# files in GEN_KBLD_DIR by using config(8), and specify that as the
# KERNBUILDDIR. This won't always work, of course, but it has a chance at
# working for the most common setups (when the user isn't using some custom
# kernel). The kernel headers, of course, we assume are in $(KSRC).
$(GEN_KBLD_DIR):
	-mkdir $(GEN_KBLD_DIR)
	cd $(KSRC)/$(MACHINE)/conf && \
		/usr/sbin/config -d $(GEN_KBLD_DIR) GENERIC

# setup for bsd.kmod.mk infrastructure
.PATH:	${TOP_SRCDIR}/afs/FBSD
KMODDIR=/boot/modules
KERN_DEBUGDIR=/usr/lib/debug
KMOD=	libafs
SYSDIR= ${KSRC}

.if empty(KBLD)
KERNBUILDDIR=$(GEN_KBLD_DIR)
.else
KERNBUILDDIR=${KBLD}
.endif

# Name of directory to hold object files and libraries.
KOBJ = MODLOAD

# This tells Makefile.common to use it's single directory build target.
COMPDIRS = single_compdir
INSTDIRS = single_instdir
DESTDIRS = single_destdir

# The common sysincludes code needs the 'h' symlink.
# We cannot include bsd.kmod.mk in this file directly, because it will start
# generating headers and objects in this directory, which we want in $(KOBJ).
# We also need the install target from bsd.kmod.mk in $(KOBJ), but the one
# from Makefile.common in this directory.  A prime candidate for cleanup.
setup: $(KERNBUILDDIR)
	-mkdir $(KOBJ)
	-$(RM) $(KOBJ)/Makefile $(KOBJ)/Makefile.common
	$(CP) Makefile $(KOBJ)/Makefile
	echo ".include <bsd.kmod.mk>" >> $(KOBJ)/Makefile
	sed -e 's/^install:/afsinstall:/' Makefile.common > $(KOBJ)/Makefile.common
	-$(RM) -f h
	-ln -fs ${KSRC}/sys h

# Makefile.common sets AFSAOBJS, COMMON_INCLUDE, TOP_{SRC,OBJ}*, and the like.
# We must live with its other pollution of targets and build rules.
include Makefile.common

# we only do the no-NFS case
OBJS=	${AFSAOBJS} ${AFSNONFSOBJS}

$(OBJS): vnode_if.h

LIBAFSNONFS=	libafs.ko
DEFINES= -DAFSDEBUG -DKERNEL -DAFS -DVICE -DNFS -DUFS -DINET -DQUOTA -DGETMOUNT
CFLAGS+= $(DEFINES) ${COMMON_INCLUDE}

INST_LIBAFS = ${DESTDIR}${afskerneldir}/${LIBAFS}
INST_LIBAFSNONFS = ${DESTDIR}${afskerneldir}/${LIBAFSNONFS}

DEST_LIBAFS = ${DEST}/root.client/bin/${LIBAFS}
DEST_LIBAFSNONFS = ${DEST}/root.client/bin/${LIBAFSNONFS}

$(DESTDIR)$(KMODDIR)/:
	$(INSTALL) -d $@

${DESTDIR}${KERN_DEBUGDIR}${KMODDIR}/:
	$(INSTALL) -d $@

install_libafs:	$(LIBAFSNONFS) $(LIBAFS_REQ_DIRS) install

dest_libafs: $(LIBAFSNONFS)
	${INSTALL} -d ${DEST}/root.client/bin
	${INSTALL} -m 644 $(LIBAFSNONFS) $(DEST_LIBAFSNONFS)

# This is ugly, but the common infrastructure needs a libafs target.
libafs: depend $(LIBAFSNONFS)

