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