blob: ae47c7e8aadcf1a61500848eae608181988ea391 [file] [log] [blame]
Johnny Chen9bc867a2010-08-23 23:56:08 +00001#----------------------------------------------------------------------
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 :=
Johnny Chene9f97702012-01-11 01:42:58 +00009# DYLIB_CXX_SOURCES :=
10#
11# Specifying DYLIB_ONLY has the effect of building dylib only, skipping
12# the building of the a.out executable program. For example,
13# DYLIB_ONLY := YES
14#
Johnny Chen64a7e742012-01-17 00:58:08 +000015# Also might be of interest:
Johnny Chen4876b5f2012-01-11 01:59:55 +000016# FRAMEWORK_INCLUDES (Darwin only) :=
17# CFLAGS_EXTRAS :=
18# LD_EXTRAS :=
Michael Sartain802b0552013-07-02 18:13:13 +000019# SPLIT_DEBUG_SYMBOLS := YES
Johnny Chen64a7e742012-01-17 00:58:08 +000020#
21# And test/functionalities/archives/Makefile:
22# MAKE_DSYM := NO
23# ARCHIVE_NAME := libfoo.a
24# ARCHIVE_C_SOURCES := a.c b.c
Johnny Chen9bc867a2010-08-23 23:56:08 +000025
26# Uncomment line below for debugging shell commands
27# SHELL = /bin/sh -x
28
29#----------------------------------------------------------------------
Johnny Chen6c17c082010-09-16 20:54:06 +000030# If ARCH is not defined, default to x86_64.
Peter Collingbourne518f03d2011-06-20 19:06:04 +000031# If OS is not defined, use 'uname -s' to determine the OS name.
Johnny Chen6c17c082010-09-16 20:54:06 +000032#----------------------------------------------------------------------
33ifeq "$(ARCH)" ""
34 ARCH = x86_64
35endif
36
Peter Collingbourne518f03d2011-06-20 19:06:04 +000037ifeq "$(OS)" ""
38 OS = $(shell uname -s)
39endif
40
Johnny Chen6c17c082010-09-16 20:54:06 +000041#----------------------------------------------------------------------
Johnny Chen597cbbb2011-08-23 21:54:10 +000042# CC defaults to clang.
Johnny Chen4ab4a9b2011-08-24 18:12:53 +000043#
44# If you change the defaults of CC, be sure to also change it in the file
45# test/plugins/builder_base.py, which provides a Python way to return the
46# value of the make variable CC -- getCompiler().
47#
Johnny Chen6055b442011-01-14 20:55:13 +000048# See also these functions:
49# o cxx_compiler
50# o cxx_linker
Johnny Chend43e2082011-01-14 20:46:49 +000051#----------------------------------------------------------------------
Johnny Chen597cbbb2011-08-23 21:54:10 +000052CC ?= clang
Johnny Chend43e2082011-01-14 20:46:49 +000053ifeq "$(CC)" "cc"
Johnny Chen597cbbb2011-08-23 21:54:10 +000054 CC = clang
Johnny Chend43e2082011-01-14 20:46:49 +000055endif
56
57#----------------------------------------------------------------------
Daniel Malea2745d842013-01-25 00:31:48 +000058# ARCHFLAG is the flag used to tell the compiler which architecture
59# to compile for. The default is the flag that clang accepts.
60#----------------------------------------------------------------------
61ARCHFLAG ?= -arch
62
63#----------------------------------------------------------------------
Johnny Chen9bc867a2010-08-23 23:56:08 +000064# Change any build/tool options needed
65#----------------------------------------------------------------------
Peter Collingbourne518f03d2011-06-20 19:06:04 +000066ifeq "$(OS)" "Darwin"
Greg Clayton3bffb082011-12-10 02:15:28 +000067 DS := dsymutil
Peter Collingbourne518f03d2011-06-20 19:06:04 +000068 DSFLAGS =
69 DSYM = $(EXE).dSYM
Johnny Chenfe141622012-01-12 23:09:42 +000070 AR := libtool
71 ARFLAGS := -static -o
Daniel Malea2745d842013-01-25 00:31:48 +000072else
73 # On non-Apple platforms, -arch becomes -m
74 ARCHFLAG := -m
75
76 # i386 becomes 32, and x86_64 becomes 64
77 ifeq "$(ARCH)" "x86_64"
78 override ARCH := $(subst x86_64,64,$(ARCH))
79 endif
80 ifeq "$(ARCH)" "i386"
81 override ARCH := $(subst i386,32,$(ARCH))
82 endif
Michael Sartain802b0552013-07-02 18:13:13 +000083
84 ifeq "$(SPLIT_DEBUG_SYMBOLS)" "YES"
85 DSYM = $(EXE).debug
86 endif
Peter Collingbourne518f03d2011-06-20 19:06:04 +000087endif
88
Greg Claytone595c6b2013-02-28 18:47:39 +000089CFLAGS ?= -g -O0
Daniel Malea2745d842013-01-25 00:31:48 +000090CFLAGS += $(ARCHFLAG)$(ARCH) $(FRAMEWORK_INCLUDES) $(CFLAGS_EXTRAS)
91
Johnny Chen53e2edb2011-08-09 20:07:16 +000092CXXFLAGS +=$(CFLAGS)
93LD = $(CC)
94LDFLAGS ?= $(CFLAGS)
Daniel Malea2745d842013-01-25 00:31:48 +000095LDFLAGS += $(LD_EXTRAS)
Johnny Chen53e2edb2011-08-09 20:07:16 +000096OBJECTS =
97EXE ?= a.out
98
Peter Collingbourne518f03d2011-06-20 19:06:04 +000099ifneq "$(DYLIB_NAME)" ""
100 ifeq "$(OS)" "Darwin"
101 DYLIB_FILENAME = lib$(DYLIB_NAME).dylib
102 else
103 DYLIB_FILENAME = lib$(DYLIB_NAME).so
104 endif
105endif
Johnny Chen9bc867a2010-08-23 23:56:08 +0000106
Johnny Chenbdb4efc2011-01-14 18:19:53 +0000107# Function that returns the counterpart C++ compiler, given $(CC) as arg.
Matt Kopecf99f0b82013-03-15 21:55:13 +0000108cxx_compiler = $(if $(findstring clang,$(1)), $(subst clang,clang++,$(1)), $(if $(findstring icc,$(1)), $(subst icc,icpc,$(1)), $(if $(findstring llvm-gcc,$(1)), $(subst llvm-gcc,llvm-g++,$(1)), $(subst gcc,g++,$(1)))))
Johnny Chenbdb4efc2011-01-14 18:19:53 +0000109
110# Function that returns the C++ linker, given $(CC) as arg.
Matt Kopecf99f0b82013-03-15 21:55:13 +0000111cxx_linker = $(if $(findstring clang,$(1)), $(subst clang,clang++,$(1)), $(if $(findstring icc,$(1)), $(subst icc,icpc,$(1)), $(if $(findstring llvm-gcc,$(1)), $(subst llvm-gcc,llvm-g++,$(1)), $(subst gcc,g++,$(1)))))
Johnny Chen832d2332010-09-30 01:22:34 +0000112
Johnny Chen9bc867a2010-08-23 23:56:08 +0000113#----------------------------------------------------------------------
Daniel Maleac7ffa7a2013-06-05 19:32:34 +0000114# C++ standard library options
115#----------------------------------------------------------------------
116ifeq (1,$(USE_LIBSTDCPP))
117 # Clang requires an extra flag: -stdlib=libstdc++
118 ifneq (,$(findstring clang,$(CC)))
Enrico Granataa1acb492013-06-07 01:58:52 +0000119 CXXFLAGS += -stdlib=libstdc++
Daniel Maleac7ffa7a2013-06-05 19:32:34 +0000120 LDFLAGS += -stdlib=libstdc++
121 endif
122endif
123
Greg Clayton1767a732013-06-13 21:27:14 +0000124ifeq (1,$(USE_LIBCPP))
125 # Clang requires an extra flag: -stdlib=libstdc++
126 ifneq (,$(findstring clang,$(CC)))
127 CXXFLAGS += -stdlib=libc++
128 LDFLAGS += -stdlib=libc++
129 endif
130endif
131
Daniel Maleac7ffa7a2013-06-05 19:32:34 +0000132#----------------------------------------------------------------------
Johnny Chen9bc867a2010-08-23 23:56:08 +0000133# dylib settings
134#----------------------------------------------------------------------
135ifneq "$(strip $(DYLIB_C_SOURCES))" ""
136 DYLIB_OBJECTS +=$(strip $(DYLIB_C_SOURCES:.c=.o))
137endif
138
Sean Callanan72772842012-02-22 23:57:45 +0000139ifneq "$(strip $(DYLIB_OBJC_SOURCES))" ""
140 DYLIB_OBJECTS +=$(strip $(DYLIB_OBJC_SOURCES:.m=.o))
141endif
142
Greg Claytond50d2382012-01-10 00:00:15 +0000143ifneq "$(strip $(DYLIB_CXX_SOURCES))" ""
144 DYLIB_OBJECTS +=$(strip $(DYLIB_CXX_SOURCES:.cpp=.o))
145 CXX = $(call cxx_compiler,$(CC))
146 LD = $(call cxx_linker,$(CC))
147endif
Johnny Chen9bc867a2010-08-23 23:56:08 +0000148
149#----------------------------------------------------------------------
150# Check if we have any C source files
151#----------------------------------------------------------------------
152ifneq "$(strip $(C_SOURCES))" ""
153 OBJECTS +=$(strip $(C_SOURCES:.c=.o))
154endif
155
156#----------------------------------------------------------------------
157# Check if we have any C++ source files
158#----------------------------------------------------------------------
159ifneq "$(strip $(CXX_SOURCES))" ""
160 OBJECTS +=$(strip $(CXX_SOURCES:.cpp=.o))
Johnny Chen832d2332010-09-30 01:22:34 +0000161 CXX = $(call cxx_compiler,$(CC))
Johnny Chenbdb4efc2011-01-14 18:19:53 +0000162 LD = $(call cxx_linker,$(CC))
Johnny Chen9bc867a2010-08-23 23:56:08 +0000163endif
164
165#----------------------------------------------------------------------
166# Check if we have any ObjC source files
167#----------------------------------------------------------------------
168ifneq "$(strip $(OBJC_SOURCES))" ""
169 OBJECTS +=$(strip $(OBJC_SOURCES:.m=.o))
170 LDFLAGS +=-lobjc
171endif
172
173#----------------------------------------------------------------------
174# Check if we have any ObjC++ source files
175#----------------------------------------------------------------------
176ifneq "$(strip $(OBJCXX_SOURCES))" ""
177 OBJECTS +=$(strip $(OBJCXX_SOURCES:.mm=.o))
Johnny Chen832d2332010-09-30 01:22:34 +0000178 CXX = $(call cxx_compiler,$(CC))
Johnny Chenbdb4efc2011-01-14 18:19:53 +0000179 LD = $(call cxx_linker,$(CC))
Johnny Chen8da4ddf2012-04-24 23:05:07 +0000180 ifeq "$(findstring lobjc,$(LDFLAGS))" ""
Johnny Chen9bc867a2010-08-23 23:56:08 +0000181 LDFLAGS +=-lobjc
182 endif
183endif
184
Johnny Chenfe141622012-01-12 23:09:42 +0000185#----------------------------------------------------------------------
186# Check if we have any C source files for archive
187#----------------------------------------------------------------------
188ifneq "$(strip $(ARCHIVE_C_SOURCES))" ""
189 ARCHIVE_OBJECTS +=$(strip $(ARCHIVE_C_SOURCES:.c=.o))
190endif
191
192#----------------------------------------------------------------------
193# Check if we have any C++ source files for archive
194#----------------------------------------------------------------------
195ifneq "$(strip $(ARCHIVE_CXX_SOURCES))" ""
196 ARCHIVE_OBJECTS +=$(strip $(ARCHIVE_CXX_SOURCES:.cpp=.o))
197 CXX = $(call cxx_compiler,$(CC))
198 LD = $(call cxx_linker,$(CC))
199endif
200
201#----------------------------------------------------------------------
202# Check if we have any ObjC source files for archive
203#----------------------------------------------------------------------
204ifneq "$(strip $(ARCHIVE_OBJC_SOURCES))" ""
205 ARCHIVE_OBJECTS +=$(strip $(ARCHIVE_OBJC_SOURCES:.m=.o))
206 LDFLAGS +=-lobjc
207endif
208
209#----------------------------------------------------------------------
210# Check if we have any ObjC++ source files for archive
211#----------------------------------------------------------------------
212ifneq "$(strip $(ARCHIVE_OBJCXX_SOURCES))" ""
213 ARCHIVE_OBJECTS +=$(strip $(ARCHIVE_OBJCXX_SOURCES:.mm=.o))
214 CXX = $(call cxx_compiler,$(CC))
215 LD = $(call cxx_linker,$(CC))
Johnny Chen8da4ddf2012-04-24 23:05:07 +0000216 ifeq "$(findstring lobjc,$(LDFLAGS))" ""
Johnny Chenfe141622012-01-12 23:09:42 +0000217 LDFLAGS +=-lobjc
218 endif
219endif
220
Matt Kopecf2430f52013-07-24 21:39:24 +0000221#----------------------------------------------------------------------
222# Check if we are compiling with gcc 4.6
223#----------------------------------------------------------------------
224ifneq (,$(filter g++,$(CXX)))
225 CXXVERSION = $(shell g++ -dumpversion | cut -b 1-3)
226 ifeq "$(CXXVERSION)" "4.6"
227 # GCC 4.6 cannot handle -std=c++11, so replace it with -std=c++0x
228 # instead. FIXME: remove once GCC version is upgraded.
229 override CXXFLAGS := $(subst -std=c++11,-std=c++0x,$(CXXFLAGS))
230 endif
231endif
Johnny Chen9bc867a2010-08-23 23:56:08 +0000232
233#----------------------------------------------------------------------
Johnny Chenbeac8f92012-01-10 00:41:11 +0000234# DYLIB_ONLY variable can be used to skip the building of a.out.
235# See the sections below regarding dSYM file as well as the building of
236# EXE from all the objects.
237#----------------------------------------------------------------------
238
239#----------------------------------------------------------------------
Johnny Chen9bc867a2010-08-23 23:56:08 +0000240# Make the dSYM file from the executable if $(MAKE_DSYM) != "NO"
241#----------------------------------------------------------------------
Matt Kopec7663b3a2013-09-25 17:44:00 +0000242ifneq "$(DYLIB_ONLY)" "YES"
Michael Sartain802b0552013-07-02 18:13:13 +0000243$(DSYM) : $(EXE)
Peter Collingbourne518f03d2011-06-20 19:06:04 +0000244ifeq "$(OS)" "Darwin"
Johnny Chen91016392011-06-20 20:08:26 +0000245ifneq "$(MAKE_DSYM)" "NO"
Johnny Chen9bc867a2010-08-23 23:56:08 +0000246 $(DS) $(DSFLAGS) -o "$(DSYM)" "$(EXE)"
Johnny Chen91016392011-06-20 20:08:26 +0000247endif
Michael Sartain802b0552013-07-02 18:13:13 +0000248else
249ifeq "$(SPLIT_DEBUG_SYMBOLS)" "YES"
Michael Sartain802b0552013-07-02 18:13:13 +0000250 objcopy --only-keep-debug "$(EXE)" "$(DSYM)"
251 objcopy --strip-debug --add-gnu-debuglink="$(DSYM)" "$(EXE)" "$(EXE)"
252endif
253endif
Johnny Chenbeac8f92012-01-10 00:41:11 +0000254endif
Johnny Chen9bc867a2010-08-23 23:56:08 +0000255
256#----------------------------------------------------------------------
257# Compile the executable from all the objects.
258#----------------------------------------------------------------------
Johnny Chenbeac8f92012-01-10 00:41:11 +0000259ifneq "$(DYLIB_NAME)" ""
260ifeq "$(DYLIB_ONLY)" ""
Johnny Chenfe141622012-01-12 23:09:42 +0000261$(EXE) : $(OBJECTS) $(ARCHIVE_NAME) $(DYLIB_FILENAME)
Richard Mittonec8b2822013-10-17 20:09:33 +0000262 $(LD) $(OBJECTS) $(ARCHIVE_NAME) -L. -l$(DYLIB_NAME) $(LDFLAGS) -o "$(EXE)"
Johnny Chenbeac8f92012-01-10 00:41:11 +0000263else
264EXE = $(DYLIB_FILENAME)
265endif
266else
Johnny Chenfe141622012-01-12 23:09:42 +0000267$(EXE) : $(OBJECTS) $(ARCHIVE_NAME)
Daniel Malea2745d842013-01-25 00:31:48 +0000268 $(LD) $(OBJECTS) $(LDFLAGS) $(ARCHIVE_NAME) -o "$(EXE)"
Johnny Chenfe141622012-01-12 23:09:42 +0000269endif
270
271#----------------------------------------------------------------------
272# Make the archive
273#----------------------------------------------------------------------
274ifneq "$(ARCHIVE_NAME)" ""
275ifeq "$(OS)" "Darwin"
276$(ARCHIVE_NAME) : $(ARCHIVE_OBJECTS)
277 $(AR) $(ARFLAGS) $(ARCHIVE_NAME) $(ARCHIVE_OBJECTS)
278 $(RM) $(ARCHIVE_OBJECTS)
279else
280$(ARCHIVE_NAME) : $(foreach ar_obj,$(ARCHIVE_OBJECTS),$(ARCHIVE_NAME)($(ar_obj)))
281endif
Johnny Chen9bc867a2010-08-23 23:56:08 +0000282endif
283
284#----------------------------------------------------------------------
285# Make the dylib
286#----------------------------------------------------------------------
Peter Collingbourne518f03d2011-06-20 19:06:04 +0000287$(DYLIB_FILENAME) : $(DYLIB_OBJECTS)
288ifeq "$(OS)" "Darwin"
289 $(LD) $(LDFLAGS) $(DYLIB_OBJECTS) -install_name "@executable_path/$(DYLIB_FILENAME)" -dynamiclib -o "$(DYLIB_FILENAME)"
Greg Clayton0c9773c2012-09-20 21:43:11 +0000290ifneq "$(MAKE_DSYM)" "NO"
291ifneq "$(DS)" ""
292 $(DS) $(DSFLAGS) "$(DYLIB_FILENAME)"
293endif
294endif
Peter Collingbourne518f03d2011-06-20 19:06:04 +0000295else
296 $(LD) $(LDFLAGS) $(DYLIB_OBJECTS) -shared -o "$(DYLIB_FILENAME)"
Michael Sartain802b0552013-07-02 18:13:13 +0000297ifeq "$(SPLIT_DEBUG_SYMBOLS)" "YES"
298 objcopy --only-keep-debug "$(DYLIB_FILENAME)" "$(DYLIB_FILENAME).debug"
299 objcopy --strip-debug --add-gnu-debuglink="$(DYLIB_FILENAME).debug" "$(DYLIB_FILENAME)" "$(DYLIB_FILENAME)"
300endif
Peter Collingbourne518f03d2011-06-20 19:06:04 +0000301endif
Johnny Chen9bc867a2010-08-23 23:56:08 +0000302
303#----------------------------------------------------------------------
304# Automatic variables based on items already entered. Below we create
305# an objects lists from the list of sources by replacing all entries
306# that end with .c with .o, and we also create a list of prerequisite
307# files by replacing all .c files with .d.
308#----------------------------------------------------------------------
309PREREQS := $(OBJECTS:.o=.d)
Johnny Chene3dc0f02010-08-24 16:35:00 +0000310ifneq "$(DYLIB_NAME)" ""
311 DYLIB_PREREQS := $(DYLIB_OBJECTS:.o=.d)
312endif
Johnny Chen9bc867a2010-08-23 23:56:08 +0000313
314#----------------------------------------------------------------------
315# Rule for Generating Prerequisites Automatically using .d files and
316# the compiler -MM option. The -M option will list all system headers,
317# and the -MM option will list all non-system dependencies.
318#----------------------------------------------------------------------
319%.d: %.c
320 @set -e; rm -f $@; \
Johnny Chen189bff12011-08-04 20:44:56 +0000321 $(CC) -M $(CFLAGS) $< > $@.$$$$; \
Johnny Chen9bc867a2010-08-23 23:56:08 +0000322 sed 's,\($*\)\.o[ :]*,\1.o $@ : ,g' < $@.$$$$ > $@; \
323 rm -f $@.$$$$
324
325%.d: %.cpp
326 @set -e; rm -f $@; \
Stefanus Du Toitf1620ef2013-07-30 17:33:30 +0000327 $(CXX) -M $(CXXFLAGS) $< > $@.$$$$; \
Johnny Chen9bc867a2010-08-23 23:56:08 +0000328 sed 's,\($*\)\.o[ :]*,\1.o $@ : ,g' < $@.$$$$ > $@; \
329 rm -f $@.$$$$
330
331%.d: %.m
332 @set -e; rm -f $@; \
Johnny Chen189bff12011-08-04 20:44:56 +0000333 $(CC) -M $(CFLAGS) $< > $@.$$$$; \
Johnny Chen9bc867a2010-08-23 23:56:08 +0000334 sed 's,\($*\)\.o[ :]*,\1.o $@ : ,g' < $@.$$$$ > $@; \
335 rm -f $@.$$$$
336
337%.d: %.mm
338 @set -e; rm -f $@; \
Stefanus Du Toitf1620ef2013-07-30 17:33:30 +0000339 $(CXX) -M $(CXXFLAGS) $< > $@.$$$$; \
Johnny Chen9bc867a2010-08-23 23:56:08 +0000340 sed 's,\($*\)\.o[ :]*,\1.o $@ : ,g' < $@.$$$$ > $@; \
341 rm -f $@.$$$$
342
343#----------------------------------------------------------------------
344# Include all of the makefiles for each source file so we don't have
345# to manually track all of the prerequisites for each source file.
346#----------------------------------------------------------------------
347sinclude $(PREREQS)
Johnny Chene3dc0f02010-08-24 16:35:00 +0000348ifneq "$(DYLIB_NAME)" ""
349 sinclude $(DYLIB_PREREQS)
350endif
Johnny Chen9bc867a2010-08-23 23:56:08 +0000351
Johnny Chen8da4ddf2012-04-24 23:05:07 +0000352# Define a suffix rule for .mm -> .o
353.SUFFIXES: .mm .o
354.mm.o:
355 $(CXX) $(CXXFLAGS) -c $<
356
Johnny Chen9bc867a2010-08-23 23:56:08 +0000357.PHONY: clean
358dsym: $(DSYM)
359all: $(EXE) $(DSYM)
Johnny Chene1030cd2010-09-27 20:44:46 +0000360clean::
Johnny Chen9bc867a2010-08-23 23:56:08 +0000361ifeq "$(DYLIB_NAME)" ""
Greg Clayton1767a732013-06-13 21:27:14 +0000362 rm -rf "$(EXE)" "$(DSYM)" $(OBJECTS) $(PREREQS) $(ARCHIVE_NAME) $(ARCHIVE_OBJECTS) *.d.[0-9] *.d.[0-9][0-9] *.d.[0-9][0-9][0-9] *.d.[0-9][0-9][0-9][0-9] *.d.[0-9][0-9][0-9][0-9][0-9]
Johnny Chen9bc867a2010-08-23 23:56:08 +0000363else
Michael Sartain802b0552013-07-02 18:13:13 +0000364 rm -rf "$(EXE)" "$(DSYM)" $(OBJECTS) $(PREREQS) $(ARCHIVE_NAME) $(ARCHIVE_OBJECTS) $(DYLIB_OBJECTS) $(DYLIB_PREREQS) $(DYLIB_FILENAME) $(DYLIB_FILENAME).dSYM $(DYLIB_FILENAME).debug *.d.[0-9] *.d.[0-9][0-9] *.d.[0-9][0-9][0-9] *.d.[0-9][0-9][0-9][0-9] *.d.[0-9][0-9][0-9][0-9][0-9]
Johnny Chen9bc867a2010-08-23 23:56:08 +0000365endif
Johnny Chenfa380412011-01-14 21:18:12 +0000366
367#----------------------------------------------------------------------
368# From http://blog.melski.net/tag/debugging-makefiles/
369#
370# Usage: make print-CC print-CXX print-LD
371#----------------------------------------------------------------------
372print-%:
373 @echo '$*=$($*)'
374 @echo ' origin = $(origin $*)'
375 @echo ' flavor = $(flavor $*)'
376 @echo ' value = $(value $*)'
Johnny Chen8b2c3212011-01-28 17:22:29 +0000377
378
379### Local Variables: ###
380### mode:makefile ###
381### End: ###