Makefile.rules 25.3 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782
#----------------------------------------------------------------------
# Clients fill in the source files to build
#----------------------------------------------------------------------
# C_SOURCES := main.c
# CXX_SOURCES :=
# OBJC_SOURCES :=
# OBJCXX_SOURCES :=
# DYLIB_C_SOURCES :=
# DYLIB_OBJC_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
#
# Specifying FRAMEWORK and its variants has the effect of building a NeXT-style
# framework.
# FRAMEWORK := "Foo"
# FRAMEWORK_HEADERS := "Foo.h"
# FRAMEWORK_MODULES := "module.modulemap"
#
# Also might be of interest:
# FRAMEWORK_INCLUDES (Darwin only) :=
# CFLAGS_EXTRAS :=
# LD_EXTRAS :=
# SPLIT_DEBUG_SYMBOLS := YES
# CROSS_COMPILE :=
# USE_PRIVATE_MODULE_CACHE := YES
#
# 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

SRCDIR := $(shell dirname $(firstword $(MAKEFILE_LIST)))
BUILDDIR := $(shell pwd)
MAKEFILE_RULES := $(lastword $(MAKEFILE_LIST))
THIS_FILE_DIR := $(shell dirname $(MAKEFILE_RULES))
LLDB_BASE_DIR := $(THIS_FILE_DIR)/../../../../../

#----------------------------------------------------------------------
# If OS is not defined, use 'uname -s' to determine the OS name.
#
# uname on Windows gives "windows32" or "server version 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)
ifneq (,$(findstring windows32,$(HOST_OS)))
	HOST_OS = Windows_NT
endif
ifeq "$(OS)" ""
	OS = $(HOST_OS)
endif

#----------------------------------------------------------------------
# If OS is Windows, force SHELL to be cmd
#
# Some versions of make on Windows will search for other shells such as
# C:\cygwin\bin\sh.exe. This shell fails for numerous different reasons
# so default to using cmd.exe.
#----------------------------------------------------------------------
ifeq "$(OS)" "Windows_NT"
	SHELL = $(WINDIR)\system32\cmd.exe
endif

#----------------------------------------------------------------------
# If the OS is Windows use double-quotes in commands
#
# For other operating systems, single-quotes work fine, but on Windows
# we strictly required double-quotes
#----------------------------------------------------------------------
ifeq "$(HOST_OS)" "Windows_NT"
	JOIN_CMD = &
	QUOTE = "
	FIXUP_SYNTAX_HIGHLIGHTING_IN_MY_EDITOR = "
else
	JOIN_CMD = ;
	QUOTE = '
	FIXUP_SYNTAX_HIGHLIGHTING_IN_MY_EDITOR = '
endif

#----------------------------------------------------------------------
# If TRIPLE is not defined try to set the ARCH, CC, CFLAGS, and more
# from the triple alone
#----------------------------------------------------------------------
ARCH_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))
	TRIPLE_ENV =$(word 4, $(triple_space))
	ifeq "$(TRIPLE_VENDOR)" "apple"
		ifeq "$(TRIPLE_OS)" "ios"
			CODESIGN := codesign
			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
					ARCH_CFLAGS :=-mios-version-min=$(TRIPLE_VERSION) -isysroot "$(SDKROOT)"
				else
					ifeq "$(TRIPLE_ENV)" "macabi"
						SDKROOT = $(shell xcrun --sdk macosx --show-sdk-path)
					else
						SDKROOT = $(shell xcrun --sdk iphonesimulator --show-sdk-path)
					endif
					ifeq "$(TRIPLE_VERSION)" ""
						TRIPLE_VERSION =$(shell echo $(notdir $(SDKROOT)) | sed -e 's/.*\([0-9]\.[0-9]\).*/\1/')
					endif
					ifeq "$(TRIPLE_ENV)" "macabi"
						ARCH_CFLAGS :=-mios-version-min=$(TRIPLE_VERSION) -isysroot "$(SDKROOT)"
					else
						ARCH_CFLAGS :=-mios-simulator-version-min=$(TRIPLE_VERSION) -isysroot "$(SDKROOT)"
					endif
				endif
			endif
		endif
		ifeq "$(TRIPLE_OS)" "watchos"
			CODESIGN := codesign
			ifeq "$(SDKROOT)" ""
				# Set SDKROOT if it wasn't set
				ifneq (,$(findstring arm,$(ARCH)))
					SDKROOT = $(shell xcrun --sdk watchos --show-sdk-path)
					ifeq "$(TRIPLE_VERSION)" ""
						TRIPLE_VERSION =$(shell echo $(notdir $(SDKROOT)) | sed -e 's/.*\([0-9]\.[0-9]\).*/\1/')
					endif
					ARCH_CFLAGS :=-mwatchos-version-min=$(TRIPLE_VERSION) -isysroot "$(SDKROOT)"
				else
					SDKROOT = $(shell xcrun --sdk watchsimulator --show-sdk-path)
					ifeq "$(TRIPLE_VERSION)" ""
						TRIPLE_VERSION =$(shell echo $(notdir $(SDKROOT)) | sed -e 's/.*\([0-9]\.[0-9]\).*/\1/')
					endif
					ARCH_CFLAGS :=-mwatchos-simulator-version-min=$(TRIPLE_VERSION) -isysroot "$(SDKROOT)"
				endif
			endif
		endif
	endif
