/*
 * $Id$
 *
 * Copyright (c) 2004, Raphael Manfredi
 *
 * Jmakefile for common sources.
 *
 *----------------------------------------------------------------------
 * This file is part of gtk-gnutella.
 *
 *  gtk-gnutella is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  gtk-gnutella 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 General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with gtk-gnutella; if not, write to the Free Software
 *  Foundation, Inc.:
 *      59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *----------------------------------------------------------------------
 */

;# $Id$

/*
 * Generation of bit_array.h and bit_field.h from a generic template.
 */

GENERIC = bit_array.h bit_field.h htable.h hset.h htable.c hset.c

bit_array.h: bit_array.ht bit_generic.t
	$(TOP)/scripts/generic-cat bit_array.ht bit_generic.t array >$@

bit_field.h: bit_field.ht bit_generic.t
	$(TOP)/scripts/generic-cat bit_field.ht bit_generic.t field >$@

htable.h: hgeneric.ht
	$(TOP)/scripts/generic-pp /dev/null hgeneric.ht table ckeyval TABLE >$@

htable.c: hgeneric.ct
	$(TOP)/scripts/generic-pp /dev/null hgeneric.ct table ckeyval TABLE >$@

hset.h: hgeneric.ht
	$(TOP)/scripts/generic-pp /dev/null hgeneric.ht set cdata SET >$@

hset.c: hgeneric.ct
	$(TOP)/scripts/generic-pp /dev/null hgeneric.ct set cdata SET >$@

all:: $(GENERIC)

local_realclean::
	$(RM) $(GENERIC)

depend:: $(GENERIC)

LSRC = \
	adns.c \
	aging.c \
	alloca.c \
	arc4random.c \
	ascii.c \
	atoms.c \
	base16.c \
	base32.c \
	base64.c \
	bfd_util.c \
	bg.c \
	bigint.c \
	bstr.c \
	chi2.c \
	ckalloc.c \
	cobs.c \
	compat_misc.c \
	compat_pause.c \
	compat_pio.c \
	compat_poll.c \
	compat_sleep_ms.c \
	compat_un.c \
	concat.c \
	constants.c \
	cpufreq.c \
	cq.c \
	crash.c \
	crc.c \
	dbmap.c \
	dbmw.c \
	dbstore.c \
	dbus_util.c \
	debug.c \
	dl_util.c \
	dualhash.c \
	elist.c \
	entropy.c \
	erbtree.c \
	eslist.c \
	etree.c \
	eval.c \
	event.c \
	exit.c \
	fast_assert.c \
	fd.c \
	fifo.c \
	file.c \
	filehead.c \
	filename.c \
	float.c \
	fragcheck.c \
	fs_free_space.c \
	getcpucount.c \
	getdate.c \
	getgateway.c \
	gethomedir.c \
	getline.c \
	getphysmemsize.c \
	glib-missing.c \
	glog.c \
	gnet_host.c \
	halloc.c \
	hash.c \
	hashing.c \
	hashlist.c \
	hashtable.c \
	header.c \
	hevset.c \
	hikset.c \
	host_addr.c \
	hset.c \
	htable.c \
	html.c \
	http_range.c \
	idtable.c \
	inputevt.c \
	iprange.c \
	ipset.c \
	iso3166.c \
	leak.c \
	list.c \
	log.c \
	magnet.c \
	malloc.c \
	map.c \
	mem.c \
	mempcpy.c \
	memusage.c \
	mime_type.c \
	mingw32.c \
	misc.c \
	mtwist.c \
	mutex.c \
	nid.c \
	nv.c \
	offtime.c \
	ohash_table.c \
	omalloc.c \
	once.c \
	options.c \
	ostream.c \
	pagetable.c \
	palloc.c \
	parse.c \
	path.c \
	patricia.c \
	pattern.c \
	pmsg.c \
	pow2.c \
	product.c \
	prop.c \
	rand31.c \
	random.c \
	rbtree.c \
	regex.c \
	sectoken.c \
	sequence.c \
	sha1.c \
	shuffle.c \
	signal.c \
	slist.c \
	smsort.c \
	sorted_array.c \
	spinlock.c \
	stacktrace.c \
	stats.c \
	str.c \
	stringify.c \
	strtok.c \
	symbols.c \
	symtab.c \
	tea.c \
	thread.c \
	tiger.c \
	tigertree.c \
	timestamp.c \
	tm.c \
	url.c \
	url_factory.c \
	urn.c \
	utf8.c \
	vector.c \
	vendors.c \
	vmm.c \
	vsort.c \
	walloc.c \
	watcher.c \
	wd.c \
	wordvec.c \
	wq.c \
	xmalloc.c \
	xsort.c \
	zalloc.c \
	zlib_util.c

