#----------------------------------------------------------------------
# Clients fill in the source files to build
#----------------------------------------------------------------------
# C_SOURCES := main.c
# CXX_SOURCES :=
# OBJC_SOURCES :=
# OBJCXX_SOURCES :=
# DYLIB_C_SOURCES :=
# DYLIB_CXX_SOURCES :=
#
# Specifying DYLIB_ONLY has the effect of building dylib only, skipping
# the building of the a.out executable program.  For example,
# DYLIB_ONLY := YES
#
# Also might be of interest:
# FRAMEWORK_INCLUDES (Darwin only) :=
# CFLAGS_EXTRAS :=
# LD_EXTRAS :=
# SPLIT_DEBUG_SYMBOLS := YES
# CROSS_COMPILE :=
#
# And test/functionalities/archives/Makefile:
# MAKE_DSYM := NO
# ARCHIVE_NAME := libfoo.a
# ARCHIVE_C_SOURCES := a.c b.c

# Uncomment line below for debugging shell commands
# SHELL = /bin/sh -x

THIS_FILE_DIR := $(shell dirname $(realpath $(lastword $(MAKEFILE_LIST))))/
LLDB_BASE_DIR := $(THIS_FILE_DIR)../../../../../


#----------------------------------------------------------------------
# If TRIPLE is not defined try to set the ARCH, CC, CFLAGS, and more
# from the triple alone
#----------------------------------------------------------------------
TRIPLE_CFLAGS :=
ifneq "$(TRIPLE)" ""
	triple_space = $(subst -, ,$(TRIPLE))
	ARCH =$(word 1, $(triple_space))
	TRIPLE_VENDOR =$(word 2, $(triple_space))
	triple_os_and_version =$(shell echo $(word 3, $(triple_space)) | sed 's/\([a-z]*\)\(.*\)/\1 \2/')
	TRIPLE_OS =$(word 1, $(triple_os_and_version))
	TRIPLE_VERSION =$(word 2, $(triple_os_and_version))
	ifeq "$(TRIPLE_VENDOR)" "apple"
		ifeq "$(TRIPLE_OS)" "ios"
		    ifeq "$(SDKROOT)" ""
				# Set SDKROOT if it wasn't set
				ifneq (,$(findstring arm,$(ARCH)))
					SDKROOT = $(shell xcrun --sdk iphoneos --show-sdk-path)
					ifeq "$(TRIPLE_VERSION)" ""
						TRIPLE_VERSION =$(shell echo $(notdir $(SDKROOT)) | sed -e 's/.*\([0-9]\.[0-9]\).*/\1/')
					endif
					TRIPLE_CFLAGS :=-mios-version-min=$(TRIPLE_VERSION) -isysroot "$(SDKROOT)"
				else
					SDKROOT = $(shell xcrun --sdk iphonesimulator --show-sdk-path)
					ifeq "$(TRIPLE_VERSION)" ""
						TRIPLE_VERSION =$(shell echo $(notdir $(SDKROOT)) | sed -e 's/.*\([0-9]\.[0-9]\).*/\1/')
					endif
					TRIPLE_CFLAGS :=-mios-simulator-version-min=$(TRIPLE_VERSION) -isysroot "$(SDKROOT)"
				endif
			endif
		endif
	endif
endif

#----------------------------------------------------------------------
# If OS is not defined, use 'uname -s' to determine the OS name.
#
# uname on Windows gives "windows32", but most environments standardize
# on "Windows_NT", so we'll make it consistent here.  When running
# tests from Visual Studio, the environment variable isn't inherited
# all the way down to the process spawned for make.
#----------------------------------------------------------------------
HOST_OS = $(shell uname -s)
ifeq "$(HOST_OS)" "windows32"
	HOST_OS = Windows_NT
endif
ifeq "$(OS)" ""
	OS = $(HOST_OS)
endif

#----------------------------------------------------------------------
# If ARCH is not defined, default to x86_64.
#----------------------------------------------------------------------
ifeq "$(ARCH)" ""
ifeq "$(OS)" "Windows_NT"
	ARCH = x86
else
	ARCH = x86_64
endif
endif

#----------------------------------------------------------------------
# CC defaults to clang.
#
# If you change the defaults of CC, be sure to also change it in the file
# test/plugins/builder_base.py, which provides a Python way to return the
# value of the make variable CC -- getCompiler().
#
# See also these functions:
#   o cxx_compiler
#   o cxx_linker
#----------------------------------------------------------------------
CC ?= clang
ifeq "$(CC)" "cc"
	ifneq "$(shell which clang)" ""
		CC = clang
	else ifneq "$(shell which clang-3.5)" ""
		CC = clang-3.5
	else ifneq "$(shell which gcc)" ""
		CC = gcc
	endif
endif

#----------------------------------------------------------------------
# ARCHFLAG is the flag used to tell the compiler which architecture
# to compile for. The default is the flag that clang accepts.
#----------------------------------------------------------------------
ARCHFLAG ?= -arch

#----------------------------------------------------------------------
# Change any build/tool options needed
#----------------------------------------------------------------------
ifeq "$(OS)" "Darwin"
	DS := $(shell xcrun -find -toolchain default dsymutil)
	DSFLAGS =
	DSYM = $(EXE).dSYM
	AR := $(CROSS_COMPILE)libtool
	ARFLAGS := -static -o
else
	AR := $(CROSS_COMPILE)ar
	# On non-Apple platforms, -arch becomes -m
	ARCHFLAG := -m

	# i386, i686, x86 -> 32
	# amd64, x86_64, x64 -> 64
	ifeq "$(ARCH)" "amd64"
		override ARCH := $(subst amd64,64,$(ARCH))
	endif
	ifeq "$(ARCH)" "x86_64"
		override ARCH := $(subst x86_64,64,$(ARCH))
	endif
	ifeq "$(ARCH)" "x64"
		override ARCH := $(subst x64,64,$(ARCH))
	endif
	ifeq "$(ARCH)" "x86"
		override ARCH := $(subst x86,32,$(ARCH))
	endif
	ifeq "$(ARCH)" "i386"
		override ARCH := $(subst i386,32,$(ARCH))
	endif
	ifeq "$(ARCH)" "i686"
		override ARCH := $(subst i686,32,$(ARCH))
	endif
	ifeq "$(ARCH)" "powerpc"
		override ARCH := $(subst powerpc,32,$(ARCH))
	endif
	ifeq "$(ARCH)" "powerpc64"
		override ARCH := $(subst powerpc64,64,$(ARCH))
	endif
	ifeq "$(ARCH)" "aarch64"
		override ARCH :=
		override ARCHFLAG :=
	endif
	ifeq "$(ARCH)" "arm"
		override ARCH :=
		override ARCHFLAG :=
	endif
	ifeq "$(ARCH)" "s390x"
		override ARCH :=
		override ARCHFLAG :=
	endif
	ifeq "$(findstring mips,$(ARCH))" "mips"
		override ARCHFLAG := -
	endif

	ifeq "$(SPLIT_DEBUG_SYMBOLS)" "YES"
		DSYM = $(EXE).debug
	endif
endif

LIMIT_DEBUG_INFO_FLAGS =
NO_LIMIT_DEBUG_INFO_FLAGS =
MODULE_DEBUG_INFO_FLAGS =
ifneq (,$(findstring clang,$(CC)))
   LIMIT_DEBUG_INFO_FLAGS += -flimit-debug-info
   NO_LIMIT_DEBUG_INFO_FLAGS += -fno-limit-debug-info
   MODULE_DEBUG_INFO_FLAGS += -gmodules
endif

DEBUG_INFO_FLAG ?= -g

CFLAGS ?= $(DEBUG_INFO_FLAG) -O0 -fno-builtin
ifeq "$(OS)" "Darwin"
	CFLAGS += $(ARCHFLAG) $(ARCH) $(FRAMEWORK_INCLUDES) $(CFLAGS_EXTRAS) -I$(LLDB_BASE_DIR)include
else
	CFLAGS += $(ARCHFLAG)$(ARCH) $(FRAMEWORK_INCLUDES) $(CFLAGS_EXTRAS) -I$(LLDB_BASE_DIR)include
