+# Make sure there isn't any extranous whitespace on the LIBRARYNAME option
+LIBRARYNAME := $(strip $(LIBRARYNAME))
+
+LIBNAME_O := $(DESTLIBRELEASE)/lib$(LIBRARYNAME).so
+LIBNAME_P := $(DESTLIBPROFILE)/lib$(LIBRARYNAME).so
+LIBNAME_G := $(DESTLIBDEBUG)/lib$(LIBRARYNAME).so
+LIBNAME_AO := $(DESTLIBRELEASE)/lib$(LIBRARYNAME).a
+LIBNAME_AP := $(DESTLIBPROFILE)/lib$(LIBRARYNAME).a
+LIBNAME_AG := $(DESTLIBDEBUG)/lib$(LIBRARYNAME).a
+LIBNAME_OBJO := $(DESTLIBRELEASE)/$(LIBRARYNAME).o
+LIBNAME_OBJP := $(DESTLIBPROFILE)/$(LIBRARYNAME).o
+LIBNAME_OBJG := $(DESTLIBDEBUG)/$(LIBRARYNAME).o
+LIBNAME_BC := $(DESTLIBBYTECODE)/lib$(LIBRARYNAME).bc
+
+#--------------------------------------------------------------------
+# Library Targets
+# Modify the top level targets to build the desired libraries.
+#--------------------------------------------------------------------
+
+# dynamic target builds a shared object version of the library...
+dynamic:: $(DESTLIBCURRENT)/lib$(LIBRARYNAME).so
+bytecodelib:: $(LIBNAME_BC)
+bytecodelib-install:: $(LLVMGCCDIR)/bytecode-libs/lib$(LIBRARYNAME).bc
+
+$(LLVMGCCDIR)/bytecode-libs/lib$(LIBRARYNAME).bc: $(LIBNAME_BC)
+ @echo ======= Installing $(LIBRARYNAME) bytecode library =======
+ cp $< $@
+
+# Does the library want a .o version built?
+ifndef DONT_BUILD_RELINKED
+all:: $(DESTLIBCURRENT)/$(LIBRARYNAME).o
+endif
+
+# Does the library want an archive version built?
+ifdef BUILD_ARCHIVE
+all:: $(DESTLIBCURRENT)/lib$(LIBRARYNAME).a
+endif
+
+#--------------------------------------------------------------------
+# Rules for building libraries
+#--------------------------------------------------------------------
+
+LinkBCLib := $(LLVMGCC) -shared
+ifdef EXPORTED_SYMBOL_LIST
+LinkBCLib += -Xlinker -internalize-public-api-list=$(EXPORTED_SYMBOL_LIST)
+else
+LinkBCLib += -Xlinker -disable-internalize
+endif
+
+
+# Rule for building bytecode libraries.
+$(LIBNAME_BC): $(ObjectsBC) $(LibSubDirs) $(DESTLIBBYTECODE)/.dir
+ @echo ======= Linking $(LIBRARYNAME) bytecode library =======
+ $(VERB) $(LinkBCLib) -o $@ $(ObjectsBC) $(LibSubDirs) $(LibLinkOpts)
+#
+# Rules for building dynamically linked libraries.
+#
+$(LIBNAME_O): $(ObjectsO) $(LibSubDirs) $(DESTLIBRELEASE)/.dir
+ @echo ======= Linking $(LIBRARYNAME) dynamic release library =======
+ $(VERB) $(Link) -o $*.la $(ObjectsO) $(LibSubDirs) $(LibLinkOpts)
+ $(VERB) $(LIBTOOL) --mode=install $(INSTALL) $*.la $(DESTLIBCURRENT)
+
+$(LIBNAME_P): $(ObjectsP) $(LibSubDirs) $(DESTLIBPROFILE)/.dir
+ @echo ======= Linking $(LIBRARYNAME) dynamic profile library =======
+ $(VERB) $(Link) -o $*.la $(ObjectsP) $(LibSubDirs) $(LibLinkOpts)
+ $(VERB) $(LIBTOOL) --mode=install $(INSTALL) $*.la $(DESTLIBCURRENT)
+
+$(LIBNAME_G): $(ObjectsG) $(LibSubDirs) $(DESTLIBDEBUG)/.dir
+ @echo ======= Linking $(LIBRARYNAME) dynamic debug library =======
+ $(VERB) $(Link) -o $*.la $(ObjectsG) $(LibSubDirs) $(LibLinkOpts)
+ $(VERB) $(LIBTOOL) --mode=install $(INSTALL) $*.la $(DESTLIBCURRENT)
+
+#
+# Rules for building static archive libraries.
+#
+$(LIBNAME_AO): $(ObjectsO) $(LibSubDirs) $(DESTLIBRELEASE)/.dir
+ @echo ======= Linking $(LIBRARYNAME) archive release library =======
+ @$(RM) -f $@
+ $(VERB) $(Link) -03 -o $@ $(ObjectsO) $(LibSubDirs) -static
+
+$(LIBNAME_AP): $(ObjectsP) $(LibSubDirs) $(DESTLIBPROFILE)/.dir
+ @echo ======= Linking $(LIBRARYNAME) archive profile library =======
+ @$(RM) -f $@
+ $(VERB) $(Link) -03 $(PROFILE) -o $@ $(ObjectsP) $(LibSubDirs) -static