1#----------------------------------------------------------------------
2# Clients fill in the source files to build
3#----------------------------------------------------------------------
4# C_SOURCES := main.c
5# CXX_SOURCES :=
6# OBJC_SOURCES :=
7# OBJCXX_SOURCES :=
8# DYLIB_C_SOURCES :=
9# DYLIB_OBJC_SOURCES :=
10# DYLIB_CXX_SOURCES :=
11#
12# Specifying DYLIB_ONLY has the effect of building dylib only, skipping
13# the building of the a.out executable program.  For example,
14# DYLIB_ONLY := YES
15#
16# Specifying FRAMEWORK and its variants has the effect of building a NeXT-style
17# framework.
18# FRAMEWORK := "Foo"
19# FRAMEWORK_HEADERS := "Foo.h"
20# FRAMEWORK_MODULES := "module.modulemap"
21#
22# Also might be of interest:
23# FRAMEWORK_INCLUDES (Darwin only) :=
24# CFLAGS_EXTRAS :=
25# LD_EXTRAS :=
26# SPLIT_DEBUG_SYMBOLS := YES
27# CROSS_COMPILE :=
28# USE_PRIVATE_MODULE_CACHE := YES
29#
30# And test/functionalities/archives/Makefile:
31# MAKE_DSYM := NO
32# ARCHIVE_NAME := libfoo.a
33# ARCHIVE_C_SOURCES := a.c b.c
34
35# Uncomment line below for debugging shell commands
36# SHELL = /bin/sh -x
37
38SRCDIR := $(shell dirname $(firstword $(MAKEFILE_LIST)))
39BUILDDIR := $(shell pwd)
40MAKEFILE_RULES := $(lastword $(MAKEFILE_LIST))
41THIS_FILE_DIR := $(shell dirname $(MAKEFILE_RULES))
42LLDB_BASE_DIR := $(THIS_FILE_DIR)/../../../../../
43
44# The test harness invokes the test Makefiles with an explicit 'all'
45# target, but its handy to be able to recursively call this Makefile
46# without specifying a goal. You almost certainly want to build 'all',
47# and not only the first target defined in this file (which might vary
48# according to variable values).
49.DEFAULT_GOAL := all
50
51#----------------------------------------------------------------------
52# If OS is not defined, use 'uname -s' to determine the OS name.
53#
54# uname on Windows gives "windows32" or "server version windows32", but most
55# environments standardize on "Windows_NT", so we'll make it consistent here.
56# When running tests from Visual Studio, the environment variable isn't
57# inherited all the way down to the process spawned for make.
58#----------------------------------------------------------------------
59HOST_OS = $(shell uname -s)
60ifneq (,$(findstring windows32,$(HOST_OS)))
61	HOST_OS = Windows_NT
62endif
63ifeq "$(OS)" ""
64	OS = $(HOST_OS)
65endif
66
67#----------------------------------------------------------------------
68# If OS is Windows, force SHELL to be cmd
69#
70# Some versions of make on Windows will search for other shells such as
71# C:\cygwin\bin\sh.exe. This shell fails for numerous different reasons
72# so default to using cmd.exe.
73#----------------------------------------------------------------------
74ifeq "$(OS)" "Windows_NT"
75	SHELL = $(WINDIR)\system32\cmd.exe
76endif
77
78#----------------------------------------------------------------------
79# If the OS is Windows use double-quotes in commands
80#
81# For other operating systems, single-quotes work fine, but on Windows
82# we strictly required double-quotes
83#----------------------------------------------------------------------
84ifeq "$(HOST_OS)" "Windows_NT"
85	JOIN_CMD = &
86	QUOTE = "
87	FIXUP_SYNTAX_HIGHLIGHTING_IN_MY_EDITOR = "
88else
89	JOIN_CMD = ;
90	QUOTE = '
91	FIXUP_SYNTAX_HIGHLIGHTING_IN_MY_EDITOR = '
92endif
93
94#----------------------------------------------------------------------
95# If TRIPLE is not defined try to set the ARCH, CC, CFLAGS, and more
96# from the triple alone
97#----------------------------------------------------------------------
98ARCH_CFLAGS :=
99ifneq "$(TRIPLE)" ""
100	triple_space = $(subst -, ,$(TRIPLE))
101	ARCH =$(word 1, $(triple_space))
102	TRIPLE_VENDOR =$(word 2, $(triple_space))
103	triple_os_and_version =$(shell echo $(word 3, $(triple_space)) | sed 's/\([a-z]*\)\(.*\)/\1 \2/')
104	TRIPLE_OS =$(word 1, $(triple_os_and_version))
105	TRIPLE_VERSION =$(word 2, $(triple_os_and_version))
106	TRIPLE_ENV =$(word 4, $(triple_space))
107	ifeq "$(TRIPLE_VENDOR)" "apple"
108		ifeq "$(TRIPLE_OS)" "ios"
109			CODESIGN := codesign
110			ifeq "$(SDKROOT)" ""
111				# Set SDKROOT if it wasn't set
112				ifneq (,$(findstring arm,$(ARCH)))
113					SDKROOT = $(shell xcrun --sdk iphoneos --show-sdk-path)
114					ifeq "$(TRIPLE_VERSION)" ""
115						TRIPLE_VERSION =$(shell echo $(notdir $(SDKROOT)) | sed -e 's/.*\([0-9]\.[0-9]\).*/\1/')
116					endif
117					ARCH_CFLAGS :=-mios-version-min=$(TRIPLE_VERSION) -isysroot "$(SDKROOT)"
118				else
119					ifeq "$(TRIPLE_ENV)" "macabi"
120						SDKROOT = $(shell xcrun --sdk macosx --show-sdk-path)
121					else
122						SDKROOT = $(shell xcrun --sdk iphonesimulator --show-sdk-path)
123					endif
124					ifeq "$(TRIPLE_VERSION)" ""
125						TRIPLE_VERSION =$(shell echo $(notdir $(SDKROOT)) | sed -e 's/.*\([0-9]\.[0-9]\).*/\1/')
126					endif
127					ifeq "$(TRIPLE_ENV)" "macabi"
128						ARCH_CFLAGS :=-mios-version-min=$(TRIPLE_VERSION) -isysroot "$(SDKROOT)"
129					else
130						ARCH_CFLAGS :=-mios-simulator-version-min=$(TRIPLE_VERSION) -isysroot "$(SDKROOT)"
131					endif
132				endif
133			endif
134		endif
135		ifeq "$(TRIPLE_OS)" "watchos"
136			CODESIGN := codesign
137			ifeq "$(SDKROOT)" ""
138				# Set SDKROOT if it wasn't set
139				ifneq (,$(findstring arm,$(ARCH)))
140					SDKROOT = $(shell xcrun --sdk watchos --show-sdk-path)
141					ifeq "$(TRIPLE_VERSION)" ""
142						TRIPLE_VERSION =$(shell echo $(notdir $(SDKROOT)) | sed -e 's/.*\([0-9]\.[0-9]\).*/\1/')
143					endif
144					ARCH_CFLAGS :=-mwatchos-version-min=$(TRIPLE_VERSION) -isysroot "$(SDKROOT)"
145				else
146					SDKROOT = $(shell xcrun --sdk watchsimulator --show-sdk-path)
147					ifeq "$(TRIPLE_VERSION)" ""
148						TRIPLE_VERSION =$(shell echo $(notdir $(SDKROOT)) | sed -e 's/.*\([0-9]\.[0-9]\).*/\1/')
149					endif
150					ARCH_CFLAGS :=-mwatchos-simulator-version-min=$(TRIPLE_VERSION) -isysroot "$(SDKROOT)"
151				endif
152			endif
153		endif
154	endif
155endif
156ifeq "$(OS)" "Android"
157	include $(THIS_FILE_DIR)/Android.rules
158endif
159
160#----------------------------------------------------------------------
161# If ARCH is not defined, default to x86_64.
162#----------------------------------------------------------------------
163ifeq "$(ARCH)" ""
164ifeq "$(OS)" "Windows_NT"
165	ARCH = x86
166else
167	ARCH = x86_64
168endif
169endif
170
171#----------------------------------------------------------------------
172# CC defaults to clang.
173#
174# If you change the defaults of CC, be sure to also change it in the file
175# test/plugins/builder_base.py, which provides a Python way to return the
176# value of the make variable CC -- getCompiler().
177#
178# See also these functions:
179#   o cxx_compiler
180#   o cxx_linker
181#----------------------------------------------------------------------
182ifeq "$(CC)" ""
183$(error "C compiler is not specified. Please run tests through lldb-dotest or lit")
184endif
185
186#----------------------------------------------------------------------
187# Handle SDKROOT on Darwin
188#----------------------------------------------------------------------
189
190ifeq "$(OS)" "Darwin"
191    ifeq "$(SDKROOT)" ""
192	# We haven't otherwise set the SDKROOT, so set it now to macosx
193	SDKROOT := $(shell xcrun --sdk macosx --show-sdk-path)
194    endif
195endif
196
197#----------------------------------------------------------------------
198# ARCHFLAG is the flag used to tell the compiler which architecture
199# to compile for. The default is the flag that clang accepts.
200#----------------------------------------------------------------------
201ARCHFLAG ?= -arch
202
203#----------------------------------------------------------------------
204# Change any build/tool options needed
205#----------------------------------------------------------------------
206ifeq "$(OS)" "Darwin"
207	DS := $(DSYMUTIL)
208	DSFLAGS =
209	DSYM = $(EXE).dSYM
210	AR := $(CROSS_COMPILE)libtool
211	ARFLAGS := -static -o
212else
213	AR := $(CROSS_COMPILE)ar
214	# On non-Apple platforms, -arch becomes -m
215	ARCHFLAG := -m
216
217	# i386, i686, x86 -> 32
218	# amd64, x86_64, x64 -> 64
219	ifeq "$(ARCH)" "amd64"
220		override ARCH := $(subst amd64,64,$(ARCH))
221	endif
222	ifeq "$(ARCH)" "x86_64"
223		override ARCH := $(subst x86_64,64,$(ARCH))
224	endif
225	ifeq "$(ARCH)" "x64"
226		override ARCH := $(subst x64,64,$(ARCH))
227	endif
228	ifeq "$(ARCH)" "x86"
229		override ARCH := $(subst x86,32,$(ARCH))
230	endif
231	ifeq "$(ARCH)" "i386"
232		override ARCH := $(subst i386,32,$(ARCH))
233	endif
234	ifeq "$(ARCH)" "i686"
235		override ARCH := $(subst i686,32,$(ARCH))
236	endif
237	ifeq "$(ARCH)" "powerpc"
238		override ARCH := $(subst powerpc,32,$(ARCH))
239	endif
240	ifeq "$(ARCH)" "powerpc64"
241		override ARCH := $(subst powerpc64,64,$(ARCH))
242	endif
243	ifeq "$(ARCH)" "powerpc64le"
244		override ARCH := $(subst powerpc64le,64,$(ARCH))
245	endif
246	ifeq "$(ARCH)" "aarch64"
247		override ARCH :=
248		override ARCHFLAG :=
249	endif
250	ifeq "$(findstring arm,$(ARCH))" "arm"
251		override ARCH :=
252		override ARCHFLAG :=
253	endif
254	ifeq "$(ARCH)" "s390x"
255		override ARCH :=
256		override ARCHFLAG :=
257	endif
258	ifeq "$(findstring mips,$(ARCH))" "mips"
259		override ARCHFLAG := -
260	endif
261
262	ifeq "$(SPLIT_DEBUG_SYMBOLS)" "YES"
263		DSYM = $(EXE).debug
264	endif
265endif
266
267LIMIT_DEBUG_INFO_FLAGS =
268NO_LIMIT_DEBUG_INFO_FLAGS =
269MODULE_DEBUG_INFO_FLAGS =
270ifneq (,$(findstring clang,$(CC)))
271   LIMIT_DEBUG_INFO_FLAGS += -flimit-debug-info
272   NO_LIMIT_DEBUG_INFO_FLAGS += -fno-limit-debug-info
273   MODULE_DEBUG_INFO_FLAGS += -gmodules
274endif
275
276DEBUG_INFO_FLAG ?= -g
277
278CFLAGS ?= $(DEBUG_INFO_FLAG) -O0 -fno-builtin
279
280ifeq "$(OS)" "Darwin"
281	ifneq "$(SDKROOT)" ""
282		CFLAGS += -isysroot "$(SDKROOT)"
283	endif
284endif
285
286ifeq "$(OS)" "Darwin"
287	CFLAGS += $(ARCHFLAG) $(ARCH) $(FRAMEWORK_INCLUDES) -I$(LLDB_BASE_DIR)include
288else
289	CFLAGS += $(ARCHFLAG)$(ARCH) $(FRAMEWORK_INCLUDES) -I$(LLDB_BASE_DIR)include
290endif
291
292CFLAGS += -I$(SRCDIR) -I$(THIS_FILE_DIR)
293
294ifndef NO_TEST_COMMON_H
295  CFLAGS += -include $(THIS_FILE_DIR)/test_common.h
296endif
297
298CFLAGS += $(NO_LIMIT_DEBUG_INFO_FLAGS) $(ARCH_CFLAGS) $(CFLAGS_EXTRAS)
299
300# If the OS is Windows, we need to pass -gdwarf to clang, otherwise it will build
301# with codeview by default but all the tests rely on dwarf.
302ifeq "$(OS)" "Windows_NT"
303	CFLAGS += -gdwarf
304endif
305
306# Use this one if you want to build one part of the result without debug information:
307ifeq "$(OS)" "Darwin"
308	CFLAGS_NO_DEBUG = -O0 $(ARCHFLAG) $(ARCH) $(FRAMEWORK_INCLUDES) $(ARCH_CFLAGS) $(CFLAGS_EXTRAS)
309else
310	CFLAGS_NO_DEBUG = -O0 $(ARCHFLAG)$(ARCH) $(FRAMEWORK_INCLUDES) $(ARCH_CFLAGS) $(CFLAGS_EXTRAS)
311endif
312
313ifeq "$(MAKE_DWO)" "YES"
314	CFLAGS += -gsplit-dwarf
315endif
316
317ifeq "$(USE_PRIVATE_MODULE_CACHE)" "YES"
318THE_CLANG_MODULE_CACHE_DIR := $(BUILDDIR)/private-module-cache
319else
320THE_CLANG_MODULE_CACHE_DIR := $(CLANG_MODULE_CACHE_DIR)
321endif
322
323MODULE_BASE_FLAGS := -fmodules -gmodules -fmodules-cache-path=$(THE_CLANG_MODULE_CACHE_DIR)
324MANDATORY_MODULE_BUILD_CFLAGS := $(MODULE_BASE_FLAGS) -gmodules
325# Build flags for building with C++ modules.
326# -glldb is necessary for emitting information about what modules were imported.
327MANDATORY_CXXMODULE_BUILD_CFLAGS := $(MODULE_BASE_FLAGS) -fcxx-modules -glldb
328
329ifeq "$(OS)" "Darwin"
330	MANDATORY_MODULE_BUILD_CFLAGS += -fcxx-modules
331endif
332
333ifeq "$(MAKE_GMODULES)" "YES"
334	CFLAGS += $(MANDATORY_MODULE_BUILD_CFLAGS)
335	CXXFLAGS += $(MANDATORY_MODULE_BUILD_CFLAGS)
336endif
337
338CXXFLAGS += -std=c++11 $(CFLAGS) $(ARCH_CXXFLAGS) $(CXXFLAGS_EXTRAS)
339LD = $(CC)
340LDFLAGS ?= $(CFLAGS)
341LDFLAGS += $(LD_EXTRAS) $(ARCH_LDFLAGS)
342ifneq (,$(LLVM_LIBS_DIR))
343	ifeq ($(OS),NetBSD)
344		LDFLAGS += -L$(LLVM_LIBS_DIR) -Wl,-rpath,$(LLVM_LIBS_DIR)
345	endif
346endif
347ifeq (,$(filter $(OS), Windows_NT Android Darwin))
348	ifneq (,$(filter YES,$(ENABLE_THREADS)))
349		LDFLAGS += -pthread
350	endif
351endif
352OBJECTS =
353EXE ?= a.out
354
355ifneq "$(FRAMEWORK)" ""
356	DYLIB_NAME ?= $(FRAMEWORK).framework/Versions/A/$(FRAMEWORK)
357	DYLIB_FILENAME ?= $(FRAMEWORK).framework/Versions/A/$(FRAMEWORK)
358endif
359
360ifneq "$(DYLIB_NAME)" ""
361	ifeq "$(OS)" "Darwin"
362		ifneq "$(FRAMEWORK)" ""
363			DYLIB_INSTALL_NAME ?= @executable_path/$(FRAMEWORK).framework/Versions/A/$(FRAMEWORK)
364		else
365			DYLIB_FILENAME = lib$(DYLIB_NAME).dylib
366			DYLIB_INSTALL_NAME ?= @executable_path/$(DYLIB_FILENAME)
367		endif
368	else ifeq "$(OS)" "Windows_NT"
369		DYLIB_FILENAME = $(DYLIB_NAME).dll
370	else
371		DYLIB_FILENAME = lib$(DYLIB_NAME).so
372	endif
373endif
374
375# Function that returns the counterpart C++ compiler, given $(CC) as arg.
376cxx_compiler_notdir = $(if $(findstring icc,$(1)), \
377			$(subst icc,icpc,$(1)), \
378			$(if $(findstring llvm-gcc,$(1)), \
379				$(subst llvm-gcc,llvm-g++,$(1)), \
380				$(if $(findstring gcc,$(1)), \
381					$(subst gcc,g++,$(1)), \
382					$(subst cc,c++,$(1)))))
383cxx_compiler = $(if $(findstring /,$(1)),$(join $(dir $(1)), $(call cxx_compiler_notdir,$(notdir $(1)))),$(call cxx_compiler_notdir,$(1)))
384
385# Function that returns the C++ linker, given $(CC) as arg.
386cxx_linker_notdir = $(if $(findstring icc,$(1)), \
387			$(subst icc,icpc,$(1)), \
388			$(if $(findstring llvm-gcc,$(1)), \
389				$(subst llvm-gcc,llvm-g++,$(1)), \
390				$(if $(findstring gcc,$(1)), \
391					$(subst gcc,g++,$(1)), \
392					$(subst cc,c++,$(1)))))
393cxx_linker = $(if $(findstring /,$(1)),$(join $(dir $(1)), $(call cxx_linker_notdir,$(notdir $(1)))),$(call cxx_linker_notdir,$(1)))
394
395ifneq "$(OS)" "Darwin"
396	CLANG_OR_GCC := $(strip $(if $(findstring clang,$(CC)), \
397				$(findstring clang,$(CC)), \
398				$(if $(findstring gcc,$(CC)), \
399					$(findstring gcc,$(CC)), \
400					cc)))
401
402	CC_LASTWORD := $(strip $(lastword $(subst -, ,$(CC))))
403
404	replace_with = $(strip $(if $(findstring $(3),$(CC_LASTWORD)), \
405			$(subst $(3),$(1),$(2)), \
406			$(subst $(3),$(1),$(subst -$(CC_LASTWORD),,$(2)))))
407
408	ifeq "$(notdir $(CC))" "$(CC)"
409		replace_cc_with = $(call replace_with,$(1),$(CC),$(CLANG_OR_GCC))
410	else
411		replace_cc_with = $(join $(dir $(CC)),$(call replace_with,$(1),$(notdir $(CC)),$(CLANG_OR_GCC)))
412	endif
413
414	OBJCOPY ?= $(call replace_cc_with,objcopy)
415	ARCHIVER ?= $(call replace_cc_with,ar)
416	override AR = $(ARCHIVER)
417endif
418
419ifdef PIE
420	LDFLAGS += -pie
421endif
422
423#----------------------------------------------------------------------
424# Windows specific options
425#----------------------------------------------------------------------
426ifeq "$(OS)" "Windows_NT"
427	ifneq (,$(findstring clang,$(CC)))
428		# Clang for Windows doesn't support C++ Exceptions
429		CXXFLAGS += -fno-exceptions
430		CXXFLAGS += -D_HAS_EXCEPTIONS=0
431
432		# MSVC 2015 or higher is required, which depends on c++14, so
433		# append these values unconditionally.
434		CXXFLAGS += -fms-compatibility-version=19.0
435		override CXXFLAGS := $(subst -std=c++11,-std=c++14,$(CXXFLAGS))
436
437		# The MSVC linker doesn't understand long section names
438		# generated by the clang compiler.
439		LDFLAGS += -fuse-ld=lld
440	endif
441endif
442
443#----------------------------------------------------------------------
444# C++ standard library options
445#----------------------------------------------------------------------
446ifeq (1,$(USE_LIBSTDCPP))
447	# Clang requires an extra flag: -stdlib=libstdc++
448	ifneq (,$(findstring clang,$(CC)))
449		CXXFLAGS += -stdlib=libstdc++ -DLLDB_USING_LIBSTDCPP
450		LDFLAGS += -stdlib=libstdc++
451	endif
452endif
453
454ifeq (1,$(USE_LIBCPP))
455	CXXFLAGS += -DLLDB_USING_LIBCPP
456	ifeq "$(OS)" "Linux"
457		ifneq (,$(findstring clang,$(CC)))
458			CXXFLAGS += -stdlib=libc++
459			LDFLAGS += -stdlib=libc++
460		else
461			CXXFLAGS += -isystem /usr/include/c++/v1
462			LDFLAGS += -lc++
463		endif
464	else ifeq "$(OS)" "Android"
465		# Nothing to do, this is already handled in
466		# Android.rules.
467	else
468		CXXFLAGS += -stdlib=libc++
469		LDFLAGS += -stdlib=libc++
470	endif
471endif
472
473#----------------------------------------------------------------------
474# Additional system libraries
475#----------------------------------------------------------------------
476ifeq (1,$(USE_LIBDL))
477	ifeq (,$(filter $(OS), NetBSD Windows_NT))
478		LDFLAGS += -ldl
479	endif
480endif
481
482#----------------------------------------------------------------------
483# dylib settings
484#----------------------------------------------------------------------
485
486DYLIB_OBJECTS +=$(strip $(DYLIB_C_SOURCES:.c=.o))
487DYLIB_OBJECTS +=$(strip $(DYLIB_OBJC_SOURCES:.m=.o))
488ifneq "$(strip $(DYLIB_CXX_SOURCES))" ""
489	DYLIB_OBJECTS +=$(strip $(patsubst %.mm, %.o, $(DYLIB_CXX_SOURCES:.cpp=.o)))
490	CXX = $(call cxx_compiler,$(CC))
491	LD = $(call cxx_linker,$(CC))
492endif
493
494#----------------------------------------------------------------------
495# Check if we have a precompiled header
496#----------------------------------------------------------------------
497ifneq "$(strip $(PCH_CXX_SOURCE))" ""
498	PCH_OUTPUT = $(PCH_CXX_SOURCE:.h=.h.pch)
499	PCHFLAGS = -include $(PCH_CXX_SOURCE)
500endif
501
502#----------------------------------------------------------------------
503# Check if we have any C source files
504#----------------------------------------------------------------------
505ifneq "$(strip $(C_SOURCES))" ""
506	OBJECTS +=$(strip $(C_SOURCES:.c=.o))
507endif
508
509#----------------------------------------------------------------------
510# Check if we have any C++ source files
511#----------------------------------------------------------------------
512ifneq "$(strip $(CXX_SOURCES))" ""
513	OBJECTS +=$(strip $(CXX_SOURCES:.cpp=.o))
514	CXX = $(call cxx_compiler,$(CC))
515	LD = $(call cxx_linker,$(CC))
516endif
517
518#----------------------------------------------------------------------
519# Check if we have any ObjC source files
520#----------------------------------------------------------------------
521ifneq "$(strip $(OBJC_SOURCES))" ""
522	OBJECTS +=$(strip $(OBJC_SOURCES:.m=.o))
523	LDFLAGS +=-lobjc
524endif
525
526#----------------------------------------------------------------------
527# Check if we have any ObjC++ source files
528#----------------------------------------------------------------------
529ifneq "$(strip $(OBJCXX_SOURCES))" ""
530	OBJECTS +=$(strip $(OBJCXX_SOURCES:.mm=.o))
531	CXX = $(call cxx_compiler,$(CC))
532	LD = $(call cxx_linker,$(CC))
533	ifeq "$(findstring lobjc,$(LDFLAGS))" ""
534		LDFLAGS +=-lobjc
535	endif
536endif
537
538#----------------------------------------------------------------------
539# Check if we have any C source files for archive
540#----------------------------------------------------------------------
541ifneq "$(strip $(ARCHIVE_C_SOURCES))" ""
542	ARCHIVE_OBJECTS +=$(strip $(ARCHIVE_C_SOURCES:.c=.o))
543endif
544
545#----------------------------------------------------------------------
546# Check if we have any C++ source files for archive
547#----------------------------------------------------------------------
548ifneq "$(strip $(ARCHIVE_CXX_SOURCES))" ""
549	ARCHIVE_OBJECTS +=$(strip $(ARCHIVE_CXX_SOURCES:.cpp=.o))
550	CXX = $(call cxx_compiler,$(CC))
551	LD = $(call cxx_linker,$(CC))
552endif
553
554#----------------------------------------------------------------------
555# Check if we have any ObjC source files for archive
556#----------------------------------------------------------------------
557ifneq "$(strip $(ARCHIVE_OBJC_SOURCES))" ""
558	ARCHIVE_OBJECTS +=$(strip $(ARCHIVE_OBJC_SOURCES:.m=.o))
559	LDFLAGS +=-lobjc
560endif
561
562#----------------------------------------------------------------------
563# Check if we have any ObjC++ source files for archive
564#----------------------------------------------------------------------
565ifneq "$(strip $(ARCHIVE_OBJCXX_SOURCES))" ""
566	ARCHIVE_OBJECTS +=$(strip $(ARCHIVE_OBJCXX_SOURCES:.mm=.o))
567	CXX = $(call cxx_compiler,$(CC))
568	LD = $(call cxx_linker,$(CC))
569	ifeq "$(findstring lobjc,$(LDFLAGS))" ""
570		LDFLAGS +=-lobjc
571	endif
572endif
573
574#----------------------------------------------------------------------
575# Check if we are compiling with gcc 4.6
576#----------------------------------------------------------------------
577ifneq "$(strip $(CXX_SOURCES) $(OBJCXX_SOURCES))" ""
578ifneq "$(filter g++,$(CXX))" ""
579	CXXVERSION = $(shell $(CXX) -dumpversion | cut -b 1-3)
580	ifeq "$(CXXVERSION)" "4.6"
581		# GCC 4.6 cannot handle -std=c++11, so replace it with -std=c++0x
582		# instead. FIXME: remove once GCC version is upgraded.
583		override CXXFLAGS := $(subst -std=c++11,-std=c++0x,$(CXXFLAGS))
584	endif
585endif
586endif
587
588ifeq ($(findstring clang, $(CXX)), clang)
589	CXXFLAGS += --driver-mode=g++
590endif
591
592ifneq "$(CXX)" ""
593	ifeq ($(findstring clang, $(LD)), clang)
594		LDFLAGS += --driver-mode=g++
595	endif
596endif
597
598#----------------------------------------------------------------------
599# DYLIB_ONLY variable can be used to skip the building of a.out.
600# See the sections below regarding dSYM file as well as the building of
601# EXE from all the objects.
602#----------------------------------------------------------------------
603
604#----------------------------------------------------------------------
605# Compile the executable from all the objects.
606#----------------------------------------------------------------------
607ifneq "$(DYLIB_NAME)" ""
608ifeq "$(DYLIB_ONLY)" ""
609$(EXE) : $(OBJECTS) $(ARCHIVE_NAME) $(DYLIB_FILENAME)
610	$(LD) $(OBJECTS) $(ARCHIVE_NAME) -L. -l$(DYLIB_NAME) $(LDFLAGS) -o "$(EXE)"
611ifneq "$(CODESIGN)" ""
612	$(CODESIGN) -s - "$(EXE)"
613endif
614else
615EXE = $(DYLIB_FILENAME)
616endif
617else
618$(EXE) : $(OBJECTS) $(ARCHIVE_NAME)
619	$(LD) $(OBJECTS) $(LDFLAGS) $(ARCHIVE_NAME) -o "$(EXE)"
620ifneq "$(CODESIGN)" ""
621	$(CODESIGN) -s - "$(EXE)"
622endif
623endif
624
625#----------------------------------------------------------------------
626# Make the dSYM file from the executable if $(MAKE_DSYM) != "NO"
627#----------------------------------------------------------------------
628$(DSYM) : $(EXE)
629ifeq "$(OS)" "Darwin"
630ifneq "$(MAKE_DSYM)" "NO"
631	"$(DS)" $(DSFLAGS) -o "$(DSYM)" "$(EXE)"
632else
633endif
634else
635ifeq "$(SPLIT_DEBUG_SYMBOLS)" "YES"
636	$(OBJCOPY) --only-keep-debug "$(EXE)" "$(DSYM)"
637	$(OBJCOPY) --strip-debug --add-gnu-debuglink="$(DSYM)" "$(EXE)" "$(EXE)"
638endif
639endif
640
641#----------------------------------------------------------------------
642# Make the archive
643#----------------------------------------------------------------------
644ifneq "$(ARCHIVE_NAME)" ""
645ifeq "$(OS)" "Darwin"
646$(ARCHIVE_NAME) : $(ARCHIVE_OBJECTS)
647	$(AR) $(ARFLAGS) $(ARCHIVE_NAME) $(ARCHIVE_OBJECTS)
648	$(RM) $(ARCHIVE_OBJECTS)
649else
650$(ARCHIVE_NAME) : $(foreach ar_obj,$(ARCHIVE_OBJECTS),$(ARCHIVE_NAME)($(ar_obj)))
651endif
652endif
653
654#----------------------------------------------------------------------
655# Make the dylib
656#----------------------------------------------------------------------
657$(DYLIB_OBJECTS) : CFLAGS += -DCOMPILING_LLDB_TEST_DLL
658
659ifneq "$(OS)" "Windows_NT"
660$(DYLIB_OBJECTS) : CFLAGS += -fPIC
661$(DYLIB_OBJECTS) : CXXFLAGS += -fPIC
662endif
663
664$(DYLIB_FILENAME) : $(DYLIB_OBJECTS)
665ifeq "$(OS)" "Darwin"
666ifneq "$(FRAMEWORK)" ""
667	mkdir -p $(FRAMEWORK).framework/Versions/A/Headers
668	mkdir -p $(FRAMEWORK).framework/Versions/A/Modules
669	mkdir -p $(FRAMEWORK).framework/Versions/A/Resources
670ifneq "$(FRAMEWORK_MODULES)" ""
671	cp -r $(FRAMEWORK_MODULES) $(FRAMEWORK).framework/Versions/A/Modules
672endif
673ifneq "$(FRAMEWORK_HEADERS)" ""
674	cp -r $(FRAMEWORK_HEADERS) $(FRAMEWORK).framework/Versions/A/Headers
675endif
676	(cd $(FRAMEWORK).framework/Versions; ln -sf A Current)
677	(cd $(FRAMEWORK).framework/; ln -sf Versions/A/Headers Headers)
678	(cd $(FRAMEWORK).framework/; ln -sf Versions/A/Modules Modules)
679	(cd $(FRAMEWORK).framework/; ln -sf Versions/A/Resources Resources)
680	(cd $(FRAMEWORK).framework/; ln -sf Versions/A/$(FRAMEWORK) $(FRAMEWORK))
681endif
682	$(LD) $(DYLIB_OBJECTS) $(LDFLAGS) -install_name "$(DYLIB_INSTALL_NAME)" -dynamiclib -o "$(DYLIB_FILENAME)"
683ifneq "$(CODESIGN)" ""
684	$(CODESIGN) -s - "$(DYLIB_FILENAME)"
685endif
686ifneq "$(MAKE_DSYM)" "NO"
687ifneq "$(DS)" ""
688	"$(DS)" $(DSFLAGS) "$(DYLIB_FILENAME)"
689endif
690endif
691else
692	$(LD) $(DYLIB_OBJECTS) $(LDFLAGS) -shared -o "$(DYLIB_FILENAME)"
693ifeq "$(SPLIT_DEBUG_SYMBOLS)" "YES"
694	$(OBJCOPY) --only-keep-debug "$(DYLIB_FILENAME)" "$(DYLIB_FILENAME).debug"
695	$(OBJCOPY) --strip-debug --add-gnu-debuglink="$(DYLIB_FILENAME).debug" "$(DYLIB_FILENAME)" "$(DYLIB_FILENAME)"
696endif
697endif
698
699#----------------------------------------------------------------------
700# Make the precompiled header and compile C++ sources against it
701#----------------------------------------------------------------------
702
703#ifneq "$(PCH_OUTPUT)" ""
704$(PCH_OUTPUT) : $(PCH_CXX_SOURCE)
705	$(CXX) $(CXXFLAGS) -x c++-header -o $@ $<
706%.o : %.cpp $(PCH_OUTPUT)
707	$(CXX) $(PCHFLAGS) $(CXXFLAGS) -c -o $@ $<
708#endif
709
710#----------------------------------------------------------------------
711# Automatic variables based on items already entered. Below we create
712# an object's lists from the list of sources by replacing all entries
713# that end with .c with .o, and we also create a list of prerequisite
714# files by replacing all .c files with .d.
715#----------------------------------------------------------------------
716PREREQS := $(OBJECTS:.o=.d)
717DWOS := $(OBJECTS:.o=.dwo) $(ARCHIVE_OBJECTS:.o=.dwo)
718ifneq "$(DYLIB_NAME)" ""
719	DYLIB_PREREQS := $(DYLIB_OBJECTS:.o=.d)
720	DYLIB_DWOS := $(DYLIB_OBJECTS:.o=.dwo)
721endif
722
723#----------------------------------------------------------------------
724# Rule for Generating Prerequisites Automatically using .d files and
725# the compiler -MM option. The -M option will list all system headers,
726# and the -MM option will list all non-system dependencies.
727#----------------------------------------------------------------------
728%.d: %.c
729	@rm -f $@ $(JOIN_CMD) \
730	$(CC) -M $(CFLAGS) $< > $@.tmp && \
731	sed $(QUOTE)s,\($*\)\.o[ :]*,\1.o $@ : ,g$(QUOTE) < $@.tmp > $@ $(JOIN_CMD) \
732	rm -f $@.tmp
733
734%.d: %.cpp
735	@rm -f $@ $(JOIN_CMD) \
736	$(CXX) -M $(CXXFLAGS) $< > $@.tmp && \
737	sed $(QUOTE)s,\($*\)\.o[ :]*,\1.o $@ : ,g$(QUOTE) < $@.tmp > $@ $(JOIN_CMD) \
738	rm -f $@.tmp
739
740%.d: %.m
741	@rm -f $@ $(JOIN_CMD) \
742	$(CC) -M $(CFLAGS) $< > $@.tmp && \
743	sed $(QUOTE)s,\($*\)\.o[ :]*,\1.o $@ : ,g$(QUOTE) < $@.tmp > $@ $(JOIN_CMD) \
744	rm -f $@.tmp
745
746%.d: %.mm
747	@rm -f $@ $(JOIN_CMD) \
748	$(CXX) -M $(CXXFLAGS) $< > $@.tmp && \
749	sed $(QUOTE)s,\($*\)\.o[ :]*,\1.o $@ : ,g$(QUOTE) < $@.tmp > $@ $(JOIN_CMD) \
750	rm -f $@.tmp
751
752#----------------------------------------------------------------------
753# Include all of the makefiles for each source file so we don't have
754# to manually track all of the prerequisites for each source file.
755#----------------------------------------------------------------------
756sinclude $(PREREQS)
757ifneq "$(DYLIB_NAME)" ""
758	sinclude $(DYLIB_PREREQS)
759endif
760
761# Define a suffix rule for .mm -> .o
762.SUFFIXES: .mm .o
763.mm.o:
764	$(CXX) $(CXXFLAGS) -c $<
765
766.PHONY: clean
767dsym:	$(DSYM)
768all:	$(EXE) $(DSYM)
769clean::
770ifeq "$(findstring lldb-test-build.noindex, $(BUILDDIR))" ""
771	$(error Trying to invoke the clean rule, but not using the default build tree layout)
772else
773	$(RM) -r $(wildcard $(BUILDDIR)/*)
774endif
775
776#----------------------------------------------------------------------
777# From http://blog.melski.net/tag/debugging-makefiles/
778#
779# Usage: make print-CC print-CXX print-LD
780#----------------------------------------------------------------------
781print-%:
782	@echo '$*=$($*)'
783	@echo '  origin = $(origin $*)'
784	@echo '  flavor = $(flavor $*)'
785	@echo '   value = $(value  $*)'
786
787### Local Variables: ###
788### mode:makefile ###
789### End: ###
790