# The sources to the bprof program
MYSRCS = bprof.cc execute.cc bmonout.cc sources.cc
MYHDRS = execute.h bmonout.h sources.h bmon.h $(wildcard arch/*.h)
MYOBJS = bprof.o execute.o bmonout.o sources.o

# The sources to bmon.o
LIBSRCS = bmon.cc
LIBOBJS = bmon.o

SRCS = $(MYSRCS) $(LIBSRCS)
HDRS = $(MYHDRS)

PROG = bprof
BLIB = bmon.o

CXX = gcc
# Warning flags
WFLAGS = -Wall
# Debug flags
#DFLAGS = -g
# Optimization flags
OFLAGS = -O2
# Put any necessary -I flags in IFLAGS
IFLAGS =
CXXFLAGS = $(WFLAGS) $(DFLAGS) $(OFLAGS) $(IFLAGS)

LDFLAGS = -s
#LDFLAGS = -g

LIBS = -lstdc++ -lbfd -liberty
# If I want to run bprof on itself
#LIBS = bmon.o -lstdc++ -lbfd -liberty

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

all: $(BLIB) $(PROG)

.PHONY: all clean tar

# 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::
	$(CXX) -M $(IFLAGS) $(SRCS) > $@

bprof: $(MYOBJS)
	$(CXX) $(LDFLAGS) -o $@ $^ $(LIBS)

# Make sure bmon.cc is compiled with -g
bmon.o: bmon.cc
	$(CXX) -g $(CXXFLAGS) -c $<

bprof.1: bprof.man
	sed -e 's:@LIBDIR@:$(LIBDIR):' $< > $@

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) bprof.1
	mkdir -p $(BINDIR) $(LIBDIR) $(MANDIR)
	$(INSTALL) -m 644 $(BLIB) $(LIBDIR)
	$(INSTALL) -m 644 bprof.1 $(MANDIR)
	$(INSTALL) -s -m 755 $(PROG) $(BINDIR)

clean:
	rm -f *~ *.o $(PROG) bprof.1 bprof.cat

# All non-source files to distribute
TEXT = README bprof.man Makefile bprof.lsm version COPYING NEWS PORTING
DIST = $(TEXT) $(SRCS) $(HDRS)
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
