################################################################################
# Makefile for mdate++                                                         
# $Id: Makefile 59 2009-05-28 22:22:46Z ewe2 $
#
# Usage:
#	make target=linux					# linux
#	make target=cygwin					# cygwin 1.3.x
#	make target=mingw32-cross mdate.exe	# linux (Debian) win32 cross-compile
#	gmake target=freebsd				# FreeBSD 4.x target
#	make target=beos					# BeOS r5
#	make target=osx						# Mac OSX 32-bit console
#
# The default target is linux on x86 with a pentium-class processor.
# I can't guarantee the other targets will work, I haven't used Cygwin for
# quite a while now, it's probably out of date. Fixes would be most welcome.
#
# Update: the latest Debian packages for the mingw32 cross-compiler AND the
# latest getopt() source files fixes previous compilation issues for that
# target: we now have a working win32 port.
#
# If the CFLAGS bother you, please dont change -ffloat-store which avoids
# architecture-related problems.
#
# If you want to add a needed target for your OS/distribution, go ahead but
# please email me (Sean Dwyer <ewe2@users.sourceforge.net>) and include a
# patch of your changes so everyone can share!
#
# You may also want to check the documentation targets if you don't need some
# formats: grep for createdoc.
#
# Installation targets may need to be defined for your platform, see the freebsd
# target for an example.
################################################################################

## setup

.SUFFIXES: .c .cc .exe .o .obj
srcdir = .

PACKAGE=Mdate
VERSION=1.5.6

SVNVERSION = 1-5-6
# Use the trunk, Luke
REPOS=https://mdate.svn.sourceforge.net/svnroot/mdate/mdate

## DEFINE YOUR PREFERRED DEFAULT LANGUAGE HERE with a 1!
DEF_EN=
DEF_ES=
DEF_PL=
DEF_DE=
DEF_FR=

# if not set, set DEFLANG to a sane value. You could cheat and simply define
# your language here.
ifeq ($(DEFLANG),)
    DEFLANG=DEF_EN
endif
ifeq ($(DEF_EN),1)
	DEFLANG=DEF_EN
endif
ifeq ($(DEF_ES),1)
	DEFLANG=DEF_ES
endif
ifeq ($(DEF_DE),1)
	DEFLANG=DEF_DE
endif
ifeq ($(DEF_PL),1)
	DEFLANG=DEF_PL
endif
ifeq ($(DEF_FR),1)
	DEFLANG=DEF_FR
endif

## Debian packaging requires a DESTDIR variable. 
## If you're not packaging, change to taste.
DESTDIR =

# User servicable part here! Change for the defaults on your system. 
BINDIR = $(DESTDIR)/usr/bin
MANDIR = $(DESTDIR)/usr/share/man/man1
DOCDIR = $(DESTDIR)/usr//share/doc/mdate
HTMLDIR = $(DOCDIR)/html
LOCALEDIR = $(DESTDIR)/usr/share/locale

## define nosnprintf=1 if you don't have snprintf, nogetopt=1 if you don't
## have getopt, noi18n if you don't want i18n. This affects what gets built!
## define nodrem for a workable replacement

nosnprintf=
nogetopt=
noi18n=
nodrem=1

ifeq ($(target),mingw32-cross)
	nogetopt=1
	noi18n=1
endif

## define pretty if you want to use the old pretty output, otherwise we stick
## with the default one-line hardcore date(1) stuff. This renders the -p flag
## inoperative.
pretty=

ifeq ($(pretty),1)
	DEFS+=-DWANT_PRETTY
endif

## define newmayan if you want newfangled Mayan calendar month names 
## define oldmayan if you want default behaviour.
## you MUST define one!
newmayan=1
oldmayan=

ifneq ($(newmayan),)
DEFS+=-DNEWMAYAN
else
DEFS+=-DOLDMAYAN
endif

## autodetection of target, linux by default
#ifneq ($(strip $(wildcard /usr/include/linux)),)
#target = linux
#endif

## no automatic target, show help
target = help
		 
OBJS=lang.o mdate.o cmdline.o main.o
		  
ifneq ($(nosnprintf),)
OBJS += snprintf.o 
endif

ifneq ($(nogetopt),)
OBJS += getopt.o getopt1.o
endif
			  
ifneq ($(noi18n),)
LIBS += -lintl
endif

## fallthrough drem replacement if no target defined
ifneq ($(nodrem),)
DEFS+=-DUSE_MYDREM
endif


