blob: 08293b2c37cf05e55e840a43366a6c81a0a2d52d [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.
15#----------------------------------------------------------------------
16ifeq "$(ARCH)" ""
17 ARCH = x86_64
18endif
19
20#----------------------------------------------------------------------
Johnny Chend43e2082011-01-14 20:46:49 +000021# CC defaults to gcc.
Johnny Chen6055b442011-01-14 20:55:13 +000022# See also these functions:
23# o cxx_compiler
24# o cxx_linker
Johnny Chend43e2082011-01-14 20:46:49 +000025#----------------------------------------------------------------------
26CC ?= gcc
27ifeq "$(CC)" "cc"
28 CC = gcc
29endif
30
31#----------------------------------------------------------------------
Johnny Chen9bc867a2010-08-23 23:56:08 +000032# Change any build/tool options needed
33#----------------------------------------------------------------------
34DS := /usr/bin/dsymutil
35DSFLAGS =
Johnny Chen6c17c082010-09-16 20:54:06 +000036CFLAGS ?=-arch $(ARCH) -gdwarf-2 -O0
Johnny Chen832d2332010-09-30 01:22:34 +000037CXXFLAGS +=$(CFLAGS)
Johnny Chen1394a4b2010-09-13 20:54:30 +000038LD = $(CC)
Johnny Chen9bc867a2010-08-23 23:56:08 +000039LDFLAGS ?= $(CFLAGS)
40OBJECTS =
41EXE = a.out
42DSYM = $(EXE).dSYM
43
Johnny Chenbdb4efc2011-01-14 18:19:53 +000044# Function that returns the counterpart C++ compiler, given $(CC) as arg.
45cxx_compiler = $(if $(findstring clang,$(1)), $(subst clang,clang++,$(1)), $(if $(findstring llvm-gcc,$(1)), $(subst llvm-gcc,llvm-g++,$(1)), $(subst gcc,g++,$(1))))
46
47# Function that returns the C++ linker, given $(CC) as arg.
48cxx_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 +000049
Johnny Chen9bc867a2010-08-23 23:56:08 +000050#----------------------------------------------------------------------
51# dylib settings
52#----------------------------------------------------------------------
53ifneq "$(strip $(DYLIB_C_SOURCES))" ""
54 DYLIB_OBJECTS +=$(strip $(DYLIB_C_SOURCES:.c=.o))
55endif
56
57
58#----------------------------------------------------------------------
59# Check if we have any C source files
60#----------------------------------------------------------------------
61ifneq "$(strip $(C_SOURCES))" ""
62 OBJECTS +=$(strip $(C_SOURCES:.c=.o))
63endif
64
65#----------------------------------------------------------------------
66# Check if we have any C++ source files
67#----------------------------------------------------------------------
68ifneq "$(strip $(CXX_SOURCES))" ""
69 OBJECTS +=$(strip $(CXX_SOURCES:.cpp=.o))
Johnny Chen832d2332010-09-30 01:22:34 +000070 CXX = $(call cxx_compiler,$(CC))
Johnny Chenbdb4efc2011-01-14 18:19:53 +000071 LD = $(call cxx_linker,$(CC))
Johnny Chen9bc867a2010-08-23 23:56:08 +000072endif
73
74#----------------------------------------------------------------------
75# Check if we have any ObjC source files
76#----------------------------------------------------------------------
77ifneq "$(strip $(OBJC_SOURCES))" ""
78 OBJECTS +=$(strip $(OBJC_SOURCES:.m=.o))
79 LDFLAGS +=-lobjc
80endif
81
82#----------------------------------------------------------------------
83# Check if we have any ObjC++ source files
84#----------------------------------------------------------------------
85ifneq "$(strip $(OBJCXX_SOURCES))" ""
86 OBJECTS +=$(strip $(OBJCXX_SOURCES:.mm=.o))
Johnny Chen832d2332010-09-30 01:22:34 +000087 CXX = $(call cxx_compiler,$(CC))
Johnny Chenbdb4efc2011-01-14 18:19:53 +000088 LD = $(call cxx_linker,$(CC))
Johnny Chen9bc867a2010-08-23 23:56:08 +000089 ifeq $(findstring lobjc,$(LDFLAGS)) ""
90 LDFLAGS +=-lobjc
91 endif
92endif
93
94
95#----------------------------------------------------------------------
96# Make the dSYM file from the executable if $(MAKE_DSYM) != "NO"
97#----------------------------------------------------------------------
98ifneq "$(MAKE_DSYM)" "NO"
99$(DSYM) : $(EXE)
100 $(DS) $(DSFLAGS) -o "$(DSYM)" "$(EXE)"
101endif
102
103#----------------------------------------------------------------------
104# Compile the executable from all the objects.
105#----------------------------------------------------------------------
106ifeq "$(DYLIB_NAME)" ""
107$(EXE) : $(OBJECTS)
108 $(LD) $(LDFLAGS) $(OBJECTS) -o "$(EXE)"
109else
110$(EXE) : $(OBJECTS) $(DYLIB_NAME)
111 $(LD) $(LDFLAGS) $(OBJECTS) -L. -l$(subst lib,,$(basename $(DYLIB_NAME))) -o "$(EXE)"
112endif
113
114#----------------------------------------------------------------------
115# Make the dylib
116#----------------------------------------------------------------------
117$(DYLIB_NAME) : $(DYLIB_OBJECTS)
118 $(LD) $(LDFLAGS) $(DYLIB_OBJECTS) -install_name "@executable_path/$(DYLIB_NAME)" -dynamiclib -o "$(DYLIB_NAME)"
119
120#----------------------------------------------------------------------
121# Automatic variables based on items already entered. Below we create
122# an objects lists from the list of sources by replacing all entries
123# that end with .c with .o, and we also create a list of prerequisite
124# files by replacing all .c files with .d.
125#----------------------------------------------------------------------
126PREREQS := $(OBJECTS:.o=.d)
Johnny Chene3dc0f02010-08-24 16:35:00 +0000127ifneq "$(DYLIB_NAME)" ""
128 DYLIB_PREREQS := $(DYLIB_OBJECTS:.o=.d)
129endif
Johnny Chen9bc867a2010-08-23 23:56:08 +0000130
131#----------------------------------------------------------------------
132# Rule for Generating Prerequisites Automatically using .d files and
133# the compiler -MM option. The -M option will list all system headers,
134# and the -MM option will list all non-system dependencies.
135#----------------------------------------------------------------------
136%.d: %.c
137 @set -e; rm -f $@; \
138 $(CC) -M $(CPPFLAGS) $< > $@.$$$$; \
139 sed 's,\($*\)\.o[ :]*,\1.o $@ : ,g' < $@.$$$$ > $@; \
140 rm -f $@.$$$$
141
142%.d: %.cpp
143 @set -e; rm -f $@; \
144 $(CC) -M $(CPPFLAGS) $< > $@.$$$$; \
145 sed 's,\($*\)\.o[ :]*,\1.o $@ : ,g' < $@.$$$$ > $@; \
146 rm -f $@.$$$$
147
148%.d: %.m
149 @set -e; rm -f $@; \
150 $(CC) -M $(CPPFLAGS) $< > $@.$$$$; \
151 sed 's,\($*\)\.o[ :]*,\1.o $@ : ,g' < $@.$$$$ > $@; \
152 rm -f $@.$$$$
153
154%.d: %.mm
155 @set -e; rm -f $@; \
156 $(CC) -M $(CPPFLAGS) $< > $@.$$$$; \
157 sed 's,\($*\)\.o[ :]*,\1.o $@ : ,g' < $@.$$$$ > $@; \
158 rm -f $@.$$$$
159
160#----------------------------------------------------------------------
161# Include all of the makefiles for each source file so we don't have
162# to manually track all of the prerequisites for each source file.
163#----------------------------------------------------------------------
164sinclude $(PREREQS)
Johnny Chene3dc0f02010-08-24 16:35:00 +0000165ifneq "$(DYLIB_NAME)" ""
166 sinclude $(DYLIB_PREREQS)
167endif
Johnny Chen9bc867a2010-08-23 23:56:08 +0000168
169.PHONY: clean
170dsym: $(DSYM)
171all: $(EXE) $(DSYM)
Johnny Chene1030cd2010-09-27 20:44:46 +0000172clean::
Johnny Chen9bc867a2010-08-23 23:56:08 +0000173ifeq "$(DYLIB_NAME)" ""
174 rm -rf "$(EXE)" "$(DSYM)" $(OBJECTS) $(PREREQS)
175else
176 rm -rf "$(EXE)" "$(DSYM)" $(OBJECTS) $(PREREQS) $(DYLIB_OBJECTS) $(DYLIB_NAME) $(DYLIB_NAME).dSYM
177endif