aboutsummaryrefslogtreecommitdiff
path: root/Makefile
blob: 6a4bf4f72695ab07c3a437b954d9e95a6ee5fc12 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
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
<<targets>> := debug release
# addons
with     ?= gmp
without  ?=
<<addons>> := gmp

# Echo function
<< := echo
ifneq ($(shell eval 'echo -e'),-e)
	<< += -e
endif

<<debug-dir>   = Debug
<<release-dir>>= Release
<<target>>     = $(firstword $(filter $(<<targets>>),$(target)))
ifeq ($(<<target>>),release)
	<<tdir>> = Release
else
	<<tdir>> = Debug
endif

# optional addons
<<with>>       = $(sort $(filter-out $(without),$(filter $(<<addons>>),$(with))))
# project configs
CXXFLAGS += -I$(PWD)/headers -I$(PWD)/

# rebuild on optional deps change
ifneq ($(filter gmp,$(<<with>>)),)
	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

<<sources>> := \
	main.cpp \
	headers/allElements.cpp \
	headers/readElementState.cpp


<<objects>> := $(<<sources>>:%=obj/$(<<tdir>>)/%.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

<<bindep>> = bin/$(<<tdir>>)/.$(BINARY).dep
<<binpth>> = bin/$(<<tdir>>)/$(BINARY)
<<->> := $(shell mkdir -p $(dir $(<<binpth>>)))
# Use $(<<bindep>>) file to track backend change
<<depends>> = $(sort $(<<with>>))
ifneq ($(sort $(file < $(<<bindep>>))),$(<<depends>>))
	<<null>> := $(file  > $(<<bindep>>),$(<<depends>>))
endif
undefine <<depends>> <<null>> <<->>
# using some makefile sorcery

all: $(<<binpth>>)

obj/$(<<tdir>>)/%.o: %
	@$(<<) "  CXX\t" $(<:src/%=%)
	@mkdir -p $(shell dirname $(@))
	@$(CXX) -c $(<) -o $(@) $(CXXFLAGS)

$(<<binpth>>): $(<<objects>>)
	@if test -n "$(<<with>>)"; then $(<<) "WITH\t" $(<<with>>);fi
	@$(<<) "LINK\t" "$(BINARY)$(^:obj/%=\\n\\t + %)"
	@$(CXX) $(^) -o $(@) $(LDFLAGS)

install: $(<<binpth>>)
	install -Dm0755 $(<) $(DESTDIR)$(PREFIX)/$(BINDIR)/$(BINARY)

uninstall:
	sudo $(RM) -f $(DESTDIR)$(PREFIX)/$(BINDIR)/$(BINARY)

clean:
	@$(<<) "  RM\t" "$(BINARY)$(<<objects>>:obj/%=\\n\\t + %)"
	@$(RM) -f $(<<objects>>) $(<<binpth>>) $(<<bindep>>)

test: $(BINARY)
	./$(BINARY)

$(<<objects>>): $(<<bindep>>)
.PHONY: all clean install uninstall test