## Default compiler. On Debian, we're up to gcc-4.2, so check your cflags!
# Updated for the nuisance deprecation of string conversions 
CXX=g++
CC=gcc
INC=-I/usr/include -I.
DEFS+=-DHAVE_CONFIG_H -D$(DEFLANG) -DPACKAGE=\"$(PACKAGE)\" -DVERSION=\"$(VERSION)\"
CFLAGS=-O -Wall -Wno-write-strings -ffloat-store $(DEFS)
CXXFLAGS=$(CFLAGS) -fno-rtti
LIBS=-lm

## define debug if you want to help debug mdate with gdb. otherwise, ignore.
debug=

ifeq ($(debug),1)
	CFLAGS+=-g
endif

## linux Target.
## If you're not sure about your -march or -mcpu, try uname -a: it will tell
## you what cpu your kernel supports. Also see /proc/cpuinfo.
## gcc 2.95 and below do NOT support -march=athlon !!
## new gcc versions (4.1.x and above) prefer -mtune to -march, if your gcc
## doesn't work with this parameter, revert

ifeq ($(target),linux)
# A Pentium II or above is i686. Doubtful that multiprocessor optimizations are
# useful here.
	CFLAGS += -mtune=pentium-m $(INC)
# an example for my net box AMD K6-2
#	CFLAGS += -march=i386 -mcpu=i586
endif

## freebsd target
## on 4.9 at least, don't use higher optimizations, they generally don't work.
## 4.x users will have to add -I. to INC and -lgnugetopt to LIBS
## FreeBSD 6.2 has the buggy 3.4.x which forces no local directory includes,
## but does have its own getopt.

ifeq ($(target),freebsd)
	INC=-I/usr/include -I/usr/local/include
	CFLAGS=-O -Wall -ffloat-store $(DEFS) $(INC)
	LIBS+=-L/usr/local/lib -lintl
endif

## cygwin 1.3.x target
ifeq ($(target),cygwin)
	CXXFLAGS += -march=i386 -mcpu=i586 $(INC) -I/usr/include/g++-3/
	CC += $(INC)
endif

## linux mingw32 cross-compiler
ifeq ($(target),mingw32-cross)
	CC = i586-mingw32msvc-gcc
	CXX = i586-mingw32msvc-g++
	CFLAGS += -mconsole -I.
endif

## BeOS has different paths
ifeq ($(target),beos)
	INC=
	CXXFLAGS += -I/boot/home/config/include -I./
	CC= gcc
	LIBS=-L/boot/home/config/lib -lintl
endif

## OSX target: this builds native binaries
ifeq ($(target),osx)
	DEFS +=-DIS_OSX
endif

## builds universal binaries, this will likely become default. ld up to 10.4.6
## does not support the -Wl syntax but may in the future, search Xcode docs for
## compiling universal binaries in the porting guide section. You shouldn't
## need them here as gcc will pass the link flags on.
##
## If compiling on Leopard, you have the option of the old MacOSX10.4u.sdk or the new one
##
ifeq ($(target),uniosx)
	CFLAGS += -isysroot /Developer/SDKs/MacOSX10.5.sdk -arch ppc -arch i386
#	LDFLAGS += -syslibroot,/Developer/SDKs/MacOSX10.5.sdk
	DEFS +=-DIS_OSX
endif

###################### END OF USER CONFIGURABLE SECTION ######################

## main targets

all: mdate

.PHONY: help clean docclean distclean updatecl tagall

mdate: $(OBJS)
	$(CXX) $(CFLAGS) $(INC) -o mdate $(OBJS) $(LDFLAGS) $(LIBS)

ifeq ($(target),mingw32-cross)
mdate.exe: all
	-cp mdate mdate.exe
endif

## documentation targets

# temporary direct make for Polish mdate docs until we have enough languages
# to justify unifying the make

ifeq ($(DEF_PL),1)
mdate_pl.html: mdate_pl.sgml
	[ -f mdate_pl.html ] || sgml2html -H header -F footer mdate_pl.sgml
endif

createdoc: mdate.pdf mdate.html mdate.tex mdate.dvi mdate.ps mdate.text

# prevent the HTML docs being accidently remade
mdate.html:	mdate.sgml
	[ -f mdate.html ] || sgml2html -H header -F footer mdate.sgml

# ditto mdate.text
mdate.text:
	[ -f mdate.text ] || sgml2txt mdate.sgml
	mv mdate.txt mdate.text
	
mdate.tex: mdate.sgml
	sgml2latex -o tex $<
	grep -v "{t1enc}" $@ > $@.tmp
	mv $@.tmp $@

mdate.dvi: mdate.tex
	latex $<
	latex $<
	
mdate.ps: mdate.dvi
	dvips -o $@ $<

mdate.pdf: mdate.tex
	pdflatex $<
	pdflatex $<
	pdflatex $<

## install targets

install: installbin

installbin: mdate installman
	@[ -d $(BINDIR) ] || mkdir -p $(BINDIR); \
	install -m 755 mdate $(BINDIR)/mdate

installman: mdate
	[ -d $(MANDIR) ] || mkdir -p $(MANDIR); \
    install -m 644 mdate.1 $(MANDIR); \
    gzip -9f $(MANDIR)/mdate.1
		

installdoc: createdoc installman
	[ -d $(DOCDIR) ] || mkdir -p $(DOCDIR); \
	install -m 644  mdate.dvi mdate.ps mdate.sgml $(DOCDIR); \
	gzip $(DOCDIR)/mdate.dvi $(DOCDIR)/mdate.ps $(DOCDIR)/mdate.sgml; \
	install -m 644 mdate.text $(DOCDIR);	gzip $(DOCDIR)/mdate.text ; \
	install -m 644  README NEWS ChangeLog GPL API $(DOCDIR); \
	install -m 644  AUTHORS Translators $(DOCDIR); \
	[ -d $(HTMLDIR) ] || mkdir -p $(HTMLDIR); \
	install -m 644  *.html $(HTMLDIR)
	cp $(HTMLDIR)/mdate.html $(HTMLDIR)/index.html

## dependencies
lang.o: lang.cc config.h lang.hh
mdate.o: mdate.cc config.h mdate.hh
cmdline.o: cmdline.cc config.h mdate.hh cmdline.h getopt.h lang.hh
main.o: main.cc config.h mdate.hh cmdline.h getopt.h
getopt.o: getopt.c config.h getopt.h
getopt1.o: getopt1.c config.h getopt.h
snprintf.o: snprintf.c config.h

distclean: clean docclean

SOURCES=*.cc *.c *.hh *.h
DOX=mdate.html mdate-*.html mdate.text NEWS README GPL API ChangeLog \
AUTHORS mdate.sgml Translators mdate.1 BUGS mdate_pl.sgml
CONFS=Makefile mdate.spec ChangeLog.header .cvsignore
DEBCONF=debian/*
DISTFILES= $(SOURCES) $(DOX) $(CONFS)

# developers targets only!

# we now need to use svn; I use a wrapper around svn2cl.xsl; we don't tag
# willy-nilly as a tag is just a snapshot directory in the repository, so the
# release target is the LAST thing we want to do. I don't know whether silent
# ChangeLog commits will turn up; I assume they do.
#
# svn2cl has now been debianized so i am using that with appropriate flags.


updatesvn:
	cat ChangeLog.header > ChangeLog
	svn2cl -i -r HEAD:0 --group-by-day --stdout >> ChangeLog

# this is now how a release is archived. dorelease and dotag are NOT really
# the same: dorelease is a stable branch off the working trunk, dotags is the
# final package for release.

dorelease:
	svn copy $(REPOS)/trunk $(REPOS)/branches/mdate-$(SVNVERSION)

dotag:
	svn copy $(REPOS)/trunk $(REPOS)/tags/mdate-$(SVNVERSION)

distdir = $(PACKAGE)-$(VERSION)
debdir = $(distdir)/debian
dist:
	rm -fr $(distdir)
	mkdir -p $(debdir)
	chmod 777 $(distdir)
	chmod 777 $(debdir)
	for file in $(DISTFILES); do \
		ln $(srcdir)$$file $(distdir) 2> /dev/null \
		|| cp -p $(srcdir)/$$file $(distdir); \
	done
	for file in $(DEBCONF); do \
		cp -p $$file $(debdir); \
	done
	tar zchovf $(distdir).tar.gz $(distdir)
	rm -fr $(distdir)

# both these targets build outside the tree and are heavily system-dependent.
rpm:
	rpmbuild -ba mdate.spec

deb:
	dpkg-buildpackage -rfakeroot

clean:
	-rm -f mdate $(OBJS) *.o mdate.exe *~ *core

psclean:
	-rm -f *.dvi *.ps *.log *.tex *.out *.aux *.toc
	
pdfclean: psclean
	-rm -f *.pdf

docclean: psclean pdfclean
	-rm -f *.bak

help:
	@echo " "
	@echo "Usage:"
	@echo "   make target=linux"
	@echo "   make target=cygwin"
	@echo "   make target=mingw32-cross mdate.exe"
	@echo "   make target=beos"
	@echo "   gmake target=freebsd"
	@echo "   make target=osx"
	@echo "   make target=uniosx"
	@echo "The default target is linux on x86 with a pentium-class processor."
	@echo "I can't guarantee the other targets will work. I haven't used Cygwin for"
	@echo "quite a while, now it's probably out of date. Fixes would be most welcome."
	@echo "If the CFLAGS bother you please don't change -ffloat-store which avoids"
	@echo "architecture-related problems."
	@echo " "
