#
# GNU Makefile to build the ADB3 Driver, including API shared libraries
#
# (C) Copyright 2010, 2011, 2026 Alpha Data
#
# Available targets:
# ------------------
#
#   all
#   Builds the kernel module and API shared libraries
#
#   clean
#   Removes the compiled kernel module, API shared libraries and any
#   intermediate build products.
#
#   install
#   Installs the kernel module and API shared libraries into the appropriate
#   system directories. Must be done when logged in as root or by running
#   "make" using "sudo".
#
#   uninstall
#   Uninstalls the kernel module and API shared libraries from the appropriate
#   system directories. Must be done when logged in as root or by running
#   "make" using "sudo".
#
# The following variables can be passed on the make command line:
#
#   BIARCH
#   If value is yes, builds for bi-archtecture 64-bit system (i.e. both 32-bit
#   and 64-bit binaries are created).
#
#   SYSROOT
#   If specified, is the root of the target filesystem; typically used when
#   cross-building.
#
#   CROSS_COMPILE
#   Prefix before tools, e.g. arm-linux-; typically used when cross-building.
#   If cross-building, the target platform's toolchain must be in the PATH.
#
#   KERNEL
#   Overrides the path to the Linux kernel header files. Should be used in
#   order to build the kernel module for a kernel other than the currently
#   running one. This is usually required when cross-compiling. For example:
#   
#     make KERNEL=/lib/modules/4.15.10-300.fc27.x86_64 clean all
#
#   KERNEL_BUILD
#   As an alternative to setting KERNEL, overrides the path to the Linux kernel
#   sources when /lib/modules/<kernel version> does not exist (e.g. you have
#   not yet installed the corresponding kernel modules package). For example:
#
#     make KERNEL_BUILD=/usr/src/kernels/5.14.0-503.23.2.el9_5.x86_64 clean all
#
#   WARNING: Do not use the "install" target with this option, because the
#   module will likely not be installed into the correct modules directory.
#
#   AUTO_START
#   If value is yes, causes the module device table to be compiled in to the
#   driver's binary, and the driver will auto-start during boot, assuming that
#   hardware that it recognizes is present.
#
#   WARNING: If the PCIe interface of your FPGA design has not been fully
#   debugged, this may render the system unbootable.
#

APIDIRS = api/adb3/linux api/modules/admxrc3/linux

MODDIR = driver/monolithic/linux

SUBDIRS = $(APIDIRS) $(MODDIR)

SUBDIRS_CLEAN = $(SUBDIRS:%=clean-%)

SUBDIRS_INSTALL = $(SUBDIRS:%=install-%)

SUBDIRS_DKMS_INSTALL = $(SUBDIRS:%=dkms_install-%)

SUBDIRS_UNINSTALL = $(SUBDIRS:%=uninstall-%)

.PHONY: all clean install dkms_install uninstall $(SUBDIRS) $(SUBDIRS_CLEAN) $(SUBDIRS_INSTALL) $(SUBDIRS_UNINSTALL)

all: $(SUBDIRS)

clean: $(SUBDIRS_CLEAN)

# This requires root privileges
install: $(SUBDIRS_INSTALL)

# This requires root privileges
dkms_install: $(SUBDIRS_DKMS_INSTALL)

# This requires root privileges
uninstall: $(SUBDIRS_UNINSTALL)

$(SUBDIRS):
	$(MAKE) -C "$@"

$(SUBDIRS_CLEAN): 
	$(MAKE) -C "$(@:clean-%=%)" clean

# This requires root privileges
$(SUBDIRS_INSTALL): 
	$(MAKE) -C "$(@:install-%=%)" install

# This requires root privileges
$(SUBDIRS_DKMS_INSTALL): 
	$(MAKE) -C "$(@:dkms_install-%=%)" dkms_install

# This requires root privileges
$(SUBDIRS_UNINSTALL): 
	$(MAKE) -C "$(@:uninstall-%=%)" uninstall
