blob: 238acb77e571fc51789ecb67c84401fd90f2af6c [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)
73LDFLAGS += $(LD_EXTRAS)
74OBJECTS =
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
Greg Claytond50d2382012-01-10 00:00:15 +000098ifneq "$(strip $(DYLIB_CXX_SOURCES))" ""
99 DYLIB_OBJECTS +=$(strip $(DYLIB_CXX_SOURCES:.cpp=.o))
100 CXX = $(call cxx_compiler,$(CC))
101 LD = $(call cxx_linker,$(CC))
102endif
Johnny Chen9bc867a2010-08-23 23:56:08 +0000103
104#----------------------------------------------------------------------
105# Check if we have any C source files
106#----------------------------------------------------------------------
107ifneq "$(strip $(C_SOURCES))" ""
108 OBJECTS +=$(strip $(C_SOURCES:.c=.o))
109endif
110
111#----------------------------------------------------------------------
112# Check if we have any C++ source files
113#----------------------------------------------------------------------
114ifneq "$(strip $(CXX_SOURCES))" ""
115 OBJECTS +=$(strip $(CXX_SOURCES:.cpp=.o))
Johnny Chen832d2332010-09-30 01:22:34 +0000116 CXX = $(call cxx_compiler,$(CC))
Johnny Chenbdb4efc2011-01-14 18:19:53 +0000117 LD = $(call cxx_linker,$(CC))
Johnny Chen9bc867a2010-08-23 23:56:08 +0000118endif
119
120#----------------------------------------------------------------------
121# Check if we have any ObjC source files
122#----------------------------------------------------------------------
123ifneq "$(strip $(OBJC_SOURCES))" ""
124 OBJECTS +=$(strip $(OBJC_SOURCES:.m=.o))
125 LDFLAGS +=-lobjc
126endif
127
128#----------------------------------------------------------------------
129# Check if we have any ObjC++ source files
130#----------------------------------------------------------------------
131ifneq "$(strip $(OBJCXX_SOURCES))" ""
132 OBJECTS +=$(strip $(OBJCXX_SOURCES:.mm=.o))
Johnny Chen832d2332010-09-30 01:22:34 +0000133 CXX = $(call cxx_compiler,$(CC))
Johnny Chenbdb4efc2011-01-14 18:19:53 +0000134 LD = $(call cxx_linker,$(CC))
Johnny Chen9bc867a2010-08-23 23:56:08 +0000135 ifeq $(findstring lobjc,$(LDFLAGS)) ""
136 LDFLAGS +=-lobjc
137 endif
138endif
139
Johnny Chenfe141622012-01-12 23:09:42 +0000140#----------------------------------------------------------------------
141# Check if we have any C source files for archive
142#----------------------------------------------------------------------
143ifneq "$(strip $(ARCHIVE_C_SOURCES))" ""
144 ARCHIVE_OBJECTS +=$(strip $(ARCHIVE_C_SOURCES:.c=.o))
145endif
146
147#----------------------------------------------------------------------
148# Check if we have any C++ source files for archive
149#----------------------------------------------------------------------
150ifneq "$(strip $(ARCHIVE_CXX_SOURCES))" ""
151 ARCHIVE_OBJECTS +=$(strip $(ARCHIVE_CXX_SOURCES:.cpp=.o))
152 CXX = $(call cxx_compiler,$(CC))
153 LD = $(call cxx_linker,$(CC))
154endif
155
156#----------------------------------------------------------------------
157# Check if we have any ObjC source files for archive
158#----------------------------------------------------------------------
159ifneq "$(strip $(ARCHIVE_OBJC_SOURCES))" ""
160 ARCHIVE_OBJECTS +=$(strip $(ARCHIVE_OBJC_SOURCES:.m=.o))
161 LDFLAGS +=-lobjc
162endif
163
164#----------------------------------------------------------------------
165# Check if we have any ObjC++ source files for archive
166#----------------------------------------------------------------------
167ifneq "$(strip $(ARCHIVE_OBJCXX_SOURCES))" ""
168 ARCHIVE_OBJECTS +=$(strip $(ARCHIVE_OBJCXX_SOURCES:.mm=.o))
169 CXX = $(call cxx_compiler,$(CC))
170 LD = $(call cxx_linker,$(CC))
171 ifeq $(findstring lobjc,$(LDFLAGS)) ""
172 LDFLAGS +=-lobjc
173 endif
174endif
175
Johnny Chen9bc867a2010-08-23 23:56:08 +0000176
177#----------------------------------------------------------------------
Johnny Chenbeac8f92012-01-10 00:41:11 +0000178# DYLIB_ONLY variable can be used to skip the building of a.out.
179# See the sections below regarding dSYM file as well as the building of
180# EXE from all the objects.
181#----------------------------------------------------------------------
182
183#----------------------------------------------------------------------
Johnny Chen9bc867a2010-08-23 23:56:08 +0000184# Make the dSYM file from the executable if $(MAKE_DSYM) != "NO"
185#----------------------------------------------------------------------
Peter Collingbourne518f03d2011-06-20 19:06:04 +0000186ifeq "$(OS)" "Darwin"
Johnny Chen91016392011-06-20 20:08:26 +0000187ifneq "$(MAKE_DSYM)" "NO"
Johnny Chenbeac8f92012-01-10 00:41:11 +0000188ifeq "$(DYLIB_ONLY)" ""
Johnny Chen9bc867a2010-08-23 23:56:08 +0000189$(DSYM) : $(EXE)
190 $(DS) $(DSFLAGS) -o "$(DSYM)" "$(EXE)"
Johnny Chen91016392011-06-20 20:08:26 +0000191endif
Johnny Chen9bc867a2010-08-23 23:56:08 +0000192endif
Johnny Chenbeac8f92012-01-10 00:41:11 +0000193endif
Johnny Chen9bc867a2010-08-23 23:56:08 +0000194
195#----------------------------------------------------------------------
196# Compile the executable from all the objects.
197#----------------------------------------------------------------------
Johnny Chenbeac8f92012-01-10 00:41:11 +0000198ifneq "$(DYLIB_NAME)" ""
199ifeq "$(DYLIB_ONLY)" ""
Johnny Chenfe141622012-01-12 23:09:42 +0000200$(EXE) : $(OBJECTS) $(ARCHIVE_NAME) $(DYLIB_FILENAME)
201 $(LD) $(LDFLAGS) $(OBJECTS) $(ARCHIVE_NAME) -L. -l$(DYLIB_NAME) -o "$(EXE)"
Johnny Chenbeac8f92012-01-10 00:41:11 +0000202else
203EXE = $(DYLIB_FILENAME)
204endif
205else
Johnny Chenfe141622012-01-12 23:09:42 +0000206$(EXE) : $(OBJECTS) $(ARCHIVE_NAME)
207 $(LD) $(LDFLAGS) $(OBJECTS) $(ARCHIVE_NAME) -o "$(EXE)"
208endif
209
210#----------------------------------------------------------------------
211# Make the archive
212#----------------------------------------------------------------------
213ifneq "$(ARCHIVE_NAME)" ""
214ifeq "$(OS)" "Darwin"
215$(ARCHIVE_NAME) : $(ARCHIVE_OBJECTS)
216 $(AR) $(ARFLAGS) $(ARCHIVE_NAME) $(ARCHIVE_OBJECTS)
217 $(RM) $(ARCHIVE_OBJECTS)
218else
219$(ARCHIVE_NAME) : $(foreach ar_obj,$(ARCHIVE_OBJECTS),$(ARCHIVE_NAME)($(ar_obj)))
220endif
Johnny Chen9bc867a2010-08-23 23:56:08 +0000221endif
222
223#----------------------------------------------------------------------
224# Make the dylib
225#----------------------------------------------------------------------
Peter Collingbourne518f03d2011-06-20 19:06:04 +0000226$(DYLIB_FILENAME) : $(DYLIB_OBJECTS)
227ifeq "$(OS)" "Darwin"
228 $(LD) $(LDFLAGS) $(DYLIB_OBJECTS) -install_name "@executable_path/$(DYLIB_FILENAME)" -dynamiclib -o "$(DYLIB_FILENAME)"
229else
230 $(LD) $(LDFLAGS) $(DYLIB_OBJECTS) -shared -o "$(DYLIB_FILENAME)"
231endif
Johnny Chen9bc867a2010-08-23 23:56:08 +0000232
233#----------------------------------------------------------------------
234# Automatic variables based on items already entered. Below we create
235# an objects lists from the list of sources by replacing all entries
236# that end with .c with .o, and we also create a list of prerequisite
237# files by replacing all .c files with .d.
238#----------------------------------------------------------------------
239PREREQS := $(OBJECTS:.o=.d)
Johnny Chene3dc0f02010-08-24 16:35:00 +0000240ifneq "$(DYLIB_NAME)" ""
241 DYLIB_PREREQS := $(DYLIB_OBJECTS:.o=.d)
242endif
Johnny Chen9bc867a2010-08-23 23:56:08 +0000243
244#----------------------------------------------------------------------
245# Rule for Generating Prerequisites Automatically using .d files and
246# the compiler -MM option. The -M option will list all system headers,
247# and the -MM option will list all non-system dependencies.
248#----------------------------------------------------------------------
249%.d: %.c
250 @set -e; rm -f $@; \
Johnny Chen189bff12011-08-04 20:44:56 +0000251 $(CC) -M $(CFLAGS) $< > $@.$$$$; \
Johnny Chen9bc867a2010-08-23 23:56:08 +0000252 sed 's,\($*\)\.o[ :]*,\1.o $@ : ,g' < $@.$$$$ > $@; \
253 rm -f $@.$$$$
254
255%.d: %.cpp
256 @set -e; rm -f $@; \
Johnny Chen189bff12011-08-04 20:44:56 +0000257 $(CC) -M $(CXXFLAGS) $< > $@.$$$$; \
Johnny Chen9bc867a2010-08-23 23:56:08 +0000258 sed 's,\($*\)\.o[ :]*,\1.o $@ : ,g' < $@.$$$$ > $@; \
259 rm -f $@.$$$$
260
261%.d: %.m
262 @set -e; rm -f $@; \
Johnny Chen189bff12011-08-04 20:44:56 +0000263 $(CC) -M $(CFLAGS) $< > $@.$$$$; \
Johnny Chen9bc867a2010-08-23 23:56:08 +0000264 sed 's,\($*\)\.o[ :]*,\1.o $@ : ,g' < $@.$$$$ > $@; \
265 rm -f $@.$$$$
266
267%.d: %.mm
268 @set -e; rm -f $@; \
Johnny Chen189bff12011-08-04 20:44:56 +0000269 $(CC) -M $(CXXFLAGS) $< > $@.$$$$; \
Johnny Chen9bc867a2010-08-23 23:56:08 +0000270 sed 's,\($*\)\.o[ :]*,\1.o $@ : ,g' < $@.$$$$ > $@; \
271 rm -f $@.$$$$
272
273#----------------------------------------------------------------------
274# Include all of the makefiles for each source file so we don't have
275# to manually track all of the prerequisites for each source file.
276#----------------------------------------------------------------------
277sinclude $(PREREQS)
Johnny Chene3dc0f02010-08-24 16:35:00 +0000278ifneq "$(DYLIB_NAME)" ""
279 sinclude $(DYLIB_PREREQS)
280endif
Johnny Chen9bc867a2010-08-23 23:56:08 +0000281
282.PHONY: clean
283dsym: $(DSYM)
284all: $(EXE) $(DSYM)
Johnny Chene1030cd2010-09-27 20:44:46 +0000285clean::
Johnny Chen9bc867a2010-08-23 23:56:08 +0000286ifeq "$(DYLIB_NAME)" ""
Johnny Chenfe141622012-01-12 23:09:42 +0000287 rm -rf "$(EXE)" "$(DSYM)" $(OBJECTS) $(PREREQS) $(ARCHIVE_NAME) $(ARCHIVE_OBJECTS)
Johnny Chen9bc867a2010-08-23 23:56:08 +0000288else
Johnny Chenfe141622012-01-12 23:09:42 +0000289 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 +0000290endif
Johnny Chenfa380412011-01-14 21:18:12 +0000291
292#----------------------------------------------------------------------
293# From http://blog.melski.net/tag/debugging-makefiles/
294#
295# Usage: make print-CC print-CXX print-LD
296#----------------------------------------------------------------------
297print-%:
298 @echo '$*=$($*)'
299 @echo ' origin = $(origin $*)'
300 @echo ' flavor = $(flavor $*)'
301 @echo ' value = $(value $*)'
Johnny Chen8b2c3212011-01-28 17:22:29 +0000302
303
304### Local Variables: ###
305### mode:makefile ###
306### End: ###