endif

CFLAGS += -include $(THIS_FILE_DIR)test_common.h $(TRIPLE_CFLAGS)

# Use this one if you want to build one part of the result without debug information:
ifeq "$(OS)" "Darwin"
	CFLAGS_NO_DEBUG = -O0 $(ARCHFLAG) $(ARCH) $(FRAMEWORK_INCLUDES) $(CFLAGS_EXTRAS) $(TRIPLE_CFLAGS)
else
	CFLAGS_NO_DEBUG = -O0 $(ARCHFLAG)$(ARCH) $(FRAMEWORK_INCLUDES) $(CFLAGS_EXTRAS) $(TRIPLE_CFLAGS)
endif

ifeq "$(MAKE_DWO)" "YES"
	CFLAGS += -gsplit-dwarf
endif

ifeq "$(MAKE_GMODULES)" "YES"
	CFLAGS += -fmodules -gmodules
endif

CXXFLAGS += -std=c++11
# FIXME: C++ modules aren't supported on all platforms.
CXXFLAGS += $(subst -fmodules,, $(CFLAGS))
LD = $(CC)
LDFLAGS ?= $(CFLAGS)
LDFLAGS += $(LD_EXTRAS)
ifeq (,$(filter $(OS), Windows_NT Android))
	ifneq (,$(filter YES,$(ENABLE_THREADS)))
		LDFLAGS += -pthread
	endif
endif
OBJECTS =
EXE ?= a.out

ifneq "$(DYLIB_NAME)" ""
	ifeq "$(OS)" "Darwin"
		DYLIB_FILENAME = lib$(DYLIB_NAME).dylib
		DYLIB_EXECUTABLE_PATH ?= @executable_path
	else ifeq "$(OS)" "Windows_NT"
		DYLIB_FILENAME = $(DYLIB_NAME).dll
	else
		DYLIB_FILENAME = lib$(DYLIB_NAME).so
	endif
endif

# Function that returns the counterpart C++ compiler, given $(CC) as arg.
cxx_compiler_notdir = $(if $(findstring clang,$(1)), \
                           $(subst clang,clang++,$(1)), \
                           $(if $(findstring icc,$(1)), \
                                $(subst icc,icpc,$(1)), \
                                $(if $(findstring llvm-gcc,$(1)), \
                                     $(subst llvm-gcc,llvm-g++,$(1)), \
                                     $(if $(findstring gcc,$(1)), \
                                          $(subst gcc,g++,$(1)), \
                                          $(subst cc,c++,$(1))))))
cxx_compiler = $(if $(findstring /,$(1)),$(join $(dir $(1)), $(call cxx_compiler_notdir,$(notdir $(1)))),$(call cxx_compiler_notdir,$(1)))

# Function that returns the C++ linker, given $(CC) as arg.
cxx_linker_notdir = $(if $(findstring clang,$(1)), \
                         $(subst clang,clang++,$(1)), \
                         $(if $(findstring icc,$(1)), \
                              $(subst icc,icpc,$(1)), \
                              $(if $(findstring llvm-gcc,$(1)), \
                                   $(subst llvm-gcc,llvm-g++,$(1)), \
                                   $(if $(findstring gcc,$(1)), \
                                        $(subst gcc,g++,$(1)), \
                                        $(subst cc,c++,$(1))))))
cxx_linker = $(if $(findstring /,$(1)),$(join $(dir $(1)), $(call cxx_linker_notdir,$(notdir $(1)))),$(call cxx_linker_notdir,$(1)))

OBJCOPY := $(CROSS_COMPILE)objcopy

#----------------------------------------------------------------------
# Windows specific options
#----------------------------------------------------------------------
ifeq "$(OS)" "Windows_NT"
	ifneq (,$(findstring clang,$(CC)))
		# Clang for Windows doesn't support C++ Exceptions
		CXXFLAGS += -fno-exceptions
		CXXFLAGS += -D_HAS_EXCEPTIONS=0
		ifeq "$(VisualStudioVersion)" "14.0"
			CXXFLAGS += -fms-compatibility-version=19.0
		endif
		# The MSVC linker doesn't understand long section names
		# generated by the clang compiler.
		LDFLAGS += -fuse-ld=lld
	endif
