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