| Johnny Chen | 9bc867a | 2010-08-23 23:56:08 +0000 | [diff] [blame] | 1 | #---------------------------------------------------------------------- | 
|  | 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 Chen | 6c17c08 | 2010-09-16 20:54:06 +0000 | [diff] [blame^] | 14 | # If ARCH is not defined, default to x86_64. | 
|  | 15 | #---------------------------------------------------------------------- | 
|  | 16 | ifeq "$(ARCH)" "" | 
|  | 17 | ARCH = x86_64 | 
|  | 18 | endif | 
|  | 19 |  | 
|  | 20 | #---------------------------------------------------------------------- | 
| Johnny Chen | 9bc867a | 2010-08-23 23:56:08 +0000 | [diff] [blame] | 21 | # Change any build/tool options needed | 
|  | 22 | #---------------------------------------------------------------------- | 
|  | 23 | DS := /usr/bin/dsymutil | 
|  | 24 | DSFLAGS = | 
|  | 25 | CC = gcc | 
| Johnny Chen | 6c17c08 | 2010-09-16 20:54:06 +0000 | [diff] [blame^] | 26 | CFLAGS ?=-arch $(ARCH) -gdwarf-2 -O0 | 
| Johnny Chen | 9bc867a | 2010-08-23 23:56:08 +0000 | [diff] [blame] | 27 | CPLUSPLUSFLAGS +=$(CFLAGS) | 
|  | 28 | CPPFLAGS +=$(CFLAGS) | 
| Johnny Chen | 1394a4b | 2010-09-13 20:54:30 +0000 | [diff] [blame] | 29 | LD = $(CC) | 
| Johnny Chen | 9bc867a | 2010-08-23 23:56:08 +0000 | [diff] [blame] | 30 | LDFLAGS ?= $(CFLAGS) | 
|  | 31 | OBJECTS = | 
|  | 32 | EXE = a.out | 
|  | 33 | DSYM = $(EXE).dSYM | 
|  | 34 |  | 
|  | 35 | #---------------------------------------------------------------------- | 
|  | 36 | # dylib settings | 
|  | 37 | #---------------------------------------------------------------------- | 
|  | 38 | ifneq "$(strip $(DYLIB_C_SOURCES))" "" | 
|  | 39 | DYLIB_OBJECTS +=$(strip $(DYLIB_C_SOURCES:.c=.o)) | 
|  | 40 | endif | 
|  | 41 |  | 
|  | 42 |  | 
|  | 43 | #---------------------------------------------------------------------- | 
|  | 44 | # Check if we have any C source files | 
|  | 45 | #---------------------------------------------------------------------- | 
|  | 46 | ifneq "$(strip $(C_SOURCES))" "" | 
|  | 47 | OBJECTS +=$(strip $(C_SOURCES:.c=.o)) | 
|  | 48 | endif | 
|  | 49 |  | 
|  | 50 | #---------------------------------------------------------------------- | 
|  | 51 | # Check if we have any C++ source files | 
|  | 52 | #---------------------------------------------------------------------- | 
|  | 53 | ifneq "$(strip $(CXX_SOURCES))" "" | 
|  | 54 | OBJECTS +=$(strip $(CXX_SOURCES:.cpp=.o)) | 
|  | 55 | LD = g++ | 
|  | 56 | endif | 
|  | 57 |  | 
|  | 58 | #---------------------------------------------------------------------- | 
|  | 59 | # Check if we have any ObjC source files | 
|  | 60 | #---------------------------------------------------------------------- | 
|  | 61 | ifneq "$(strip $(OBJC_SOURCES))" "" | 
|  | 62 | OBJECTS +=$(strip $(OBJC_SOURCES:.m=.o)) | 
|  | 63 | LDFLAGS +=-lobjc | 
|  | 64 | endif | 
|  | 65 |  | 
|  | 66 | #---------------------------------------------------------------------- | 
|  | 67 | # Check if we have any ObjC++ source files | 
|  | 68 | #---------------------------------------------------------------------- | 
|  | 69 | ifneq "$(strip $(OBJCXX_SOURCES))" "" | 
|  | 70 | OBJECTS +=$(strip $(OBJCXX_SOURCES:.mm=.o)) | 
|  | 71 | LD = g++ | 
|  | 72 | ifeq $(findstring lobjc,$(LDFLAGS)) "" | 
|  | 73 | LDFLAGS +=-lobjc | 
|  | 74 | endif | 
|  | 75 | endif | 
|  | 76 |  | 
|  | 77 |  | 
|  | 78 | #---------------------------------------------------------------------- | 
|  | 79 | # Make the dSYM file from the executable if $(MAKE_DSYM) != "NO" | 
|  | 80 | #---------------------------------------------------------------------- | 
|  | 81 | ifneq "$(MAKE_DSYM)" "NO" | 
|  | 82 | $(DSYM) : $(EXE) | 
|  | 83 | $(DS) $(DSFLAGS) -o "$(DSYM)" "$(EXE)" | 
|  | 84 | endif | 
|  | 85 |  | 
|  | 86 | #---------------------------------------------------------------------- | 
|  | 87 | # Compile the executable from all the objects. | 
|  | 88 | #---------------------------------------------------------------------- | 
|  | 89 | ifeq "$(DYLIB_NAME)" "" | 
|  | 90 | $(EXE) : $(OBJECTS) | 
|  | 91 | $(LD) $(LDFLAGS) $(OBJECTS) -o "$(EXE)" | 
|  | 92 | else | 
|  | 93 | $(EXE) : $(OBJECTS) $(DYLIB_NAME) | 
|  | 94 | $(LD) $(LDFLAGS) $(OBJECTS) -L. -l$(subst lib,,$(basename $(DYLIB_NAME))) -o "$(EXE)" | 
|  | 95 | endif | 
|  | 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 | #---------------------------------------------------------------------- | 
|  | 109 | PREREQS := $(OBJECTS:.o=.d) | 
| Johnny Chen | e3dc0f0 | 2010-08-24 16:35:00 +0000 | [diff] [blame] | 110 | ifneq "$(DYLIB_NAME)" "" | 
|  | 111 | DYLIB_PREREQS := $(DYLIB_OBJECTS:.o=.d) | 
|  | 112 | endif | 
| Johnny Chen | 9bc867a | 2010-08-23 23:56:08 +0000 | [diff] [blame] | 113 |  | 
|  | 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 | #---------------------------------------------------------------------- | 
|  | 147 | sinclude $(PREREQS) | 
| Johnny Chen | e3dc0f0 | 2010-08-24 16:35:00 +0000 | [diff] [blame] | 148 | ifneq "$(DYLIB_NAME)" "" | 
|  | 149 | sinclude $(DYLIB_PREREQS) | 
|  | 150 | endif | 
| Johnny Chen | 9bc867a | 2010-08-23 23:56:08 +0000 | [diff] [blame] | 151 |  | 
|  | 152 | .PHONY: clean | 
|  | 153 | dsym:	$(DSYM) | 
|  | 154 | all:	$(EXE) $(DSYM) | 
|  | 155 | clean: | 
|  | 156 | ifeq "$(DYLIB_NAME)" "" | 
|  | 157 | rm -rf "$(EXE)" "$(DSYM)" $(OBJECTS) $(PREREQS) | 
|  | 158 | else | 
|  | 159 | rm -rf "$(EXE)" "$(DSYM)" $(OBJECTS) $(PREREQS) $(DYLIB_OBJECTS) $(DYLIB_NAME) $(DYLIB_NAME).dSYM | 
|  | 160 | endif |