1ifdef BUILDDIR 2# make sure BUILDDIR ends with a slash 3override BUILDDIR := $(BUILDDIR)/ 4# bit of a hack, but we want to make sure BUILDDIR directory structure 5# is correct before any commands 6$(if $(findstring n,$(MAKEFLAGS)),, $(shell mkdir -p \ 7 $(BUILDDIR) \ 8 $(BUILDDIR)bd \ 9 $(BUILDDIR)tests)) 10endif 11 12# overridable target/src/tools/flags/etc 13ifneq ($(wildcard test.c main.c),) 14TARGET ?= $(BUILDDIR)lfs 15else 16TARGET ?= $(BUILDDIR)lfs.a 17endif 18 19 20CC ?= gcc 21AR ?= ar 22SIZE ?= size 23CTAGS ?= ctags 24NM ?= nm 25LCOV ?= lcov 26 27SRC ?= $(wildcard *.c) 28OBJ := $(SRC:%.c=$(BUILDDIR)%.o) 29DEP := $(SRC:%.c=$(BUILDDIR)%.d) 30ASM := $(SRC:%.c=$(BUILDDIR)%.s) 31 32ifdef DEBUG 33override CFLAGS += -O0 -g3 34else 35override CFLAGS += -Os 36endif 37ifdef TRACE 38override CFLAGS += -DLFS_YES_TRACE 39endif 40override CFLAGS += -I. 41override CFLAGS += -std=c99 -Wall -pedantic 42override CFLAGS += -Wextra -Wshadow -Wjump-misses-init -Wundef 43 44ifdef VERBOSE 45override TESTFLAGS += -v 46override CODEFLAGS += -v 47override COVERAGEFLAGS += -v 48endif 49ifdef EXEC 50override TESTFLAGS += --exec="$(EXEC)" 51endif 52ifdef BUILDDIR 53override TESTFLAGS += --build-dir="$(BUILDDIR:/=)" 54override CODEFLAGS += --build-dir="$(BUILDDIR:/=)" 55endif 56ifneq ($(NM),nm) 57override CODEFLAGS += --nm-tool="$(NM)" 58endif 59 60 61# commands 62.PHONY: all build 63all build: $(TARGET) 64 65.PHONY: asm 66asm: $(ASM) 67 68.PHONY: size 69size: $(OBJ) 70 $(SIZE) -t $^ 71 72.PHONY: tags 73tags: 74 $(CTAGS) --totals --c-types=+p $(shell find -H -name '*.h') $(SRC) 75 76.PHONY: code 77code: $(OBJ) 78 ./scripts/code.py $^ $(CODEFLAGS) 79 80.PHONY: test 81test: 82 ./scripts/test.py $(TESTFLAGS) 83.SECONDEXPANSION: 84test%: tests/test$$(firstword $$(subst \#, ,%)).toml 85 ./scripts/test.py $@ $(TESTFLAGS) 86 87.PHONY: coverage 88coverage: 89 ./scripts/coverage.py $(BUILDDIR)tests/*.toml.info $(COVERAGEFLAGS) 90 91# rules 92-include $(DEP) 93.SUFFIXES: 94 95$(BUILDDIR)lfs: $(OBJ) 96 $(CC) $(CFLAGS) $^ $(LFLAGS) -o $@ 97 98$(BUILDDIR)%.a: $(OBJ) 99 $(AR) rcs $@ $^ 100 101$(BUILDDIR)%.o: %.c 102 $(CC) -c -MMD $(CFLAGS) $< -o $@ 103 104$(BUILDDIR)%.s: %.c 105 $(CC) -S $(CFLAGS) $< -o $@ 106 107# clean everything 108.PHONY: clean 109clean: 110 rm -f $(TARGET) 111 rm -f $(OBJ) 112 rm -f $(DEP) 113 rm -f $(ASM) 114 rm -f $(BUILDDIR)tests/*.toml.* 115