#
# Makefile - Makefile for NILFS
#
# Copyright (C) 2005 Nippon Telegraph and Telephone Corporation.
#
# This file is part of NILFS.
#
# NILFS 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 of the License, or
# (at your option) any later version.
#
# NILFS 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 NILFS; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
#
# Makefile,v 1.41 2006/07/10 12:13:16 ryusuke Exp
#

ifndef CONFIG_NILFS
  EXTERNAL_BUILD=y
  CONFIG_NILFS=m
  # Uncomment below to do debug build.
  #CONFIG_NILFS_DEBUG=y
endif


# Some assembler options.
ifneq ($(CONFIG_NILFS_DEBUG),)
  EXTRA_CFLAGS += -g -Wa,-adhlms=$@.lst -fno-omit-frame-pointer
endif

list-$(CONFIG_NILFS) += nilfs

obj-$(CONFIG_NILFS) += nilfs.o

nilfs-objs := inode.o file.o dir.o super.o namei.o page.o radix-tree-64.o \
	ialloc.o symlink.o bbt.o ibt.o btree.o segment.o recovery.o \
	btnode.o gc.o the_nilfs.o sketch.o

ifneq ($(CONFIG_NILFS_DEBUG),)
	nilfs-objs += debug.o
endif

#
# We support only external build, and does not support kernel build.
# 
ifneq ($(KERNELRELEASE),)

ifeq ($(EXTERNAL_BUILD),y)
  EXTRA_CFLAGS += -U CONFIG_NILFS_XATTR       # not supported yet
  EXTRA_CFLAGS += -U CONFIG_NILFS_POSIX_ACL   # not supported yet

  # Following switches are provisional and should be removed in the future
  EXTRA_CFLAGS += -U NILFS_SHRINKER_DISABLE
  EXTRA_CFLAGS += -D NILFS_ATIME_DISABLE
  EXTRA_CFLAGS += -U NILFS_CP_BARRIER         # apply the barrier for all checkpoints.
  EXTRA_CFLAGS += -D NILFS_SB_BARRIER         # apply the barrier for superblock updates.

  ifneq ($(CONFIG_NILFS_DEBUG),)
    EXTRA_CFLAGS += -D CONFIG_NILFS_DEBUG

    # Exhaustive debug switches
    EXTRA_CFLAGS += -U NILFS_BH_DEBUG 	      # trace get_bh, put_bh and 
                                              # brelse in nilfs files
    EXTRA_CFLAGS += -U NILFS_BH_COUNT_DETAIL  # detail report on 
                                              # /proc/fs/nilfs/nputblk
    EXTRA_CFLAGS += -U NILFS_SEMAPHORE_DEBUG
    EXTRA_CFLAGS += -U NILFS_SPINLOCK_DEBUG
    EXTRA_CFLAGS += -U NILFS_LOCK_BUFFER_DEBUG
    EXTRA_CFLAGS += -U NILFS_LOCK_PAGE_DEBUG
    EXTRA_CFLAGS += -U NILFS_KMALLOC_DEBUG

    # Debug counters
    EXTRA_CFLAGS += -U NILFS_PAGE_COUNT
    EXTRA_CFLAGS += -U NILFS_KMALLOC_COUNT
    EXTRA_CFLAGS += -U NILFS_BIO_COUNT
    EXTRA_CFLAGS += -U NILFS_SLAB_COUNT
    EXTRA_CFLAGS += -U NILFS_KMAP_COUNT
    EXTRA_CFLAGS += -U NILFS_ALLOCPAGE_COUNT
  endif
endif

else

#
# Description for external build begins here
#

KVER := $(shell uname -r)
KSRC := /lib/modules/$(KVER)/build
MDIR := /lib/modules/$(KVER)/kernel/fs/nilfs/
PWD := $(shell pwd)

CFLAGS += -Wall

default:
	$(MAKE) -C $(KSRC) SUBDIRS=$(PWD) BUILD_DIR=$(PWD) modules

install: unload
	install -d $(MDIR)
	install -m 644 -c $(addsuffix .ko,$(list-m)) $(MDIR)
	/sbin/depmod $(KVER) -A

uninstall: unload
	rm -rf $(addprefix $(MDIR),$(addsuffix .ko,$(list-m)))
	/sbin/depmod $(KVER) -A

reinstall:	uninstall install

load:
	modprobe $(list-m)

unload:
	@test "`uname -r`" != "$(KVER)" || \
	     if [ -n "`mount -t $(list-m)`" ]; then \
	         echo "Unmount nilfs first."; false; \
	     elif (lsmod | grep -e ^$(list-m)' ') > /dev/null; then rmmod $(list-m); \
	     	 if [ $$? != 0 ]; then echo "failed to unload $(list-m)"; false; \
	         else echo "unloaded $(list-m)"; fi \
	     fi \

clean:
	rm -f *.mod.c *.mod *.o *.ko .*.cmd .*.flags *.lst *~ .#*
	rm -rf .tmp_versions .$(obj-m:.o=)*

endif

TAGS:
	etags *.h *.c

.PHONY: TAGS
