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 #You should have received a copy of the GNU General Public License
#along with this program. If not, see <http://www.gnu.org/licenses/>. #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 #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 EXT::=cpp
LANG::=$(EXT) LANG::=$(EXT)
MAIN_EXECUTABLE::=tester MAIN_EXECUTABLE::=tester
PRE_TARGETS::=
POST_TARGETS::=
RELEASE?=0 RELEASE?=0
MEMCHK?=0 MEMCHK?=0
@ -54,9 +56,23 @@ else #windows
RANLIB::=$(MINGW_PREFIX)ranlib RANLIB::=$(MINGW_PREFIX)ranlib
AR::=$(MINGW_PREFIX)ar AR::=$(MINGW_PREFIX)ar
AS::=$(MINGW_PREFIX)as AS::=$(MINGW_PREFIX)as
MAIN_EXECUTABLE::=$(MAIN_EXECUTABLE).exe
endif #windows 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 #system dependant bullshit
ifeq ($(OS),Windows_NT) ifeq ($(OS),Windows_NT)
#windows' cmd commands #windows' cmd commands
@ -86,6 +102,7 @@ endif
ifeq ($(RELEASE),1) ifeq ($(RELEASE),1)
#a lot of false strict aliasing warnings from gcc 9 #a lot of false strict aliasing warnings from gcc 9
COMPILER_FLAGS+=-O2 -Wno-strict-aliasing COMPILER_FLAGS+=-O2 -Wno-strict-aliasing
POST_TARGETS+= do_strip
else ifeq ($(MEMCHK),1) else ifeq ($(MEMCHK),1)
#use asan to check memory leaks/invalid accesses #use asan to check memory leaks/invalid accesses
LDFLAGS+=-fsanitize=address -fno-omit-frame-pointer -fno-optimize-sibling-calls LDFLAGS+=-fsanitize=address -fno-omit-frame-pointer -fno-optimize-sibling-calls
@ -97,14 +114,28 @@ endif
#add dependency tracking and include directories #add dependency tracking and include directories
INTERNAL_COMPILERFLAGS=-c $(foreach dir,$(INCLUDE_DIRS),-I"$(dir)") -MMD -MP -MF"$(DEPDIR)/$(notdir $(patsubst %.o,%.d,$@))" 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)))) SOURCES::=$(foreach source,$(SOURCE_DIRS),$(foreach ext,$(EXT),$(wildcard $(source)/*.$(ext))))
OBJECTS::=$(addprefix $(OBJDIR)/,$(subst \,.,$(subst /,.,$(addsuffix .o,$(SOURCES))))) 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 .PHONY: all
all: $(MAIN_EXECUTABLE) all:
ifeq ($(RELEASE),1) $(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)" $(STRIP) --strip-all "$(MAIN_EXECUTABLE)"
endif
#Link executable #Link executable
$(MAIN_EXECUTABLE): $(OBJECTS) $(MAIN_EXECUTABLE): $(OBJECTS)
@ -131,10 +162,7 @@ clean:
$(call rmdir,"$(DEPDIR)") $(call rmdir,"$(DEPDIR)")
$(call rmdir,"$(OBJDIR)") $(call rmdir,"$(OBJDIR)")
$(call rm,"$(MAIN_EXECUTABLE)") $(call rm,"$(MAIN_EXECUTABLE)")
ifneq ($(WINDOWS),1)
$(call rm,"$(MAIN_EXECUTABLE).exe") $(call rm,"$(MAIN_EXECUTABLE).exe")
endif
#header file dep tracking #header file dep tracking
-include $(wildcard $(DEPDIR)/*.d) -include $(wildcard $(DEPDIR)/*.d)