endif

#----------------------------------------------------------------------
# Android specific options
#----------------------------------------------------------------------
ifeq "$(OS)" "Android"
    ifdef PIE
        LDFLAGS += -pie
    endif
    replace_with = $(if $(findstring clang,$(1)), \
                        $(subst clang,$(2),$(1)), \
                        $(if $(findstring gcc,$(1)), \
                             $(subst gcc,$(2),$(1)), \
                             $(subst cc,$(2),$(1))))
    ifeq "$(notdir $(CC))" "$(CC)"
        replace_cc_with = $(call replace_with,$(CC),$(1))
    else
        replace_cc_with = $(join $(dir $(CC)),$(call replace_with,$(notdir $(CC)),$(1)))
    endif
    OBJCOPY = $(call replace_cc_with,objcopy)
    AR = $(call replace_cc_with,ar)
endif

#----------------------------------------------------------------------
# C++ standard library options
#----------------------------------------------------------------------
ifeq (1,$(USE_LIBSTDCPP))
	# Clang requires an extra flag: -stdlib=libstdc++
	ifneq (,$(findstring clang,$(CC)))
		CXXFLAGS += -stdlib=libstdc++ -DLLDB_USING_LIBSTDCPP
		LDFLAGS += -stdlib=libstdc++
	endif
endif

ifeq (1,$(USE_LIBCPP))
	# Clang requires an extra flag: -stdlib=libstdc++
	ifneq (,$(findstring clang,$(CC)))
		ifeq "$(OS)" "Linux"
			# This is the default install location on Ubuntu 14.04
			ifneq ($(wildcard /usr/include/c++/v1/.),)
				CXXFLAGS += -stdlib=libc++ -DLLDB_USING_LIBCPP
				LDFLAGS += -stdlib=libc++
				CXXFLAGS += -I/usr/include/c++/v1
			endif
		else
			CXXFLAGS += -stdlib=libc++ -DLLDB_USING_LIBCPP
			LDFLAGS += -stdlib=libc++
		endif
	endif
endif

#----------------------------------------------------------------------
# dylib settings
#----------------------------------------------------------------------
ifneq "$(strip $(DYLIB_C_SOURCES))" ""
	DYLIB_OBJECTS +=$(strip $(DYLIB_C_SOURCES:.c=.o))
endif

ifneq "$(strip $(DYLIB_OBJC_SOURCES))" ""
	DYLIB_OBJECTS +=$(strip $(DYLIB_OBJC_SOURCES:.m=.o))
endif

ifneq "$(strip $(DYLIB_CXX_SOURCES))" ""
    DYLIB_OBJECTS +=$(strip $(DYLIB_CXX_SOURCES:.cpp=.o))
    CXX = $(call cxx_compiler,$(CC))
    LD = $(call cxx_linker,$(CC))
endif

#----------------------------------------------------------------------
# Check if we have a precompiled header
#----------------------------------------------------------------------
ifneq "$(strip $(PCH_CXX_SOURCE))" ""
    PCH_OUTPUT = $(PCH_CXX_SOURCE:.h=.h.pch)
    PCHFLAGS = -include $(PCH_CXX_SOURCE)
endif

#----------------------------------------------------------------------
# Check if we have any C source files
#----------------------------------------------------------------------
ifneq "$(strip $(C_SOURCES))" ""
	OBJECTS +=$(strip $(C_SOURCES:.c=.o))
endif

#----------------------------------------------------------------------
# Check if we have any C++ source files
#----------------------------------------------------------------------
ifneq "$(strip $(CXX_SOURCES))" ""
	OBJECTS +=$(strip $(CXX_SOURCES:.cpp=.o))
	CXX = $(call cxx_compiler,$(CC))
	LD = $(call cxx_linker,$(CC))
endif

#----------------------------------------------------------------------
# Check if we have any ObjC source files
#----------------------------------------------------------------------
ifneq "$(strip $(OBJC_SOURCES))" ""
	OBJECTS +=$(strip $(OBJC_SOURCES:.m=.o))
	LDFLAGS +=-lobjc
endif

#----------------------------------------------------------------------
# Check if we have any ObjC++ source files
#----------------------------------------------------------------------
ifneq "$(strip $(OBJCXX_SOURCES))" ""
	OBJECTS +=$(strip $(OBJCXX_SOURCES:.mm=.o))
	CXX = $(call cxx_compiler,$(CC))
	LD = $(call cxx_linker,$(CC))
	ifeq "$(findstring lobjc,$(LDFLAGS))" ""
		LDFLAGS +=-lobjc
	endif
endif

#----------------------------------------------------------------------
# Check if we have any C source files for archive
#----------------------------------------------------------------------
ifneq "$(strip $(ARCHIVE_C_SOURCES))" ""
	ARCHIVE_OBJECTS +=$(strip $(ARCHIVE_C_SOURCES:.c=.o))
endif

#----------------------------------------------------------------------
# Check if we have any C++ source files for archive
#----------------------------------------------------------------------
ifneq "$(strip $(ARCHIVE_CXX_SOURCES))" ""
	ARCHIVE_OBJECTS +=$(strip $(ARCHIVE_CXX_SOURCES:.cpp=.o))
	CXX = $(call cxx_compiler,$(CC))
	LD = $(call cxx_linker,$(CC))
endif

#----------------------------------------------------------------------
# Check if we have any ObjC source files for archive
#----------------------------------------------------------------------
ifneq "$(strip $(ARCHIVE_OBJC_SOURCES))" ""
	ARCHIVE_OBJECTS +=$(strip $(ARCHIVE_OBJC_SOURCES:.m=.o))
	LDFLAGS +=-lobjc
endif

#----------------------------------------------------------------------
# Check if we have any ObjC++ source files for archive
#----------------------------------------------------------------------
ifneq "$(strip $(ARCHIVE_OBJCXX_SOURCES))" ""
	ARCHIVE_OBJECTS +=$(strip $(ARCHIVE_OBJCXX_SOURCES:.mm=.o))
	CXX = $(call cxx_compiler,$(CC))
	LD = $(call cxx_linker,$(CC))
	ifeq "$(findstring lobjc,$(LDFLAGS))" ""
		LDFLAGS +=-lobjc
	endif
endif

#----------------------------------------------------------------------
# Check if we are compiling with gcc 4.6
#----------------------------------------------------------------------
ifneq "$(strip $(CXX_SOURCES) $(OBJCXX_SOURCES))" ""
ifneq "$(filter g++,$(CXX))" ""
	CXXVERSION = $(shell $(CXX) -dumpversion | cut -b 1-3)
	ifeq "$(CXXVERSION)" "4.6"
                # GCC 4.6 cannot handle -std=c++11, so replace it with -std=c++0x
                # instead. FIXME: remove once GCC version is upgraded.
		override CXXFLAGS := $(subst -std=c++11,-std=c++0x,$(CXXFLAGS))
	endif
endif
endif

#----------------------------------------------------------------------
# DYLIB_ONLY variable can be used to skip the building of a.out.
# See the sections below regarding dSYM file as well as the building of
# EXE from all the objects.
#----------------------------------------------------------------------

#----------------------------------------------------------------------
# Make the dSYM file from the executable if $(MAKE_DSYM) != "NO"
#----------------------------------------------------------------------
ifneq "$(DYLIB_ONLY)" "YES"
$(DSYM) : $(EXE)
ifeq "$(OS)" "Darwin"
ifneq "$(MAKE_DSYM)" "NO"
	"$(DS)" $(DSFLAGS) -o "$(DSYM)" "$(EXE)"
endif
else
ifeq "$(SPLIT_DEBUG_SYMBOLS)" "YES"
	$(OBJCOPY) --only-keep-debug "$(EXE)" "$(DSYM)"
	$(OBJCOPY) --strip-debug --add-gnu-debuglink="$(DSYM)" "$(EXE)" "$(EXE)"
endif
endif
endif

#----------------------------------------------------------------------
# Compile the executable from all the objects.
#----------------------------------------------------------------------
ifneq "$(DYLIB_NAME)" ""
ifeq "$(DYLIB_ONLY)" ""
$(EXE) : $(OBJECTS) $(ARCHIVE_NAME) $(DYLIB_FILENAME)
	$(LD) $(OBJECTS) $(ARCHIVE_NAME) -L. -l$(DYLIB_NAME) $(LDFLAGS) -o "$(EXE)"
else
EXE = $(DYLIB_FILENAME)
endif
else
$(EXE) : $(OBJECTS) $(ARCHIVE_NAME)
	$(LD) $(OBJECTS) $(LDFLAGS) $(ARCHIVE_NAME) -o "$(EXE)"
endif

#----------------------------------------------------------------------
# Make the archive
#----------------------------------------------------------------------
ifneq "$(ARCHIVE_NAME)" ""
ifeq "$(OS)" "Darwin"
$(ARCHIVE_NAME) : $(ARCHIVE_OBJECTS)
	$(AR) $(ARFLAGS) $(ARCHIVE_NAME) $(ARCHIVE_OBJECTS)
	$(RM) $(ARCHIVE_OBJECTS)
else
$(ARCHIVE_NAME) : $(foreach ar_obj,$(ARCHIVE_OBJECTS),$(ARCHIVE_NAME)($(ar_obj)))
endif
endif

#----------------------------------------------------------------------
# Make the dylib
#----------------------------------------------------------------------
$(DYLIB_OBJECTS) : CFLAGS += -DCOMPILING_LLDB_TEST_DLL

$(DYLIB_FILENAME) : $(DYLIB_OBJECTS)
ifeq "$(OS)" "Darwin"
	$(LD) $(DYLIB_OBJECTS) $(LDFLAGS) -install_name "$(DYLIB_EXECUTABLE_PATH)/$(DYLIB_FILENAME)" -dynamiclib -o "$(DYLIB_FILENAME)"
ifneq "$(MAKE_DSYM)" "NO"
ifneq "$(DS)" ""
	"$(DS)" $(DSFLAGS) "$(DYLIB_FILENAME)"
endif
endif
else
	$(LD) $(DYLIB_OBJECTS) $(LDFLAGS) -shared -o "$(DYLIB_FILENAME)"
ifeq "$(SPLIT_DEBUG_SYMBOLS)" "YES"
	$(OBJCOPY) --only-keep-debug "$(DYLIB_FILENAME)" "$(DYLIB_FILENAME).debug"
	$(OBJCOPY) --strip-debug --add-gnu-debuglink="$(DYLIB_FILENAME).debug" "$(DYLIB_FILENAME)" "$(DYLIB_FILENAME)"
endif
endif

#----------------------------------------------------------------------
# Make the precompiled header and compile C++ sources against it
#----------------------------------------------------------------------

#ifneq "$(PCH_OUTPUT)" ""
$(PCH_OUTPUT) : $(PCH_CXX_SOURCE)
	$(CXX) $(CXXFLAGS) -x c++-header -o $(PCH_OUTPUT) $(PCH_CXX_SOURCE)
%.o : %.cpp $(PCH_OUTPUT)
	$(CXX) $(PCHFLAGS) $(CXXFLAGS) -c -o $@ $<
#endif

#----------------------------------------------------------------------
# Automatic variables based on items already entered. Below we create
# an object's lists from the list of sources by replacing all entries
# that end with .c with .o, and we also create a list of prerequisite
# files by replacing all .c files with .d.
#----------------------------------------------------------------------
PREREQS := $(OBJECTS:.o=.d)
DWOS := $(OBJECTS:.o=.dwo) $(ARCHIVE_OBJECTS:.o=.dwo)
ifneq "$(DYLIB_NAME)" ""
	DYLIB_PREREQS := $(DYLIB_OBJECTS:.o=.d)
	DYLIB_DWOS := $(DYLIB_OBJECTS:.o=.dwo)
endif