/* Object files are derived from source files */
LOBJ = \
|expand f!$(LSRC)!
	!f:\.c=.o \
-expand \\

/* Additional flags for glib compilation, added in the substituted section */
++GLIB_CFLAGS $glibcflags

/* Additional flags for dbus compilation, added in the substituted section */
++DBUS_CFLAGS $dbuscflags

;# Those extra flags are expected to be user-defined
CFLAGS = -I$(TOP) -I.. $(GLIB_CFLAGS) $(DBUS_CFLAGS) -DCURDIR=$(CURRENT)
DPFLAGS = $(CFLAGS)

NormalLibraryTarget(shared,$(LSRC),$(LOBJ))
DependTarget()

/*
 * Since getdate.c is in CVS, they can compile even if they lack yacc.
 * Do that only for developpers (the ".package" file is required to be able
 * to run "metaconfig").
 */
getdate.c: getdate.y
	-@if test -f $(TOP)/.package; then \
		echo "	$(YACC) $(JYFLAGS) $<"; \
		$(YACC) $(JYFLAGS) $<; \
		echo "	$(MV) y.tab.c $@"; \
		$(MV) y.tab.c $@; else touch $@; fi

getdate: getdate.c
	$(CC) -o $@ $(JCFLAGS) -DTEST $< libshared.a $(LIBS)

;#
;# Floating point formatting tests
;#
;# The "bad-fixed" target show formatting errors: the output file MUST be empty
;# or there are bugs in the floating point formatting routine.
;#

++GLIB_LDFLAGS $glibldflags
++COMMON_LIBS $libs

LDFLAGS =
LIBS = $(GLIB_LDFLAGS) $(COMMON_LIBS)

floats: floats.bz2
	bzip2 -c -d floats.bz2 >$@

float-dragon.out: float-dragon_out.bz2
	bzip2 -c -d float-dragon_out.bz2 >$@

bad-fixed: float-test floats
	./float-test floats compare >$@

bad-free: float-test floats float-dragon.out
	./float-test floats dragon | diff -u - float-dragon.out >$@

float-times: float-test floats
	uname -a >$@
	echo -n "base:    " >>$@
	sh -c "time ./float-test floats base" >>$@ 2>&1
	echo -n "sprintf: " >>$@
	sh -c "time ./float-test floats sprintf" >>$@ 2>&1
	echo -n "printf:  " >>$@
	sh -c "time ./float-test floats printf > /dev/null" >>$@ 2>&1
	echo -n "fixed:   " >>$@
	sh -c "time ./float-test floats fixed > /dev/null" >>$@ 2>&1
	echo -n "free:    " >>$@
	sh -c "time ./float-test floats dragon > /dev/null" >>$@ 2>&1

local_clean::
	$(RM) floats float-dragon.out bad-fixed float-times

NormalProgramLibTarget(float-test, float-test.c, float-test.o, libshared.a)
NormalProgramLibTarget(sort-test, sort-test.c, sort-test.o, libshared.a)
NormalProgramLibTarget(random-test, random-test.c, random-test.o, libshared.a)