endif
ifeq "$(OS)" "Android"
	include $(THIS_FILE_DIR)/Android.rules
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
#----------------------------------------------------------------------
ifeq "$(CC)" ""
$(error "C compiler is not specified. Please run tests through lldb-dotest or lit")
endif

#----------------------------------------------------------------------
# Handle SDKROOT on Darwin
#----------------------------------------------------------------------

ifeq "$(OS)" "Darwin"
    ifeq "$(SDKROOT)" ""
	# We haven't otherwise set the SDKROOT, so set it now to macosx
	SDKROOT := $(shell xcrun --sdk macosx --show-sdk-path)
    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 := $(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)" "powerpc64le"
		override ARCH := $(subst powerpc64le,64,$(ARCH))
	endif
	ifeq "$(ARCH)" "aarch64"
		override ARCH :=
		override ARCHFLAG :=
	endif
	ifeq "$(findstring arm,$(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"
	ifneq "$(SDKROOT)" ""
		CFLAGS += -isysroot "$(SDKROOT)"
	endif
endif

ifeq "$(OS)" "Darwin"
	CFLAGS += $(ARCHFLAG) $(ARCH) $(FRAMEWORK_INCLUDES) -I$(LLDB_BASE_DIR)include
else
	CFLAGS += $(ARCHFLAG)$(ARCH) $(FRAMEWORK_INCLUDES) -I$(LLDB_BASE_DIR)include
endif

CFLAGS += -I$(SRCDIR) -I$(THIS_FILE_DIR)

ifndef NO_TEST_COMMON_H
  CFLAGS += -include $(THIS_FILE_DIR)/test_common.h
endif

CFLAGS += $(NO_LIMIT_DEBUG_INFO_FLAGS) $(ARCH_CFLAGS) $(CFLAGS_EXTRAS)

# If the OS is Windows, we need to pass -gdwarf to clang, otherwise it will build
# with codeview by default but all the tests rely on dwarf.
ifeq "$(OS)" "Windows_NT"
	CFLAGS += -gdwarf
endif

# 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) $(ARCH_CFLAGS) $(CFLAGS_EXTRAS)
else
	CFLAGS_NO_DEBUG = -O0 $(ARCHFLAG)$(ARCH) $(FRAMEWORK_INCLUDES) $(ARCH_CFLAGS) $(CFLAGS_EXTRAS)
endif

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

ifeq "$(USE_PRIVATE_MODULE_CACHE)" "YES"
THE_CLANG_MODULE_CACHE_DIR := $(BUILDDIR)/private-module-cache
else
THE_CLANG_MODULE_CACHE_DIR := $(CLANG_MODULE_CACHE_DIR)
endif

