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 | d43e208 | 2011-01-14 20:46:49 +0000 | [diff] [blame] | 21 | # CC defaults to gcc. |
Johnny Chen | 6055b44 | 2011-01-14 20:55:13 +0000 | [diff] [blame^] | 22 | # See also these functions: |
| 23 | # o cxx_compiler |
| 24 | # o cxx_linker |
Johnny Chen | d43e208 | 2011-01-14 20:46:49 +0000 | [diff] [blame] | 25 | #---------------------------------------------------------------------- |
| 26 | CC ?= gcc |
| 27 | ifeq "$(CC)" "cc" |
| 28 | CC = gcc |
| 29 | endif |
| 30 | |
| 31 | #---------------------------------------------------------------------- |
Johnny Chen | 9bc867a | 2010-08-23 23:56:08 +0000 | [diff] [blame] | 32 | # Change any build/tool options needed |
| 33 | #---------------------------------------------------------------------- |
| 34 | DS := /usr/bin/dsymutil |
| 35 | DSFLAGS = |
Johnny Chen | 6c17c08 | 2010-09-16 20:54:06 +0000 | [diff] [blame] | 36 | CFLAGS ?=-arch $(ARCH) -gdwarf-2 -O0 |
Johnny Chen | 832d233 | 2010-09-30 01:22:34 +0000 | [diff] [blame] | 37 | CXXFLAGS +=$(CFLAGS) |
Johnny Chen | 1394a4b | 2010-09-13 20:54:30 +0000 | [diff] [blame] | 38 | LD = $(CC) |
Johnny Chen | 9bc867a | 2010-08-23 23:56:08 +0000 | [diff] [blame] | 39 | LDFLAGS ?= $(CFLAGS) |
| 40 | OBJECTS = |
| 41 | EXE = a.out |
| 42 | DSYM = $(EXE).dSYM |
| 43 | |
Johnny Chen | bdb4efc | 2011-01-14 18:19:53 +0000 | [diff] [blame] | 44 | # Function that returns the counterpart C++ compiler, given $(CC) as arg. |
| 45 | 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)))) |
| 46 | |
| 47 | # Function that returns the C++ linker, given $(CC) as arg. |
| 48 | 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] | 49 | |
Johnny Chen | 9bc867a | 2010-08-23 23:56:08 +0000 | [diff] [blame] | 50 | #---------------------------------------------------------------------- |
| 51 | # dylib settings |
| 52 | #---------------------------------------------------------------------- |
| 53 | ifneq "$(strip $(DYLIB_C_SOURCES))" "" |
| 54 | DYLIB_OBJECTS +=$(strip $(DYLIB_C_SOURCES:.c=.o)) |
| 55 | endif |
| 56 | |
| 57 | |
| 58 | #---------------------------------------------------------------------- |
| 59 | # Check if we have any C source files |
| 60 | #---------------------------------------------------------------------- |
| 61 | ifneq "$(strip $(C_SOURCES))" "" |
| 62 | OBJECTS +=$(strip $(C_SOURCES:.c=.o)) |
| 63 | endif |
| 64 | |
| 65 | #---------------------------------------------------------------------- |
| 66 | # Check if we have any C++ source files |
| 67 | #---------------------------------------------------------------------- |
| 68 | ifneq "$(strip $(CXX_SOURCES))" "" |
| 69 | OBJECTS +=$(strip $(CXX_SOURCES:.cpp=.o)) |
Johnny Chen | 832d233 | 2010-09-30 01:22:34 +0000 | [diff] [blame] | 70 | CXX = $(call cxx_compiler,$(CC)) |
Johnny Chen | bdb4efc | 2011-01-14 18:19:53 +0000 | [diff] [blame] | 71 | LD = $(call cxx_linker,$(CC)) |
Johnny Chen | 9bc867a | 2010-08-23 23:56:08 +0000 | [diff] [blame] | 72 | endif |
| 73 | |
| 74 | #---------------------------------------------------------------------- |
| 75 | # Check if we have any ObjC source files |
| 76 | #---------------------------------------------------------------------- |
| 77 | ifneq "$(strip $(OBJC_SOURCES))" "" |
| 78 | OBJECTS +=$(strip $(OBJC_SOURCES:.m=.o)) |
| 79 | LDFLAGS +=-lobjc |
| 80 | endif |
| 81 | |
| 82 | #---------------------------------------------------------------------- |
| 83 | # Check if we have any ObjC++ source files |
| 84 | #---------------------------------------------------------------------- |
| 85 | ifneq "$(strip $(OBJCXX_SOURCES))" "" |
| 86 | OBJECTS +=$(strip $(OBJCXX_SOURCES:.mm=.o)) |
Johnny Chen | 832d233 | 2010-09-30 01:22:34 +0000 | [diff] [blame] | 87 | CXX = $(call cxx_compiler,$(CC)) |
Johnny Chen | bdb4efc | 2011-01-14 18:19:53 +0000 | [diff] [blame] | 88 | LD = $(call cxx_linker,$(CC)) |
Johnny Chen | 9bc867a | 2010-08-23 23:56:08 +0000 | [diff] [blame] | 89 | ifeq $(findstring lobjc,$(LDFLAGS)) "" |
| 90 | LDFLAGS +=-lobjc |
| 91 | endif |
| 92 | endif |
| 93 | |
| 94 | |
| 95 | #---------------------------------------------------------------------- |
| 96 | # Make the dSYM file from the executable if $(MAKE_DSYM) != "NO" |
| 97 | #---------------------------------------------------------------------- |
| 98 | ifneq "$(MAKE_DSYM)" "NO" |
| 99 | $(DSYM) : $(EXE) |
| 100 | $(DS) $(DSFLAGS) -o "$(DSYM)" "$(EXE)" |
| 101 | endif |
| 102 | |
| 103 | #---------------------------------------------------------------------- |
| 104 | # Compile the executable from all the objects. |
| 105 | #---------------------------------------------------------------------- |
| 106 | ifeq "$(DYLIB_NAME)" "" |
| 107 | $(EXE) : $(OBJECTS) |
| 108 | $(LD) $(LDFLAGS) $(OBJECTS) -o "$(EXE)" |
| 109 | else |
| 110 | $(EXE) : $(OBJECTS) $(DYLIB_NAME) |
| 111 | $(LD) $(LDFLAGS) $(OBJECTS) -L. -l$(subst lib,,$(basename $(DYLIB_NAME))) -o "$(EXE)" |
| 112 | endif |
| 113 | |
| 114 | #---------------------------------------------------------------------- |
| 115 | # Make the dylib |
| 116 | #---------------------------------------------------------------------- |
| 117 | $(DYLIB_NAME) : $(DYLIB_OBJECTS) |
| 118 | $(LD) $(LDFLAGS) $(DYLIB_OBJECTS) -install_name "@executable_path/$(DYLIB_NAME)" -dynamiclib -o "$(DYLIB_NAME)" |
| 119 | |
| 120 | #---------------------------------------------------------------------- |
| 121 | # Automatic variables based on items already entered. Below we create |
| 122 | # an objects lists from the list of sources by replacing all entries |
| 123 | # that end with .c with .o, and we also create a list of prerequisite |
| 124 | # files by replacing all .c files with .d. |
| 125 | #---------------------------------------------------------------------- |
| 126 | PREREQS := $(OBJECTS:.o=.d) |
Johnny Chen | e3dc0f0 | 2010-08-24 16:35:00 +0000 | [diff] [blame] | 127 | ifneq "$(DYLIB_NAME)" "" |
| 128 | DYLIB_PREREQS := $(DYLIB_OBJECTS:.o=.d) |
| 129 | endif |
Johnny Chen | 9bc867a | 2010-08-23 23:56:08 +0000 | [diff] [blame] | 130 | |
| 131 | #---------------------------------------------------------------------- |
| 132 | # Rule for Generating Prerequisites Automatically using .d files and |
| 133 | # the compiler -MM option. The -M option will list all system headers, |
| 134 | # and the -MM option will list all non-system dependencies. |
| 135 | #---------------------------------------------------------------------- |
| 136 | %.d: %.c |
| 137 | @set -e; rm -f $@; \ |
| 138 | $(CC) -M $(CPPFLAGS) $< > $@.$$$$; \ |
| 139 | sed 's,\($*\)\.o[ :]*,\1.o $@ : ,g' < $@.$$$$ > $@; \ |
| 140 | rm -f $@.$$$$ |
| 141 | |
| 142 | %.d: %.cpp |
| 143 | @set -e; rm -f $@; \ |
| 144 | $(CC) -M $(CPPFLAGS) $< > $@.$$$$; \ |
| 145 | sed 's,\($*\)\.o[ :]*,\1.o $@ : ,g' < $@.$$$$ > $@; \ |
| 146 | rm -f $@.$$$$ |
| 147 | |
| 148 | %.d: %.m |
| 149 | @set -e; rm -f $@; \ |
| 150 | $(CC) -M $(CPPFLAGS) $< > $@.$$$$; \ |
| 151 | sed 's,\($*\)\.o[ :]*,\1.o $@ : ,g' < $@.$$$$ > $@; \ |
| 152 | rm -f $@.$$$$ |
| 153 | |
| 154 | %.d: %.mm |
| 155 | @set -e; rm -f $@; \ |
| 156 | $(CC) -M $(CPPFLAGS) $< > $@.$$$$; \ |
| 157 | sed 's,\($*\)\.o[ :]*,\1.o $@ : ,g' < $@.$$$$ > $@; \ |
| 158 | rm -f $@.$$$$ |
| 159 | |
| 160 | #---------------------------------------------------------------------- |
| 161 | # Include all of the makefiles for each source file so we don't have |
| 162 | # to manually track all of the prerequisites for each source file. |
| 163 | #---------------------------------------------------------------------- |
| 164 | sinclude $(PREREQS) |
Johnny Chen | e3dc0f0 | 2010-08-24 16:35:00 +0000 | [diff] [blame] | 165 | ifneq "$(DYLIB_NAME)" "" |
| 166 | sinclude $(DYLIB_PREREQS) |
| 167 | endif |
Johnny Chen | 9bc867a | 2010-08-23 23:56:08 +0000 | [diff] [blame] | 168 | |
| 169 | .PHONY: clean |
| 170 | dsym: $(DSYM) |
| 171 | all: $(EXE) $(DSYM) |
Johnny Chen | e1030cd | 2010-09-27 20:44:46 +0000 | [diff] [blame] | 172 | clean:: |
Johnny Chen | 9bc867a | 2010-08-23 23:56:08 +0000 | [diff] [blame] | 173 | ifeq "$(DYLIB_NAME)" "" |
| 174 | rm -rf "$(EXE)" "$(DSYM)" $(OBJECTS) $(PREREQS) |
| 175 | else |
| 176 | rm -rf "$(EXE)" "$(DSYM)" $(OBJECTS) $(PREREQS) $(DYLIB_OBJECTS) $(DYLIB_NAME) $(DYLIB_NAME).dSYM |
| 177 | endif |