blob: 844e297fd5da41fd19eb742fc6d9f7e27de51771 [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 Chen9bc867a2010-08-23 23:56:08 +000021# Change any build/tool options needed
22#----------------------------------------------------------------------
23DS := /usr/bin/dsymutil
24DSFLAGS =
25CC = gcc
Johnny Chen6c17c082010-09-16 20:54:06 +000026CFLAGS ?=-arch $(ARCH) -gdwarf-2 -O0
Johnny Chen9bc867a2010-08-23 23:56:08 +000027CPLUSPLUSFLAGS +=$(CFLAGS)
28CPPFLAGS +=$(CFLAGS)
Johnny Chen1394a4b2010-09-13 20:54:30 +000029LD = $(CC)
Johnny Chen9bc867a2010-08-23 23:56:08 +000030LDFLAGS ?= $(CFLAGS)
31OBJECTS =
32EXE = a.out
33DSYM = $(EXE).dSYM
34
35#----------------------------------------------------------------------
36# dylib settings
37#----------------------------------------------------------------------
38ifneq "$(strip $(DYLIB_C_SOURCES))" ""
39 DYLIB_OBJECTS +=$(strip $(DYLIB_C_SOURCES:.c=.o))
40endif
41
42
43#----------------------------------------------------------------------
44# Check if we have any C source files
45#----------------------------------------------------------------------
46ifneq "$(strip $(C_SOURCES))" ""
47 OBJECTS +=$(strip $(C_SOURCES:.c=.o))
48endif
49
50#----------------------------------------------------------------------
51# Check if we have any C++ source files
52#----------------------------------------------------------------------
53ifneq "$(strip $(CXX_SOURCES))" ""
54 OBJECTS +=$(strip $(CXX_SOURCES:.cpp=.o))
55 LD = g++
56endif
57
58#----------------------------------------------------------------------
59# Check if we have any ObjC source files
60#----------------------------------------------------------------------
61ifneq "$(strip $(OBJC_SOURCES))" ""
62 OBJECTS +=$(strip $(OBJC_SOURCES:.m=.o))
63 LDFLAGS +=-lobjc
64endif
65
66#----------------------------------------------------------------------
67# Check if we have any ObjC++ source files
68#----------------------------------------------------------------------
69ifneq "$(strip $(OBJCXX_SOURCES))" ""
70 OBJECTS +=$(strip $(OBJCXX_SOURCES:.mm=.o))
71 LD = g++
72 ifeq $(findstring lobjc,$(LDFLAGS)) ""
73 LDFLAGS +=-lobjc
74 endif
75endif
76
77
78#----------------------------------------------------------------------
79# Make the dSYM file from the executable if $(MAKE_DSYM) != "NO"
80#----------------------------------------------------------------------
81ifneq "$(MAKE_DSYM)" "NO"
82$(DSYM) : $(EXE)
83 $(DS) $(DSFLAGS) -o "$(DSYM)" "$(EXE)"
84endif
85
86#----------------------------------------------------------------------
87# Compile the executable from all the objects.
88#----------------------------------------------------------------------
89ifeq "$(DYLIB_NAME)" ""
90$(EXE) : $(OBJECTS)
91 $(LD) $(LDFLAGS) $(OBJECTS) -o "$(EXE)"
92else
93$(EXE) : $(OBJECTS) $(DYLIB_NAME)
94 $(LD) $(LDFLAGS) $(OBJECTS) -L. -l$(subst lib,,$(basename $(DYLIB_NAME))) -o "$(EXE)"
95endif
96
97#----------------------------------------------------------------------
98# Make the dylib
99#----------------------------------------------------------------------
100$(DYLIB_NAME) : $(DYLIB_OBJECTS)
101 $(LD) $(LDFLAGS) $(DYLIB_OBJECTS) -install_name "@executable_path/$(DYLIB_NAME)" -dynamiclib -o "$(DYLIB_NAME)"
102
103#----------------------------------------------------------------------
104# Automatic variables based on items already entered. Below we create
105# an objects lists from the list of sources by replacing all entries
106# that end with .c with .o, and we also create a list of prerequisite
107# files by replacing all .c files with .d.
108#----------------------------------------------------------------------
109PREREQS := $(OBJECTS:.o=.d)
Johnny Chene3dc0f02010-08-24 16:35:00 +0000110ifneq "$(DYLIB_NAME)" ""
111 DYLIB_PREREQS := $(DYLIB_OBJECTS:.o=.d)
112endif
Johnny Chen9bc867a2010-08-23 23:56:08 +0000113
114#----------------------------------------------------------------------
115# Rule for Generating Prerequisites Automatically using .d files and
116# the compiler -MM option. The -M option will list all system headers,
117# and the -MM option will list all non-system dependencies.
118#----------------------------------------------------------------------
119%.d: %.c
120 @set -e; rm -f $@; \
121 $(CC) -M $(CPPFLAGS) $< > $@.$$$$; \
122 sed 's,\($*\)\.o[ :]*,\1.o $@ : ,g' < $@.$$$$ > $@; \
123 rm -f $@.$$$$
124
125%.d: %.cpp
126 @set -e; rm -f $@; \
127 $(CC) -M $(CPPFLAGS) $< > $@.$$$$; \
128 sed 's,\($*\)\.o[ :]*,\1.o $@ : ,g' < $@.$$$$ > $@; \
129 rm -f $@.$$$$
130
131%.d: %.m
132 @set -e; rm -f $@; \
133 $(CC) -M $(CPPFLAGS) $< > $@.$$$$; \
134 sed 's,\($*\)\.o[ :]*,\1.o $@ : ,g' < $@.$$$$ > $@; \
135 rm -f $@.$$$$
136
137%.d: %.mm
138 @set -e; rm -f $@; \
139 $(CC) -M $(CPPFLAGS) $< > $@.$$$$; \
140 sed 's,\($*\)\.o[ :]*,\1.o $@ : ,g' < $@.$$$$ > $@; \
141 rm -f $@.$$$$
142
143#----------------------------------------------------------------------
144# Include all of the makefiles for each source file so we don't have
145# to manually track all of the prerequisites for each source file.
146#----------------------------------------------------------------------
147sinclude $(PREREQS)
Johnny Chene3dc0f02010-08-24 16:35:00 +0000148ifneq "$(DYLIB_NAME)" ""
149 sinclude $(DYLIB_PREREQS)
150endif
Johnny Chen9bc867a2010-08-23 23:56:08 +0000151
152.PHONY: clean
153dsym: $(DSYM)
154all: $(EXE) $(DSYM)
155clean:
156ifeq "$(DYLIB_NAME)" ""
157 rm -rf "$(EXE)" "$(DSYM)" $(OBJECTS) $(PREREQS)
158else
159 rm -rf "$(EXE)" "$(DSYM)" $(OBJECTS) $(PREREQS) $(DYLIB_OBJECTS) $(DYLIB_NAME) $(DYLIB_NAME).dSYM
160endif