2020-05-02 22:06:47 +02:00
#### PROJECT SETTINGS ####
# The name of the executable to be created
BIN_NAME := reHDD
# Compiler used
CXX ?= g++
# Extension of source files used in the project
SRC_EXT = cpp
# Path to the source directory, relative to the makefile
SRC_PATH = src
# Space-separated pkg-config libraries used by this project
LIBS =
# General compiler flags
2020-08-26 18:38:39 +02:00
COMPILE_FLAGS = -std= c++17 -Wall -Wextra -g
2020-05-02 22:06:47 +02:00
# Additional release-specific flags
RCOMPILE_FLAGS = -D NDEBUG
# Additional debug-specific flags
DCOMPILE_FLAGS = -D DEBUG
# Add additional include paths
INCLUDES = include
# General linker settings
2020-08-04 22:35:29 +02:00
LINK_FLAGS = -lpthread -lncurses
2020-05-02 22:47:37 +02:00
# Doc
DOCDIR = doc
2020-05-02 22:06:47 +02:00
#### END PROJECT SETTINGS ####
# Optionally you may move the section above to a separate config.mk file, and
# uncomment the line below
# include config.mk
# Generally should not need to edit below this line
# Obtains the OS type, either 'Darwin' (OS X) or 'Linux'
UNAME_S := $( shell uname -s)
# Function used to check variables. Use on the command line:
# make print-VARNAME
# Useful for debugging and adding features
print-% : ; @echo $*=$( $ *)
# Shell used in this makefile
# bash is used for 'echo -en'
SHELL = /bin/bash
# Clear built-in rules
.SUFFIXES :
# Verbose option, to output compile and link commands
export V := false
export CMD_PREFIX := @
i f e q ( $( V ) , t r u e )
CMD_PREFIX :=
e n d i f
# Combine compiler and linker flags
release : export CXXFLAGS := $( CXXFLAGS ) $( COMPILE_FLAGS ) $( RCOMPILE_FLAGS )
release : export LDFLAGS := $( LDFLAGS ) $( LINK_FLAGS ) $( RLINK_FLAGS )
debug : export CXXFLAGS := $( CXXFLAGS ) $( COMPILE_FLAGS ) $( DCOMPILE_FLAGS )
debug : export LDFLAGS := $( LDFLAGS ) $( LINK_FLAGS ) $( DLINK_FLAGS )
# Build and output paths
release : export BUILD_PATH := build /release
release : export BIN_PATH := bin /release
debug : export BUILD_PATH := build /debug
debug : export BIN_PATH := bin /debug
install : export BIN_PATH := bin /release
# Find all source files in the source directory, sorted by most
# recently modified
i f e q ( $( UNAME_S ) , D a r w i n )
SOURCES = $( shell find $( SRC_PATH) -name '*.$(SRC_EXT)' | sort -k 1nr | cut -f2-)
e l s e
SOURCES = $( shell find $( SRC_PATH) -name '*.$(SRC_EXT)' -printf '%T@\t%p\n' \
| sort -k 1nr | cut -f2-)
e n d i f
# fallback in case the above fails
rwildcard = $( foreach d, $( wildcard $1 *) , $( call rwildcard,$d /,$2 ) \
$( filter $( subst *,%,$2 ) , $d ) )
i f e q ( $( SOURCES ) , )
SOURCES := $( call rwildcard, $( SRC_PATH) , *.$( SRC_EXT) )
e n d i f
# Set the object file names, with the source directory stripped
# from the path, and the build path prepended in its place
OBJECTS = $( SOURCES:$( SRC_PATH) /%.$( SRC_EXT) = $( BUILD_PATH) /%.o)
# Set the dependency files that will be used to add header dependencies
DEPS = $( OBJECTS:.o= .d)
# Macros for timing compilation
i f e q ( $( UNAME_S ) , D a r w i n )
CUR_TIME = awk 'BEGIN{srand(); print srand()}'
TIME_FILE = $( dir $@ ) .$( notdir $@ ) _time
START_TIME = $( CUR_TIME) > $( TIME_FILE)
END_TIME = read st < $( TIME_FILE) ; \
$( RM) $( TIME_FILE) ; \
st = $$ ( ( ` $( CUR_TIME) ` - $$ st) ) ; \
echo $$ st
e l s e
TIME_FILE = $( dir $@ ) .$( notdir $@ ) _time
START_TIME = date '+%s' > $( TIME_FILE)
END_TIME = read st < $( TIME_FILE) ; \
$( RM) $( TIME_FILE) ; \
st = $$ ( ( ` date '+%s' ` - $$ st - 86400) ) ; \
echo ` date -u -d @$$ st '+%H:%M:%S' `
e n d i f
# Version macros
# Comment/remove this section to remove versioning
USE_VERSION := false
# If this isn't a git repo or the repo has no tags, git describe will return non-zero
i f e q ( $( shell git describe > /dev /null 2>&1 ; echo $ $ ?) , 0 )
USE_VERSION := true
VERSION := $( shell git describe --tags --long --dirty --always | \
sed 's/v\([0-9]*\)\.\([0-9]*\)\.\([0-9]*\)-\?.*-\([0-9]*\)-\(.*\)/\1 \2 \3 \4 \5/g' )
VERSION_MAJOR := $( word 1, $( VERSION) )
VERSION_MINOR := $( word 2, $( VERSION) )
VERSION_PATCH := $( word 3, $( VERSION) )
VERSION_REVISION := $( word 4, $( VERSION) )
VERSION_HASH := $( word 5, $( VERSION) )
VERSION_STRING := \
" $( VERSION_MAJOR) . $( VERSION_MINOR) . $( VERSION_PATCH) . $( VERSION_REVISION) - $( VERSION_HASH) "
override CXXFLAGS := $( CXXFLAGS) \
-D VERSION_MAJOR = $( VERSION_MAJOR) \
-D VERSION_MINOR = $( VERSION_MINOR) \
-D VERSION_PATCH = $( VERSION_PATCH) \
-D VERSION_REVISION = $( VERSION_REVISION) \
-D VERSION_HASH = \" $( VERSION_HASH) \"
e n d i f
# Standard, non-optimized release build
.PHONY : release
release : dirs
i f e q ( $( USE_VERSION ) , t r u e )
@echo " Beginning release build v $( VERSION_STRING) "
e l s e
@echo "Beginning release build"
e n d i f
@$( START_TIME)
@$( MAKE) all --no-print-directory
@echo -n "Total build time: "
@$( END_TIME)
# Debug build for gdb debugging
.PHONY : debug
debug : dirs
i f e q ( $( USE_VERSION ) , t r u e )
@echo " Beginning debug build v $( VERSION_STRING) "
e l s e
@echo "Beginning debug build"
e n d i f
@$( START_TIME)
@$( MAKE) all --no-print-directory
@echo -n "Total build time: "
@$( END_TIME)
# Create the directories used in the build
.PHONY : dirs
dirs :
@echo "Creating directories"
@mkdir -p $( dir $( OBJECTS) )
@mkdir -p $( BIN_PATH)
# Removes all build files
.PHONY : clean
clean :
@echo " Deleting $( BIN_NAME) symlink "
@$( RM) $( BIN_NAME)
@echo "Deleting directories"
@$( RM) -r build
@$( RM) -r bin
# Main rule, checks the executable and symlinks to the output
all : $( BIN_PATH ) /$( BIN_NAME )
@echo " Making symlink: $( BIN_NAME) -> $< "
@$( RM) $( BIN_NAME)
@ln -s $( BIN_PATH) /$( BIN_NAME) $( BIN_NAME)
# Link the executable
$(BIN_PATH)/$(BIN_NAME) : $( OBJECTS )
@echo " Linking: $@ "
@$( START_TIME)
$( CMD_PREFIX) $( CXX) $( OBJECTS) $( LDFLAGS) -o $@
@echo -en "\t Link time: "
@$( END_TIME)
# Add dependency files, if they exist
- i n c l u d e $( DEPS )
# Source file rules
# After the first compilation they will be joined with the rules from the
# dependency files to provide header dependencies
$(BUILD_PATH)/%.o : $( SRC_PATH ) /%.$( SRC_EXT )
@echo " Compiling: $< -> $@ "
@$( START_TIME)
2020-05-02 22:47:37 +02:00
$( CMD_PREFIX) $( CXX) $( CXXFLAGS) -c $< -o $@
2020-05-02 22:06:47 +02:00
@echo -en "\t Compile time: "
@$( END_TIME)
2020-05-02 22:47:37 +02:00
.PHONY : docs
docs :
@$( RM) -r $( DOCDIR) /html
@doxygen $( DOCDIR) /doxyfile