Skip to content

Instantly share code, notes, and snippets.

@adragomir
Forked from LiquidityC/Makefile
Created October 27, 2023 20:00
Show Gist options
  • Save adragomir/2f782392cd3a7a6a18c7b565bb426496 to your computer and use it in GitHub Desktop.
Save adragomir/2f782392cd3a7a6a18c7b565bb426496 to your computer and use it in GitHub Desktop.
Generic drop in Makefile
VERSION = \"1.0.0\"
PREFIX ?= out
INCDIR = inc
SRCDIR = src
LANG = c
OBJDIR = .obj
MODULE = binary_name
CC = gcc
LDFLAGS =
CFLAGS = -I$(INCDIR) -DVERSION=$(VERSION)
RM = rm
FORMAT = clang-format
CHECK = cppcheck
CP = cp
ECHO = echo
MKDIR = mkdir
# This is taken from the kernel build system because I like the way it looks
ifneq ($(silent),1)
ifneq ($(V),1)
QUIET_CC = @echo ' CC '$@;
QUIET_CC_FPIC = @echo ' CC FPIC '$@;
QUIET_CLANG = @echo ' CLANG '$@;
QUIET_AR = @echo ' AR '$@;
QUIET_LINK = @echo ' LINK '$@;
QUIET_MKDIR = @echo ' MKDIR '$@;
QUIET_GEN = @echo ' GEN '$@;
QUIET_SUBDIR0 = +@subdir=
QUIET_SUBDIR1 = ;$(NO_SUBDIR) \
echo ' SUBDIR '$$subdir; \
$(MAKE) $(PRINT_DIR) -C $$subdir
QUIET_FLEX = @echo ' FLEX '$@;
QUIET_BISON = @echo ' BISON '$@;
QUIET_GENSKEL = @echo ' GENSKEL '$@;
descend = \
+@echo ' DESCEND '$(1); \
mkdir -p $(OUTPUT)$(1) && \
$(MAKE) $(COMMAND_O) subdir=$(if $(subdir),$(subdir)/$(1),$(1)) $(PRINT_DIR) -C $(1) $(2)
QUIET_CLEAN = @printf ' CLEAN %s\n' $1;
QUIET_INSTALL = @printf ' INSTALL %s\n' $1;
QUIET_UNINST = @printf ' UNINST %s\n' $1;
endif
endif
ifndef RELEASE_BUILD
CFLAGS += -DDEBUG_BUILD -Wall -g -fsanitize=address -fno-omit-frame-pointer
else
CFLAGS += -O2 -DRELEASE_BUILD
OBJDIR = .objrls
endif
SRC = $(wildcard $(SRCDIR)/*.$(LANG))
OBJ = $(subst $(SRCDIR)/,$(OBJDIR)/,$(patsubst %.$(LANG),%.o,$(SRC)))
default: $(MODULE)
all: $(MODULE)
$(OBJDIR):
$(QUIET_MKDIR)$(MKDIR) -p $@
$(OBJ): $(OBJDIR)/%.o: $(SRCDIR)/%.$(LANG)
$(QUIET_CC)$(CC) $(CFLAGS) -c $< -o $@
$(MODULE): $(OBJDIR) $(OBJ)
$(QUIET_LINK)$(CC) $(CFLAGS) -o $@ $(OBJ) $(LDFLAGS)
run: $(MODULE)
@./$(MODULE)
clean:
$(call QUIET_CLEAN, $(MODULE))
@$(RM) -rf $(OBJ) $(MODULE) $(PREFIX) $(OBJDIR)
fmt:
@$(FORMAT) -i $(SRCDIR)/*.$(LANG) $(INCDIR)/*.h
check:
@$(ECHO) "Checking formatting"
@$(FORMAT) --dry-run -Werror $(SRCDIR)/*.[ch]
@$(CHECK) -x c --std=c11 -Iinc -i/usr/include --enable=all --suppress=missingIncludeSystem .
install: $(MODULE) $(MANFILES)
$(call QUIET_INSTALL, $(PREFIX)/bin/$(MODULE))
@$(MKDIR) -p $(PREFIX)/bin
@$(CP) $(MODULE) $(PREFIX)/bin/$(MODULE)
.PHONY: $(MODULE) clean all fmt run check install
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment