ifdef LLVM_CONFIG
LLVM_CPPFLAGS := -I$(shell $(LLVM_CONFIG) --includedir) -DLTO_SUPPORT
LLVM_LDFLAGS := -L$(shell $(LLVM_CONFIG) --libdir) -Wl,-rpath,$(shell $(LLVM_CONFIG) --libdir) -lLTO
endif

CPPFLAGS = $(LLVM_CPPFLAGS) -Isrc/abstraction -Isrc/ld -Isrc/ld/parsers $(OTHER_CPPFLAGS)
CFLAGS = -Os $(OTHER_CFLAGS)
CXXFLAGS = -Os $(OTHER_CXXFLAGS)
LDFLAGS = $(OTHER_LDFLAGS)

ifndef RANLIB
RANLIB = ranlib
endif
ifndef AR
AR = ar
endif
ifndef PREFIX
PREFIX = /usr
endif

# libprunetrie.a
all : src/ld/configure.h ObjectDump dyldinfo ld machocheck rebase unwinddump

src/ld/Snapshot.o : src/ld/compile_stubs.h
src/ld/compile_stubs.h : compile_stubs
	echo "static const char *compile_stubs = " > $@
	cat $^ | sed s/\"/\\\\\"/g | sed s/^/\"/ | sed s/$$/\\\\n\"/ >> $@
	echo ";" >> $@

src/ld/configure.h : src/create_configure
	DERIVED_SOURCES_DIR=src/ld DERIVED_FILE_DIR=src/ld $^ > $@

ObjectDump : src/ld/debugline.o
ObjectDump : src/ld/parsers/macho_relocatable_file.o
ObjectDump : src/ld/parsers/lto_file.o
ObjectDump : src/other/ObjectDump.o
	$(CXX) $(LLVM_LDFLAGS) $(LDFLAGS) $^ -o $@

dyldinfo : src/other/dyldinfo.o
	$(CXX) $(LDFLAGS) -Wl,-exported_symbol,__mh_execute_header $^ -o $@

ld : src/ld/debugline.o
ld : src/ld/ld.o
ld : src/ld/InputFiles.o
ld : src/ld/Options.o
ld : src/ld/OutputFile.o
ld : src/ld/Resolver.o
ld : src/ld/Snapshot.o
ld : src/ld/SymbolTable.o
ld : src/ld/parsers/archive_file.o
ld : src/ld/parsers/lto_file.o
ld : src/ld/parsers/macho_dylib_file.o
ld : src/ld/parsers/macho_relocatable_file.o
ld : src/ld/parsers/opaque_section_file.o
ld : src/ld/passes/branch_island.o
ld : src/ld/passes/branch_shim.o
ld : src/ld/passes/compact_unwind.o
ld : src/ld/passes/dtrace_dof.o
ld : src/ld/passes/dylibs.o
ld : src/ld/passes/got.o
ld : src/ld/passes/huge.o
ld : src/ld/passes/objc.o
ld : src/ld/passes/order.o
ld : src/ld/passes/tlvp.o
ld : src/ld/passes/stubs/stubs.o
	$(CXX) $(LLVM_LDFLAGS) $(LDFLAGS) $(OTHER_LDFLAGS_LD64) -Wl,-exported_symbol,__mh_execute_header $^ -o $@

machocheck : src/other/machochecker.o
	$(CXX) $(LDFLAGS) $^ -o $@

rebase : src/other/rebase.o
	$(CXX) $(LDFLAGS) -Wl,-exported_symbol,__mh_execute_header $^ -o $@

unwinddump : src/other/unwinddump.o
	$(CXX) $(LDFLAGS) -Wl,-exported_symbol,__mh_execute_header $^ -o $@

src/other/PruneTrie.o : src/ld/configure.h
libprunetrie.a : src/other/PruneTrie.o
	$(AR) cru $@ $^
	$(RANLIB) $@

install : all
	install -d -m 755 $(DESTDIR)$(PREFIX)/bin
	install -d -m 755 $(DESTDIR)$(PREFIX)/lib
	install -d -m 755 $(DESTDIR)$(PREFIX)/include/mach-o
	install -d -m 755 $(DESTDIR)$(PREFIX)/share/man/man1

	install -m 755 ObjectDump $(DESTDIR)$(PREFIX)/bin
	install -m 755 dyldinfo   $(DESTDIR)$(PREFIX)/bin
	install -m 755 ld         $(DESTDIR)$(PREFIX)/bin
	install -m 755 machocheck $(DESTDIR)$(PREFIX)/bin
	install -m 755 rebase     $(DESTDIR)$(PREFIX)/bin
	install -m 755 unwinddump $(DESTDIR)$(PREFIX)/bin

	#install -m 644 src/other/prune_trie.h $(DESTDIR)$(PREFIX)/include/mach-o
	#install -m 644 libprunetrie.a $(DESTDIR)$(PREFIX)/lib

	install -m 644 doc/man/man1/dyldinfo.1   $(DESTDIR)$(PREFIX)/share/man/man1
	install -m 644 doc/man/man1/ld.1         $(DESTDIR)$(PREFIX)/share/man/man1
	install -m 644 doc/man/man1/ld64.1       $(DESTDIR)$(PREFIX)/share/man/man1
	install -m 644 doc/man/man1/rebase.1     $(DESTDIR)$(PREFIX)/share/man/man1
	install -m 644 doc/man/man1/unwinddump.1 $(DESTDIR)$(PREFIX)/share/man/man1