#----------------------------------------------------------------------
# Rule for Generating Prerequisites Automatically using .d files and
# the compiler -MM option. The -M option will list all system headers,
# and the -MM option will list all non-system dependencies.
#----------------------------------------------------------------------
ifeq "$(HOST_OS)" "Windows_NT"
	JOIN_CMD = &
	QUOTE = "
else
	JOIN_CMD = ;
	QUOTE = '
endif

%.d: %.c
	@rm -f $@ $(JOIN_CMD) \
	$(CC) -M $(CFLAGS) $< > $@.tmp && \
	sed $(QUOTE)s,\($*\)\.o[ :]*,\1.o $@ : ,g$(QUOTE) < $@.tmp > $@ $(JOIN_CMD) \
	rm -f $@.tmp

%.d: %.cpp
	@rm -f $@ $(JOIN_CMD) \
	$(CXX) -M $(CXXFLAGS) $< > $@.tmp && \
	sed $(QUOTE)s,\($*\)\.o[ :]*,\1.o $@ : ,g$(QUOTE) < $@.tmp > $@ $(JOIN_CMD) \
	rm -f $@.tmp

%.d: %.m
	@rm -f $@ $(JOIN_CMD) \
	$(CC) -M $(CFLAGS) $< > $@.tmp && \
	sed $(QUOTE)s,\($*\)\.o[ :]*,\1.o $@ : ,g$(QUOTE) < $@.tmp > $@ $(JOIN_CMD) \
	rm -f $@.tmp

%.d: %.mm
	@rm -f $@ $(JOIN_CMD) \
	$(CXX) -M $(CXXFLAGS) $< > $@.tmp && \
	sed $(QUOTE)s,\($*\)\.o[ :]*,\1.o $@ : ,g$(QUOTE) < $@.tmp > $@ $(JOIN_CMD) \
	rm -f $@.tmp

#----------------------------------------------------------------------
# Include all of the makefiles for each source file so we don't have
# to manually track all of the prerequisites for each source file.
#----------------------------------------------------------------------
sinclude $(PREREQS)
ifneq "$(DYLIB_NAME)" ""
	sinclude $(DYLIB_PREREQS)
endif

# Define a suffix rule for .mm -> .o
.SUFFIXES: .mm .o
.mm.o:
	$(CXX) $(CXXFLAGS) -c $<

.PHONY: clean
dsym:	$(DSYM)
all:	$(EXE) $(DSYM)
clean::
	$(RM) $(OBJECTS) $(PREREQS) $(PREREQS:.d=.d.tmp) $(DWOS) $(ARCHIVE_NAME) $(ARCHIVE_OBJECTS)
ifneq "$(DYLIB_NAME)" ""
	$(RM) -r $(DYLIB_FILENAME).dSYM
	$(RM) $(DYLIB_OBJECTS) $(DYLIB_PREREQS) $(DYLIB_PREREQS:.d=.d.tmp) $(DYLIB_DWOS) $(DYLIB_FILENAME) $(DYLIB_FILENAME).debug
endif
ifneq "$(PCH_OUTPUT)" ""
	$(RM) $(PCH_OUTPUT)
endif
ifneq "$(DSYM)" ""
	$(RM) -r "$(DSYM)"
endif
ifeq "$(OS)" "Windows_NT"
# http://llvm.org/pr24589
	IF EXIST "$(EXE)" del "$(EXE)"
	$(RM) $(wildcard *.manifest *.pdb *.ilk)
ifneq "$(DYLIB_NAME)" ""
	$(RM) $(DYLIB_NAME).lib $(DYLIB_NAME).exp
endif
else
	$(RM) "$(EXE)"
endif

#----------------------------------------------------------------------
# From http://blog.melski.net/tag/debugging-makefiles/
# 
# Usage: make print-CC print-CXX print-LD
#----------------------------------------------------------------------
print-%:
	@echo '$*=$($*)'
	@echo '  origin = $(origin $*)'
	@echo '  flavor = $(flavor $*)'
	@echo '   value = $(value  $*)'

### Local Variables: ###
### mode:makefile ###
### End: ###
