Skip to content

Commit

Permalink
Updated Makefile
Browse files Browse the repository at this point in the history
  • Loading branch information
avdstaaij committed Nov 19, 2021
1 parent 3dabb5c commit a53627d
Showing 1 changed file with 52 additions and 24 deletions.
76 changes: 52 additions & 24 deletions examples/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,15 @@
# The variables below the "Settings" header can be altered to change the build
# behavior. Their meaning is as follows:

# BINNAME_R The name of the generated release binary
# BINNAME_D The name of the genereated debug binary

# CXX The C++ compiler
# CXXFLAGS Compiler flags, both for building object files and linking them
# FLAGS_R Release-specific compiler flags
# FLAGS_D Debug-specific compiler flags
# LDFLAGS Compiler flags used when linking the the binary
# INCFLAGS Compiler flags for file inclusion; used for building object files
# BINNAME_R The name of the generated release binary
# BINNAME_D The name of the genereated debug binary

# BINDIR Directory in which the binaries are placed
# SRCDIR Directory in which to look for C++ source files
Expand All @@ -51,21 +52,23 @@
# these commands with $^ set to the list of object files and $@ set to the path
# of the binary to create.

# VERBOSE Should be 0 or 1; if 1, all executed commands are fully printed
# FILE_TPUT The tput commands used to style filenames in progress messages

#==============================================================================#
#---------------------------------- Settings ----------------------------------#
#==============================================================================#

CXX = g++
BINNAME_R = examples
BINNAME_D = $(BINNAME_R)_debug

CXX = g++
CXXFLAGS = -std=c++11 -Wall -Wextra -Wignored-qualifiers
FLAGS_R = -O2
FLAGS_D = -g
FLAGS_R = -O2 -DNDEBUG
FLAGS_D = -Og -g3 -DDEBUG
LDFLAGS =
INCFLAGS = -I ../include

BINNAME_R = examples
BINNAME_D = $(BINNAME_R)_debug

BINDIR = .
SRCDIR = .
BLDDIR = build
Expand All @@ -77,6 +80,9 @@ RM = rm -f
MKBIN_R = $(CXX) $(CXXFLAGS) $(FLAGS_R) $^ $(LDFLAGS) -o $@
MKBIN_D = $(CXX) $(CXXFLAGS) $(FLAGS_D) $^ $(LDFLAGS) -o $@

VERBOSE = 0
FILE_TPUT = tput bold; tput setaf 3

#==============================================================================#
#------------------ Are you sure you know what you're doing? ------------------#
#==============================================================================#
Expand All @@ -87,6 +93,10 @@ ifeq ($(BLDDIR), .)
$(error You MONSTER, never set BLDDIR to '.'! It would clutter your project directory)
endif

ifeq ($(findstring $(VERBOSE),01),)
$(error The VERBOSE variable is set to "$(VERBOSE)", but it should be either 0 or 1)
endif


# Functions

Expand Down Expand Up @@ -122,6 +132,18 @@ DEPGENFLAGS_D = -MT "$@" -MMD -MP -MF $(DEPDIR_D)/$*.Td
POSTCOMPILE_R = mv -f $(DEPDIR_R)/$*.Td $(DEPDIR_R)/$*.d && touch $@
POSTCOMPILE_D = mv -f $(DEPDIR_D)/$*.Td $(DEPDIR_D)/$*.d && touch $@

QUIET ::= @
ifeq ($(VERBOSE),1)
QUIET ::=
endif

FILE_TPUT_BEGIN ::=
FILE_TPUT_END ::=
ifneq ($(shell tput -V 2>/dev/null),)
FILE_TPUT_BEGIN ::= $(shell $(FILE_TPUT))
FILE_TPUT_END ::= $(shell tput sgr0)
endif


# Targets

Expand All @@ -139,42 +161,48 @@ release: $(BIN_R)

.PHONY: rrun drun
rrun: release
$(BIN_R) $(ARGS)
$(QUIET)$(BIN_R) $(ARGS)
drun: debug
$(BIN_D) $(ARGS)
$(QUIET)$(BIN_D) $(ARGS)

