Skip to content

Instantly share code, notes, and snippets.

@LiquidityC
Last active September 2, 2024 17:48
Show Gist options
  • Save LiquidityC/c832cd3d8394f267299d43b1f1719e90 to your computer and use it in GitHub Desktop.
Save LiquidityC/c832cd3d8394f267299d43b1f1719e90 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
@lpsantil
Copy link

lpsantil commented Oct 19, 2023

@LiquidityC
Copy link
Author

I'm not sure I follow?

@lpsantil
Copy link

Geez. Totally missed $(LDFLAGS) at the end of the line. My mistake.

@angeloc
Copy link

angeloc commented Oct 20, 2023

To make it cross-compile compatible, you should declare the variable with ?=. That the makefile can grab compiler or the ldflags variables from the environment as exported by the various SDKs (like the yocto one).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment