Skip to content

Instantly share code, notes, and snippets.

@Xisabla
Created June 10, 2022 13:10
Show Gist options
  • Save Xisabla/019ab731c547cd4175f525de3719e83f to your computer and use it in GitHub Desktop.
Save Xisabla/019ab731c547cd4175f525de3719e83f to your computer and use it in GitHub Desktop.
Makefiles for C project with dynamic libraries (modules)
#[[--------------------------------------- Configuration ---------------------------------------]]#
Q = @
ifeq ($(VERBOSE),1)
Q =
endif
#[[-------------------------------------- Recipes > Core ---------------------------------------]]#
%.o: %.c
@echo "CC $@"
$(Q)$(CC) $(CFLAGS) -fPIC -I../include -c $< -o $@
fs-xfs.so: fs.c
@echo "CC $@"
$(Q) $(CC) -shared $(CFLAGS) -fPIC -I../include $? -o $@
clean:
$(Q)rm -f fs-xfs.so *.o
#[[--------------------------------------- Configuration ---------------------------------------]]#
# Make variables
CC = clang
CFLAGS = -Wall -g -O0 -DDEBUG
LDFLAGS =
LDLIBS = -ldl
MAKEFLAGS += --no-print-directory
.DEFAULT_GOAL: all
.PHONY: all clean doc format release tests
Q = @
ifeq ($(VERBOSE),1)
Q =
endif
# Core
BINARIES = \
fsmk \
fsdump
# Modules
MODULES = \
fs-xfs/fs-xfs.so
# Tests
TESTS = \
test/mkfs
#[[-------------------------------------- Recipes > Core ---------------------------------------]]#
release: CFLAGS = -Wall -O2
release: clean all doc
all: $(BINARIES) $(MODULES)
common/%.o: common/%.c
@echo "CC $@"
$(Q)$(CC) $(CFLAGS) -Iinclude -c $< -o $@
%: bin/%.c common/fslib_loader.o
@echo "CC $@"
$(Q)$(CC) $(CFLAGS) -Iinclude $? -o $@ $(LDFLAGS) $(LDLIBS)
#fstest: bin/fstest.c common/helpers.o
# $(CC) $(CFLAGS) -Iinclude $? -o $@
clean: __m_clean
$(Q)$(RM) -f $(BINARIES) $(MODULES) $(TESTS)
$(Q)$(RM) -f common/*.o
$(Q)$(RM) -rf doc/html
$(Q)$(MAKE) -C fs-xfs clean
#[[------------------------------------- Recipes > Modules -------------------------------------]]#
fs-xfs/fs-xfs.so: __m_build_fs-xfs.so
__m_clean: __m_clean_fs-xfs
__m_build_%.so: %/Makefile
$(Q)$(MAKE) CC="$(CC)" CFLAGS="$(CFLAGS)" VERBOSE="$(VERBOSE)" -C $* $*.so
__m_clean_%: %/Makefile
$(Q)$(MAKE) RM="$(RM)" VERBOSE="$(VERBOSE)" -C $* clean
#[[-------------------------------------- Recipes > Others -------------------------------------]]#
doc/html:
@echo "DOC doc"
$(Q)doxygen ./Doxyfile > /dev/null
doc: doc/html
format:
$(Q)clang-format ./**/**.c ./**/**.h -i
$(Q)clang-tidy ./**/**.c ./**/**.h --fix -- -Iinclude 2> /dev/null
#[[-------------------------------------- Recipes > Tests --------------------------------------]]#
test/mkfs: test/mkfs.c fs-xfs/fs-xfs.so common/fslib_loader.o
@echo "CC $@"
$(Q)$(CC) $(CFLAGS) -Iinclude test/mkfs.c common/fslib_loader.o $(LDFLAGS) $(LDLIBS) -o $@
test/%: test/%.c common/fslib_load.o
@echo "CC $@"
$(Q)$(CC) $(CFLAGS) -Iinclude $? -o $@
tests: $(TESTS)
$(Q)./scripts/run-tests.sh $(TESTS)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment