##############################################################################
##############################################################################
##
## COPYRIGHT (C) 1998 By Arthur Naseef
##
## This file is covered under the GNU General Public License Version 2.  For
##  more information, see the file COPYING.
##
##
## FILE: Makefile
##
## SCCS ID: @(#) Makefile 1.9
##
## DESCRIPTION:
##	- This file contains the dependencies and build rules for the overlay
##	  filesystem.
##
## NOTES:
##	- run "make all" to build the overlay filesystem module.
##
##	- run "make install" to put the ovl.o file into the directory,
##	  $(prefix)/lib/module/<KERNEL VERSION>/fs.
##
##	- The user may use the COPTS makefile variable to add compiler
##	  options to the command line (before all other options).
##
##
## REVISION HISTORY:
##
## DATE		AUTHOR		DESCRIPTION
## ==========	===============	==============================================
## 02/26/1998	ARTHUR N.	Added to source code control.
## 02/27/1998	ARTHUR N.	Updated the build rules for dump_inode.o
## 03/03/1998	ARTHUR N.	Added the precompiler suffix rule.
## 03/05/1998	ARTHUR N.	Added the ovl_alloc.c file.
## 03/06/1998	ARTHUR N.	Improved the install actions, and added macros
##				 for module installation directories.  Also,
##				 improved comments.
## 03/08/1998	ARTHUR N.	Added the PROTECT_MMAP_WRITE flag.
## 03/08/1998	ARTHUR N.	Continue even if genksyms complains, and turn
##				 off the warnings from that program.
## 03/09/1998	ARTHUR N.	Added the copyright notice to the file.
## 03/09/1998	ARTHUR N.	Set the default for VERIFY_MALLOC to 0.
## 03/11/1998	ARTHUR N.	Make the installation directories.
##
##############################################################################
##############################################################################

##
## MACRO DEFINITIONS
##

INC=../include

MISC_FILES=\
	IDEAS \
	NOTES \
	TODO

DEBUG_FILES=\
	ovl_debug.c

DEBUG_OBJ=\
	ovl_debug.o

SRC_FILES=\
	$(DEBUG_FILES) \
	ovl_main.c \
	ovl_alloc.c \
	ovl_dir.c \
	ovl_file.c \
	ovl_ino.c \
	ovl_kern.c \
	ovl_stg.c \
	ovl_sys.c

OBJ_FILES=\
	$(DEBUG_OBJ) \
	ovl_main.o \
	ovl_alloc.o \
	ovl_dir.o \
	ovl_file.o \
	ovl_ino.o \
	ovl_kern.o \
	ovl_stg.o \
	ovl_sys.o

OVL_OBJ=ovlfs.o

VER_FILE=ovlfs.ver

INSTALL_OBJ=ovl.o


## Intallation directories:

OS_REV=`uname -r`
MOD_INST_DIR=$(prefix)/lib/modules/$(OS_REV)
FS_MOD_INST_DIR=$(MOD_INST_DIR)/fs
MISC_MOD_INST_DIR=$(MOD_INST_DIR)/misc


##############################################################################
##############################################################################
##
## COMPILER OPTIONS
##

## The following defines may be used on the compile line:
##
##	MODULE		- Set to 1 in order to compile the ovlfs.o file as
##			  a loadable module.  If this is not set, the file
##			  must be linked into the kernel to be used.
##
##	KDEBUG		- Set to turn on debug messages; set to > 1 to get
##			  more verbose error messages (range is 1 to 3).
##
##	KDEBUG_CALLS	- Display debug messages at the start of each ovlfs
##			  function called.
##
##	USE_MAGIC	- Set to non-zero value to turn on the use of the
##			  "magic" and "magicr" directory entries which allow
##			  the user to work with the storage filesystem
##			  directory and the base filesystem directory,
##			  respectively.  If set, the magic filesystem options
##			  may be used to modify the settings on a mounted-
##			  filesystem basis.
##
##	STORE_REF_INODES
##			- Set to store reference inodes when mounting and
##			  getting inodes.  If set, the storage and overlay
##			  roots will be impervious to chroot() calls,
##			  otherwise the overlay and storage roots will be
##			  relative to the current root directory.  For better
##			  security (and most likely the expected behavior),
##			  set this.
##
##	STORE_MAPPINGS	- Turn on to allow inode mappings to be stored in the
##			  storage file.  If this is set, the "storemaps" and
##			  related fs options may be used to set the storage of
##			  inode maps on a per-mounted fs basis.
##
##	VERIFY_MALLOC	- Set to 1 to turn on checking of total memory
##			  allocated and freed by the overlay filesystem (except
##			  the kernel lists).  In addition, if this value is
##			  set to an integer greater than 1, check the size of
##			  memory being freed.
##
##
## NOTES:
##	- The -O2 is necessary unless the kernel is compiled with debugging;
##	  without it, external inline functions defined in the kernel header
##	  files will be missing in the ovlfs.o file and, as a result, the
##	  file will be unusable.


### OVLFS compile flags:

DATE_STAMP=-DDATE_STAMP="`date`"

DEBUG_FLAGS=-DKDEBUG=1 \
            -DKDEBUG_CALLS=0 \
            -DVERIFY_MALLOC=0

OVLFS_FLAGS=$(DEBUG_FLAGS) $(DATE_STAMP) \
            -DUSE_MAGIC=1 \
            -DSTORE_REF_INODES=1 \
            -DOVLFS_BANNER=1 \
            -DSTORE_MAPPINGS \
            -DPROTECT_MMAP_WRITE

### Kernel flags:

MODULE_FLAGS=-DMODULE=1 \
             -DMODVERSIONS \
             -DEXPORT_SYMTAB
KERNEL_FLAGS=$(MODULE_FLAGS) -D__KERNEL__


### CFLAGS

CFLAGS=$(COPTS) -Wall -O2 $(KERNEL_FLAGS) $(OVLFS_FLAGS) -I$(INC)



##############################################################################
##############################################################################
##
## DEFAULT TARGETS
##

.PHONY: all bak fresh modules_install install


all:	$(OVL_OBJ)

fresh:	clean all



##############################################################################
##############################################################################
##
## OBJECT TARGETS
##

$(VER_FILE): ovl_main.c
	@echo "Compiling $@"
	@-$(CC) $(CFLAGS) -D__GENKSYMS__ -E ovl_main.c | /sbin/genksyms .
	@mv ovl_main.ver $(VER_FILE)

$(OVL_OBJ): $(OBJ_FILES) $(VER_FILE)
	$(LD) $(LDFLAGS) -o $(OVL_OBJ) -r $(OBJ_FILES)

dump_inode.o: dump_inode.c $(INC)/dumpi.h $(INC)/ovl_fs.h
	$(CC) $(CFLAGS) -c dump_inode.c -o dump_inode0.o
	$(LD) $(LDFLAGS) -o dump_inode.o -r dump_inode0.o ovl_sys.o
	@/bin/rm -f dump_inode0.o


##############################################################################
##############################################################################
##
## MISCELLANEOUS TARGETS
##

clean:
	@/bin/rm -f $(OBJ_FILES) $(OVL_OBJ) $(VER_FILE)

bak:
	@ARCHIVE_NAME=OVLFS bak $(SRC_FILES) $(MISC_FILES) Makefile

install:	modules_install

modules_install:
	@echo "Installing $(INSTALL_OBJ) in $(FS_MOD_INST_DIR)"
	@/bin/mkdir -p $(FS_MOD_INST_DIR)
	@/bin/cp $(OVL_OBJ) $(FS_MOD_INST_DIR)/$(INSTALL_OBJ)
	@if [ -f dump_inode.o ]; \
	then \
		echo "Installing dump_inode.o in $(MISC_MOD_INST_DIR)"; \
		/bin/mkdir -p $(MISC_MOD_INST_DIR) \
		/bin/cp dump_inode.o $(MISC_MOD_INST_DIR)/dump_inode.o; \
	fi



##############################################################################
##############################################################################
##
## OBJECT FILE DEPENDENCIES
##

KLIST_HDRS=$(INC)/kernel_lists.h $(INC)/klists.ver

ovl_main.o: ovl_main.c $(INC)/ovl_fs.h $(VER_FILE) $(KLIST_HDRS)

ovl_ino.o: ovl_ino.c $(INC)/ovl_fs.h $(VER_FILE) $(KLIST_HDRS)

ovl_dir.o: ovl_dir.c $(INC)/ovl_fs.h $(VER_FILE) $(KLIST_HDRS)

ovl_file.o: ovl_file.c $(INC)/ovl_fs.h $(VER_FILE) $(KLIST_HDRS)

ovl_kern.o: ovl_kern.c $(INC)/ovl_fs.h $(VER_FILE) $(KLIST_HDRS)

ovl_stg.o: ovl_stg.c $(INC)/ovl_fs.h $(VER_FILE) $(KLIST_HDRS)

ovl_debug.o: ovl_debug.c $(INC)/ovl_fs.h $(VER_FILE) $(KLIST_HDRS)

ovl_sys.o: ovl_sys.c $(INC)/ovl_fs.h $(VER_FILE) $(KLIST_HDRS)



##############################################################################
##############################################################################
##
## Suffixes
##

.SUFFIXES: .c .i .o

.c.o:
	@echo "Compiling $*.o"
	@$(CC) $(CFLAGS) -c $*.c

.c.i:
	@echo "Compiling $*.i"
	@$(CC) $(CFLAGS) -E $*.c >$*.i
