# if $CXX is not set, guess default `cxx'. It has to be in system CXX ?= cxx # compile options CXXFLAGS ?= -Os -Wall -Wpedantic -Wextra # link options LDFLAGS ?= -s target ?= debug <> := debug release # addons with ?= gmp without ?= <> := gmp # Echo function << := echo ifneq ($(shell eval 'echo -e'),-e) << += -e endif < = Debug <>= Release <> = $(firstword $(filter $(<>),$(target))) ifeq ($(<>),release) <> = Release else <> = Debug endif # optional addons <> = $(sort $(filter-out $(without),$(filter $(<>),$(with)))) # project configs CXXFLAGS += -I$(PWD)/headers -I$(PWD)/ # rebuild on optional deps change ifneq ($(filter gmp,$(<>)),) ifneq ($(shell pkg-config --exists gmp gmpxx && echo exists),) PKGS += gmpxx gmp CFLAGS += -D_USE_GMP endif endif ifneq ($(PKGS),) CXXFLAGS += $(shell pkg-config --cflags $(PKGS)) LDFLAGS += $(shell pkg-config --libs $(PKGS)) endif <> := \ main.cpp \ headers/allElements.cpp \ headers/readElementState.cpp <> := $(<>:%=obj/$(<>)/%.o) # Output file name ifeq ($(filter $(shell uname -o),Msys Cygwin),) BINARY ?= entropy-calculator else ifeq ($(MSYSTEM_CARCH),i686) BINARY ?= entropy-calculator.exe else BINARY ?= entropy-calculator64.exe endif endif <> = bin/$(<>)/.$(BINARY).dep <> = bin/$(<>)/$(BINARY) <<->> := $(shell mkdir -p $(dir $(<>))) # Use $(<>) file to track backend change <> = $(sort $(<>)) ifneq ($(sort $(file < $(<>))),$(<>)) <> := $(file > $(<>),$(<>)) endif undefine <> <> <<->> # using some makefile sorcery all: $(<>) obj/$(<>)/%.o: % @$(<<) " CXX\t" $(<:src/%=%) @mkdir -p $(shell dirname $(@)) @$(CXX) -c $(<) -o $(@) $(CXXFLAGS) $(<>): $(<>) @if test -n "$(<>)"; then $(<<) "WITH\t" $(<>);fi @$(<<) "LINK\t" "$(BINARY)$(^:obj/%=\\n\\t + %)" @$(CXX) $(^) -o $(@) $(LDFLAGS) install: $(<>) install -Dm0755 $(<) $(DESTDIR)$(PREFIX)/$(BINDIR)/$(BINARY) uninstall: sudo $(RM) -f $(DESTDIR)$(PREFIX)/$(BINDIR)/$(BINARY) clean: @$(<<) " RM\t" "$(BINARY)$(<>:obj/%=\\n\\t + %)" @$(RM) -f $(<>) $(<>) $(<>) test: $(BINARY) ./$(BINARY) $(<>): $(<>) .PHONY: all clean install uninstall test