# Samples of usage:
# compile with debug info: make OPTFLAGS=-g LDFLAGS= TARGET=rhash.debug
# compile for pentiumpro: make OPTFLAGS="-O2 -march=i586 -mcpu=pentiumpro"
# create rpm with statically linked program: make rpm LDFLAGS="-static -s -Wl,--gc-sections"
CC      = gcc
AR      = ar
#NOTE: NDEBUG turns off asserts, SHRFLAGS is used to make shared library
OPTFLAGS   = -O2 -DNDEBUG -fomit-frame-pointer -ffunction-sections -fdata-sections
OPTLDFLAGS =
SHRFLAGS   =
ADDCFLAGS  =
ADDLDFLAGS =
DEFFLAGS   = -DIN_RHASH
CFLAGS  := -pipe $(DEFFLAGS) $(ADDCFLAGS) $(OPTFLAGS) \
  -Wall -W -Wstrict-prototypes -Wnested-externs -Winline -Wpointer-arith \
  -Wbad-function-cast -Wmissing-prototypes -Wmissing-declarations
LDFLAGS = -L. -lrhash $(OPTLDFLAGS) $(ADDLDFLAGS)
HEADERS = algorithms.h aich.h timing.h byte_order.h plug_openssl.h rhash.h crc32.h util.h ed2k.h edonr.h hex.h md4.h md5.h sha1.h sha256.h sha512.h ripemd-160.h gost.h has160.h snefru.h tiger.h              tth.h torrent.h whirlpool.h
SOURCES = algorithms.c aich.c timing.c byte_order.c plug_openssl.c rhash.c crc32.c util.c ed2k.c edonr.c hex.c md4.c md5.c sha1.c sha256.c sha512.c ripemd-160.c gost.c has160.c snefru.c tiger.c tiger_sbox.c tth.c torrent.c whirlpool.c whirlpool_sbox.c
OBJECTS = algorithms.o aich.o timing.o byte_order.o plug_openssl.o rhash.o crc32.o util.o ed2k.o edonr.o hex.o md4.o md5.o sha1.o sha256.o sha512.o ripemd-160.o gost.o has160.o snefru.o tiger.o tiger_sbox.o tth.o torrent.o whirlpool.o whirlpool_sbox.o
HEADERS_LIB = rhash.h timing.h
# installation directories and names
DESTDIR =
PREFIX  = /usr/local
INCDIR  = $(PREFIX)/include/rhash
LIBDIR  = $(PREFIX)/lib
LIBRARY = librhash.a
SONAME  = librhash.so.0
SOLINK  = librhash.so
TEST_TARGET = test_sums
TEST_SHARED = test_shared
# Set variables according to GNU coding standard
INSTALL = install
INSTALL_DATA    = $(INSTALL) -m 644
INSTALL_SHARED  = $(INSTALL) -m 644

all: $(LIBRARY)
lib-static: $(LIBRARY)
lib-shared: $(SONAME)
dist-clean: clean

install-lib-static: $(LIBRARY)
	$(INSTALL) -d $(DESTDIR)$(LIBDIR) $(DESTDIR)$(INCDIR)
	$(INSTALL_DATA) $(LIBRARY) $(DESTDIR)$(LIBDIR)/
	$(INSTALL_DATA) $(HEADERS_LIB) $(DESTDIR)$(INCDIR)/
	ln -s $(SONAME) $(DESTDIR)$(LIBDIR)/$(SOLINK)

install-lib-shared: $(SONAME)
	$(INSTALL) -d $(DESTDIR)$(LIBDIR)
	$(INSTALL_SHARED) $(SONAME) $(DESTDIR)$(LIBDIR)/

