From 6658af9911170425e918b49b0ebd83ad0d04ad7f Mon Sep 17 00:00:00 2001 From: Mubashshir Date: Wed, 13 Mar 2024 18:54:36 +0600 Subject: Create makefile Signed-off-by: Mubashshir --- Makefile | 104 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 104 insertions(+) create mode 100644 Makefile diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..6a4bf4f --- /dev/null +++ b/Makefile @@ -0,0 +1,104 @@ +# 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 -- cgit v1.2.3