#$FreeBSD$

# Automatically enable multi-job make if it wasn't specified. To disable
# running in parallel, the caller may pass -j1.
.if !defined(.MAKE.JOBS)
.MAKE.JOBS != sysctl -n hw.ncpu
.endif

#Ensure that pass the maximum number of jobs to all recursive Make processes
MAKE += -j $(.MAKE.JOBS)

KMODDIR ?= /boot/kernel

# Attempt to locate the running kernel build directory using the
# kern.version sysctl. This enables reading the kernel configuration,
# including things like INVARIANTS, RSS, INET, DTRACE, and other optional
# features.
#
# Without this, the module is build purely from the source with "default"
# options that may not match the running kernel.
#
# To override this, specify the KERNBUILDDIR manually on the command line.
.if !defined(KERNBUILDDIR)
KERNBUILDDIR := ${:!sysctl -n kern.version!:[-1]:C/^.*://}
.if !(exists(${KERNBUILDDIR}) && \
      exists(${KERNBUILDDIR}/Makefile) && \
      exists(${KERNBUILDDIR}/opt_global.h))
.info Unable to locate the running kernel build directory. Kernel options may not be accurate.
.undef KERNBUILDDIR
.endif
.endif

THIS_FILE := $(.PARSEDIR)/$(.PARSEFILE)

.include <bsd.own.mk>
.include <bsd.compiler.mk>

.PATH: ${.CURDIR}

KMOD = if_ice
SRCS != ls *.[ch]
SRCS += device_if.h bus_if.h pci_if.h

# kernel option files
SRCS += opt_inet.h opt_inet6.h opt_rss.h ifdi_if.h

# RDMA interface files,
.if exists(irdma_if.m) && exists(irdma_di_if.m)
  SRCS += irdma_if.c irdma_if.h irdma_di_if.c irdma_di_if.h
.endif

SRIOV_ENABLE ?= 1

KERNVER != uname -K
NO_IFLIB != test $(KERNVER) -ge 1100110; echo $$?
ADD_SRIOV != test $(KERNVER) -lt 1100122; echo $$?

.if $(SRIOV_ENABLE) && $(ADD_SRIOV)
  CFLAGS += -DPCI_IOV
  SRCS += pci_iov_if.h
.else
  SRCS := ${SRCS:N*iov*}
.endif
SRCS := ${SRCS:O:u}

# Allow driver to set its own queue indices on (14.0+) iflib versions
# CFLAGS += -DIFLIB_FEATURE_QUEUE_SELECT_V2

CFLAGS += -Wformat=2 -Wextra -Wno-missing-field-initializers

# Mark some include files as system headers, to ignore warnings we can't fix
CFLAGS += --system-header-prefix=net --system-header-prefix=sys

DEBUG_FLAGS = -g3 -gdwarf-4

.if ${COMPILER_TYPE} == "clang"
.if ${COMPILER_VERSION} >= 60000
DEBUG_FLAGS += -fdebug-macro
.endif
.endif

clean:
	rm -f opt_*.h *_if.h *_if.c setdef* *_StripErr
	rm -f *.o *.kld *.ko *.ko.* .depend.* *.profraw
	rm -f @ export_syms machine x86
.if exists(./ddp)
	$(MAKE) -C ddp $@
.endif

# Build the package module as well
.if exists(./ddp)
all:
	$(MAKE) -C ddp $@

# after the installation, also install the package
afterinstall:
	$(MAKE) -C ddp install
.endif

.include <bsd.kmod.mk>

legacy:
	$(MAKE) -f $(THIS_FILE) LEGACY_ENABLE=1
