makefile/makefile.basic

116 lines
3.0 KiB
Plaintext

#This program is free software: you can redistribute it and/or modify
#it under the terms of the GNU General Public License as published by
#the Free Software Foundation, either version 3 of the License, or
#(at your option) any later version.
#This program is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
#GNU General Public License for more details.
#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
#Makefile to generate a single executable from all sources in SOURCE_DIRS that end in EXT
ifeq ($(OS),Windows_NT)
WINDOWS=1
endif
SOURCE_DIRS::=src
OBJDIR::=obj
DEPDIR::=$(OBJDIR)/dep
LIBDIR::=lib
INCLUDE_DIRS::=include
CXXFLAGS::=-g -std=c++17 -Wall -pedantic -Wextra
EXT::=cpp
MAIN_EXECUTABLE::=tester
RELEASE?=0
ifneq ($(WINDOWS),1)
CXX::=g++
LDLIBS::=
LDFLAGS::=
STRIP?=strip
RANLIB?=ranlib
AR?=ar
AS?=as
else
MINGW_PREFIX::=x86_64-w64-mingw32-
CXX::=$(MINGW_PREFIX)g++
LDLIBS::=
LDFLAGS::=
STRIP?=$(MINGW_PREFIX)strip
RANLIB?=$(MINGW_PREFIX)ranlib
AR?=$(MINGW_PREFIX)ar
AS?=$(MINGW_PREFIX)as
MAIN_EXECUTABLE::=$(MAIN_EXECUTABLE).exe
endif
ifeq ($(RELEASE),1)
#a lot of false strict aliasing warnings from gcc 9
CXXFLAGS+=-O2 -Wno-strict-aliasing
else ifeq ($(MEMCHK),1)
LDFLAGS+=-fsanitize=address -fno-omit-frame-pointer -fno-optimize-sibling-calls
CXXFLAGS+=-O0 -g3 -ggdb -fsanitize=address -fno-omit-frame-pointer -fno-optimize-sibling-calls
else
CXXFLAGS+=-O0 -g3 -ggdb
endif
ifeq ($(OS),Windows_NT)
mkdir=mkdir $(subst /,\,$(1)) > NUL 2>&1
rm=del /F $(1) > NUL 2>&1
rmdir=rd /S /Q $(1) > NUL 2>&1
move=move /Y $(subst /,\,$(1)) $(subst /,\,$(2)) > NUL 2>&1
copy=copy /Y /B $(subst /,\,$(1)) $(subst /,\,$(2)) > NUL 2>&1
else
mkdir=mkdir -p $(1)
rm=rm -f $(1)
rmdir=rm -rf $(1)
move=mv $(1) $(2)
copy=cp $(1) $(2)
endif
INTERNAL_CXXFLAGS=-c $(foreach dir,$(INCLUDE_DIRS),-I"$(dir)") -MMD -MP -MF"$(DEPDIR)/$(notdir $(patsubst %.o,%.d,$@))"
SOURCES::=$(foreach source,$(SOURCE_DIRS),$(foreach ext,$(EXT),$(wildcard $(source)/*.$(ext))))
OBJECTS::=$(addprefix $(OBJDIR)/,$(subst \,.,$(subst /,.,$(addsuffix .o,$(SOURCES)))))
.PHONY: all
all: $(MAIN_EXECUTABLE)
ifeq ($(RELEASE),1)
$(CXX) $(LDFLAGS) $^ -o "$(basename $(MAIN_EXECUTABLE))" $(LDLIBS)
$(STRIP) --strip-all "$(MAIN_EXECUTABLE)"
endif
$(MAIN_EXECUTABLE): $(OBJECTS)
$(CXX) $(LDFLAGS) $^ -o "$(basename $@)" $(LDLIBS)
define GENERATE_OBJECTS
$$(OBJDIR)/$(subst \,.,$(subst /,.,$(1))).%.o: $(1)/%
$$(CXX) $$(CXXFLAGS) $$(INTERNAL_CXXFLAGS) "$$<" -o "$$@"
endef
$(foreach dir,$(SOURCE_DIRS),$(eval $(call GENERATE_OBJECTS,$(dir))))
$(OBJECTS): | $(OBJDIR) $(DEPDIR)
$(OBJDIR):
$(call mkdir,"$@")
$(DEPDIR):
$(call mkdir,"$@")
.PHONY: clean
clean:
$(call rmdir,"$(DEPDIR)")
$(call rmdir,"$(OBJDIR)")
$(call rm,"$(MAIN_EXECUTABLE)")
ifneq ($(OS),Windows_NT)
$(call rm,"$(MAIN_EXECUTABLE).exe")
endif
-include $(wildcard $(DEPDIR)/*.d)