public
Last active

  • Download Gist
makefile
Makefile
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296
#####
#
# Dynamic Makefile for C++11 Projects.
# Recursively builds all files in a project and its test suite.
#
# Standard build targets are:
# - (none): all
# - all: testing
# - primary: build primary targets
# - testing: build testing targets
# - run: build and link primary targets, execute primary binary
# - test: build and link testing targets, execute testing binary
# - clean: remove objects created from primary and testing builds
# - remove remove main objects and executables from primary and testing links
# and perform a clean
#
# Build modes can be speficified on the command-line (mode=XXXX):
# - debug: debug symbols, minor optimizations, defines DEBUG
# - release: massive optimizations, defines NDEBUG
# - profile: debug and profiling symbols, safe optimizations, defines NDEBUG
# - default: set to value of DEFAULT_BUILD_MODE
#
# Variables expected to be edited (and their default values):
# - LIBRARIES
# - INCLUDES
# - DEFINES
# - WARNINGS (all extra)
# - CXXFLAGS (--std=c++11 -pipe)
# - LXXFLAGS
#
# Oher variables that can be changed (and their default values):
# - SOURCE_DIR (src)
# - OBJECT_DIR (obj)
# - PRIMARY_DIR (project)
# - TESTING_DIR (testing)
# - DEFAULT_BUILD_MODE (debug)
#
# Requires separate project and testing directories and mains:
# |- ./
# |- Makefile
# |- project/
# |- main.cpp
# |- src/
# |- ... project files and directories
# |- testing/
# |- main.cpp
# |- src/
# |- ... testing files and directories
#
#####
 
.PHONY: all run test primary testing \
prepare prepare_primary prepare_testing \
build build_primary build_testing \
link link_primary link_testing \
clean clean_primary clean_testing \
remove remove_primary remove_testing
.DEFAULT_GOAL: all
 
 
###
# TARGETS
###
 
all: testing
primary: build_primary
testing: build_testing
 
 
###
# PATHS
###
 
# Common
SOURCE_DIR = src
OBJECT_DIR = obj
 
# Primary
PRIMARY_DIR = project
PRIMARY_SOURCE_DIR = $(PRIMARY_DIR)/$(SOURCE_DIR)
PRIMARY_OBJECT_DIR = $(PRIMARY_DIR)/$(OBJECT_DIR)
 
PRIMARY_SOURCE_SUB_DIRS = $(shell find $(PRIMARY_SOURCE_DIR) -type d)
PRIMARY_OBJECT_SUB_DIRS = \
$(patsubst $(PRIMARY_SOURCE_DIR)%,$(PRIMARY_OBJECT_DIR)%,$(PRIMARY_SOURCE_SUB_DIRS))
 
# Testing
TESTING_DIR = testing
TESTING_SOURCE_DIR = $(TESTING_DIR)/$(SOURCE_DIR)
TESTING_OBJECT_DIR = $(TESTING_DIR)/$(OBJECT_DIR)
 
TESTING_SOURCE_SUB_DIRS = $(shell find $(TESTING_SOURCE_DIR) -type d)
TESTING_OBJECT_SUB_DIRS = \
$(patsubst $(TESTING_SOURCE_DIR)%,$(TESTING_OBJECT_DIR)%,$(TESTING_SOURCE_SUB_DIRS))
 
 
###
# FILES
###
 
# Primary
PRIMARY_SOURCE_FILES = $(shell find $(PRIMARY_SOURCE_DIR) -name "*.cpp")
PRIMARY_HEADER_FILES = $(shell find $(PRIMARY_SOURCE_DIR) -name "*.hpp")
PRIMARY_OBJECT_FILES = \
$(patsubst $(PRIMARY_SOURCE_DIR)/%.o,$(PRIMARY_OBJECT_DIR)/%.o,$(PRIMARY_SOURCE_FILES:.cpp=.o))
 
PRIMARY_SOURCE_MAIN = $(PRIMARY_DIR)/main.cpp
PRIMARY_OBJECT_MAIN = $(OBJECT_DIR)/$(patsubst %.cpp,%.o,$(PRIMARY_SOURCE_MAIN))
PRIMARY_EXEC = $(shell basename `pwd`)
 
# Testing
TESTING_SOURCE_FILES = $(shell find $(TESTING_SOURCE_DIR) -name "*.cpp")
TESTING_HEADER_FILES = $(shell find $(TESTING_SOURCE_DIR) -name "*.hpp")
TESTING_OBJECT_FILES = \
$(patsubst $(TESTING_SOURCE_DIR)/%.o,$(TESTING_OBJECT_DIR)/%.o,$(TESTING_SOURCE_FILES:.cpp=.o))
 
TESTING_SOURCE_MAIN = $(TESTING_DIR)/main.cpp
TESTING_OBJECT_MAIN = $(OBJECT_DIR)/$(patsubst %.cpp,%.o,$(TESTING_SOURCE_MAIN))
TESTING_EXEC = $(PRIMARY_EXEC)_test
 
 
###
# VARIABLES
###
 
# Common
LIBRARIES =
INCLUDES =
DEFINES =
WARNINGS = all extra
 
CXX = g++
CXXFLAGS = --std=c++11 -pipe
 
LXX = g++
LXXFLAGS =
 
# Target-specific
MODE ?= default
BUILD_MODE = $(shell echo $(MODE) | tr '[:upper:]' '[:lower:]')
 
DEFAULT_BUILD_MODE = debug
ifeq ($(BUILD_MODE),default)
BUILD_MODE = $(DEFAULT_BUILD_MODE)
endif
 
ifeq ($(BUILD_MODE),debug)
CXXFLAGS += -g -O
DEFINES += DEBUG
else
ifeq ($(BUILD_MODE),release)
CXXFLAGS += -Ofast
DEFINES += NDEBUG
else
ifeq ($(BUILD_MODE),profile)
CXXFLAGS += -g -pg -Og
LXXFLAGS += -pg
DEFINES += NDEBUG
else
$(warning Unrecognized build mode: $(BUILD_MODE))
endif
endif
endif
 
# Primary
PRIMARY_LIBRARIES = $(LIBRARIES)
PRIMARY_INCLUDES = $(INCLUDES)
PRIMARY_DEFINES = $(DEFINES)
PRIMARY_WARNINGS = $(WARNINGS)
 
PRIMARY_CXXFLAGS = $(CXXFLAGS) $(foreach d,$(PRIMARY_DEFINES),-D$d) \
$(foreach d,$(PRIMARY_WARNINGS),-W$d) $(foreach d,$(PRIMARY_INCLUDES),-I$d)
PRIMARY_LXXFLAGS =
 
# Testing
TESTING_LIBRARIES = $(LIBRARIES)
TESTING_INCLUDES = $(INCLUDES)
TESTING_DEFINES = $(DEFINES)
TESTING_WARNINGS = $(WARNINGS)
 
TESTING_CXXFLAGS = $(CXXFLAGS) $(foreach d,$(TESTING_DEFINES),-D$d) \
$(foreach d,$(TESTING_WARNINGS),-W$d) $(foreach d,$(TESTING_INCLUDES),-I$d)
TESTING_LXXFLAGS =
 
 
###
# TARGETS
###
 
# Common
build: build_primary build_testing
 
link: link_primary link_testing
 
clean: clean_primary clean_test
 
remove: remove_primary remove_test
rm -rf $(OBJECT_DIR)
 
# Primary
$(PRIMARY_OBJECT_DIR)/%.o: $(PRIMARY_SOURCE_DIR)/%.cpp
$(CXX) $(PRIMARY_CXXFLAGS) -c -o $@ $<
 
$(PRIMARY_OBJECT_MAIN): $(PRIMARY_SOURCE_MAIN)
$(CXX) $(PRIMARY_CXXFLAGS) -c -o $@ $<
 
prepare_primary:
@echo " --------"
@echo "| creating primary build directories..."
@echo " --------"
mkdir -p $(PRIMARY_OBJECT_SUB_DIRS) $(OBJECT_DIR)/$(PRIMARY_DIR)
 
declare_primary_build:
@echo " --------"
@echo "| building primary object files..."
@echo " --------"
 
build_primary: prepare_primary declare_primary_build $(PRIMARY_OBJECT_FILES)
 
link_primary: $(PRIMARY_EXEC)
 
$(PRIMARY_EXEC): build_primary $(PRIMARY_OBJECT_MAIN)
@echo " --------"
@echo "| linking primary executable..."
@echo " --------"
$(LXX) $(LXXFLAGS) -o $@ $(PRIMARY_OBJECT_FILES) $(PRIMARY_OBJECT_MAIN) \
$(foreach l,$(PRIMARY_LIBRARIES),-l$l)
 
run: link_primary
@echo " --------"
@echo "| running primary executable..."
@echo " --------"
./$(PRIMARY_EXEC)
 
clean_primary:
@echo " --------"
@echo "| cleaning primary build..."
@echo " --------"
rm -f $(PRIMARY_OBJECTS)
rm -rf $(PRIMARY_OBJECT_SUB_DIRS)
 
remove_primary: clean_primary
@echo " --------"
@echo "| removing primary executable..."
@echo " --------"
rm -f $(PRIMARY_OBJECT_MAIN) $(PRIMARY_EXEC)
rm -rf $(OBJECT_DIR)/$(PRIMARY_DIR)
 
# Testing
$(TESTING_OBJECT_DIR)/%.o: $(TESTING_SOURCE_DIR)/%.cpp
$(CXX) $(TESTING_CXXFLAGS) -c -o $@ $<
 
$(TESTING_OBJECT_MAIN): $(TESTING_SOURCE_MAIN)
$(CXX) $(TESTING_CXXFLAGS) -c -o $@ $<
 
prepare_testing:
@echo " --------"
@echo "| creating testing build directories..."
@echo " --------"
mkdir -p $(TESTING_OBJECT_SUB_DIRS) $(OBJECT_DIR)/$(TESTING_DIR)
 
declare_testing_build:
@echo " --------"
@echo "| building testing object files..."
@echo " --------"
 
build_testing: build_primary prepare_testing declare_testing_build $(TESTING_OBJECT_FILES)
 
link_testing: $(TESTING_EXEC)
 
$(TESTING_EXEC): build_testing $(TESTING_OBJECT_MAIN)
@echo " --------"
@echo "| linking testing executable..."
@echo " --------"
$(LXX) $(LXXFLAGS) -o $@ $(PRIMARY_OBJECT_FILES) $(TESTING_OBJECT_FILES) \
$(TESTING_OBJECT_MAIN) $(foreach l,$(TESTING_LIBRARIES),-l$l)
 
test: link_testing
@echo " --------"
@echo "| running testing executable..."
@echo " --------"
./$(TESTING_EXEC)
 
clean_test:
@echo " --------"
@echo "| cleaning testing build..."
@echo " --------"
rm -f $(TESTING_OBJECTS) $(TESTING_OBJECT_MAIN)
rm -rf $(TESTING_OBJECT_SUB_DIRS)
 
remove_test: clean_test
@echo " --------"
@echo "| removing testing executable..."
@echo " --------"
rm -f $(TESTING_OBJECT_MAIN) $(TESTING_EXEC)
rm -rf $(OBJECT_DIR)/$(TESTING_DIR)

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.