summaryrefslogtreecommitdiff
path: root/Makefile
blob: cb8b82462bce21f7458b9cde3d4453f558a042b9 (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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
OPTIMIZE ?= -O3 -flto -march=native
DEBUG ?= -g

AR ?= ar
CXX ?= g++
IWYU ?= iwyu
CHRONIC ?= chronic

PKGS := fmt
PKG_FLAGS := $(shell pkg-config --cflags ${PKGS})
PKG_LIBS := -Wl,--push-state,--as-needed,--start-group $(shell pkg-config --libs ${PKGS}) -Wl,--end-group,--pop-state

CXXFLAGS := -Wall -Werror -std=c++20 -fPIC -iquote . ${PKG_FLAGS} ${OPTIMIZE} ${DEBUG}

.DEFAULT_GOAL := all

ifneq ($(shell which ${IWYU}),)
iwyu = ${CHRONIC} ${IWYU} -Xiwyu --error -Xiwyu --mapping_file=tools/iwyu.imp -Xiwyu --no_fwd_decls ${CXXFLAGS} $(1)
else
$(warning Not using IWYU)
iwyu =
endif

libname = $(shell realpath --canonicalize-missing --relative-to . build/$(dir $(1))/lib$(notdir $(1)))

define mksodeps =
$(1)_SODEPS :=
endef

define mklib =

ifneq ($(wildcard $(1)/*.cpp),)

$(call libname,$(1).a): $(patsubst %.cpp,build/%.o,$(wildcard $(1)/*.cpp))
	@mkdir -p $$(dir $$@)
	$${AR} cr $$@ $$+

$(call libname,$(1).so): $(patsubst %.cpp,build/%.o,$(wildcard $(1)/*.cpp)) $${$(1)_SODEPS}
	@mkdir -p $$(dir $$@)
	$${CXX} $${CXXFLAGS} -shared -o $$@ -Wl,--start-group $$+ -Wl,--end-group

PARTARS += $(call libname,$(1).a)
PARTSOS += $(call libname,$(1).so)

endif

ifdef iwyu

build/$(1)/iwyu.cpp: $$(wildcard $(1)/*.h)
	@mkdir -p $$(dir $$@)
	@tools/iwyu-header $$(wildcard $(1)/*.h) > $$@
	@$$(call iwyu,$$@)

IWYU_CPPS += build/$(1)/iwyu.cpp

endif

endef

PARTS := $(patsubst ./%,%,$(shell find -mindepth 1 -type d -\( -name .\* -prune -o -name build -prune -o -name tools -prune -o -print -\)))

PARTARS :=
PARTSOS :=
IWYU_CPPS :=

$(foreach part,${PARTS},$(eval $(call mksodeps,${part})))

DEPFILES := $(shell find -mindepth 1 -name .\* -prune -o -type f -name \*.d -print)
include ${DEPFILES}

$(foreach part,${PARTS},$(eval $(call mklib,${part})))

ifdef iwyu

build/iwyu.cpp: $(wildcard *.h)
	@mkdir -p $(dir $@)
	@tools/iwyu-header $(wildcard *.h) > $@
	@$(call iwyu,$@)

IWYU_CPPS += build/iwyu.cpp

endif

MAINOBJS := $(patsubst %.cpp,build/%.o,$(wildcard *.cpp))
MAINOBJS += build/git-tag.o

all: ${IWYU_CPPS} issim issim-static

issim: build/issim-dynamic
	@ln -sf $< $@
.PHONY: issim

issim-static: build/issim-static
	@ln -sf $< $@
.PHONY: issim-static

build/issim-dynamic: ${MAINOBJS} ${PARTSOS}
	@mkdir -p $(dir $@)
	${CXX} ${CXXFLAGS} -o $@ -Wl,--start-group $+ -Wl,--end-group ${PKG_LIBS}

build/issim-static: ${MAINOBJS} ${PARTARS}
	@mkdir -p $(dir $@)
	${CXX} ${CXXFLAGS} -o $@ -Wl,--start-group $+ -Wl,--end-group ${PKG_LIBS}

clean:
	rm -rf build issim issim-static
.PHONY: clean

build/git-tag.cpp:
	@mkdir -p $(dir $@)
	@tools/mkgit.rb > $@
.PHONY: build/git-tag.cpp

build/%.o: %.cpp
	@mkdir -p $(dir $@)
	@$(call iwyu,$<)
	${CXX} ${CXXFLAGS} -MMD -c -o $@ $<

build/%.o: build/%.cpp
	@mkdir -p $(dir $@)
	${CXX} ${CXXFLAGS} -MMD -c -o $@ $<

.SUFFIXES:
.SECONDARY: