Skip to content

Instantly share code, notes, and snippets.

@Leo1003
Last active October 9, 2022 13:07
Show Gist options
  • Save Leo1003/73a23a8666e2e8d9d5b08934c6d502fa to your computer and use it in GitHub Desktop.
Save Leo1003/73a23a8666e2e8d9d5b08934c6d502fa to your computer and use it in GitHub Desktop.
Leo's makefile
# Toolchain settings
TARGET :=
CC := $(TARGET)gcc
CXX := $(TARGET)g++
AS := $(TARGET)as
LD := $(TARGET)gcc
GDB := $(TARGET)gdb
BUILD ?= debug
# User Settings
BINNAME = main
PKG_CONFIG =
AINCDIR =
INCDIR =
LIBS = m
FLAGS =
CFLAGS = -std=c11
CXXFLAGS = -std=c++17
LDFLAGS = -Wl,--gc-sections
DEFFLAGS =
DBGFLAGS = -g -DDEBUG
LDSCRIPT =
# Compile Flags
__FLAGS = $(shell pkg-config --cflags $(PKG_CONFIG))
__ASFLAGS = $(foreach d, $(AINCDIR), -I$d)
__CFLAGS = $(OPTIFLAGS) $(DEFFLAGS) $(foreach d, $(INCDIR), -I$d) $(CFLAGS)
__CXXFLAGS = $(OPTIFLAGS) $(DEFFLAGS) $(foreach d, $(INCDIR), -I$d) $(CXXFLAGS)
__LDFLAGS = $(shell pkg-config --libs $(PKG_CONFIG)) $(foreach l, $(LIBS), -l$l) $(LDFLAGS)
OPTIFLAGS =
ifeq ($(BUILD),debug)
OBJDIR = obj/Debug
DEPDIR = dep/Debug
__FLAGS += $(DBGFLAGS)
OPTIFLAGS = -O0
else ifeq ($(BUILD),release)
OBJDIR = obj/Release
DEPDIR = dep/Release
OPTIFLAGS = -O2
__FLAGS += -DNDEBUG
else
OBJDIR = $(error "Unknown build type")
endif
ifneq ($(LDSCRIPT),)
__LDFLAGS += -T $(LDSCRIPT)
endif
SRCS.s = $(wildcard *.s)
SRCS.c = $(wildcard *.c)
SRCS.cpp = $(wildcard *.cpp)
OBJFILES = $(SRCS.c:.c=.o) $(SRCS.cpp:.cpp=.o) $(SRCS.s:.s=.o)
OBJS = $(addprefix $(OBJDIR)/,$(OBJFILES))
DEPFILES = $(SRCS.c:.c=.d) $(SRCS.cpp:.cpp=.d)
DEPS = $(addprefix $(DEPDIR)/,$(DEPFILES))
.PHONY: clean distclean gdb
all: $(BINNAME)
$(OBJDIR):
mkdir -p $(OBJDIR)
$(DEPDIR):
mkdir -p $(DEPDIR)
$(OBJDIR)/%.o: %.s | $(OBJDIR)
$(AS) $(FLAGS) $(__ASFLAGS) -o $@ $<
$(OBJDIR)/%.o: %.c | $(OBJDIR)
$(CC) $(FLAGS) $(__FLAGS) $(__CFLAGS) -c -o $@ $<
$(OBJDIR)/%.o: %.cpp | $(OBJDIR)
$(CXX) $(FLAGS) $(__FLAGS) $(__CXXFLAGS) -c -o $@ $<
$(DEPDIR)/%.d: %.c | $(DEPDIR)
@echo -n "$(OBJDIR)/" > $@
@$(CC) -MM -MP $(FLAGS) $(__CFLAGS) $< >> $@
$(DEPDIR)/%.d: %.cpp | $(DEPDIR)
@echo -n "$(OBJDIR)/" > $@
@$(CXX) -MM -MP $(FLAGS) $(__CXXFLAGS) $< >> $@
$(BINNAME): $(OBJS)
$(LD) $(FLAGS) $(__LDFLAGS) -o $@ $(OBJS)
ifneq ($(DEPS),)
sinclude $(DEPS)
endif
gdb: $(BINNAME)
$(GDB) -se $(BINNAME)
clean:
rm -rf obj
rm -f *.o $(BINNAME)
distclean: clean
rm -rf dep
rm -f *.d
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment