|
| 1 | +# are we using clang? |
| 2 | +ISCLANG := $(shell if $(CC) --version | grep -e 'LLVM\|clang' >/dev/null; then echo 1; else echo 0; fi) |
| 3 | + |
| 4 | +CFLAGS := -std=gnu11 -W -Wall -Wshadow -g $(DEFS) $(CFLAGS) |
| 5 | +CXXFLAGS := -std=gnu++1z -W -Wall -Wshadow -g $(DEFS) $(CXXFLAGS) |
| 6 | + |
| 7 | +O ?= -O3 |
| 8 | +ifeq ($(filter 0 1 2 3 s,$(O)$(NOOVERRIDEO)),$(strip $(O))) |
| 9 | +override O := -O$(O) |
| 10 | +endif |
| 11 | + |
| 12 | +ifndef SAN |
| 13 | +SAN := $(SANITIZE) |
| 14 | +endif |
| 15 | +ifndef TSAN |
| 16 | + ifeq ($(WANT_TSAN),1) |
| 17 | +TSAN := $(SAN) |
| 18 | + endif |
| 19 | +endif |
| 20 | + |
| 21 | +check_for_sanitizer = $(if $(strip $(shell $(CC) -fsanitize=$(1) -x c -E /dev/null 2>&1 | grep sanitize=)),$(info ** WARNING: The `$(CC)` compiler does not support `-fsanitize=$(1)`.),1) |
| 22 | +ifeq ($(TSAN),1) |
| 23 | + ifeq ($(or $(ISCLANG),$(filter-out 3.% 4.% 5.% 6.%,$(shell $(CC) -dumpversion)),$(filter-out Linux%,$(shell uname))),) |
| 24 | +$(info ** WARNING: If ThreadSanitizer fails, try `make SAN=1 CC=clang`.) |
| 25 | + endif |
| 26 | + ifeq ($(call check_for_sanitizer,thread),1) |
| 27 | +CFLAGS += -fsanitize=thread |
| 28 | +CXXFLAGS += -fsanitize=thread |
| 29 | + endif |
| 30 | +else |
| 31 | + ifeq ($(or $(ASAN),$(SAN)),1) |
| 32 | + ifeq ($(call check_for_sanitizer,address),1) |
| 33 | +CFLAGS += -fsanitize=address |
| 34 | +CXXFLAGS += -fsanitize=address |
| 35 | + endif |
| 36 | + endif |
| 37 | + ifeq ($(LEAKSAN),1) |
| 38 | + ifeq ($(call check_for_sanitizer,leak),1) |
| 39 | +CFLAGS += -fsanitize=leak |
| 40 | +CXXFLAGS += -fsanitize=leak |
| 41 | + endif |
| 42 | + endif |
| 43 | +endif |
| 44 | +ifeq ($(or $(UBSAN),$(SAN)),1) |
| 45 | + ifeq ($(call check_for_sanitizer,undefined),1) |
| 46 | +CFLAGS += -fsanitize=undefined |
| 47 | +CXXFLAGS += -fsanitize=undefined |
| 48 | + endif |
| 49 | +endif |
| 50 | + |
| 51 | +# these rules ensure dependencies are created |
| 52 | +DEPCFLAGS = -MD -MF $(DEPSDIR)/$*.d -MP |
| 53 | +DEPSDIR := .deps |
| 54 | +BUILDSTAMP := $(DEPSDIR)/rebuildstamp |
| 55 | +DEPFILES := $(wildcard $(DEPSDIR)/*.d) |
| 56 | +ifneq ($(DEPFILES),) |
| 57 | +include $(DEPFILES) |
| 58 | +endif |
| 59 | + |
| 60 | +# when the C compiler or optimization flags change, rebuild all objects |
| 61 | +ifneq ($(strip $(DEP_CC)),$(strip $(CC) $(CPPFLAGS) $(CFLAGS) $(O))) |
| 62 | +DEP_CC := $(shell mkdir -p $(DEPSDIR); echo >$(BUILDSTAMP); echo "DEP_CC:=$(CC) $(CPPFLAGS) $(CFLAGS) $(O)" >$(DEPSDIR)/_cc.d) |
| 63 | +endif |
| 64 | +ifneq ($(strip $(DEP_CXX)),$(strip $(CXX) $(CPPFLAGS) $(CXXFLAGS) $(O))) |
| 65 | +DEP_CXX := $(shell mkdir -p $(DEPSDIR); echo >$(BUILDSTAMP); echo "DEP_CXX:=$(CXX) $(CPPFLAGS) $(CXXFLAGS) $(O)" >$(DEPSDIR)/_cxx.d) |
| 66 | +endif |
| 67 | + |
| 68 | + |
| 69 | +V = 0 |
| 70 | +ifeq ($(V),1) |
| 71 | +run = $(1) $(3) |
| 72 | +xrun = /bin/echo "$(1) $(3)" && $(1) $(3) |
| 73 | +else |
| 74 | +run = @$(if $(2),/bin/echo " $(2) $(3)" &&,) $(1) $(3) |
| 75 | +xrun = $(if $(2),/bin/echo " $(2) $(3)" &&,) $(1) $(3) |
| 76 | +endif |
| 77 | +runquiet = @$(1) $(3) |
| 78 | + |
| 79 | +# cancel implicit rules we don't want |
| 80 | +%: %.c |
| 81 | +%.o: %.c |
| 82 | +%: %.cc |
| 83 | +%.o: %.cc |
| 84 | +%: %.o |
| 85 | + |
| 86 | +$(BUILDSTAMP): |
| 87 | + @mkdir -p $(@D) |
| 88 | + @echo >$@ |
| 89 | + |
| 90 | +always: |
| 91 | + @: |
| 92 | + |
| 93 | +clean-hook: |
| 94 | + @: |
| 95 | + |
| 96 | +.PHONY: always clean-hook |
| 97 | +.PRECIOUS: %.o |
0 commit comments