Update basic makefile to allow easily running custom targets before and after build

This commit is contained in:
rexy712 2020-01-06 11:38:05 -08:00
parent 64df153961
commit 13c61f26a3

View File

@ -11,7 +11,7 @@
#You should have received a copy of the GNU General Public License
#along with this program. If not, see <http://www.gnu.org/licenses/>.
#Copyright 2018-2019 rexy712
#Copyright 2018-2020 rexy712
#Makefile to generate a single executable from all sources in SOURCE_DIRS that end in EXT
@ -29,6 +29,8 @@ CXXFLAGS::=-g -std=c++17 -Wall -pedantic -Wextra
EXT::=cpp
LANG::=$(EXT)
MAIN_EXECUTABLE::=tester
PRE_TARGETS::=
POST_TARGETS::=
RELEASE?=0
MEMCHK?=0
@ -54,9 +56,23 @@ else #windows
RANLIB::=$(MINGW_PREFIX)ranlib
AR::=$(MINGW_PREFIX)ar
AS::=$(MINGW_PREFIX)as
MAIN_EXECUTABLE::=$(MAIN_EXECUTABLE).exe
endif #windows
#Put your custom targets for PRE_TARGETS and POST_TARGETS here:
###########################################################################################################
#Everything past this point is internal BS, probably best not to touch it unless you know what you're doing
#set the all target as the default target, otherwise the topmost target will run
.DEFAULT_GOAL::=all
#set the main target to match the output of mingw
ifeq ($(WINDOWS),1)
MAIN_EXECUTABLE::=$(MAIN_EXECUTABLE).exe
endif
#system dependant bullshit
ifeq ($(OS),Windows_NT)
#windows' cmd commands
@ -86,6 +102,7 @@ endif
ifeq ($(RELEASE),1)
#a lot of false strict aliasing warnings from gcc 9
COMPILER_FLAGS+=-O2 -Wno-strict-aliasing
POST_TARGETS+= do_strip
else ifeq ($(MEMCHK),1)
#use asan to check memory leaks/invalid accesses
LDFLAGS+=-fsanitize=address -fno-omit-frame-pointer -fno-optimize-sibling-calls
@ -97,14 +114,28 @@ endif
#add dependency tracking and include directories
INTERNAL_COMPILERFLAGS=-c $(foreach dir,$(INCLUDE_DIRS),-I"$(dir)") -MMD -MP -MF"$(DEPDIR)/$(notdir $(patsubst %.o,%.d,$@))"
THIS_MAKEFILE_NAME::=$(lastword $(MAKEFILE_LIST))
SOURCES::=$(foreach source,$(SOURCE_DIRS),$(foreach ext,$(EXT),$(wildcard $(source)/*.$(ext))))
OBJECTS::=$(addprefix $(OBJDIR)/,$(subst \,.,$(subst /,.,$(addsuffix .o,$(SOURCES)))))
#generate a command to run a submake using this same makefile without printing "Entering directory" stuff
#note: the empty line is required in this definition
define RUN_SUBMAKE_TARGET
@$(MAKE) --no-print-directory -f "$(THIS_MAKEFILE_NAME)" "$(1)"
endef
#default target: run targets in PRE_TARGETS, then the main executable, then POST_TARGETS
.PHONY: all
all: $(MAIN_EXECUTABLE)
ifeq ($(RELEASE),1)
all:
$(foreach target,$(PRE_TARGETS),$(call RUN_SUBMAKE_TARGET,$(target)))
$(call RUN_SUBMAKE_TARGET,$(MAIN_EXECUTABLE))
$(foreach target,$(POST_TARGETS),$(call RUN_SUBMAKE_TARGET,$(target)))
#Called in POST_TARGETS when RELEASE=1
.PHONY: do_strip
do_strip:
$(STRIP) --strip-all "$(MAIN_EXECUTABLE)"
endif
#Link executable
$(MAIN_EXECUTABLE): $(OBJECTS)
@ -131,10 +162,7 @@ clean:
$(call rmdir,"$(DEPDIR)")
$(call rmdir,"$(OBJDIR)")
$(call rm,"$(MAIN_EXECUTABLE)")
ifneq ($(WINDOWS),1)
$(call rm,"$(MAIN_EXECUTABLE).exe")
endif
#header file dep tracking
-include $(wildcard $(DEPDIR)/*.d)