#
# Makefile for LD_PRELOAD pthread_mutex_attr_util
#
# Written by Roland Mainz <roland.mainz@nrubsig.org>
#

#
#
# ToDo:
# - cleanup (this Makefile is horrible and should actually use macros)
# - build 32bit and 64bit at the same time
#

SHELL=/bin/ksh93

CSTDFLAGS=-std=c17
CARCHFLAGS=-m32 # -m32 or -m64
CC=gcc # gcc or clang

#
# misc
#
all:	tests pthread_mutex_attr_util_binaries.tar.bz2

clean:
	rm -f \
		*.o \
		*.so \
		test1 \
		pthread_mutex_attr_util_wrapper \
		pthread_mutex_attr_util_binaries.tar.bz2

#
# build shared library object for LD_PRELOAD
#
pthread_mutex_attr_util.o: pthread_mutex_attr_util.c
	$(CC) $(CSTDFLAGS) $(CARCHFLAGS) -Wall -g pthread_mutex_attr_util.c -fPIC -rdynamic -c -o pthread_mutex_attr_util.o
pthread_mutex_attr_util.so: pthread_mutex_attr_util.o
	$(CC) $(CSTDFLAGS) $(CARCHFLAGS) -Wall -g pthread_mutex_attr_util.o -fPIC -rdynamic -shared -ldl -lpthread -o pthread_mutex_attr_util.so

#
# compile wrapper script
#
pthread_mutex_attr_util_wrapper: pthread_mutex_attr_util_wrapper.ksh
	shcomp pthread_mutex_attr_util_wrapper.ksh >pthread_mutex_attr_util_wrapper
	chmod a+x pthread_mutex_attr_util_wrapper

#
# tests
#

# -rdynamic is required for gcc so that we get accurate function
# names from |backtrace()|&co
test1: test1.c
	$(CC) $(CSTDFLAGS) $(CARCHFLAGS) -Wall -rdynamic -g test1.c -o test1
	
tests: test1 pthread_mutex_attr_util.so pthread_mutex_attr_util_wrapper
	@printf "# Begin tests.\n"
	./pthread_mutex_attr_util_wrapper ./test1
	@printf "# End tests.\n"

#
# package binaries
#
pthread_mutex_attr_util_binaries.tar.bz2: \
	pthread_mutex_attr_util.so \
	pthread_mutex_attr_util_wrapper \
	pthread_mutex_attr_util_wrapper.ksh \
	README.pthread_mutex_attr_util
	tar -cvf - pthread_mutex_attr_util.so \
		pthread_mutex_attr_util_wrapper \
		pthread_mutex_attr_util_wrapper.ksh \
		README.pthread_mutex_attr_util | \
		bzip2 -9 >"pthread_mutex_attr_util_binaries.tar.bz2"

# EOF.
