#---------------------------------------------------------------------- # Clients fill in the source files to build #---------------------------------------------------------------------- # C_SOURCES := main.c # CXX_SOURCES := # OBJC_SOURCES := # OBJCXX_SOURCES := # DYLIB_C_SOURCES := # Uncomment line below for debugging shell commands # SHELL = /bin/sh -x #---------------------------------------------------------------------- # If ARCH is not defined, default to x86_64. # If OS is not defined, use 'uname -s' to determine the OS name. #---------------------------------------------------------------------- ifeq "$(ARCH)" "" ARCH = x86_64 endif ifeq "$(OS)" "" OS = $(shell uname -s) endif #---------------------------------------------------------------------- # CC defaults to gcc. # See also these functions: # o cxx_compiler # o cxx_linker #---------------------------------------------------------------------- CC ?= gcc ifeq "$(CC)" "cc" CC = gcc endif #---------------------------------------------------------------------- # Change any build/tool options needed #---------------------------------------------------------------------- CFLAGS ?= -gdwarf-2 -O0 CXXFLAGS +=$(CFLAGS) LD = $(CC) LDFLAGS ?= $(CFLAGS) OBJECTS = EXE ?= a.out ifeq "$(OS)" "Darwin" CFLAGS += -arch $(ARCH) DS := /usr/bin/dsymutil DSFLAGS = DSYM = $(EXE).dSYM endif ifneq "$(DYLIB_NAME)" "" ifeq "$(OS)" "Darwin" DYLIB_FILENAME = lib$(DYLIB_NAME).dylib else DYLIB_FILENAME = lib$(DYLIB_NAME).so endif endif # Function that returns the counterpart C++ compiler, given $(CC) as arg. 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)))) # Function that returns the C++ linker, given $(CC) as arg. cxx_linker = $(if $(findstring clang,$(1)), $(subst clang,g++,$(1)), $(if $(findstring llvm-gcc,$(1)), $(subst llvm-gcc,g++,$(1)), $(subst gcc,g++,$(1)))) #---------------------------------------------------------------------- # dylib settings #---------------------------------------------------------------------- ifneq "$(strip $(DYLIB_C_SOURCES))" "" DYLIB_OBJECTS +=$(strip $(DYLIB_C_SOURCES:.c=.o)) endif #---------------------------------------------------------------------- # Check if we have any C source files #---------------------------------------------------------------------- ifneq "$(strip $(C_SOURCES))" "" OBJECTS +=$(strip $(C_SOURCES:.c=.o)) endif #---------------------------------------------------------------------- # Check if we have any C++ source files #---------------------------------------------------------------------- ifneq "$(strip $(CXX_SOURCES))" "" OBJECTS +=$(strip $(CXX_SOURCES:.cpp=.o)) CXX = $(call cxx_compiler,$(CC)) LD = $(call cxx_linker,$(CC)) endif #---------------------------------------------------------------------- # Check if we have any ObjC source files #---------------------------------------------------------------------- ifneq "$(strip $(OBJC_SOURCES))" "" OBJECTS +=$(strip $(OBJC_SOURCES:.m=.o)) LDFLAGS +=-lobjc endif #---------------------------------------------------------------------- # Check if we have any ObjC++ source files #---------------------------------------------------------------------- ifneq "$(strip $(OBJCXX_SOURCES))" "" OBJECTS +=$(strip $(OBJCXX_SOURCES:.mm=.o)) CXX = $(call cxx_compiler,$(CC)) LD = $(call cxx_linker,$(CC)) ifeq $(findstring lobjc,$(LDFLAGS)) "" LDFLAGS +=-lobjc endif endif #---------------------------------------------------------------------- # Make the dSYM file from the executable if $(MAKE_DSYM) != "NO" #---------------------------------------------------------------------- ifeq "$(OS)" "Darwin" ifneq "$(MAKE_DSYM)" "NO" $(DSYM) : $(EXE) $(DS) $(DSFLAGS) -o "$(DSYM)" "$(EXE)" endif endif #---------------------------------------------------------------------- # Compile the executable from all the objects. #---------------------------------------------------------------------- ifeq "$(DYLIB_NAME)" "" $(EXE) : $(OBJECTS) $(LD) $(LDFLAGS) $(OBJECTS) -o "$(EXE)" else $(EXE) : $(OBJECTS) $(DYLIB_FILENAME) $(LD) $(LDFLAGS) $(OBJECTS) -L. -l$(DYLIB_NAME) -o "$(EXE)" endif #---------------------------------------------------------------------- # Make the dylib #---------------------------------------------------------------------- $(DYLIB_FILENAME) : $(DYLIB_OBJECTS) ifeq "$(OS)" "Darwin" $(LD) $(LDFLAGS) $(DYLIB_OBJECTS) -install_name "@executable_path/$(DYLIB_FILENAME)" -dynamiclib -o "$(DYLIB_FILENAME)" else $(LD) $(LDFLAGS) $(DYLIB_OBJECTS) -shared -o "$(DYLIB_FILENAME)" endif #---------------------------------------------------------------------- # Automatic variables based on items already entered. Below we create # an objects lists from the list of sources by replacing all entries # that end with .c with .o, and we also create a list of prerequisite # files by replacing all .c files with .d. #---------------------------------------------------------------------- PREREQS := $(OBJECTS:.o=.d) ifneq "$(DYLIB_NAME)" "" DYLIB_PREREQS := $(DYLIB_OBJECTS:.o=.d) endif #---------------------------------------------------------------------- # Rule for Generating Prerequisites Automatically using .d files and # the compiler -MM option. The -M option will list all system headers, # and the -MM option will list all non-system dependencies. #---------------------------------------------------------------------- %.d: %.c @set -e; rm -f $@; \ $(CC) -M $(CPPFLAGS) $< > $@.$$$$; \ sed 's,\($*\)\.o[ :]*,\1.o $@ : ,g' < $@.$$$$ > $@; \ rm -f $@.$$$$ %.d: %.cpp @set -e; rm -f $@; \ $(CC) -M $(CPPFLAGS) $< > $@.$$$$; \ sed 's,\($*\)\.o[ :]*,\1.o $@ : ,g' < $@.$$$$ > $@; \ rm -f $@.$$$$ %.d: %.m @set -e; rm -f $@; \ $(CC) -M $(CPPFLAGS) $< > $@.$$$$; \ sed 's,\($*\)\.o[ :]*,\1.o $@ : ,g' < $@.$$$$ > $@; \ rm -f $@.$$$$ %.d: %.mm @set -e; rm -f $@; \ $(CC) -M $(CPPFLAGS) $< > $@.$$$$; \ sed 's,\($*\)\.o[ :]*,\1.o $@ : ,g' < $@.$$$$ > $@; \ rm -f $@.$$$$ #---------------------------------------------------------------------- # Include all of the makefiles for each source file so we don't have # to manually track all of the prerequisites for each source file. #---------------------------------------------------------------------- sinclude $(PREREQS) ifneq "$(DYLIB_NAME)" "" sinclude $(DYLIB_PREREQS) endif .PHONY: clean dsym: $(DSYM) all: $(EXE) $(DSYM) clean:: ifeq "$(DYLIB_NAME)" "" rm -rf "$(EXE)" "$(DSYM)" $(OBJECTS) $(PREREQS) else rm -rf "$(EXE)" "$(DSYM)" $(OBJECTS) $(PREREQS) $(DYLIB_OBJECTS) $(DYLIB_FILENAME) $(DYLIB_FILENAME).dSYM endif #---------------------------------------------------------------------- # From http://blog.melski.net/tag/debugging-makefiles/ # # Usage: make print-CC print-CXX print-LD #---------------------------------------------------------------------- print-%: @echo '$*=$($*)' @echo ' origin = $(origin $*)' @echo ' flavor = $(flavor $*)' @echo ' value = $(value $*)' ### Local Variables: ### ### mode:makefile ### ### End: ###