# Makefile for binaries.

.PHONY: all dirs saber tags TAGS

ifdef ARCH

ARCHBIN := $(ARCH)/$(BIN)
ARCHOBJS := $(addprefix $(ARCH)/,$(OBJS))

# Nasty bit of hackery to stick the architecture in all the libraries, ie:
# 		foo.a -lm ../bar.a
# becomes
#		sun3/foo.a ../sun3/bar.a -lm
#
# NOTE -- the order of the libraries is not retained ("-l"s are put at 
#         the end).  If someone can figure out how to do this right, please 
#         do.
RLIBS := $(filter-out -l%,$(LIBS))
OLIBS := $(filter -l%,$(LIBS))
XX1 := $(addsuffix $(ARCH)/,$(dir $(RLIBS)))
XX2 := $(notdir $(RLIBS))
ARCHLIBS := $(join $(XX1),$(XX2)) $(OLIBS)

all: dirs $(ARCHBIN)

$(ARCHBIN): $(ARCHOBJS) FRC
	$(CC) $(LFLAGS) $(ARCHOBJS) $(ARCHLIBS) $(TAILLIBS) -o $@

FRC::

clean:
	-rm -f $(ARCHOBJS) $(ARCHBIN)

.PHONY: $(BIN)
$(BIN): $(ARCH)/$(BIN)

install:
	rm -f $(BINDIR)/$(BIN)
	cp $(ARCH)/$(BIN) $(BINDIR)/$(BIN)

else

all: dirs $(BIN)

$(BIN): $(OBJS) $(LIBS)
	$(CC) $(LFLAGS) $(OBJS) $(LIBS) $(TAILLIBS) -o $@

clean:
	-rm -f $(OBJS) $(BIN)

install:
	rm -f $(BINDIR)/$(BIN)
	cp $(BIN) $(BINDIR)/$(BIN)

endif

saber:
	%load $(CFLAGS) $(SRCS) $(SFLAGS)

TAGSRCS := $(SRCDIR)/h/*.h *.c $(patsubst %,$(SRCDIR)/empmain/%/*.c,$(DIRS))

tags:
	@echo "Making tags in $(SRCDIR)/h . $(DIRS)..."
	@ctags $(TAGSRCS)
	@echo "done."

TAGS:
	@echo "Making tags in $(SRCDIR)/h . $(DIRS)..."
	@etags $(TAGSRCS)
	@echo "done."
