blob: 1388c9afa1b0b18ffa7661acd54f9e769dc8729d [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 :=
9
10# Uncomment line below for debugging shell commands
11# SHELL = /bin/sh -x
12
13#----------------------------------------------------------------------
Johnny Chen6c17c082010-09-16 20:54:06 +000014# If ARCH is not defined, default to x86_64.
Peter Collingbourne518f03d2011-06-20 19:06:04 +000015# If OS is not defined, use 'uname -s' to determine the OS name.
Johnny Chen6c17c082010-09-16 20:54:06 +000016#----------------------------------------------------------------------
17ifeq "$(ARCH)" ""
18 ARCH = x86_64
19endif
20
Peter Collingbourne518f03d2011-06-20 19:06:04 +000021ifeq "$(OS)" ""
22 OS = $(shell uname -s)
23endif
24
Johnny Chen6c17c082010-09-16 20:54:06 +000025#----------------------------------------------------------------------
Johnny Chen597cbbb2011-08-23 21:54:10 +000026# CC defaults to clang.
Johnny Chen4ab4a9b2011-08-24 18:12:53 +000027#
28# If you change the defaults of CC, be sure to also change it in the file
29# test/plugins/builder_base.py, which provides a Python way to return the
30# value of the make variable CC -- getCompiler().
31#
Johnny Chen6055b442011-01-14 20:55:13 +000032# See also these functions:
33# o cxx_compiler
34# o cxx_linker
Johnny Chend43e2082011-01-14 20:46:49 +000035#----------------------------------------------------------------------
Johnny Chen597cbbb2011-08-23 21:54:10 +000036CC ?= clang
Johnny Chend43e2082011-01-14 20:46:49 +000037ifeq "$(CC)" "cc"
Johnny Chen597cbbb2011-08-23 21:54:10 +000038 CC = clang
Johnny Chend43e2082011-01-14 20:46:49 +000039endif
40
41#----------------------------------------------------------------------
Johnny Chen9bc867a2010-08-23 23:56:08 +000042# Change any build/tool options needed
43#----------------------------------------------------------------------
Peter Collingbourne518f03d2011-06-20 19:06:04 +000044CFLAGS ?= -gdwarf-2 -O0
Johnny Chen19bfde32011-08-04 18:17:16 +000045CFLAGS += $(FRAMEWORK_INCLUDES)
Peter Collingbourne518f03d2011-06-20 19:06:04 +000046ifeq "$(OS)" "Darwin"
47 CFLAGS += -arch $(ARCH)
Greg Clayton3bffb082011-12-10 02:15:28 +000048 DS := dsymutil
Peter Collingbourne518f03d2011-06-20 19:06:04 +000049 DSFLAGS =
50 DSYM = $(EXE).dSYM
51endif
52
Johnny Chen53e2edb2011-08-09 20:07:16 +000053CXXFLAGS +=$(CFLAGS)
54LD = $(CC)
55LDFLAGS ?= $(CFLAGS)
56LDFLAGS += $(LD_EXTRAS)
57OBJECTS =
58EXE ?= a.out
59
Peter Collingbourne518f03d2011-06-20 19:06:04 +000060ifneq "$(DYLIB_NAME)" ""
61 ifeq "$(OS)" "Darwin"
62 DYLIB_FILENAME = lib$(DYLIB_NAME).dylib
63 else
64 DYLIB_FILENAME = lib$(DYLIB_NAME).so
65 endif
66endif
Johnny Chen9bc867a2010-08-23 23:56:08 +000067
Johnny Chenbdb4efc2011-01-14 18:19:53 +000068# Function that returns the counterpart C++ compiler, given $(CC) as arg.
69cxx_compiler = $(if $(findstring clang,$(1)), $(subst clang,clang++,$(1)), $(if $(findstring llvm-gcc,$(1)), $(subst llvm-gcc,llvm-g++,$(1)), $(subst gcc,g++,$(1))))
70
71# Function that returns the C++ linker, given $(CC) as arg.
72cxx_linker = $(if $(findstring clang,$(1)), $(subst clang,g++,$(1)), $(if $(findstring llvm-gcc,$(1)), $(subst llvm-gcc,g++,$(1)), $(subst gcc,g++,$(1))))
Johnny Chen832d2332010-09-30 01:22:34 +000073
Johnny Chen9bc867a2010-08-23 23:56:08 +000074#----------------------------------------------------------------------
75# dylib settings
76#----------------------------------------------------------------------
77ifneq "$(strip $(DYLIB_C_SOURCES))" ""
78 DYLIB_OBJECTS +=$(strip $(DYLIB_C_SOURCES:.c=.o))
79endif
80
81
82#----------------------------------------------------------------------
83# Check if we have any C source files
84#----------------------------------------------------------------------
85ifneq "$(strip $(C_SOURCES))" ""
86 OBJECTS +=$(strip $(C_SOURCES:.c=.o))
87endif
88
89#----------------------------------------------------------------------
90# Check if we have any C++ source files
91#----------------------------------------------------------------------
92ifneq "$(strip $(CXX_SOURCES))" ""
93 OBJECTS +=$(strip $(CXX_SOURCES:.cpp=.o))
Johnny Chen832d2332010-09-30 01:22:34 +000094 CXX = $(call cxx_compiler,$(CC))
Johnny Chenbdb4efc2011-01-14 18:19:53 +000095 LD = $(call cxx_linker,$(CC))
Johnny Chen9bc867a2010-08-23 23:56:08 +000096endif
97
98#----------------------------------------------------------------------
99# Check if we have any ObjC source files
100#----------------------------------------------------------------------
101ifneq "$(strip $(OBJC_SOURCES))" ""
102 OBJECTS +=$(strip $(OBJC_SOURCES:.m=.o))
103 LDFLAGS +=-lobjc
104endif
105
106#----------------------------------------------------------------------
107# Check if we have any ObjC++ source files
108#----------------------------------------------------------------------
109ifneq "$(strip $(OBJCXX_SOURCES))" ""
110 OBJECTS +=$(strip $(OBJCXX_SOURCES:.mm=.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 +0000113 ifeq $(findstring lobjc,$(LDFLAGS)) ""
114 LDFLAGS +=-lobjc
115 endif
116endif
117
118
119#----------------------------------------------------------------------
120# Make the dSYM file from the executable if $(MAKE_DSYM) != "NO"
121#----------------------------------------------------------------------
Peter Collingbourne518f03d2011-06-20 19:06:04 +0000122ifeq "$(OS)" "Darwin"
Johnny Chen91016392011-06-20 20:08:26 +0000123ifneq "$(MAKE_DSYM)" "NO"
Johnny Chen9bc867a2010-08-23 23:56:08 +0000124$(DSYM) : $(EXE)
125 $(DS) $(DSFLAGS) -o "$(DSYM)" "$(EXE)"
Johnny Chen91016392011-06-20 20:08:26 +0000126endif
Johnny Chen9bc867a2010-08-23 23:56:08 +0000127endif
128
129#----------------------------------------------------------------------
130# Compile the executable from all the objects.
131#----------------------------------------------------------------------
132ifeq "$(DYLIB_NAME)" ""
133$(EXE) : $(OBJECTS)
134 $(LD) $(LDFLAGS) $(OBJECTS) -o "$(EXE)"
135else
Peter Collingbourne518f03d2011-06-20 19:06:04 +0000136$(EXE) : $(OBJECTS) $(DYLIB_FILENAME)
137 $(LD) $(LDFLAGS) $(OBJECTS) -L. -l$(DYLIB_NAME) -o "$(EXE)"
Johnny Chen9bc867a2010-08-23 23:56:08 +0000138endif
139
140#----------------------------------------------------------------------
141# Make the dylib
142#----------------------------------------------------------------------
Peter Collingbourne518f03d2011-06-20 19:06:04 +0000143$(DYLIB_FILENAME) : $(DYLIB_OBJECTS)
144ifeq "$(OS)" "Darwin"
145 $(LD) $(LDFLAGS) $(DYLIB_OBJECTS) -install_name "@executable_path/$(DYLIB_FILENAME)" -dynamiclib -o "$(DYLIB_FILENAME)"
146else
147 $(LD) $(LDFLAGS) $(DYLIB_OBJECTS) -shared -o "$(DYLIB_FILENAME)"
148endif
Johnny Chen9bc867a2010-08-23 23:56:08 +0000149
150#----------------------------------------------------------------------
151# Automatic variables based on items already entered. Below we create
152# an objects lists from the list of sources by replacing all entries
153# that end with .c with .o, and we also create a list of prerequisite
154# files by replacing all .c files with .d.
155#----------------------------------------------------------------------
156PREREQS := $(OBJECTS:.o=.d)
Johnny Chene3dc0f02010-08-24 16:35:00 +0000157ifneq "$(DYLIB_NAME)" ""
158 DYLIB_PREREQS := $(DYLIB_OBJECTS:.o=.d)
159endif
Johnny Chen9bc867a2010-08-23 23:56:08 +0000160
161#----------------------------------------------------------------------
162# Rule for Generating Prerequisites Automatically using .d files and
163# the compiler -MM option. The -M option will list all system headers,
164# and the -MM option will list all non-system dependencies.
165#----------------------------------------------------------------------
166%.d: %.c
167 @set -e; rm -f $@; \
Johnny Chen189bff12011-08-04 20:44:56 +0000168 $(CC) -M $(CFLAGS) $< > $@.$$$$; \
Johnny Chen9bc867a2010-08-23 23:56:08 +0000169 sed 's,\($*\)\.o[ :]*,\1.o $@ : ,g' < $@.$$$$ > $@; \
170 rm -f $@.$$$$
171
172%.d: %.cpp
173 @set -e; rm -f $@; \
Johnny Chen189bff12011-08-04 20:44:56 +0000174 $(CC) -M $(CXXFLAGS) $< > $@.$$$$; \
Johnny Chen9bc867a2010-08-23 23:56:08 +0000175 sed 's,\($*\)\.o[ :]*,\1.o $@ : ,g' < $@.$$$$ > $@; \
176 rm -f $@.$$$$
177
178%.d: %.m
179 @set -e; rm -f $@; \
Johnny Chen189bff12011-08-04 20:44:56 +0000180 $(CC) -M $(CFLAGS) $< > $@.$$$$; \
Johnny Chen9bc867a2010-08-23 23:56:08 +0000181 sed 's,\($*\)\.o[ :]*,\1.o $@ : ,g' < $@.$$$$ > $@; \
182 rm -f $@.$$$$
183
184%.d: %.mm
185 @set -e; rm -f $@; \
Johnny Chen189bff12011-08-04 20:44:56 +0000186 $(CC) -M $(CXXFLAGS) $< > $@.$$$$; \
Johnny Chen9bc867a2010-08-23 23:56:08 +0000187 sed 's,\($*\)\.o[ :]*,\1.o $@ : ,g' < $@.$$$$ > $@; \
188 rm -f $@.$$$$
189
190#----------------------------------------------------------------------
191# Include all of the makefiles for each source file so we don't have
192# to manually track all of the prerequisites for each source file.
193#----------------------------------------------------------------------
194sinclude $(PREREQS)
Johnny Chene3dc0f02010-08-24 16:35:00 +0000195ifneq "$(DYLIB_NAME)" ""
196 sinclude $(DYLIB_PREREQS)
197endif
Johnny Chen9bc867a2010-08-23 23:56:08 +0000198
199.PHONY: clean
200dsym: $(DSYM)
201all: $(EXE) $(DSYM)
Johnny Chene1030cd2010-09-27 20:44:46 +0000202clean::
Johnny Chen9bc867a2010-08-23 23:56:08 +0000203ifeq "$(DYLIB_NAME)" ""
204 rm -rf "$(EXE)" "$(DSYM)" $(OBJECTS) $(PREREQS)
205else
Johnny Chen6b0a1e32011-11-14 18:37:49 +0000206 rm -rf "$(EXE)" "$(DSYM)" $(OBJECTS) $(PREREQS) $(DYLIB_OBJECTS) $(DYLIB_PREREQS) $(DYLIB_FILENAME) $(DYLIB_FILENAME).dSYM
Johnny Chen9bc867a2010-08-23 23:56:08 +0000207endif
Johnny Chenfa380412011-01-14 21:18:12 +0000208
209#----------------------------------------------------------------------
210# From http://blog.melski.net/tag/debugging-makefiles/
211#
212# Usage: make print-CC print-CXX print-LD
213#----------------------------------------------------------------------
214print-%:
215 @echo '$*=$($*)'
216 @echo ' origin = $(origin $*)'
217 @echo ' flavor = $(flavor $*)'
218 @echo ' value = $(value $*)'
Johnny Chen8b2c3212011-01-28 17:22:29 +0000219
220
221### Local Variables: ###
222### mode:makefile ###
223### End: ###