uninstall-lib-static:
	rm -f $(DESTDIR)$(LIBDIR)/$(LIBRARY) $(DESTDIR)$(INCDIR)/*.h
	rm -f $(DESTDIR)$(LIBDIR)/$(SOLINK)
	-rmdir $(DESTDIR)$(INCDIR)

uninstall-lib-shared:
	rm -f $(DESTDIR)$(LIBDIR)/$(SONAME)

install-all-headers:
	$(INSTALL_DATA) $(HEADERS) $(DESTDIR)$(INCDIR)/

# not using GNU make extensions for compatibility with Unix/*BSD make
#%.o: %.c
#	$(CC) -c $(CFLAGS) $< -o $@

# NOTE: dependences were generated by 'gcc -MM -DIN_RHASH *.c'
# we are using plain old makefile style to support BSD make
aich.o: aich.c byte_order.h config.h algorithms.h rhash.h aich.h sha1.h
	$(CC) -c $(CFLAGS) $< -o $@

algorithms.o: algorithms.c byte_order.h config.h rhash.h algorithms.h \
 aich.h sha1.h crc32.h ed2k.h md4.h edonr.h gost.h has160.h md5.h \
 ripemd-160.h snefru.h sha256.h sha512.h tiger.h torrent.h util.h tth.h \
 whirlpool.h
	$(CC) -c $(CFLAGS) $< -o $@

byte_order.o: byte_order.c byte_order.h config.h
	$(CC) -c $(CFLAGS) $< -o $@

crc32.o: crc32.c byte_order.h config.h crc32.h
	$(CC) -c $(CFLAGS) $< -o $@

ed2k.o: ed2k.c ed2k.h md4.h
	$(CC) -c $(CFLAGS) $< -o $@

edonr.o: edonr.c byte_order.h config.h edonr.h
	$(CC) -c $(CFLAGS) $< -o $@

gost.o: gost.c byte_order.h config.h gost.h
	$(CC) -c $(CFLAGS) $< -o $@

has160.o: has160.c byte_order.h config.h has160.h
	$(CC) -c $(CFLAGS) $< -o $@

hex.o: hex.c hex.h
	$(CC) -c $(CFLAGS) $< -o $@

md4.o: md4.c byte_order.h config.h md4.h
	$(CC) -c $(CFLAGS) $< -o $@

md5.o: md5.c byte_order.h config.h md5.h
	$(CC) -c $(CFLAGS) $< -o $@

plug_openssl.o: plug_openssl.c algorithms.h rhash.h byte_order.h config.h \
 plug_openssl.h
	$(CC) -c $(CFLAGS) $< -o $@

rhash.o: rhash.c byte_order.h config.h algorithms.h rhash.h torrent.h \
 util.h sha1.h plug_openssl.h hex.h
	$(CC) -c $(CFLAGS) $< -o $@

ripemd-160.o: ripemd-160.c byte_order.h config.h ripemd-160.h
	$(CC) -c $(CFLAGS) $< -o $@

sha1.o: sha1.c byte_order.h config.h sha1.h
	$(CC) -c $(CFLAGS) $< -o $@

sha256.o: sha256.c byte_order.h config.h sha256.h
	$(CC) -c $(CFLAGS) $< -o $@

sha512.o: sha512.c byte_order.h config.h sha512.h
	$(CC) -c $(CFLAGS) $< -o $@

snefru.o: snefru.c byte_order.h config.h snefru.h
	$(CC) -c $(CFLAGS) $< -o $@

test_sums.o: test_sums.c byte_order.h config.h timing.h rhash.h \
 test_sums.h
	$(CC) -c $(CFLAGS) $< -o $@

tiger.o: tiger.c byte_order.h config.h tiger.h
	$(CC) -c $(CFLAGS) $< -o $@

tiger_sbox.o: tiger_sbox.c byte_order.h config.h
	$(CC) -c $(CFLAGS) $< -o $@

timing.o: timing.c byte_order.h config.h rhash.h timing.h
	$(CC) -c $(CFLAGS) $< -o $@

torrent.o: torrent.c byte_order.h config.h algorithms.h rhash.h torrent.h \
 util.h sha1.h
	$(CC) -c $(CFLAGS) $< -o $@

tth.o: tth.c byte_order.h config.h tth.h tiger.h
	$(CC) -c $(CFLAGS) $< -o $@

util.o: util.c util.h
	$(CC) -c $(CFLAGS) $< -o $@

whirlpool.o: whirlpool.c byte_order.h config.h whirlpool.h
	$(CC) -c $(CFLAGS) $< -o $@

whirlpool_sbox.o: whirlpool_sbox.c byte_order.h config.h
	$(CC) -c $(CFLAGS) $< -o $@

# MINGW dll target
DLLNAME = librhash.dll

dll: $(DLLNAME)
$(DLLNAME): $(SOURCES)
#	rm -f $@
	sed -n '1s/.*/{ global:/p; s/^RHASH_API.* \([a-z0-9_]\+\)(.*/  \1;/p; $$s/.*/local: *; };/p' rhash.h timing.h > exports.sym
	$(CC) -shared -DRHASH_EXPORTS $(CFLAGS) -Wl,--version-script,exports.sym,-soname,$@ $(OPTLDFLAGS) $(SOURCES) -o $@

test-dll: $(DLLNAME) test_sums.o
	$(CC) test_sums.o $(DLLNAME) -o $(TEST_SHARED) && ./$(TEST_SHARED)

# shared and static libraries
$(SONAME): $(SOURCES)
#	rm -f $@
	sed -n '1s/.*/{ global:/p; s/^RHASH_API.* \([a-z0-9_]\+\)(.*/  \1;/p; $$s/.*/local: *; };/p' rhash.h timing.h > exports.sym
	$(CC) -fpic -shared $(CFLAGS) -Wl,--version-script,exports.sym,-soname,$@ $(OPTLDFLAGS) $(SOURCES) -o $@
# use 'nm -Cg --defined-only $@' to view exported symbols

$(LIBRARY): $(OBJECTS)
	$(AR) cqs $@ $(OBJECTS)
#	$(AR) cr $(LIBRARY) $(OBJECTS) && runlib $(LIBRARY)

$(TEST_TARGET): $(LIBRARY) test_sums.o
	$(CC) test_sums.o $(LDFLAGS) -o $@

#$(TEST_SHARED): $(DLLNAME) test_sums.o
#	$(CC) test_sums.o $(DLLNAME) -o $@

test-shared: $(SONAME) test_sums.o
	$(CC) test_sums.o $(SONAME) $(LDFLAGS) -o $(TEST_SHARED)
	LD_LIBRARY_PATH=. ./$(TEST_SHARED)

test: $(TEST_TARGET)
	./$(TEST_TARGET)
	if [ -f $(SONAME) ]; then make test-shared; fi
#	if [ -f $(DLLNAME) ]; then make test-dll; fi

clean:
	rm -f *.o $(LIBRARY) $(TEST_TARGET) $(TEST_SHARED) $(SONAME) exports.sym
