# makefile for drbd for linux 2.4 // 2.6
#
# This file is part of drbd by Philipp Reisner
# By Lars Ellenberg.
#
# 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.
#

# usage: make [ KDIR=/path/to/kernel/source ]
#
# this file is read twice:
# the first invocation calls out to the toplevel Makefile in the
# kernel source tree, which then in turn will call this file again
# as subdir Makefile, with all appropriate vars and macros set.
#
# note: if you get strange make errors when ARCH=um, you
# probably need to "make mrproper" in the KDIR first... 

ifneq ($(KERNELRELEASE),)
  ifneq ($(VERSION),2)
    $(error "won't compile with this kernel version")
  endif

  # linux 2.6 style:
  obj-m     += drbd.o
  drbd-objs := drbd_fs.o drbd_main.o drbd_proc.o drbd_receiver.o drbd_syncer.o
  drbd-objs += drbd_req-2.$(PATCHLEVEL).o

  # rest is magic for KERNEL_VERSION < 2.6
  ifneq ($(findstring $(PATCHLEVEL),24),)
    # linux 2.2//2.4 style needs in addition:

    list-multi := drbd.o

    ifeq ($(PATCHLEVEL),2)
      include $(SUBDIRS)/Rules-2.2.x.make
      # this rules file extracts the 2.2 variables
      # from the 2.4+ obj-[ynm] ones
    else
      include $(TOPDIR)/Rules.make
    endif

    drbd.o: $(drbd-objs)
	echo $(KERNELRELEASE) > .kernelrelease
	$(LD) -r -o $@ $(drbd-objs)

  else
    # we only compile for 2.2 and 2.4. sorry.
    $(error "won't compile with this kernel version ($(KERNELRELEASE))")
  endif
else
  # called from command line in current directory
  PWD  := $(shell pwd)

  # to be overridden on command line:
  PREFIX := /
  ifeq ($(wildcard ../build-for-uml),../build-for-uml)
    #### for Philipp's convenience :) 
    ARCH_UM := "ARCH=um"
    KDIR := /usr/src/linux-um
  else
    KDIR := /lib/modules/$(shell uname -r)/build
  endif

  .PHONY: drbd.o default all greeting clean kbuild install dep

  drbd.o: kbuild
  default: drbd.o
  all:     drbd.o

  # for the install target. (also works if kernel source is not there)
  KERNELRELEASE := $(shell test -r .kernelrelease && \
                           sed 's/-1um//' < .kernelrelease)

  MODSUBDIR := $(strip \
    $(if $(wildcard /lib/modules/$(KERNELRELEASE)/kernel),\
         kernel/drivers/block,\
         block))
  LINUX := $(wildcard /lib/modules/$(KERNELRELEASE)/build)


  greeting:
	@echo "" ;\
	echo "    Calling toplevel makefile of kernel source tree, which I believe is in" ;\
	echo "    KDIR=$(KDIR)"; echo ""

  kbuild: greeting
	$(MAKE) -C $(KDIR) SUBDIRS=$(PWD) $(ARCH_UM) modules
	@echo -n "Memorizing module configuration ... "
	@{ echo -e "#\n# drbd.o was compiled with"          ; \
	  echo "#  `gcc -v 2>&1 | tail -1`"                 ; \
	  echo "# against this kernelrelease:"              ; \
	  sed 's/^/#  /' .kernelrelease                     ; \
          echo    "# kernel .config from"                   ; \
	  echo -n "#  $(KDIR)/.config"                      ; \
          test -L "$(KDIR)" && echo "	alias"   &&           \
	  echo "#  $$(readlink $(KDIR))/.config" || echo "" ; \
	  echo -e "# follows\n#\n"                             ; \
	  cat $(KDIR)/.config ; } | gzip > .kernel.config.gz
	@echo "done."

  dep: greeting
	@echo "make dep ..."
	@$(MAKE) -s -C $(KDIR) SUBDIRS=$(PWD) $(ARCH_UM) dep || \
         echo "I'll ignore this error, but this can cause inconsistencies!"
	@echo "done."

  kbuild: dep

  clean:
	rm -f *.[oas] *.ko .*.cmd .*.d .*.tmp *.mod.c .*.flags .depend .kernel*

  .kernelrelease:
	@echo "someone moved the .kernelrelease out of the way."
	@echo "please re-make."
	@false

  install: .kernelrelease
	@if ! [ -e drbd.o ] ; then \
	  echo "No drbd.o: nothing to install??"; false ; \
	fi
	test -n "$(KERNELRELEASE)"
	install -D -m 644 drbd.o "$(PREFIX)lib/modules/$(KERNELRELEASE)/$(MODSUBDIR)/drbd.o"
	install -d -m 700 "$(PREFIX)var/lib/drbd"
  ifeq ($(shell uname -r),$(KERNELRELEASE))
	/sbin/depmod -a || /sbin/depmod -e drbd.o 2>&1 >/dev/null || true
  else
	[ -e $(LINUX)/System.map ] && \
	   /sbin/depmod -F $(LINUX)/System.map -e ./drbd.o 2>&1 >/dev/null || true
  endif
endif
