blob: 8c4288de0750737af265911fd1703191fbd34023 [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#----------------------------------------------------------------------
Daniel Maleac7ffa7a2013-06-05 19:32:34 +0000115# C++ standard library options
116#----------------------------------------------------------------------
117ifeq (1,$(USE_LIBSTDCPP))
118 # Clang requires an extra flag: -stdlib=libstdc++
119 ifneq (,$(findstring clang,$(CC)))
120 CFLAGS += -stdlib=libstdc++
121 LDFLAGS += -stdlib=libstdc++
122 endif
123endif
124
125#----------------------------------------------------------------------
Johnny Chen9bc867a2010-08-23 23:56:08 +0000126# dylib settings
127#----------------------------------------------------------------------
128ifneq "$(strip $(DYLIB_C_SOURCES))" ""
129 DYLIB_OBJECTS +=$(strip $(DYLIB_C_SOURCES:.c=.o))
130endif
131
Sean Callanan72772842012-02-22 23:57:45 +0000132ifneq "$(strip $(DYLIB_OBJC_SOURCES))" ""
133 DYLIB_OBJECTS +=$(strip $(DYLIB_OBJC_SOURCES:.m=.o))
134endif
135
Greg Claytond50d2382012-01-10 00:00:15 +0000136ifneq "$(strip $(DYLIB_CXX_SOURCES))" ""
137 DYLIB_OBJECTS +=$(strip $(DYLIB_CXX_SOURCES:.cpp=.o))
138 CXX = $(call cxx_compiler,$(CC))
139 LD = $(call cxx_linker,$(CC))
140endif
Johnny Chen9bc867a2010-08-23 23:56:08 +0000141
142#----------------------------------------------------------------------
143# Check if we have any C source files
144#----------------------------------------------------------------------
145ifneq "$(strip $(C_SOURCES))" ""
146 OBJECTS +=$(strip $(C_SOURCES:.c=.o))
147endif
148
149#----------------------------------------------------------------------
150# Check if we have any C++ source files
151#----------------------------------------------------------------------
152ifneq "$(strip $(CXX_SOURCES))" ""
153 OBJECTS +=$(strip $(CXX_SOURCES:.cpp=.o))
Johnny Chen832d2332010-09-30 01:22:34 +0000154 CXX = $(call cxx_compiler,$(CC))
Johnny Chenbdb4efc2011-01-14 18:19:53 +0000155 LD = $(call cxx_linker,$(CC))
Johnny Chen9bc867a2010-08-23 23:56:08 +0000156endif
157
158#----------------------------------------------------------------------
159# Check if we have any ObjC source files
160#----------------------------------------------------------------------
161ifneq "$(strip $(OBJC_SOURCES))" ""
162 OBJECTS +=$(strip $(OBJC_SOURCES:.m=.o))
163 LDFLAGS +=-lobjc
164endif
165
166#----------------------------------------------------------------------
167# Check if we have any ObjC++ source files
168#----------------------------------------------------------------------
169ifneq "$(strip $(OBJCXX_SOURCES))" ""
170 OBJECTS +=$(strip $(OBJCXX_SOURCES:.mm=.o))
Johnny Chen832d2332010-09-30 01:22:34 +0000171 CXX = $(call cxx_compiler,$(CC))
Johnny Chenbdb4efc2011-01-14 18:19:53 +0000172 LD = $(call cxx_linker,$(CC))
Johnny Chen8da4ddf2012-04-24 23:05:07 +0000173 ifeq "$(findstring lobjc,$(LDFLAGS))" ""
Johnny Chen9bc867a2010-08-23 23:56:08 +0000174 LDFLAGS +=-lobjc
175 endif
176endif
177
Johnny Chenfe141622012-01-12 23:09:42 +0000178#----------------------------------------------------------------------
179# Check if we have any C source files for archive
180#----------------------------------------------------------------------
181ifneq "$(strip $(ARCHIVE_C_SOURCES))" ""
182 ARCHIVE_OBJECTS +=$(strip $(ARCHIVE_C_SOURCES:.c=.o))
183endif
184
185#----------------------------------------------------------------------
186# Check if we have any C++ source files for archive
187#----------------------------------------------------------------------
188ifneq "$(strip $(ARCHIVE_CXX_SOURCES))" ""
189 ARCHIVE_OBJECTS +=$(strip $(ARCHIVE_CXX_SOURCES:.cpp=.o))
190 CXX = $(call cxx_compiler,$(CC))
191 LD = $(call cxx_linker,$(CC))
192endif
193
194#----------------------------------------------------------------------
195# Check if we have any ObjC source files for archive
196#----------------------------------------------------------------------
197ifneq "$(strip $(ARCHIVE_OBJC_SOURCES))" ""
198 ARCHIVE_OBJECTS +=$(strip $(ARCHIVE_OBJC_SOURCES:.m=.o))
199 LDFLAGS +=-lobjc
200endif
201
202#----------------------------------------------------------------------
203# Check if we have any ObjC++ source files for archive
204#----------------------------------------------------------------------
205ifneq "$(strip $(ARCHIVE_OBJCXX_SOURCES))" ""
206 ARCHIVE_OBJECTS +=$(strip $(ARCHIVE_OBJCXX_SOURCES:.mm=.o))
207 CXX = $(call cxx_compiler,$(CC))
208 LD = $(call cxx_linker,$(CC))
Johnny Chen8da4ddf2012-04-24 23:05:07 +0000209 ifeq "$(findstring lobjc,$(LDFLAGS))" ""
Johnny Chenfe141622012-01-12 23:09:42 +0000210 LDFLAGS +=-lobjc
211 endif
212endif
213
Johnny Chen9bc867a2010-08-23 23:56:08 +0000214
215#----------------------------------------------------------------------
Johnny Chenbeac8f92012-01-10 00:41:11 +0000216# DYLIB_ONLY variable can be used to skip the building of a.out.
217# See the sections below regarding dSYM file as well as the building of
218# EXE from all the objects.
219#----------------------------------------------------------------------
220
221#----------------------------------------------------------------------
Johnny Chen9bc867a2010-08-23 23:56:08 +0000222# Make the dSYM file from the executable if $(MAKE_DSYM) != "NO"
223#----------------------------------------------------------------------
Peter Collingbourne518f03d2011-06-20 19:06:04 +0000224ifeq "$(OS)" "Darwin"
Johnny Chen91016392011-06-20 20:08:26 +0000225ifneq "$(MAKE_DSYM)" "NO"
Johnny Chenbeac8f92012-01-10 00:41:11 +0000226ifeq "$(DYLIB_ONLY)" ""
Johnny Chen9bc867a2010-08-23 23:56:08 +0000227$(DSYM) : $(EXE)
228 $(DS) $(DSFLAGS) -o "$(DSYM)" "$(EXE)"
Johnny Chen91016392011-06-20 20:08:26 +0000229endif
Johnny Chen9bc867a2010-08-23 23:56:08 +0000230endif
Johnny Chenbeac8f92012-01-10 00:41:11 +0000231endif
Johnny Chen9bc867a2010-08-23 23:56:08 +0000232
233#----------------------------------------------------------------------
234# Compile the executable from all the objects.
235#----------------------------------------------------------------------
Johnny Chenbeac8f92012-01-10 00:41:11 +0000236ifneq "$(DYLIB_NAME)" ""
237ifeq "$(DYLIB_ONLY)" ""
Johnny Chenfe141622012-01-12 23:09:42 +0000238$(EXE) : $(OBJECTS) $(ARCHIVE_NAME) $(DYLIB_FILENAME)
239 $(LD) $(LDFLAGS) $(OBJECTS) $(ARCHIVE_NAME) -L. -l$(DYLIB_NAME) -o "$(EXE)"
Johnny Chenbeac8f92012-01-10 00:41:11 +0000240else
241EXE = $(DYLIB_FILENAME)
242endif
243else
Johnny Chenfe141622012-01-12 23:09:42 +0000244$(EXE) : $(OBJECTS) $(ARCHIVE_NAME)
Daniel Malea2745d842013-01-25 00:31:48 +0000245 $(LD) $(OBJECTS) $(LDFLAGS) $(ARCHIVE_NAME) -o "$(EXE)"
Johnny Chenfe141622012-01-12 23:09:42 +0000246endif
247
248#----------------------------------------------------------------------
249# Make the archive
250#----------------------------------------------------------------------
251ifneq "$(ARCHIVE_NAME)" ""
252ifeq "$(OS)" "Darwin"
253$(ARCHIVE_NAME) : $(ARCHIVE_OBJECTS)
254 $(AR) $(ARFLAGS) $(ARCHIVE_NAME) $(ARCHIVE_OBJECTS)
255 $(RM) $(ARCHIVE_OBJECTS)
256else
257$(ARCHIVE_NAME) : $(foreach ar_obj,$(ARCHIVE_OBJECTS),$(ARCHIVE_NAME)($(ar_obj)))
258endif
Johnny Chen9bc867a2010-08-23 23:56:08 +0000259endif
260
261#----------------------------------------------------------------------
262# Make the dylib
263#----------------------------------------------------------------------
Peter Collingbourne518f03d2011-06-20 19:06:04 +0000264$(DYLIB_FILENAME) : $(DYLIB_OBJECTS)
265ifeq "$(OS)" "Darwin"
266 $(LD) $(LDFLAGS) $(DYLIB_OBJECTS) -install_name "@executable_path/$(DYLIB_FILENAME)" -dynamiclib -o "$(DYLIB_FILENAME)"
Greg Clayton0c9773c2012-09-20 21:43:11 +0000267ifneq "$(MAKE_DSYM)" "NO"
268ifneq "$(DS)" ""
269 $(DS) $(DSFLAGS) "$(DYLIB_FILENAME)"
270endif
271endif
Peter Collingbourne518f03d2011-06-20 19:06:04 +0000272else
273 $(LD) $(LDFLAGS) $(DYLIB_OBJECTS) -shared -o "$(DYLIB_FILENAME)"
274endif
Johnny Chen9bc867a2010-08-23 23:56:08 +0000275
276#----------------------------------------------------------------------
277# Automatic variables based on items already entered. Below we create
278# an objects lists from the list of sources by replacing all entries
279# that end with .c with .o, and we also create a list of prerequisite
280# files by replacing all .c files with .d.
281#----------------------------------------------------------------------
282PREREQS := $(OBJECTS:.o=.d)
Johnny Chene3dc0f02010-08-24 16:35:00 +0000283ifneq "$(DYLIB_NAME)" ""
284 DYLIB_PREREQS := $(DYLIB_OBJECTS:.o=.d)
285endif
Johnny Chen9bc867a2010-08-23 23:56:08 +0000286
287#----------------------------------------------------------------------
288# Rule for Generating Prerequisites Automatically using .d files and
289# the compiler -MM option. The -M option will list all system headers,
290# and the -MM option will list all non-system dependencies.
291#----------------------------------------------------------------------
292%.d: %.c
293 @set -e; rm -f $@; \
Johnny Chen189bff12011-08-04 20:44:56 +0000294 $(CC) -M $(CFLAGS) $< > $@.$$$$; \
Johnny Chen9bc867a2010-08-23 23:56:08 +0000295 sed 's,\($*\)\.o[ :]*,\1.o $@ : ,g' < $@.$$$$ > $@; \
296 rm -f $@.$$$$
297
298%.d: %.cpp
299 @set -e; rm -f $@; \
Johnny Chen189bff12011-08-04 20:44:56 +0000300 $(CC) -M $(CXXFLAGS) $< > $@.$$$$; \
Johnny Chen9bc867a2010-08-23 23:56:08 +0000301 sed 's,\($*\)\.o[ :]*,\1.o $@ : ,g' < $@.$$$$ > $@; \
302 rm -f $@.$$$$
303
304%.d: %.m
305 @set -e; rm -f $@; \
Johnny Chen189bff12011-08-04 20:44:56 +0000306 $(CC) -M $(CFLAGS) $< > $@.$$$$; \
Johnny Chen9bc867a2010-08-23 23:56:08 +0000307 sed 's,\($*\)\.o[ :]*,\1.o $@ : ,g' < $@.$$$$ > $@; \
308 rm -f $@.$$$$
309
310%.d: %.mm
311 @set -e; rm -f $@; \
Johnny Chen189bff12011-08-04 20:44:56 +0000312 $(CC) -M $(CXXFLAGS) $< > $@.$$$$; \
Johnny Chen9bc867a2010-08-23 23:56:08 +0000313 sed 's,\($*\)\.o[ :]*,\1.o $@ : ,g' < $@.$$$$ > $@; \
314 rm -f $@.$$$$
315
316#----------------------------------------------------------------------
317# Include all of the makefiles for each source file so we don't have
318# to manually track all of the prerequisites for each source file.
319#----------------------------------------------------------------------
320sinclude $(PREREQS)
Johnny Chene3dc0f02010-08-24 16:35:00 +0000321ifneq "$(DYLIB_NAME)" ""
322 sinclude $(DYLIB_PREREQS)
323endif
Johnny Chen9bc867a2010-08-23 23:56:08 +0000324
Johnny Chen8da4ddf2012-04-24 23:05:07 +0000325# Define a suffix rule for .mm -> .o
326.SUFFIXES: .mm .o
327.mm.o:
328 $(CXX) $(CXXFLAGS) -c $<
329
Johnny Chen9bc867a2010-08-23 23:56:08 +0000330.PHONY: clean
331dsym: $(DSYM)
332all: $(EXE) $(DSYM)
Johnny Chene1030cd2010-09-27 20:44:46 +0000333clean::
Johnny Chen9bc867a2010-08-23 23:56:08 +0000334ifeq "$(DYLIB_NAME)" ""
Johnny Chenfe141622012-01-12 23:09:42 +0000335 rm -rf "$(EXE)" "$(DSYM)" $(OBJECTS) $(PREREQS) $(ARCHIVE_NAME) $(ARCHIVE_OBJECTS)
Johnny Chen9bc867a2010-08-23 23:56:08 +0000336else
Johnny Chenfe141622012-01-12 23:09:42 +0000337 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 +0000338endif
Johnny Chenfa380412011-01-14 21:18:12 +0000339
340#----------------------------------------------------------------------
341# From http://blog.melski.net/tag/debugging-makefiles/
342#
343# Usage: make print-CC print-CXX print-LD
344#----------------------------------------------------------------------
345print-%:
346 @echo '$*=$($*)'
347 @echo ' origin = $(origin $*)'
348 @echo ' flavor = $(flavor $*)'
349 @echo ' value = $(value $*)'
Johnny Chen8b2c3212011-01-28 17:22:29 +0000350
351
352### Local Variables: ###
353### mode:makefile ###
354### End: ###