annotate Makefile @ 251:bc1fc8985170

Actually add Makefiles! - Not added in previous commit due to being in .gitignore
author Jamie Bullock <jamie@jamiebullock.com>
date Thu, 06 Nov 2014 17:29:37 +0000
parents
children fbdb69cb1b53
rev   line source
jamie@251 1 ####
jamie@251 2 #### Generic Makefile for C or C++ projects
jamie@251 3 ####
jamie@251 4 #### This file is public domain.
jamie@251 5 #### Jamie Bullock 2014 <jamie@jamiebullock.com>
jamie@251 6 ####
jamie@251 7
jamie@251 8 ###################################
jamie@251 9 ### User configurable variables ###
jamie@251 10 ###################################
jamie@251 11
jamie@251 12 #### It is best not to modify this file
jamie@251 13 #### Instead override these variables in a separate Make.config file if needed
jamie@251 14
jamie@251 15 # The name of the product to build (default uses parent directory name)
jamie@251 16 NAME ?= $(notdir $(CURDIR))
jamie@251 17 # The file suffix of source files, can be .c or .cpp
jamie@251 18 SUFFIX ?= .c
jamie@251 19 # List of directories containing source files to be compiled
jamie@251 20 DIRS ?= .
jamie@251 21 # Flags to pass to the compiler for release builds
jamie@251 22 FLAGS ?= -O3
jamie@251 23 # Flags to pass to the compiler for debug builds
jamie@251 24 DEBUG_FLAGS ?= -O0 -g
jamie@251 25 # Flags to pass to the linker
jamie@251 26 LDFLAGS ?=
jamie@251 27 # Type of product to build: "shared" for a shared library, "static" for a static library, empty for standalone
jamie@251 28 LIBRARY ?= static
jamie@251 29 # Prefix to the path that the "install" target will install into. libs to $(PREFIX)/lib, executables to $(PREFIX)/bin
jamie@251 30 PREFIX ?= /usr/local
jamie@251 31
jamie@251 32 ##############################################
jamie@251 33 ### Do not modify anything below this line ###
jamie@251 34 ##############################################
jamie@251 35
jamie@251 36 ifeq ($(OS),Windows_NT)
jamie@251 37 else
jamie@251 38 PLATFORM := $(shell uname -s)
jamie@251 39 endif
jamie@251 40
jamie@251 41 -include Make.config
jamie@251 42
jamie@251 43 OUT_DIR := .build
jamie@251 44 SRC := $(foreach dir, $(DIRS), $(wildcard $(dir)/*$(SUFFIX)))
jamie@251 45 OBJ_ := $(SRC:$(SUFFIX)=.o)
jamie@251 46 OBJ := $(addprefix $(OUT_DIR)/,$(OBJ_))
jamie@251 47 DEPS := $(OBJ:.o=.d)
jamie@251 48 SHARED_SUFFIX := dll
jamie@251 49 STATIC_SUFFIX := lib
jamie@251 50 INSTALL_DIR := $(PREFIX)/lib
jamie@251 51
jamie@251 52 ifeq "$(PLATFORM)" "Darwin"
jamie@251 53 SHARED_SUFFIX := dylib
jamie@251 54 STATIC_SUFFIX := a
jamie@251 55 endif
jamie@251 56
jamie@251 57 ifeq "$(PLATFORM)" "Linux"
jamie@251 58 SHARED_SUFFIX := so
jamie@251 59 STATIC_SUFFIX := a
jamie@251 60 endif
jamie@251 61
jamie@251 62 ifeq "$(LIBRARY)" "shared"
jamie@251 63 OUT=lib$(NAME).$(SHARED_SUFFIX)
jamie@251 64 LDFLAGS += -shared
jamie@251 65 else ifeq "$(LIBRARY)" "static"
jamie@251 66 OUT=lib$(NAME).$(STATIC_SUFFIX)
jamie@251 67 else
jamie@251 68 OUT=$(NAME)
jamie@251 69 INSTALL_DIR := $(PREFIX)/bin
jamie@251 70 endif
jamie@251 71
jamie@251 72 ifeq "$(SUFFIX)" ".cpp"
jamie@251 73 COMPILER := $(CXX)
jamie@251 74 else ifeq "$(SUFFIX)" ".c"
jamie@251 75 COMPILER := $(CC)
jamie@251 76 endif
jamie@251 77
jamie@251 78 .SUFFIXES:
jamie@251 79 .PHONY: debug clean install uninstall
jamie@251 80
jamie@251 81 $(OUT): $(OBJ)
jamie@251 82 ifeq "$(LIBRARY)" "static"
jamie@251 83 @$(AR) rcs $@ $^
jamie@251 84 else
jamie@251 85 @$(COMPILER) $(LDFLAGS) $^ -o $@
jamie@251 86 endif
jamie@251 87
jamie@251 88 debug: FLAGS = $(DEBUG_FLAGS)
jamie@251 89 debug: $(OUT)
jamie@251 90
jamie@251 91 $(OUT_DIR)/%.o: %$(SUFFIX)
jamie@251 92 @mkdir -p $(dir $@)
jamie@251 93 @$(COMPILER) $(CXXFLAGS) $(FLAGS) -MMD -MP -fPIC -c $< -o $@
jamie@251 94
jamie@251 95 install: $(OUT)
jamie@251 96 @install -d $(INSTALL_DIR)
jamie@251 97 @install $(OUT) $(INSTALL_DIR)
jamie@251 98
jamie@251 99 uninstall:
jamie@251 100 @$(RM) $(INSTALL_DIR)/$(OUT)
jamie@251 101
jamie@251 102 clean:
jamie@251 103 @$(RM) -r $(OUT) $(OUT_DIR)
jamie@251 104
jamie@251 105 -include: $(DEPS)