%/.:
mkdir -p $@
$(QUIET)mkdir -p $@

$(BIN_R): $(OBJS_R) | $(BINDIR)/.
$(MKBIN_R)
@echo 'Creating $(FILE_TPUT_BEGIN)$@$(FILE_TPUT_END)'
$(QUIET)$(MKBIN_R)

$(BIN_D): $(OBJS_D) | $(BINDIR)/.
$(MKBIN_D)
@echo 'Creating $(FILE_TPUT_BEGIN)$@$(FILE_TPUT_END)'
$(QUIET)$(MKBIN_D)

$(DEPS): ;
include $(wildcard $(DEPS))

.PHONY: c
c:
-if [ -d '$(OBJDIR)' ]; then find '$(OBJDIR)' -type f -name '*.o' -exec $(RM) {} +; fi
-if [ -d '$(DEPDIR)' ]; then find '$(DEPDIR)' -type f -name '*.d' -exec $(RM) {} +; fi
-if [ -d '$(DEPDIR)' ]; then find '$(DEPDIR)' -type f -name '*.Td' -exec $(RM) {} +; fi
-find $(OBJDIR) $(DEPDIR) -type d -empty -exec 'rmdir' '-p' {} \; 2>/dev/null || true
@echo 'Removing cached build byproducts'
$(QUIET)-if [ -d '$(OBJDIR)' ]; then find '$(OBJDIR)' -type f -name '*.o' -exec $(RM) {} +; fi
$(QUIET)-if [ -d '$(DEPDIR)' ]; then find '$(DEPDIR)' -type f -name '*.d' -exec $(RM) {} +; fi
$(QUIET)-if [ -d '$(DEPDIR)' ]; then find '$(DEPDIR)' -type f -name '*.Td' -exec $(RM) {} +; fi
$(QUIET)-find $(OBJDIR) $(DEPDIR) -type d -empty -exec 'rmdir' '-p' {} \; 2>/dev/null || true

.PHONY: clean
clean: c
-$(RM) $(BIN_R)
-$(RM) $(BIN_D)
-if [ -d $(BINDIR) ]; then rmdir --ignore-fail-on-non-empty -p "$$(cd '$(BINDIR)'; pwd)"; fi
@echo 'Removing generated binaries'
$(QUIET)-$(RM) $(BIN_R)
$(QUIET)-$(RM) $(BIN_D)
$(QUIET)-if [ -d $(BINDIR) ]; then rmdir --ignore-fail-on-non-empty -p "$$(cd '$(BINDIR)'; pwd)"; fi


.SECONDEXPANSION:

$(OBJDIR_R)/%.o: $(SRCDIR)/%$(SRCEXT) $(DEPDIR_R)/%.d | $$(dir $$@). $$(dir $(DEPDIR_R)/$$*.d).
$(CXX) $(CXXFLAGS) $(FLAGS_R) $(INCFLAGS) -c $< -o $@ $(DEPGENFLAGS_R)
$(POSTCOMPILE_R)
@echo 'Compiling $(FILE_TPUT_BEGIN)$<$(FILE_TPUT_END)'
$(QUIET)$(CXX) $(CXXFLAGS) $(FLAGS_R) $(INCFLAGS) -c $< -o $@ $(DEPGENFLAGS_R)
$(QUIET)$(POSTCOMPILE_R)

$(OBJDIR_D)/%.o: $(SRCDIR)/%$(SRCEXT) $(DEPDIR_D)/%.d | $$(dir $$@). $$(dir $(DEPDIR_D)/$$*.d).
$(CXX) $(CXXFLAGS) $(FLAGS_D) $(INCFLAGS) -c $< -o $@ $(DEPGENFLAGS_D)
$(POSTCOMPILE_D)
@echo 'Compiling $(FILE_TPUT_BEGIN)$<$(FILE_TPUT_END)'
$(QUIET)$(CXX) $(CXXFLAGS) $(FLAGS_D) $(INCFLAGS) -c $< -o $@ $(DEPGENFLAGS_D)
$(QUIET)$(POSTCOMPILE_D)

0 comments on commit a53627d

Please sign in to comment.