blob: 20b172fbef0fe4812f0e84cc264946f73e225660 [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 :=
Johnny Chen64a7e742012-01-17 00:58:08 +000019#
20# And test/functionalities/archives/Makefile:
21# MAKE_DSYM := NO
22# ARCHIVE_NAME := libfoo.a
23# ARCHIVE_C_SOURCES := a.c b.c
Johnny Chen9bc867a2010-08-23 23:56:08 +000024
25# Uncomment line below for debugging shell commands
26# SHELL = /bin/sh -x
27
28#----------------------------------------------------------------------
Johnny Chen6c17c082010-09-16 20:54:06 +000029# If ARCH is not defined, default to x86_64.
Peter Collingbourne518f03d2011-06-20 19:06:04 +000030# If OS is not defined, use 'uname -s' to determine the OS name.
Johnny Chen6c17c082010-09-16 20:54:06 +000031#----------------------------------------------------------------------
32ifeq "$(ARCH)" ""
33 ARCH = x86_64
34endif
35
Peter Collingbourne518f03d2011-06-20 19:06:04 +000036ifeq "$(OS)" ""
37 OS = $(shell uname -s)
38endif
39
Johnny Chen6c17c082010-09-16 20:54:06 +000040#----------------------------------------------------------------------
Johnny Chen597cbbb2011-08-23 21:54:10 +000041# CC defaults to clang.
Johnny Chen4ab4a9b2011-08-24 18:12:53 +000042#
43# If you change the defaults of CC, be sure to also change it in the file
44# test/plugins/builder_base.py, which provides a Python way to return the
45# value of the make variable CC -- getCompiler().
46#
Johnny Chen6055b442011-01-14 20:55:13 +000047# See also these functions:
48# o cxx_compiler
49# o cxx_linker
Johnny Chend43e2082011-01-14 20:46:49 +000050#----------------------------------------------------------------------
Johnny Chen597cbbb2011-08-23 21:54:10 +000051CC ?= clang
Johnny Chend43e2082011-01-14 20:46:49 +000052ifeq "$(CC)" "cc"
Johnny Chen597cbbb2011-08-23 21:54:10 +000053 CC = clang
Johnny Chend43e2082011-01-14 20:46:49 +000054endif
55
56#----------------------------------------------------------------------
Daniel Malea2745d842013-01-25 00:31:48 +000057# ARCHFLAG is the flag used to tell the compiler which architecture
58# to compile for. The default is the flag that clang accepts.
59#----------------------------------------------------------------------
60ARCHFLAG ?= -arch
61
62#----------------------------------------------------------------------
Johnny Chen9bc867a2010-08-23 23:56:08 +000063# Change any build/tool options needed
64#----------------------------------------------------------------------
Peter Collingbourne518f03d2011-06-20 19:06:04 +000065ifeq "$(OS)" "Darwin"
Greg Clayton3bffb082011-12-10 02:15:28 +000066 DS := dsymutil
Peter Collingbourne518f03d2011-06-20 19:06:04 +000067 DSFLAGS =
68 DSYM = $(EXE).dSYM
Johnny Chenfe141622012-01-12 23:09:42 +000069 AR := libtool
70 ARFLAGS := -static -o
Daniel Malea2745d842013-01-25 00:31:48 +000071else
72 # On non-Apple platforms, -arch becomes -m
73 ARCHFLAG := -m
74
75 # i386 becomes 32, and x86_64 becomes 64
76 ifeq "$(ARCH)" "x86_64"
77 override ARCH := $(subst x86_64,64,$(ARCH))
78 endif
79 ifeq "$(ARCH)" "i386"
80 override ARCH := $(subst i386,32,$(ARCH))
81 endif
Peter Collingbourne518f03d2011-06-20 19:06:04 +000082endif
83
Greg Claytone595c6b2013-02-28 18:47:39 +000084CFLAGS ?= -g -O0
Daniel Malea2745d842013-01-25 00:31:48 +000085CFLAGS += $(ARCHFLAG)$(ARCH) $(FRAMEWORK_INCLUDES) $(CFLAGS_EXTRAS)
86
Johnny Chen53e2edb2011-08-09 20:07:16 +000087CXXFLAGS +=$(CFLAGS)
88LD = $(CC)
89LDFLAGS ?= $(CFLAGS)
Daniel Malea2745d842013-01-25 00:31:48 +000090LDFLAGS += $(LD_EXTRAS)
Johnny Chen53e2edb2011-08-09 20:07:16 +000091OBJECTS =
92EXE ?= a.out
93
Daniel Malea2745d842013-01-25 00:31:48 +000094ifneq (,$(findstring g++,$(CXX)))
95 # GCC 4.6 cannot handle -std=c++11, so replace it with -std=c++0x
96 # instead. FIXME: remove once GCC version is upgraded.
97 override CXXFLAGS := $(subst -std=c++11,-std=c++0x,$(CXXFLAGS))
98endif
99
Peter Collingbourne518f03d2011-06-20 19:06:04 +0000100ifneq "$(DYLIB_NAME)" ""
101 ifeq "$(OS)" "Darwin"
102 DYLIB_FILENAME = lib$(DYLIB_NAME).dylib
103 else
104 DYLIB_FILENAME = lib$(DYLIB_NAME).so
105 endif
106endif
Johnny Chen9bc867a2010-08-23 23:56:08 +0000107
Johnny Chenbdb4efc2011-01-14 18:19:53 +0000108# Function that returns the counterpart C++ compiler, given $(CC) as arg.
Matt Kopecf99f0b82013-03-15 21:55:13 +0000109cxx_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 +0000110
111# Function that returns the C++ linker, given $(CC) as arg.
Matt Kopecf99f0b82013-03-15 21:55:13 +0000112cxx_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 +0000113
Johnny Chen9bc867a2010-08-23 23:56:08 +0000114#----------------------------------------------------------------------
115# dylib settings
116#----------------------------------------------------------------------
117ifneq "$(strip $(DYLIB_C_SOURCES))" ""
118 DYLIB_OBJECTS +=$(strip $(DYLIB_C_SOURCES:.c=.o))
119endif
120
Sean Callanan72772842012-02-22 23:57:45 +0000121ifneq "$(strip $(DYLIB_OBJC_SOURCES))" ""
122 DYLIB_OBJECTS +=$(strip $(DYLIB_OBJC_SOURCES:.m=.o))
123endif
124
Greg Claytond50d2382012-01-10 00:00:15 +0000125ifneq "$(strip $(DYLIB_CXX_SOURCES))" ""
126 DYLIB_OBJECTS +=$(strip $(DYLIB_CXX_SOURCES:.cpp=.o))
127 CXX = $(call cxx_compiler,$(CC))
128 LD = $(call cxx_linker,$(CC))
129endif
Johnny Chen9bc867a2010-08-23 23:56:08 +0000130
131#----------------------------------------------------------------------
132# Check if we have any C source files
133#----------------------------------------------------------------------
134ifneq "$(strip $(C_SOURCES))" ""
135 OBJECTS +=$(strip $(C_SOURCES:.c=.o))
136endif
137
138#----------------------------------------------------------------------
139# Check if we have any C++ source files
140#----------------------------------------------------------------------
141ifneq "$(strip $(CXX_SOURCES))" ""
142 OBJECTS +=$(strip $(CXX_SOURCES:.cpp=.o))
Johnny Chen832d2332010-09-30 01:22:34 +0000143 CXX = $(call cxx_compiler,$(CC))
Johnny Chenbdb4efc2011-01-14 18:19:53 +0000144 LD = $(call cxx_linker,$(CC))
Johnny Chen9bc867a2010-08-23 23:56:08 +0000145endif
146
147#----------------------------------------------------------------------
148# Check if we have any ObjC source files
149#----------------------------------------------------------------------
150ifneq "$(strip $(OBJC_SOURCES))" ""
151 OBJECTS +=$(strip $(OBJC_SOURCES:.m=.o))
152 LDFLAGS +=-lobjc
153endif
154
155#----------------------------------------------------------------------
156# Check if we have any ObjC++ source files
157#----------------------------------------------------------------------
158ifneq "$(strip $(OBJCXX_SOURCES))" ""
159 OBJECTS +=$(strip $(OBJCXX_SOURCES:.mm=.o))
Johnny Chen832d2332010-09-30 01:22:34 +0000160 CXX = $(call cxx_compiler,$(CC))
Johnny Chenbdb4efc2011-01-14 18:19:53 +0000161 LD = $(call cxx_linker,$(CC))
Johnny Chen8da4ddf2012-04-24 23:05:07 +0000162 ifeq "$(findstring lobjc,$(LDFLAGS))" ""
Johnny Chen9bc867a2010-08-23 23:56:08 +0000163 LDFLAGS +=-lobjc
164 endif
165endif
166
Johnny Chenfe141622012-01-12 23:09:42 +0000167#----------------------------------------------------------------------
168# Check if we have any C source files for archive
169#----------------------------------------------------------------------
170ifneq "$(strip $(ARCHIVE_C_SOURCES))" ""
171 ARCHIVE_OBJECTS +=$(strip $(ARCHIVE_C_SOURCES:.c=.o))
172endif
173
174#----------------------------------------------------------------------
175# Check if we have any C++ source files for archive
176#----------------------------------------------------------------------
177ifneq "$(strip $(ARCHIVE_CXX_SOURCES))" ""
178 ARCHIVE_OBJECTS +=$(strip $(ARCHIVE_CXX_SOURCES:.cpp=.o))
179 CXX = $(call cxx_compiler,$(CC))
180 LD = $(call cxx_linker,$(CC))
181endif
182
183#----------------------------------------------------------------------
184# Check if we have any ObjC source files for archive
185#----------------------------------------------------------------------
186ifneq "$(strip $(ARCHIVE_OBJC_SOURCES))" ""
187 ARCHIVE_OBJECTS +=$(strip $(ARCHIVE_OBJC_SOURCES:.m=.o))
188 LDFLAGS +=-lobjc
189endif
190
191#----------------------------------------------------------------------
192# Check if we have any ObjC++ source files for archive
193#----------------------------------------------------------------------
194ifneq "$(strip $(ARCHIVE_OBJCXX_SOURCES))" ""
195 ARCHIVE_OBJECTS +=$(strip $(ARCHIVE_OBJCXX_SOURCES:.mm=.o))
196 CXX = $(call cxx_compiler,$(CC))
197 LD = $(call cxx_linker,$(CC))
Johnny Chen8da4ddf2012-04-24 23:05:07 +0000198 ifeq "$(findstring lobjc,$(LDFLAGS))" ""
Johnny Chenfe141622012-01-12 23:09:42 +0000199 LDFLAGS +=-lobjc
200 endif
201endif
202
Johnny Chen9bc867a2010-08-23 23:56:08 +0000203
204#----------------------------------------------------------------------
Johnny Chenbeac8f92012-01-10 00:41:11 +0000205# DYLIB_ONLY variable can be used to skip the building of a.out.
206# See the sections below regarding dSYM file as well as the building of
207# EXE from all the objects.
208#----------------------------------------------------------------------
209
210#----------------------------------------------------------------------
Johnny Chen9bc867a2010-08-23 23:56:08 +0000211# Make the dSYM file from the executable if $(MAKE_DSYM) != "NO"
212#----------------------------------------------------------------------
Peter Collingbourne518f03d2011-06-20 19:06:04 +0000213ifeq "$(OS)" "Darwin"
Johnny Chen91016392011-06-20 20:08:26 +0000214ifneq "$(MAKE_DSYM)" "NO"
Johnny Chenbeac8f92012-01-10 00:41:11 +0000215ifeq "$(DYLIB_ONLY)" ""
Johnny Chen9bc867a2010-08-23 23:56:08 +0000216$(DSYM) : $(EXE)
217 $(DS) $(DSFLAGS) -o "$(DSYM)" "$(EXE)"
Johnny Chen91016392011-06-20 20:08:26 +0000218endif
Johnny Chen9bc867a2010-08-23 23:56:08 +0000219endif
Johnny Chenbeac8f92012-01-10 00:41:11 +0000220endif
Johnny Chen9bc867a2010-08-23 23:56:08 +0000221
222#----------------------------------------------------------------------
223# Compile the executable from all the objects.
224#----------------------------------------------------------------------
Johnny Chenbeac8f92012-01-10 00:41:11 +0000225ifneq "$(DYLIB_NAME)" ""
226ifeq "$(DYLIB_ONLY)" ""
Johnny Chenfe141622012-01-12 23:09:42 +0000227$(EXE) : $(OBJECTS) $(ARCHIVE_NAME) $(DYLIB_FILENAME)
228 $(LD) $(LDFLAGS) $(OBJECTS) $(ARCHIVE_NAME) -L. -l$(DYLIB_NAME) -o "$(EXE)"
Johnny Chenbeac8f92012-01-10 00:41:11 +0000229else
230EXE = $(DYLIB_FILENAME)
231endif
232else
Johnny Chenfe141622012-01-12 23:09:42 +0000233$(EXE) : $(OBJECTS) $(ARCHIVE_NAME)
Daniel Malea2745d842013-01-25 00:31:48 +0000234 $(LD) $(OBJECTS) $(LDFLAGS) $(ARCHIVE_NAME) -o "$(EXE)"
Johnny Chenfe141622012-01-12 23:09:42 +0000235endif
236
237#----------------------------------------------------------------------
238# Make the archive
239#----------------------------------------------------------------------
240ifneq "$(ARCHIVE_NAME)" ""
241ifeq "$(OS)" "Darwin"
242$(ARCHIVE_NAME) : $(ARCHIVE_OBJECTS)
243 $(AR) $(ARFLAGS) $(ARCHIVE_NAME) $(ARCHIVE_OBJECTS)
244 $(RM) $(ARCHIVE_OBJECTS)
245else
246$(ARCHIVE_NAME) : $(foreach ar_obj,$(ARCHIVE_OBJECTS),$(ARCHIVE_NAME)($(ar_obj)))
247endif
Johnny Chen9bc867a2010-08-23 23:56:08 +0000248endif
249
250#----------------------------------------------------------------------
251# Make the dylib
252#----------------------------------------------------------------------
Peter Collingbourne518f03d2011-06-20 19:06:04 +0000253$(DYLIB_FILENAME) : $(DYLIB_OBJECTS)
254ifeq "$(OS)" "Darwin"
255 $(LD) $(LDFLAGS) $(DYLIB_OBJECTS) -install_name "@executable_path/$(DYLIB_FILENAME)" -dynamiclib -o "$(DYLIB_FILENAME)"
Greg Clayton0c9773c2012-09-20 21:43:11 +0000256ifneq "$(MAKE_DSYM)" "NO"
257ifneq "$(DS)" ""
258 $(DS) $(DSFLAGS) "$(DYLIB_FILENAME)"
259endif
260endif
Peter Collingbourne518f03d2011-06-20 19:06:04 +0000261else
262 $(LD) $(LDFLAGS) $(DYLIB_OBJECTS) -shared -o "$(DYLIB_FILENAME)"
263endif
Johnny Chen9bc867a2010-08-23 23:56:08 +0000264
265#----------------------------------------------------------------------
266# Automatic variables based on items already entered. Below we create
267# an objects lists from the list of sources by replacing all entries
268# that end with .c with .o, and we also create a list of prerequisite
269# files by replacing all .c files with .d.
270#----------------------------------------------------------------------
271PREREQS := $(OBJECTS:.o=.d)
Johnny Chene3dc0f02010-08-24 16:35:00 +0000272ifneq "$(DYLIB_NAME)" ""
273 DYLIB_PREREQS := $(DYLIB_OBJECTS:.o=.d)
274endif
Johnny Chen9bc867a2010-08-23 23:56:08 +0000275
276#----------------------------------------------------------------------
277# Rule for Generating Prerequisites Automatically using .d files and
278# the compiler -MM option. The -M option will list all system headers,
279# and the -MM option will list all non-system dependencies.
280#----------------------------------------------------------------------
281%.d: %.c
282 @set -e; rm -f $@; \
Johnny Chen189bff12011-08-04 20:44:56 +0000283 $(CC) -M $(CFLAGS) $< > $@.$$$$; \
Johnny Chen9bc867a2010-08-23 23:56:08 +0000284 sed 's,\($*\)\.o[ :]*,\1.o $@ : ,g' < $@.$$$$ > $@; \
285 rm -f $@.$$$$
286
287%.d: %.cpp
288 @set -e; rm -f $@; \
Johnny Chen189bff12011-08-04 20:44:56 +0000289 $(CC) -M $(CXXFLAGS) $< > $@.$$$$; \
Johnny Chen9bc867a2010-08-23 23:56:08 +0000290 sed 's,\($*\)\.o[ :]*,\1.o $@ : ,g' < $@.$$$$ > $@; \
291 rm -f $@.$$$$
292
293%.d: %.m
294 @set -e; rm -f $@; \
Johnny Chen189bff12011-08-04 20:44:56 +0000295 $(CC) -M $(CFLAGS) $< > $@.$$$$; \
Johnny Chen9bc867a2010-08-23 23:56:08 +0000296 sed 's,\($*\)\.o[ :]*,\1.o $@ : ,g' < $@.$$$$ > $@; \
297 rm -f $@.$$$$
298
299%.d: %.mm
300 @set -e; rm -f $@; \
Johnny Chen189bff12011-08-04 20:44:56 +0000301 $(CC) -M $(CXXFLAGS) $< > $@.$$$$; \
Johnny Chen9bc867a2010-08-23 23:56:08 +0000302 sed 's,\($*\)\.o[ :]*,\1.o $@ : ,g' < $@.$$$$ > $@; \
303 rm -f $@.$$$$
304
305#----------------------------------------------------------------------
306# Include all of the makefiles for each source file so we don't have
307# to manually track all of the prerequisites for each source file.
308#----------------------------------------------------------------------
309sinclude $(PREREQS)
Johnny Chene3dc0f02010-08-24 16:35:00 +0000310ifneq "$(DYLIB_NAME)" ""
311 sinclude $(DYLIB_PREREQS)
312endif
Johnny Chen9bc867a2010-08-23 23:56:08 +0000313
Johnny Chen8da4ddf2012-04-24 23:05:07 +0000314# Define a suffix rule for .mm -> .o
315.SUFFIXES: .mm .o
316.mm.o:
317 $(CXX) $(CXXFLAGS) -c $<
318
Johnny Chen9bc867a2010-08-23 23:56:08 +0000319.PHONY: clean
320dsym: $(DSYM)
321all: $(EXE) $(DSYM)
Johnny Chene1030cd2010-09-27 20:44:46 +0000322clean::
Johnny Chen9bc867a2010-08-23 23:56:08 +0000323ifeq "$(DYLIB_NAME)" ""
Johnny Chenfe141622012-01-12 23:09:42 +0000324 rm -rf "$(EXE)" "$(DSYM)" $(OBJECTS) $(PREREQS) $(ARCHIVE_NAME) $(ARCHIVE_OBJECTS)
Johnny Chen9bc867a2010-08-23 23:56:08 +0000325else
Johnny Chenfe141622012-01-12 23:09:42 +0000326 rm -rf "$(EXE)" "$(DSYM)" $(OBJECTS) $(PREREQS) $(ARCHIVE_NAME) $(ARCHIVE_OBJECTS) $(DYLIB_OBJECTS) $(DYLIB_PREREQS) $(DYLIB_FILENAME) $(DYLIB_FILENAME).dSYM
Johnny Chen9bc867a2010-08-23 23:56:08 +0000327endif
Johnny Chenfa380412011-01-14 21:18:12 +0000328
329#----------------------------------------------------------------------
330# From http://blog.melski.net/tag/debugging-makefiles/
331#
332# Usage: make print-CC print-CXX print-LD
333#----------------------------------------------------------------------
334print-%:
335 @echo '$*=$($*)'
336 @echo ' origin = $(origin $*)'
337 @echo ' flavor = $(flavor $*)'
338 @echo ' value = $(value $*)'
Johnny Chen8b2c3212011-01-28 17:22:29 +0000339
340
341### Local Variables: ###
342### mode:makefile ###
343### End: ###