| 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 := |
| Johnny Chen | e9f9770 | 2012-01-11 01:42:58 +0000 | [diff] [blame^] | 9 | # DYLIB_CXX_SOURCES := |
| 10 | # |
| 11 | # Specifying DYLIB_ONLY has the effect of building dylib only, skipping |
| 12 | # the building of the a.out executable program. For example, |
| 13 | # DYLIB_ONLY := YES |
| 14 | # |
| Johnny Chen | 9bc867a | 2010-08-23 23:56:08 +0000 | [diff] [blame] | 15 | |
| 16 | # Uncomment line below for debugging shell commands |
| 17 | # SHELL = /bin/sh -x |
| 18 | |
| 19 | #---------------------------------------------------------------------- |
| Johnny Chen | 6c17c08 | 2010-09-16 20:54:06 +0000 | [diff] [blame] | 20 | # If ARCH is not defined, default to x86_64. |
| Peter Collingbourne | 518f03d | 2011-06-20 19:06:04 +0000 | [diff] [blame] | 21 | # 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] | 22 | #---------------------------------------------------------------------- |
| 23 | ifeq "$(ARCH)" "" |
| 24 | ARCH = x86_64 |
| 25 | endif |
| 26 | |
| Peter Collingbourne | 518f03d | 2011-06-20 19:06:04 +0000 | [diff] [blame] | 27 | ifeq "$(OS)" "" |
| 28 | OS = $(shell uname -s) |
| 29 | endif |
| 30 | |
| Johnny Chen | 6c17c08 | 2010-09-16 20:54:06 +0000 | [diff] [blame] | 31 | #---------------------------------------------------------------------- |
| Johnny Chen | 597cbbb | 2011-08-23 21:54:10 +0000 | [diff] [blame] | 32 | # CC defaults to clang. |
| Johnny Chen | 4ab4a9b | 2011-08-24 18:12:53 +0000 | [diff] [blame] | 33 | # |
| 34 | # If you change the defaults of CC, be sure to also change it in the file |
| 35 | # test/plugins/builder_base.py, which provides a Python way to return the |
| 36 | # value of the make variable CC -- getCompiler(). |
| 37 | # |
| Johnny Chen | 6055b44 | 2011-01-14 20:55:13 +0000 | [diff] [blame] | 38 | # See also these functions: |
| 39 | # o cxx_compiler |
| 40 | # o cxx_linker |
| Johnny Chen | d43e208 | 2011-01-14 20:46:49 +0000 | [diff] [blame] | 41 | #---------------------------------------------------------------------- |
| Johnny Chen | 597cbbb | 2011-08-23 21:54:10 +0000 | [diff] [blame] | 42 | CC ?= clang |
| Johnny Chen | d43e208 | 2011-01-14 20:46:49 +0000 | [diff] [blame] | 43 | ifeq "$(CC)" "cc" |
| Johnny Chen | 597cbbb | 2011-08-23 21:54:10 +0000 | [diff] [blame] | 44 | CC = clang |
| Johnny Chen | d43e208 | 2011-01-14 20:46:49 +0000 | [diff] [blame] | 45 | endif |
| 46 | |
| 47 | #---------------------------------------------------------------------- |
| Johnny Chen | 9bc867a | 2010-08-23 23:56:08 +0000 | [diff] [blame] | 48 | # Change any build/tool options needed |
| 49 | #---------------------------------------------------------------------- |
| Peter Collingbourne | 518f03d | 2011-06-20 19:06:04 +0000 | [diff] [blame] | 50 | CFLAGS ?= -gdwarf-2 -O0 |
| Johnny Chen | beac8f9 | 2012-01-10 00:41:11 +0000 | [diff] [blame] | 51 | CFLAGS += $(FRAMEWORK_INCLUDES) $(CFLAGS_EXTRAS) |
| Peter Collingbourne | 518f03d | 2011-06-20 19:06:04 +0000 | [diff] [blame] | 52 | ifeq "$(OS)" "Darwin" |
| 53 | CFLAGS += -arch $(ARCH) |
| Greg Clayton | 3bffb08 | 2011-12-10 02:15:28 +0000 | [diff] [blame] | 54 | DS := dsymutil |
| Peter Collingbourne | 518f03d | 2011-06-20 19:06:04 +0000 | [diff] [blame] | 55 | DSFLAGS = |
| 56 | DSYM = $(EXE).dSYM |
| 57 | endif |
| 58 | |
| Johnny Chen | 53e2edb | 2011-08-09 20:07:16 +0000 | [diff] [blame] | 59 | CXXFLAGS +=$(CFLAGS) |
| 60 | LD = $(CC) |
| 61 | LDFLAGS ?= $(CFLAGS) |
| 62 | LDFLAGS += $(LD_EXTRAS) |
| 63 | OBJECTS = |
| 64 | EXE ?= a.out |
| 65 | |
| Peter Collingbourne | 518f03d | 2011-06-20 19:06:04 +0000 | [diff] [blame] | 66 | ifneq "$(DYLIB_NAME)" "" |
| 67 | ifeq "$(OS)" "Darwin" |
| 68 | DYLIB_FILENAME = lib$(DYLIB_NAME).dylib |
| 69 | else |
| 70 | DYLIB_FILENAME = lib$(DYLIB_NAME).so |
| 71 | endif |
| 72 | endif |
| Johnny Chen | 9bc867a | 2010-08-23 23:56:08 +0000 | [diff] [blame] | 73 | |
| Johnny Chen | bdb4efc | 2011-01-14 18:19:53 +0000 | [diff] [blame] | 74 | # Function that returns the counterpart C++ compiler, given $(CC) as arg. |
| 75 | 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)))) |
| 76 | |
| 77 | # Function that returns the C++ linker, given $(CC) as arg. |
| Greg Clayton | d50d238 | 2012-01-10 00:00:15 +0000 | [diff] [blame] | 78 | cxx_linker = $(if $(findstring clang,$(1)), $(subst clang,clang++,$(1)), $(if $(findstring llvm-gcc,$(1)), $(subst llvm-gcc,llvm-g++,$(1)), $(subst gcc,g++,$(1)))) |
| Johnny Chen | 832d233 | 2010-09-30 01:22:34 +0000 | [diff] [blame] | 79 | |
| Johnny Chen | 9bc867a | 2010-08-23 23:56:08 +0000 | [diff] [blame] | 80 | #---------------------------------------------------------------------- |
| 81 | # dylib settings |
| 82 | #---------------------------------------------------------------------- |
| 83 | ifneq "$(strip $(DYLIB_C_SOURCES))" "" |
| 84 | DYLIB_OBJECTS +=$(strip $(DYLIB_C_SOURCES:.c=.o)) |
| 85 | endif |
| 86 | |
| Greg Clayton | d50d238 | 2012-01-10 00:00:15 +0000 | [diff] [blame] | 87 | ifneq "$(strip $(DYLIB_CXX_SOURCES))" "" |
| 88 | DYLIB_OBJECTS +=$(strip $(DYLIB_CXX_SOURCES:.cpp=.o)) |
| 89 | CXX = $(call cxx_compiler,$(CC)) |
| 90 | LD = $(call cxx_linker,$(CC)) |
| 91 | endif |
| Johnny Chen | 9bc867a | 2010-08-23 23:56:08 +0000 | [diff] [blame] | 92 | |
| 93 | #---------------------------------------------------------------------- |
| 94 | # Check if we have any C source files |
| 95 | #---------------------------------------------------------------------- |
| 96 | ifneq "$(strip $(C_SOURCES))" "" |
| 97 | OBJECTS +=$(strip $(C_SOURCES:.c=.o)) |
| 98 | endif |
| 99 | |
| 100 | #---------------------------------------------------------------------- |
| 101 | # Check if we have any C++ source files |
| 102 | #---------------------------------------------------------------------- |
| 103 | ifneq "$(strip $(CXX_SOURCES))" "" |
| 104 | OBJECTS +=$(strip $(CXX_SOURCES:.cpp=.o)) |
| Johnny Chen | 832d233 | 2010-09-30 01:22:34 +0000 | [diff] [blame] | 105 | CXX = $(call cxx_compiler,$(CC)) |
| Johnny Chen | bdb4efc | 2011-01-14 18:19:53 +0000 | [diff] [blame] | 106 | LD = $(call cxx_linker,$(CC)) |
| Johnny Chen | 9bc867a | 2010-08-23 23:56:08 +0000 | [diff] [blame] | 107 | endif |
| 108 | |
| 109 | #---------------------------------------------------------------------- |
| 110 | # Check if we have any ObjC source files |
| 111 | #---------------------------------------------------------------------- |
| 112 | ifneq "$(strip $(OBJC_SOURCES))" "" |
| 113 | OBJECTS +=$(strip $(OBJC_SOURCES:.m=.o)) |
| 114 | LDFLAGS +=-lobjc |
| 115 | endif |
| 116 | |
| 117 | #---------------------------------------------------------------------- |
| 118 | # Check if we have any ObjC++ source files |
| 119 | #---------------------------------------------------------------------- |
| 120 | ifneq "$(strip $(OBJCXX_SOURCES))" "" |
| 121 | OBJECTS +=$(strip $(OBJCXX_SOURCES:.mm=.o)) |
| Johnny Chen | 832d233 | 2010-09-30 01:22:34 +0000 | [diff] [blame] | 122 | CXX = $(call cxx_compiler,$(CC)) |
| Johnny Chen | bdb4efc | 2011-01-14 18:19:53 +0000 | [diff] [blame] | 123 | LD = $(call cxx_linker,$(CC)) |
| Johnny Chen | 9bc867a | 2010-08-23 23:56:08 +0000 | [diff] [blame] | 124 | ifeq $(findstring lobjc,$(LDFLAGS)) "" |
| 125 | LDFLAGS +=-lobjc |
| 126 | endif |
| 127 | endif |
| 128 | |
| 129 | |
| 130 | #---------------------------------------------------------------------- |
| Johnny Chen | beac8f9 | 2012-01-10 00:41:11 +0000 | [diff] [blame] | 131 | # DYLIB_ONLY variable can be used to skip the building of a.out. |
| 132 | # See the sections below regarding dSYM file as well as the building of |
| 133 | # EXE from all the objects. |
| 134 | #---------------------------------------------------------------------- |
| 135 | |
| 136 | #---------------------------------------------------------------------- |
| Johnny Chen | 9bc867a | 2010-08-23 23:56:08 +0000 | [diff] [blame] | 137 | # Make the dSYM file from the executable if $(MAKE_DSYM) != "NO" |
| 138 | #---------------------------------------------------------------------- |
| Peter Collingbourne | 518f03d | 2011-06-20 19:06:04 +0000 | [diff] [blame] | 139 | ifeq "$(OS)" "Darwin" |
| Johnny Chen | 9101639 | 2011-06-20 20:08:26 +0000 | [diff] [blame] | 140 | ifneq "$(MAKE_DSYM)" "NO" |
| Johnny Chen | beac8f9 | 2012-01-10 00:41:11 +0000 | [diff] [blame] | 141 | ifeq "$(DYLIB_ONLY)" "" |
| Johnny Chen | 9bc867a | 2010-08-23 23:56:08 +0000 | [diff] [blame] | 142 | $(DSYM) : $(EXE) |
| 143 | $(DS) $(DSFLAGS) -o "$(DSYM)" "$(EXE)" |
| Johnny Chen | 9101639 | 2011-06-20 20:08:26 +0000 | [diff] [blame] | 144 | endif |
| Johnny Chen | 9bc867a | 2010-08-23 23:56:08 +0000 | [diff] [blame] | 145 | endif |
| Johnny Chen | beac8f9 | 2012-01-10 00:41:11 +0000 | [diff] [blame] | 146 | endif |
| Johnny Chen | 9bc867a | 2010-08-23 23:56:08 +0000 | [diff] [blame] | 147 | |
| 148 | #---------------------------------------------------------------------- |
| 149 | # Compile the executable from all the objects. |
| 150 | #---------------------------------------------------------------------- |
| Johnny Chen | beac8f9 | 2012-01-10 00:41:11 +0000 | [diff] [blame] | 151 | ifneq "$(DYLIB_NAME)" "" |
| 152 | ifeq "$(DYLIB_ONLY)" "" |
| Peter Collingbourne | 518f03d | 2011-06-20 19:06:04 +0000 | [diff] [blame] | 153 | $(EXE) : $(OBJECTS) $(DYLIB_FILENAME) |
| 154 | $(LD) $(LDFLAGS) $(OBJECTS) -L. -l$(DYLIB_NAME) -o "$(EXE)" |
| Johnny Chen | beac8f9 | 2012-01-10 00:41:11 +0000 | [diff] [blame] | 155 | else |
| 156 | EXE = $(DYLIB_FILENAME) |
| 157 | endif |
| 158 | else |
| 159 | $(EXE) : $(OBJECTS) |
| 160 | $(LD) $(LDFLAGS) $(OBJECTS) -o "$(EXE)" |
| Johnny Chen | 9bc867a | 2010-08-23 23:56:08 +0000 | [diff] [blame] | 161 | endif |
| 162 | |
| 163 | #---------------------------------------------------------------------- |
| 164 | # Make the dylib |
| 165 | #---------------------------------------------------------------------- |
| Peter Collingbourne | 518f03d | 2011-06-20 19:06:04 +0000 | [diff] [blame] | 166 | $(DYLIB_FILENAME) : $(DYLIB_OBJECTS) |
| 167 | ifeq "$(OS)" "Darwin" |
| 168 | $(LD) $(LDFLAGS) $(DYLIB_OBJECTS) -install_name "@executable_path/$(DYLIB_FILENAME)" -dynamiclib -o "$(DYLIB_FILENAME)" |
| 169 | else |
| 170 | $(LD) $(LDFLAGS) $(DYLIB_OBJECTS) -shared -o "$(DYLIB_FILENAME)" |
| 171 | endif |
| Johnny Chen | 9bc867a | 2010-08-23 23:56:08 +0000 | [diff] [blame] | 172 | |
| 173 | #---------------------------------------------------------------------- |
| 174 | # Automatic variables based on items already entered. Below we create |
| 175 | # an objects lists from the list of sources by replacing all entries |
| 176 | # that end with .c with .o, and we also create a list of prerequisite |
| 177 | # files by replacing all .c files with .d. |
| 178 | #---------------------------------------------------------------------- |
| 179 | PREREQS := $(OBJECTS:.o=.d) |
| Johnny Chen | e3dc0f0 | 2010-08-24 16:35:00 +0000 | [diff] [blame] | 180 | ifneq "$(DYLIB_NAME)" "" |
| 181 | DYLIB_PREREQS := $(DYLIB_OBJECTS:.o=.d) |
| 182 | endif |
| Johnny Chen | 9bc867a | 2010-08-23 23:56:08 +0000 | [diff] [blame] | 183 | |
| 184 | #---------------------------------------------------------------------- |
| 185 | # Rule for Generating Prerequisites Automatically using .d files and |
| 186 | # the compiler -MM option. The -M option will list all system headers, |
| 187 | # and the -MM option will list all non-system dependencies. |
| 188 | #---------------------------------------------------------------------- |
| 189 | %.d: %.c |
| 190 | @set -e; rm -f $@; \ |
| Johnny Chen | 189bff1 | 2011-08-04 20:44:56 +0000 | [diff] [blame] | 191 | $(CC) -M $(CFLAGS) $< > $@.$$$$; \ |
| Johnny Chen | 9bc867a | 2010-08-23 23:56:08 +0000 | [diff] [blame] | 192 | sed 's,\($*\)\.o[ :]*,\1.o $@ : ,g' < $@.$$$$ > $@; \ |
| 193 | rm -f $@.$$$$ |
| 194 | |
| 195 | %.d: %.cpp |
| 196 | @set -e; rm -f $@; \ |
| Johnny Chen | 189bff1 | 2011-08-04 20:44:56 +0000 | [diff] [blame] | 197 | $(CC) -M $(CXXFLAGS) $< > $@.$$$$; \ |
| Johnny Chen | 9bc867a | 2010-08-23 23:56:08 +0000 | [diff] [blame] | 198 | sed 's,\($*\)\.o[ :]*,\1.o $@ : ,g' < $@.$$$$ > $@; \ |
| 199 | rm -f $@.$$$$ |
| 200 | |
| 201 | %.d: %.m |
| 202 | @set -e; rm -f $@; \ |
| Johnny Chen | 189bff1 | 2011-08-04 20:44:56 +0000 | [diff] [blame] | 203 | $(CC) -M $(CFLAGS) $< > $@.$$$$; \ |
| Johnny Chen | 9bc867a | 2010-08-23 23:56:08 +0000 | [diff] [blame] | 204 | sed 's,\($*\)\.o[ :]*,\1.o $@ : ,g' < $@.$$$$ > $@; \ |
| 205 | rm -f $@.$$$$ |
| 206 | |
| 207 | %.d: %.mm |
| 208 | @set -e; rm -f $@; \ |
| Johnny Chen | 189bff1 | 2011-08-04 20:44:56 +0000 | [diff] [blame] | 209 | $(CC) -M $(CXXFLAGS) $< > $@.$$$$; \ |
| Johnny Chen | 9bc867a | 2010-08-23 23:56:08 +0000 | [diff] [blame] | 210 | sed 's,\($*\)\.o[ :]*,\1.o $@ : ,g' < $@.$$$$ > $@; \ |
| 211 | rm -f $@.$$$$ |
| 212 | |
| 213 | #---------------------------------------------------------------------- |
| 214 | # Include all of the makefiles for each source file so we don't have |
| 215 | # to manually track all of the prerequisites for each source file. |
| 216 | #---------------------------------------------------------------------- |
| 217 | sinclude $(PREREQS) |
| Johnny Chen | e3dc0f0 | 2010-08-24 16:35:00 +0000 | [diff] [blame] | 218 | ifneq "$(DYLIB_NAME)" "" |
| 219 | sinclude $(DYLIB_PREREQS) |
| 220 | endif |
| Johnny Chen | 9bc867a | 2010-08-23 23:56:08 +0000 | [diff] [blame] | 221 | |
| 222 | .PHONY: clean |
| 223 | dsym: $(DSYM) |
| 224 | all: $(EXE) $(DSYM) |
| Johnny Chen | e1030cd | 2010-09-27 20:44:46 +0000 | [diff] [blame] | 225 | clean:: |
| Johnny Chen | 9bc867a | 2010-08-23 23:56:08 +0000 | [diff] [blame] | 226 | ifeq "$(DYLIB_NAME)" "" |
| 227 | rm -rf "$(EXE)" "$(DSYM)" $(OBJECTS) $(PREREQS) |
| 228 | else |
| Johnny Chen | 6b0a1e3 | 2011-11-14 18:37:49 +0000 | [diff] [blame] | 229 | 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] | 230 | endif |
| Johnny Chen | fa38041 | 2011-01-14 21:18:12 +0000 | [diff] [blame] | 231 | |
| 232 | #---------------------------------------------------------------------- |
| 233 | # From http://blog.melski.net/tag/debugging-makefiles/ |
| 234 | # |
| 235 | # Usage: make print-CC print-CXX print-LD |
| 236 | #---------------------------------------------------------------------- |
| 237 | print-%: |
| 238 | @echo '$*=$($*)' |
| 239 | @echo ' origin = $(origin $*)' |
| 240 | @echo ' flavor = $(flavor $*)' |
| 241 | @echo ' value = $(value $*)' |
| Johnny Chen | 8b2c321 | 2011-01-28 17:22:29 +0000 | [diff] [blame] | 242 | |
| 243 | |
| 244 | ### Local Variables: ### |
| 245 | ### mode:makefile ### |
| 246 | ### End: ### |