MODULE_BASE_FLAGS := -fmodules -gmodules -fmodules-cache-path=$(THE_CLANG_MODULE_CACHE_DIR)
MANDATORY_MODULE_BUILD_CFLAGS := $(MODULE_BASE_FLAGS) -gmodules
# Build flags for building with C++ modules.
# -glldb is necessary for emitting information about what modules were imported.
MANDATORY_CXXMODULE_BUILD_CFLAGS := $(MODULE_BASE_FLAGS) -fcxx-modules -glldb

ifeq "$(OS)" "Darwin"
	MANDATORY_MODULE_BUILD_CFLAGS += -fcxx-modules
endif

ifeq "$(MAKE_GMODULES)" "YES"
	CFLAGS += $(MANDATORY_MODULE_BUILD_CFLAGS)
	CXXFLAGS += $(MANDATORY_MODULE_BUILD_CFLAGS)
endif

CXXFLAGS += -std=c++11 $(CFLAGS) $(ARCH_CXXFLAGS) $(CXXFLAGS_EXTRAS)
LD = $(CC)
LDFLAGS ?= $(CFLAGS)
LDFLAGS += $(LD_EXTRAS) $(ARCH_LDFLAGS)
ifneq (,$(LLVM_LIBS_DIR))
	ifeq ($(OS),NetBSD)
		LDFLAGS += -L$(LLVM_LIBS_DIR) -Wl,-rpath,$(LLVM_LIBS_DIR)
	endif
endif
ifeq (,$(filter $(OS), Windows_NT Android Darwin))
	ifneq (,$(filter YES,$(ENABLE_THREADS)))
		LDFLAGS += -pthread
	endif
endif
OBJECTS =
EXE ?= a.out

ifneq "$(FRAMEWORK)" ""
	DYLIB_NAME ?= $(FRAMEWORK).framework/Versions/A/$(FRAMEWORK)
	DYLIB_FILENAME ?= $(FRAMEWORK).framework/Versions/A/$(FRAMEWORK)
endif

ifneq "$(DYLIB_NAME)" ""
	ifeq "$(OS)" "Darwin"
		ifneq "$(FRAMEWORK)" ""
			DYLIB_INSTALL_NAME ?= @executable_path/$(FRAMEWORK).framework/Versions/A/$(FRAMEWORK)
		else
			DYLIB_FILENAME = lib$(DYLIB_NAME).dylib
			DYLIB_INSTALL_NAME ?= @executable_path/$(DYLIB_FILENAME)
		endif
	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 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 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)))

ifneq "$(OS)" "Darwin"
	CLANG_OR_GCC := $(strip $(if $(findstring clang,$(CC)), \
				$(findstring clang,$(CC)), \
				$(if $(findstring gcc,$(CC)), \
					$(findstring gcc,$(CC)), \
					cc)))

	CC_LASTWORD := $(strip $(lastword $(subst -, ,$(CC))))

	replace_with = $(strip $(if $(findstring $(3),$(CC_LASTWORD)), \
			$(subst $(3),$(1),$(2)), \
			$(subst $(3),$(1),$(subst -$(CC_LASTWORD),,$(2)))))

	ifeq "$(notdir $(CC))" "$(CC)"
		replace_cc_with = $(call replace_with,$(1),$(CC),$(CLANG_OR_GCC))
	else
		replace_cc_with = $(join $(dir $(CC)),$(call replace_with,$(1),$(notdir $(CC)),$(CLANG_OR_GCC)))
	endif

	OBJCOPY ?= $(call replace_cc_with,objcopy)
	ARCHIVER ?= $(call replace_cc_with,ar)
	override AR = $(ARCHIVER)
endif

ifdef PIE
	LDFLAGS += -pie
endif

#----------------------------------------------------------------------
# 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

		# MSVC 2015 or higher is required, which depends on c++14, so
		# append these values unconditionally.
		CXXFLAGS += -fms-compatibility-version=19.0
		override CXXFLAGS := $(subst -std=c++11,-std=c++14,$(CXXFLAGS))

		# The MSVC linker doesn't understand long section names
		# generated by the clang compiler.
		LDFLAGS += -fuse-ld=lld
	endif
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))
	CXXFLAGS += -DLLDB_USING_LIBCPP
	ifeq "$(OS)" "Linux"
		ifneq (,$(findstring clang,$(CC)))
			CXXFLAGS += -stdlib=libc++
			LDFLAGS += -stdlib=libc++
		else
			CXXFLAGS += -isystem /usr/include/c++/v1
			LDFLAGS += -lc++
		endif
	else ifeq "$(OS)" "Android"
		# Nothing to do, this is already handled in
		# Android.rules.
	else
		CXXFLAGS += -stdlib=libc++
		LDFLAGS += -stdlib=libc++
	endif
endif

#----------------------------------------------------------------------
# Additional system libraries
#----------------------------------------------------------------------
ifeq (1,$(USE_LIBDL))
	ifneq ($(OS),NetBSD)
		LDFLAGS += -ldl
	endif
endif

#----------------------------------------------------------------------
# dylib settings
#----------------------------------------------------------------------

DYLIB_OBJECTS +=$(strip $(DYLIB_C_SOURCES:.c=.o))
DYLIB_OBJECTS +=$(strip $(DYLIB_OBJC_SOURCES:.m=.o))
ifneq "$(strip $(DYLIB_CXX_SOURCES))" ""
	DYLIB_OBJECTS +=$(strip $(patsubst %.mm, %.o, $(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

ifeq ($(findstring clang, $(CXX)), clang)
	CXXFLAGS += --driver-mode=g++
endif

ifneq "$(CXX)" ""
	ifeq ($(findstring clang, $(LD)), clang)
		LDFLAGS += --driver-mode=g++
	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.
#----------------------------------------------------------------------

#----------------------------------------------------------------------
# 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)"
ifneq "$(CODESIGN)" ""
	$(CODESIGN) -s - "$(EXE)"
endif
else
EXE = $(DYLIB_FILENAME)
endif
else
$(EXE) : $(OBJECTS) $(ARCHIVE_NAME)
	$(LD) $(OBJECTS) $(LDFLAGS) $(ARCHIVE_NAME) -o "$(EXE)"
ifneq "$(CODESIGN)" ""
	$(CODESIGN) -s - "$(EXE)"
endif
endif

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

ifneq "$(OS)" "Windows_NT"
$(DYLIB_OBJECTS) : CFLAGS += -fPIC
$(DYLIB_OBJECTS) : CXXFLAGS += -fPIC
endif

$(DYLIB_FILENAME) : $(DYLIB_OBJECTS)
ifeq "$(OS)" "Darwin"
ifneq "$(FRAMEWORK)" ""
	mkdir -p $(FRAMEWORK).framework/Versions/A/Headers
	mkdir -p $(FRAMEWORK).framework/Versions/A/Modules
	mkdir -p $(FRAMEWORK).framework/Versions/A/Resources
ifneq "$(FRAMEWORK_MODULES)" ""
	cp -r $(FRAMEWORK_MODULES) $(FRAMEWORK).framework/Versions/A/Modules
endif
ifneq "$(FRAMEWORK_HEADERS)" ""
	cp -r $(FRAMEWORK_HEADERS) $(FRAMEWORK).framework/Versions/A/Headers
endif
	(cd $(FRAMEWORK).framework/Versions; ln -sf A Current)
	(cd $(FRAMEWORK).framework/; ln -sf Versions/A/Headers Headers)
	(cd $(FRAMEWORK).framework/; ln -sf Versions/A/Modules Modules)
	(cd $(FRAMEWORK).framework/; ln -sf Versions/A/Resources Resources)
	(cd $(FRAMEWORK).framework/; ln -sf Versions/A/$(FRAMEWORK) $(FRAMEWORK))
endif
	$(LD) $(DYLIB_OBJECTS) $(LDFLAGS) -install_name "$(DYLIB_INSTALL_NAME)" -dynamiclib -o "$(DYLIB_FILENAME)"
ifneq "$(CODESIGN)" ""
	$(CODESIGN) -s - "$(DYLIB_FILENAME)"
endif
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 $@ $<
%.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.
#----------------------------------------------------------------------
%.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::
ifeq "$(findstring lldb-test-build.noindex, $(BUILDDIR))" ""
	$(error Trying to invoke the clean rule, but not using the default build tree layout)
else
	$(RM) -r $(wildcard $(BUILDDIR)/*)
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: ###