###############################################################################
# mod_uploader / GNUmakefile.in
###############################################################################
# Copyright (C) 2005 Tetsuya Kimata <kimata@acapulco.dyndns.org>
#
# All rights reserved.
#
# This software is provided 'as-is', without any express or implied
# warranty.  In no event will the authors be held liable for any
# damages arising from the use of this software.
#
# Permission is granted to anyone to use this software for any
# purpose, including commercial applications, and to alter it and
# redistribute it freely, subject to the following restrictions:
#
# 1. The origin of this software must not be misrepresented; you must
#    not claim that you wrote the original software. If you use this
#    software in a product, an acknowledgment in the product
#    documentation would be appreciated but is not required.
#
# 2. Altered source versions must be plainly marked as such, and must
#    not be misrepresented as being the original software.
#
# 3. This notice may not be removed or altered from any source
#    distribution.
#
# $Id: GNUmakefile.in 691 2005-09-17 04:48:38Z svn $
###############################################################################

include .depend
include @AP_BUILD_DIR@/config_vars.mk

###############################################################################
# 設定
CC          := @CC@
CXX         := @CXX@

MODULE_NAME := mod_uploader.so

HEADERDIR   := ../include
INCLUDES    := $(sort -I$(HEADERDIR) -I$(shell @APXS2@ -q INCLUDEDIR) \
	-I$(shell @APRCONF@ --includedir) $(shell @MCONF@ --cppflags) -I/usr/local/include)
DEFS        := $(sort @DEFS@ $(EXTRA_CPPFLAGS))
CFLAGS      := $(sort -ansi $(filter-out -g, @CFLAGS@) @MARCH_OPT@)
LDFLAGS     := $(sort $(shell @MCONF@ --ldflags) @MARCH_OPT@)
LIBS        := $(sort @LIBS@ $(shell @APRCONF@ --link-ld) $(shell @MCONF@ --libs) -lstdc++)

CFLAGS_     := $(CFLAGS)

ifeq ($(CC), icc)
CXX         := icpc
CXX_        := $(CXX)
LINK        := $(CXX)
CFLAGS      := $(filter-out -O2, $(CFLAGS)) -ipo -O3 -w1
DEFS        := $(DEFS) -D__interface=interace
LDFLAGS     := $(LDFLAGS) -ipo -O3 -w1
LIBS        := $(filter-out -lstdc++, $(LIBS))
else
CXX_        := $(CXX)
LIBTOOL     := @LIBTOOL@
CC          := $(LIBTOOL) --silent --mode=compile --tag=CC $(CC) -prefer-pic
CXX         := $(LIBTOOL) --silent --mode=compile --tag=CXX $(CXX) -prefer-pic
LINK        := $(LIBTOOL) --silent --mode=link --tag=CXX $(CXX)
CFLAGS      := $(CFLAGS) -Wall -pipe
LDFLAGS     := $(LDFLAGS) -O2
endif

CXXFLAGS    = $(CFLAGS)

###############################################################################
# デバッグ
ifeq (@DEBUG@,on)
DEFS        += -DDEBUG
CFLAGS      += -g -fno-inline -DAPR_POOL_DEBUG=1
endif

###############################################################################
# ターゲット
all: compile

compile: $(MODULE_NAME)
	@cp -f $(MODULE_NAME) ..

depend: .depend

.depend: $(wildcard *.cpp)
	gcc -MM $(INCLUDES) $(CFLAGS_) $^ > .depend

###############################################################################
# 本体
$(MODULE_NAME): mod_uploader.o UploaderConfig.o \
	RFC1867ParserImpl.o PostReader.o ApachePostReader.o FileWriter.o @WRITER_CLASS@.o \
	TemplateParser.o TemplateLexer.o TemplateExecutor.o TemplateVariableCreator.o \
	UploadItem.o UploadItemCreator.o UploadItemWriter.o UploadItemReader.o UploadItemList.o \
	ThumbnailWriter.o MessageDigest5.o \
	PageView.o RssView.o \
	DirectoryCleaner.o BinString.o CharCodeConverter.o Auxiliary.o
	@echo -e "\033[1;34mGenerating \033[1;31m$@\033[0m"
ifeq ($(CC),icc)
	$(LINK) -fPIC -shared $(LDFLAGS) $(LIBS) -o $@ $(sort $^)
else
	$(LINK) $(LDFLAGS) $(LIBS) -export-dynamic -module -avoid-version -rpath $(shell @APXS2@ -q LIBEXECDIR) \
		-o $(subst .so,.la,$@) $(sort $(subst .o,.lo,$^))
endif
	@cp -f .libs/$@ .

RFC1867ParserImpl.o: RFC1867Parser.o FileWriter.o @WRITER_CLASS@.o DirectoryCleaner.o BinString.o

###############################################################################
# ツール
pconv: PlainConverter.o UploadItemReader.o CharCodeConverter.o Auxiliary.o
	@echo -e "\033[0;36mGenerating \033[1;36m$@\033[0m"
	$(CXX_) $(LDFLAGS) $(LIBS) -o $@ $^
	@cp -f $@ ..

###############################################################################
# テストのコンパイル
BasicFileWriter.test: DEFS += -DDEBUG_BasicFileWriter
BasicFileWriter.test: FileWriter.o

MmapFileWriter.test: DEFS += -DDEBUG_MmapFileWriter
MmapFileWriter.test: FileWriter.o

RFC1867Parser.test: DEFS += -DDEBUG_UPLOAD -DDEBUG_RFC1867Parser
RFC1867Parser.test: PostReader.o ApachePostReader.o FileWriter.o @WRITER_CLASS@.o \
	DirectoryCleaner.o BinString.o Auxiliary.o

UploadItem.test: DEFS += -DDEBUG_UploadItem
UploadItem.test: Auxiliary.o

UploadItemWriter.test: DEFS += -DDEBUG_UPLOAD -DDEBUG_TEMPLATE -DDEBUG_UploadItemWriter
UploadItemWriter.test: FileWriter.o RFC1867ParserImpl.o @WRITER_CLASS@.o \
	UploadItem.o UploadItemCreator.o \
	TemplateExecutor.o DirectoryCleaner.o BinString.o CharCodeConverter.o Auxiliary.o

UploadItemReader.test: DEFS += -DDEBUG_UPLOAD -DDEBUG_TEMPLATE -DDEBUG_UploadItemReader
UploadItemReader.test: FileWriter.o RFC1867ParserImpl.o @WRITER_CLASS@.o \
	UploadItem.o UploadItemCreator.o UploadItemWriter.o \
	TemplateExecutor.o DirectoryCleaner.o BinString.o CharCodeConverter.o Auxiliary.o

UploadItemList.test: DEFS += -DDEBUG_UPLOAD -DDEBUG_TEMPLATE -DDEBUG_UploadItemList
UploadItemList.test: FileWriter.o RFC1867ParserImpl.o @WRITER_CLASS@.o \
	UploadItem.o UploadItemReader.o TemplateExecutor.o TemplateVariableCreator.o \
	ThumbnailWriter.o DirectoryCleaner.o BinString.o CharCodeConverter.o Auxiliary.o

TemplateLexer.test: DEFS += -DDEBUG_TemplateLexer
TemplateLexer.test: Auxiliary.o

TemplateParser.test: DEFS += -DDEBUG_TemplateParser
TemplateParser.test: TemplateLexer.o Auxiliary.o

TemplateExecutor.test: DEFS += -DDEBUG_TEMPLATE -DDEBUG_TemplateExecutor
TemplateExecutor.test: UploadItem.o UploadItemReader.o UploadItemList.o \
	TemplateParser.o TemplateLexer.o TemplateVariableCreator.o \
	ThumbnailWriter.o CharCodeConverter.o Auxiliary.o

ThumbnailWriter.test: DEFS += -DDEBUG_TEMPLATE -DDEBUG_ThumbnailWriter
ThumbnailWriter.test: UploadItem.o UploadItemReader.o TemplateExecutor.o \
	CharCodeConverter.o Auxiliary.o

RssView.test: DEFS += -DDEBUG_TEMPLATE -DDEBUG_RssView
RssView.test: PageView.o RssView.o \
	UploadItem.o UploadItemReader.o UploadItemList.o \
	TemplateExecutor.o TemplateVariableCreator.o \
	ThumbnailWriter.o CharCodeConverter.o Auxiliary.o

CharCodeConverter.test: DEFS += -DDEBUG_CharCodeConverter
CharCodeConverter.test:

MessageDigest5.test: DEFS += -DDEBUG_MessageDigest5
MessageDigest5.test:

###############################################################################
# テストの実行
TemplateParser.run: TemplateParser.test
	./TemplateParser.test ../tmpl/view.htm > parse_tree.dot
	@DOT@ -Tps2 -oparse_tree.ps parse_tree.dot
	@PS2PDF@ -sPAPERSIZE=b0 parse_tree.ps

RssView.run: RssView.test
	./RssView.test ../file

###############################################################################
# その他
GNUmakefile: GNUmakefile.in
	cd .. && $(MAKE) config

test: TARGETS := BasicFileWriter MmapFileWriter RFC1867Parser UploadItemWriter \
	UploadItemReader UploadItemList TemplateLexer TemplateParser TemplateExecutor \
	RssView CharCodeConverter TemplateParser RssView # ThumbnailWriter
test:
	@for target in $(TARGETS); \
	do \
		echo; \
		echo -ne "\033[1;33m"; \
		echo "============================================================"; \
		echo -e "\033[0;32mStart to compile \033[1;32m$${target}.test\033[0m"; \
		touch $${target}.test; \
		$(MAKE) $${target}.test || exit; $(MAKE) clean-obj; \
	done

clean-obj:
	@rm -rf .libs
	@rm -f *.o *.lo *.obj *.pdb *.exp *.lib *.il *.loT
	@rm -f .depend

clean: clean-obj
	@rm -f mod_uploader.lo mod_uploader.slo mod_uploader.la
	@rm -f *.dot *.ps *.pdf
	@rm -f cachegrind.out.* gmon.out
	@rm -f *.log *.status *.scan *.bak
	@rm -f *.dpi *dyn *.pchi
	@rm -f *.so *.out *.test *.exe
	@rm -f pconv
	@rm -f *~ \#*\#

###############################################################################
# 型ルール
%.cpp: %.cpp.in
	cd .. && $(MAKE) config
$(HEADERDIR)/%.h: $(HEADERDIR)/%.h.in
	cd .. && $(MAKE) config

%.o: %.c $(HEADERDIR)/%.h $(HEADERDIR)/mod_uploader_config.h
	@echo -e "\033[0;36mCompiling \033[1;36m$<\033[0m"
	$(CXX) $(DEFS) -c $(INCLUDES) $(CFLAGS) -o $@ $<
%.o: %.cpp $(HEADERDIR)/%.h $(HEADERDIR)/mod_uploader_config.h
	@echo -e "\033[0;36mCompiling \033[1;36m$<\033[0m"
	$(CXX) $(DEFS) -c $(INCLUDES) $(CXXFLAGS) -o $@ $<
%.o: %.cpp $(HEADERDIR)/mod_uploader_config.h
	@echo -e "\033[0;36mCompiling \033[1;36m$<\033[0m"
	$(CXX) $(DEFS) -c $(INCLUDES) $(CXXFLAGS) -o $@ $<

%.test: DEFS += -DDEBUG
%.test: CFLAGS += -pg -g
%.test: LDFLAGS += -pg -g
%.test: %.o
	@echo -e "\033[1;34mGenerating \033[1;31m$@\033[0m"
	@$(CXX_) $(LDFLAGS) $(LIBS) $^ -o $@

.SUFFIXES: .c .cpp .in .h .o .so .test   
.PHONY: all compile test clean clean-obj depend


# Local Variables:
# mode: makefile
# buffer-file-coding-system: euc-japan-unix
# End:
