# Makefile for ModSecurity

MOD_SECURITY2 = mod_security2 apache2_config apache2_io apache2_util \
    re re_operators re_actions re_tfns re_variables \
    msc_logging msc_xml msc_multipart modsecurity msc_parsers msc_util msc_pcre \
    persist_dbm msc_reqbody pdf_protect msc_geo acmp msc_lua msc_release

MSC_TEST = re re_operators re_actions re_tfns re_variables \
    msc_logging msc_xml msc_multipart modsecurity \
    msc_parsers msc_util msc_pcre  persist_dbm \
    msc_reqbody msc_geo acmp msc_lua msc_release

MOD_SECURITY2_H = re.h modsecurity.h msc_logging.h msc_multipart.h msc_parsers.h \
    msc_pcre.h msc_util.h msc_xml.h persist_dbm.h apache2.h pdf_protect.h \
    msc_geo.h acmp.h utf8tables.h msc_lua.h msc_release.h

CC = i486-linux-gnu-gcc
LIBTOOL = /usr/share/apr-1.0/build/libtool --silent
PERL = /usr/bin/perl
EXTRA_CFLAGS = -O2 -g -Wall 
MODSEC_EXTRA_CFLAGS = -DWITH_PCRE_STUDY -DMODSEC_PCRE_MATCH_LIMIT=1500 -DMODSEC_PCRE_MATCH_LIMIT_RECURSION=1500      

### Note: must be in APXS format: -Wc,-flag
APXS_EXTRA_CFLAGS =  -Wc,-O2 -Wc,-g -Wc,-Wall
MODSEC_APXS_EXTRA_CFLAGS =  -Wc,-DWITH_PCRE_STUDY -Wc,-DMODSEC_PCRE_MATCH_LIMIT=1500 -Wc,-DMODSEC_PCRE_MATCH_LIMIT_RECURSION=1500

APXS = /usr/bin/apxs2
APXS_WRAPPER = build/apxs-wrapper
APXS_INCLUDEDIR = /usr/include/apache2
APXS_INCLUDES = -I/usr/include/apache2  -I/usr/include/apache2 -I. -I/usr/include/apr-1.0
APXS_CFLAGS = -DLINUX=2 -D_FORTIFY_SOURCE=2 -D_GNU_SOURCE -D_LARGEFILE64_SOURCE -D_REENTRANT -I/usr/include/apr-1.0 -I/usr/include/openssl -I/usr/include/xmltok -pthread  
APXS_LDFLAGS = -Wl,--as-needed -Wl,-z,relro 
APXS_LIBS = -L/usr/lib  -lm -L/usr/lib -lpcre

PCRE_CFLAGS = 
PCRE_LIBS = -L/usr/lib -lpcre

LUA_CFLAGS = -DWITH_LUA -I/usr/include/lua5.1  
LUA_LIBS = -llua5.1  

LIBXML2_CFLAGS = -I/usr/include/libxml2
LIBXML2_LIBS = -lxml2

APR_CFLAGS =  -I/usr/include/apr-1.0  -DLINUX=2 -D_REENTRANT -D_GNU_SOURCE -D_LARGEFILE64_SOURCE  -pthread
APR_LDFLAGS =  
APR_LIBS =  
APR_LINK_LD =  -L/usr/lib -lapr-1

APU_CFLAGS =  -I/usr/include/apr-1.0 
APU_LDFLAGS =  
APU_LIBS =   -ldb    
APU_LINK_LD =  -L/usr/lib -laprutil-1

CPPFLAGS = -I/usr/include/apache2  -I/usr/include/apache2 -I. -I/usr/include/apr-1.0  $(PCRE_CFLAGS) $(LIBXML2_CFLAGS) $(LUA_CFLAGS) 
LIBS =  $(PCRE_LIBS) $(LIBXML2_LIBS) $(LUA_LIBS)
LDFLAGS = 
CFLAGS = 

COMPILE_APACHE_MOD = $(APXS_WRAPPER) -c $(CPPFLAGS) $(LDFLAGS) $(LIBS)

INSTALL_MOD_SHARED = $(APXS_WRAPPER) -i

all: mod_security2.la

install: install-mods

clean-extras:
	@for dir in mlogc-src; do \
	    if test -d $$dir; then \
			(cd $$dir && $(MAKE) clean); \
		fi; \
	done
	@rm -rf ../tools/mlogc ../tools/mlogc-batch-load.pl

clean: clean-extras
	@rm -rf *.la *.lo *.loT *.o *.slo .libs msc_test msc-test-debug.log

distclean: clean
	@rm -rf Makefile mlogc-src/Makefile mlogc-src/mlogc-batch-load.pl ../tools/*.pl t/run-unit-tests.pl t/run-regression-tests.pl t/gen_rx-pm.pl t/csv_rx-pm.pl t/run-tests.pl t/regression/server_root/conf/httpd.conf t/regression/server_root/conf/*.t_*.conf t/regression/server_root/tmp/* t/regression/server_root/logs/*.log t/regression/server_root/logs/audit/* t/regression/server_root/upload/* t/regression/server_root/data/* config config.log config.status build/apxs-wrapper

maintainer-clean: distclean
	@rm -rf config config.log config.status configure mod_security2_config.h autoscan.log configure.scan build/libtool.m4 build/config.guess build/config.sub build/ltmain.sh

install-mods: mod_security2.la
	$(INSTALL_MOD_SHARED) mod_security2.la

${MOD_SECURITY2:=.slo}: $(MOD_SECURITY2_H)
${MOD_SECURITY2:=.lo}: $(MOD_SECURITY2_H)
${MOD_SECURITY2:=.o}: $(MOD_SECURITY2_H})

mod_security2.la: $(MOD_SECURITY2_H) *.c
	@src=""; \
	for f in $(MOD_SECURITY2); do \
		src="$$src $$f.c"; \
	done; \
	rm -f msc_test msc_test.o msc_test.lo msc_test.slo; \
	$(COMPILE_APACHE_MOD) $(APXS_EXTRA_CFLAGS) $(MODSEC_APXS_EXTRA_CFLAGS) $$src

### MLogC
mlogc:
	@(cd mlogc-src && $(MAKE) mlogc) \
	&& cp -p mlogc-src/mlogc ../tools \
	&& cp -p mlogc-src/mlogc-batch-load.pl ../tools \
	&& echo \
	&& echo "Successfully built \"mlogc\" in ../tools." \
	&& echo "See: mlogc-src/INSTALL" \
	&& echo

### Experimental Test Framework (*NIX only right now)
msc_test.lo: msc_test.c
	$(LIBTOOL) --mode=compile $(CC) $(APXS_INCLUDES) $(APXS_CFLAGS) $(CPPFLAGS) $(APR_CFLAGS) $(APU_CFLAGS) -o msc_test.lo -c msc_test.c

msc_test: $(TESTOBJS) $(MOD_SECURITY2_H}) msc_test.lo 
	objs=""; \
	for f in $(MSC_TEST); do \
		objs="$$objs $$f.lo"; \
	done; \
	$(LIBTOOL) --mode=link $(CC) $$objs -o msc_test msc_test.lo $(LDFLAGS) $(LIBS) $(APR_LINK_LD) $(APU_LINK_LD)

test: t/run-unit-tests.pl msc_test
	@rm -f msc-test-debug.log; \
	$(PERL) t/run-unit-tests.pl

test-regression: t/run-regression-tests.pl
	@$(PERL) t/run-regression-tests.pl

.PHONY: all install clean-extras clean maintainer-clean distclean install-mods test test-regression
