| 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. |
| Peter Collingbourne | 518f03d | 2011-06-20 19:06:04 +0000 | [diff] [blame] | 15 | # If OS is not defined, use 'uname -s' to determine the OS name. |
| Johnny Chen | 6c17c08 | 2010-09-16 20:54:06 +0000 | [diff] [blame] | 16 | #---------------------------------------------------------------------- |
| 17 | ifeq "$(ARCH)" "" |
| 18 | ARCH = x86_64 |
| 19 | endif |
| 20 | |
| Peter Collingbourne | 518f03d | 2011-06-20 19:06:04 +0000 | [diff] [blame] | 21 | ifeq "$(OS)" "" |
| 22 | OS = $(shell uname -s) |
| 23 | endif |
| 24 | |
| Johnny Chen | 6c17c08 | 2010-09-16 20:54:06 +0000 | [diff] [blame] | 25 | #---------------------------------------------------------------------- |
| Johnny Chen | 597cbbb | 2011-08-23 21:54:10 +0000 | [diff] [blame] | 26 | # CC defaults to clang. |
| Johnny Chen | 4ab4a9b | 2011-08-24 18:12:53 +0000 | [diff] [blame] | 27 | # |
| 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 Chen | 6055b44 | 2011-01-14 20:55:13 +0000 | [diff] [blame] | 32 | # See also these functions: |
| 33 | # o cxx_compiler |
| 34 | # o cxx_linker |
| Johnny Chen | d43e208 | 2011-01-14 20:46:49 +0000 | [diff] [blame] | 35 | #---------------------------------------------------------------------- |
| Johnny Chen | 597cbbb | 2011-08-23 21:54:10 +0000 | [diff] [blame] | 36 | CC ?= clang |
| Johnny Chen | d43e208 | 2011-01-14 20:46:49 +0000 | [diff] [blame] | 37 | ifeq "$(CC)" "cc" |
| Johnny Chen | 597cbbb | 2011-08-23 21:54:10 +0000 | [diff] [blame] | 38 | CC = clang |
| Johnny Chen | d43e208 | 2011-01-14 20:46:49 +0000 | [diff] [blame] | 39 | endif |
| 40 | |
| 41 | #---------------------------------------------------------------------- |
| Johnny Chen | 9bc867a | 2010-08-23 23:56:08 +0000 | [diff] [blame] | 42 | # Change any build/tool options needed |
| 43 | #---------------------------------------------------------------------- |
| Peter Collingbourne | 518f03d | 2011-06-20 19:06:04 +0000 | [diff] [blame] | 44 | CFLAGS ?= -gdwarf-2 -O0 |
| Johnny Chen | 19bfde3 | 2011-08-04 18:17:16 +0000 | [diff] [blame] | 45 | CFLAGS += $(FRAMEWORK_INCLUDES) |
| Peter Collingbourne | 518f03d | 2011-06-20 19:06:04 +0000 | [diff] [blame] | 46 | ifeq "$(OS)" "Darwin" |
| 47 | CFLAGS += -arch $(ARCH) |
| Greg Clayton | 3bffb08 | 2011-12-10 02:15:28 +0000 | [diff] [blame^] | 48 | DS := dsymutil |
| Peter Collingbourne | 518f03d | 2011-06-20 19:06:04 +0000 | [diff] [blame] | 49 | DSFLAGS = |
| 50 | DSYM = $(EXE).dSYM |
| 51 | endif |
| 52 | |
| Johnny Chen | 53e2edb | 2011-08-09 20:07:16 +0000 | [diff] [blame] | 53 | CXXFLAGS +=$(CFLAGS) |
| 54 | LD = $(CC) |
| 55 | LDFLAGS ?= $(CFLAGS) |
| 56 | LDFLAGS += $(LD_EXTRAS) |
| 57 | OBJECTS = |
| 58 | EXE ?= a.out |
| 59 | |
| Peter Collingbourne | 518f03d | 2011-06-20 19:06:04 +0000 | [diff] [blame] | 60 | ifneq "$(DYLIB_NAME)" "" |
| 61 | ifeq "$(OS)" "Darwin" |
| 62 | DYLIB_FILENAME = lib$(DYLIB_NAME).dylib |
| 63 | else |
| 64 | DYLIB_FILENAME = lib$(DYLIB_NAME).so |
| 65 | endif |
| 66 | endif |
| Johnny Chen | 9bc867a | 2010-08-23 23:56:08 +0000 | [diff] [blame] | 67 | |
| Johnny Chen | bdb4efc | 2011-01-14 18:19:53 +0000 | [diff] [blame] | 68 | # Function that returns the counterpart C++ compiler, given $(CC) as arg. |
| 69 | cxx_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. |
| 72 | cxx_linker = $(if $(findstring clang,$(1)), $(subst clang,g++,$(1)), $(if $(findstring llvm-gcc,$(1)), $(subst llvm-gcc,g++,$(1)), $(subst gcc,g++,$(1)))) |
| Johnny Chen | 832d233 | 2010-09-30 01:22:34 +0000 | [diff] [blame] | 73 | |
| Johnny Chen | 9bc867a | 2010-08-23 23:56:08 +0000 | [diff] [blame] | 74 | #---------------------------------------------------------------------- |
| 75 | # dylib settings |
| 76 | #---------------------------------------------------------------------- |
| 77 | ifneq "$(strip $(DYLIB_C_SOURCES))" "" |
| 78 | DYLIB_OBJECTS +=$(strip $(DYLIB_C_SOURCES:.c=.o)) |
| 79 | endif |
| 80 | |
| 81 | |
| 82 | #---------------------------------------------------------------------- |
| 83 | # Check if we have any C source files |
| 84 | #---------------------------------------------------------------------- |
| 85 | ifneq "$(strip $(C_SOURCES))" "" |
| 86 | OBJECTS +=$(strip $(C_SOURCES:.c=.o)) |
| 87 | endif |
| 88 | |
| 89 | #---------------------------------------------------------------------- |
| 90 | # Check if we have any C++ source files |
| 91 | #---------------------------------------------------------------------- |
| 92 | ifneq "$(strip $(CXX_SOURCES))" "" |
| 93 | OBJECTS +=$(strip $(CXX_SOURCES:.cpp=.o)) |
| Johnny Chen | 832d233 | 2010-09-30 01:22:34 +0000 | [diff] [blame] | 94 | CXX = $(call cxx_compiler,$(CC)) |
| Johnny Chen | bdb4efc | 2011-01-14 18:19:53 +0000 | [diff] [blame] | 95 | LD = $(call cxx_linker,$(CC)) |
| Johnny Chen | 9bc867a | 2010-08-23 23:56:08 +0000 | [diff] [blame] | 96 | endif |
| 97 | |
| 98 | #---------------------------------------------------------------------- |
| 99 | # Check if we have any ObjC source files |
| 100 | #---------------------------------------------------------------------- |
| 101 | ifneq "$(strip $(OBJC_SOURCES))" "" |
| 102 | OBJECTS +=$(strip $(OBJC_SOURCES:.m=.o)) |
| 103 | LDFLAGS +=-lobjc |
| 104 | endif |
| 105 | |
| 106 | #---------------------------------------------------------------------- |
| 107 | # Check if we have any ObjC++ source files |
| 108 | #---------------------------------------------------------------------- |
| 109 | ifneq "$(strip $(OBJCXX_SOURCES))" "" |
| 110 | OBJECTS +=$(strip $(OBJCXX_SOURCES:.mm=.o)) |
| Johnny Chen | 832d233 | 2010-09-30 01:22:34 +0000 | [diff] [blame] | 111 | CXX = $(call cxx_compiler,$(CC)) |
| Johnny Chen | bdb4efc | 2011-01-14 18:19:53 +0000 | [diff] [blame] | 112 | LD = $(call cxx_linker,$(CC)) |
| Johnny Chen | 9bc867a | 2010-08-23 23:56:08 +0000 | [diff] [blame] | 113 | ifeq $(findstring lobjc,$(LDFLAGS)) "" |
| 114 | LDFLAGS +=-lobjc |
| 115 | endif |
| 116 | endif |
| 117 | |
| 118 | |
| 119 | #---------------------------------------------------------------------- |
| 120 | # Make the dSYM file from the executable if $(MAKE_DSYM) != "NO" |
| 121 | #---------------------------------------------------------------------- |
| Peter Collingbourne | 518f03d | 2011-06-20 19:06:04 +0000 | [diff] [blame] | 122 | ifeq "$(OS)" "Darwin" |
| Johnny Chen | 9101639 | 2011-06-20 20:08:26 +0000 | [diff] [blame] | 123 | ifneq "$(MAKE_DSYM)" "NO" |
| Johnny Chen | 9bc867a | 2010-08-23 23:56:08 +0000 | [diff] [blame] | 124 | $(DSYM) : $(EXE) |
| 125 | $(DS) $(DSFLAGS) -o "$(DSYM)" "$(EXE)" |
| Johnny Chen | 9101639 | 2011-06-20 20:08:26 +0000 | [diff] [blame] | 126 | endif |
| Johnny Chen | 9bc867a | 2010-08-23 23:56:08 +0000 | [diff] [blame] | 127 | endif |
| 128 | |
| 129 | #---------------------------------------------------------------------- |
| 130 | # Compile the executable from all the objects. |
| 131 | #---------------------------------------------------------------------- |
| 132 | ifeq "$(DYLIB_NAME)" "" |
| 133 | $(EXE) : $(OBJECTS) |
| 134 | $(LD) $(LDFLAGS) $(OBJECTS) -o "$(EXE)" |
| 135 | else |
| Peter Collingbourne | 518f03d | 2011-06-20 19:06:04 +0000 | [diff] [blame] | 136 | $(EXE) : $(OBJECTS) $(DYLIB_FILENAME) |
| 137 | $(LD) $(LDFLAGS) $(OBJECTS) -L. -l$(DYLIB_NAME) -o "$(EXE)" |
| Johnny Chen | 9bc867a | 2010-08-23 23:56:08 +0000 | [diff] [blame] | 138 | endif |
| 139 | |
| 140 | #---------------------------------------------------------------------- |
| 141 | # Make the dylib |
| 142 | #---------------------------------------------------------------------- |
| Peter Collingbourne | 518f03d | 2011-06-20 19:06:04 +0000 | [diff] [blame] | 143 | $(DYLIB_FILENAME) : $(DYLIB_OBJECTS) |
| 144 | ifeq "$(OS)" "Darwin" |
| 145 | $(LD) $(LDFLAGS) $(DYLIB_OBJECTS) -install_name "@executable_path/$(DYLIB_FILENAME)" -dynamiclib -o "$(DYLIB_FILENAME)" |
| 146 | else |
| 147 | $(LD) $(LDFLAGS) $(DYLIB_OBJECTS) -shared -o "$(DYLIB_FILENAME)" |
| 148 | endif |
| Johnny Chen | 9bc867a | 2010-08-23 23:56:08 +0000 | [diff] [blame] | 149 | |
| 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 | #---------------------------------------------------------------------- |
| 156 | PREREQS := $(OBJECTS:.o=.d) |
| Johnny Chen | e3dc0f0 | 2010-08-24 16:35:00 +0000 | [diff] [blame] | 157 | ifneq "$(DYLIB_NAME)" "" |
| 158 | DYLIB_PREREQS := $(DYLIB_OBJECTS:.o=.d) |
| 159 | endif |
| Johnny Chen | 9bc867a | 2010-08-23 23:56:08 +0000 | [diff] [blame] | 160 | |
| 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 Chen | 189bff1 | 2011-08-04 20:44:56 +0000 | [diff] [blame] | 168 | $(CC) -M $(CFLAGS) $< > $@.$$$$; \ |
| Johnny Chen | 9bc867a | 2010-08-23 23:56:08 +0000 | [diff] [blame] | 169 | sed 's,\($*\)\.o[ :]*,\1.o $@ : ,g' < $@.$$$$ > $@; \ |
| 170 | rm -f $@.$$$$ |
| 171 | |
| 172 | %.d: %.cpp |
| 173 | @set -e; rm -f $@; \ |
| Johnny Chen | 189bff1 | 2011-08-04 20:44:56 +0000 | [diff] [blame] | 174 | $(CC) -M $(CXXFLAGS) $< > $@.$$$$; \ |
| Johnny Chen | 9bc867a | 2010-08-23 23:56:08 +0000 | [diff] [blame] | 175 | sed 's,\($*\)\.o[ :]*,\1.o $@ : ,g' < $@.$$$$ > $@; \ |
| 176 | rm -f $@.$$$$ |
| 177 | |
| 178 | %.d: %.m |
| 179 | @set -e; rm -f $@; \ |
| Johnny Chen | 189bff1 | 2011-08-04 20:44:56 +0000 | [diff] [blame] | 180 | $(CC) -M $(CFLAGS) $< > $@.$$$$; \ |
| Johnny Chen | 9bc867a | 2010-08-23 23:56:08 +0000 | [diff] [blame] | 181 | sed 's,\($*\)\.o[ :]*,\1.o $@ : ,g' < $@.$$$$ > $@; \ |
| 182 | rm -f $@.$$$$ |
| 183 | |
| 184 | %.d: %.mm |
| 185 | @set -e; rm -f $@; \ |
| Johnny Chen | 189bff1 | 2011-08-04 20:44:56 +0000 | [diff] [blame] | 186 | $(CC) -M $(CXXFLAGS) $< > $@.$$$$; \ |
| Johnny Chen | 9bc867a | 2010-08-23 23:56:08 +0000 | [diff] [blame] | 187 | 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 | #---------------------------------------------------------------------- |
| 194 | sinclude $(PREREQS) |
| Johnny Chen | e3dc0f0 | 2010-08-24 16:35:00 +0000 | [diff] [blame] | 195 | ifneq "$(DYLIB_NAME)" "" |
| 196 | sinclude $(DYLIB_PREREQS) |
| 197 | endif |
| Johnny Chen | 9bc867a | 2010-08-23 23:56:08 +0000 | [diff] [blame] | 198 | |
| 199 | .PHONY: clean |
| 200 | dsym: $(DSYM) |
| 201 | all: $(EXE) $(DSYM) |
| Johnny Chen | e1030cd | 2010-09-27 20:44:46 +0000 | [diff] [blame] | 202 | clean:: |
| Johnny Chen | 9bc867a | 2010-08-23 23:56:08 +0000 | [diff] [blame] | 203 | ifeq "$(DYLIB_NAME)" "" |
| 204 | rm -rf "$(EXE)" "$(DSYM)" $(OBJECTS) $(PREREQS) |
| 205 | else |
| Johnny Chen | 6b0a1e3 | 2011-11-14 18:37:49 +0000 | [diff] [blame] | 206 | rm -rf "$(EXE)" "$(DSYM)" $(OBJECTS) $(PREREQS) $(DYLIB_OBJECTS) $(DYLIB_PREREQS) $(DYLIB_FILENAME) $(DYLIB_FILENAME).dSYM |
| Johnny Chen | 9bc867a | 2010-08-23 23:56:08 +0000 | [diff] [blame] | 207 | endif |
| Johnny Chen | fa38041 | 2011-01-14 21:18:12 +0000 | [diff] [blame] | 208 | |
| 209 | #---------------------------------------------------------------------- |
| 210 | # From http://blog.melski.net/tag/debugging-makefiles/ |
| 211 | # |
| 212 | # Usage: make print-CC print-CXX print-LD |
| 213 | #---------------------------------------------------------------------- |
| 214 | print-%: |
| 215 | @echo '$*=$($*)' |
| 216 | @echo ' origin = $(origin $*)' |
| 217 | @echo ' flavor = $(flavor $*)' |
| 218 | @echo ' value = $(value $*)' |
| Johnny Chen | 8b2c321 | 2011-01-28 17:22:29 +0000 | [diff] [blame] | 219 | |
| 220 | |
| 221 | ### Local Variables: ### |
| 222 | ### mode:makefile ### |
| 223 | ### End: ### |