Skip to content

Instantly share code, notes, and snippets.

@matthewelmer
Last active July 1, 2023 03:06
Show Gist options
  • Save matthewelmer/3a1bbdd31a7c5e15ba7c0cacd0005643 to your computer and use it in GitHub Desktop.
Save matthewelmer/3a1bbdd31a7c5e15ba7c0cacd0005643 to your computer and use it in GitHub Desktop.
Makefile with automatic dependency generation and build vs release modes
# MIT License
#
# Copyright (c) 2023 Matthew Elmer
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
# Compiler, preprocessor, and linker setup
CXX := g++
CXXFLAGS := -Wall -Wextra -Wshadow -std=${STD}# -fopenmp (for Eigen)
DEFINEFLAGS :=# -DHAVE_INLINE (for gsl)
CPPFLAGS = ${DEFINEFLAGS} -MP -MMD -MT $@ -MF ${DEP_DIR}/$*.d# Note: uses = instead of :=
LDFLAGS :=# -lgsl -lgslcblas -lm (for gsl)
ifeq (${mode},release) # If given mode=release, make a release build
CXXFLAGS += -O3
CPPFLAGS += -DRELEASE -DNDEBUG
LDFLAGS +=
else # Else, make a debug build
CXXFLAGS += -g
CPPFLAGS +=
LDFLAGS +=
endif
.PHONY: all
all: ${BIN}
# Link the object files to build the executable
# Linking note: Dependencies must go to the left of prerequisites. Thus, the -l
# linker flags must come after the .o files:
# https://stackoverflow.com/a/409402/12610080
${BIN}: ${OBJ} # Note: You can call this recipe directly!
${CXX} $^ ${LDFLAGS} -o $@
# Compile the source files into object files
${OBJ}: ${OBJ_DIR}/%.o: ${SRC_DIR}/%.cc | ${BUILD_DIRS}
cppcheck ${CHECKFLAGS} $<
${CXX} ${CXXFLAGS} ${CPPFLAGS} ${INCLUDE} -c $< -o $@
# Create the required directories if they don't already exist
${BUILD_DIRS}:
mkdir -p $@
# The dash makes make not fail if .d file not found
-include ${DEP}
# MIT License
#
# Copyright (c) 2023 Matthew Elmer
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
# GNU Make notes:
# -MD: Generate dependency output file as side effect of compilation process
# -MMD: Same as MD but only mention user header files
# -MP: Makes make not freak out when you remove a header w/o updating makefile
# -MT: Change the target of the rule emitted by dependency generation
# $@: target
# $^: all prerequisites
# $<: first prerequisite
# vpath not used because it doesn't work with automatic source file discovery
# target: prerequisites | order-only prerequisites
# Set ARGS="" when invoking make to pass arguments to the program
# .PHONY: https://www.gnu.org/software/make/manual/html_node/Phony-Targets.html
# g++ notes:
# [-fexceptions](https://gcc.gnu.org/onlinedocs/gcc/Code-Gen-Options.html)
# Options:
# export MAKEFLAGS = -j1 # In case you want single-threaded compilation
export # exports all variables to children (epic)
# Select standard
STD := c++11
# Name directories
SRC_DIR := src
BUILD_DIR := build
OBJ_DIR := ${BUILD_DIR}/obj
DEP_DIR := ${BUILD_DIR}/dep
CPPCHECK_DIR := ${BUILD_DIR}/cppcheck
BUILD_DIRS := ${BUILD_DIR} ${OBJ_DIR} ${DEP_DIR} ${CPPCHECK_DIR}
# Name executable
BIN := ${BUILD_DIR}/executable_name
# List directories where header files are found
_INCLUDE := include # /usr/lib/boost_1_80_0 /usr/lib/eigen-3.4.0
INCLUDE := ${_INCLUDE:%=-I%}
# Make obj and dep files per source file
SRC := $(wildcard ${SRC_DIR}/*.cc) # Sneaky trick to get all .cc in a directory
OBJ := ${SRC:${SRC_DIR}/%.cc=${OBJ_DIR}/%.o}
DEP := ${OBJ:${OBJ_DIR}/%.o=${DEP_DIR}/%.d}
# cppcheck setup
CHECKFLAGS := --cppcheck-build-dir=${CPPCHECK_DIR} --std=${STD} --quiet --error-exitcode=1
# Set up modefile
MODEFILE := build/mode
ifeq (${mode},) # default to debug build
mode := debug
endif
# If "make" or "make all" is called, output info and make the executable
.PHONY: all
all:
@make --no-print-directory information
@mkdir -p build
@touch ${MODEFILE}
ifneq (${mode},$(file < ${MODEFILE}))
@make --no-print-directory clean
endif
@make --no-print-directory -f build.mk
@echo ${mode} > build/mode
# Run cppcheck on all source files
.PHONY: check
check: ${SRC}
cppcheck ${CHECKFLAGS} $^
# Info: Echo makeflags and build mode
.PHONY: information
information:
ifeq (${mode},release)
@echo -n "\033[38;5;196m"
else ifeq (${mode},debug)
@echo -n "\033[38;5;39m"
else
@echo "\033[1;31;40mInvalid build mode.\033[0m"
@echo "\033[1;31;40mPlease use 'make mode=release' or 'make mode=debug'\033[0m"
@exit 1
endif
@echo "Building in "$(mode)" mode...\033[0m "
ifneq ($(strip $(subst --no-print-directory,,${MAKEFLAGS})),)
@echo "\033[38;5;184mMAKEFLAGS: $(strip $(subst -- mode=release,,$(subst --no-print-directory,,${MAKEFLAGS})))\033[0m"
endif
# Create the required directories if they don't already exist
${BUILD_DIRS}:
mkdir -p $@
# If "make run" is called, make the executable and run it
.PHONY: run
run: all
@echo "\033[38;5;40mRunning\033[0m ${BIN}..."
@${BIN} ${ARGS}
# Delete all object files, all dep files, and the executable
.PHONY: clean
clean:
rm -rf ${BUILD_DIR}
# Easter egg
.PHONY: apocalypse
apocalypse: clean
@echo "\033[91mI love the smell of napalm in the morning\033[39m"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment