Merging r259342 (with s/p2align 4/align 16) because r258750 is not in 3.8.
[oota-llvm.git] / Makefile.rules
index fdad178ea2563bb84d5e71afb8709cfbb4881d23..a67aef7c97aa161dd48fe1d51035293bf7f071f9 100644 (file)
 #--------------------------------------------------------------------
 # Define the various target sets
 #--------------------------------------------------------------------
-RecursiveTargets := all clean clean-all install uninstall install-bytecode
+RecursiveTargets := all clean clean-all install uninstall install-bytecode \
+                    unitcheck
 LocalTargets     := all-local clean-local clean-all-local check-local \
                     install-local printvars uninstall-local \
-                   install-bytecode-local unittests
+                   install-bytecode-local
 TopLevelTargets  := check dist dist-check dist-clean dist-gzip dist-bzip2 \
                     dist-zip unittests
 UserTargets      := $(RecursiveTargets) $(LocalTargets) $(TopLevelTargets)
@@ -41,7 +42,7 @@ VPATH=$(PROJ_SRC_DIR)
 # Reset the list of suffixes we know how to build.
 #--------------------------------------------------------------------
 .SUFFIXES:
-.SUFFIXES: .c .cpp .cc .h .hpp .o .a .bc .td .ps .dot .ll
+.SUFFIXES: .c .cpp .cc .h .hpp .o .a .td .ps .dot .m .mm
 .SUFFIXES: $(SHLIBEXT) $(SUFFIXES)
 
 #--------------------------------------------------------------------
@@ -56,6 +57,81 @@ VPATH=$(PROJ_SRC_DIR)
 
 $(UserTargets)::
 
+#------------------------------------------------------------------------
+# LLVMBuild Integration
+#------------------------------------------------------------------------
+#
+# We use llvm-build to generate all the data required by the Makefile based
+# build system in one swoop:
+#
+#  - We generate a file (a Makefile fragment) in the object root which contains
+#    all the definitions that are required by Makefiles across the entire
+#    project.
+#
+#  - We generate the library table used by llvm-config.
+#
+#  - We generate the dependencies for the Makefile fragment, so that we will
+#    automatically reconfigure outselves.
+
+# The path to the llvm-build tool itself.
+LLVMBuildTool  := $(PROJ_SRC_ROOT)/utils/llvm-build/llvm-build
+
+# The files we are going to generate using llvm-build.
+LLVMBuildMakeFrag := $(PROJ_OBJ_ROOT)/Makefile.llvmbuild
+LLVMBuildCMakeFrag := $(PROJ_OBJ_ROOT)/LLVMBuild.cmake
+LLVMBuildCMakeExportsFrag := $(PROJ_OBJ_ROOT)/cmake/modules/LLVMBuildExports.cmake
+LLVMBuildMakeFrags := \
+       $(LLVMBuildMakeFrag) \
+       $(LLVMBuildCMakeFrag) \
+       $(LLVMBuildCMakeExportsFrag)
+LLVMConfigLibraryDependenciesInc := \
+       $(PROJ_OBJ_ROOT)/tools/llvm-config/LibraryDependencies.inc
+
+# This is for temporary backwards compatibility.
+ifndef TARGET_NATIVE_ARCH
+TARGET_NATIVE_ARCH := $(ARCH)
+endif
+
+# The rule to create the LLVMBuild Makefile fragment as well as the llvm-config
+# library table.
+#
+# Note that this target gets its real dependencies generated for us by
+# llvm-build.
+#
+# We include a dependency on this Makefile to ensure that changes to the
+# generation command get picked up.
+$(LLVMBuildMakeFrags): $(PROJ_SRC_ROOT)/Makefile.rules \
+                      $(PROJ_OBJ_ROOT)/Makefile.config
+       $(Echo) Constructing LLVMBuild project information.
+       $(Verb)$(PYTHON) $(LLVMBuildTool) \
+         --native-target "$(TARGET_NATIVE_ARCH)" \
+         --enable-targets "$(TARGETS_TO_BUILD)" \
+         --enable-optional-components "$(OPTIONAL_COMPONENTS)" \
+         --write-library-table $(LLVMConfigLibraryDependenciesInc) \
+         --write-make-fragment $(LLVMBuildMakeFrag) \
+         --write-cmake-fragment $(LLVMBuildCMakeFrag) \
+         --write-cmake-exports-fragment $(LLVMBuildCMakeExportsFrag)
+
+# For completeness, let Make know how the extra files are generated.
+$(LLVMConfigLibraryDependenciesInc): $(LLVMBuildMakeFrags)
+
+# Include the generated Makefile fragment.
+#
+# We currently only include the dependencies for the fragment itself if we are
+# at the top-level. Otherwise, recursive invocations would ends up doing
+# substantially more redundant stat'ing.
+#
+# This means that we won't properly regenerate things for developers used to
+# building from a subdirectory, but that is always somewhat unreliable.
+ifeq ($(LEVEL),.)
+LLVMBUILD_INCLUDE_DEPENDENCIES := 1
+
+# Clean the generated makefile fragment at the top-level.
+clean-local::
+       -$(Verb) $(RM) -f $(LLVMBuildMakeFrags)
+endif
+-include $(LLVMBuildMakeFrag)
+
 ################################################################################
 # PRECONDITIONS: that which must be built/checked first
 ################################################################################
@@ -122,9 +198,6 @@ $(BUILT_SOURCES) : $(ObjMakefiles)
 reconfigure:
        $(Echo) Reconfiguring $(PROJ_OBJ_ROOT)
        $(Verb) cd $(PROJ_OBJ_ROOT) && \
-         if test -w $(PROJ_OBJ_ROOT)/config.cache ; then \
-           $(RM) $(PROJ_OBJ_ROOT)/config.cache ; \
-         fi ; \
          $(ConfigStatusScript) --recheck $(ConfigureScriptFLAGS) && \
          $(ConfigStatusScript)
 
@@ -132,9 +205,6 @@ reconfigure:
 $(ConfigStatusScript): $(ConfigureScript)
        $(Echo) Reconfiguring with $<
        $(Verb) cd $(PROJ_OBJ_ROOT) && \
-         if test -w $(PROJ_OBJ_ROOT)/config.cache ; then \
-           $(RM) $(PROJ_OBJ_ROOT)/config.cache ; \
-         fi ; \
          $(ConfigStatusScript) --recheck $(ConfigureScriptFLAGS) && \
          $(ConfigStatusScript)
 
@@ -157,9 +227,13 @@ endif
 # If the Makefile in the source tree has been updated, copy it over into the
 # build tree. But, only do this if the source and object makefiles differ
 #------------------------------------------------------------------------
+ifndef PROJ_MAKEFILE
+PROJ_MAKEFILE := $(PROJ_SRC_DIR)/Makefile
+endif
+
 ifneq ($(PROJ_OBJ_DIR),$(PROJ_SRC_DIR))
 
-Makefile: $(PROJ_SRC_DIR)/Makefile $(ExtraMakefiles)
+Makefile: $(PROJ_MAKEFILE) $(ExtraMakefiles)
        $(Echo) "Updating Makefile"
        $(Verb) $(MKDIR) $(@D)
        $(Verb) $(CP) -f $< $@
@@ -167,7 +241,7 @@ Makefile: $(PROJ_SRC_DIR)/Makefile $(ExtraMakefiles)
 # Copy the Makefile.* files unless we're in the root directory which avoids
 # the copying of Makefile.config.in or other things that should be explicitly
 # taken care of.
-$(PROJ_OBJ_DIR)/Makefile% : $(PROJ_SRC_DIR)/Makefile%
+$(PROJ_OBJ_DIR)/Makefile% : $(PROJ_MAKEFILE)%
        @case '$?' in \
           *Makefile.rules) ;; \
           *.in) ;; \
@@ -191,28 +265,6 @@ uninstall:: uninstall-local
 install-local:: all-local
 install-bytecode:: install-bytecode-local
 
-###############################################################################
-# LLVMC: Provide rules for compiling llvmc plugins
-###############################################################################
-
-ifdef LLVMC_PLUGIN
-
-LIBRARYNAME := $(patsubst %,plugin_llvmc_%,$(LLVMC_PLUGIN))
-REQUIRES_EH := 1
-
-# Build a dynamic library if the user runs `make` directly from the plugin
-# directory.
-ifndef LLVMC_BUILTIN_PLUGIN
-LOADABLE_MODULE = 1
-endif
-
-# TableGen stuff...
-ifneq ($(BUILT_SOURCES),)
-BUILD_AUTOGENERATED_INC=1
-endif
-
-endif # LLVMC_PLUGIN
-
 ###############################################################################
 # VARIABLES: Set up various variables based on configuration data
 ###############################################################################
@@ -227,74 +279,120 @@ endif
 #--------------------------------------------------------------------
 
 CPP.Defines :=
-# OPTIMIZE_OPTION - The optimization level option we want to build LLVM with
-# this can be overridden on the make command line.
-ifneq ($(OS),MingW)
-  OPTIMIZE_OPTION := -O3
-else
-  OPTIMIZE_OPTION := -O2
-endif
+ifeq ($(ENABLE_OPTIMIZED),1)
+  BuildMode := Release
+  # Don't use -fomit-frame-pointer on Darwin or FreeBSD.
+  ifneq ($(HOST_OS), $(filter $(HOST_OS), Cygwin Darwin DragonFly FreeBSD GNU/kFreeBSD))
+    OmitFramePointer := -fomit-frame-pointer
+  endif
 
-ifdef ENABLE_PROFILING
-  BuildMode := Profile
-  CXX.Flags += $(OPTIMIZE_OPTION) -pg -g
-  C.Flags   += $(OPTIMIZE_OPTION) -pg -g
-  LD.Flags  += $(OPTIMIZE_OPTION) -pg -g
-  KEEP_SYMBOLS := 1
+  CXX.Flags += $(OPTIMIZE_OPTION) $(OmitFramePointer)
+  C.Flags   += $(OPTIMIZE_OPTION) $(OmitFramePointer)
+  LD.Flags  += $(OPTIMIZE_OPTION)
+  ifdef DEBUG_SYMBOLS
+    BuildMode := $(BuildMode)+Debug
+    CXX.Flags += -g
+    C.Flags   += -g
+    KEEP_SYMBOLS := 1
+  endif
 else
-  ifeq ($(ENABLE_OPTIMIZED),1)
-    BuildMode := Release
-    # Don't use -fomit-frame-pointer on Darwin or FreeBSD.
-    ifneq ($(OS),FreeBSD)
-    ifneq ($(OS),Darwin)
-      OmitFramePointer := -fomit-frame-pointer
-    endif
-    endif
-
-    # Darwin requires -fstrict-aliasing to be explicitly enabled.
-    ifeq ($(OS),Darwin)
-      EXTRA_OPTIONS += -fstrict-aliasing -Wstrict-aliasing
-    endif
-
-    CXX.Flags += $(OPTIMIZE_OPTION) $(OmitFramePointer)
-    C.Flags   += $(OPTIMIZE_OPTION) $(OmitFramePointer)
-    LD.Flags  += $(OPTIMIZE_OPTION)
+  ifdef NO_DEBUG_SYMBOLS
+    BuildMode := Unoptimized
+    CXX.Flags +=
+    C.Flags   +=
+    KEEP_SYMBOLS := 1
   else
     BuildMode := Debug
+    ifeq ($(ENABLE_SPLIT_DWARF), 1)
+    CXX.Flags += -gsplit-dwarf
+    C.Flags   += -gsplit-dwarf
+    else
     CXX.Flags += -g
     C.Flags   += -g
-    LD.Flags  += -g
+    endif
     KEEP_SYMBOLS := 1
   endif
 endif
 
-#ifeq ($(ENABLE_VISIBILITY_INLINES_HIDDEN),1)
-#    CXX.Flags += -fvisibility-inlines-hidden
-#endif
+ifeq ($(ENABLE_LIBCPP),1)
+  CXX.Flags +=  -stdlib=libc++
+  LD.Flags +=  -stdlib=libc++
+endif
+
+ifeq ($(ENABLE_CXX1Y),1)
+  CXX.Flags += -std=c++1y
+else
+  ifeq ($(HOST_OS), $(filter $(HOST_OS), Cygwin MingW))
+    # MinGW and Cygwin are a bit stricter and lack things like
+    # 'strdup', 'stricmp', etc in c++11 mode.
+    CXX.Flags += -std=gnu++11
+ else
+    CXX.Flags += -std=c++11
+ endif
+endif
+
+ifeq ($(ENABLE_WERROR),1)
+  CXX.Flags += -Werror
+  C.Flags += -Werror
+endif
+
+ifeq ($(ENABLE_PROFILING),1)
+  BuildMode := $(BuildMode)+Profile
+  CXX.Flags := $(filter-out -fomit-frame-pointer,$(CXX.Flags)) -pg -g
+  C.Flags   := $(filter-out -fomit-frame-pointer,$(C.Flags)) -pg -g
+  LD.Flags  := $(filter-out -fomit-frame-pointer,$(LD.Flags)) -pg
+  KEEP_SYMBOLS := 1
+endif
+
+ifeq ($(ENABLE_VISIBILITY_INLINES_HIDDEN),1)
+    CXX.Flags += -fvisibility-inlines-hidden
+endif
+
+ifdef ENABLE_EXPENSIVE_CHECKS
+  # GNU libstdc++ uses RTTI if you define _GLIBCXX_DEBUG, which we did above.
+  # See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=40160
+  REQUIRES_RTTI := 1
+endif
 
 # IF REQUIRES_EH=1 is specified then don't disable exceptions
 ifndef REQUIRES_EH
   CXX.Flags += -fno-exceptions
+else
+  # If the library requires EH, it also requires RTTI.
+  REQUIRES_RTTI := 1
 endif
 
-# IF REQUIRES_RTTI=1 is specified then don't disable run-time type id
-ifndef REQUIRES_RTTI
-#  CXX.Flags += -fno-rtti
+ifdef REQUIRES_FRAME_POINTER
+  CXX.Flags := $(filter-out -fomit-frame-pointer,$(CXX.Flags))
+  C.Flags   := $(filter-out -fomit-frame-pointer,$(C.Flags))
+  LD.Flags  := $(filter-out -fomit-frame-pointer,$(LD.Flags))
+endif
+
+# If REQUIRES_RTTI=1 is specified then don't disable run-time type id.
+ifneq ($(REQUIRES_RTTI), 1)
+  CXX.Flags += -fno-rtti
+endif
+
+ifeq ($(ENABLE_COVERAGE),1)
+  BuildMode := $(BuildMode)+Coverage
+  CXX.Flags += -ftest-coverage -fprofile-arcs
+  C.Flags   += -ftest-coverage -fprofile-arcs
+  LD.Flags   += -ftest-coverage -fprofile-arcs
 endif
 
 # If DISABLE_ASSERTIONS=1 is specified (make command line or configured),
 # then disable assertions by defining the appropriate preprocessor symbols.
-ifdef DISABLE_ASSERTIONS
-  BuildMode := $(BuildMode)-Asserts
+ifeq ($(DISABLE_ASSERTIONS),1)
   CPP.Defines += -DNDEBUG
 else
+  BuildMode := $(BuildMode)+Asserts
   CPP.Defines += -D_DEBUG
 endif
 
 # If ENABLE_EXPENSIVE_CHECKS=1 is specified (make command line or
 # configured), then enable expensive checks by defining the
 # appropriate preprocessor symbols.
-ifdef ENABLE_EXPENSIVE_CHECKS
+ifeq ($(ENABLE_EXPENSIVE_CHECKS),1)
   BuildMode := $(BuildMode)+Checks
   CPP.Defines += -D_GLIBCXX_DEBUG -DXDEBUG
 endif
@@ -303,7 +401,6 @@ endif
 # defined/on.
 ifdef LOADABLE_MODULE
   SHARED_LIBRARY := 1
-  DONT_BUILD_RELINKED := 1
   LINK_LIBS_IN_SHARED := 1
 endif
 
@@ -313,10 +410,10 @@ ifdef SHARED_LIBRARY
 endif
 
 ifeq ($(ENABLE_PIC),1)
-  ifeq ($(LLVM_ON_WIN32),1)
+  ifeq ($(HOST_OS), $(filter $(HOST_OS), Cygwin MingW))
     # Nothing. Win32 defaults to PIC and warns when given -fPIC
   else
-    ifeq ($(OS),Darwin)
+    ifeq ($(HOST_OS),Darwin)
       # Common symbols not allowed in dylib files
       CXX.Flags += -fno-common
       C.Flags   += -fno-common
@@ -326,13 +423,33 @@ ifeq ($(ENABLE_PIC),1)
       C.Flags   += -fPIC
     endif
   endif
+else
+  ifeq ($(HOST_OS),Darwin)
+      CXX.Flags += -mdynamic-no-pic
+      C.Flags   += -mdynamic-no-pic
+  endif
+endif
+
+# Support makefile variable to disable any kind of timestamp/non-deterministic
+# info from being used in the build.
+ifeq ($(ENABLE_TIMESTAMPS),1)
+  DOTDIR_TIMESTAMP_COMMAND := $(DATE)
+else
+  DOTDIR_TIMESTAMP_COMMAND := echo 'Created.'
+endif
+
+ifeq ($(HOST_OS),MingW)
+  # Work around PR4957
+  CPP.Defines += -D__NO_CTYPE_INLINE
+  ifeq ($(LLVM_CROSS_COMPILING),1)
+    # Work around http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=525016
+    ifdef TOOLNAME
+      LD.Flags += -Wl,--allow-multiple-definition
+    endif
+  endif
 endif
 
-CXX.Flags     += $(CXXFLAGS) -Woverloaded-virtual
-C.Flags       += $(CFLAGS)
-CPP.Defines   += $(CPPFLAGS)
 CPP.BaseFlags += $(CPP.Defines)
-LD.Flags      += $(LDFLAGS)
 AR.Flags      := cru
 
 # Make Floating point IEEE compliant on Alpha.
@@ -343,12 +460,21 @@ ifeq ($(ENABLE_PIC),0)
   CXX.Flags     += -fPIC
   CPP.BaseFlags += -fPIC
 endif
-endif
 
-ifeq ($(ARCH),Alpha)
   LD.Flags += -Wl,--no-relax
 endif
 
+# GNU ld/PECOFF accepts but ignores them below;
+#   --version-script
+#   --export-dynamic
+#   --rpath
+# FIXME: autoconf should be aware of them.
+ifneq (,$(filter $(HOST_OS),Cygwin MingW))
+  HAVE_LINK_VERSION_SCRIPT := 0
+  RPATH :=
+  RDYNAMIC := -Wl,--export-all-symbols
+endif
+
 #--------------------------------------------------------------------
 # Directory locations
 #--------------------------------------------------------------------
@@ -360,31 +486,57 @@ endif
 ObjRootDir  := $(PROJ_OBJ_DIR)/$(BuildMode)
 ObjDir      := $(ObjRootDir)
 LibDir      := $(PROJ_OBJ_ROOT)/$(BuildMode)/lib
+LibexecDir  := $(PROJ_OBJ_ROOT)/$(BuildMode)/libexec
+ShareDir    := $(PROJ_OBJ_ROOT)/$(BuildMode)/share
 ToolDir     := $(PROJ_OBJ_ROOT)/$(BuildMode)/bin
 ExmplDir    := $(PROJ_OBJ_ROOT)/$(BuildMode)/examples
 LLVMLibDir  := $(LLVM_OBJ_ROOT)/$(BuildMode)/lib
 LLVMToolDir := $(LLVM_OBJ_ROOT)/$(BuildMode)/bin
 LLVMExmplDir:= $(LLVM_OBJ_ROOT)/$(BuildMode)/examples
-CFERuntimeLibDir := $(LLVMGCCDIR)/lib
+
+#--------------------------------------------------------------------
+# Locations of shared libraries
+#--------------------------------------------------------------------
+
+SharedPrefix     := lib
+SharedLibDir     := $(LibDir)
+LLVMSharedLibDir := $(LLVMLibDir)
+
+# Win32.DLL prefers to be located on the "PATH" of binaries.
+ifeq ($(HOST_OS), $(filter $(HOST_OS), Cygwin MingW))
+  SharedLibDir     := $(ToolDir)
+  LLVMSharedLibDir := $(LLVMToolDir)
+
+  ifeq ($(HOST_OS),Cygwin)
+    SharedPrefix  := cyg
+  else
+    SharedPrefix  :=
+  endif
+endif
 
 #--------------------------------------------------------------------
 # Full Paths To Compiled Tools and Utilities
 #--------------------------------------------------------------------
-EchoCmd  = $(ECHO) llvm[$(MAKELEVEL)]:
-Echo     = @$(EchoCmd)
+EchoCmd  := $(ECHO) llvm[$(MAKELEVEL)]:
+ifdef BUILD_DIRS_ONLY
+EchoCmd  := $(EchoCmd) "(build tools)":
+endif
+
+Echo     := @$(EchoCmd)
 ifndef LLVMAS
 LLVMAS   := $(LLVMToolDir)/llvm-as$(EXEEXT)
 endif
-ifndef TBLGEN
+ifndef LLVM_TBLGEN
   ifeq ($(LLVM_CROSS_COMPILING),1)
-    TBLGEN   := $(BuildLLVMToolDir)/tblgen$(BUILD_EXEEXT)
+    LLVM_TBLGEN   := $(BuildLLVMToolDir)/llvm-tblgen$(BUILD_EXEEXT)
   else
-    TBLGEN   := $(LLVMToolDir)/tblgen$(EXEEXT)
+    LLVM_TBLGEN   := $(LLVMToolDir)/llvm-tblgen$(EXEEXT)
   endif
 endif
-LLVM_CONFIG := $(LLVMToolDir)/llvm-config
-ifndef LLVMLD
-LLVMLD    := $(LLVMToolDir)/llvm-ld$(EXEEXT)
+ifeq ($(LLVM_CROSS_COMPILING),1)
+  LLVM_CONFIG := $(BuildLLVMToolDir)/llvm-config$(BUILD_EXEEXT)
+else
+  LLVM_CONFIG := $(LLVMToolDir)/llvm-config$(EXEEXT)
 endif
 ifndef LLVMDIS
 LLVMDIS  := $(LLVMToolDir)/llvm-dis$(EXEEXT)
@@ -401,34 +553,56 @@ endif
 ifndef LBUGPOINT
 LBUGPOINT := $(LLVMToolDir)/bugpoint$(EXEEXT)
 endif
-ifndef LUPGRADE
-LUPGRADE := $(LLVMToolDir)/llvm-upgrade$(EXEEXT)
-endif
-ifeq ($(LLVMGCC_MAJVERS),3)
-UPGRADE_MSG = $(Echo) "Upgrading $(1) assembly to latest."
-UPGRADE_LL  = $(Verb)$(LUPGRADE) $(1) -o $(1).up.tmp -f ; $(MV) $(1).up.tmp $(1)
-LLVMGCCWITHPATH  := PATH="$(LLVMToolDir):$(PATH)" $(LLVMGCC)
-LLVMGXXWITHPATH  := PATH="$(LLVMToolDir):$(PATH)" $(LLVMGXX)
-else
-UPGRADE_MSG =
-UPGRADE_LL  =
-LLVMGCCWITHPATH  := $(LLVMGCC)
-LLVMGXXWITHPATH  := $(LLVMGXX)
+ifndef LLVMLINK
+LLVMLINK      := $(LLVMToolDir)/llvm-link$(EXEEXT)
 endif
 
 #--------------------------------------------------------------------
 # Adjust to user's request
 #--------------------------------------------------------------------
 
-# Adjust LD.Flags depending on the kind of library that is to be built. Note
-# that if LOADABLE_MODULE is specified then the resulting shared library can
-# be opened with dlopen.
-ifdef LOADABLE_MODULE
-  LD.Flags += -module
+ifeq ($(HOST_OS),Darwin)
+ ifdef MACOSX_DEPLOYMENT_TARGET
+  DARWIN_VERSION := $(MACOSX_DEPLOYMENT_TARGET)
+ else
+  DARWIN_VERSION := `sw_vers -productVersion`
+ endif
+  # Strip a number like 10.4.7 to 10.4
+  DARWIN_VERSION := $(shell echo $(DARWIN_VERSION)| sed -E 's/(10.[0-9]+).*/\1/')
+  # Get "4" out of 10.4 for later pieces in the makefile.
+  DARWIN_MAJVERS := $(shell echo $(DARWIN_VERSION)| sed -E 's/10.([0-9]+).*/\1/')
+
+  LoadableModuleOptions := -Wl,-flat_namespace -Wl,-undefined,suppress
+  SharedLinkOptions := -dynamiclib
+  ifdef DEPLOYMENT_TARGET
+    SharedLinkOptions += $(DEPLOYMENT_TARGET)
+  else
+    ifneq ($(ARCH),ARM)
+      SharedLinkOptions += -mmacosx-version-min=$(DARWIN_VERSION)
+    endif
+  endif
+else
+  SharedLinkOptions=-shared
+endif
+
+ifeq ($(TARGET_OS),Darwin)
+  ifdef DEPLOYMENT_TARGET
+    TargetCommonOpts += $(DEPLOYMENT_TARGET)
+  else
+    ifneq ($(ARCH),ARM)
+      TargetCommonOpts += -mmacosx-version-min=$(DARWIN_VERSION)
+    endif
+  endif
 endif
 
 ifdef SHARED_LIBRARY
-  LD.Flags += -Wl,-rpath -Wl,$(LibDir)
+ifneq ($(HOST_OS), $(filter $(HOST_OS), Cygwin MingW))
+ifneq ($(HOST_OS),Darwin)
+  LD.Flags += $(RPATH) -Wl,'$$ORIGIN'
+else
+  LD.Flags += -Wl,-install_name  -Wl,"@rpath/lib$(LIBRARYNAME)$(SHLIBEXT)"
+endif
+endif
 endif
 
 ifdef TOOL_VERBOSE
@@ -454,44 +628,81 @@ ifndef KEEP_SYMBOLS
   Install.StripFlag += -s
 endif
 
-# Adjust linker flags for building an executable
-ifneq ($(OS),Darwin)
-ifdef TOOLNAME
-ifdef EXAMPLE_TOOL
-  LD.Flags += -Wl,-rpath -Wl,$(ExmplDir) -export-dynamic
-else
-  LD.Flags += -Wl,-rpath -Wl,$(ToolDir) -export-dynamic
+# By default, strip dead symbols at link time
+ifneq ($(HOST_OS), $(filter $(HOST_OS), Cygwin MingW))
+ifneq ($(HOST_OS),Darwin)
+ifneq ($(HOST_ARCH),Mips)
+  CXX.Flags += -ffunction-sections -fdata-sections
+  ifeq ($(HOST_OS),SunOS)
+      CXX.Flags += -falign-functions=8
+  endif
+endif
 endif
 endif
+ifndef NO_DEAD_STRIP
+  ifeq ($(HOST_OS),Darwin)
+    LD.Flags += -Wl,-dead_strip
+  else
+    ifeq ($(HOST_OS),SunOS)
+      LD.Flags += -Wl,-z -Wl,discard-unused=sections
+    else
+      ifneq ($(HOST_OS), $(filter $(HOST_OS), Cygwin MingW))
+        LD.Flags += -Wl,--gc-sections
+      endif
+    endif
+  endif
 endif
 
-ifeq ($(OS),Darwin)
-  DARWIN_VERSION := `sw_vers -productVersion`
-  SharedLinkOptions=-Wl,-flat_namespace -Wl,-undefined -Wl,suppress -bundle \
-                    -mmacosx-version-min=$(DARWIN_VERSION)
-  CompileCommonOpts += -mmacosx-version-min=$(DARWIN_VERSION)
-else
-  SharedLinkOptions=-shared
+# Adjust linker flags for building an executable
+ifneq ($(HOST_OS), $(filter $(HOST_OS), Cygwin MingW))
+  ifndef TOOL_NO_EXPORTS
+    LD.Flags += $(RDYNAMIC)
+  endif
+  ifneq ($(HOST_OS), Darwin)
+    ifdef TOOLNAME
+      LD.Flags += $(RPATH) -Wl,'$$ORIGIN/../lib'
+    endif
+  else
+    ifneq ($(DARWIN_MAJVERS),4)
+      LD.Flags += $(RPATH) -Wl,@executable_path/../lib
+    endif
+    ifeq ($(RC_XBS),YES)
+      TempFile := $(shell mkdir -p ${OBJROOT}/dSYMs ; mktemp ${OBJROOT}/dSYMs/llvm-lto.XXXXXX)
+      LD.Flags += -Wl,-object_path_lto -Wl,$(TempFile)
+    endif
+  endif
 endif
 
