# The sources to the bprof program I made myself
MYSRCS = bprof.cc execute.cc bmonout.cc sources.cc
MYHDRS = execute.h bmonout.h sources.h
MYOBJS = bprof.o execute.o bmonout.o sources.o

# The sources of bprof made by genclass from libg++
# Put them in an archive as advised in the manual.
GSRCS = String.sfpnt.VHMap.cc String.sfpnt.Map.cc
GHDRS = String.sfpnt.VHMap.h String.sfpnt.Map.h String.defs.h
# Needed to truncate these names or ar would map them to
# the same name.  How stupid.
GOBJS = VHMap.o Map.o

# The sources to libbmon.a
LIBSRCS = bmon.c
LIBHDRS =
LIBOBJS = bmon.o

PROG = bprof
# I first used the lib(file.o) feature of make, but this turned
# out to be recent, so not everywhere present.
BLIB = libbmon.a
GLIB = libgpp.a

CC = gcc
CXX = g++
# I like -Wshadow, but the $(GSRCS) files generate too many
# warnings with it.
CFLAGS = -Wall -O2
CXXFLAGS = -Wall -O2

INSTALL = install
PREFIX = /usr
BINDIR = $(PREFIX)/bin
LIBDIR = $(PREFIX)/lib
MANDIR = $(PREFIX)/man/man1

all: $(PROG) $(BLIB)

.PHONY: clean

# Somewhat weird rule to fool make into not remaking depend every
# time I make a small change to a source file.  This gets only
# executed if I explicitly "make depend".
depend::
	gcc -M $(MYSRCS) $(LIBSRCS) $(GSRCS) > $@

$(PROG): $(MYOBJS) $(GLIB)
	$(CXX) -o $@ $(MYOBJS) $(GLIB)

# Make sure bmon.c is compiled with -g
bmon.o: bmon.c
	$(CC) -g $(CFLAGS) -c $<

VHMap.o: String.sfpnt.VHMap.o
	ln -f $< $@

Map.o: String.sfpnt.Map.o
	ln -f $< $@

$(GLIB): $(GOBJS)
	ar r $@ $?

$(BLIB): $(LIBOBJS)
	ar r $@ $?

bprof.cat: bprof.1
	groff -Tascii -ww -man $< > $@

# Using GNU install -d would also create the lib/man
# directories with mode 644.  Weird.
install: $(PROG) $(BLIB)
	mkdir -p $(BINDIR) $(LIBDIR) $(MANDIR)
	$(INSTALL) -s -m 755 $(PROG) $(BINDIR)
	$(INSTALL) -m 644 $(BLIB) $(LIBDIR)
	$(INSTALL) -m 644 bprof.1 $(MANDIR)

clean:
	rm -f *.o $(PROG) $(GLIB) $(BLIB) core

# All non-source files to distribute
TEXT = README bprof.1 bprof.cat Makefile depend bprof.lsm version COPYING NEWS
DIST = $(TEXT) $(MYSRCS) $(MYHDRS) $(LIBSRCS) $(LIBHDRS) $(GSRCS) $(GHDRS)
VERSION = $(shell cat version)

# I can't stand tar files that unpack in the current directory
tar:	$(DIST)
	tar -z -f bprof-$(VERSION).tar.gz -C .. -c $(addprefix bprof/,$^)

include depend
