# SE Tools Main makefile

MAKEFILE =  Makefile
MAKE = make

TCL_INCLUDE	= -I/usr/include
TCL_LIBINC	= -L/usr/lib
TCL_LIBS	= -ltk8.3 -ltcl8.3 -lfl -lm -ldl

# NOTE: if you are using tcl/tk 8.4 you may want to uncomment the following 
# three lines and then comment or remove the three lines above. You may still 
# have to change these lines depending on you particular installation
# of tcl/tk 8.4

#TCL_INCLUDE	= -I/usr/local/include
#TCL_LIBINC	= -L/usr/local/lib
#TCL_LIBS	= -ltk8.4 -ltcl8.4 -lfl -lm -ldl

LINKFLAGS	= 
CC		= gcc 
YACC		= bison -y 
LEX		= flex -olex.yy.c


# Compile options
#
# -DUSE_SORTING		sorted arrays using AVL trees within arrays (must use, alternative
#			code is deprecated and no longer supported and probably won't work!)
# -DAPOL_PERFORM_TEST	simple performance measure tests (shouldn't normally use)
# -DCONFIG_SECURITY_SELINUX_MLS 
#			compiles library to be compatible with MLS 
#			in the policy (experimental, see Readme)
#
CC_DEFINES	= -DUSE_SORTING

CFLAGS		= -Wall -O2 $(TCL_INCLUDE) $(CC_DEFINES)
#CFLAGS		= -Wall -g $(TCL_INCLUDE) $(CC_DEFINES)

# Install directories
# Binaries go here
BINDIR		= $(DESTDIR)/usr/bin


# NOTE: DON'T CHANGE INSTALL_LIBDIR.  The code expects below INSTALL_LIBDIR 
# to point here.  We will eventually fix this so that the code
# uses the path below, rather than having a hard-coded path.  If you really want
# to change this, then change it here, and change the #define for APOL_INSTALL_DIR
# in ./setools/libapol/apol_tcl.h and change the value of the "help_file"
# variable in ./setools/sepct/top.tcl .  Also make sure to change 
# ./setools/policy/seuser.fc to reflect the change.
#
INSTALL_LIBDIR	= $(DESTDIR)/usr/lib/apol
#
# END NOTE

# Assumed policy source directory
POLICY_SRC_DIR	= $(DESTDIR)/etc/security/selinux/src

# This should be imported from tools/Makefile
SRC_POLICY_DIR = ../../

# all apps that have a te/fc file need to be listed here
POLICYINSTALLDIRS = seuser

# exports
export CFLAGS CC YACC LEX LINKFLAGS BINDIR INSTALL_LIBDIR TCL_LIBINC TCL_LIBS MAKE POLICY_SRC_DIR SRC_POLICY_DIR

default:
	@echo "Make targets for setools: "
	@echo "   install:       build and install everything (selinux required)"
	@echo "   install-apol   build and install apol (selinux not required)"
	@echo "   install-sepcut build and install sepct (selinux not required)"
	@echo "   install-seuser build and install seuser (selinux required)"
	@echo " "
	@echo "   The following 3 targets are for use in the selinux build process only"
	@echo "	  and are deprecated."
	@echo "   insert-policy  insert policy files into pre-selinux policy"
	@echo "   sys-install    build and install everything pre-selinux"
	@echo "   sys-make	 build everthing pre-selinux (no install)"
	@echo " "
	@echo "   all:           build everything, but don't install"
	@echo "   rpm:           create a source and binary rpm - must be root"
	@echo "   apol:          build policy analysis tool"
	@echo "   seuser:        build SE Linux add user tool"
	@echo "   sepcut         build policy customization/browsing tool"
	@echo "   awish:         build TCL/TK wish interpreter with SE Linux tools extensions"
	@echo " "
	@echo "   clean:         clean up interim files"
	@echo "   bare:          more extensive clean up"


apol: selinux_tool
	cd apol; $(MAKE) apol 

awish: selinux_tool
	cd awish; $(MAKE) awish

seuser: selinux_tool
	cd seuser; $(MAKE) seuser

sepcut: selinux_tool
	cd sepct; $(MAKE) sepcut

libapol: selinux_tool
	cd libapol; $(MAKE) 

libseuser: selinux_tool
	cd libseuser; $(MAKE)

all: libapol libseuser apol awish seuser sepcut

$(INSTALL_LIBDIR):
	install -m 755 -d $(INSTALL_LIBDIR)

install-apol: $(INSTALL_LIBDIR)
	cd apol; $(MAKE) install
	cd awish; $(MAKE) install	

install-seuser: $(INSTALL_LIBDIR)
	cd seuser; $(MAKE) install

install-sepcut: $(INSTALL_LIBDIR)
	cd sepct; $(MAKE) install

install: install-apol install-seuser install-sepcut

# Next four targets are to support installation as part of a system
# install. These targets are deprecated.
#
# copy all policy files to the uninstalled selinux policy build directory
insert-policy: 
	cd seuser ; $(MAKE) $@ 

sysinstall-seuser: $(INSTALL_LIBDIR)
	cd seuser; $(MAKE) sys-install "CFLAGS+=$(SYS_OPTIONS)"

sysall-seuser: 
	cd seuser; $(MAKE) sys-all "CFLAGS+=$(SYS_OPTIONS)"

sys-install: install-apol sysinstall-seuser install-sepcut

sys-all: apol sysall-seuser sepcut

VER=$(shell cat VERSION)

rpm: bare
	cd packages; make rpm
	@if [ ! -e ../setools-$(VER) ]; then \
		cd ..; mv setools setools-$(VER); \
	fi
	cd ..; tar cfz setools-$(VER).tgz setools-$(VER); rpmbuild -tb setools-$(VER).tgz; 

# test targets
tests: test-seuser test-apol

test-apol: selinux_tool
	cd libapol/test; $(MAKE) $@

test-seuser: selinux_tool
	cd libseuser/test; $(MAKE) $@

test-clean: 
	cd libapol/test; $(MAKE) clean
	cd libseuser/test; $(MAKE) clean

test-bare:
	cd libapol/test; $(MAKE) bare
	cd libseuser/test; $(MAKE) clean

clean: test-clean
	cd apol; $(MAKE) clean
	cd awish; $(MAKE) clean
	cd libapol; $(MAKE) clean
	cd sepct; $(MAKE) clean
	cd seuser; $(MAKE) clean
	cd libseuser; $(MAKE) clean
	rm -f *~

bare: test-bare
	cd apol; $(MAKE) bare
	cd awish; $(MAKE) bare
	cd libapol; $(MAKE) bare
	cd seuser; $(MAKE) bare
	cd sepct; $(MAKE) bare
	cd libseuser; $(MAKE) bare
	rm -f *~
	rm -rf ./lib


# Leave this empty target here!
selinux_tool:
# empty!


