# All Rights Reserved, Copyright (C) 2003, Hitachi Software Engineering Co., Ltd.
# Yuichi Nakamura: 2005 0313 fixed Make test 
CC		= gcc
LEX		= lex
YACC		= yacc
CFLAGS		= -Wall -O2 -g -DDEBUG=1
LD		= -ll -lselinux
INCLUDE		= -I/usr/include/selinux
LDPATH		= -L/usr/lib
OBJS		= y.tab.o action.o hashtab.o file_label.o convert.o security_class.o common.o initial_sids.o initial_policy.o debug.o out_file_acl.o

POLICYFILES	= ./macros/*.te 
CONFDIR		= ./conf_test
TESTDIR		= ./test_data

INSTALL_PATH	= /etc/security/selinux/converter

all: converter

converter: $(OBJS)
	$(CC) -o $@ $(OBJS) $(LD) $(LDPATH)

.c.o:
	$(CC) -c $(CFLAGS) $(INCLUDE) $<

y.tab.o: lex.yy.c y.tab.c
	$(CC) -c $(CFLAGS) y.tab.c

lex.yy.c: middle_lang.l 
	$(LEX) middle_lang.l

y.tab.c: middle_lang.y middle_lang.h lex.yy.c action.h initial_policy.h initial_sids.h security_class.h
	$(YACC) middle_lang.y

action.o: middle_lang.h action.h hashtab.h file_label.h global.h common.h debug.h

hashtab.o: hashtab.h

file_label.o: hashtab.h file_label.h global.h

convert.o: file_label.h action.h hashtab.h convert.h global.h debug.h out_file_acl.h

out_file_acl.o: global.h debug.h hashtab.h action.h

initial_policy.o: initial_policy.h security_class.h initial_sids.h file_label.h

security_class.o: common.h security_class.h initial_policy.h

initial_sids.o: common.h initial_sids.h initial_policy.h

common.o: common.h

debug.o: debug.h

install: converter
	mkdir -p $(INSTALL_PATH)
	mkdir -p $(INSTALL_PATH)/macros
	mkdir -p $(INSTALL_PATH)/policy
	cp fs_use $(INSTALL_PATH)
	cp genfs_contexts $(INSTALL_PATH)
	cp initial_sid_contexts $(INSTALL_PATH)
	cp converter $(INSTALL_PATH)
	cp macros/*.te $(INSTALL_PATH)/macros/
	cp conf_test/*.a conf_test/global  $(INSTALL_PATH)/policy/
	cp Makefile.policy $(INSTALL_PATH)/Makefile
	touch $(INSTALL_PATH)/exc_label.tmp
	/usr/bin/chcon system_u:object_r:bin_t $(INSTALL_PATH)/converter

clean:
	rm -f *.o converter lex.yy.c y.tab.c a.out policy.conf test.conf file_context

policy.conf: converter $(CONFDIR)/*.a
	cat $(CONFDIR)/global $(CONFDIR)/*.a >$(CONFDIR)/all;
	./converter -i $(CONFDIR)/all -o test.conf -f file_context;
	m4 -Imacros -s ./macros/*.te test.conf > policy.conf;
	checkpolicy policy.conf;

compile: converter
	cat $(CONFDIR)/global $(CONFDIR)/*.a >$(CONFDIR)/all;
	./converter -i $(CONFDIR)/all -o test.conf -f file_context;
	m4 -Imacros -s ./macros/*.te test.conf > policy.conf;

test: compile
	checkpolicy policy.conf
