Last active
August 4, 2021 13:33
-
-
Save ljmccarthy/8c67eebd71027a2705c598883a1dd723 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
TARGETNAME := my-program | |
BUILDDIR := build | |
SRCDIRS := src | |
INCDIRS := include | |
LIBDIRS := | |
LIBS := | |
DEFINES := | |
PKGS := | |
STDC := c11 | |
STDCPP := c++20 | |
WARNFLAGS := -Wall -W | |
VARIANT ?= release | |
ifeq ($(VARIANT),debug) | |
OPTFLAGS := -O0 -ggdb | |
else | |
ifeq ($(VARIANT),release) | |
OPTFLAGS := -O2 -fomit-frame-pointer | |
DEFINES += NDEBUG | |
else | |
$(error Unknown variant $(VARIANT)) | |
endif | |
endif | |
CFLAGS := -std=$(STDC) $(WARNFLAGS) $(OPTFLAGS) -fvisibility=hidden | |
CXXFLAGS := -std=$(STDCPP) $(WARNFLAGS) $(OPTFLAGS) -fvisibility=hidden -fvisibility-inlines-hidden | |
LDFLAGS := | |
TARGETPATH := $(BUILDDIR)/$(VARIANT)/$(TARGETNAME) | |
PKG_CFLAGS := $(foreach pkg,$(PKGS),$(shell pkg-config --cflags $(pkg))) | |
PKG_LIBS := $(foreach pkg,$(PKGS),$(shell pkg-config --libs $(pkg))) | |
CPPFLAGS := $(PKG_CFLAGS) $(DEFINES:%=-D%) $(INCDIRS:%=-I%) | |
LIBFLAGS := $(PKG_LIBS) $(LIBDIRS:%=-L%) $(LIBS:%=-l%) | |
LINKFLAGS := $(OPTFLAGS) $(LDFLAGS:%=-Wl,%) | |
SRCFILES := $(foreach dir,$(SRCDIRS),$(wildcard $(dir)/*.c)) \ | |
$(foreach dir,$(SRCDIRS),$(wildcard $(dir)/*.cpp)) | |
OBJDIR := $(BUILDDIR)/$(VARIANT) | |
DEPFILES := $(SRCFILES:%=$(OBJDIR)/%.d) | |
OBJFILES := $(SRCFILES:%=$(OBJDIR)/%.o) | |
.PHONY : all clean | |
all : $(TARGETPATH) | |
clean : | |
@rm -rf $(BUILDDIR) | |
$(TARGETPATH) : $(OBJFILES) | |
@echo " Linking : $@" | |
@$(CXX) -pipe $(LINKFLAGS) $(LIBFLAGS) $(OBJFILES) -o $@ | |
$(OBJDIR)/%.c.o : %.c | |
@echo " Compiling : $<" | |
@mkdir -p $(dir $@) | |
@$(CC) -pipe -MMD -MP -MT "$(OBJDIR)/$<.o" $(CFLAGS) $(CPPFLAGS) -c -o $@ $< | |
$(OBJDIR)/%.cpp.o : %.cpp | |
@echo " Compiling : $<" | |
@mkdir -p $(dir $@) | |
@$(CXX) -pipe -MMD -MP -MT "$(OBJDIR)/$<.o" $(CXXFLAGS) $(CPPFLAGS) -c -o $@ $< | |
ifneq ($(MAKECMDGOALS),clean) | |
-include $(DEPFILES) | |
endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment