blob: 2d0621a49cca685500f790a8e6278dca47d51e8a [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#----------------------------------------------------------------------
Johnny Chen9bc867a2010-08-23 23:56:08 +000057# Change any build/tool options needed
58#----------------------------------------------------------------------
Peter Collingbourne518f03d2011-06-20 19:06:04 +000059CFLAGS ?= -gdwarf-2 -O0
Johnny Chenbeac8f92012-01-10 00:41:11 +000060CFLAGS += $(FRAMEWORK_INCLUDES) $(CFLAGS_EXTRAS)
Peter Collingbourne518f03d2011-06-20 19:06:04 +000061ifeq "$(OS)" "Darwin"
62 CFLAGS += -arch $(ARCH)
Greg Clayton3bffb082011-12-10 02:15:28 +000063 DS := dsymutil
Peter Collingbourne518f03d2011-06-20 19:06:04 +000064 DSFLAGS =
65 DSYM = $(EXE).dSYM
Johnny Chenfe141622012-01-12 23:09:42 +000066 AR := libtool
67 ARFLAGS := -static -o
Peter Collingbourne518f03d2011-06-20 19:06:04 +000068endif
69
Johnny Chen53e2edb2011-08-09 20:07:16 +000070CXXFLAGS +=$(CFLAGS)
71LD = $(CC)
72LDFLAGS ?= $(CFLAGS)
Johnny Chen45872c02012-04-13 00:13:35 +000073LDFLAGS += $(LD_EXTRAS) -arch $(ARCH)
Johnny Chen53e2edb2011-08-09 20:07:16 +000074OBJECTS =
75EXE ?= a.out
76
Peter Collingbourne518f03d2011-06-20 19:06:04 +000077ifneq "$(DYLIB_NAME)" ""
78 ifeq "$(OS)" "Darwin"
79 DYLIB_FILENAME = lib$(DYLIB_NAME).dylib
80 else
81 DYLIB_FILENAME = lib$(DYLIB_NAME).so
82 endif
83endif
Johnny Chen9bc867a2010-08-23 23:56:08 +000084
Johnny Chenbdb4efc2011-01-14 18:19:53 +000085# Function that returns the counterpart C++ compiler, given $(CC) as arg.
86cxx_compiler = $(if $(findstring clang,$(1)), $(subst clang,clang++,$(1)), $(if $(findstring llvm-gcc,$(1)), $(subst llvm-gcc,llvm-g++,$(1)), $(subst gcc,g++,$(1))))
87
88# Function that returns the C++ linker, given $(CC) as arg.
Greg Claytond50d2382012-01-10 00:00:15 +000089cxx_linker = $(if $(findstring clang,$(1)), $(subst clang,clang++,$(1)), $(if $(findstring llvm-gcc,$(1)), $(subst llvm-gcc,llvm-g++,$(1)), $(subst gcc,g++,$(1))))
Johnny Chen832d2332010-09-30 01:22:34 +000090
Johnny Chen9bc867a2010-08-23 23:56:08 +000091#----------------------------------------------------------------------
92# dylib settings
93#----------------------------------------------------------------------
94ifneq "$(strip $(DYLIB_C_SOURCES))" ""
95 DYLIB_OBJECTS +=$(strip $(DYLIB_C_SOURCES:.c=.o))
96endif
97
Sean Callanan72772842012-02-22 23:57:45 +000098ifneq "$(strip $(DYLIB_OBJC_SOURCES))" ""
99 DYLIB_OBJECTS +=$(strip $(DYLIB_OBJC_SOURCES:.m=.o))
100endif
101
Greg Claytond50d2382012-01-10 00:00:15 +0000102ifneq "$(strip $(DYLIB_CXX_SOURCES))" ""
103 DYLIB_OBJECTS +=$(strip $(DYLIB_CXX_SOURCES:.cpp=.o))
104 CXX = $(call cxx_compiler,$(CC))
105 LD = $(call cxx_linker,$(CC))
106endif
Johnny Chen9bc867a2010-08-23 23:56:08 +0000107
108#----------------------------------------------------------------------
109# Check if we have any C source files
110#----------------------------------------------------------------------
111ifneq "$(strip $(C_SOURCES))" ""
112 OBJECTS +=$(strip $(C_SOURCES:.c=.o))
113endif
114
115#----------------------------------------------------------------------
116# Check if we have any C++ source files
117#----------------------------------------------------------------------
118ifneq "$(strip $(CXX_SOURCES))" ""
119 OBJECTS +=$(strip $(CXX_SOURCES:.cpp=.o))
Johnny Chen832d2332010-09-30 01:22:34 +0000120 CXX = $(call cxx_compiler,$(CC))
Johnny Chenbdb4efc2011-01-14 18:19:53 +0000121 LD = $(call cxx_linker,$(CC))
Johnny Chen9bc867a2010-08-23 23:56:08 +0000122endif
123
124#----------------------------------------------------------------------
125# Check if we have any ObjC source files
126#----------------------------------------------------------------------
127ifneq "$(strip $(OBJC_SOURCES))" ""
128 OBJECTS +=$(strip $(OBJC_SOURCES:.m=.o))
129 LDFLAGS +=-lobjc
130endif
131
132#----------------------------------------------------------------------
133# Check if we have any ObjC++ source files
134#----------------------------------------------------------------------
135ifneq "$(strip $(OBJCXX_SOURCES))" ""
136 OBJECTS +=$(strip $(OBJCXX_SOURCES:.mm=.o))
Johnny Chen832d2332010-09-30 01:22:34 +0000137 CXX = $(call cxx_compiler,$(CC))
Johnny Chenbdb4efc2011-01-14 18:19:53 +0000138 LD = $(call cxx_linker,$(CC))
Johnny Chen8da4ddf2012-04-24 23:05:07 +0000139 ifeq "$(findstring lobjc,$(LDFLAGS))" ""
Johnny Chen9bc867a2010-08-23 23:56:08 +0000140 LDFLAGS +=-lobjc
141 endif
142endif
143
Johnny Chenfe141622012-01-12 23:09:42 +0000144#----------------------------------------------------------------------
145# Check if we have any C source files for archive
146#----------------------------------------------------------------------
147ifneq "$(strip $(ARCHIVE_C_SOURCES))" ""
148 ARCHIVE_OBJECTS +=$(strip $(ARCHIVE_C_SOURCES:.c=.o))
149endif
150
151#----------------------------------------------------------------------
152# Check if we have any C++ source files for archive
153#----------------------------------------------------------------------
154ifneq "$(strip $(ARCHIVE_CXX_SOURCES))" ""
155 ARCHIVE_OBJECTS +=$(strip $(ARCHIVE_CXX_SOURCES:.cpp=.o))
156 CXX = $(call cxx_compiler,$(CC))
157 LD = $(call cxx_linker,$(CC))
158endif
159
160#----------------------------------------------------------------------
161# Check if we have any ObjC source files for archive
162#----------------------------------------------------------------------
163ifneq "$(strip $(ARCHIVE_OBJC_SOURCES))" ""
164 ARCHIVE_OBJECTS +=$(strip $(ARCHIVE_OBJC_SOURCES:.m=.o))
165 LDFLAGS +=-lobjc
166endif
167
168#----------------------------------------------------------------------
169# Check if we have any ObjC++ source files for archive
170#----------------------------------------------------------------------
171ifneq "$(strip $(ARCHIVE_OBJCXX_SOURCES))" ""
172 ARCHIVE_OBJECTS +=$(strip $(ARCHIVE_OBJCXX_SOURCES:.mm=.o))
173 CXX = $(call cxx_compiler,$(CC))
174 LD = $(call cxx_linker,$(CC))
Johnny Chen8da4ddf2012-04-24 23:05:07 +0000175 ifeq "$(findstring lobjc,$(LDFLAGS))" ""
Johnny Chenfe141622012-01-12 23:09:42 +0000176 LDFLAGS +=-lobjc
177 endif
178endif
179
Johnny Chen9bc867a2010-08-23 23:56:08 +0000180
181#----------------------------------------------------------------------
Johnny Chenbeac8f92012-01-10 00:41:11 +0000182# DYLIB_ONLY variable can be used to skip the building of a.out.
183# See the sections below regarding dSYM file as well as the building of
184# EXE from all the objects.
185#----------------------------------------------------------------------
186
187#----------------------------------------------------------------------
Johnny Chen9bc867a2010-08-23 23:56:08 +0000188# Make the dSYM file from the executable if $(MAKE_DSYM) != "NO"
189#----------------------------------------------------------------------
Peter Collingbourne518f03d2011-06-20 19:06:04 +0000190ifeq "$(OS)" "Darwin"
Johnny Chen91016392011-06-20 20:08:26 +0000191ifneq "$(MAKE_DSYM)" "NO"
Johnny Chenbeac8f92012-01-10 00:41:11 +0000192ifeq "$(DYLIB_ONLY)" ""
Johnny Chen9bc867a2010-08-23 23:56:08 +0000193$(DSYM) : $(EXE)
194 $(DS) $(DSFLAGS) -o "$(DSYM)" "$(EXE)"
Johnny Chen91016392011-06-20 20:08:26 +0000195endif
Johnny Chen9bc867a2010-08-23 23:56:08 +0000196endif
Johnny Chenbeac8f92012-01-10 00:41:11 +0000197endif
Johnny Chen9bc867a2010-08-23 23:56:08 +0000198
199#----------------------------------------------------------------------
200# Compile the executable from all the objects.
201#----------------------------------------------------------------------
Johnny Chenbeac8f92012-01-10 00:41:11 +0000202ifneq "$(DYLIB_NAME)" ""
203ifeq "$(DYLIB_ONLY)" ""
Johnny Chenfe141622012-01-12 23:09:42 +0000204$(EXE) : $(OBJECTS) $(ARCHIVE_NAME) $(DYLIB_FILENAME)
205 $(LD) $(LDFLAGS) $(OBJECTS) $(ARCHIVE_NAME) -L. -l$(DYLIB_NAME) -o "$(EXE)"
Johnny Chenbeac8f92012-01-10 00:41:11 +0000206else
207EXE = $(DYLIB_FILENAME)
208endif
209else
Johnny Chenfe141622012-01-12 23:09:42 +0000210$(EXE) : $(OBJECTS) $(ARCHIVE_NAME)
211 $(LD) $(LDFLAGS) $(OBJECTS) $(ARCHIVE_NAME) -o "$(EXE)"
212endif
213
214#----------------------------------------------------------------------
215# Make the archive
216#----------------------------------------------------------------------
217ifneq "$(ARCHIVE_NAME)" ""
218ifeq "$(OS)" "Darwin"
219$(ARCHIVE_NAME) : $(ARCHIVE_OBJECTS)
220 $(AR) $(ARFLAGS) $(ARCHIVE_NAME) $(ARCHIVE_OBJECTS)
221 $(RM) $(ARCHIVE_OBJECTS)
222else
223$(ARCHIVE_NAME) : $(foreach ar_obj,$(ARCHIVE_OBJECTS),$(ARCHIVE_NAME)($(ar_obj)))
224endif
Johnny Chen9bc867a2010-08-23 23:56:08 +0000225endif
226
227#----------------------------------------------------------------------
228# Make the dylib
229#----------------------------------------------------------------------
Peter Collingbourne518f03d2011-06-20 19:06:04 +0000230$(DYLIB_FILENAME) : $(DYLIB_OBJECTS)
231ifeq "$(OS)" "Darwin"
232 $(LD) $(LDFLAGS) $(DYLIB_OBJECTS) -install_name "@executable_path/$(DYLIB_FILENAME)" -dynamiclib -o "$(DYLIB_FILENAME)"
233else
234 $(LD) $(LDFLAGS) $(DYLIB_OBJECTS) -shared -o "$(DYLIB_FILENAME)"
235endif
Johnny Chen9bc867a2010-08-23 23:56:08 +0000236
237#----------------------------------------------------------------------
238# Automatic variables based on items already entered. Below we create
239# an objects lists from the list of sources by replacing all entries
240# that end with .c with .o, and we also create a list of prerequisite
241# files by replacing all .c files with .d.
242#----------------------------------------------------------------------
243PREREQS := $(OBJECTS:.o=.d)
Johnny Chene3dc0f02010-08-24 16:35:00 +0000244ifneq "$(DYLIB_NAME)" ""
245 DYLIB_PREREQS := $(DYLIB_OBJECTS:.o=.d)
246endif
Johnny Chen9bc867a2010-08-23 23:56:08 +0000247
248#----------------------------------------------------------------------
249# Rule for Generating Prerequisites Automatically using .d files and
250# the compiler -MM option. The -M option will list all system headers,
251# and the -MM option will list all non-system dependencies.
252#----------------------------------------------------------------------
253%.d: %.c
254 @set -e; rm -f $@; \
Johnny Chen189bff12011-08-04 20:44:56 +0000255 $(CC) -M $(CFLAGS) $< > $@.$$$$; \
Johnny Chen9bc867a2010-08-23 23:56:08 +0000256 sed 's,\($*\)\.o[ :]*,\1.o $@ : ,g' < $@.$$$$ > $@; \
257 rm -f $@.$$$$
258
259%.d: %.cpp
260 @set -e; rm -f $@; \
Johnny Chen189bff12011-08-04 20:44:56 +0000261 $(CC) -M $(CXXFLAGS) $< > $@.$$$$; \
Johnny Chen9bc867a2010-08-23 23:56:08 +0000262 sed 's,\($*\)\.o[ :]*,\1.o $@ : ,g' < $@.$$$$ > $@; \
263 rm -f $@.$$$$
264
265%.d: %.m
266 @set -e; rm -f $@; \
Johnny Chen189bff12011-08-04 20:44:56 +0000267 $(CC) -M $(CFLAGS) $< > $@.$$$$; \
Johnny Chen9bc867a2010-08-23 23:56:08 +0000268 sed 's,\($*\)\.o[ :]*,\1.o $@ : ,g' < $@.$$$$ > $@; \
269 rm -f $@.$$$$
270
271%.d: %.mm
272 @set -e; rm -f $@; \
Johnny Chen189bff12011-08-04 20:44:56 +0000273 $(CC) -M $(CXXFLAGS) $< > $@.$$$$; \
Johnny Chen9bc867a2010-08-23 23:56:08 +0000274 sed 's,\($*\)\.o[ :]*,\1.o $@ : ,g' < $@.$$$$ > $@; \
275 rm -f $@.$$$$
276
277#----------------------------------------------------------------------
278# Include all of the makefiles for each source file so we don't have
279# to manually track all of the prerequisites for each source file.
280#----------------------------------------------------------------------
281sinclude $(PREREQS)
Johnny Chene3dc0f02010-08-24 16:35:00 +0000282ifneq "$(DYLIB_NAME)" ""
283 sinclude $(DYLIB_PREREQS)
284endif
Johnny Chen9bc867a2010-08-23 23:56:08 +0000285
Johnny Chen8da4ddf2012-04-24 23:05:07 +0000286# Define a suffix rule for .mm -> .o
287.SUFFIXES: .mm .o
288.mm.o:
289 $(CXX) $(CXXFLAGS) -c $<
290
Johnny Chen9bc867a2010-08-23 23:56:08 +0000291.PHONY: clean
292dsym: $(DSYM)
293all: $(EXE) $(DSYM)
Johnny Chene1030cd2010-09-27 20:44:46 +0000294clean::
Johnny Chen9bc867a2010-08-23 23:56:08 +0000295ifeq "$(DYLIB_NAME)" ""
Johnny Chenfe141622012-01-12 23:09:42 +0000296 rm -rf "$(EXE)" "$(DSYM)" $(OBJECTS) $(PREREQS) $(ARCHIVE_NAME) $(ARCHIVE_OBJECTS)
Johnny Chen9bc867a2010-08-23 23:56:08 +0000297else
Johnny Chenfe141622012-01-12 23:09:42 +0000298 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 +0000299endif
Johnny Chenfa380412011-01-14 21:18:12 +0000300
301#----------------------------------------------------------------------
302# From http://blog.melski.net/tag/debugging-makefiles/
303#
304# Usage: make print-CC print-CXX print-LD
305#----------------------------------------------------------------------
306print-%:
307 @echo '$*=$($*)'
308 @echo ' origin = $(origin $*)'
309 @echo ' flavor = $(flavor $*)'
310 @echo ' value = $(value $*)'
Johnny Chen8b2c3212011-01-28 17:22:29 +0000311
312
313### Local Variables: ###
314### mode:makefile ###
315### End: ###