+
 #----------------------------------------------------------
 # Options To Invoke Tools
 #----------------------------------------------------------
 
-CompileCommonOpts += -pedantic -Wall -W -Wwrite-strings -Wno-long-long \
-                     -Wunused -Wno-unused-parameter $(EXTRA_OPTIONS)
+ifdef EXTRA_LD_OPTIONS
+LD.Flags += $(EXTRA_LD_OPTIONS)
+endif
 
-ifeq ($(OS),HP-UX)
+ifndef NO_PEDANTIC
+CompileCommonOpts += -pedantic -Wno-long-long
+endif
+CompileCommonOpts += -Wall -W -Wno-unused-parameter -Wwrite-strings \
+                     $(EXTRA_OPTIONS) $(COVERED_SWITCH_DEFAULT) \
+                     $(NO_UNINITIALIZED) $(NO_MAYBE_UNINITIALIZED) \
+                     $(MISSING_FIELD_INITIALIZERS) $(NO_COMMENT)
+# Enable cast-qual for C++; the workaround is to use const_cast.
+CXX.Flags += -Wcast-qual
+
+ifeq ($(HOST_OS),HP-UX)
   CompileCommonOpts := -D_REENTRANT -D_HPUX_SOURCE
 endif
 
-# If we are building a universal binary on Mac OS/X, pass extra options.  This
+# If we are building a universal binary on Mac OS X, pass extra options.  This
 # is useful to people that want to link the LLVM libraries into their universal
 # apps.
 #
 # The following can be optionally specified:
 #   UNIVERSAL_SDK_PATH variable can be specified as a path to the SDK to use.
-#      For Mac OS/X 10.4 Intel machines, the traditional one is:
+#      For Mac OS X 10.4 Intel machines, the traditional one is:
 #      UNIVERSAL_SDK_PATH=/Developer/SDKs/MacOSX10.4u.sdk/
 #   UNIVERSAL_ARCH can be optionally specified to be a list of architectures
 #      to build for, e.g. UNIVERSAL_ARCH="i386 ppc ppc64".  This defaults to
@@ -501,23 +712,37 @@ ifdef UNIVERSAL
     UNIVERSAL_ARCH := i386 ppc
   endif
   UNIVERSAL_ARCH_OPTIONS := $(UNIVERSAL_ARCH:%=-arch %)
-  CompileCommonOpts += $(UNIVERSAL_ARCH_OPTIONS)
-  Relink.Flags := $(UNIVERSAL_ARCH_OPTIONS)
+  TargetCommonOpts += $(UNIVERSAL_ARCH_OPTIONS)
   ifdef UNIVERSAL_SDK_PATH
-    CompileCommonOpts += -isysroot $(UNIVERSAL_SDK_PATH)
-    Relink.Flags      += -isysroot $(UNIVERSAL_SDK_PATH)
+    TargetCommonOpts += -isysroot $(UNIVERSAL_SDK_PATH)
   endif
 
   # Building universal cannot compute dependencies automatically.
   DISABLE_AUTO_DEPENDENCIES=1
+else
+  ifeq ($(TARGET_OS),Darwin)
+    ifeq ($(ARCH),x86_64)
+      TargetCommonOpts = -m64
+    else
+      ifeq ($(ARCH),x86)
+        TargetCommonOpts = -m32
+      endif
+    endif
+  endif
 endif
 
-ifeq ($(OS),SunOS)
-CPP.BaseFlags += -include llvm/System/Solaris.h
+ifeq ($(HOST_OS),SunOS)
+CPP.BaseFlags += -include llvm/Support/Solaris.h
 endif
 
-LD.Flags      += -L$(LibDir) -L$(LLVMLibDir)
-CPP.BaseFlags += -D_GNU_SOURCE -D__STDC_LIMIT_MACROS -D__STDC_CONSTANT_MACROS
+# On Windows, SharedLibDir != LibDir. The order is important.
+ifeq ($(HOST_OS), $(filter $(HOST_OS), Cygwin MingW))
+  LD.Flags    += -L$(SharedLibDir) -L$(LibDir) -L$(LLVMToolDir) -L$(LLVMLibDir)
+else
+  LD.Flags    += -L$(LibDir) -L$(LLVMLibDir)
+endif
+
+CPP.BaseFlags += -D_GNU_SOURCE -D__STDC_CONSTANT_MACROS -D__STDC_FORMAT_MACROS -D__STDC_LIMIT_MACROS
 # All -I flags should go here, so that they don't confuse llvm-config.
 CPP.Flags     += $(sort -I$(PROJ_OBJ_DIR) -I$(PROJ_SRC_DIR) \
                 $(patsubst %,-I%/include,\
@@ -525,29 +750,33 @@ CPP.Flags     += $(sort -I$(PROJ_OBJ_DIR) -I$(PROJ_SRC_DIR) \
                 $(LLVM_OBJ_ROOT) $(LLVM_SRC_ROOT))) \
                 $(CPP.BaseFlags)
 
- ifeq ($(BUILD_COMPONENT), 1)
-  Compile.C     = $(BUILD_CC) $(CPP.Flags) $(C.Flags) $(CompileCommonOpts) -c
-  Compile.CXX   = $(BUILD_CXX) $(CPP.Flags) $(CXX.Flags) $(CompileCommonOpts) -c
-  Preprocess.CXX= $(BUILD_CXX) $(CPP.Flags) $(CompileCommonOpts) $(CXX.Flags) -E
-  Link          = $(BUILD_CXX) $(CPP.Flags) $(CXX.Flags) $(CompileCommonOpts) \
-                  $(LD.Flags) $(Strip)
-  Relink        = $(BUILD_CXX) $(CPP.Flags) $(CXX.Flags) $(CompileCommonOpts) \
-                 $(Relink.Flags)
+ifeq ($(INCLUDE_BUILD_DIR),1)
+  CPP.Flags   += -I$(ObjDir)
+endif
+
+# SHOW_DIAGNOSTICS support.
+ifeq ($(SHOW_DIAGNOSTICS),1)
+  Compile.Wrapper := env CC_LOG_DIAGNOSTICS=1 \
+                         CC_LOG_DIAGNOSTICS_FILE="$(LLVM_OBJ_ROOT)/$(BuildMode)/diags"
 else
-  Compile.C     = $(CC) $(CPP.Flags) $(C.Flags) $(CompileCommonOpts) -c
-  Compile.CXX   = $(CXX) $(CPP.Flags) $(CXX.Flags) $(CompileCommonOpts) -c
-  Preprocess.CXX= $(CXX) $(CPP.Flags) $(CompileCommonOpts) $(CXX.Flags) -E
-  Link          = $(CXX) $(CPP.Flags) $(CXX.Flags) $(CompileCommonOpts) \
-                  $(LD.Flags) $(Strip)
-  Relink        = $(CXX) $(CPP.Flags) $(CXX.Flags) $(CompileCommonOpts) \
-                 $(Relink.Flags)
+  Compile.Wrapper :=
 endif
 
-BCCompile.C   = $(LLVMGCCWITHPATH) $(CPP.Flags) $(C.Flags) $(CompileCommonOpts)
-Preprocess.C  = $(CC) $(CPP.Flags) $(C.Flags) $(CompileCommonOpts) -E
+Compile.C     = $(Compile.Wrapper) \
+                 $(CC) $(CPP.Flags) $(C.Flags) $(CFLAGS) $(CPPFLAGS) \
+                $(TargetCommonOpts) $(CompileCommonOpts) -c
+Compile.CXX   = $(Compile.Wrapper) \
+                 $(CXX) $(CPP.Flags) $(CXX.Flags) $(CXXFLAGS) $(CPPFLAGS) \
+                $(TargetCommonOpts) $(CompileCommonOpts) -c
+Preprocess.CXX= $(Compile.Wrapper) \
+                 $(CXX) $(CPP.Flags) $(TargetCommonOpts) $(CPPFLAGS) \
+                $(CompileCommonOpts) $(CXX.Flags) -E
+Link          = $(Compile.Wrapper) \
+                 $(CXX) $(CXXFLAGS) $(LD.Flags) $(LDFLAGS) \
+                $(TargetCommonOpts) $(Strip)
 
-BCCompile.CXX = $(LLVMGXXWITHPATH) $(CPP.Flags) $(CXX.Flags) \
-                $(CompileCommonOpts)
+Preprocess.C  = $(CC) $(CPP.Flags) $(C.Flags) $(CPPFLAGS) \
+                $(TargetCommonOpts) $(CompileCommonOpts) -E
 
 ProgInstall   = $(INSTALL) $(Install.StripFlag) -m 0755
 ScriptInstall = $(INSTALL) -m 0755
@@ -556,19 +785,21 @@ DataInstall   = $(INSTALL) -m 0644
 # When compiling under Mingw/Cygwin, the tblgen tool expects Windows
 # paths. In this case, the SYSPATH function (defined in
 # Makefile.config) transforms Unix paths into Windows paths.
-TableGen      = $(TBLGEN) -I $(call SYSPATH, $(PROJ_SRC_DIR)) \
+TableGen.Flags= -I $(call SYSPATH, $(PROJ_SRC_DIR)) \
                 -I $(call SYSPATH, $(LLVM_SRC_ROOT)/include) \
                 -I $(call SYSPATH, $(PROJ_SRC_ROOT)/include) \
                 -I $(call SYSPATH, $(PROJ_SRC_ROOT)/lib/Target)
+LLVMTableGen  = $(LLVM_TBLGEN) $(TableGen.Flags)
 
 Archive       = $(AR) $(AR.Flags)
-LArchive      = $(LLVMToolDir)/llvm-ar rcsf
 ifdef RANLIB
 Ranlib        = $(RANLIB)
 else
 Ranlib        = ranlib
 endif
 
+AliasTool     = ln -sf
+
 #----------------------------------------------------------
 # Get the list of source files and compute object file
 # names from them.
@@ -586,9 +817,28 @@ Sources += $(filter %.cpp %.c %.cc,$(BUILT_SOURCES))
 endif
 
 BaseNameSources := $(sort $(basename $(Sources)))
+SourceDirs := $(sort $(dir $(Sources)))
 
 ObjectsO  := $(BaseNameSources:%=$(ObjDir)/%.o)
-ObjectsBC := $(BaseNameSources:%=$(ObjDir)/%.bc)
+ObjectDirs := $(SourceDirs:%=$(ObjDir)/%)
+
+#----------------------------------------------------------
+# For Mingw MSYS bash and Python/w32:
+#
+# $(ECHOPATH) prints DOSish pathstring.
+#   ex) $(ECHOPATH) /include/sys/types.h
+#   --> C:/mingw/include/sys/types.h
+# built-in "echo" does not transform path to DOSish path.
+#
+# FIXME: It would not be needed when MSYS's python
+# were provided.
+#----------------------------------------------------------
+
+ifeq (-mingw32,$(findstring -mingw32,$(BUILD_TRIPLE)))
+  ECHOPATH := $(Verb)$(PYTHON) -u -c "import sys;print ' '.join(sys.argv[1:])"
+else
+  ECHOPATH := $(Verb)$(ECHO)
+endif
 
 ###############################################################################
 # DIRECTORIES: Handle recursive descent of directory structure
@@ -599,16 +849,26 @@ ObjectsBC := $(BaseNameSources:%=$(ObjDir)/%.bc)
 # in the file so they get built before dependencies
 #---------------------------------------------------------
 
-$(PROJ_bindir) $(PROJ_libdir) $(PROJ_includedir) $(PROJ_etcdir)::
+$(DESTDIR)$(PROJ_bindir) $(DESTDIR)$(PROJ_libdir) $(DESTDIR)$(PROJ_includedir) $(DESTDIR)$(PROJ_etcdir)::
        $(Verb) $(MKDIR) $@
 
 # To create other directories, as needed, and timestamp their creation
 %/.dir:
        $(Verb) $(MKDIR) $* > /dev/null
-       $(Verb) $(DATE) > $@
+       $(Verb) $(DOTDIR_TIMESTAMP_COMMAND) > $@
 
-.PRECIOUS: $(ObjDir)/.dir $(LibDir)/.dir $(ToolDir)/.dir $(ExmplDir)/.dir
+.PRECIOUS: $(LibDir)/.dir $(ToolDir)/.dir $(ExmplDir)/.dir
 .PRECIOUS: $(LLVMLibDir)/.dir $(LLVMToolDir)/.dir $(LLVMExmplDir)/.dir
+.PRECIOUS: $(LibexecDir)/.dir $(ShareDir)/.dir
+
+#---------------------------------------------------------
+# Collect the object directories (as there may be more
+# than one if the source code is spread across
+# subdirectories).
+#---------------------------------------------------------
+
+OBJECT_DIRS := $(ObjDir)/.dir $(ObjectDirs:%=%/.dir)
+.PRECIOUS: $(OBJECT_DIRS)
 
 #---------------------------------------------------------
 # Handle the DIRS options for sequential construction
@@ -621,7 +881,8 @@ SubDirs += $(DIRS)
 ifneq ($(PROJ_SRC_ROOT),$(PROJ_OBJ_ROOT))
 $(RecursiveTargets)::
        $(Verb) for dir in $(DIRS); do \
-         if [ ! -f $$dir/Makefile ]; then \
+         if ([ ! -f $$dir/Makefile ] || \
+             command test $$dir/Makefile -ot $(PROJ_SRC_DIR)/$$dir/Makefile ); then \
            $(MKDIR) $$dir; \
            $(CP) $(PROJ_SRC_DIR)/$$dir/Makefile $$dir/Makefile; \
          fi; \
@@ -643,7 +904,8 @@ endif
 ifdef EXPERIMENTAL_DIRS
 $(RecursiveTargets)::
        $(Verb) for dir in $(EXPERIMENTAL_DIRS); do \
-         if [ ! -f $$dir/Makefile ]; then \
+         if ([ ! -f $$dir/Makefile ] || \
+             command test $$dir/Makefile -ot $(PROJ_SRC_DIR)/$$dir/Makefile ); then \
            $(MKDIR) $$dir; \
            $(CP) $(PROJ_SRC_DIR)/$$dir/Makefile $$dir/Makefile; \
          fi; \
@@ -655,7 +917,7 @@ endif
 # Handle the OPTIONAL_PARALLEL_DIRS options for optional parallel construction
 #-----------------------------------------------------------
 ifdef OPTIONAL_PARALLEL_DIRS
-  PARALLEL_DIRS += $(foreach T,$(OPTIONAL_PARALLEL_DIRS),$(shell test -d $(PROJ_SRC_DIR)/$(T) && echo "$(T)"))
+  PARALLEL_DIRS += $(foreach T,$(OPTIONAL_PARALLEL_DIRS),$(shell test -d $(PROJ_SRC_DIR)/$(T) -o -f $(T)/Makefile && echo "$(T)"))
 endif
 
 #-----------------------------------------------------------
@@ -672,15 +934,25 @@ clean-all:: $(addsuffix /.makeclean-all,$(PARALLEL_DIRS))
 install  :: $(addsuffix /.makeinstall  ,$(PARALLEL_DIRS))
 uninstall:: $(addsuffix /.makeuninstall,$(PARALLEL_DIRS))
 install-bytecode  :: $(addsuffix /.makeinstall-bytecode,$(PARALLEL_DIRS))
+unitcheck:: $(addsuffix /.makeunitcheck,$(PARALLEL_DIRS))
 
 ParallelTargets := $(foreach T,$(RecursiveTargets),%/.make$(T))
 
 $(ParallelTargets) :
-       $(Verb) if [ ! -f $(@D)/Makefile ]; then \
-         $(MKDIR) $(@D); \
-         $(CP) $(PROJ_SRC_DIR)/$(@D)/Makefile $(@D)/Makefile; \
+       $(Verb) \
+         SD=$(PROJ_SRC_DIR)/$(@D); \
+         DD=$(@D); \
+         if [ ! -f $$SD/Makefile ]; then \
+           SD=$(@D); \
+           DD=$(notdir $(@D)); \
+         fi; \
+         if ([ ! -f $$DD/Makefile ] || \
+                   command test $$DD/Makefile -ot \
+                      $$SD/Makefile ); then \
+         $(MKDIR) $$DD; \
+         $(CP) $$SD/Makefile $$DD/Makefile; \
        fi; \
-       $(MAKE) -C $(@D) $(subst $(@D)/.make,,$@)
+       $(MAKE) -C $$DD $(subst $(@D)/.make,,$@)
 endif
 
 #---------------------------------------------------------
@@ -695,7 +967,8 @@ ifneq ($(PROJ_SRC_ROOT),$(PROJ_OBJ_ROOT))
 $(RecursiveTargets)::
        $(Verb) for dir in $(OPTIONAL_DIRS); do \
          if [ -d $(PROJ_SRC_DIR)/$$dir ]; then\
-           if [ ! -f $$dir/Makefile ]; then \
+           if ([ ! -f $$dir/Makefile ] || \
+               command test $$dir/Makefile -ot $(PROJ_SRC_DIR)/$$dir/Makefile ); then \
              $(MKDIR) $$dir; \
              $(CP) $(PROJ_SRC_DIR)/$$dir/Makefile $$dir/Makefile; \
            fi; \
@@ -705,7 +978,9 @@ $(RecursiveTargets)::
 else
 $(RecursiveTargets)::
        $(Verb) for dir in $(OPTIONAL_DIRS); do \
-         ($(MAKE) -C$$dir $@ ) || exit 1; \
+         if [ -d $(PROJ_SRC_DIR)/$$dir ]; then\
+           ($(MAKE) -C$$dir $@ ) || exit 1; \
+         fi \
        done
 endif
 endif
@@ -721,29 +996,29 @@ install-local::
 uninstall-local::
        $(Echo) UnInstall circumvented with NO_INSTALL
 else
-install-local:: $(PROJ_etcdir) $(CONFIG_FILES)
-       $(Echo) Installing Configuration Files To $(PROJ_etcdir)
+install-local:: $(DESTDIR)$(PROJ_etcdir) $(CONFIG_FILES)
+       $(Echo) Installing Configuration Files To $(DESTDIR)$(PROJ_etcdir)
        $(Verb)for file in $(CONFIG_FILES); do \
           if test -f $(PROJ_OBJ_DIR)/$${file} ; then \
-            $(DataInstall) $(PROJ_OBJ_DIR)/$${file} $(PROJ_etcdir) ; \
+            $(DataInstall) $(PROJ_OBJ_DIR)/$${file} $(DESTDIR)$(PROJ_etcdir) ; \
           elif test -f $(PROJ_SRC_DIR)/$${file} ; then \
-            $(DataInstall) $(PROJ_SRC_DIR)/$${file} $(PROJ_etcdir) ; \
+            $(DataInstall) $(PROJ_SRC_DIR)/$${file} $(DESTDIR)$(PROJ_etcdir) ; \
           else \
             $(ECHO) Error: cannot find config file $${file}. ; \
           fi \
        done
 
 uninstall-local::
-       $(Echo) Uninstalling Configuration Files From $(PROJ_etcdir)
+       $(Echo) Uninstalling Configuration Files From $(DESTDIR)$(PROJ_etcdir)
        $(Verb)for file in $(CONFIG_FILES); do \
-         $(RM) -f $(PROJ_etcdir)/$${file} ; \
+         $(RM) -f $(DESTDIR)$(PROJ_etcdir)/$${file} ; \
        done
 endif
 
 endif
 
 ###############################################################################
-# Set up variables for building libararies
+# Set up variables for building libraries
 ###############################################################################
 
 #---------------------------------------------------------
@@ -767,6 +1042,16 @@ LLVMUsedLibs    := $(patsubst %.a.o, lib%.a, $(addsuffix .o, $(LLVMLIBS)))
 LLVMLibsPaths   := $(addprefix $(LLVMLibDir)/,$(LLVMUsedLibs))
 endif
 
+# Loadable module for Win32 requires all symbols resolved for linking.
+# Then all symbols in LLVM.dll will be available.
+ifeq ($(ENABLE_SHARED),1)
+  ifdef LOADABLE_MODULE
+    ifneq (,$(filter $(HOST_OS),Cygwin MingW))
+      LINK_COMPONENTS += all
+    endif
+  endif
+endif
+
 ifndef IS_CLEANING_TARGET
 ifdef LINK_COMPONENTS
 
@@ -778,91 +1063,121 @@ $(LLVM_CONFIG):
 
 $(ToolDir)/$(strip $(TOOLNAME))$(EXEEXT): $(LLVM_CONFIG)
 
-ProjLibsOptions += $(shell $(LLVM_CONFIG) --libs     $(LINK_COMPONENTS))
-ProjLibsPaths   += $(LLVM_CONFIG) \
-                  $(shell $(LLVM_CONFIG) --libfiles $(LINK_COMPONENTS))
+ifeq ($(ENABLE_SHARED), 1)
+# We can take the "auto-import" feature to get rid of using dllimport.
+ifeq ($(HOST_OS), $(filter $(HOST_OS), Cygwin MingW))
+LLVMLibsOptions += -Wl,--enable-auto-import,--enable-runtime-pseudo-reloc \
+                   -L $(SharedLibDir)
 endif
-endif
-
-###############################################################################
-# Library Build Rules: Four ways to build a library
-###############################################################################
-
-#---------------------------------------------------------
-# Bytecode Module Targets:
-#   If the user set MODULE_NAME then they want to build a
-#   bytecode module from the sources. We compile all the
-#   sources and link it together into a single bytecode
-#   module.
-#---------------------------------------------------------
-
-ifdef MODULE_NAME
-ifeq ($(strip $(LLVMGCC)),)
-$(warning Modules require llvm-gcc but no llvm-gcc is available ****)
+LLVM_SO_NAME = LLVM-$(LLVM_VERSION_MAJOR).$(LLVM_VERSION_MINOR)$(LLVM_VERSION_SUFFIX)
+LLVMLibsOptions += -l$(LLVM_SO_NAME)
+LLVMLibsPaths += $(SharedLibDir)/$(SharedPrefix)$(LLVM_SO_NAME)$(SHLIBEXT)
 else
 
-Module     := $(LibDir)/$(MODULE_NAME).bc
-LinkModule := $(LLVMLD) -L$(CFERuntimeLibDir) -r
-
+ifndef NO_LLVM_CONFIG
+LLVMConfigLibs := $(shell $(LLVM_CONFIG) --libs $(LINK_COMPONENTS) || echo Error)
+ifeq ($(LLVMConfigLibs),Error)
+$(error llvm-config --libs failed)
+endif
+LLVMLibsOptions += $(LLVMConfigLibs)
+LLVMConfigLibfiles := $(shell $(LLVM_CONFIG) --libfiles $(LINK_COMPONENTS) || echo Error)
+ifeq ($(LLVMConfigLibfiles),Error)
+$(error llvm-config --libfiles failed)
+endif
+LLVMLibsPaths += $(LLVM_CONFIG) $(LLVMConfigLibfiles)
+endif
 
-ifdef EXPORTED_SYMBOL_FILE
-LinkModule += -internalize-public-api-file=$(EXPORTED_SYMBOL_FILE)
+endif
+endif
 endif
 
-$(Module): $(BUILT_SOURCES) $(ObjectsBC) $(LibDir)/.dir $(LLVMLD)
-       $(Echo) Building $(BuildMode) Bytecode Module $(notdir $@)
-       $(Verb) $(LinkModule) -o $@ $(ObjectsBC)
+# Set up the library exports file.
+ifdef EXPORTED_SYMBOL_FILE
 
-all-local:: $(Module)
+# First, set up the native export file, which may differ from the source
+# export file.
 
+ifeq ($(HOST_OS),Darwin)
+# Darwin convention prefixes symbols with underscores.
+NativeExportsFile := $(ObjDir)/$(notdir $(EXPORTED_SYMBOL_FILE)).sed
+$(NativeExportsFile): $(EXPORTED_SYMBOL_FILE) $(ObjDir)/.dir
+       $(Verb) sed -e 's/^/_/' < $< > $@
 clean-local::
-ifneq ($(strip $(Module)),)
-       -$(Verb) $(RM) -f $(Module)
-endif
-
-ifdef BYTECODE_DESTINATION
-ModuleDestDir := $(BYTECODE_DESTINATION)
+       -$(Verb) $(RM) -f $(NativeExportsFile)
 else
-ModuleDestDir := $(PROJ_libdir)
-endif
-
-ifdef NO_INSTALL
-install-local::
-       $(Echo) Install circumvented with NO_INSTALL
-uninstall-local::
-       $(Echo) Uninstall circumvented with NO_INSTALL
+ifeq ($(HAVE_LINK_VERSION_SCRIPT),1)
+# Gold and BFD ld require a version script rather than a plain list.
+NativeExportsFile := $(ObjDir)/$(notdir $(EXPORTED_SYMBOL_FILE)).map
+$(NativeExportsFile): $(EXPORTED_SYMBOL_FILE) $(ObjDir)/.dir
+       $(Verb) echo "{" > $@
+       $(Verb) grep -q '[[:alnum:]_]' $< && echo "  global:" >> $@ || :
+       $(Verb) sed -e 's/$$/;/' -e 's/^/    /' < $< >> $@
+ifneq ($(HOST_OS),OpenBSD)
+       $(Verb) echo "  local: *;" >> $@
+endif
+       $(Verb) echo "};" >> $@
+clean-local::
+       -$(Verb) $(RM) -f $(NativeExportsFile)
+else
+ifeq ($(HOST_OS), $(filter $(HOST_OS), Cygwin MingW))
+# GNU ld Win32 accepts .DEF files that contain "DATA" entries.
+NativeExportsFile := $(ObjDir)/$(notdir $(EXPORTED_SYMBOL_FILE:.exports=.def))
+$(NativeExportsFile): $(EXPORTED_SYMBOL_FILE) $(ObjDir)/.dir
+       $(Echo) Generating $(notdir $@)
+       $(Verb) $(ECHO) "EXPORTS" > $@
+       $(Verb) $(CAT) $< >> $@
+clean-local::
+       -$(Verb) $(RM) -f $(NativeExportsFile)
 else
-DestModule := $(ModuleDestDir)/$(MODULE_NAME).bc
+# Default behavior: just use the exports file verbatim.
+NativeExportsFile := $(EXPORTED_SYMBOL_FILE)
+endif
+endif
+endif
 
-install-module:: $(DestModule)
-install-local:: $(DestModule)
+# Now add the linker command-line options to use the native export file.
 
-$(DestModule): $(ModuleDestDir) $(Module)
-       $(Echo) Installing $(BuildMode) Bytecode Module $(DestModule)
-       $(Verb) $(DataInstall) $(Module) $(DestModule)
+# Darwin
+ifeq ($(HOST_OS),Darwin)
+LLVMLibsOptions += -Wl,-exported_symbols_list,$(NativeExportsFile)
+endif
 
-uninstall-local::
-       $(Echo) Uninstalling $(BuildMode) Bytecode Module $(DestModule)
-       -$(Verb) $(RM) -f $(DestModule)
+# gold, bfd ld, etc.
+ifeq ($(HAVE_LINK_VERSION_SCRIPT),1)
+LLVMLibsOptions += -Wl,--version-script,$(NativeExportsFile)
 endif
 
+# Windows
+ifeq ($(HOST_OS), $(filter $(HOST_OS), Cygwin MingW))
+# LLVMLibsOptions is invalidated at processing tools/llvm-shlib.
+SharedLinkOptions += $(NativeExportsFile)
 endif
+
 endif
 
+###############################################################################
+# Library Build Rules: Four ways to build a library
+###############################################################################
+
 # if we're building a library ...
 ifdef LIBRARYNAME
 
-# Make sure there isn't any extranous whitespace on the LIBRARYNAME option
+# Make sure there isn't any extraneous whitespace on the LIBRARYNAME option
 LIBRARYNAME := $(strip $(LIBRARYNAME))
+LIBRARYALIASNAME := $(strip $(LIBRARYALIASNAME))
 ifdef LOADABLE_MODULE
-LibName.A  := $(LibDir)/$(LIBRARYNAME).a
-LibName.SO := $(LibDir)/$(LIBRARYNAME)$(SHLIBEXT)
+BaseLibName.A  := $(LIBRARYNAME).a
+BaseLibName.SO := $(LIBRARYNAME)$(SHLIBEXT)
+BaseAliasName.SO := $(LIBRARYALIASNAME)$(SHLIBEXT)
 else
-LibName.A  := $(LibDir)/lib$(LIBRARYNAME).a
-LibName.SO := $(LibDir)/lib$(LIBRARYNAME)$(SHLIBEXT)
+BaseLibName.A  := lib$(LIBRARYNAME).a
+BaseLibName.SO := $(SharedPrefix)$(LIBRARYNAME)$(SHLIBEXT)
+BaseAliasName.SO := $(SharedPrefix)$(LIBRARYALIASNAME)$(SHLIBEXT)
 endif
+LibName.A  := $(LibDir)/$(BaseLibName.A)
+LibName.SO := $(SharedLibDir)/$(BaseLibName.SO)
+AliasName.SO := $(SharedLibDir)/$(BaseAliasName.SO)
 LibName.O  := $(LibDir)/$(LIBRARYNAME).o
-LibName.BCA:= $(LibDir)/lib$(LIBRARYNAME).bca
 
 #---------------------------------------------------------
 # Shared Library Targets:
@@ -872,22 +1187,32 @@ LibName.BCA:= $(LibDir)/lib$(LIBRARYNAME).bca
 #---------------------------------------------------------
 ifdef SHARED_LIBRARY
 
-all-local:: $(LibName.SO)
+all-local:: $(AliasName.SO)
+
+$(AliasName.SO): $(LibName.SO)
+ifdef SHARED_ALIAS
+       $(Verb) $(AliasTool) $(BaseLibName.SO) $(AliasName.SO)
+endif
+
+ifdef EXPORTED_SYMBOL_FILE
+$(LibName.SO): $(NativeExportsFile)
+endif
 
 ifdef LINK_LIBS_IN_SHARED
 ifdef LOADABLE_MODULE
 SharedLibKindMessage := "Loadable Module"
+SharedLinkOptions := $(LoadableModuleOptions) $(SharedLinkOptions)
 else
 SharedLibKindMessage := "Shared Library"
 endif
-$(LibName.SO): $(ObjectsO) $(ProjLibsPaths) $(LLVMLibsPaths) $(LibDir)/.dir
+$(LibName.SO): $(ObjectsO) $(ProjLibsPaths) $(LLVMLibsPaths) $(SharedLibDir)/.dir
        $(Echo) Linking $(BuildMode) $(SharedLibKindMessage) \
-         $(LIBRARYNAME)$(SHLIBEXT)
+         $(notdir $@)
        $(Verb) $(Link) $(SharedLinkOptions) -o $@ $(ObjectsO) \
-         $(ProjLibsOptions) $(LLVMLibsOptions)
+         $(ProjLibsOptions) $(LLVMLibsOptions) $(LIBS)
 else
-$(LibName.SO): $(ObjectsO) $(LibDir)/.dir
-       $(Echo) Linking $(BuildMode) Shared Library $(LIBRARYNAME)$(SHLIBEXT)
+$(LibName.SO): $(ObjectsO) $(SharedLibDir)/.dir
+       $(Echo) Linking $(BuildMode) Shared Library $(notdir $@)
        $(Verb) $(Link) $(SharedLinkOptions) -o $@ $(ObjectsO)
 endif
 
@@ -902,130 +1227,45 @@ install-local::
 uninstall-local::
        $(Echo) Uninstall circumvented with NO_INSTALL
 else
-DestSharedLib = $(PROJ_libdir)/lib$(LIBRARYNAME)$(SHLIBEXT)
 
-install-local:: $(DestSharedLib)
-
-$(DestSharedLib): $(LibName.SO) $(PROJ_libdir)
-       $(Echo) Installing $(BuildMode) Shared Library $(DestSharedLib)
-       $(Verb) $(INSTALL) $(LibName.SO) $(DestSharedLib)
-
-uninstall-local::
-       $(Echo) Uninstalling $(BuildMode) Shared Library $(DestSharedLib)
-       -$(Verb) $(RM) -f $(PROJ_libdir)/lib$(LIBRARYNAME).*
-endif
-endif
-
-#---------------------------------------------------------
-# Bytecode Library Targets:
-#   If the user asked for a bytecode library to be built
-#   with the BYTECODE_LIBRARY variable, then we provide
-#   targets for building them.
-#---------------------------------------------------------
-ifdef BYTECODE_LIBRARY
-ifeq ($(strip $(LLVMGCC)),)
-$(warning Bytecode libraries require llvm-gcc which could not be found ****)
-else
-
-all-local:: $(LibName.BCA)
-
-ifdef EXPORTED_SYMBOL_FILE
-BCLinkLib = $(LLVMLD) -L$(CFERuntimeLibDir) \
-                     -internalize-public-api-file=$(EXPORTED_SYMBOL_FILE)
-
-$(LibName.BCA): $(ObjectsBC) $(LibDir)/.dir $(LLVMLD) \
-                $(LLVMToolDir)/llvm-ar
-       $(Echo) Building $(BuildMode) Bytecode Archive $(notdir $@) \
-         "(internalize)"
-       $(Verb) $(BCLinkLib) -o $(ObjDir)/$(LIBRARYNAME).o $(ObjectsBC)
-       $(Verb) $(RM) -f $@
-       $(Verb) $(LArchive) $@ $(ObjDir)/$(LIBRARYNAME).o
+# Win32.DLL prefers to be located on the "PATH" of binaries.
+ifeq ($(HOST_OS), $(filter $(HOST_OS), Cygwin MingW))
+DestSharedLibDir := $(DESTDIR)$(PROJ_bindir)
 else
-$(LibName.BCA): $(ObjectsBC) $(LibDir)/.dir \
-                $(LLVMToolDir)/llvm-ar
-       $(Echo) Building $(BuildMode) Bytecode Archive $(notdir $@)
-       $(Verb) $(RM) -f $@
-       $(Verb) $(LArchive) $@ $(ObjectsBC)
-
+DestSharedLibDir := $(DESTDIR)$(PROJ_libdir)
 endif
+DestSharedLib := $(DestSharedLibDir)/$(BaseLibName.SO)
+DestSharedAlias := $(DestSharedLibDir)/$(BaseAliasName.SO)
 
-clean-local::
-ifneq ($(strip $(LibName.BCA)),)
-       -$(Verb) $(RM) -f $(LibName.BCA)
-endif
+install-local:: $(DestSharedLib)
 
-ifdef BYTECODE_DESTINATION
-BytecodeDestDir := $(BYTECODE_DESTINATION)
-else
-BytecodeDestDir := $(PROJ_libdir)
+$(DestSharedLib): $(LibName.SO) $(DestSharedLibDir)
+       $(Echo) Installing $(BuildMode) Shared Library $(DestSharedLib)
+       $(Verb) $(INSTALL) $(LibName.SO) $(DestSharedLib)
+ifdef SHARED_ALIAS
+       $(Echo) Creating alias from $(DestSharedLib) to $(DestSharedAlias)
+       $(Verb) $(AliasTool) $(BaseLibName.SO) $(DestSharedAlias)
 endif
 
-DestBytecodeLib = $(BytecodeDestDir)/lib$(LIBRARYNAME).a
-
-install-bytecode-local:: $(DestBytecodeLib)
-
-ifdef NO_INSTALL
-install-local::
-       $(Echo) Install circumvented with NO_INSTALL
 uninstall-local::
-       $(Echo) Uninstall circumvented with NO_INSTALL
-else
-install-local:: $(DestBytecodeLib)
-
-$(DestBytecodeLib): $(LibName.BCA) $(BytecodeDestDir)
-       $(Echo) Installing $(BuildMode) Bytecode Archive $(DestBytecodeLib)
-       $(Verb) $(DataInstall) $(LibName.BCA) $(DestBytecodeLib)
-
-uninstall-local::
-       $(Echo) Uninstalling $(BuildMode) Bytecode Archive $(DestBytecodeLib)
-       -$(Verb) $(RM) -f $(DestBytecodeLib)
+       $(Echo) Uninstalling $(BuildMode) Shared Library $(DestSharedLib)
+       -$(Verb) $(RM) -f $(DestSharedLib)
+ifdef SHARED_ALIAS
+       -$(Verb) $(RM) -f $(DestSharedAlias)
 endif
 endif
 endif
 
 #---------------------------------------------------------
-# ReLinked Library Targets:
-#   If the user explicitly requests a relinked library with
-#   BUILD_RELINKED, provide it.  Otherwise, if they specify
-#   neither of BUILD_ARCHIVE or DONT_BUILD_RELINKED, give
-#   them one.
+# Library Targets:
+#   If neither BUILD_ARCHIVE or LOADABLE_MODULE are specified, default to
+#   building an archive.
 #---------------------------------------------------------
+ifndef NO_BUILD_ARCHIVE
 ifndef BUILD_ARCHIVE
-ifndef DONT_BUILD_RELINKED
-BUILD_RELINKED = 1
-endif
-endif
-
-ifdef BUILD_RELINKED
-
-all-local:: $(LibName.O)
-
-$(LibName.O): $(ObjectsO) $(LibDir)/.dir
-       $(Echo) Linking $(BuildMode) Object Library $(notdir $@)
-       $(Verb) $(Relink) -Wl,-r -nodefaultlibs -nostdlib -nostartfiles -o $@ $(ObjectsO)
-
-clean-local::
-ifneq ($(strip $(LibName.O)),)
-       -$(Verb) $(RM) -f $(LibName.O)
+ifndef LOADABLE_MODULE
+BUILD_ARCHIVE = 1
 endif
-
-ifdef NO_INSTALL
-install-local::
-       $(Echo) Install circumvented with NO_INSTALL
-uninstall-local::
-       $(Echo) Uninstall circumvented with NO_INSTALL
-else
-DestRelinkedLib = $(PROJ_libdir)/$(LIBRARYNAME).o
-
-install-local:: $(DestRelinkedLib)
-
-$(DestRelinkedLib): $(LibName.O) $(PROJ_libdir)
-       $(Echo) Installing $(BuildMode) Object Library $(DestRelinkedLib)
-       $(Verb) $(INSTALL) $(LibName.O) $(DestRelinkedLib)
-
-uninstall-local::
-       $(Echo) Uninstalling $(BuildMode) Object Library $(DestRelinkedLib)
-       -$(Verb) $(RM) -f $(DestRelinkedLib)
 endif
 endif
 
@@ -1055,13 +1295,19 @@ install-local::
 uninstall-local::
        $(Echo) Uninstall circumvented with NO_INSTALL
 else
-DestArchiveLib := $(PROJ_libdir)/lib$(LIBRARYNAME).a
+ifdef NO_INSTALL_ARCHIVES
+install-local::
+       $(Echo) Install circumvented with NO_INSTALL
+uninstall-local::
+       $(Echo) Uninstall circumvented with NO_INSTALL
+else
+DestArchiveLib := $(DESTDIR)$(PROJ_libdir)/lib$(LIBRARYNAME).a
 
 install-local:: $(DestArchiveLib)
 
-$(DestArchiveLib): $(LibName.A) $(PROJ_libdir)
+$(DestArchiveLib): $(LibName.A) $(DESTDIR)$(PROJ_libdir)
        $(Echo) Installing $(BuildMode) Archive Library $(DestArchiveLib)
-       $(Verb) $(MKDIR) $(PROJ_libdir)
+       $(Verb) $(MKDIR) $(DESTDIR)$(PROJ_libdir)
        $(Verb) $(INSTALL) $(LibName.A) $(DestArchiveLib)
 
 uninstall-local::
@@ -1069,6 +1315,7 @@ uninstall-local::
        -$(Verb) $(RM) -f $(DestArchiveLib)
 endif
 endif
+endif
 
 # endif LIBRARYNAME
 endif
@@ -1082,10 +1329,20 @@ ifdef TOOLNAME
 #---------------------------------------------------------
 # Set up variables for building a tool.
 #---------------------------------------------------------
+TOOLEXENAME := $(strip $(TOOLNAME))$(EXEEXT)
+ifdef EXAMPLE_TOOL
+ToolBuildPath   := $(ExmplDir)/$(TOOLEXENAME)
+else
+ToolBuildPath   := $(ToolDir)/$(TOOLEXENAME)
+endif
+
+# TOOLALIAS is a name to symlink (or copy) the tool to.
+ifdef TOOLALIAS
 ifdef EXAMPLE_TOOL
-ToolBuildPath   := $(ExmplDir)/$(strip $(TOOLNAME))$(EXEEXT)
+ToolAliasBuildPath   := $(ExmplDir)/$(strip $(TOOLALIAS))$(EXEEXT)
 else
-ToolBuildPath   := $(ToolDir)/$(strip $(TOOLNAME))$(EXEEXT)
+ToolAliasBuildPath   := $(ToolDir)/$(strip $(TOOLALIAS))$(EXEEXT)
+endif
 endif
 
 #---------------------------------------------------------
@@ -1096,25 +1353,67 @@ endif
 # not exporting all of the weak symbols from the binary.  This reduces dyld
 # startup time by 4x on darwin in some cases.
 ifdef TOOL_NO_EXPORTS
-ifeq ($(OS),Darwin)
-LD.Flags += -Wl,-exported_symbol -Wl,_main
+ifeq ($(HOST_OS),Darwin)
+
+# Tiger tools don't support this.
+ifneq ($(DARWIN_MAJVERS),4)
+LD.Flags += -Wl,-exported_symbol,_main
+endif
+endif
+
+ifeq ($(HOST_OS), $(filter $(HOST_OS), DragonFly Linux NetBSD FreeBSD GNU/kFreeBSD GNU))
+ifneq ($(ARCH), Mips)
+  LD.Flags += -Wl,--version-script=$(LLVM_SRC_ROOT)/autoconf/ExportMap.map
 endif
+endif
+endif
+
+#---------------------------------------------------------
+# Tool Order File Support
+#---------------------------------------------------------
+
+ifeq ($(HOST_OS),Darwin)
+ifdef TOOL_ORDER_FILE
+
+LD.Flags += -Wl,-order_file,$(TOOL_ORDER_FILE)
 
-ifeq ($(OS), $(filter $(OS), Linux NetBSD FreeBSD))
-LD.Flags += -Wl,--version-script=$(LLVM_SRC_ROOT)/autoconf/ExportMap.map
 endif
 endif
 
+#---------------------------------------------------------
+# Tool Version Info Support
+#---------------------------------------------------------
+
+ifeq ($(HOST_OS),Darwin)
+ifdef TOOL_INFO_PLIST
+
+LD.Flags += -Wl,-sectcreate,__TEXT,__info_plist,$(ObjDir)/$(TOOL_INFO_PLIST)
+
+$(ToolBuildPath): $(ObjDir)/$(TOOL_INFO_PLIST)
+
+$(ObjDir)/$(TOOL_INFO_PLIST): $(PROJ_SRC_DIR)/$(TOOL_INFO_PLIST).in $(ObjDir)/.dir
+       $(Echo) "Creating $(TOOLNAME) '$(TOOL_INFO_PLIST)' file..."
+       $(Verb)sed -e "s#@TOOL_INFO_UTI@#$(TOOL_INFO_UTI)#g" \
+                  -e "s#@TOOL_INFO_NAME@#$(TOOL_INFO_NAME)#g" \
+                  -e "s#@TOOL_INFO_VERSION@#$(TOOL_INFO_VERSION)#g" \
+                -e "s#@TOOL_INFO_BUILD_VERSION@#$(TOOL_INFO_BUILD_VERSION)#g" \
+                  $< > $@
+
+endif
+endif
 
 #---------------------------------------------------------
 # Provide targets for building the tools
 #---------------------------------------------------------
-all-local:: $(ToolBuildPath)
+all-local:: $(ToolBuildPath) $(ToolAliasBuildPath)
 
 clean-local::
 ifneq ($(strip $(ToolBuildPath)),)
        -$(Verb) $(RM) -f $(ToolBuildPath)
 endif
+ifneq ($(strip $(ToolAliasBuildPath)),)
+       -$(Verb) $(RM) -f $(ToolAliasBuildPath)
+endif
 
 ifdef EXAMPLE_TOOL
 $(ToolBuildPath): $(ExmplDir)/.dir
@@ -1122,12 +1421,34 @@ else
 $(ToolBuildPath): $(ToolDir)/.dir
 endif
 
+ifdef CODESIGN_TOOLS
+TOOL_CODESIGN_IDENTITY ?= -
+
+$(ToolBuildPath): $(ObjectsO) $(ProjLibsPaths) $(LLVMLibsPaths)
+       $(Echo) Linking $(BuildMode) executable $(TOOLNAME) $(StripWarnMsg)
+       $(Verb) $(Link) -o $@ $(TOOLLINKOPTS) $(ObjectsO) $(ProjLibsOptions) \
+       $(LLVMLibsOptions) $(ExtraLibs) $(TOOLLINKOPTSB) $(LIBS)
+       $(Echo) ======= Finished Linking $(BuildMode) Executable $(TOOLNAME) \
+          $(StripWarnMsg)
+       $(Echo) ======= Code-Signing $(BuildMode) Executable $(TOOLNAME)
+       $(Verb) codesign -s $(TOOL_CODESIGN_IDENTITY) $@
+else
 $(ToolBuildPath): $(ObjectsO) $(ProjLibsPaths) $(LLVMLibsPaths)
        $(Echo) Linking $(BuildMode) executable $(TOOLNAME) $(StripWarnMsg)
        $(Verb) $(Link) -o $@ $(TOOLLINKOPTS) $(ObjectsO) $(ProjLibsOptions) \
        $(LLVMLibsOptions) $(ExtraLibs) $(TOOLLINKOPTSB) $(LIBS)
        $(Echo) ======= Finished Linking $(BuildMode) Executable $(TOOLNAME) \
           $(StripWarnMsg)
+endif
+
+ifneq ($(strip $(ToolAliasBuildPath)),)
+$(ToolAliasBuildPath): $(ToolBuildPath)
+       $(Echo) Creating $(BuildMode) Alias $(TOOLALIAS) $(StripWarnMsg)
+       $(Verb) $(RM) -f $(ToolAliasBuildPath)
+       $(Verb) $(AliasTool) $(notdir $(ToolBuildPath)) $(ToolAliasBuildPath)
+       $(Echo) ======= Finished Creating $(BuildMode) Alias $(TOOLALIAS) \
+          $(StripWarnMsg)
+endif
 
 ifdef NO_INSTALL
 install-local::
@@ -1135,17 +1456,41 @@ install-local::
 uninstall-local::
        $(Echo) Uninstall circumvented with NO_INSTALL
 else
-DestTool = $(PROJ_bindir)/$(TOOLNAME)
+
+ifdef INTERNAL_TOOL
+ToolBinDir = $(DESTDIR)$(PROJ_internal_prefix)/bin
+else
+ToolBinDir = $(DESTDIR)$(PROJ_bindir)
+endif
+DestTool = $(ToolBinDir)/$(program_prefix)$(TOOLEXENAME)
 
 install-local:: $(DestTool)
 
-$(DestTool): $(ToolBuildPath) $(PROJ_bindir)
+$(DestTool): $(ToolBuildPath)
        $(Echo) Installing $(BuildMode) $(DestTool)
+       $(Verb) $(MKDIR) $(ToolBinDir)
        $(Verb) $(ProgInstall) $(ToolBuildPath) $(DestTool)
 
 uninstall-local::
        $(Echo) Uninstalling $(BuildMode) $(DestTool)
        -$(Verb) $(RM) -f $(DestTool)
+
+# TOOLALIAS install.
+ifdef TOOLALIAS
+DestToolAlias = $(ToolBinDir)/$(program_prefix)$(TOOLALIAS)$(EXEEXT)
+
+install-local:: $(DestToolAlias)
+
+$(DestToolAlias): $(DestTool)
+       $(Echo) Installing $(BuildMode) $(DestToolAlias)
+       $(Verb) $(RM) -f $(DestToolAlias)
+       $(Verb) $(AliasTool) $(notdir $(DestTool)) $(DestToolAlias)
+
+uninstall-local::
+       $(Echo) Uninstalling $(BuildMode) $(DestToolAlias)
+       -$(Verb) $(RM) -f $(DestToolAlias)
+endif
+
 endif
 endif
 
@@ -1154,10 +1499,12 @@ endif
 ###############################################################################
 
 # FIXME: This should be checking for "if not GCC or ICC", not for "if HP-UX"
-ifeq ($(OS),HP-UX)
+ifeq ($(HOST_OS),HP-UX)
   DISABLE_AUTO_DEPENDENCIES=1
 endif
 
+COMPILE_DEPS = $(OBJECT_DIRS) $(BUILT_SOURCES) $(PROJ_MAKEFILE)
+
 # Provide rule sets for when dependency generation is enabled
 ifndef DISABLE_AUTO_DEPENDENCIES
 
@@ -1168,175 +1515,127 @@ ifndef DISABLE_AUTO_DEPENDENCIES
 DEPEND_OPTIONS = -MMD -MP -MF "$(ObjDir)/$*.d.tmp" \
          -MT "$(ObjDir)/$*.o" -MT "$(ObjDir)/$*.d"
 
-# If the build succeeded, move the dependency file over.  If it failed, put an
-# empty file there.
+# If the build succeeded, move the dependency file over, otherwise
+# remove it.
 DEPEND_MOVEFILE = then $(MV) -f "$(ObjDir)/$*.d.tmp" "$(ObjDir)/$*.d"; \
                   else $(RM) "$(ObjDir)/$*.d.tmp"; exit 1; fi
 
-$(ObjDir)/%.o: %.cpp $(ObjDir)/.dir $(BUILT_SOURCES)
-       $(Echo) "Compiling $*.cpp for $(BuildMode) build " $(PIC_FLAG)
+$(ObjDir)/%.o: %.cpp $(COMPILE_DEPS)
+       $(Echo) "Compiling $*.cpp for $(BuildMode) build" $(PIC_FLAG)
+       $(Verb) if $(Compile.CXX) $(DEPEND_OPTIONS) $< -o $(ObjDir)/$*.o ; \
+               $(DEPEND_MOVEFILE)
+
+$(ObjDir)/%.o: %.mm $(COMPILE_DEPS)
+       $(Echo) "Compiling $*.mm for $(BuildMode) build" $(PIC_FLAG)
        $(Verb) if $(Compile.CXX) $(DEPEND_OPTIONS) $< -o $(ObjDir)/$*.o ; \
                $(DEPEND_MOVEFILE)
 
-$(ObjDir)/%.o: %.cc $(ObjDir)/.dir $(BUILT_SOURCES)
+$(ObjDir)/%.o: %.cc $(COMPILE_DEPS)
        $(Echo) "Compiling $*.cc for $(BuildMode) build" $(PIC_FLAG)
        $(Verb) if $(Compile.CXX) $(DEPEND_OPTIONS) $< -o $(ObjDir)/$*.o ; \
                $(DEPEND_MOVEFILE)
 
-$(ObjDir)/%.o: %.c $(ObjDir)/.dir $(BUILT_SOURCES)
+$(ObjDir)/%.o: %.c $(COMPILE_DEPS)
        $(Echo) "Compiling $*.c for $(BuildMode) build" $(PIC_FLAG)
        $(Verb) if $(Compile.C) $(DEPEND_OPTIONS) $< -o $(ObjDir)/$*.o ; \
                $(DEPEND_MOVEFILE)
 
-#---------------------------------------------------------
-# Create .bc files in the ObjDir directory from .cpp .cc and .c files...
-#---------------------------------------------------------
-
-$(ObjDir)/%.ll: %.cpp $(ObjDir)/.dir $(BUILT_SOURCES) $(LLVMGXX)
-       $(Echo) "Compiling $*.cpp for $(BuildMode) build (bytecode)"
-       $(Verb) if $(BCCompile.CXX) -MD -MT $@ -MP -MF "$(ObjDir)/$*.BCCXXd" \
-                              $< -o $@ -S -emit-llvm ; \
-       then $(MV) -f "$(ObjDir)/$*.BCCXXd" "$(ObjDir)/$*.d"; \
-       else $(RM) -f "$(ObjDir)/$*.BCCXXd"; exit 1; fi
-       $(call UPGRADE_MSG,$@)
-       $(call UPGRADE_LL,$@)
-
-$(ObjDir)/%.ll: %.cc $(ObjDir)/.dir $(BUILT_SOURCES) $(LLVMGXX)
-       $(Echo) "Compiling $*.cc for $(BuildMode) build (bytecode)"
-       $(Verb) if $(BCCompile.CXX) -MD -MT $@ -MP -MF "$(ObjDir)/$*.BCCXXd" \
-                              $< -o $@ -S -emit-llvm ; \
-       then $(MV) -f "$(ObjDir)/$*.BCCXXd" "$(ObjDir)/$*.d"; \
-       else $(RM) -f "$(ObjDir)/$*.BCCXXd"; exit 1; fi
-       $(call UPGRADE_MSG,$@)
-       $(call UPGRADE_LL,$@)
-
-$(ObjDir)/%.ll: %.c $(ObjDir)/.dir $(BUILT_SOURCES) $(LLVMGCC)
-       $(Echo) "Compiling $*.c for $(BuildMode) build (bytecode)"
-       $(Verb) if $(BCCompile.C) -MD -MT $@ -MP -MF "$(ObjDir)/$*.BCCd" \
-                            $< -o $@ -S -emit-llvm ; \
-       then $(MV) -f "$(ObjDir)/$*.BCCd" "$(ObjDir)/$*.d"; \
-       else $(RM) -f "$(ObjDir)/$*.BCCd"; exit 1; fi
-       $(call UPGRADE_MSG,$@)
-       $(call UPGRADE_LL,$@)
+$(ObjDir)/%.o: %.m $(COMPILE_DEPS)
+       $(Echo) "Compiling $*.m for $(BuildMode) build" $(PIC_FLAG)
+       $(Verb) if $(Compile.C) $(DEPEND_OPTIONS) $< -o $(ObjDir)/$*.o ; \
+               $(DEPEND_MOVEFILE)
 
 # Provide alternate rule sets if dependencies are disabled
 else
 
-$(ObjDir)/%.o: %.cpp $(ObjDir)/.dir $(BUILT_SOURCES)
+$(ObjDir)/%.o: %.cpp $(COMPILE_DEPS)
        $(Echo) "Compiling $*.cpp for $(BuildMode) build" $(PIC_FLAG)
        $(Compile.CXX) $< -o $@
 
-$(ObjDir)/%.o: %.cc $(ObjDir)/.dir $(BUILT_SOURCES)
+$(ObjDir)/%.o: %.mm $(COMPILE_DEPS)
+       $(Echo) "Compiling $*.mm for $(BuildMode) build" $(PIC_FLAG)
+       $(Compile.CXX) $< -o $@
+
+$(ObjDir)/%.o: %.cc $(COMPILE_DEPS)
        $(Echo) "Compiling $*.cc for $(BuildMode) build" $(PIC_FLAG)
        $(Compile.CXX) $< -o $@
 
-$(ObjDir)/%.o: %.c $(ObjDir)/.dir $(BUILT_SOURCES)
+$(ObjDir)/%.o: %.c $(COMPILE_DEPS)
        $(Echo) "Compiling $*.c for $(BuildMode) build" $(PIC_FLAG)
        $(Compile.C) $< -o $@
 
-$(ObjDir)/%.ll: %.cpp $(ObjDir)/.dir $(BUILT_SOURCES) $(LLVMGXX)
-       $(Echo) "Compiling $*.cpp for $(BuildMode) build (bytecode)"
-       $(BCCompile.CXX) $< -o $@ -S -emit-llvm
-       $(call UPGRADE_MSG,$@)
-       $(call UPGRADE_LL,$@)
-
-$(ObjDir)/%.ll: %.cc $(ObjDir)/.dir $(BUILT_SOURCES) $(LLVMGXX)
-       $(Echo) "Compiling $*.cc for $(BuildMode) build (bytecode)"
-       $(BCCompile.CXX) $< -o $@ -S -emit-llvm
-       $(call UPGRADE_MSG,$@)
-       $(call UPGRADE_LL,$@)
-
-$(ObjDir)/%.ll: %.c $(ObjDir)/.dir $(BUILT_SOURCES) $(LLVMGCC)
-       $(Echo) "Compiling $*.c for $(BuildMode) build (bytecode)"
-       $(BCCompile.C) $< -o $@ -S -emit-llvm
-       $(call UPGRADE_MSG,@)
-       $(call UPGRADE_LL,@)
-
+$(ObjDir)/%.o: %.m $(COMPILE_DEPS)
+       $(Echo) "Compiling $*.m for $(BuildMode) build" $(PIC_FLAG)
+       $(Compile.C) $< -o $@
 endif
 
 
 ## Rules for building preprocessed (.i/.ii) outputs.
-$(BuildMode)/%.ii: %.cpp $(ObjDir)/.dir $(BUILT_SOURCES)
+$(BuildMode)/%.ii: %.cpp $(COMPILE_DEPS)
        $(Echo) "Compiling $*.cpp for $(BuildMode) build to .ii file"
        $(Verb) $(Preprocess.CXX) $< -o $@
 
-$(BuildMode)/%.ii: %.cc $(ObjDir)/.dir $(BUILT_SOURCES)
+$(BuildMode)/%.ii: %.mm $(COMPILE_DEPS)
+       $(Echo) "Compiling $*.mm for $(BuildMode) build to .ii file"
+       $(Verb) $(Preprocess.CXX) $< -o $@
+
+$(BuildMode)/%.ii: %.cc $(COMPILE_DEPS)
        $(Echo) "Compiling $*.cc for $(BuildMode) build to .ii file"
        $(Verb) $(Preprocess.CXX) $< -o $@
 
-$(BuildMode)/%.i: %.c $(ObjDir)/.dir $(BUILT_SOURCES)
+$(BuildMode)/%.i: %.c $(COMPILE_DEPS)
        $(Echo) "Compiling $*.c for $(BuildMode) build to .i file"
        $(Verb) $(Preprocess.C) $< -o $@
 
+$(BuildMode)/%.i: %.m $(COMPILE_DEPS)
+       $(Echo) "Compiling $*.m for $(BuildMode) build to .i file"
+       $(Verb) $(Preprocess.C) $< -o $@
+
 
-$(ObjDir)/%.s: %.cpp $(ObjDir)/.dir $(BUILT_SOURCES)
+$(ObjDir)/%.s: %.cpp $(COMPILE_DEPS)
        $(Echo) "Compiling $*.cpp to asm for $(BuildMode) build" $(PIC_FLAG)
        $(Compile.CXX) $< -o $@ -S
 
-$(ObjDir)/%.s: %.cc $(ObjDir)/.dir $(BUILT_SOURCES)
+$(ObjDir)/%.s: %.mm $(COMPILE_DEPS)
+       $(Echo) "Compiling $*.mm to asm for $(BuildMode) build" $(PIC_FLAG)
+       $(Compile.CXX) $< -o $@ -S
+
+$(ObjDir)/%.s: %.cc $(COMPILE_DEPS)
        $(Echo) "Compiling $*.cc to asm for $(BuildMode) build" $(PIC_FLAG)
        $(Compile.CXX) $< -o $@ -S
 
-$(ObjDir)/%.s: %.c $(ObjDir)/.dir $(BUILT_SOURCES)
+$(ObjDir)/%.s: %.c $(COMPILE_DEPS)
        $(Echo) "Compiling $*.c to asm for $(BuildMode) build" $(PIC_FLAG)
        $(Compile.C) $< -o $@ -S
 
-
-# make the C and C++ compilers strip debug info out of bytecode libraries.
-ifdef DEBUG_RUNTIME
-$(ObjectsBC): $(ObjDir)/%.bc: $(ObjDir)/%.ll $(LLVMAS) $(LOPT)
-       $(Echo) "Compiling $*.ll to $*.bc for $(BuildMode) build (bytecode)"
-       $(Verb) $(LLVMAS) $< -o - | $(LOPT) -std-compile-opts -o $@ -f
-else
-$(ObjectsBC): $(ObjDir)/%.bc: $(ObjDir)/%.ll $(LLVMAS) $(LOPT)
-       $(Echo) "Compiling $*.ll to $*.bc for $(BuildMode) build (bytecode)"
-       $(Verb) $(LLVMAS) $< -o - | \
-          $(LOPT) -std-compile-opts -strip-debug -o $@ -f
-endif
-
-
-#---------------------------------------------------------
-# Provide rule to build .bc files from .ll sources,
-# regardless of dependencies
-#---------------------------------------------------------
-$(ObjDir)/%.bc: %.ll $(ObjDir)/.dir $(LLVMAS)
-       $(Echo) "Compiling $*.ll for $(BuildMode) build"
-       $(Verb) $(LLVMAS) $< -f -o $@
+$(ObjDir)/%.s: %.m $(COMPILE_DEPS)
+       $(Echo) "Compiling $*.m to asm for $(BuildMode) build" $(PIC_FLAG)
+       $(Compile.C) $< -o $@ -S
 
 ###############################################################################
-# LLVMC: Provide rules for compiling llvmc plugins, pt. 2
+# TABLEGEN: Provide rules for running tblgen to produce *.inc files
 ###############################################################################
 
-ifdef BUILD_AUTOGENERATED_INC
-
-# This needs to be in a separate section, otherwise we get an infinite loop:)
+ifdef TARGET
+TABLEGEN_INC_FILES_COMMON = 1
+endif
 
-# This stuff is mostly copied from the TABLEGEN section below
-# TODO: merge
+ifdef TABLEGEN_INC_FILES_COMMON
 
-LLVMCPluginSrc := $(strip $(wildcard $(PROJ_SRC_DIR)/*.td))
-TDFiles := $(LLVMCPluginSrc) \
-       $(strip $(wildcard $(LLVM_SRC_ROOT)/include/llvm/CompilerDriver/*.td))
 INCFiles := $(filter %.inc,$(BUILT_SOURCES))
 INCTMPFiles := $(INCFiles:%=$(ObjDir)/%.tmp)
 .PRECIOUS: $(INCTMPFiles) $(INCFiles)
 
-# All of these files depend on tblgen and the .td files.
-$(INCTMPFiles) : $(TBLGEN) $(TDFiles)
-
+# INCFiles rule: All of the tblgen generated files are emitted to
+# $(ObjDir)/%.inc.tmp, instead of emitting them directly to %.inc.  This allows
+# us to only "touch" the real file if the contents of it change.  IOW, if
+# tblgen is modified, all of the .inc.tmp files are regenerated, but no
+# dependencies of the .inc files are, unless the contents of the .inc file
+# changes.
 $(INCFiles) : %.inc : $(ObjDir)/%.inc.tmp
        $(Verb) $(CMP) -s $@ $< || $(CP) $< $@
 
-$(ObjDir)/AutoGenerated.inc.tmp: $(LLVMCPluginSrc) $(ObjDir)/.dir \
-                               $(TBLGEN) $(TD_COMMON)
-       $(Echo) "Building LLVMC configuration library with tblgen"
-       $(Verb) $(TableGen) -gen-llvmc -o $(call SYSPATH, $@) $<
-
-endif # BUILD_AUTOGENERATED_INC
-
-###############################################################################
-# TABLEGEN: Provide rules for running tblgen to produce *.inc files
-###############################################################################
+endif # TABLEGEN_INC_FILES_COMMON
 
 ifdef TARGET
 
@@ -1346,92 +1645,94 @@ TDFiles := $(strip $(wildcard $(PROJ_SRC_DIR)/*.td) \
            $(LLVM_SRC_ROOT)/include/llvm/Target/TargetSchedule.td \
            $(LLVM_SRC_ROOT)/include/llvm/Target/TargetSelectionDAG.td \
            $(LLVM_SRC_ROOT)/include/llvm/CodeGen/ValueTypes.td) \
-           $(wildcard $(LLVM_SRC_ROOT)/include/llvm/Intrinsics*.td)
-INCFiles := $(filter %.inc,$(BUILT_SOURCES))
-INCTMPFiles := $(INCFiles:%=$(ObjDir)/%.tmp)
-.PRECIOUS: $(INCTMPFiles) $(INCFiles)
+           $(wildcard $(LLVM_SRC_ROOT)/include/llvm/IR/Intrinsics*.td)
 
-# All of these files depend on tblgen and the .td files.
-$(INCTMPFiles) : $(TBLGEN) $(TDFiles)
-
-# INCFiles rule: All of the tblgen generated files are emitted to
-# $(ObjDir)/%.inc.tmp, instead of emitting them directly to %.inc.  This allows
-# us to only "touch" the real file if the contents of it change.  IOW, if
-# tblgen is modified, all of the .inc.tmp files are regenerated, but no
-# dependencies of the .inc files are, unless the contents of the .inc file
-# changes.
-$(INCFiles) : %.inc : $(ObjDir)/%.inc.tmp
-       $(Verb) $(CMP) -s $@ $< || $(CP) $< $@
-
-$(TARGET:%=$(ObjDir)/%GenRegisterNames.inc.tmp): \
-$(ObjDir)/%GenRegisterNames.inc.tmp : %.td $(ObjDir)/.dir
-       $(Echo) "Building $(<F) register names with tblgen"
-       $(Verb) $(TableGen) -gen-register-enums -o $(call SYSPATH, $@) $<
-
-$(TARGET:%=$(ObjDir)/%GenRegisterInfo.h.inc.tmp): \
-$(ObjDir)/%GenRegisterInfo.h.inc.tmp : %.td $(ObjDir)/.dir
-       $(Echo) "Building $(<F) register information header with tblgen"
-       $(Verb) $(TableGen) -gen-register-desc-header -o $(call SYSPATH, $@) $<
+# All .inc.tmp files depend on the .td files.
+$(INCTMPFiles) : $(TDFiles)
 
 $(TARGET:%=$(ObjDir)/%GenRegisterInfo.inc.tmp): \
-$(ObjDir)/%GenRegisterInfo.inc.tmp : %.td $(ObjDir)/.dir
+$(ObjDir)/%GenRegisterInfo.inc.tmp : %.td $(ObjDir)/.dir $(LLVM_TBLGEN)
        $(Echo) "Building $(<F) register info implementation with tblgen"
-       $(Verb) $(TableGen) -gen-register-desc -o $(call SYSPATH, $@) $<
-
-$(TARGET:%=$(ObjDir)/%GenInstrNames.inc.tmp): \
-$(ObjDir)/%GenInstrNames.inc.tmp : %.td $(ObjDir)/.dir
-       $(Echo) "Building $(<F) instruction names with tblgen"
-       $(Verb) $(TableGen) -gen-instr-enums -o $(call SYSPATH, $@) $<
+       $(Verb) $(LLVMTableGen) -gen-register-info -o $(call SYSPATH, $@) $<
 
 $(TARGET:%=$(ObjDir)/%GenInstrInfo.inc.tmp): \
-$(ObjDir)/%GenInstrInfo.inc.tmp : %.td $(ObjDir)/.dir
+$(ObjDir)/%GenInstrInfo.inc.tmp : %.td $(ObjDir)/.dir $(LLVM_TBLGEN)
        $(Echo) "Building $(<F) instruction information with tblgen"
-       $(Verb) $(TableGen) -gen-instr-desc -o $(call SYSPATH, $@) $<
+       $(Verb) $(LLVMTableGen) -gen-instr-info -o $(call SYSPATH, $@) $<
 
 $(TARGET:%=$(ObjDir)/%GenAsmWriter.inc.tmp): \
-$(ObjDir)/%GenAsmWriter.inc.tmp : %.td $(ObjDir)/.dir
+$(ObjDir)/%GenAsmWriter.inc.tmp : %.td $(ObjDir)/.dir $(LLVM_TBLGEN)
        $(Echo) "Building $(<F) assembly writer with tblgen"
-       $(Verb) $(TableGen) -gen-asm-writer -o $(call SYSPATH, $@) $<
+       $(Verb) $(LLVMTableGen) -gen-asm-writer -o $(call SYSPATH, $@) $<
 
 $(TARGET:%=$(ObjDir)/%GenAsmWriter1.inc.tmp): \
-$(ObjDir)/%GenAsmWriter1.inc.tmp : %.td $(ObjDir)/.dir
+$(ObjDir)/%GenAsmWriter1.inc.tmp : %.td $(ObjDir)/.dir $(LLVM_TBLGEN)
        $(Echo) "Building $(<F) assembly writer #1 with tblgen"
-       $(Verb) $(TableGen) -gen-asm-writer -asmwriternum=1 -o $(call SYSPATH, $@) $<
+       $(Verb) $(LLVMTableGen) -gen-asm-writer -asmwriternum=1 -o $(call SYSPATH, $@) $<
 
-$(TARGET:%=$(ObjDir)/%GenCodeEmitter.inc.tmp): \
-$(ObjDir)/%GenCodeEmitter.inc.tmp: %.td $(ObjDir)/.dir
-       $(Echo) "Building $(<F) code emitter with tblgen"
-       $(Verb) $(TableGen) -gen-emitter -o $(call SYSPATH, $@) $<
+$(TARGET:%=$(ObjDir)/%GenAsmMatcher.inc.tmp): \
+$(ObjDir)/%GenAsmMatcher.inc.tmp : %.td $(ObjDir)/.dir $(LLVM_TBLGEN)
+       $(Echo) "Building $(<F) assembly matcher with tblgen"
+       $(Verb) $(LLVMTableGen) -gen-asm-matcher -o $(call SYSPATH, $@) $<
+
+$(TARGET:%=$(ObjDir)/%GenMCCodeEmitter.inc.tmp): \
+$(ObjDir)/%GenMCCodeEmitter.inc.tmp: %.td $(ObjDir)/.dir $(LLVM_TBLGEN)
+       $(Echo) "Building $(<F) MC code emitter with tblgen"
+       $(Verb) $(LLVMTableGen) -gen-emitter -o $(call SYSPATH, $@) $<
+
+$(TARGET:%=$(ObjDir)/%GenMCPseudoLowering.inc.tmp): \
+$(ObjDir)/%GenMCPseudoLowering.inc.tmp: %.td $(ObjDir)/.dir $(LLVM_TBLGEN)
+       $(Echo) "Building $(<F) MC Pseudo instruction expander with tblgen"
+       $(Verb) $(LLVMTableGen) -gen-pseudo-lowering -o $(call SYSPATH, $@) $<
 
 $(TARGET:%=$(ObjDir)/%GenDAGISel.inc.tmp): \
-$(ObjDir)/%GenDAGISel.inc.tmp : %.td $(ObjDir)/.dir
+$(ObjDir)/%GenDAGISel.inc.tmp : %.td $(ObjDir)/.dir $(LLVM_TBLGEN)
        $(Echo) "Building $(<F) DAG instruction selector implementation with tblgen"
-       $(Verb) $(TableGen) -gen-dag-isel -o $(call SYSPATH, $@) $<
+       $(Verb) $(LLVMTableGen) -gen-dag-isel -o $(call SYSPATH, $@) $<
+
+$(TARGET:%=$(ObjDir)/%GenDisassemblerTables.inc.tmp): \
+$(ObjDir)/%GenDisassemblerTables.inc.tmp : %.td $(ObjDir)/.dir $(LLVM_TBLGEN)
+       $(Echo) "Building $(<F) disassembly tables with tblgen"
+       $(Verb) $(LLVMTableGen) -gen-disassembler -o $(call SYSPATH, $@) $<
 
 $(TARGET:%=$(ObjDir)/%GenFastISel.inc.tmp): \
-$(ObjDir)/%GenFastISel.inc.tmp : %.td $(ObjDir)/.dir
+$(ObjDir)/%GenFastISel.inc.tmp : %.td $(ObjDir)/.dir $(LLVM_TBLGEN)
        $(Echo) "Building $(<F) \"fast\" instruction selector implementation with tblgen"
-       $(Verb) $(TableGen) -gen-fast-isel -o $(call SYSPATH, $@) $<
+       $(Verb) $(LLVMTableGen) -gen-fast-isel -o $(call SYSPATH, $@) $<
 
-$(TARGET:%=$(ObjDir)/%GenSubtarget.inc.tmp): \
-$(ObjDir)/%GenSubtarget.inc.tmp : %.td $(ObjDir)/.dir
+$(TARGET:%=$(ObjDir)/%GenSubtargetInfo.inc.tmp): \
+$(ObjDir)/%GenSubtargetInfo.inc.tmp : %.td $(ObjDir)/.dir $(LLVM_TBLGEN)
        $(Echo) "Building $(<F) subtarget information with tblgen"
-       $(Verb) $(TableGen) -gen-subtarget -o $(call SYSPATH, $@) $<
+       $(Verb) $(LLVMTableGen) -gen-subtarget -o $(call SYSPATH, $@) $<
 
 $(TARGET:%=$(ObjDir)/%GenCallingConv.inc.tmp): \
-$(ObjDir)/%GenCallingConv.inc.tmp : %.td $(ObjDir)/.dir
+$(ObjDir)/%GenCallingConv.inc.tmp : %.td $(ObjDir)/.dir $(LLVM_TBLGEN)
        $(Echo) "Building $(<F) calling convention information with tblgen"
-       $(Verb) $(TableGen) -gen-callingconv -o $(call SYSPATH, $@) $<
+       $(Verb) $(LLVMTableGen) -gen-callingconv -o $(call SYSPATH, $@) $<
 
 $(TARGET:%=$(ObjDir)/%GenIntrinsics.inc.tmp): \
-$(ObjDir)/%GenIntrinsics.inc.tmp : Intrinsics%.td $(ObjDir)/.dir
-       $(Echo) "Building $(<F) calling convention information with tblgen"
-       $(Verb) $(TableGen) -gen-tgt-intrinsic -o $(call SYSPATH, $@) $<
+$(ObjDir)/%GenIntrinsics.inc.tmp : %.td $(ObjDir)/.dir $(LLVM_TBLGEN)
+       $(Echo) "Building $(<F) intrinsics information with tblgen"
+       $(Verb) $(LLVMTableGen) -gen-tgt-intrinsic -o $(call SYSPATH, $@) $<
+
+$(ObjDir)/ARMGenDecoderTables.inc.tmp : ARM.td $(ObjDir)/.dir $(LLVM_TBLGEN)
+       $(Echo) "Building $(<F) decoder tables with tblgen"
+       $(Verb) $(LLVMTableGen) -gen-arm-decoder -o $(call SYSPATH, $@) $<
+
+$(ObjDir)/%GenDFAPacketizer.inc.tmp : %.td $(ObjDir)/.dir $(LLVM_TBLGEN)
+       $(Echo) "Building $(<F) DFA packetizer tables with tblgen"
+       $(Verb) $(LLVMTableGen) -gen-dfa-packetizer -o $(call SYSPATH, $@) $<
+
+# Dump all the records to <target>.td.expanded.  This is useful for debugging.
+$(TARGET:%=%.td.expanded): \
+%.td.expanded : %.td $(LLVM_TBLGEN) $(TDFiles)
+       $(Echo) "Building a fully expanded version of $(<F)"
+       $(Verb) $(LLVMTableGen) -o $(call SYSPATH, $@) $<
 
 clean-local::
-       -$(Verb) $(RM) -f $(INCFiles)
+       -$(Verb) $(RM) -f $(INCFiles) $(TARGET).td.expanded
 
-endif
+endif # TARGET
 
 ###############################################################################
 # OTHER RULES: Other rules needed
@@ -1459,7 +1760,6 @@ clean-local::
 ifneq ($(strip $(ObjRootDir)),)
        -$(Verb) $(RM) -rf $(ObjRootDir)
 endif
-       -$(Verb) $(RM) -f core core.[0-9][0-9]* *.o *.d *~ *.flc
 ifneq ($(strip $(SHLIBEXT)),) # Extra paranoia - make real sure SHLIBEXT is set
        -$(Verb) $(RM) -f *$(SHLIBEXT)
 endif
@@ -1477,8 +1777,8 @@ ifndef DISABLE_AUTO_DEPENDENCIES
 ifndef IS_CLEANING_TARGET
 
 # Get the list of dependency files
-DependFiles := $(basename $(filter %.cpp %.c %.cc, $(Sources)))
-DependFiles := $(DependFiles:%=$(PROJ_OBJ_DIR)/$(BuildMode)/%.d)
+DependSourceFiles := $(basename $(filter %.cpp %.c %.cc %.m %.mm, $(Sources)))
+DependFiles := $(DependSourceFiles:%=$(PROJ_OBJ_DIR)/$(BuildMode)/%.d)
 
 -include $(DependFiles) ""
 
@@ -1503,6 +1803,21 @@ check::
          $(EchoCmd) No test directory ; \
        fi
 
+# An alias dating from when both lit and DejaGNU test runners were used.
+check-lit:: check
+
+check-all::
+       $(Verb) if test -d "$(PROJ_OBJ_ROOT)/test" ; then \
+         if test -f "$(PROJ_OBJ_ROOT)/test/Makefile" ; then \
+           $(EchoCmd) Running test suite ; \
+           $(MAKE) -C $(PROJ_OBJ_ROOT)/test check-local-all ; \
+         else \
+           $(EchoCmd) No Makefile in test directory ; \
+         fi ; \
+       else \
+         $(EchoCmd) No test directory ; \
+       fi
+
 ###############################################################################
 # UNITTESTS: Running the unittests test suite
 ###############################################################################
@@ -1511,7 +1826,7 @@ unittests::
        $(Verb) if test -d "$(PROJ_OBJ_ROOT)/unittests" ; then \
          if test -f "$(PROJ_OBJ_ROOT)/unittests/Makefile" ; then \
            $(EchoCmd) Running unittests test suite ; \
-           $(MAKE) -C $(PROJ_OBJ_ROOT)/unittests ; \
+           $(MAKE) -C $(PROJ_OBJ_ROOT)/unittests unitcheck; \
          else \
            $(EchoCmd) No Makefile in unittests directory ; \
          fi ; \
@@ -1588,7 +1903,7 @@ $(DistZip) : $(TopDistDir)/.makedistdir
        $(Verb) cd $(PROJ_OBJ_ROOT) ; $(ZIP) -rq $(DistZip) $(DistName)
 
 dist :: $(DistTarGZip) $(DistTarBZ2) $(DistZip)
-       $(Echo) ===== DISTRIBUTION PACKAGING SUCESSFUL =====
+       $(Echo) ===== DISTRIBUTION PACKAGING SUCCESSFUL =====
 
 DistCheckDir := $(PROJ_OBJ_ROOT)/_distcheckdir
 
@@ -1734,25 +2049,42 @@ uninstall-local::
 else
 install-local::
        $(Echo) Installing include files
-       $(Verb) $(MKDIR) $(PROJ_includedir)
+       $(Verb) $(MKDIR) $(DESTDIR)$(PROJ_includedir)
        $(Verb) if test -d "$(PROJ_SRC_ROOT)/include" ; then \
          cd $(PROJ_SRC_ROOT)/include && \
-         for  hdr in `find . -type f '!' '(' -name '*~' \
-             -o -name '.#*' -o -name '*.in' ')' -print | grep -v CVS | \
+         for hdr in `find . -type f \
+             '(' -name LICENSE.TXT \
+              -o -name '*.def' \
+              -o -name '*.h' \
+              -o -name '*.inc' \
+              -o -name '*.td' \
+             ')' -print | grep -v CVS | \
              grep -v .svn` ; do \
-           instdir=`dirname "$(PROJ_includedir)/$$hdr"` ; \
+           instdir=`dirname "$(DESTDIR)$(PROJ_includedir)/$$hdr"` ; \
            if test \! -d "$$instdir" ; then \
              $(EchoCmd) Making install directory $$instdir ; \
              $(MKDIR) $$instdir ;\
            fi ; \
-           $(DataInstall) $$hdr $(PROJ_includedir)/$$hdr ; \
+           $(DataInstall) $$hdr $(DESTDIR)$(PROJ_includedir)/$$hdr ; \
          done ; \
        fi
 ifneq ($(PROJ_SRC_ROOT),$(PROJ_OBJ_ROOT))
        $(Verb) if test -d "$(PROJ_OBJ_ROOT)/include" ; then \
          cd $(PROJ_OBJ_ROOT)/include && \
-         for hdr in `find . -type f -print | grep -v CVS` ; do \
-           $(DataInstall) $$hdr $(PROJ_includedir)/$$hdr ; \
+         for hdr in `find . -type f \
+             '(' -name LICENSE.TXT \
+              -o -name '*.def' \
+              -o -name '*.h' \
+              -o -name '*.inc' \
+              -o -name '*.td' \
+             ')' -print | grep -v CVS | \
+             grep -v .svn` ; do \
+           instdir=`dirname "$(DESTDIR)$(PROJ_includedir)/$$hdr"` ; \
+           if test \! -d "$$instdir" ; then \
+             $(EchoCmd) Making install directory $$instdir ; \
+             $(MKDIR) $$instdir ;\
+           fi ; \
+           $(DataInstall) $$hdr $(DESTDIR)$(PROJ_includedir)/$$hdr ; \
          done ; \
        fi
 endif
@@ -1764,10 +2096,10 @@ uninstall-local::
            $(RM) -f `find . -path '*/Internal' -prune -o '(' -type f \
              '!' '(' -name '*~' -o -name '.#*' \
         -o -name '*.in' ')' -print ')' | \
-        grep -v CVS | sed 's#^#$(PROJ_includedir)/#'` ; \
+        grep -v CVS | sed 's#^#$(DESTDIR)$(PROJ_includedir)/#'` ; \
          cd $(PROJ_SRC_ROOT)/include && \
            $(RM) -f `find . -path '*/Internal' -prune -o '(' -type f -name '*.in' \
-      -print ')' | sed 's#\.in$$##;s#^#$(PROJ_includedir)/#'` ; \
+      -print ')' | sed 's#\.in$$##;s#^#$(DESTDIR)$(PROJ_includedir)/#'` ; \
        fi
 endif
 endif
@@ -1805,6 +2137,7 @@ printvars::
        $(Echo) "LLVM_SRC_ROOT: " '$(LLVM_SRC_ROOT)'
        $(Echo) "LLVM_OBJ_ROOT: " '$(LLVM_OBJ_ROOT)'
        $(Echo) "PROJ_prefix  : " '$(PROJ_prefix)'
+       $(Echo) "PROJ_internal_prefix  : " '$(PROJ_internal_prefix)'
        $(Echo) "PROJ_bindir  : " '$(PROJ_bindir)'
        $(Echo) "PROJ_libdir  : " '$(PROJ_libdir)'
        $(Echo) "PROJ_etcdir  : " '$(PROJ_etcdir)'
@@ -1814,6 +2147,8 @@ printvars::
        $(Echo) "SrcMakefiles : " '$(SrcMakefiles)'
        $(Echo) "ObjDir       : " '$(ObjDir)'
        $(Echo) "LibDir       : " '$(LibDir)'
+       $(Echo) "LibexecDir   : " '$(LibexecDir)'
+       $(Echo) "ShareDir     : " '$(ShareDir)'
        $(Echo) "ToolDir      : " '$(ToolDir)'
        $(Echo) "ExmplDir     : " '$(ExmplDir)'
        $(Echo) "Sources      : " '$(Sources)'
@@ -1835,7 +2170,7 @@ printvars::
 ###
 # Debugging
 
-# General debugging rule, use 'make print-XXX' to print the
+# General debugging rule, use 'make dbg-print-XXX' to print the
 # definition, value and origin of XXX.
-print-%:
+make-print-%:
        $(error PRINT: $(value $*) = "$($*)" (from $(origin $*)))