builddir = @builddir@
top_builddir = @top_builddir@
srcdir = @srcdir@
top_srcdir = @top_srcdir@

prefix		= @prefix@
exec_prefix	= @exec_prefix@
bindir		= @bindir@

VPATH = @srcdir@

CC              = @CC@
CFLAGS          = @CFLAGS@
WARNINGS	= @WARNINGS@
MDYNAMICNOPIC   = @MDYNAMICNOPIC@
CPPFLAGS        = @CPPFLAGS@
DEFS            = @DEFS@ @ENDIAN_FLAG@
LDFLAGS         = @LDFLAGS@
HAVE_PRIVEXT    = @HAVE_PRIVEXT@
HAVE_FOREIGN_HEADERS    = @HAVE_FOREIGN_HEADERS@

ifeq ($(HAVE_PRIVEXT),NO)
INCPRIVEXT      = -include $(top_srcdir)/include/extern.h
endif

ifeq ($(HAVE_FOREIGN_HEADERS),YES)
FORHDRS         = -I$(top_srcdir)/include/foreign
endif

INSTALL		= @INSTALL@
INSTALL_PROGRAM	= @INSTALL_PROGRAM@
transform       = @program_transform_name@
LD64PROG	= $(shell echo ld64 | sed '$(transform)')

MYINCLUDES      = -I$(builddir) -I$(srcdir) -I$(top_builddir)/include \
			-I$(top_srcdir)/include $(INCPRIVEXT) $(FORHDRS)
MYWARNINGS      = 
MYDEFS          = -DDEBUG -DBINDIR="\"$(bindir)\"" -DLD64PROG="\"$(LD64PROG)\""
MYLDFLAGS	= -L$(top_builddir)/libstuff
MYLIBS		= -lstuff

MYCOMPILEFLAGS	= $(WARNINGS) $(MYWARNINGS) $(DEFS) $(MYDEFS) \
	$(CPPFLAGS) $(MYINCLUDES) $(CFLAGS) $(MDYNAMICNOPIC) 

MYLINKFLAGS	= $(LDFLAGS) $(MYLDFLAGS) $(MYLIBS)

CFILES = ld.c pass1.c objects.c sections.c cstring_literals.c symbols.c \
         fvmlibs.c layout.c specs.c pass2.c generic_reloc.c rld.c sets.c \
         4byte_literals.c 8byte_literals.c literal_pointers.c dylibs.c \
         indirect_sections.c mod_sections.c i860_reloc.c ppc_reloc.c \
         m88k_reloc.c hppa_reloc.c sparc_reloc.c coalesced_sections.c

OBJS = $(CFILES:.c=.o) 
.c.o:
	$(CC) $(MYCOMPILEFLAGS) -c -o $@ $<

.PHONY: default clean distclean install

default: ld

ld: $(OBJS)
	$(CC) -o $@ $^ $(MYLINKFLAGS)

install: ld
	mkdir -p $(DESTDIR)/$(bindir)
	$(INSTALL_PROGRAM) ld \
		$(DESTDIR)/$(bindir)/`echo ld | sed '$(transform)'`

clean:
	rm -r -f *.o
	rm -f ld
