#
# $Id: GNUmakefile.in,v 1.35 2005/07/12 19:15:36 will_mason Exp $
#
# vi: set ft=make:

# ObjectiveLib - a library of containers and algorithms for Objective-C
#
# Copyright (c) 2004
# Will Mason
#
# Portions:
#
# Copyright (c) 1994
# Hewlett-Packard Company
#
# Copyright (c) 1996,1997
# Silicon Graphics Computer Systems, Inc.
#
# Copyright (c) 1997
# Moscow Center for SPARC Technology
#
# Copyright (c) 1999 
# Boris Fomitchev
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
# You may contact the author at will_mason@users.sourceforge.net.

.PHONY: doc install-doc sf-doc

BUILD_COCOA := @BUILD_COCOA@
DOXYGEN_PATH := @DOXYGEN_PATH@
BUILD_DIR := @BUILD_DIR@
HAVE_ZLIB := @HAVE_ZLIB@
HAVE_BZLIB := @HAVE_BZLIB@

# This may be confusing. These variables are only defined when we're not building with
# (b)zlib because we're going to filter them out of the file lists. We could avoid using the
# filter function for the headers, but then (B)ZLIB_HEADER_FILES would only be defined if
# we were building with zlib, and (B)ZLIB_OBJC_FILES would only be defined if we weren't.
# I think that would be pretty confusing...
ifneq (${HAVE_ZLIB},yes)
ZLIB_HEADER_FILES := ZlibInStream.h ZlibOutStream.h GzipOutStream.h GzipInStream.h
ZLIB_OBJC_FILES := ZlibInStream.m ZlibOutStream.m GzipOutStream.m GzipInStream.m
endif
ifneq ($(HAVE_BZLIB),yes)
BZLIB_HEADER_FILES := BzlibInStream.h BzlibOutStream.h
BZLIB_OBJC_FILES := BzlibInStream.m BzlibOutStream.m
endif

OBJECTIVELIB_PUBLIC_HEADERS := \
    $(filter-out $(BZLIB_HEADER_FILES) $(ZLIB_HEADER_FILES), \
    Config.h \
    Algorithm.h \
    Arithmetic.h \
    BitSet.h \
    BufferingInStream.h \
    BufferingOutStream.h \
    BzlibInStream.h \
    BzlibOutStream.h \
    Comparison.h \
    ConnectedInStream.h \
    ConnectedOutStream.h \
    Copying.h \
    DataInStream.h \
    DataOutStream.h \
    Deque.h \
    FileInStream.h \
    FileOutStream.h \
    Functional.h \
    GzipInStream.h \
    GzipOutStream.h \
    HashMap.h \
    HashSet.h \
    HashTable.h \
    InStream.h \
    Iterator.h \
    LayeredInStream.h \
    LayeredOutStream.h \
    List.h \
    Map.h \
    Memory.h \
    ObjectInStream.h \
    ObjectOutStream.h \
    OutStream.h \
    Pair.h \
    Queue.h \
    Set.h \
    Socket.h \
    SocketAddress.h \
    Stack.h \
    Streamable.h \
    Types.h \
    Vector.h \
    ZlibInStream.h \
    ZlibOutStream.h)

test-comma := ,
test-names-final := $(foreach name,$(subst $(test-comma), ,$(test-names)),-n $(name))
test-test-final := $(foreach test,$(test-test),-t $(test))
test-pause-final := $(if $(test-pause),-m)
test-list-final := $(if $(test-list),-l)

ifeq ($(BUILD_COCOA),yes)

XCODE_PATH := @XCODE_PATH@
FWK_DIR := @FWK_DIR@
XCODE_TARGET := @XCODE_TARGET@

.PHONY: framework framework-debug tests check install clean distclean

framework: ObjectiveLib.h
	$(XCODE_PATH) -target "$(XCODE_TARGET)" -buildstyle Deployment build BUILD_DIR=$(BUILD_DIR) OBJROOT=$(BUILD_DIR)
	cp License.txt $(BUILD_DIR)/ObjectiveLib.framework

framework-debug: ObjectiveLib.h
	$(XCODE_PATH) -target "$(XCODE_TARGET)" -buildstyle Development build BUILD_DIR=$(BUILD_DIR) OBJROOT=$(BUILD_DIR)
	cp License.txt $(BUILD_DIR)/ObjectiveLib.framework

tests: framework
	$(MAKE) --directory=test testapp

run-tests: tests
	DYLD_FRAMEWORK_PATH=$(BUILD_DIR) $(BUILD_DIR)/TestObjectiveLib.app/Contents/MacOS/TestObjectiveLib $(test-test-final) $(test-names-final) $(test-pause-final) $(test-list-final)

install: framework install-doc
	$(XCODE_PATH) -buildstyle Deployment install BUILD_DIR=$(BUILD_DIR) INSTALL_PATH=$(FWK_DIR)

clean:
	-rm -fr $(BUILD_DIR)

distclean: clean
	rm -fr doc autom4te.cache config.log config.status GNUMakefile Config.h \
	ConfigPrivate.h test/GNUMakefile test/Config.h DocTools/Doxygen.cfg \
	ObjectiveLib.xcode/*.pbxuser ObjectiveLib.xcode/*.mode1 \
	test/TestObjectiveLib.xcode/*.pbxuser test/TestObjectiveLib.xcode/*.mode1 \
	.DS_Store DocTools/.DS_Store ObjectiveLib.h

help:
	@echo "Supported targets:"
	@echo
	@echo "framework"
	@echo "    Build the release version of the framework"
	@echo "framework-debug"
	@echo "    Build the debug version of the framework"
	@echo "tests"
	@echo "    Build the framework and the test suite"
	@echo "run-tests"
	@echo "    Build the framework and the test suite, then run the test suite"
	@echo "install"
	@echo "    Build the release version of the framework and the documentation and install them"
	@echo "doc"
	@echo "    Build the documentation"
	@echo "install-doc"
	@echo "    Build and install the documentation"
	@echo "sf-doc"
	@echo "    Build API documentation for posting at SourceForge. Use only if you're me, which you aren't."
	@echo "clean"
	@echo "    Remove the built framework and the test suite and all intermediate files"
	@echo "distclean"
	@echo "    Perform 'clean' plus remove the documentation and all files produced by configure"
	@echo "    and Xcode user files"
	@echo

else

include $(GNUSTEP_MAKEFILES)/common.make

ADDL_GNUSTEP_LIBS := @ADDL_GNUSTEP_LIBS@
IS_WINDOWS := $(if $(filter mingw32 cygwin, $(GNUSTEP_HOST_OS)),yes,no)
GDB_PATH := @GDB_PATH@

.PHONY: tests check

LIBRARY_NAME := libObjectiveLib
libObjectiveLib_OBJC_FILES := $(filter-out $(BZLIB_OBJC_FILES) $(ZLIB_OBJC_FILES), $(wildcard *.m))
libObjectiveLib_HEADER_FILES := $(OBJECTIVELIB_PUBLIC_HEADERS) ObjectiveLib.h
libObjectiveLib_HEADER_FILES_INSTALL_DIR := ObjectiveLib

ifeq ($(IS_WINDOWS), yes)
libObjectiveLib_DLL_DEF := objectivelib.def
endif

-include GNUmakefile.preamble
include $(GNUSTEP_MAKEFILES)/library.make
-include GNUmakefile.postamble

tests:
	$(MAKE)
	$(MAKE) --directory=test

run-tests: tests
ifeq ($(IS_WINDOWS), yes)
	PATH="${PATH}:$(LD_LIBRARY_PATH):$(GNUSTEP_OBJ_DIR)" test/$(GNUSTEP_OBJ_DIR)/TestObjectiveLib $(test-test-final) $(test-names-final) $(test-pause-final) $(test-list-final)
else
	LD_LIBRARY_PATH="${LD_LIBRARY_PATH}:$(GNUSTEP_OBJ_DIR)" test/$(GNUSTEP_OBJ_DIR)/TestObjectiveLib $(test-test-final) $(test-names-final) $(test-pause-final) $(test-list-final)
endif

ifdef GDB_PATH
run-gdb: tests
ifeq ($(IS_WINDOWS), yes)
	PATH="${PATH}:$(LD_LIBRARY_PATH):$(GNUSTEP_OBJ_DIR)" $(GDB_PATH) test/$(GNUSTEP_OBJ_DIR)/TestObjectiveLib
else
	LD_LIBRARY_PATH="${LD_LIBRARY_PATH}:$(GNUSTEP_OBJ_DIR)" $(GDB_PATH) test/$(GNUSTEP_OBJ_DIR)/TestObjectiveLib
endif
else
run-gdb:
endif

endif

ifdef DOXYGEN_PATH

DOC_DIR := @DOC_DIR@

doc:
	mkdir -p $(BUILD_DIR)
	$(DOXYGEN_PATH) DocTools/Doxygen.cfg
	cp DocTools/ObjectiveLibLogoTiny.gif doc/html

install-doc: doc
	mkdir -p "$(DOC_DIR)/ObjectiveLib Documentation"
	cp -R doc/* "$(DOC_DIR)/ObjectiveLib Documentation"

sf-doc:
	mv DocTools/ObjectiveLibFooter.html DocTools/ObjectiveLibFooter.html.GodOfAwk
	awk '/^<!--|^-->/ {next} {print > "DocTools/ObjectiveLibFooter.html"}' DocTools/ObjectiveLibFooter.html.GodOfAwk
	$(MAKE) doc
	mv -f DocTools/ObjectiveLibFooter.html.GodOfAwk DocTools/ObjectiveLibFooter.html

else

doc:
install-doc:
sf-doc:

endif

ObjectiveLib.h:
	echo "$(foreach HEADER, $(OBJECTIVELIB_PUBLIC_HEADERS),#import <ObjectiveLib/$(HEADER)>&)" | tr \& \\n | sed -e 's/ #/#/' | cat OLHeaderHeader.txt - OLHeaderFooter.txt > ObjectiveLib.h
