blob: 50efad349d2c78e7583f42eba81e886d19ee8aa0 [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 Chen4876b5f2012-01-11 01:59:55 +000015# And also might be of interest:
16# FRAMEWORK_INCLUDES (Darwin only) :=
17# CFLAGS_EXTRAS :=
18# LD_EXTRAS :=
Johnny Chen9bc867a2010-08-23 23:56:08 +000019
20# Uncomment line below for debugging shell commands
21# SHELL = /bin/sh -x
22
23#----------------------------------------------------------------------
Johnny Chen6c17c082010-09-16 20:54:06 +000024# If ARCH is not defined, default to x86_64.
Peter Collingbourne518f03d2011-06-20 19:06:04 +000025# If OS is not defined, use 'uname -s' to determine the OS name.
Johnny Chen6c17c082010-09-16 20:54:06 +000026#----------------------------------------------------------------------
27ifeq "$(ARCH)" ""
28 ARCH = x86_64
29endif
30
Peter Collingbourne518f03d2011-06-20 19:06:04 +000031ifeq "$(OS)" ""
32 OS = $(shell uname -s)
33endif
34
Johnny Chen6c17c082010-09-16 20:54:06 +000035#----------------------------------------------------------------------
Johnny Chen597cbbb2011-08-23 21:54:10 +000036# CC defaults to clang.
Johnny Chen4ab4a9b2011-08-24 18:12:53 +000037#
38# If you change the defaults of CC, be sure to also change it in the file
39# test/plugins/builder_base.py, which provides a Python way to return the
40# value of the make variable CC -- getCompiler().
41#
Johnny Chen6055b442011-01-14 20:55:13 +000042# See also these functions:
43# o cxx_compiler
44# o cxx_linker
Johnny Chend43e2082011-01-14 20:46:49 +000045#----------------------------------------------------------------------
Johnny Chen597cbbb2011-08-23 21:54:10 +000046CC ?= clang
Johnny Chend43e2082011-01-14 20:46:49 +000047ifeq "$(CC)" "cc"
Johnny Chen597cbbb2011-08-23 21:54:10 +000048 CC = clang
Johnny Chend43e2082011-01-14 20:46:49 +000049endif
50
51#----------------------------------------------------------------------
Johnny Chen9bc867a2010-08-23 23:56:08 +000052# Change any build/tool options needed
53#----------------------------------------------------------------------
Peter Collingbourne518f03d2011-06-20 19:06:04 +000054CFLAGS ?= -gdwarf-2 -O0
Johnny Chenbeac8f92012-01-10 00:41:11 +000055CFLAGS += $(FRAMEWORK_INCLUDES) $(CFLAGS_EXTRAS)
Peter Collingbourne518f03d2011-06-20 19:06:04 +000056ifeq "$(OS)" "Darwin"
57 CFLAGS += -arch $(ARCH)
Greg Clayton3bffb082011-12-10 02:15:28 +000058 DS := dsymutil
Peter Collingbourne518f03d2011-06-20 19:06:04 +000059 DSFLAGS =
60 DSYM = $(EXE).dSYM
Johnny Chenfe141622012-01-12 23:09:42 +000061 AR := libtool
62 ARFLAGS := -static -o
Peter Collingbourne518f03d2011-06-20 19:06:04 +000063endif
64
Johnny Chen53e2edb2011-08-09 20:07:16 +000065CXXFLAGS +=$(CFLAGS)
66LD = $(CC)
67LDFLAGS ?= $(CFLAGS)
68LDFLAGS += $(LD_EXTRAS)
69OBJECTS =
70EXE ?= a.out
71
Peter Collingbourne518f03d2011-06-20 19:06:04 +000072ifneq "$(DYLIB_NAME)" ""
73 ifeq "$(OS)" "Darwin"
74 DYLIB_FILENAME = lib$(DYLIB_NAME).dylib
75 else
76 DYLIB_FILENAME = lib$(DYLIB_NAME).so
77 endif
78endif
Johnny Chen9bc867a2010-08-23 23:56:08 +000079
Johnny Chenbdb4efc2011-01-14 18:19:53 +000080# Function that returns the counterpart C++ compiler, given $(CC) as arg.
81cxx_compiler = $(if $(findstring clang,$(1)), $(subst clang,clang++,$(1)), $(if $(findstring llvm-gcc,$(1)), $(subst llvm-gcc,llvm-g++,$(1)), $(subst gcc,g++,$(1))))
82
83# Function that returns the C++ linker, given $(CC) as arg.
Greg Claytond50d2382012-01-10 00:00:15 +000084cxx_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 +000085
Johnny Chen9bc867a2010-08-23 23:56:08 +000086#----------------------------------------------------------------------
87# dylib settings
88#----------------------------------------------------------------------
89ifneq "$(strip $(DYLIB_C_SOURCES))" ""
90 DYLIB_OBJECTS +=$(strip $(DYLIB_C_SOURCES:.c=.o))
91endif
92
Greg Claytond50d2382012-01-10 00:00:15 +000093ifneq "$(strip $(DYLIB_CXX_SOURCES))" ""
94 DYLIB_OBJECTS +=$(strip $(DYLIB_CXX_SOURCES:.cpp=.o))
95 CXX = $(call cxx_compiler,$(CC))
96 LD = $(call cxx_linker,$(CC))
97endif
Johnny Chen9bc867a2010-08-23 23:56:08 +000098
99#----------------------------------------------------------------------
100# Check if we have any C source files
101#----------------------------------------------------------------------
102ifneq "$(strip $(C_SOURCES))" ""
103 OBJECTS +=$(strip $(C_SOURCES:.c=.o))
104endif
105
106#----------------------------------------------------------------------
107# Check if we have any C++ source files
108#----------------------------------------------------------------------
109ifneq "$(strip $(CXX_SOURCES))" ""
110 OBJECTS +=$(strip $(CXX_SOURCES:.cpp=.o))
Johnny Chen832d2332010-09-30 01:22:34 +0000111 CXX = $(call cxx_compiler,$(CC))
Johnny Chenbdb4efc2011-01-14 18:19:53 +0000112 LD = $(call cxx_linker,$(CC))
Johnny Chen9bc867a2010-08-23 23:56:08 +0000113endif
114
115#----------------------------------------------------------------------
116# Check if we have any ObjC source files
117#----------------------------------------------------------------------
118ifneq "$(strip $(OBJC_SOURCES))" ""
119 OBJECTS +=$(strip $(OBJC_SOURCES:.m=.o))
120 LDFLAGS +=-lobjc
121endif
122
123#----------------------------------------------------------------------
124# Check if we have any ObjC++ source files
125#----------------------------------------------------------------------
126ifneq "$(strip $(OBJCXX_SOURCES))" ""
127 OBJECTS +=$(strip $(OBJCXX_SOURCES:.mm=.o))
Johnny Chen832d2332010-09-30 01:22:34 +0000128 CXX = $(call cxx_compiler,$(CC))
Johnny Chenbdb4efc2011-01-14 18:19:53 +0000129 LD = $(call cxx_linker,$(CC))
Johnny Chen9bc867a2010-08-23 23:56:08 +0000130 ifeq $(findstring lobjc,$(LDFLAGS)) ""
131 LDFLAGS +=-lobjc
132 endif
133endif
134
Johnny Chenfe141622012-01-12 23:09:42 +0000135#----------------------------------------------------------------------
136# Check if we have any C source files for archive
137#----------------------------------------------------------------------
138ifneq "$(strip $(ARCHIVE_C_SOURCES))" ""
139 ARCHIVE_OBJECTS +=$(strip $(ARCHIVE_C_SOURCES:.c=.o))
140endif
141
142#----------------------------------------------------------------------
143# Check if we have any C++ source files for archive
144#----------------------------------------------------------------------
145ifneq "$(strip $(ARCHIVE_CXX_SOURCES))" ""
146 ARCHIVE_OBJECTS +=$(strip $(ARCHIVE_CXX_SOURCES:.cpp=.o))
147 CXX = $(call cxx_compiler,$(CC))
148 LD = $(call cxx_linker,$(CC))
149endif
150
151#----------------------------------------------------------------------
152# Check if we have any ObjC source files for archive
153#----------------------------------------------------------------------
154ifneq "$(strip $(ARCHIVE_OBJC_SOURCES))" ""
155 ARCHIVE_OBJECTS +=$(strip $(ARCHIVE_OBJC_SOURCES:.m=.o))
156 LDFLAGS +=-lobjc
157endif
158
159#----------------------------------------------------------------------
160# Check if we have any ObjC++ source files for archive
161#----------------------------------------------------------------------
162ifneq "$(strip $(ARCHIVE_OBJCXX_SOURCES))" ""
163 ARCHIVE_OBJECTS +=$(strip $(ARCHIVE_OBJCXX_SOURCES:.mm=.o))
164 CXX = $(call cxx_compiler,$(CC))
165 LD = $(call cxx_linker,$(CC))
166 ifeq $(findstring lobjc,$(LDFLAGS)) ""
167 LDFLAGS +=-lobjc
168 endif
169endif
170
Johnny Chen9bc867a2010-08-23 23:56:08 +0000171
172#----------------------------------------------------------------------
Johnny Chenbeac8f92012-01-10 00:41:11 +0000173# DYLIB_ONLY variable can be used to skip the building of a.out.
174# See the sections below regarding dSYM file as well as the building of
175# EXE from all the objects.
176#----------------------------------------------------------------------
177
178#----------------------------------------------------------------------
Johnny Chen9bc867a2010-08-23 23:56:08 +0000179# Make the dSYM file from the executable if $(MAKE_DSYM) != "NO"
180#----------------------------------------------------------------------
Peter Collingbourne518f03d2011-06-20 19:06:04 +0000181ifeq "$(OS)" "Darwin"
Johnny Chen91016392011-06-20 20:08:26 +0000182ifneq "$(MAKE_DSYM)" "NO"
Johnny Chenbeac8f92012-01-10 00:41:11 +0000183ifeq "$(DYLIB_ONLY)" ""
Johnny Chen9bc867a2010-08-23 23:56:08 +0000184$(DSYM) : $(EXE)
185 $(DS) $(DSFLAGS) -o "$(DSYM)" "$(EXE)"
Johnny Chen91016392011-06-20 20:08:26 +0000186endif
Johnny Chen9bc867a2010-08-23 23:56:08 +0000187endif
Johnny Chenbeac8f92012-01-10 00:41:11 +0000188endif
Johnny Chen9bc867a2010-08-23 23:56:08 +0000189
190#----------------------------------------------------------------------
191# Compile the executable from all the objects.
192#----------------------------------------------------------------------
Johnny Chenbeac8f92012-01-10 00:41:11 +0000193ifneq "$(DYLIB_NAME)" ""
194ifeq "$(DYLIB_ONLY)" ""
Johnny Chenfe141622012-01-12 23:09:42 +0000195$(EXE) : $(OBJECTS) $(ARCHIVE_NAME) $(DYLIB_FILENAME)
196 $(LD) $(LDFLAGS) $(OBJECTS) $(ARCHIVE_NAME) -L. -l$(DYLIB_NAME) -o "$(EXE)"
Johnny Chenbeac8f92012-01-10 00:41:11 +0000197else
198EXE = $(DYLIB_FILENAME)
199endif
200else
Johnny Chenfe141622012-01-12 23:09:42 +0000201$(EXE) : $(OBJECTS) $(ARCHIVE_NAME)
202 $(LD) $(LDFLAGS) $(OBJECTS) $(ARCHIVE_NAME) -o "$(EXE)"
203endif
204
205#----------------------------------------------------------------------
206# Make the archive
207#----------------------------------------------------------------------
208ifneq "$(ARCHIVE_NAME)" ""
209ifeq "$(OS)" "Darwin"
210$(ARCHIVE_NAME) : $(ARCHIVE_OBJECTS)
211 $(AR) $(ARFLAGS) $(ARCHIVE_NAME) $(ARCHIVE_OBJECTS)
212 $(RM) $(ARCHIVE_OBJECTS)
213else
214$(ARCHIVE_NAME) : $(foreach ar_obj,$(ARCHIVE_OBJECTS),$(ARCHIVE_NAME)($(ar_obj)))
215endif
Johnny Chen9bc867a2010-08-23 23:56:08 +0000216endif
217
218#----------------------------------------------------------------------
219# Make the dylib
220#----------------------------------------------------------------------
Peter Collingbourne518f03d2011-06-20 19:06:04 +0000221$(DYLIB_FILENAME) : $(DYLIB_OBJECTS)
222ifeq "$(OS)" "Darwin"
223 $(LD) $(LDFLAGS) $(DYLIB_OBJECTS) -install_name "@executable_path/$(DYLIB_FILENAME)" -dynamiclib -o "$(DYLIB_FILENAME)"
224else
225 $(LD) $(LDFLAGS) $(DYLIB_OBJECTS) -shared -o "$(DYLIB_FILENAME)"
226endif
Johnny Chen9bc867a2010-08-23 23:56:08 +0000227
228#----------------------------------------------------------------------
229# Automatic variables based on items already entered. Below we create
230# an objects lists from the list of sources by replacing all entries
231# that end with .c with .o, and we also create a list of prerequisite
232# files by replacing all .c files with .d.
233#----------------------------------------------------------------------
234PREREQS := $(OBJECTS:.o=.d)
Johnny Chene3dc0f02010-08-24 16:35:00 +0000235ifneq "$(DYLIB_NAME)" ""
236 DYLIB_PREREQS := $(DYLIB_OBJECTS:.o=.d)
237endif
Johnny Chen9bc867a2010-08-23 23:56:08 +0000238
239#----------------------------------------------------------------------
240# Rule for Generating Prerequisites Automatically using .d files and
241# the compiler -MM option. The -M option will list all system headers,
242# and the -MM option will list all non-system dependencies.
243#----------------------------------------------------------------------
244%.d: %.c
245 @set -e; rm -f $@; \
Johnny Chen189bff12011-08-04 20:44:56 +0000246 $(CC) -M $(CFLAGS) $< > $@.$$$$; \
Johnny Chen9bc867a2010-08-23 23:56:08 +0000247 sed 's,\($*\)\.o[ :]*,\1.o $@ : ,g' < $@.$$$$ > $@; \
248 rm -f $@.$$$$
249
250%.d: %.cpp
251 @set -e; rm -f $@; \
Johnny Chen189bff12011-08-04 20:44:56 +0000252 $(CC) -M $(CXXFLAGS) $< > $@.$$$$; \
Johnny Chen9bc867a2010-08-23 23:56:08 +0000253 sed 's,\($*\)\.o[ :]*,\1.o $@ : ,g' < $@.$$$$ > $@; \
254 rm -f $@.$$$$
255
256%.d: %.m
257 @set -e; rm -f $@; \
Johnny Chen189bff12011-08-04 20:44:56 +0000258 $(CC) -M $(CFLAGS) $< > $@.$$$$; \
Johnny Chen9bc867a2010-08-23 23:56:08 +0000259 sed 's,\($*\)\.o[ :]*,\1.o $@ : ,g' < $@.$$$$ > $@; \
260 rm -f $@.$$$$
261
262%.d: %.mm
263 @set -e; rm -f $@; \
Johnny Chen189bff12011-08-04 20:44:56 +0000264 $(CC) -M $(CXXFLAGS) $< > $@.$$$$; \
Johnny Chen9bc867a2010-08-23 23:56:08 +0000265 sed 's,\($*\)\.o[ :]*,\1.o $@ : ,g' < $@.$$$$ > $@; \
266 rm -f $@.$$$$
267
268#----------------------------------------------------------------------
269# Include all of the makefiles for each source file so we don't have
270# to manually track all of the prerequisites for each source file.
271#----------------------------------------------------------------------
272sinclude $(PREREQS)
Johnny Chene3dc0f02010-08-24 16:35:00 +0000273ifneq "$(DYLIB_NAME)" ""
274 sinclude $(DYLIB_PREREQS)
275endif
Johnny Chen9bc867a2010-08-23 23:56:08 +0000276
277.PHONY: clean
278dsym: $(DSYM)
279all: $(EXE) $(DSYM)
Johnny Chene1030cd2010-09-27 20:44:46 +0000280clean::
Johnny Chen9bc867a2010-08-23 23:56:08 +0000281ifeq "$(DYLIB_NAME)" ""
Johnny Chenfe141622012-01-12 23:09:42 +0000282 rm -rf "$(EXE)" "$(DSYM)" $(OBJECTS) $(PREREQS) $(ARCHIVE_NAME) $(ARCHIVE_OBJECTS)
Johnny Chen9bc867a2010-08-23 23:56:08 +0000283else
Johnny Chenfe141622012-01-12 23:09:42 +0000284 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 +0000285endif
Johnny Chenfa380412011-01-14 21:18:12 +0000286
287#----------------------------------------------------------------------
288# From http://blog.melski.net/tag/debugging-makefiles/
289#
290# Usage: make print-CC print-CXX print-LD
291#----------------------------------------------------------------------
292print-%:
293 @echo '$*=$($*)'
294 @echo ' origin = $(origin $*)'
295 @echo ' flavor = $(flavor $*)'
296 @echo ' value = $(value $*)'
Johnny Chen8b2c3212011-01-28 17:22:29 +0000297
298
299### Local Variables: ###
300### mode:makefile ###
301### End: ###