# Makefile for StegFS modules
# Copyright (c) 2001  Andrew McDonald <andrew@mcdonald.org.uk>
#
# Various bits borrowed from the lm-sensors makefiles
# (Copyright (c) 1998, 1999  Frodo Looijaard <frodol@dds.nl>)
#
#  This program 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.
#
#  This program 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 this program; if not, write to the Free Software
#  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.


# The location of linux itself. This is used to find the kernel headers
# and other things.
LINUX = /usr/src/linux
LINUX_HEADERS = $(LINUX)/include

# Uncomment the third line on SMP systems if the magic invocation fails. It
# is a bit complicated because SMP configuration changed around kernel 2.1.130
SMP := $(shell if grep -q '^SMP[[:space:]]*=' $(LINUX)/Makefile || \
                  grep -q '^[[:space:]]*\#define[[:space:]]*CONFIG_SMP[[:space:]]*1' $(LINUX_HEADERS)/linux/autoconf.h ; \
               then echo 1; else echo 0; fi)
#SMP := 0
#SMP := 1

# Uncomment the second or third line if the magic invocation fails.
# We need to know whether CONFIG_MODVERSIONS is defined.
MODVER := $(shell if cat $(LINUX_HEADERS)/linux/config.h $(LINUX_HEADERS)/linux/autoconf.h 2>/dev/null | grep -q '^[[:space:]]*\#define[[:space:]]*CONFIG_MODVERSIONS[[:space:]]*1'; then echo 1; else echo 0; fi)
#MODVER := 0
#MODVER := 1

# Uncomment the second line if you want to get (loads of) debug information
# at run-time.
# Not recommended, unless you are actually debugging the code
DEBUG := 0
#DEBUG := 1

# This is the prefix that will be used for almost all directories below.
PREFIX := /usr/local

# This is the directory into which the modules will be installed.
# The magic invocation will return something like this:
#   /lib/modules/2.2.15-ac9/misc
MODDIR := /lib/modules/`grep UTS_RELEASE $(LINUX_HEADERS)/linux/version.h|cut -f 2 -d'"'`/fs

# KERNELRELEASE := `grep UTS_RELEASE $(LINUX_HEADERS)/linux/version.h|cut -f 2 -d'"'`

# These determine whether we are using the kernel crypto patch
# for ciphers and digest algorithms
KCIPHERS := $(shell if grep -q 'CONFIG_CIPHERS[_MODULE]*.1' $(LINUX_HEADERS)/linux/autoconf.h; then echo 1; else echo 0; fi)
# KCIPHERS := 1
# KCIPHERS := 0

KDIGEST := $(shell if grep -q 'CONFIG_DIGEST[_MODULE]*.1' $(LINUX_HEADERS)/linux/autoconf.h; then echo 1; else echo 0; fi)
# KDIGEST := 1
# KDIGEST := 0

# You should not need to change this. It is the directory into which the
# executable program files will be installed. BINDIR for programs that are
# also useful for normal users, SBINDIR for programs that can only be run
# by the superuser.
# Note that not all programs in this package are really installed;
# some are just examples. You can always install them by hand, of
# course.
BINDIR := $(PREFIX)/bin
SBINDIR := $(PREFIX)/sbin

# You should not need to change this. It is the base directory under which the
# manual pages will be installed.
MANDIR := $(PREFIX)/man

# Some often-used commands with default options
CC := gcc
LD := ld
RM := rm -f
INSTALL := install
DEPMOD := /sbin/depmod

# Determine the default compiler flags
# MODCFLAGS is to create in-kernel object files (modules); PROGFLAGS is to
# create non-kernel object files (which are linked into executables).
# ARCFLAGS are used to create archive object files (static libraries), and
# LIBCFLAGS are for shared library objects.
CFLAGS := -I. -I$(LINUX_HEADERS) -O2

ifeq ($(DEBUG),1)
CFLAGS += -DSTEGFS_DEBUG
endif

ifeq ($(WARN),1)
CFLAGS += -Wall -Wstrict-prototypes -Wshadow -Wpointer-arith -Wcast-qual \
          -Wcast-align -Wwrite-strings -Wnested-externs -Winline
else
CFLAGS += -Wall -Wstrict-prototypes
endif

MODCFLAGS := $(CFLAGS) -D__KERNEL__ -DMODULE -fomit-frame-pointer
MODCFLAGS := $(MODCFLAGS) -DEXPORT_SYMTAB

ifeq ($(SMP),1)
MODCFLAGS += -D__SMP__
endif

ifeq ($(MODVER),1)
MODCFLAGS += -DMODVERSIONS -include $(LINUX_HEADERS)/linux/modversions.h
endif

STEGFSOBJS := acl.o balloc.o bitmap.o dir.o file.o fsync.o ialloc.o \
        inode.o ioctl.o level.o mmap.o namei.o \
        super.o symlink.o truncate.o

ifneq ($(KCIPHERS),1)
STEGFSOBJS += aes.o mars.o serpent.o twofish.o
endif

ifneq ($(KDIGEST),1)
STEGFSOBJS += sha1.o
endif

.PHONY: all clean install version package dep

# Make all the default rule
all: stegfs.o


INCLUDEFILES := $(STEGFSOBJS:.o=.d)
ifneq ($(MAKECMDGOALS),clean)
-include $(INCLUDEFILES)
endif


stegfs.o: $(STEGFSOBJS)
	$(LD) -r -o $@ $^

fs.h:
	./make_fs_h.pl . $(LINUX)

$(INCLUDEFILES): fs.h

clean:
	$(RM) *.d *.o fs.h

install: stegfs.o
	$(INSTALL) -m644 stegfs.o $(MODDIR)

.SUFFIXES:

%.o: %.c
	$(CC) $(MODCFLAGS) -c $< -o $@

%.d: %.c
	$(CC) -M -MG $(MODCFLAGS) $< | \
	sed -e 's@^\(.*\)\.o:@$*.d $*.o: Makefile @' > $@

