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# ARCHFLAG is the flag used to tell the compiler which architecture
171# to compile for. The default is the flag that clang accepts.
172#----------------------------------------------------------------------
173ARCHFLAG ?= -arch
174
175#----------------------------------------------------------------------
176# Change any build/tool options needed
177#----------------------------------------------------------------------
178ifeq "$(OS)" "Darwin"
179	DS := $(DSYMUTIL)
180	DSFLAGS =
181	DSYM = $(EXE).dSYM
182	AR := $(CROSS_COMPILE)libtool
183	ARFLAGS := -static -o
184else
185	AR := $(CROSS_COMPILE)ar
186	# On non-Apple platforms, -arch becomes -m
187	ARCHFLAG := -m
188
189	# i386, i686, x86 -> 32
190	# amd64, x86_64, x64 -> 64
191	ifeq "$(ARCH)" "amd64"
192		override ARCH := $(subst amd64,64,$(ARCH))
193	endif
194	ifeq "$(ARCH)" "x86_64"
195		override ARCH := $(subst x86_64,64,$(ARCH))
196	endif
197	ifeq "$(ARCH)" "x64"
198		override ARCH := $(subst x64,64,$(ARCH))
199	endif
200	ifeq "$(ARCH)" "x86"
201		override ARCH := $(subst x86,32,$(ARCH))
202	endif
203	ifeq "$(ARCH)" "i386"
204		override ARCH := $(subst i386,32,$(ARCH))
205	endif
206	ifeq "$(ARCH)" "i686"
207		override ARCH := $(subst i686,32,$(ARCH))
208	endif
209	ifeq "$(ARCH)" "powerpc"
210		override ARCH := $(subst powerpc,32,$(ARCH))
211	endif
212	ifeq "$(ARCH)" "powerpc64"
213		override ARCH := $(subst powerpc64,64,$(ARCH))
214	endif
215	ifeq "$(ARCH)" "powerpc64le"
216		override ARCH := $(subst powerpc64le,64,$(ARCH))
217	endif
218	ifeq "$(ARCH)" "aarch64"
219		override ARCH :=
220		override ARCHFLAG :=
221	endif
222	ifeq "$(ARCH)" "arm"
223		override ARCH :=
224		override ARCHFLAG :=
225	endif
226	ifeq "$(ARCH)" "s390x"
227		override ARCH :=
228		override ARCHFLAG :=
229	endif
230	ifeq "$(findstring mips,$(ARCH))" "mips"
231		override ARCHFLAG := -
232	endif
233
234	ifeq "$(SPLIT_DEBUG_SYMBOLS)" "YES"
235		DSYM = $(EXE).debug
236	endif
237endif
238
239LIMIT_DEBUG_INFO_FLAGS =
240NO_LIMIT_DEBUG_INFO_FLAGS =
241MODULE_DEBUG_INFO_FLAGS =
242ifneq (,$(findstring clang,$(CC)))
243   LIMIT_DEBUG_INFO_FLAGS += -flimit-debug-info
244   NO_LIMIT_DEBUG_INFO_FLAGS += -fno-limit-debug-info
245   MODULE_DEBUG_INFO_FLAGS += -gmodules
246endif
247
248DEBUG_INFO_FLAG ?= -g
249
250CFLAGS ?= $(DEBUG_INFO_FLAG) -O0 -fno-builtin
251ifeq "$(OS)" "Darwin"
252	CFLAGS += $(ARCHFLAG) $(ARCH) $(FRAMEWORK_INCLUDES) -I$(LLDB_BASE_DIR)include
253else
254	CFLAGS += $(ARCHFLAG)$(ARCH) $(FRAMEWORK_INCLUDES) -I$(LLDB_BASE_DIR)include
255endif
256
257CFLAGS += -I$(SRCDIR) -include $(THIS_FILE_DIR)test_common.h -I$(THIS_FILE_DIR)
258CFLAGS += $(NO_LIMIT_DEBUG_INFO_FLAGS) $(ARCH_CFLAGS) $(CFLAGS_EXTRAS)
259
260# Use this one if you want to build one part of the result without debug information:
261ifeq "$(OS)" "Darwin"
262	CFLAGS_NO_DEBUG = -O0 $(ARCHFLAG) $(ARCH) $(FRAMEWORK_INCLUDES) $(ARCH_CFLAGS) $(CFLAGS_EXTRAS)
263else
264	CFLAGS_NO_DEBUG = -O0 $(ARCHFLAG)$(ARCH) $(FRAMEWORK_INCLUDES) $(ARCH_CFLAGS) $(CFLAGS_EXTRAS)
265endif
266
267ifeq "$(MAKE_DWO)" "YES"
268	CFLAGS += -gsplit-dwarf
269endif
270
271# Use a shared module cache when building in the default test build directory.
272CLANG_MODULE_CACHE_DIR := $(shell echo "$(BUILDDIR)" | sed $(QUOTE)s/lldb-test-build.noindex.*/lldb-test-build.noindex\/module-cache-clang/$(QUOTE))
273
274ifeq "$(findstring lldb-test-build.noindex, $(BUILDDIR))" ""
275CLANG_MODULE_CACHE_DIR := $(BUILDDIR)/module-cache
276$(warning failed to set the shared clang module cache dir)
277endif
278
279MANDATORY_MODULE_BUILD_CFLAGS := -fmodules -gmodules -fmodules-cache-path=$(CLANG_MODULE_CACHE_DIR)
280
281ifeq "$(MAKE_GMODULES)" "YES"
282	CFLAGS += $(MANDATORY_MODULE_BUILD_CFLAGS)
283	CXXFLAGS += $(MANDATORY_MODULE_BUILD_CFLAGS)
284	ifeq "$(OS)" "Darwin"
285		CXXFLAGS += -fcxx-modules
286	endif
287endif
288
289CXXFLAGS += -std=c++11 $(CFLAGS) $(ARCH_CXXFLAGS)
290LD = $(CC)
291LDFLAGS ?= $(CFLAGS)
292LDFLAGS += $(LD_EXTRAS) $(ARCH_LDFLAGS)
293ifeq (,$(filter $(OS), Windows_NT Android Darwin))
294	ifneq (,$(filter YES,$(ENABLE_THREADS)))
295		LDFLAGS += -pthread
296	endif
297endif
298OBJECTS =
299EXE ?= a.out
300
301ifneq "$(DYLIB_NAME)" ""
302	ifeq "$(OS)" "Darwin"
303		DYLIB_FILENAME = lib$(DYLIB_NAME).dylib
304		DYLIB_EXECUTABLE_PATH ?= @executable_path
305	else ifeq "$(OS)" "Windows_NT"
306		DYLIB_FILENAME = $(DYLIB_NAME).dll
307	else
308		DYLIB_FILENAME = lib$(DYLIB_NAME).so
309	endif
310endif
311
312# Function that returns the counterpart C++ compiler, given $(CC) as arg.
313cxx_compiler_notdir = $(if $(findstring icc,$(1)), \
314                            $(subst icc,icpc,$(1)), \
315                            $(if $(findstring llvm-gcc,$(1)), \
316                                 $(subst llvm-gcc,llvm-g++,$(1)), \
317                                 $(if $(findstring gcc,$(1)), \
318                                      $(subst gcc,g++,$(1)), \
319                                      $(subst cc,c++,$(1)))))
320cxx_compiler = $(if $(findstring /,$(1)),$(join $(dir $(1)), $(call cxx_compiler_notdir,$(notdir $(1)))),$(call cxx_compiler_notdir,$(1)))
321
322# Function that returns the C++ linker, given $(CC) as arg.
323cxx_linker_notdir = $(if $(findstring icc,$(1)), \
324                          $(subst icc,icpc,$(1)), \
325                          $(if $(findstring llvm-gcc,$(1)), \
326                               $(subst llvm-gcc,llvm-g++,$(1)), \
327                               $(if $(findstring gcc,$(1)), \
328                                    $(subst gcc,g++,$(1)), \
329                                    $(subst cc,c++,$(1)))))
330cxx_linker = $(if $(findstring /,$(1)),$(join $(dir $(1)), $(call cxx_linker_notdir,$(notdir $(1)))),$(call cxx_linker_notdir,$(1)))
331
332ifneq "$(OS)" "Darwin"
333    CLANG_OR_GCC := $(strip $(if $(findstring clang,$(CC)), \
334                                 $(findstring clang,$(CC)), \
335                                 $(if $(findstring gcc,$(CC)), \
336                                      $(findstring gcc,$(CC)), \
337                                      cc)))
338
339    CC_LASTWORD := $(strip $(lastword $(subst -, ,$(CC))))
340
341    replace_with = $(strip $(if $(findstring $(3),$(CC_LASTWORD)), \
342                           $(subst $(3),$(1),$(2)), \
343                           $(subst $(3),$(1),$(subst -$(CC_LASTWORD),,$(2)))))
344
345    ifeq "$(notdir $(CC))" "$(CC)"
346        replace_cc_with = $(call replace_with,$(1),$(CC),$(CLANG_OR_GCC))
347    else
348        replace_cc_with = $(join $(dir $(CC)),$(call replace_with,$(1),$(notdir $(CC)),$(CLANG_OR_GCC)))
349    endif
350
351    OBJCOPY ?= $(call replace_cc_with,objcopy)
352    ARCHIVER ?= $(call replace_cc_with,ar)
353    override AR = $(ARCHIVER)
354endif
355
356ifdef PIE
357	LDFLAGS += -pie
358endif
359
360#----------------------------------------------------------------------
361# Windows specific options
362#----------------------------------------------------------------------
363ifeq "$(OS)" "Windows_NT"
364	ifneq (,$(findstring clang,$(CC)))
365		# Clang for Windows doesn't support C++ Exceptions
366		CXXFLAGS += -fno-exceptions
367		CXXFLAGS += -D_HAS_EXCEPTIONS=0
368
369		# MSVC 2015 or higher is required, which depends on c++14, so
370		# append these values unconditionally.
371		CXXFLAGS += -fms-compatibility-version=19.0
372		override CXXFLAGS := $(subst -std=c++11,-std=c++14,$(CXXFLAGS))
373
374		# The MSVC linker doesn't understand long section names
375		# generated by the clang compiler.
376		LDFLAGS += -fuse-ld=lld
377	endif
378endif
379
380#----------------------------------------------------------------------
381# C++ standard library options
382#----------------------------------------------------------------------
383ifeq (1,$(USE_LIBSTDCPP))
384	# Clang requires an extra flag: -stdlib=libstdc++
385	ifneq (,$(findstring clang,$(CC)))
386		CXXFLAGS += -stdlib=libstdc++ -DLLDB_USING_LIBSTDCPP
387		LDFLAGS += -stdlib=libstdc++
388	endif
389endif
390
391ifeq (1,$(USE_LIBCPP))
392	CXXFLAGS += -DLLDB_USING_LIBCPP
393	ifeq "$(OS)" "Linux"
394		ifneq (,$(findstring clang,$(CC)))
395			CXXFLAGS += -stdlib=libc++
396			LDFLAGS += -stdlib=libc++
397		else
398			CXXFLAGS += -isystem /usr/include/c++/v1
399			LDFLAGS += -lc++
400		endif
401	else ifeq "$(OS)" "Android"
402		# Nothing to do, this is already handled in
403		# Android.rules.
404	else
405		CXXFLAGS += -stdlib=libc++
406		LDFLAGS += -stdlib=libc++
407	endif
408endif
409
410#----------------------------------------------------------------------
411# dylib settings
412#----------------------------------------------------------------------
413ifneq "$(strip $(DYLIB_C_SOURCES))" ""
414	DYLIB_OBJECTS +=$(strip $(DYLIB_C_SOURCES:.c=.o))
415endif
416
417ifneq "$(strip $(DYLIB_OBJC_SOURCES))" ""
418	DYLIB_OBJECTS +=$(strip $(DYLIB_OBJC_SOURCES:.m=.o))
419endif
420
421ifneq "$(strip $(DYLIB_CXX_SOURCES))" ""
422	DYLIB_OBJECTS +=$(strip $(DYLIB_CXX_SOURCES:.cpp=.o))
423	CXX = $(call cxx_compiler,$(CC))
424	LD = $(call cxx_linker,$(CC))
425endif
426
427#----------------------------------------------------------------------
428# Check if we have a precompiled header
429#----------------------------------------------------------------------
430ifneq "$(strip $(PCH_CXX_SOURCE))" ""
431	PCH_OUTPUT = $(PCH_CXX_SOURCE:.h=.h.pch)
432	PCHFLAGS = -include $(PCH_CXX_SOURCE)
433endif
434
435#----------------------------------------------------------------------
436# Check if we have any C source files
437#----------------------------------------------------------------------
438ifneq "$(strip $(C_SOURCES))" ""
439	OBJECTS +=$(strip $(C_SOURCES:.c=.o))
440endif
441
442#----------------------------------------------------------------------
443# Check if we have any C++ source files
444#----------------------------------------------------------------------
445ifneq "$(strip $(CXX_SOURCES))" ""
446	OBJECTS +=$(strip $(CXX_SOURCES:.cpp=.o))
447	CXX = $(call cxx_compiler,$(CC))
448	LD = $(call cxx_linker,$(CC))
449endif
450
451#----------------------------------------------------------------------
452# Check if we have any ObjC source files
453#----------------------------------------------------------------------
454ifneq "$(strip $(OBJC_SOURCES))" ""
455	OBJECTS +=$(strip $(OBJC_SOURCES:.m=.o))
456	LDFLAGS +=-lobjc
457endif
458
459#----------------------------------------------------------------------
460# Check if we have any ObjC++ source files
461#----------------------------------------------------------------------
462ifneq "$(strip $(OBJCXX_SOURCES))" ""
463	OBJECTS +=$(strip $(OBJCXX_SOURCES:.mm=.o))
464	CXX = $(call cxx_compiler,$(CC))
465	LD = $(call cxx_linker,$(CC))
466	ifeq "$(findstring lobjc,$(LDFLAGS))" ""
467		LDFLAGS +=-lobjc
468	endif
469endif
470
471#----------------------------------------------------------------------
472# Check if we have any C source files for archive
473#----------------------------------------------------------------------
474ifneq "$(strip $(ARCHIVE_C_SOURCES))" ""
475	ARCHIVE_OBJECTS +=$(strip $(ARCHIVE_C_SOURCES:.c=.o))
476endif
477
478#----------------------------------------------------------------------
479# Check if we have any C++ source files for archive
480#----------------------------------------------------------------------
481ifneq "$(strip $(ARCHIVE_CXX_SOURCES))" ""
482	ARCHIVE_OBJECTS +=$(strip $(ARCHIVE_CXX_SOURCES:.cpp=.o))
483	CXX = $(call cxx_compiler,$(CC))
484	LD = $(call cxx_linker,$(CC))
485endif
486
487#----------------------------------------------------------------------
488# Check if we have any ObjC source files for archive
489#----------------------------------------------------------------------
490ifneq "$(strip $(ARCHIVE_OBJC_SOURCES))" ""
491	ARCHIVE_OBJECTS +=$(strip $(ARCHIVE_OBJC_SOURCES:.m=.o))
492	LDFLAGS +=-lobjc
493endif
494
495#----------------------------------------------------------------------
496# Check if we have any ObjC++ source files for archive
497#----------------------------------------------------------------------
498ifneq "$(strip $(ARCHIVE_OBJCXX_SOURCES))" ""
499	ARCHIVE_OBJECTS +=$(strip $(ARCHIVE_OBJCXX_SOURCES:.mm=.o))
500	CXX = $(call cxx_compiler,$(CC))
501	LD = $(call cxx_linker,$(CC))
502	ifeq "$(findstring lobjc,$(LDFLAGS))" ""
503		LDFLAGS +=-lobjc
504	endif
505endif
506
507#----------------------------------------------------------------------
508# Check if we are compiling with gcc 4.6
509#----------------------------------------------------------------------
510ifneq "$(strip $(CXX_SOURCES) $(OBJCXX_SOURCES))" ""
511ifneq "$(filter g++,$(CXX))" ""
512	CXXVERSION = $(shell $(CXX) -dumpversion | cut -b 1-3)
513	ifeq "$(CXXVERSION)" "4.6"
514		# GCC 4.6 cannot handle -std=c++11, so replace it with -std=c++0x
515		# instead. FIXME: remove once GCC version is upgraded.
516		override CXXFLAGS := $(subst -std=c++11,-std=c++0x,$(CXXFLAGS))
517	endif
518endif
519endif
520
521ifeq ($(findstring clang, $(CXX)), clang)
522	CXXFLAGS += --driver-mode=g++
523endif
524
525ifneq "$(CXX)" ""
526	ifeq ($(findstring clang, $(LD)), clang)
527		LDFLAGS += --driver-mode=g++
528	endif
529endif
530
531#----------------------------------------------------------------------
532# DYLIB_ONLY variable can be used to skip the building of a.out.
533# See the sections below regarding dSYM file as well as the building of
534# EXE from all the objects.
535#----------------------------------------------------------------------
536
537#----------------------------------------------------------------------
538# Compile the executable from all the objects.
539#----------------------------------------------------------------------
540ifneq "$(DYLIB_NAME)" ""
541ifeq "$(DYLIB_ONLY)" ""
542$(EXE) : $(OBJECTS) $(ARCHIVE_NAME) $(DYLIB_FILENAME)
543	$(LD) $(OBJECTS) $(ARCHIVE_NAME) -L. -l$(DYLIB_NAME) $(LDFLAGS) -o "$(EXE)"
544else
545EXE = $(DYLIB_FILENAME)
546endif
547else
548$(EXE) : $(OBJECTS) $(ARCHIVE_NAME)
549	$(LD) $(OBJECTS) $(LDFLAGS) $(ARCHIVE_NAME) -o "$(EXE)"
550ifneq "$(CODESIGN)" ""
551	$(CODESIGN) -s - "$(EXE)"
552endif
553endif
554
555#----------------------------------------------------------------------
556# Make the dSYM file from the executable if $(MAKE_DSYM) != "NO"
557#----------------------------------------------------------------------
558$(DSYM) : $(EXE)
559ifeq "$(OS)" "Darwin"
560ifneq "$(MAKE_DSYM)" "NO"
561	"$(DS)" $(DSFLAGS) -o "$(DSYM)" "$(EXE)"
562else
563endif
564else
565ifeq "$(SPLIT_DEBUG_SYMBOLS)" "YES"
566	$(OBJCOPY) --only-keep-debug "$(EXE)" "$(DSYM)"
567	$(OBJCOPY) --strip-debug --add-gnu-debuglink="$(DSYM)" "$(EXE)" "$(EXE)"
568endif
569endif
570
571#----------------------------------------------------------------------
572# Make the archive
573#----------------------------------------------------------------------
574ifneq "$(ARCHIVE_NAME)" ""
575ifeq "$(OS)" "Darwin"
576$(ARCHIVE_NAME) : $(ARCHIVE_OBJECTS)
577	$(AR) $(ARFLAGS) $(ARCHIVE_NAME) $(ARCHIVE_OBJECTS)
578	$(RM) $(ARCHIVE_OBJECTS)
579else
580$(ARCHIVE_NAME) : $(foreach ar_obj,$(ARCHIVE_OBJECTS),$(ARCHIVE_NAME)($(ar_obj)))
581endif
582endif
583
584#----------------------------------------------------------------------
585# Make the dylib
586#----------------------------------------------------------------------
587$(DYLIB_OBJECTS) : CFLAGS += -DCOMPILING_LLDB_TEST_DLL
588
589ifneq "$(OS)" "Windows_NT"
590$(DYLIB_OBJECTS) : CFLAGS += -fPIC
591$(DYLIB_OBJECTS) : CXXFLAGS += -fPIC
592endif
593
594$(DYLIB_FILENAME) : $(DYLIB_OBJECTS)
595ifeq "$(OS)" "Darwin"
596	$(LD) $(DYLIB_OBJECTS) $(LDFLAGS) -install_name "$(DYLIB_EXECUTABLE_PATH)/$(DYLIB_FILENAME)" -dynamiclib -o "$(DYLIB_FILENAME)"
597ifneq "$(CODESIGN)" ""
598	$(CODESIGN) -s - "$(DYLIB_FILENAME)"
599endif
600ifneq "$(MAKE_DSYM)" "NO"
601ifneq "$(DS)" ""
602	"$(DS)" $(DSFLAGS) "$(DYLIB_FILENAME)"
603endif
604endif
605else
606	$(LD) $(DYLIB_OBJECTS) $(LDFLAGS) -shared -o "$(DYLIB_FILENAME)"
607ifeq "$(SPLIT_DEBUG_SYMBOLS)" "YES"
608	$(OBJCOPY) --only-keep-debug "$(DYLIB_FILENAME)" "$(DYLIB_FILENAME).debug"
609	$(OBJCOPY) --strip-debug --add-gnu-debuglink="$(DYLIB_FILENAME).debug" "$(DYLIB_FILENAME)" "$(DYLIB_FILENAME)"
610endif
611endif
612
613#----------------------------------------------------------------------
614# Make the precompiled header and compile C++ sources against it
615#----------------------------------------------------------------------
616
617#ifneq "$(PCH_OUTPUT)" ""
618$(PCH_OUTPUT) : $(PCH_CXX_SOURCE)
619	$(CXX) $(CXXFLAGS) -x c++-header -o $@ $<
620%.o : %.cpp $(PCH_OUTPUT)
621	$(CXX) $(PCHFLAGS) $(CXXFLAGS) -c -o $@ $<
622#endif
623
624#----------------------------------------------------------------------
625# Automatic variables based on items already entered. Below we create
626# an object's lists from the list of sources by replacing all entries
627# that end with .c with .o, and we also create a list of prerequisite
628# files by replacing all .c files with .d.
629#----------------------------------------------------------------------
630PREREQS := $(OBJECTS:.o=.d)
631DWOS := $(OBJECTS:.o=.dwo) $(ARCHIVE_OBJECTS:.o=.dwo)
632ifneq "$(DYLIB_NAME)" ""
633	DYLIB_PREREQS := $(DYLIB_OBJECTS:.o=.d)
634	DYLIB_DWOS := $(DYLIB_OBJECTS:.o=.dwo)
635endif
636
637#----------------------------------------------------------------------
638# Rule for Generating Prerequisites Automatically using .d files and
639# the compiler -MM option. The -M option will list all system headers,
640# and the -MM option will list all non-system dependencies.
641#----------------------------------------------------------------------
642%.d: %.c
643	@rm -f $@ $(JOIN_CMD) \
644	$(CC) -M $(CFLAGS) $< > $@.tmp && \
645	sed $(QUOTE)s,\($*\)\.o[ :]*,\1.o $@ : ,g$(QUOTE) < $@.tmp > $@ $(JOIN_CMD) \
646	rm -f $@.tmp
647
648%.d: %.cpp
649	@rm -f $@ $(JOIN_CMD) \
650	$(CXX) -M $(CXXFLAGS) $< > $@.tmp && \
651	sed $(QUOTE)s,\($*\)\.o[ :]*,\1.o $@ : ,g$(QUOTE) < $@.tmp > $@ $(JOIN_CMD) \
652	rm -f $@.tmp
653
654%.d: %.m
655	@rm -f $@ $(JOIN_CMD) \
656	$(CC) -M $(CFLAGS) $< > $@.tmp && \
657	sed $(QUOTE)s,\($*\)\.o[ :]*,\1.o $@ : ,g$(QUOTE) < $@.tmp > $@ $(JOIN_CMD) \
658	rm -f $@.tmp
659
660%.d: %.mm
661	@rm -f $@ $(JOIN_CMD) \
662	$(CXX) -M $(CXXFLAGS) $< > $@.tmp && \
663	sed $(QUOTE)s,\($*\)\.o[ :]*,\1.o $@ : ,g$(QUOTE) < $@.tmp > $@ $(JOIN_CMD) \
664	rm -f $@.tmp
665
666#----------------------------------------------------------------------
667# Include all of the makefiles for each source file so we don't have
668# to manually track all of the prerequisites for each source file.
669#----------------------------------------------------------------------
670sinclude $(PREREQS)
671ifneq "$(DYLIB_NAME)" ""
672	sinclude $(DYLIB_PREREQS)
673endif
674
675# Define a suffix rule for .mm -> .o
676.SUFFIXES: .mm .o
677.mm.o:
678	$(CXX) $(CXXFLAGS) -c $<
679
680.PHONY: clean
681dsym:	$(DSYM)
682all:	$(EXE) $(DSYM)
683clean::
684	$(RM) -rf $(OBJECTS) $(PREREQS) $(PREREQS:.d=.d.tmp) $(DWOS) $(ARCHIVE_NAME) $(ARCHIVE_OBJECTS) $(CLANG_MODULE_CACHE_DIR)
685ifneq "$(DYLIB_NAME)" ""
686	$(RM) -r $(DYLIB_FILENAME).dSYM
687	$(RM) $(DYLIB_OBJECTS) $(DYLIB_PREREQS) $(DYLIB_PREREQS:.d=.d.tmp) $(DYLIB_DWOS) $(DYLIB_FILENAME) $(DYLIB_FILENAME).debug
688endif
689ifneq "$(PCH_OUTPUT)" ""
690	$(RM) $(PCH_OUTPUT)
691endif
692ifneq "$(DSYM)" ""
693	$(RM) -r "$(DSYM)"
694endif
695ifeq "$(OS)" "Windows_NT"
696# http://llvm.org/pr24589
697	IF EXIST "$(EXE)" del "$(EXE)"
698	$(RM) $(wildcard *.manifest *.pdb *.ilk)
699ifneq "$(DYLIB_NAME)" ""
700	$(RM) $(DYLIB_NAME).lib $(DYLIB_NAME).exp
701endif
702else
703	$(RM) "$(EXE)"
704endif
705
706#----------------------------------------------------------------------
707# From http://blog.melski.net/tag/debugging-makefiles/
708#
709# Usage: make print-CC print-CXX print-LD
710#----------------------------------------------------------------------
711print-%:
712	@echo '$*=$($*)'
713	@echo '  origin = $(origin $*)'
714	@echo '  flavor = $(flavor $*)'
715	@echo '   value = $(value  $*)'
716
717### Local Variables: ###
718### mode:makefile ###
719### End: ###
720