blob: 75f99900af3ecb17b10960b98cf873fe000b0414 [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 Chen9bc867a2010-08-23 23:56:08 +000015
16# Uncomment line below for debugging shell commands
17# SHELL = /bin/sh -x
18
19#----------------------------------------------------------------------
Johnny Chen6c17c082010-09-16 20:54:06 +000020# If ARCH is not defined, default to x86_64.
Peter Collingbourne518f03d2011-06-20 19:06:04 +000021# If OS is not defined, use 'uname -s' to determine the OS name.
Johnny Chen6c17c082010-09-16 20:54:06 +000022#----------------------------------------------------------------------
23ifeq "$(ARCH)" ""
24 ARCH = x86_64
25endif
26
Peter Collingbourne518f03d2011-06-20 19:06:04 +000027ifeq "$(OS)" ""
28 OS = $(shell uname -s)
29endif
30
Johnny Chen6c17c082010-09-16 20:54:06 +000031#----------------------------------------------------------------------
Johnny Chen597cbbb2011-08-23 21:54:10 +000032# CC defaults to clang.
Johnny Chen4ab4a9b2011-08-24 18:12:53 +000033#
34# If you change the defaults of CC, be sure to also change it in the file
35# test/plugins/builder_base.py, which provides a Python way to return the
36# value of the make variable CC -- getCompiler().
37#
Johnny Chen6055b442011-01-14 20:55:13 +000038# See also these functions:
39# o cxx_compiler
40# o cxx_linker
Johnny Chend43e2082011-01-14 20:46:49 +000041#----------------------------------------------------------------------
Johnny Chen597cbbb2011-08-23 21:54:10 +000042CC ?= clang
Johnny Chend43e2082011-01-14 20:46:49 +000043ifeq "$(CC)" "cc"
Johnny Chen597cbbb2011-08-23 21:54:10 +000044 CC = clang
Johnny Chend43e2082011-01-14 20:46:49 +000045endif
46
47#----------------------------------------------------------------------
Johnny Chen9bc867a2010-08-23 23:56:08 +000048# Change any build/tool options needed
49#----------------------------------------------------------------------
Peter Collingbourne518f03d2011-06-20 19:06:04 +000050CFLAGS ?= -gdwarf-2 -O0
Johnny Chenbeac8f92012-01-10 00:41:11 +000051CFLAGS += $(FRAMEWORK_INCLUDES) $(CFLAGS_EXTRAS)
Peter Collingbourne518f03d2011-06-20 19:06:04 +000052ifeq "$(OS)" "Darwin"
53 CFLAGS += -arch $(ARCH)
Greg Clayton3bffb082011-12-10 02:15:28 +000054 DS := dsymutil
Peter Collingbourne518f03d2011-06-20 19:06:04 +000055 DSFLAGS =
56 DSYM = $(EXE).dSYM
57endif
58
Johnny Chen53e2edb2011-08-09 20:07:16 +000059CXXFLAGS +=$(CFLAGS)
60LD = $(CC)
61LDFLAGS ?= $(CFLAGS)
62LDFLAGS += $(LD_EXTRAS)
63OBJECTS =
64EXE ?= a.out
65
Peter Collingbourne518f03d2011-06-20 19:06:04 +000066ifneq "$(DYLIB_NAME)" ""
67 ifeq "$(OS)" "Darwin"
68 DYLIB_FILENAME = lib$(DYLIB_NAME).dylib
69 else
70 DYLIB_FILENAME = lib$(DYLIB_NAME).so
71 endif
72endif
Johnny Chen9bc867a2010-08-23 23:56:08 +000073
Johnny Chenbdb4efc2011-01-14 18:19:53 +000074# Function that returns the counterpart C++ compiler, given $(CC) as arg.
75cxx_compiler = $(if $(findstring clang,$(1)), $(subst clang,clang++,$(1)), $(if $(findstring llvm-gcc,$(1)), $(subst llvm-gcc,llvm-g++,$(1)), $(subst gcc,g++,$(1))))
76
77# Function that returns the C++ linker, given $(CC) as arg.
Greg Claytond50d2382012-01-10 00:00:15 +000078cxx_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 +000079
Johnny Chen9bc867a2010-08-23 23:56:08 +000080#----------------------------------------------------------------------
81# dylib settings
82#----------------------------------------------------------------------
83ifneq "$(strip $(DYLIB_C_SOURCES))" ""
84 DYLIB_OBJECTS +=$(strip $(DYLIB_C_SOURCES:.c=.o))
85endif
86
Greg Claytond50d2382012-01-10 00:00:15 +000087ifneq "$(strip $(DYLIB_CXX_SOURCES))" ""
88 DYLIB_OBJECTS +=$(strip $(DYLIB_CXX_SOURCES:.cpp=.o))
89 CXX = $(call cxx_compiler,$(CC))
90 LD = $(call cxx_linker,$(CC))
91endif
Johnny Chen9bc867a2010-08-23 23:56:08 +000092
93#----------------------------------------------------------------------
94# Check if we have any C source files
95#----------------------------------------------------------------------
96ifneq "$(strip $(C_SOURCES))" ""
97 OBJECTS +=$(strip $(C_SOURCES:.c=.o))
98endif
99
100#----------------------------------------------------------------------
101# Check if we have any C++ source files
102#----------------------------------------------------------------------
103ifneq "$(strip $(CXX_SOURCES))" ""
104 OBJECTS +=$(strip $(CXX_SOURCES:.cpp=.o))
Johnny Chen832d2332010-09-30 01:22:34 +0000105 CXX = $(call cxx_compiler,$(CC))
Johnny Chenbdb4efc2011-01-14 18:19:53 +0000106 LD = $(call cxx_linker,$(CC))
Johnny Chen9bc867a2010-08-23 23:56:08 +0000107endif
108
109#----------------------------------------------------------------------
110# Check if we have any ObjC source files
111#----------------------------------------------------------------------
112ifneq "$(strip $(OBJC_SOURCES))" ""
113 OBJECTS +=$(strip $(OBJC_SOURCES:.m=.o))
114 LDFLAGS +=-lobjc
115endif
116
117#----------------------------------------------------------------------
118# Check if we have any ObjC++ source files
119#----------------------------------------------------------------------
120ifneq "$(strip $(OBJCXX_SOURCES))" ""
121 OBJECTS +=$(strip $(OBJCXX_SOURCES:.mm=.o))
Johnny Chen832d2332010-09-30 01:22:34 +0000122 CXX = $(call cxx_compiler,$(CC))
Johnny Chenbdb4efc2011-01-14 18:19:53 +0000123 LD = $(call cxx_linker,$(CC))
Johnny Chen9bc867a2010-08-23 23:56:08 +0000124 ifeq $(findstring lobjc,$(LDFLAGS)) ""
125 LDFLAGS +=-lobjc
126 endif
127endif
128
129
130#----------------------------------------------------------------------
Johnny Chenbeac8f92012-01-10 00:41:11 +0000131# DYLIB_ONLY variable can be used to skip the building of a.out.
132# See the sections below regarding dSYM file as well as the building of
133# EXE from all the objects.
134#----------------------------------------------------------------------
135
136#----------------------------------------------------------------------
Johnny Chen9bc867a2010-08-23 23:56:08 +0000137# Make the dSYM file from the executable if $(MAKE_DSYM) != "NO"
138#----------------------------------------------------------------------
Peter Collingbourne518f03d2011-06-20 19:06:04 +0000139ifeq "$(OS)" "Darwin"
Johnny Chen91016392011-06-20 20:08:26 +0000140ifneq "$(MAKE_DSYM)" "NO"
Johnny Chenbeac8f92012-01-10 00:41:11 +0000141ifeq "$(DYLIB_ONLY)" ""
Johnny Chen9bc867a2010-08-23 23:56:08 +0000142$(DSYM) : $(EXE)
143 $(DS) $(DSFLAGS) -o "$(DSYM)" "$(EXE)"
Johnny Chen91016392011-06-20 20:08:26 +0000144endif
Johnny Chen9bc867a2010-08-23 23:56:08 +0000145endif
Johnny Chenbeac8f92012-01-10 00:41:11 +0000146endif
Johnny Chen9bc867a2010-08-23 23:56:08 +0000147
148#----------------------------------------------------------------------
149# Compile the executable from all the objects.
150#----------------------------------------------------------------------
Johnny Chenbeac8f92012-01-10 00:41:11 +0000151ifneq "$(DYLIB_NAME)" ""
152ifeq "$(DYLIB_ONLY)" ""
Peter Collingbourne518f03d2011-06-20 19:06:04 +0000153$(EXE) : $(OBJECTS) $(DYLIB_FILENAME)
154 $(LD) $(LDFLAGS) $(OBJECTS) -L. -l$(DYLIB_NAME) -o "$(EXE)"
Johnny Chenbeac8f92012-01-10 00:41:11 +0000155else
156EXE = $(DYLIB_FILENAME)
157endif
158else
159$(EXE) : $(OBJECTS)
160 $(LD) $(LDFLAGS) $(OBJECTS) -o "$(EXE)"
Johnny Chen9bc867a2010-08-23 23:56:08 +0000161endif
162
163#----------------------------------------------------------------------
164# Make the dylib
165#----------------------------------------------------------------------
Peter Collingbourne518f03d2011-06-20 19:06:04 +0000166$(DYLIB_FILENAME) : $(DYLIB_OBJECTS)
167ifeq "$(OS)" "Darwin"
168 $(LD) $(LDFLAGS) $(DYLIB_OBJECTS) -install_name "@executable_path/$(DYLIB_FILENAME)" -dynamiclib -o "$(DYLIB_FILENAME)"
169else
170 $(LD) $(LDFLAGS) $(DYLIB_OBJECTS) -shared -o "$(DYLIB_FILENAME)"
171endif
Johnny Chen9bc867a2010-08-23 23:56:08 +0000172
173#----------------------------------------------------------------------
174# Automatic variables based on items already entered. Below we create
175# an objects lists from the list of sources by replacing all entries
176# that end with .c with .o, and we also create a list of prerequisite
177# files by replacing all .c files with .d.
178#----------------------------------------------------------------------
179PREREQS := $(OBJECTS:.o=.d)
Johnny Chene3dc0f02010-08-24 16:35:00 +0000180ifneq "$(DYLIB_NAME)" ""
181 DYLIB_PREREQS := $(DYLIB_OBJECTS:.o=.d)
182endif
Johnny Chen9bc867a2010-08-23 23:56:08 +0000183
184#----------------------------------------------------------------------
185# Rule for Generating Prerequisites Automatically using .d files and
186# the compiler -MM option. The -M option will list all system headers,
187# and the -MM option will list all non-system dependencies.
188#----------------------------------------------------------------------
189%.d: %.c
190 @set -e; rm -f $@; \
Johnny Chen189bff12011-08-04 20:44:56 +0000191 $(CC) -M $(CFLAGS) $< > $@.$$$$; \
Johnny Chen9bc867a2010-08-23 23:56:08 +0000192 sed 's,\($*\)\.o[ :]*,\1.o $@ : ,g' < $@.$$$$ > $@; \
193 rm -f $@.$$$$
194
195%.d: %.cpp
196 @set -e; rm -f $@; \
Johnny Chen189bff12011-08-04 20:44:56 +0000197 $(CC) -M $(CXXFLAGS) $< > $@.$$$$; \
Johnny Chen9bc867a2010-08-23 23:56:08 +0000198 sed 's,\($*\)\.o[ :]*,\1.o $@ : ,g' < $@.$$$$ > $@; \
199 rm -f $@.$$$$
200
201%.d: %.m
202 @set -e; rm -f $@; \
Johnny Chen189bff12011-08-04 20:44:56 +0000203 $(CC) -M $(CFLAGS) $< > $@.$$$$; \
Johnny Chen9bc867a2010-08-23 23:56:08 +0000204 sed 's,\($*\)\.o[ :]*,\1.o $@ : ,g' < $@.$$$$ > $@; \
205 rm -f $@.$$$$
206
207%.d: %.mm
208 @set -e; rm -f $@; \
Johnny Chen189bff12011-08-04 20:44:56 +0000209 $(CC) -M $(CXXFLAGS) $< > $@.$$$$; \
Johnny Chen9bc867a2010-08-23 23:56:08 +0000210 sed 's,\($*\)\.o[ :]*,\1.o $@ : ,g' < $@.$$$$ > $@; \
211 rm -f $@.$$$$
212
213#----------------------------------------------------------------------
214# Include all of the makefiles for each source file so we don't have
215# to manually track all of the prerequisites for each source file.
216#----------------------------------------------------------------------
217sinclude $(PREREQS)
Johnny Chene3dc0f02010-08-24 16:35:00 +0000218ifneq "$(DYLIB_NAME)" ""
219 sinclude $(DYLIB_PREREQS)
220endif
Johnny Chen9bc867a2010-08-23 23:56:08 +0000221
222.PHONY: clean
223dsym: $(DSYM)
224all: $(EXE) $(DSYM)
Johnny Chene1030cd2010-09-27 20:44:46 +0000225clean::
Johnny Chen9bc867a2010-08-23 23:56:08 +0000226ifeq "$(DYLIB_NAME)" ""
227 rm -rf "$(EXE)" "$(DSYM)" $(OBJECTS) $(PREREQS)
228else
Johnny Chen6b0a1e32011-11-14 18:37:49 +0000229 rm -rf "$(EXE)" "$(DSYM)" $(OBJECTS) $(PREREQS) $(DYLIB_OBJECTS) $(DYLIB_PREREQS) $(DYLIB_FILENAME) $(DYLIB_FILENAME).dSYM
Johnny Chen9bc867a2010-08-23 23:56:08 +0000230endif
Johnny Chenfa380412011-01-14 21:18:12 +0000231
232#----------------------------------------------------------------------
233# From http://blog.melski.net/tag/debugging-makefiles/
234#
235# Usage: make print-CC print-CXX print-LD
236#----------------------------------------------------------------------
237print-%:
238 @echo '$*=$($*)'
239 @echo ' origin = $(origin $*)'
240 @echo ' flavor = $(flavor $*)'
241 @echo ' value = $(value $*)'
Johnny Chen8b2c3212011-01-28 17:22:29 +0000242
243
244### Local Variables: ###
245### mode:makefile ###
246### End: ###