# Makefile for drbd.o
#
# This file is part of drbd by Philipp Reisner
#
# drbd is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2, or (at your option)
# any later version.
#
# drbd is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with drbd; see the file COPYING.  If not, write to
# the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
#

include ../Makefile.vars

KSUBD := $(strip \
$(if $(wildcard /lib/modules/$(KERNVER)/kernel),\
  kernel/drivers/block,\
  block))

REQ_O = $(shell \
  if test "$(MAJOR)" -lt "2" \
  || test "$(MAJOR)" -eq "2" -a "$(MINOR)" -lt "4" ;\
  then echo "drbd_req-2.2.o"; \
  else echo "drbd_req-2.4.o"; fi)

OBJS=drbd_fs.o drbd_main.o drbd_proc.o drbd_receiver.o drbd_syncer.o $(REQ_O)

####### Implicit rules

.SUFFIXES: .c .o

.c.o:
	$(CC) -c $(KERNFLAGS) $(VERSIONFLAGS) $<

#######

# error, if we have no kernel source tree
define ERR_NO_KERNEL_SOURCE

Could not find kernel sources.
  Install them, or tell me where to find them with
  make LINUX=/path/to/kernel/source
endef
ifeq ($(LINUX),)
  $(error $(ERR_NO_KERNEL_SOURCE)))
endif
ifneq ($(shell cd $(LINUX) 2>&1),)
  $(error $(LINUX): no such directory. $(ERR_NO_KERNEL_SOURCE))
endif

# which version is configured in the kernel source tree
# just "make" it, in case it is out of date.
CONFIGURED_KERNVER := $(shell \
 cd $(LINUX) && $(MAKE) include/linux/version.h > /dev/null 2>&1 && \
 sed -e 's/[^"]*"//;s/".*//; 1q' include/linux/version.h)
# which version the current drbd.o was built for
COMPILED_KERNVER := $(shell test -e .kversion && cat .kversion)

define ERR_KERNEL_VERSION_MISMATCH
Version of running kernel and kernel config differ:
  $(KERNVER) <uname -r or specified on command line>
  $(CONFIGURED_KERNVER) <$(LINUX)/include/linux/version.h>
  You may want to reconfigure to match the running kernel, or
  make KERNVER=2.4.XY LINUX=/path/to/kernel/source 
  to override this
endef

# error, if running or commandline specified kernel version
# and version from the kernel source config do not match
ifneq ($(CONFIGURED_KERNVER),$(KERNVER))
  $(error $(ERR_KERNEL_VERSION_MISMATCH))
endif

NEEDNEW := $(shell \
	if test -e .kversion \
	&& test "$(CONFIGURED_KERNVER)" = "`cat .kversion`"; \
	then \
		true; \
	else \
		echo clean; \
	fi)


#######

all: $(NEEDNEW) drbd.o 

drbd.o: $(OBJS)
	$(LD) -r -o $@ $^
	@echo $(CONFIGURED_KERNVER) >.kversion

clean:
	rm -f *.o
	rm -f *~


install: $(if $(COMPILED_KERNVER),,all)
ifneq ($(COMPILED_KERNVER),$(KERNVER))
	@echo
	@echo "kernel version mismatch:"
	@echo "  current drbd.o compiled for: $(COMPILED_KERNVER)"
	@echo "  requested kernel version:    $(KERNVER)"
	@echo "either make all && make install, or override with"
	@echo "make KERNVER=$(COMPILED_KERNVER)"
	@echo
	@false
endif
	install -d $(PREFIX)lib/modules/$(KERNVER)/$(KSUBD)
	install -m 644 drbd.o $(PREFIX)lib/modules/$(KERNVER)/$(KSUBD)
	install -d $(PREFIX)var/lib/drbd
	/sbin/depmod -a || true

uninstall:
	rm $(PREFIX)lib/modules/$(KERNVER)/$(KSUBD)/drbd.o
	/sbin/depmod -a || true

### dependencies:

$(OBJS): drbd_int.h drbd.h

