Last active
September 22, 2023 08:21
-
-
Save matthewelmer/943bcf62bb14b8ce1ed22ff47387832f to your computer and use it in GitHub Desktop.
simpler makefile
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 | |
# export MAKEFLAGS = -j1 # For single-threaded compilation | |
# 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 | |
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} | |
# Compiler, preprocessor, and linker setup | |
STD := c++11 | |
CXX := g++ | |
CXXFLAGS := -Wall -Wextra -Wshadow -std=${STD} | |
CPPFLAGS = -MP -MMD -MT $@ -MF ${DEP_DIR}/$*.d # Note: uses = instead of := | |
LDFLAGS := | |
# Debug configuration | |
CXXFLAGS += -g | |
# Release configuration | |
# CXXFLAGS += -O3 | |
# CPPFLAGS += -DRELEASE -DNDEBUG | |
# If "make" or "make all" is called, output info. Check and make the executable. | |
.PHONY: all | |
all: ${BIN} | |
.PHONY: memcheck | |
memcheck: all | |
valgrind --leak-check=yes ${BIN} ${ARGS} | |
# If "make run" is called, make the executable and run it | |
.PHONY: run | |
run: all | |
${BIN} ${ARGS} | |
# Delete all object files, all dep files, and the executable | |
.PHONY: clean | |
clean: | |
rm -rf ${BUILD_DIR} | |
# Link the object files to build the executable | |
${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 --enable=warning --cppcheck-build-dir=${CPPCHECK_DIR} --std=${STD} --quiet --error-exitcode=1 $< | |
${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} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment